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

Revision 21514, 17.5 KB checked in by Seasoft, 12 years ago (diff)

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

  • 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        $cnt = 0;
83        if (!$seq) {
84            foreach ($this->keyname as $val) {
85                if (isset($arrVal[$val])) {
86                    $this->setValue($val, $arrVal[$val]);
87                }
88            }
89        } else {
90            foreach ($this->keyname as $val) {
91                $this->param[$cnt] = $arrVal[$cnt];
92                $cnt++;
93            }
94        }
95    }
96
97    // 画面表示用タイトル生成
98    function setHtmlDispNameArray() {
99        $cnt = 0;
100        foreach ($this->keyname as $val) {
101            $find = false;
102            foreach ($this->arrCheck[$cnt] as $val) {
103                if ($val == 'EXIST_CHECK') {
104                    $find = true;
105                }
106            }
107
108            if ($find) {
109                $this->html_disp_name[$cnt] = $this->disp_name[$cnt] . '<span class="red">(※ 必須)</span>';
110            } else {
111                $this->html_disp_name[$cnt] = $this->disp_name[$cnt];
112            }
113            if ($this->default[$cnt] != '') {
114                $this->html_disp_name[$cnt] .= ' [省略時初期値: ' . $this->default[$cnt] . ']';
115            }
116            if ($this->input_db[$cnt] == false) {
117                $this->html_disp_name[$cnt] .= ' [登録・更新不可] ';
118            }
119            $cnt++;
120        }
121    }
122
123    // 画面表示用タイトル取得
124    function getHtmlDispNameArray() {
125        return $this->html_disp_name;
126    }
127
128    // 複数列パラメーターの取得
129    function setParamList($arrVal, $keyname) {
130        // DBの件数を取得する。
131        $count = count($arrVal);
132        $no = 1;
133        for ($cnt = 0; $cnt < $count; $cnt++) {
134            $key = $keyname.$no;
135            if ($arrVal[$cnt][$keyname] != '') {
136                $this->setValue($key, $arrVal[$cnt][$keyname]);
137            }
138            $no++;
139        }
140    }
141
142    function setDBDate($db_date, $year_key = 'year', $month_key = 'month', $day_key = 'day') {
143
144        if (!empty($db_date)) {
145            list($y, $m, $d) = preg_split('/[- ]/', $db_date);
146            $this->setValue($year_key, $y);
147            $this->setValue($month_key, $m);
148            $this->setValue($day_key, $d);
149        }
150    }
151
152    // キーに対応した値をセットする。
153    function setValue($key, $param) {
154        $cnt = 0;
155        foreach ($this->keyname as $val) {
156            if ($val == $key) {
157                $this->param[$cnt] = $param;
158                // 複数一致の場合もあるので break してはいけない。
159            }
160            $cnt++;
161        }
162    }
163
164    function toLower($key) {
165        $cnt = 0;
166        foreach ($this->keyname as $val) {
167            if ($val == $key) {
168                $this->param[$cnt] = strtolower($this->param[$cnt]);
169                // 複数一致の場合もあるので break してはいけない。
170            }
171            $cnt++;
172        }
173    }
174
175    // エラーチェック
176    function checkError($br = true) {
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] = preg_replace("/<br(\s+\/)?>/i", "", $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 $error_last_key エラーメッセージを格納する配列の末端のキー
258     * @return void
259     */
260    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key,
261                            $length = 0, $depth = 0, $error_last_key = null) {
262        if (is_array($value)) {
263            $depth++;
264            foreach ($value as $key => $in) {
265                $this->recursionCheck($disp_name, $func, $in, $arrErr, $error_key,
266                                      $length, $depth, $key);
267            }
268        } else {
269            $objErr = new SC_CheckError_Ex(array(0 => $value));
270            $objErr->doFunc(array($disp_name, 0, $length), array($func));
271            if (!SC_Utils_Ex::isBlank($objErr->arrErr)) {
272                foreach ($objErr->arrErr as $message) {
273
274                    if (!SC_Utils_Ex::isBlank($message)) {
275                        // 再帰した場合は多次元配列のエラーメッセージを生成
276                        $error_var = '$arrErr[$error_key]';
277                        for ($i = 0; $i < $depth; $i++) {
278                            // FIXME 二次元以上の対応
279                            $error_var .= '[' . $error_last_key . ']';
280                        }
281                        eval($error_var . ' = $message;');
282                    }
283                }
284            }
285        }
286    }
287
288    /**
289     * フォームの入力パラメーターに応じて, 再帰的に mb_convert_kana 関数を実行する.
290     *
291     * @return voi
292     * @see mb_convert_kana
293     */
294    function convParam() {
295        $cnt = 0;
296        foreach ($this->keyname as $val) {
297            if (!isset($this->param[$cnt])) $this->param[$cnt] = '';
298            $this->recursionConvParam($this->param[$cnt], $this->convert[$cnt]);
299            $cnt++;
300        }
301    }
302
303    /**
304     * 再帰的に mb_convert_kana を実行する.
305     *
306     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
307     * @param string $convert mb_convert_kana の変換オプション
308     */
309    function recursionConvParam(&$value, $convert) {
310        if (is_array($value)) {
311            foreach (array_keys($value) as $key) {
312                $this->recursionConvParam($value[$key], $convert);
313            }
314        } else {
315            if (!SC_Utils_Ex::isBlank($value)) {
316                $value = mb_convert_kana($value, $convert);
317            }
318        }
319    }
320
321    /**
322     * 連想配列で返す
323     *
324     * @param array $arrKey 対象のキー
325     * @return array 連想配列
326     */
327    function getHashArray($arrKey = array()) {
328        $arrRet = array();
329        foreach ($this->keyname as $index => $keyname) {
330            if (empty($arrKey) || in_array($keyname, $arrKey)) {
331                $arrRet[$keyname] = isset($this->param[$index]) ? $this->param[$index] : '';
332            }
333        }
334        return $arrRet;
335    }
336
337    // DB格納用配列の作成
338    function getDbArray() {
339        $cnt = 0;
340        foreach ($this->keyname as $val) {
341            if ($this->input_db[$cnt]) {
342                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : '';
343            }
344            $cnt++;
345        }
346        return $arrRet;
347    }
348
349    /**
350     * 配列の縦横を入れ替えて返す
351     *
352     * @param array $arrKey 対象のキー
353     * @return array 縦横を入れ替えた配列
354     */
355    function getSwapArray($arrKey = array()) {
356        $arrTmp = $this->getHashArray($arrKey);
357        return SC_Utils_Ex::sfSwapArray($arrTmp);
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     * キー名の一覧を返す
398     *
399     * @return array キー名の一覧
400     */
401    function getKeyList() {
402        return $this->keyname;
403    }
404
405    // キー名と一致した値を返す
406    function getValue($keyname, $default = '') {
407        $cnt = 0;
408        $ret = null;
409        foreach ($this->keyname as $val) {
410            if ($val == $keyname) {
411                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : '';
412                break;
413            }
414            $cnt++;
415        }
416
417        if (is_array($ret)) {
418            foreach (array_keys($ret) as $key) {
419                if (SC_Utils_Ex::isBlank($ret[$key])) {
420                    $ret[$key] = $default;
421                }
422            }
423        } else {
424            if (SC_Utils_Ex::isBlank($ret)) {
425                $ret = $default;
426            }
427        }
428        return $ret;
429    }
430
431    /**
432     * @deprecated
433     */
434    function splitParamCheckBoxes($keyname) {
435        $cnt = 0;
436        foreach ($this->keyname as $val) {
437            if ($val == $keyname) {
438                if (isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
439                    $this->param[$cnt] = explode('-', $this->param[$cnt]);
440                }
441            }
442            $cnt++;
443        }
444    }
445
446    /**
447     * 入力パラメーターの先頭及び末尾にある空白文字を削除する.
448     *
449     * @param boolean $has_wide_space 全角空白も削除する場合 true
450     * @return void
451     */
452    function trimParam($has_wide_space = true) {
453        $cnt = 0;
454        foreach ($this->keyname as $val) {
455            if (!isset($this->param[$cnt])) $this->param[$cnt] = '';
456            $this->recursionTrim($this->param[$cnt], $has_wide_space);
457            $cnt++;
458        }
459    }
460
461    /**
462     * 再帰的に入力パラメーターの先頭及び末尾にある空白文字を削除する.
463     *
464     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
465     * @param boolean $has_wide_space 全角空白も削除する場合 true
466     * @return void
467     */
468    function recursionTrim(&$value, $has_wide_space = true) {
469        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
470        if (is_array($value)) {
471            foreach (array_keys($value) as $key) {
472                $this->recursionTrim($value[$key], $convert);
473            }
474        } else {
475            if (!SC_Utils_Ex::isBlank($value)) {
476                if ($has_wide_space) {
477                    $value = preg_replace($pattern, '$1', $value);
478                }
479                $value = trim($value);
480            }
481        }
482    }
483
484    /**
485     * 検索結果引き継ぎ用の連想配列を取得する.
486     *
487     * 引数で指定した文字列で始まるパラメーター名の入力値を連想配列で取得する.
488     *
489     * @param string $prefix パラメーター名の接頭辞
490     * @return array 検索結果引き継ぎ用の連想配列.
491     */
492    function getSearchArray($prefix = 'search_') {
493        $cnt = 0;
494        $arrResults = array();
495        foreach ($this->keyname as $key) {
496            if (preg_match('/^' . $prefix . '/', $key)) {
497                $arrResults[$key] = isset($this->param[$cnt])
498                    ? $this->param[$cnt] : '';
499            }
500            $cnt++;
501        }
502        return $arrResults;
503    }
504
505
506    // addParam の内容をそのまま返す
507    function getFormDispArray() {
508        $cnt = 0;
509        foreach ($this->keyname as $val) {
510            // キー名
511            $arrRet[$cnt]['keyname'] = $this->keyname[$cnt];
512            // 文字数制限
513            $arrRet[$cnt]['length'] = $this->length[$cnt];
514
515            $arrRet[$cnt]['disp_name']  = $this->disp_name[$cnt];
516            // 入力値
517            if (isset($this->param[$cnt])) {
518                $arrRet[$cnt]['value'] = $this->param[$cnt];
519            }
520
521            if (!isset($this->param[$cnt])) $this->param[$cnt] = '';
522
523            if ($this->default[$cnt] != '' && $this->param[$cnt] == '') {
524                $arrRet[$cnt]['value'] = $this->default[$cnt];
525            }
526            $cnt++;
527        }
528        return $arrRet;
529    }
530
531
532}
Note: See TracBrowser for help on using the repository browser.