source: branches/version-2_5-dev/data/class/SC_FormParam.php @ 20503

Revision 20503, 16.4 KB checked in by shutta, 13 years ago (diff)

SC_CheckErrorクラスのclass_extends対応

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