source: branches/version-2_13-dev/data/class/SC_FormParam.php @ 23632

Revision 23632, 20.2 KB checked in by shutta, 9 years ago (diff)

#2626 (SC_FormParam::overwriteParam()にて、default値が上書きできない)

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