source: branches/version-2_12-dev/data/class/SC_FormParam.php @ 21917

Revision 21917, 19.5 KB checked in by pineray, 12 years ago (diff)

#163 国際化対応

SC_FormParam.php 内の出力文字列を差し替え.

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