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

Revision 20091, 15.2 KB checked in by nanasess, 13 years ago (diff)

#984([フロント]商品購入 リファクタリング)
#783(ページ間の遷移方法の改善)

  • /shopping/index.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-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 * TODO 配列の再帰処理
29 *
30 * @package SC
31 * @author LOCKON CO.,LTD.
32 */
33class SC_FormParam {
34
35    var $param;
36    var $disp_name;
37    var $keyname;
38    var $length;
39    var $convert;
40    var $arrCheck;
41    var $default;   // 何も入力されていないときに表示する値
42    var $input_db;  // DBにそのまま挿入可能か否か
43    var $html_disp_name;
44
45    // コンストラクタ
46    function SC_FormParam() {
47        $this->check_dir = IMAGE_SAVE_REALDIR;
48        $this->initParam();
49    }
50
51    /**
52     * パラメータの初期化
53     *
54     * @return void
55     */
56    function initParam() {
57        $this->disp_name = array();
58        $this->keyname = array();
59        $this->length = array();
60        $this->convert = array();
61        $this->arrCheck = array();
62        $this->default = array();
63        $this->input_db = array();
64    }
65
66    // パラメータの追加
67    function addParam($disp_name, $keyname, $length="", $convert="", $arrCheck=array(), $default="", $input_db="true") {
68        $this->disp_name[] = $disp_name;
69        $this->keyname[] = $keyname;
70        $this->length[] = $length;
71        $this->convert[] = $convert;
72        $this->arrCheck[] = $arrCheck;
73        $this->default[] = $default;
74        $this->input_db[] = $input_db;
75    }
76
77    // パラメータの入力
78    // $arrVal  :$arrVal['keyname']・・の配列を一致したキーのインスタンスに格納する
79    // $seq     :trueの場合、$arrVal[0]~の配列を登録順にインスタンスに格納する
80    function setParam($arrVal, $seq = false) {
81        $cnt = 0;
82        if(!$seq){
83            foreach($this->keyname as $val) {
84                if(isset($arrVal[$val])) {
85                    $this->setValue($val, $arrVal[$val]);
86                }
87            }
88        } else {
89            foreach($this->keyname as $val) {
90                $this->param[$cnt] = $arrVal[$cnt];
91                $cnt++;
92            }
93        }
94    }
95
96    // 画面表示用タイトル生成
97    function setHtmlDispNameArray() {
98        $cnt = 0;
99        foreach($this->keyname as $val) {
100            $find = false;
101            foreach($this->arrCheck[$cnt] as $val) {
102                if($val == "EXIST_CHECK") {
103                    $find = true;
104                }
105            }
106
107            if($find) {
108                $this->html_disp_name[$cnt] = $this->disp_name[$cnt] . '<span class="red">(※ 必須)</span>';
109            } else {
110                $this->html_disp_name[$cnt] = $this->disp_name[$cnt];
111            }
112            if($this->default[$cnt] != "") {
113                $this->html_disp_name[$cnt] .= ' [省略時初期値: ' . $this->default[$cnt] . ']';
114            }
115            if($this->input_db[$cnt] == false) {
116                $this->html_disp_name[$cnt] .= ' [登録・更新不可] ';
117            }
118            $cnt++;
119        }
120    }
121
122    // 画面表示用タイトル取得
123    function getHtmlDispNameArray() {
124        return $this->html_disp_name;
125    }
126
127    // 複数列パラメータの取得
128    function setParamList($arrVal, $keyname) {
129        // DBの件数を取得する。
130        $count = count($arrVal);
131        $no = 1;
132        for($cnt = 0; $cnt < $count; $cnt++) {
133            $key = $keyname.$no;
134            if($arrVal[$cnt][$keyname] != "") {
135                $this->setValue($key, $arrVal[$cnt][$keyname]);
136            }
137            $no++;
138        }
139    }
140
141    function setDBDate($db_date, $year_key = 'year', $month_key = 'month', $day_key = 'day') {
142
143        if (!empty($db_date)) {
144            list($y, $m, $d) = split("[- ]", $db_date);
145            $this->setValue($year_key, $y);
146            $this->setValue($month_key, $m);
147            $this->setValue($day_key, $d);
148        }
149    }
150
151    // キーに対応した値をセットする。
152    function setValue($key, $param) {
153        $cnt = 0;
154        foreach($this->keyname as $val) {
155            if($val == $key) {
156                $this->param[$cnt] = $param;
157                // 複数一致の場合もあるので break してはいけない。
158            }
159            $cnt++;
160        }
161    }
162
163    function toLower($key) {
164        $cnt = 0;
165        foreach($this->keyname as $val) {
166            if($val == $key) {
167                $this->param[$cnt] = strtolower($this->param[$cnt]);
168                // 複数一致の場合もあるので break してはいけない。
169            }
170            $cnt++;
171        }
172    }
173
174    // エラーチェック
175    function checkError($br = true, $keyname = "") {
176        // 連想配列の取得
177        $arrRet = $this->getHashArray($keyname);
178        $objErr = new SC_CheckError($arrRet);
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
205                    if(!is_array($this->param[$cnt])) {
206                        $objErr->doFunc(array($this->disp_name[$cnt], $val), array($func));
207                    } else {
208                        $max = count($this->param[$cnt]);
209                        for($i = 0; $i < $max; $i++) {
210                            $objSubErr = new SC_CheckError($this->param[$cnt]);
211                            $objSubErr->doFunc(array($this->disp_name[$cnt], $i), array($func));
212                            if(count($objSubErr->arrErr) > 0) {
213                                foreach($objSubErr->arrErr as $mess) {
214                                    if($mess != "") {
215                                        $objErr->arrErr[$val] = $mess;
216                                    }
217                                }
218                            }
219                        }
220                    }
221                    break;
222                case 'MAX_LENGTH_CHECK':
223                case 'MIN_LENGTH_CHECK':
224                case 'NUM_COUNT_CHECK':
225                    if(!is_array($this->param[$cnt])) {
226                        $objErr->doFunc(array($this->disp_name[$cnt], $val, $this->length[$cnt]), array($func));
227                    } else {
228                        $max = count($this->param[$cnt]);
229                        for($i = 0; $i < $max; $i++) {
230                            $objSubErr = new SC_CheckError($this->param[$cnt]);
231                            $objSubErr->doFunc(array($this->disp_name[$cnt], $i, $this->length[$cnt]), array($func));
232                            if(count($objSubErr->arrErr) > 0) {
233                                foreach($objSubErr->arrErr as $mess) {
234                                    if($mess != "") {
235                                        $objErr->arrErr[$val] = $mess;
236                                    }
237                                }
238                            }
239                        }
240                    }
241                    break;
242                // 小文字に変換
243                case 'CHANGE_LOWER':
244                    $this->param[$cnt] = strtolower($this->param[$cnt]);
245                    break;
246                // ファイルの存在チェック
247                case 'FILE_EXISTS':
248                    if($this->param[$cnt] != "" && !file_exists($this->check_dir . $this->param[$cnt])) {
249                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
250                    }
251                    break;
252                // ダウンロード用ファイルの存在チェック
253                case 'DOWN_FILE_EXISTS':
254                    if($this->param[$cnt] != "" && !file_exists(DOWN_SAVE_REALDIR . $this->param[$cnt])) {
255                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
256                    }
257                    break;
258                default:
259                    $objErr->arrErr[$val] = "※※ エラーチェック形式($func)には対応していません ※※ <br>";
260                    break;
261                }
262            }
263
264            if (isset($objErr->arrErr[$val]) && !$br) {
265                $objErr->arrErr[$val] = ereg_replace("<br>$", "", $objErr->arrErr[$val]);
266            }
267            $cnt++;
268        }
269        return $objErr->arrErr;
270    }
271
272    // 入力文字の変換
273    function convParam() {
274        /*
275         *  文字列の変換
276         *  K :  「半角(ハンカク)片仮名」を「全角片仮名」に変換
277         *  C :  「全角ひら仮名」を「全角かた仮名」に変換
278         *  V :  濁点付きの文字を一文字に変換。"K","H"と共に使用します
279         *  n :  「全角」数字を「半角(ハンカク)」に変換
280         *  a :  「全角」英字を「半角」英字に変換
281         */
282        $cnt = 0;
283        foreach ($this->keyname as $val) {
284            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
285
286            if(!is_array($this->param[$cnt])) {
287                if($this->convert[$cnt] != "") {
288                    $this->param[$cnt] = mb_convert_kana($this->param[$cnt] ,$this->convert[$cnt]);
289                }
290            } else {
291                $max = count($this->param[$cnt]);
292                for($i = 0; $i < $max; $i++) {
293                    if($this->convert[$cnt] != "") {
294                        $this->param[$cnt][$i] = mb_convert_kana($this->param[$cnt][$i] ,$this->convert[$cnt]);
295                    }
296                }
297            }
298            $cnt++;
299        }
300    }
301
302    // 連想配列の作成
303    function getHashArray($keyname = "") {
304        $arrRet = array();
305        $cnt = 0;
306        foreach($this->keyname as $val) {
307            if($keyname == "" || $keyname == $val) {
308                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
309                $cnt++;
310            }
311        }
312        return $arrRet;
313    }
314
315    // DB格納用配列の作成
316    function getDbArray() {
317        $cnt = 0;
318        foreach ($this->keyname as $val) {
319            if ($this->input_db[$cnt]) {
320                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
321            }
322            $cnt++;
323        }
324        return $arrRet;
325    }
326
327    // 配列の縦横を入れ替えて返す
328    function getSwapArray($arrKey) {
329        foreach($arrKey as $keyname) {
330            $arrVal = $this->getValue($keyname);
331            $max = count($arrVal);
332            for($i = 0; $i < $max; $i++) {
333                $arrRet[$i][$keyname] = $arrVal[$i];
334            }
335        }
336        return $arrRet;
337    }
338
339    // 項目名一覧の取得
340    function getTitleArray() {
341        return $this->disp_name;
342    }
343
344    // 項目数を返す
345    function getCount() {
346        $count = count($this->keyname);
347        return $count;
348    }
349
350    // フォームに渡す用のパラメータを返す
351    function getFormParamList() {
352        $cnt = 0;
353        foreach($this->keyname as $val) {
354
355            // キー名
356            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
357            // 文字数制限
358            $arrRet[$val]['length'] = $this->length[$cnt];
359            // 入力値
360            if (isset($this->param[$cnt])) {
361                $arrRet[$val]['value'] = $this->param[$cnt];
362            }
363
364            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
365
366            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
367                $arrRet[$val]['value'] = $this->default[$cnt];
368            }
369
370            $cnt++;
371        }
372        return $arrRet;
373    }
374
375    // キー名の一覧を返す
376    function getKeyList() {
377        foreach($this->keyname as $val) {
378            $arrRet[] = $val;
379        }
380        return $arrRet;
381    }
382
383    // キー名と一致した値を返す
384    function getValue($keyname) {
385        $cnt = 0;
386        foreach($this->keyname as $val) {
387            if($val == $keyname) {
388                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
389                break;
390            }
391            $cnt++;
392        }
393        return $ret;
394    }
395
396    function splitCheckBoxes($keyname) {
397        $cnt = 0;
398        foreach($this->keyname as $val) {
399            if($val == $keyname) {
400                $this->param[$cnt] = sfSplitCheckBoxes($this->param[$cnt]);
401            }
402            $cnt++;
403        }
404    }
405
406    function splitParamCheckBoxes($keyname) {
407        $cnt = 0;
408        foreach($this->keyname as $val) {
409            if($val == $keyname) {
410                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
411                    $this->param[$cnt] = split("-", $this->param[$cnt]);
412                }
413            }
414            $cnt++;
415        }
416    }
417
418    /**
419     * 入力パラメータの先頭及び末尾にある空白文字を削除する.
420     *
421     * @param boolean $has_wide_space 全角空白も削除する場合 true
422     * @return void
423     */
424    function trimParam($has_wide_space = true) {
425        $cnt = 0;
426        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
427        foreach ($this->keyname as $val) {
428            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
429
430            if (!is_array($this->param[$cnt])) {
431                if ($has_wide_space) {
432                    $this->param[$cnt] = preg_replace($pattern, '$1', $this->param[$cnt]);
433                }
434                $this->param[$cnt] = trim($this->param[$cnt]);
435            } else {
436                $max = count($this->param[$cnt]);
437                // XXX foreach の方が良い?
438                for ($i = 0; $i < $max; $i++) {
439                    if ($has_wide_space) {
440                        $this->param[$cnt][$i] = preg_replace($pattern, '$1', $this->param[$cnt][$i]);
441                    }
442                    $this->param[$cnt][$i] = trim($this->param[$cnt][$i]);
443                }
444            }
445            $cnt++;
446        }
447    }
448}
449?>
Note: See TracBrowser for help on using the repository browser.