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

Revision 22856, 19.7 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

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