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

Revision 21661, 16.8 KB checked in by Seasoft, 12 years ago (diff)

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