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

Revision 21577, 17.7 KB checked in by Seasoft, 12 years ago (diff)

#1679 (PHP 警告撲滅)

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