source: branches/version-2_12-multilang/data/class/SC_FormParam.php @ 22496

Revision 22496, 19.6 KB checked in by m_uehara, 11 years ago (diff)

#2084 メッセージIDの振り直し

  • 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-2012 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    /**
35     * 何も入力されていないときに表示する値
36     * キーはキー名
37     */
38    var $arrValue = array();
39
40    /** 表示名 */
41    var $disp_name = array();
42
43    /** キー名 */
44    var $keyname = array();
45
46    var $length = array();
47    var $convert = array();
48    var $arrCheck = array();
49
50    /**
51     * 何も入力されていないときに表示する値
52     * キーはキー名
53     */
54    var $arrDefault = array();
55
56    /** DBにそのまま挿入可能か否か */
57    var $input_db = array();
58
59    var $html_disp_name = array();
60
61    /**
62     * コンストラクタ
63     */
64    function __construct() {
65        $this->check_dir = IMAGE_SAVE_REALDIR;
66
67        // SC_FormParamのフックポイント
68        // TODO: debug_backtrace以外にいい方法があれば良いが、一旦これで
69        $backtraces = debug_backtrace();
70        // 呼び出し元のクラスを取得
71        $class = $backtraces[1]['class'];
72        $objPage = $backtraces[1]['object'];
73        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($objPage->plugin_activate_flg);
74        if (is_object($objPlugin)) {
75            $objPlugin->doAction('SC_FormParam_construct', array($class, $this));
76        }
77    }
78
79    /**
80     * 前方互換用
81     *
82     * @deprecated 2.12.0 #1702
83     */
84    function initParam() {
85        $this->disp_name = array();
86        $this->keyname = array();
87        $this->length = array();
88        $this->convert = array();
89        $this->arrCheck = array();
90        $this->arrDefault = array();
91        $this->input_db = array();
92    }
93
94    // パラメーターの追加
95    function addParam($disp_name, $keyname, $length = '', $convert = '', $arrCheck = array(), $default = '', $input_db = true) {
96        $this->disp_name[] = $disp_name;
97        $this->keyname[] = $keyname;
98        $this->length[] = $length;
99        $this->convert[] = $convert;
100        $this->arrCheck[] = $arrCheck;
101        // XXX このタイミングで arrValue へ格納するほうがスマートかもしれない。しかし、バリデーションや変換の対象となるので、その良し悪しは気になる。
102        $this->arrDefault[$keyname] = $default;
103        $this->input_db[] = $input_db;
104    }
105
106    // パラメーターの入力
107    // $arrVal  :$arrVal['keyname']・・の配列を一致したキーのインスタンスに格納する
108    // $seq     :trueの場合、$arrVal[0]~の配列を登録順にインスタンスに格納する
109    function setParam($arrVal, $seq = false) {
110        if (!is_array($arrVal)) return;
111        if (!$seq) {
112            foreach ($arrVal as $key => $val) {
113                $this->setValue($key, $val);
114            }
115        } else {
116            foreach ($this->keyname as $index => $key) {
117                $this->setValue($key, $arrVal[$index]);
118            }
119        }
120    }
121
122    // 画面表示用タイトル生成
123    function setHtmlDispNameArray() {
124        foreach ($this->keyname as $index => $key) {
125            $find = false;
126            foreach ($this->arrCheck[$index] as $val) {
127                if ($val == 'EXIST_CHECK') {
128                    $find = true;
129                }
130            }
131
132            if ($find) {
133                $this->html_disp_name[$index] = t("c_T_ARG1<span class='red'>(* Required)</span>_01", array('T_ARG1' => $this->disp_name[$index]));
134            } else {
135                $this->html_disp_name[$index] = $this->disp_name[$index];
136            }
137            if ($this->arrDefault[$key] != '') {
138                $this->html_disp_name[$index] .= t('c_[Default value: T_ARG1]_01', array('T_ARG1' => $this->arrDefault[$key]));
139            }
140            if ($this->input_db[$index] == false) {
141                $this->html_disp_name[$index] .= t('c_ [Registration/update not possible] _01');
142            }
143        }
144    }
145
146    // 画面表示用タイトル取得
147    function getHtmlDispNameArray() {
148        return $this->html_disp_name;
149    }
150
151    // 複数列パラメーターの取得
152    function setParamList($arrVal2d, $keyname) {
153        // DBの件数を取得する。
154        $no = 1;
155        foreach ($arrVal2d as $arrVal) {
156            $key = $keyname . $no;
157            $this->setValue($key, $arrVal[$keyname]);
158            $no++;
159        }
160    }
161
162    function setDBDate($db_date, $year_key = 'year', $month_key = 'month', $day_key = 'day') {
163        if (empty($db_date)) {
164            return;
165        }
166        list($y, $m, $d) = preg_split('/[- ]/', $db_date);
167        $this->setValue($year_key, $y);
168        $this->setValue($month_key, $m);
169        $this->setValue($day_key, $d);
170    }
171
172    // キーに対応した値をセットする。
173    function setValue($key, $value) {
174        if (!in_array($key, $this->keyname)) {
175            // TODO 警告発生
176            return;
177        }
178        $this->arrValue[$key] = $value;
179    }
180
181    function toLower($key) {
182        if (isset($this->arrValue[$key])) {
183            $this->arrValue[$key] = strtolower($this->arrValue[$key]);
184        }
185    }
186
187    // エラーチェック
188    function checkError($br = true) {
189        $arrErr = array();
190
191        foreach ($this->keyname as $index => $key) {
192            foreach ($this->arrCheck[$index] as $func) {
193                $value = $this->getValue($key);
194                switch ($func) {
195                    case 'EXIST_CHECK':
196                    case 'NUM_CHECK':
197                    case 'EMAIL_CHECK':
198                    case 'EMAIL_CHAR_CHECK':
199                    case 'ALNUM_CHECK':
200                    case 'GRAPH_CHECK':
201                    case 'KANA_CHECK':
202                    case 'URL_CHECK':
203                    case 'IP_CHECK':
204                    case 'SPTAB_CHECK':
205                    case 'ZERO_CHECK':
206                    case 'ALPHA_CHECK':
207                    case 'ZERO_START':
208                    case 'FIND_FILE':
209                    case 'NO_SPTAB':
210                    case 'DIR_CHECK':
211                    case 'DOMAIN_CHECK':
212                    case 'FILE_NAME_CHECK':
213                    case 'MOBILE_EMAIL_CHECK':
214                    case 'MAX_LENGTH_CHECK':
215                    case 'MIN_LENGTH_CHECK':
216                    case 'NUM_COUNT_CHECK':
217                    case 'KANABLANK_CHECK':
218                    case 'SELECT_CHECK':
219                    case 'FILE_NAME_CHECK_BY_NOUPLOAD':
220                        $this->recursionCheck($this->disp_name[$index], $func,
221                            $value, $arrErr, $key, $this->length[$index]);
222                        break;
223                    // 小文字に変換
224                    case 'CHANGE_LOWER':
225                        $this->toLower($key);
226                        break;
227                    // ファイルの存在チェック
228                    case 'FILE_EXISTS':
229                        if ($value != '' && !file_exists($this->check_dir . $value)) {
230                            $arrErr[$key] = t('c_* The file T_ARG1 does not exist. <br />_01', array('T_ARG1' => $this->disp_name[$index]));
231                        }
232                        break;
233                    // ダウンロード用ファイルの存在チェック
234                    case 'DOWN_FILE_EXISTS':
235                        if ($value != '' && !file_exists(DOWN_SAVE_REALDIR . $value)) {
236                            $arrErr[$key] = t('c_* The file T_ARG1 does not exist. <br />_01', array('T_ARG1' => $this->disp_name[$index]));
237                        }
238                        break;
239                    default:
240                        $arrErr[$key] = t('c_** Does not support the error check format (T_ARG1) **<br />_01', array('T_ARG1' => $func));
241                        break;
242                }
243            }
244
245            if (isset($arrErr[$key]) && !$br) {
246                $arrErr[$key] = preg_replace("/<br(\s+\/)?>/i", '', $arrErr[$key]);
247            }
248        }
249        return $arrErr;
250    }
251
252    /**
253     * SC_CheckError::doFunc() を再帰的に実行する.
254     *
255     * 再帰実行した場合は, エラーメッセージを多次元配列で格納する
256     *
257     * TODO 二次元以上のエラーメッセージへの対応
258     *
259     * @param string $disp_name 表示名
260     * @param string $func チェック種別
261     * @param mixed $value チェック対象の値. 配列の場合は再帰的にチェックする.
262     * @param array $arrErr エラーメッセージを格納する配列
263     * @param string $error_key エラーメッセージを格納する配列のキー
264     * @param integer $length チェック対象の値の長さ
265     * @param integer $depth 再帰実行した場合の深度
266     * @param integer $error_last_key エラーメッセージを格納する配列の末端のキー
267     * @return void
268     */
269    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key,
270        $length = 0, $depth = 0, $error_last_key = null
271    ) {
272        if (is_array($value)) {
273            $depth++;
274            foreach ($value as $key => $in) {
275                $this->recursionCheck($disp_name, $func, $in, $arrErr, $error_key,
276                                      $length, $depth, $key);
277            }
278        } else {
279            $objErr = new SC_CheckError_Ex(array(0 => $value));
280            $objErr->doFunc(array($disp_name, 0, $length), array($func));
281            if (!SC_Utils_Ex::isBlank($objErr->arrErr)) {
282                foreach ($objErr->arrErr as $message) {
283
284                    if (!SC_Utils_Ex::isBlank($message)) {
285                        // 再帰した場合は多次元配列のエラーメッセージを生成
286                        $error_var = '$arrErr[$error_key]';
287                        for ($i = 0; $i < $depth; $i++) {
288                            // FIXME 二次元以上の対応
289                            $error_var .= '[' . $error_last_key . ']';
290                        }
291                        eval($error_var . ' = $message;');
292                    }
293                }
294            }
295        }
296    }
297
298    /**
299     * フォームの入力パラメーターに応じて, 再帰的に mb_convert_kana 関数を実行する.
300     *
301     * @return void
302     * @see mb_convert_kana
303     */
304    function convParam() {
305        foreach ($this->keyname as $index => $key) {
306            if (isset($this->arrValue[$key])) {
307                $this->recursionConvParam($this->arrValue[$key], $this->convert[$index]);
308            }
309        }
310    }
311
312    /**
313     * 再帰的に mb_convert_kana を実行する.
314     *
315     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
316     * @param string $convert mb_convert_kana の変換オプション
317     */
318    function recursionConvParam(&$value, $convert) {
319        if (is_array($value)) {
320            foreach ($value as $key => $val) {
321                $this->recursionConvParam($value[$key], $convert);
322            }
323        } else {
324            if (!SC_Utils_Ex::isBlank($value)) {
325                $value = mb_convert_kana($value, $convert);
326            }
327        }
328    }
329
330    /**
331     * 連想配列で返す
332     *
333     * @param array $arrKey 対象のキー
334     * @return array 連想配列
335     */
336    function getHashArray($arrKey = array()) {
337        $arrRet = array();
338        foreach ($this->keyname as $keyname) {
339            if (empty($arrKey) || in_array($keyname, $arrKey)) {
340                $arrRet[$keyname] = $this->getValue($keyname);
341            }
342        }
343        return $arrRet;
344    }
345
346    // DB格納用配列の作成
347    function getDbArray() {
348        $dbArray = array();
349        foreach ($this->keyname as $index => $key) {
350            if ($this->input_db[$index]) {
351                $dbArray[$key] = $this->getValue($key);
352            }
353        }
354        return $dbArray;
355    }
356
357    /**
358     * 配列の縦横を入れ替えて返す
359     *
360     * @param array $arrKey 対象のキー
361     * @return array 縦横を入れ替えた配列
362     */
363    function getSwapArray($arrKey = array()) {
364        $arrTmp = $this->getHashArray($arrKey);
365        return SC_Utils_Ex::sfSwapArray($arrTmp);
366    }
367
368    // 項目名一覧の取得
369    function getTitleArray() {
370        return $this->disp_name;
371    }
372
373    // 項目数を返す
374    function getCount() {
375        $count = count($this->keyname);
376        return $count;
377    }
378
379    // フォームに渡す用のパラメーターを返す
380    function getFormParamList() {
381        $formParamList = array();
382        foreach ($this->keyname as $index => $key) {
383            // キー名
384            $formParamList[$key]['keyname'] = $key;
385            // 表示名
386            $formParamList[$key]['disp_name'] = $this->disp_name[$index];
387            // 文字数制限
388            $formParamList[$key]['length'] = $this->length[$index];
389            // 入力値
390            $formParamList[$key]['value'] = $this->getValue($key);
391        }
392        return $formParamList;
393    }
394
395    /**
396     * キー名の一覧を返す
397     *
398     * @return array キー名の一覧
399     */
400    function getKeyList() {
401        return $this->keyname;
402    }
403
404    // キー名と一致した値を返す
405    function getValue($keyname, $default = '') {
406        $ret = null;
407        foreach ($this->keyname as $key) {
408            if ($key == $keyname) {
409                $ret = isset($this->arrValue[$key]) ? $this->arrValue[$key] : $this->arrDefault[$key];
410                break;
411            }
412        }
413
414        if (is_array($ret)) {
415            foreach ($ret as $key => $value) {
416                if (SC_Utils_Ex::isBlank($ret[$key])) {
417                    $ret[$key] = $default;
418                }
419            }
420        } else {
421            if (SC_Utils_Ex::isBlank($ret)) {
422                $ret = $default;
423            }
424        }
425        return $ret;
426    }
427
428    /**
429     * @deprecated
430     */
431    function splitParamCheckBoxes($keyname) {
432        foreach ($this->keyname as $key) {
433            if ($key == $keyname) {
434                if (isset($this->arrValue[$key]) && !is_array($this->arrValue[$key])) {
435                    $this->arrValue[$key] = explode('-', $this->arrValue[$key]);
436                }
437            }
438        }
439    }
440
441    /**
442     * 入力パラメーターの先頭及び末尾にある空白文字を削除する.
443     *
444     * @param boolean $has_wide_space 全角空白も削除する場合 true
445     * @return void
446     */
447    function trimParam($has_wide_space = true) {
448        foreach ($this->arrValue as &$value) {
449            $this->recursionTrim($value, $has_wide_space);
450        }
451    }
452
453    /**
454     * 再帰的に入力パラメーターの先頭及び末尾にある空白文字を削除する.
455     *
456     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
457     * @param boolean $has_wide_space 全角空白も削除する場合 true
458     * @return void
459     */
460    function recursionTrim(&$value, $has_wide_space = true) {
461        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
462        if (is_array($value)) {
463            foreach ($value as $key => $val) {
464                $this->recursionTrim($value[$key], $convert);
465            }
466        } else {
467            if (!SC_Utils_Ex::isBlank($value)) {
468                if ($has_wide_space) {
469                    $value = preg_replace($pattern, '$1', $value);
470                }
471                $value = trim($value);
472            }
473        }
474    }
475
476    /**
477     * 検索結果引き継ぎ用の連想配列を取得する.
478     *
479     * 引数で指定した文字列で始まるパラメーター名の入力値を連想配列で取得する.
480     *
481     * @param string $prefix パラメーター名の接頭辞
482     * @return array 検索結果引き継ぎ用の連想配列.
483     */
484    function getSearchArray($prefix = 'search_') {
485        $arrResults = array();
486        foreach ($this->keyname as $key) {
487            if (preg_match('/^' . $prefix . '/', $key)) {
488                $arrResults[$key] = $this->getValue($key);
489            }
490        }
491        return $arrResults;
492    }
493
494    /**
495     * 前方互換用
496     *
497     * 1次キーが添字なのが特徴だったと思われる。
498     * @deprecated 2.12.0 必要ならば getFormParamList メソッドに引数を追加するなどで実現可能
499     */
500    function getFormDispArray() {
501        $formDispArray = array();
502        foreach ($this->keyname as $index => $key) {
503            // キー名
504            $formDispArray[$index]['keyname'] = $key;
505            // 表示名
506            $formDispArray[$index]['disp_name']  = $this->disp_name[$index];
507            // 文字数制限
508            $formDispArray[$index]['length'] = $this->length[$index];
509            // 入力値
510            $formDispArray[$index]['value'] = $this->getValue($key);
511        }
512        return $formDispArray;
513    }
514
515    /**
516     * パラメーターの削除
517     * addParamの逆の関数
518     * カスタマイズおよびプラグインで使用されるのを想定
519     */
520    function removeParam($keyname) {
521        $index = array_search($keyname, $this->keyname);
522
523        if ($index !== FALSE) {
524            // $this->paramに歯抜けが存在する場合は、NULLで埋めておく。
525            // 最後に配列を詰める際に、全ての項目が埋まっている必要がある。
526            foreach ($this->keyname as $key => $value) {
527                if (!isset($this->param[$key])) {
528                    $this->param[$key] = NULL;
529                }
530            }
531            // $this->paramがソートされていない時があるのでソート。
532            ksort($this->param);
533
534            // 削除
535            unset($this->disp_name[$index]);
536            unset($this->keyname[$index]);
537            unset($this->length[$index]);
538            unset($this->convert[$index]);
539            unset($this->arrCheck[$index]);
540            unset($this->default[$index]);
541            unset($this->input_db[$index]);
542            unset($this->param[$index]);
543
544            // 歯抜けになった配列を詰める
545            $this->disp_name = array_merge($this->disp_name);
546            $this->keyname = array_merge($this->keyname);
547            $this->length = array_merge($this->length);
548            $this->convert = array_merge($this->convert);
549            $this->arrCheck = array_merge($this->arrCheck);
550            $this->default = array_merge($this->default);
551            $this->input_db = array_merge($this->input_db);
552            $this->param = array_merge($this->param);
553        }
554    }
555
556    /**
557     * パラメーター定義の上書き
558     *
559     * @param string $keyname キー名
560     * @param string $target 上書きしたい項目名(disp_name,length,convert等)
561     * @param mixed $value 指定した内容に上書きする
562     */
563    function overwriteParam($keyname, $target, $value) {
564        $index = array_search($keyname, $this->keyname);
565
566        if ($index !== FALSE) {
567            $this->{$target}[$index] = $value;
568        }
569    }
570}
Note: See TracBrowser for help on using the repository browser.