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

Revision 20562, 17.2 KB checked in by Seasoft, 13 years ago (diff)

#627(ソース整形・ソースコメントの改善)

  • TAB
  • 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    /** 何も入力されていないときに表示する値 */
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, $keyname = "") {
177        // 連想配列の取得
178        $arrRet = $this->getHashArray($keyname);
179        $objErr->arrErr = array();
180
181        $cnt = 0;
182        foreach($this->keyname as $val) {
183            foreach($this->arrCheck[$cnt] as $func) {
184                if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
185                switch($func) {
186                case 'EXIST_CHECK':
187                case 'NUM_CHECK':
188                case 'EMAIL_CHECK':
189                case 'EMAIL_CHAR_CHECK':
190                case 'ALNUM_CHECK':
191                case 'GRAPH_CHECK':
192                case 'KANA_CHECK':
193                case 'URL_CHECK':
194                case 'IP_CHECK':
195                case 'SPTAB_CHECK':
196                case 'ZERO_CHECK':
197                case 'ALPHA_CHECK':
198                case 'ZERO_START':
199                case 'FIND_FILE':
200                case 'NO_SPTAB':
201                case 'DIR_CHECK':
202                case 'DOMAIN_CHECK':
203                case 'FILE_NAME_CHECK':
204                case 'MOBILE_EMAIL_CHECK':
205                case 'MAX_LENGTH_CHECK':
206                case 'MIN_LENGTH_CHECK':
207                case 'NUM_COUNT_CHECK':
208                case 'KANABLANK_CHECK':
209                case 'SELECT_CHECK':
210                case 'FILE_NAME_CHECK_BY_NOUPLOAD':
211                    $this->recursionCheck($this->disp_name[$cnt], $func,
212                                          $this->param[$cnt], $objErr->arrErr,
213                                          $val, $this->length[$cnt]);
214                    break;
215                // 小文字に変換
216                case 'CHANGE_LOWER':
217                    $this->param[$cnt] = strtolower($this->param[$cnt]);
218                    break;
219                // ファイルの存在チェック
220                case 'FILE_EXISTS':
221                    if($this->param[$cnt] != "" && !file_exists($this->check_dir . $this->param[$cnt])) {
222                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
223                    }
224                    break;
225                // ダウンロード用ファイルの存在チェック
226                case 'DOWN_FILE_EXISTS':
227                    if($this->param[$cnt] != "" && !file_exists(DOWN_SAVE_REALDIR . $this->param[$cnt])) {
228                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
229                    }
230                    break;
231                default:
232                    $objErr->arrErr[$val] = "※※ エラーチェック形式($func)には対応していません ※※ <br>";
233                    break;
234                }
235            }
236
237            if (isset($objErr->arrErr[$val]) && !$br) {
238                $objErr->arrErr[$val] = ereg_replace("<br>$", "", $objErr->arrErr[$val]);
239            }
240            $cnt++;
241        }
242        return $objErr->arrErr;
243    }
244
245    /**
246     * SC_CheckError::doFunc() を再帰的に実行する.
247     *
248     * 再帰実行した場合は, エラーメッセージを多次元配列で格納する
249     *
250     * TODO 二次元以上のエラーメッセージへの対応
251     *
252     * @param string $disp_name 表示名
253     * @param string $func チェック種別
254     * @param mixed $value チェック対象の値. 配列の場合は再帰的にチェックする.
255     * @param array $arrErr エラーメッセージを格納する配列
256     * @param string $error_key エラーメッセージを格納する配列のキー
257     * @param integer $length チェック対象の値の長さ
258     * @param integer $depth 再帰実行した場合の深度
259     * @param integer $recursion_count 再帰実行した回数
260     * @return void
261     */
262    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key,
263                            $length = 0, $depth = 0, $recursion_count = 0) {
264        if (is_array($value)) {
265            $depth++;
266            $recursion_count = 0;
267            foreach ($value as $in) {
268                $this->recursionCheck($disp_name, $func, $in, $arrErr, $error_key,
269                                      $length, $depth, $recursion_count);
270                $recursion_count++;
271            }
272        } else {
273            $objErr = new SC_CheckError_Ex(array(0 => $value));
274            $objErr->doFunc(array($disp_name, 0, $length), array($func));
275            if (!SC_Utils_Ex::isBlank($objErr->arrErr)) {
276                foreach($objErr->arrErr as $message) {
277
278                    if(!SC_Utils_Ex::isBlank($message)) {
279                        // 再帰した場合は多次元配列のエラーメッセージを生成
280                        $error_var = '$arrErr[$error_key]';
281                        for ($i = 0; $i < $depth; $i++) {
282                            // FIXME 二次元以上の対応
283                            $error_var .= '[' . $recursion_count . ']';
284                        }
285                        eval($error_var . ' = $message;');
286                    }
287                }
288            }
289        }
290    }
291
292    /**
293     * フォームの入力パラメータに応じて, 再帰的に mb_convert_kana 関数を実行する.
294     *
295     * @return voi
296     * @see mb_convert_kana
297     */
298    function convParam() {
299        $cnt = 0;
300        foreach ($this->keyname as $val) {
301            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
302            $this->recursionConvParam($this->param[$cnt], $this->convert[$cnt]);
303            $cnt++;
304        }
305    }
306
307    /**
308     * 再帰的に mb_convert_kana を実行する.
309     *
310     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
311     * @param string $convert mb_convert_kana の変換オプション
312     */
313    function recursionConvParam(&$value, $convert) {
314        if (is_array($value)) {
315            foreach (array_keys($value) as $key) {
316                $this->recursionConvParam($value[$key], $convert);
317            }
318        } else {
319            if (!SC_Utils_Ex::isBlank($value)) {
320                $value = mb_convert_kana($value, $convert);
321            }
322        }
323    }
324
325    // 連想配列の作成
326    function getHashArray($keyname = "") {
327        $arrRet = array();
328        $cnt = 0;
329        foreach($this->keyname as $val) {
330            if($keyname == "" || $keyname == $val) {
331                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
332                $cnt++;
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    function getSwapArray($arrKey) {
352        foreach($arrKey as $keyname) {
353            $arrVal = $this->getValue($keyname);
354            $max = count($arrVal);
355            for($i = 0; $i < $max; $i++) {
356                $arrRet[$i][$keyname] = $arrVal[$i];
357            }
358        }
359        return $arrRet;
360    }
361
362    // 項目名一覧の取得
363    function getTitleArray() {
364        return $this->disp_name;
365    }
366
367    // 項目数を返す
368    function getCount() {
369        $count = count($this->keyname);
370        return $count;
371    }
372
373    // フォームに渡す用のパラメータを返す
374    function getFormParamList() {
375        $cnt = 0;
376        foreach($this->keyname as $val) {
377
378            // キー名
379            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
380            // 文字数制限
381            $arrRet[$val]['length'] = $this->length[$cnt];
382            // 入力値
383            if (isset($this->param[$cnt])) {
384                $arrRet[$val]['value'] = $this->param[$cnt];
385            }
386
387            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
388
389            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
390                $arrRet[$val]['value'] = $this->default[$cnt];
391            }
392
393            $cnt++;
394        }
395        return $arrRet;
396    }
397
398    // キー名の一覧を返す
399    function getKeyList() {
400        foreach($this->keyname as $val) {
401            $arrRet[] = $val;
402        }
403        return $arrRet;
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        if(is_null($ret)){
418            $ret = $default;
419        }
420        return $ret;
421    }
422
423    /**
424     * @deprecated
425     */
426    function splitParamCheckBoxes($keyname) {
427        $cnt = 0;
428        foreach($this->keyname as $val) {
429            if($val == $keyname) {
430                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
431                    $this->param[$cnt] = explode("-", $this->param[$cnt]);
432                }
433            }
434            $cnt++;
435        }
436    }
437
438    /**
439     * 入力パラメータの先頭及び末尾にある空白文字を削除する.
440     *
441     * @param boolean $has_wide_space 全角空白も削除する場合 true
442     * @return void
443     */
444    function trimParam($has_wide_space = true) {
445        $cnt = 0;
446        foreach ($this->keyname as $val) {
447            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
448            $this->recursionTrim($this->param[$cnt], $has_wide_space);
449            $cnt++;
450        }
451    }
452
453    /**
454     * 再帰的に入力パラメータの先頭及び末尾にある空白文字を削除する.
455     *
456     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
457     * @param boolean $has_wide_space 全角空白も削除する場合 true
458     * @return void
459     */
460    function recursionTrim(&$value, $has_wide_space = true) {
461        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
462        if (is_array($value)) {
463            foreach (array_keys($value) as $key) {
464                $this->recursionTrim($value[$key], $convert);
465            }
466        } else {
467            if (!SC_Utils_Ex::isBlank($value)) {
468                if ($has_wide_space) {
469                    $value = preg_replace($pattern, '$1', $value);
470                }
471                $value = trim($value);
472            }
473        }
474    }
475
476    /**
477     * 検索結果引き継ぎ用の連想配列を取得する.
478     *
479     * 引数で指定した文字列で始まるパラメータ名の入力値を連想配列で取得する.
480     *
481     * @param string $prefix パラメータ名の接頭辞
482     * @return array 検索結果引き継ぎ用の連想配列.
483     */
484    function getSearchArray($prefix = 'search_') {
485        $cnt = 0;
486        $arrResults = array();
487        foreach ($this->keyname as $key) {
488            if (preg_match('/^' . $prefix . '/', $key)) {
489                $arrResults[$key] = isset($this->param[$cnt])
490                    ? $this->param[$cnt] : "";
491            }
492            $cnt++;
493        }
494        return $arrResults;
495    }
496
497
498    // addParam の内容をそのまま返す
499    function getFormDispArray() {
500        $cnt = 0;
501        foreach($this->keyname as $val) {
502            // キー名
503            $arrRet[$cnt]['keyname'] = $this->keyname[$cnt];
504            // 文字数制限
505            $arrRet[$cnt]['length'] = $this->length[$cnt];
506
507            $arrRet[$cnt]['disp_name']  = $this->disp_name[$cnt];
508            // 入力値
509            if (isset($this->param[$cnt])) {
510                $arrRet[$cnt]['value'] = $this->param[$cnt];
511            }
512
513            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
514
515            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
516                $arrRet[$cnt]['value'] = $this->default[$cnt];
517            }
518            $cnt++;
519        }
520        return $arrRet;
521    }
522
523
524}
525?>
Note: See TracBrowser for help on using the repository browser.