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

Revision 20127, 15.1 KB checked in by nanasess, 15 years ago (diff)

#975([管理画面]受注管理(受注一覧、登録編集))

  • 配送商品のチェックを多次元配列で行うため, SC_FormParam::checkError() を多次元配列に対応
  • 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->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                    $this->recursionCheck($this->disp_name[$cnt], $func,
205                                          $this->param[$cnt], $objErr->arrErr,
206                                          $val, $this->length[$cnt]);
207                    break;
208                case 'MAX_LENGTH_CHECK':
209                case 'MIN_LENGTH_CHECK':
210                case 'NUM_COUNT_CHECK':
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     * @param string $disp_name 表示名
249     * @param string $func チェック種別
250     * @param mixed $value チェック対象の値. 配列の場合は再帰的にチェックする.
251     * @param array $arrErr エラーメッセージを格納する配列
252     * @param string $error_key エラーメッセージを格納する配列のキー
253     * @param integer $length チェック対象の値の長さ
254     * @return void
255     */
256    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key,
257                            $length = 0) {
258        if (is_array($value)) {
259            foreach ($value as $in) {
260                $this->recursionCheck($disp_name, $func, $in, $arrErr, $error_key,
261                                      $length);
262            }
263        } else {
264            $objSubErr = new SC_CheckError(array(0 => $value));
265            $objSubErr->doFunc(array($disp_name, 0, $length), array($func));
266            if(count($objSubErr->arrErr) > 0) {
267                foreach($objSubErr->arrErr as $mess) {
268                    if($mess != "") {
269                        $arrErr[$error_key] .= $mess;
270                    }
271                }
272            }
273        }
274    }
275
276    // 入力文字の変換
277    function convParam() {
278        /*
279         *  文字列の変換
280         *  K :  「半角(ハンカク)片仮名」を「全角片仮名」に変換
281         *  C :  「全角ひら仮名」を「全角かた仮名」に変換
282         *  V :  濁点付きの文字を一文字に変換。"K","H"と共に使用します
283         *  n :  「全角」数字を「半角(ハンカク)」に変換
284         *  a :  「全角」英字を「半角」英字に変換
285         */
286        $cnt = 0;
287        foreach ($this->keyname as $val) {
288            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
289
290            if(!is_array($this->param[$cnt])) {
291                if($this->convert[$cnt] != "") {
292                    $this->param[$cnt] = mb_convert_kana($this->param[$cnt] ,$this->convert[$cnt]);
293                }
294            } else {
295                $max = count($this->param[$cnt]);
296                for($i = 0; $i < $max; $i++) {
297                    if($this->convert[$cnt] != "") {
298                        $this->param[$cnt][$i] = mb_convert_kana($this->param[$cnt][$i] ,$this->convert[$cnt]);
299                    }
300                }
301            }
302            $cnt++;
303        }
304    }
305
306    // 連想配列の作成
307    function getHashArray($keyname = "") {
308        $arrRet = array();
309        $cnt = 0;
310        foreach($this->keyname as $val) {
311            if($keyname == "" || $keyname == $val) {
312                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
313                $cnt++;
314            }
315        }
316        return $arrRet;
317    }
318
319    // DB格納用配列の作成
320    function getDbArray() {
321        $cnt = 0;
322        foreach ($this->keyname as $val) {
323            if ($this->input_db[$cnt]) {
324                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
325            }
326            $cnt++;
327        }
328        return $arrRet;
329    }
330
331    // 配列の縦横を入れ替えて返す
332    function getSwapArray($arrKey) {
333        foreach($arrKey as $keyname) {
334            $arrVal = $this->getValue($keyname);
335            $max = count($arrVal);
336            for($i = 0; $i < $max; $i++) {
337                $arrRet[$i][$keyname] = $arrVal[$i];
338            }
339        }
340        return $arrRet;
341    }
342
343    // 項目名一覧の取得
344    function getTitleArray() {
345        return $this->disp_name;
346    }
347
348    // 項目数を返す
349    function getCount() {
350        $count = count($this->keyname);
351        return $count;
352    }
353
354    // フォームに渡す用のパラメータを返す
355    function getFormParamList() {
356        $cnt = 0;
357        foreach($this->keyname as $val) {
358
359            // キー名
360            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
361            // 文字数制限
362            $arrRet[$val]['length'] = $this->length[$cnt];
363            // 入力値
364            if (isset($this->param[$cnt])) {
365                $arrRet[$val]['value'] = $this->param[$cnt];
366            }
367
368            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
369
370            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
371                $arrRet[$val]['value'] = $this->default[$cnt];
372            }
373
374            $cnt++;
375        }
376        return $arrRet;
377    }
378
379    // キー名の一覧を返す
380    function getKeyList() {
381        foreach($this->keyname as $val) {
382            $arrRet[] = $val;
383        }
384        return $arrRet;
385    }
386
387    // キー名と一致した値を返す
388    function getValue($keyname) {
389        $cnt = 0;
390        foreach($this->keyname as $val) {
391            if($val == $keyname) {
392                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
393                break;
394            }
395            $cnt++;
396        }
397        return $ret;
398    }
399
400    function splitCheckBoxes($keyname) {
401        $cnt = 0;
402        foreach($this->keyname as $val) {
403            if($val == $keyname) {
404                $this->param[$cnt] = sfSplitCheckBoxes($this->param[$cnt]);
405            }
406            $cnt++;
407        }
408    }
409
410    function splitParamCheckBoxes($keyname) {
411        $cnt = 0;
412        foreach($this->keyname as $val) {
413            if($val == $keyname) {
414                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
415                    $this->param[$cnt] = split("-", $this->param[$cnt]);
416                }
417            }
418            $cnt++;
419        }
420    }
421
422    /**
423     * 入力パラメータの先頭及び末尾にある空白文字を削除する.
424     *
425     * @param boolean $has_wide_space 全角空白も削除する場合 true
426     * @return void
427     */
428    function trimParam($has_wide_space = true) {
429        $cnt = 0;
430        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
431        foreach ($this->keyname as $val) {
432            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
433
434            if (!is_array($this->param[$cnt])) {
435                if ($has_wide_space) {
436                    $this->param[$cnt] = preg_replace($pattern, '$1', $this->param[$cnt]);
437                }
438                $this->param[$cnt] = trim($this->param[$cnt]);
439            } else {
440                $max = count($this->param[$cnt]);
441                // XXX foreach の方が良い?
442                for ($i = 0; $i < $max; $i++) {
443                    if ($has_wide_space) {
444                        $this->param[$cnt][$i] = preg_replace($pattern, '$1', $this->param[$cnt][$i]);
445                    }
446                    $this->param[$cnt][$i] = trim($this->param[$cnt][$i]);
447                }
448            }
449            $cnt++;
450        }
451    }
452}
453?>
Note: See TracBrowser for help on using the repository browser.