source: branches/version-2_11-dev/data/class/SC_FormParam.php @ 21099

Revision 21099, 17.5 KB checked in by Seasoft, 13 years ago (diff)

#1423 (SC_FormParam#getValue 第2引数の指定時の処理、戻り値が配列の場合を考慮していない)
#1294 (ソースを読みやすくする)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 */
23
24/**
25 * パラメーター管理クラス
26 *
27 * :XXX: addParam と setParam で言う「パラメーター」が用語として競合しているように感じる。(2009/10/17 Seasoft 塚田)
28 *
29 * @package SC
30 * @author LOCKON CO.,LTD.
31 */
32class SC_FormParam {
33
34    var $param;
35    var $disp_name;
36    var $keyname;
37    var $length;
38    var $convert;
39    var $arrCheck;
40    /** 何も入力されていないときに表示する値 */
41    var $default;
42    /** DBにそのまま挿入可能か否か */
43    var $input_db;
44    var $html_disp_name;
45
46    // コンストラクタ
47    function SC_FormParam() {
48        $this->check_dir = IMAGE_SAVE_REALDIR;
49        $this->initParam();
50    }
51
52    /**
53     * パラメーターの初期化
54     *
55     * @return void
56     */
57    function initParam() {
58        $this->disp_name = array();
59        $this->keyname = array();
60        $this->length = array();
61        $this->convert = array();
62        $this->arrCheck = array();
63        $this->default = array();
64        $this->input_db = array();
65    }
66
67    // パラメーターの追加
68    function addParam($disp_name, $keyname, $length = "", $convert = "", $arrCheck = array(), $default = "", $input_db = 'true') {
69        $this->disp_name[] = $disp_name;
70        $this->keyname[] = $keyname;
71        $this->length[] = $length;
72        $this->convert[] = $convert;
73        $this->arrCheck[] = $arrCheck;
74        $this->default[] = $default;
75        $this->input_db[] = $input_db;
76    }
77
78    // パラメーターの入力
79    // $arrVal  :$arrVal['keyname']・・の配列を一致したキーのインスタンスに格納する
80    // $seq     :trueの場合、$arrVal[0]~の配列を登録順にインスタンスに格納する
81    function setParam($arrVal, $seq = false) {
82        $cnt = 0;
83        if(!$seq){
84            foreach($this->keyname as $val) {
85                if(isset($arrVal[$val])) {
86                    $this->setValue($val, $arrVal[$val]);
87                }
88            }
89        } else {
90            foreach($this->keyname as $val) {
91                $this->param[$cnt] = $arrVal[$cnt];
92                $cnt++;
93            }
94        }
95    }
96
97    // 画面表示用タイトル生成
98    function setHtmlDispNameArray() {
99        $cnt = 0;
100        foreach($this->keyname as $val) {
101            $find = false;
102            foreach($this->arrCheck[$cnt] as $val) {
103                if($val == "EXIST_CHECK") {
104                    $find = true;
105                }
106            }
107
108            if($find) {
109                $this->html_disp_name[$cnt] = $this->disp_name[$cnt] . '<span class="red">(※ 必須)</span>';
110            } else {
111                $this->html_disp_name[$cnt] = $this->disp_name[$cnt];
112            }
113            if($this->default[$cnt] != "") {
114                $this->html_disp_name[$cnt] .= ' [省略時初期値: ' . $this->default[$cnt] . ']';
115            }
116            if($this->input_db[$cnt] == false) {
117                $this->html_disp_name[$cnt] .= ' [登録・更新不可] ';
118            }
119            $cnt++;
120        }
121    }
122
123    // 画面表示用タイトル取得
124    function getHtmlDispNameArray() {
125        return $this->html_disp_name;
126    }
127
128    // 複数列パラメーターの取得
129    function setParamList($arrVal, $keyname) {
130        // DBの件数を取得する。
131        $count = count($arrVal);
132        $no = 1;
133        for($cnt = 0; $cnt < $count; $cnt++) {
134            $key = $keyname.$no;
135            if($arrVal[$cnt][$keyname] != "") {
136                $this->setValue($key, $arrVal[$cnt][$keyname]);
137            }
138            $no++;
139        }
140    }
141
142    function setDBDate($db_date, $year_key = 'year', $month_key = 'month', $day_key = 'day') {
143
144        if (!empty($db_date)) {
145            list($y, $m, $d) = preg_split("/[- ]/", $db_date);
146            $this->setValue($year_key, $y);
147            $this->setValue($month_key, $m);
148            $this->setValue($day_key, $d);
149        }
150    }
151
152    // キーに対応した値をセットする。
153    function setValue($key, $param) {
154        $cnt = 0;
155        foreach($this->keyname as $val) {
156            if($val == $key) {
157                $this->param[$cnt] = $param;
158                // 複数一致の場合もあるので break してはいけない。
159            }
160            $cnt++;
161        }
162    }
163
164    function toLower($key) {
165        $cnt = 0;
166        foreach($this->keyname as $val) {
167            if($val == $key) {
168                $this->param[$cnt] = strtolower($this->param[$cnt]);
169                // 複数一致の場合もあるので break してはいけない。
170            }
171            $cnt++;
172        }
173    }
174
175    // エラーチェック
176    function checkError($br = true) {
177        $objErr->arrErr = array();
178
179        $cnt = 0;
180        foreach($this->keyname as $val) {
181            foreach($this->arrCheck[$cnt] as $func) {
182                if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
183                switch($func) {
184                case 'EXIST_CHECK':
185                case 'NUM_CHECK':
186                case 'EMAIL_CHECK':
187                case 'EMAIL_CHAR_CHECK':
188                case 'ALNUM_CHECK':
189                case 'GRAPH_CHECK':
190                case 'KANA_CHECK':
191                case 'URL_CHECK':
192                case 'IP_CHECK':
193                case 'SPTAB_CHECK':
194                case 'ZERO_CHECK':
195                case 'ALPHA_CHECK':
196                case 'ZERO_START':
197                case 'FIND_FILE':
198                case 'NO_SPTAB':
199                case 'DIR_CHECK':
200                case 'DOMAIN_CHECK':
201                case 'FILE_NAME_CHECK':
202                case 'MOBILE_EMAIL_CHECK':
203                case 'MAX_LENGTH_CHECK':
204                case 'MIN_LENGTH_CHECK':
205                case 'NUM_COUNT_CHECK':
206                case 'KANABLANK_CHECK':
207                case 'SELECT_CHECK':
208                case 'FILE_NAME_CHECK_BY_NOUPLOAD':
209                    $this->recursionCheck($this->disp_name[$cnt], $func,
210                                          $this->param[$cnt], $objErr->arrErr,
211                                          $val, $this->length[$cnt]);
212                    break;
213                // 小文字に変換
214                case 'CHANGE_LOWER':
215                    $this->param[$cnt] = strtolower($this->param[$cnt]);
216                    break;
217                // ファイルの存在チェック
218                case 'FILE_EXISTS':
219                    if($this->param[$cnt] != "" && !file_exists($this->check_dir . $this->param[$cnt])) {
220                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
221                    }
222                    break;
223                // ダウンロード用ファイルの存在チェック
224                case 'DOWN_FILE_EXISTS':
225                    if($this->param[$cnt] != "" && !file_exists(DOWN_SAVE_REALDIR . $this->param[$cnt])) {
226                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
227                    }
228                    break;
229                default:
230                    $objErr->arrErr[$val] = "※※ エラーチェック形式($func)には対応していません ※※ <br>";
231                    break;
232                }
233            }
234
235            if (isset($objErr->arrErr[$val]) && !$br) {
236                $objErr->arrErr[$val] = ereg_replace("<br>$", "", $objErr->arrErr[$val]);
237            }
238            $cnt++;
239        }
240        return $objErr->arrErr;
241    }
242
243    /**
244     * SC_CheckError::doFunc() を再帰的に実行する.
245     *
246     * 再帰実行した場合は, エラーメッセージを多次元配列で格納する
247     *
248     * TODO 二次元以上のエラーメッセージへの対応
249     *
250     * @param string $disp_name 表示名
251     * @param string $func チェック種別
252     * @param mixed $value チェック対象の値. 配列の場合は再帰的にチェックする.
253     * @param array $arrErr エラーメッセージを格納する配列
254     * @param string $error_key エラーメッセージを格納する配列のキー
255     * @param integer $length チェック対象の値の長さ
256     * @param integer $depth 再帰実行した場合の深度
257     * @param integer $recursion_count 再帰実行した回数
258     * @return void
259     */
260    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key,
261                            $length = 0, $depth = 0, $recursion_count = 0) {
262        if (is_array($value)) {
263            $depth++;
264            $recursion_count = 0;
265            foreach ($value as $in) {
266                $this->recursionCheck($disp_name, $func, $in, $arrErr, $error_key,
267                                      $length, $depth, $recursion_count);
268                $recursion_count++;
269            }
270        } else {
271            $objErr = new SC_CheckError_Ex(array(0 => $value));
272            $objErr->doFunc(array($disp_name, 0, $length), array($func));
273            if (!SC_Utils_Ex::isBlank($objErr->arrErr)) {
274                foreach($objErr->arrErr as $message) {
275
276                    if(!SC_Utils_Ex::isBlank($message)) {
277                        // 再帰した場合は多次元配列のエラーメッセージを生成
278                        $error_var = '$arrErr[$error_key]';
279                        for ($i = 0; $i < $depth; $i++) {
280                            // FIXME 二次元以上の対応
281                            $error_var .= '[' . $recursion_count . ']';
282                        }
283                        eval($error_var . ' = $message;');
284                    }
285                }
286            }
287        }
288    }
289
290    /**
291     * フォームの入力パラメーターに応じて, 再帰的に mb_convert_kana 関数を実行する.
292     *
293     * @return voi
294     * @see mb_convert_kana
295     */
296    function convParam() {
297        $cnt = 0;
298        foreach ($this->keyname as $val) {
299            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
300            $this->recursionConvParam($this->param[$cnt], $this->convert[$cnt]);
301            $cnt++;
302        }
303    }
304
305    /**
306     * 再帰的に mb_convert_kana を実行する.
307     *
308     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
309     * @param string $convert mb_convert_kana の変換オプション
310     */
311    function recursionConvParam(&$value, $convert) {
312        if (is_array($value)) {
313            foreach (array_keys($value) as $key) {
314                $this->recursionConvParam($value[$key], $convert);
315            }
316        } else {
317            if (!SC_Utils_Ex::isBlank($value)) {
318                $value = mb_convert_kana($value, $convert);
319            }
320        }
321    }
322
323    /**
324     * 連想配列で返す
325     *
326     * @param array $arrKey 対象のキー
327     * @return array 連想配列
328     */
329    function getHashArray($arrKey = array()) {
330        $arrRet = array();
331        foreach($this->keyname as $index => $keyname) {
332            if (empty($arrKey) || in_array($keyname, $arrKey)) {
333                $arrRet[$keyname] = isset($this->param[$index]) ? $this->param[$index] : '';
334            }
335        }
336        return $arrRet;
337    }
338
339    // DB格納用配列の作成
340    function getDbArray() {
341        $cnt = 0;
342        foreach ($this->keyname as $val) {
343            if ($this->input_db[$cnt]) {
344                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
345            }
346            $cnt++;
347        }
348        return $arrRet;
349    }
350
351    /**
352     * 配列の縦横を入れ替えて返す
353     *
354     * @param array $arrKey 対象のキー
355     * @return array 縦横を入れ替えた配列
356     */
357    function getSwapArray($arrKey = array()) {
358        $arrTmp = $this->getHashArray($arrKey);
359        return SC_Utils_Ex::sfSwapArray($arrTmp);
360    }
361
362    // 項目名一覧の取得
363    function getTitleArray() {
364        return $this->disp_name;
365    }
366
367    // 項目数を返す
368    function getCount() {
369        $count = count($this->keyname);
370        return $count;
371    }
372
373    // フォームに渡す用のパラメーターを返す
374    function getFormParamList() {
375        $cnt = 0;
376        foreach($this->keyname as $val) {
377
378            // キー名
379            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
380            // 文字数制限
381            $arrRet[$val]['length'] = $this->length[$cnt];
382            // 入力値
383            if (isset($this->param[$cnt])) {
384                $arrRet[$val]['value'] = $this->param[$cnt];
385            }
386
387            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
388
389            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
390                $arrRet[$val]['value'] = $this->default[$cnt];
391            }
392
393            $cnt++;
394        }
395        return $arrRet;
396    }
397
398    /**
399     * キー名の一覧を返す
400     *
401     * @return array キー名の一覧
402     */
403    function getKeyList() {
404        return $this->keyname;
405    }
406
407    // キー名と一致した値を返す
408    function getValue($keyname, $default = '') {
409        $cnt = 0;
410        $ret = null;
411        foreach($this->keyname as $val) {
412            if($val == $keyname) {
413                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
414                break;
415            }
416            $cnt++;
417        }
418
419        if (is_array($ret)) {
420            foreach (array_keys($ret) as $key) {
421                if (SC_Utils_Ex::isBlank($ret[$key])) {
422                    $ret[$key] = $default;
423                }
424            }
425        } else {
426            if (SC_Utils_Ex::isBlank($ret)) {
427                $ret = $default;
428            }
429        }
430        return $ret;
431    }
432
433    /**
434     * @deprecated
435     */
436    function splitParamCheckBoxes($keyname) {
437        $cnt = 0;
438        foreach($this->keyname as $val) {
439            if($val == $keyname) {
440                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
441                    $this->param[$cnt] = explode("-", $this->param[$cnt]);
442                }
443            }
444            $cnt++;
445        }
446    }
447
448    /**
449     * 入力パラメーターの先頭及び末尾にある空白文字を削除する.
450     *
451     * @param boolean $has_wide_space 全角空白も削除する場合 true
452     * @return void
453     */
454    function trimParam($has_wide_space = true) {
455        $cnt = 0;
456        foreach ($this->keyname as $val) {
457            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
458            $this->recursionTrim($this->param[$cnt], $has_wide_space);
459            $cnt++;
460        }
461    }
462
463    /**
464     * 再帰的に入力パラメーターの先頭及び末尾にある空白文字を削除する.
465     *
466     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
467     * @param boolean $has_wide_space 全角空白も削除する場合 true
468     * @return void
469     */
470    function recursionTrim(&$value, $has_wide_space = true) {
471        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
472        if (is_array($value)) {
473            foreach (array_keys($value) as $key) {
474                $this->recursionTrim($value[$key], $convert);
475            }
476        } else {
477            if (!SC_Utils_Ex::isBlank($value)) {
478                if ($has_wide_space) {
479                    $value = preg_replace($pattern, '$1', $value);
480                }
481                $value = trim($value);
482            }
483        }
484    }
485
486    /**
487     * 検索結果引き継ぎ用の連想配列を取得する.
488     *
489     * 引数で指定した文字列で始まるパラメーター名の入力値を連想配列で取得する.
490     *
491     * @param string $prefix パラメーター名の接頭辞
492     * @return array 検索結果引き継ぎ用の連想配列.
493     */
494    function getSearchArray($prefix = 'search_') {
495        $cnt = 0;
496        $arrResults = array();
497        foreach ($this->keyname as $key) {
498            if (preg_match('/^' . $prefix . '/', $key)) {
499                $arrResults[$key] = isset($this->param[$cnt])
500                    ? $this->param[$cnt] : "";
501            }
502            $cnt++;
503        }
504        return $arrResults;
505    }
506
507
508    // addParam の内容をそのまま返す
509    function getFormDispArray() {
510        $cnt = 0;
511        foreach($this->keyname as $val) {
512            // キー名
513            $arrRet[$cnt]['keyname'] = $this->keyname[$cnt];
514            // 文字数制限
515            $arrRet[$cnt]['length'] = $this->length[$cnt];
516
517            $arrRet[$cnt]['disp_name']  = $this->disp_name[$cnt];
518            // 入力値
519            if (isset($this->param[$cnt])) {
520                $arrRet[$cnt]['value'] = $this->param[$cnt];
521            }
522
523            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
524
525            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
526                $arrRet[$cnt]['value'] = $this->default[$cnt];
527            }
528            $cnt++;
529        }
530        return $arrRet;
531    }
532
533
534}
535?>
Note: See TracBrowser for help on using the repository browser.