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

Revision 22567, 19.7 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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