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

Revision 22472, 19.6 KB checked in by Seasoft, 11 years ago (diff)

#2081 (SC_FormParam#checkError NUM_POINT_CHECK に対応させる)

  • 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-2013 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] = $this->disp_name[$index] . '<span class="red">(※ 必須)</span>';
134            } else {
135                $this->html_disp_name[$index] = $this->disp_name[$index];
136            }
137            if ($this->arrDefault[$key] != '') {
138                $this->html_disp_name[$index] .= ' [省略時初期値: ' . $this->arrDefault[$key] . ']';
139            }
140            if ($this->input_db[$index] == false) {
141                $this->html_disp_name[$index] .= ' [登録・更新不可] ';
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                    case 'NUM_POINT_CHECK':
221                        $this->recursionCheck($this->disp_name[$index], $func,
222                            $value, $arrErr, $key, $this->length[$index]);
223                        break;
224                    // 小文字に変換
225                    case 'CHANGE_LOWER':
226                        $this->toLower($key);
227                        break;
228                    // ファイルの存在チェック
229                    case 'FILE_EXISTS':
230                        if ($value != '' && !file_exists($this->check_dir . $value)) {
231                            $arrErr[$key] = '※ ' . $this->disp_name[$index] . 'のファイルが存在しません。<br>';
232                        }
233                        break;
234                    // ダウンロード用ファイルの存在チェック
235                    case 'DOWN_FILE_EXISTS':
236                        if ($value != '' && !file_exists(DOWN_SAVE_REALDIR . $value)) {
237                            $arrErr[$key] = '※ ' . $this->disp_name[$index] . 'のファイルが存在しません。<br>';
238                        }
239                        break;
240                    default:
241                        $arrErr[$key] = "※※ エラーチェック形式($func)には対応していません ※※ <br>";
242                        break;
243                }
244            }
245
246            if (isset($arrErr[$key]) && !$br) {
247                $arrErr[$key] = preg_replace("/<br(\s+\/)?>/i", '', $arrErr[$key]);
248            }
249        }
250        return $arrErr;
251    }
252
253    /**
254     * SC_CheckError::doFunc() を再帰的に実行する.
255     *
256     * 再帰実行した場合は, エラーメッセージを多次元配列で格納する
257     *
258     * TODO 二次元以上のエラーメッセージへの対応
259     *
260     * @param string $disp_name 表示名
261     * @param string $func チェック種別
262     * @param mixed $value チェック対象の値. 配列の場合は再帰的にチェックする.
263     * @param array $arrErr エラーメッセージを格納する配列
264     * @param string $error_key エラーメッセージを格納する配列のキー
265     * @param integer $length チェック対象の値の長さ
266     * @param integer $depth 再帰実行した場合の深度
267     * @param integer $error_last_key エラーメッセージを格納する配列の末端のキー
268     * @return void
269     */
270    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key,
271        $length = 0, $depth = 0, $error_last_key = null
272    ) {
273        if (is_array($value)) {
274            $depth++;
275            foreach ($value as $key => $in) {
276                $this->recursionCheck($disp_name, $func, $in, $arrErr, $error_key,
277                                      $length, $depth, $key);
278            }
279        } else {
280            $objErr = new SC_CheckError_Ex(array(0 => $value));
281            $objErr->doFunc(array($disp_name, 0, $length), array($func));
282            if (!SC_Utils_Ex::isBlank($objErr->arrErr)) {
283                foreach ($objErr->arrErr as $message) {
284
285                    if (!SC_Utils_Ex::isBlank($message)) {
286                        // 再帰した場合は多次元配列のエラーメッセージを生成
287                        $error_var = '$arrErr[$error_key]';
288                        for ($i = 0; $i < $depth; $i++) {
289                            // FIXME 二次元以上の対応
290                            $error_var .= '[' . $error_last_key . ']';
291                        }
292                        eval($error_var . ' = $message;');
293                    }
294                }
295            }
296        }
297    }
298
299    /**
300     * フォームの入力パラメーターに応じて, 再帰的に mb_convert_kana 関数を実行する.
301     *
302     * @return void
303     * @see mb_convert_kana
304     */
305    function convParam() {
306        foreach ($this->keyname as $index => $key) {
307            if (isset($this->arrValue[$key])) {
308                $this->recursionConvParam($this->arrValue[$key], $this->convert[$index]);
309            }
310        }
311    }
312
313    /**
314     * 再帰的に mb_convert_kana を実行する.
315     *
316     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
317     * @param string $convert mb_convert_kana の変換オプション
318     */
319    function recursionConvParam(&$value, $convert) {
320        if (is_array($value)) {
321            foreach ($value as $key => $val) {
322                $this->recursionConvParam($value[$key], $convert);
323            }
324        } else {
325            if (!SC_Utils_Ex::isBlank($value)) {
326                $value = mb_convert_kana($value, $convert);
327            }
328        }
329    }
330
331    /**
332     * 連想配列で返す
333     *
334     * @param array $arrKey 対象のキー
335     * @return array 連想配列
336     */
337    function getHashArray($arrKey = array()) {
338        $arrRet = array();
339        foreach ($this->keyname as $keyname) {
340            if (empty($arrKey) || in_array($keyname, $arrKey)) {
341                $arrRet[$keyname] = $this->getValue($keyname);
342            }
343        }
344        return $arrRet;
345    }
346
347    // DB格納用配列の作成
348    function getDbArray() {
349        $dbArray = array();
350        foreach ($this->keyname as $index => $key) {
351            if ($this->input_db[$index]) {
352                $dbArray[$key] = $this->getValue($key);
353            }
354        }
355        return $dbArray;
356    }
357
358    /**
359     * 配列の縦横を入れ替えて返す
360     *
361     * @param array $arrKey 対象のキー
362     * @return array 縦横を入れ替えた配列
363     */
364    function getSwapArray($arrKey = array()) {
365        $arrTmp = $this->getHashArray($arrKey);
366        return SC_Utils_Ex::sfSwapArray($arrTmp);
367    }
368
369    // 項目名一覧の取得
370    function getTitleArray() {
371        return $this->disp_name;
372    }
373
374    // 項目数を返す
375    function getCount() {
376        $count = count($this->keyname);
377        return $count;
378    }
379
380    // フォームに渡す用のパラメーターを返す
381    function getFormParamList() {
382        $formParamList = array();
383        foreach ($this->keyname as $index => $key) {
384            // キー名
385            $formParamList[$key]['keyname'] = $key;
386            // 表示名
387            $formParamList[$key]['disp_name'] = $this->disp_name[$index];
388            // 文字数制限
389            $formParamList[$key]['length'] = $this->length[$index];
390            // 入力値
391            $formParamList[$key]['value'] = $this->getValue($key);
392        }
393        return $formParamList;
394    }
395
396    /**
397     * キー名の一覧を返す
398     *
399     * @return array キー名の一覧
400     */
401    function getKeyList() {
402        return $this->keyname;
403    }
404
405    // キー名と一致した値を返す
406    function getValue($keyname, $default = '') {
407        $ret = null;
408        foreach ($this->keyname as $key) {
409            if ($key == $keyname) {
410                $ret = isset($this->arrValue[$key]) ? $this->arrValue[$key] : $this->arrDefault[$key];
411                break;
412            }
413        }
414
415        if (is_array($ret)) {
416            foreach ($ret as $key => $value) {
417                if (SC_Utils_Ex::isBlank($ret[$key])) {
418                    $ret[$key] = $default;
419                }
420            }
421        } else {
422            if (SC_Utils_Ex::isBlank($ret)) {
423                $ret = $default;
424            }
425        }
426        return $ret;
427    }
428
429    /**
430     * @deprecated
431     */
432    function splitParamCheckBoxes($keyname) {
433        foreach ($this->keyname as $key) {
434            if ($key == $keyname) {
435                if (isset($this->arrValue[$key]) && !is_array($this->arrValue[$key])) {
436                    $this->arrValue[$key] = explode('-', $this->arrValue[$key]);
437                }
438            }
439        }
440    }
441
442    /**
443     * 入力パラメーターの先頭及び末尾にある空白文字を削除する.
444     *
445     * @param boolean $has_wide_space 全角空白も削除する場合 true
446     * @return void
447     */
448    function trimParam($has_wide_space = true) {
449        foreach ($this->arrValue as &$value) {
450            $this->recursionTrim($value, $has_wide_space);
451        }
452    }
453
454    /**
455     * 再帰的に入力パラメーターの先頭及び末尾にある空白文字を削除する.
456     *
457     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
458     * @param boolean $has_wide_space 全角空白も削除する場合 true
459     * @return void
460     */
461    function recursionTrim(&$value, $has_wide_space = true) {
462        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
463        if (is_array($value)) {
464            foreach ($value as $key => $val) {
465                $this->recursionTrim($value[$key], $has_wide_space);
466            }
467        } else {
468            if (!SC_Utils_Ex::isBlank($value)) {
469                if ($has_wide_space) {
470                    $value = preg_replace($pattern, '$1', $value);
471                }
472                $value = trim($value);
473            }
474        }
475    }
476
477    /**
478     * 検索結果引き継ぎ用の連想配列を取得する.
479     *
480     * 引数で指定した文字列で始まるパラメーター名の入力値を連想配列で取得する.
481     *
482     * @param string $prefix パラメーター名の接頭辞
483     * @return array 検索結果引き継ぎ用の連想配列.
484     */
485    function getSearchArray($prefix = 'search_') {
486        $arrResults = array();
487        foreach ($this->keyname as $key) {
488            if (preg_match('/^' . $prefix . '/', $key)) {
489                $arrResults[$key] = $this->getValue($key);
490            }
491        }
492        return $arrResults;
493    }
494
495    /**
496     * 前方互換用
497     *
498     * 1次キーが添字なのが特徴だったと思われる。
499     * @deprecated 2.12.0 必要ならば getFormParamList メソッドに引数を追加するなどで実現可能
500     */
501    function getFormDispArray() {
502        $formDispArray = array();
503        foreach ($this->keyname as $index => $key) {
504            // キー名
505            $formDispArray[$index]['keyname'] = $key;
506            // 表示名
507            $formDispArray[$index]['disp_name']  = $this->disp_name[$index];
508            // 文字数制限
509            $formDispArray[$index]['length'] = $this->length[$index];
510            // 入力値
511            $formDispArray[$index]['value'] = $this->getValue($key);
512        }
513        return $formDispArray;
514    }
515
516    /**
517     * パラメーターの削除
518     * addParamの逆の関数
519     * カスタマイズおよびプラグインで使用されるのを想定
520     */
521    function removeParam($keyname) {
522        $index = array_search($keyname, $this->keyname);
523
524        if ($index !== FALSE) {
525            // $this->paramに歯抜けが存在する場合は、NULLで埋めておく。
526            // 最後に配列を詰める際に、全ての項目が埋まっている必要がある。
527            foreach ($this->keyname as $key => $value) {
528                if (!isset($this->param[$key])) {
529                    $this->param[$key] = NULL;
530                }
531            }
532            // $this->paramがソートされていない時があるのでソート。
533            ksort($this->param);
534
535            // 削除
536            unset($this->disp_name[$index]);
537            unset($this->keyname[$index]);
538            unset($this->length[$index]);
539            unset($this->convert[$index]);
540            unset($this->arrCheck[$index]);
541            unset($this->default[$index]);
542            unset($this->input_db[$index]);
543            unset($this->param[$index]);
544
545            // 歯抜けになった配列を詰める
546            $this->disp_name = array_merge($this->disp_name);
547            $this->keyname = array_merge($this->keyname);
548            $this->length = array_merge($this->length);
549            $this->convert = array_merge($this->convert);
550            $this->arrCheck = array_merge($this->arrCheck);
551            $this->default = array_merge($this->default);
552            $this->input_db = array_merge($this->input_db);
553            $this->param = array_merge($this->param);
554        }
555    }
556
557    /**
558     * パラメーター定義の上書き
559     *
560     * @param string $keyname キー名
561     * @param string $target 上書きしたい項目名(disp_name,length,convert等)
562     * @param mixed $value 指定した内容に上書きする
563     */
564    function overwriteParam($keyname, $target, $value) {
565        $index = array_search($keyname, $this->keyname);
566
567        if ($index !== FALSE) {
568            $this->{$target}[$index] = $value;
569        }
570    }
571}
Note: See TracBrowser for help on using the repository browser.