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

Revision 20180, 16.2 KB checked in by nanasess, 15 years ago (diff)

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

  • 受注登録/編集ページ
  • 配送方法を選択できるよう追加カスタマイズ

#642(共通ロジックの機能向上)

  • SC_FormParam の処理を多次元配列へ対応
  • SC_Helper_DB::sfUpdateOrderNameCol(), SC_Helper_DB::sfUpdateOrderStatus() を SC_Helper_Purchase へ移動
  • 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) = 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                    $this->recursionCheck($this->disp_name[$cnt], $func,
208                                          $this->param[$cnt], $objErr->arrErr,
209                                          $val, $this->length[$cnt]);
210                    break;
211                // 小文字に変換
212                case 'CHANGE_LOWER':
213                    $this->param[$cnt] = strtolower($this->param[$cnt]);
214                    break;
215                // ファイルの存在チェック
216                case 'FILE_EXISTS':
217                    if($this->param[$cnt] != "" && !file_exists($this->check_dir . $this->param[$cnt])) {
218                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
219                    }
220                    break;
221                // ダウンロード用ファイルの存在チェック
222                case 'DOWN_FILE_EXISTS':
223                    if($this->param[$cnt] != "" && !file_exists(DOWN_SAVE_REALDIR . $this->param[$cnt])) {
224                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
225                    }
226                    break;
227                default:
228                    $objErr->arrErr[$val] = "※※ エラーチェック形式($func)には対応していません ※※ <br>";
229                    break;
230                }
231            }
232
233            if (isset($objErr->arrErr[$val]) && !$br) {
234                $objErr->arrErr[$val] = ereg_replace("<br>$", "", $objErr->arrErr[$val]);
235            }
236            $cnt++;
237        }
238        return $objErr->arrErr;
239    }
240
241    /**
242     * SC_CheckError::doFunc() を再帰的に実行する.
243     *
244     * 再帰実行した場合は, エラーメッセージを多次元配列で格納する
245     *
246     * TODO 二次元以上のエラーメッセージへの対応
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     * @param integer $depth 再帰実行した場合の深度
255     * @param integer $recursion_count 再帰実行した回数
256     * @return void
257     */
258    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key,
259                            $length = 0, $depth = 0, $recursion_count = 0) {
260        if (is_array($value)) {
261            $depth++;
262            $recursion_count = 0;
263            foreach ($value as $in) {
264                $this->recursionCheck($disp_name, $func, $in, $arrErr, $error_key,
265                                      $length, $depth, $recursion_count);
266                $recursion_count++;
267            }
268        } else {
269            $objErr = new SC_CheckError(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 .= '[' . $recursion_count . ']';
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    function getHashArray($keyname = "") {
323        $arrRet = array();
324        $cnt = 0;
325        foreach($this->keyname as $val) {
326            if($keyname == "" || $keyname == $val) {
327                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
328                $cnt++;
329            }
330        }
331        return $arrRet;
332    }
333
334    // DB格納用配列の作成
335    function getDbArray() {
336        $cnt = 0;
337        foreach ($this->keyname as $val) {
338            if ($this->input_db[$cnt]) {
339                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
340            }
341            $cnt++;
342        }
343        return $arrRet;
344    }
345
346    // 配列の縦横を入れ替えて返す
347    function getSwapArray($arrKey) {
348        foreach($arrKey as $keyname) {
349            $arrVal = $this->getValue($keyname);
350            $max = count($arrVal);
351            for($i = 0; $i < $max; $i++) {
352                $arrRet[$i][$keyname] = $arrVal[$i];
353            }
354        }
355        return $arrRet;
356    }
357
358    // 項目名一覧の取得
359    function getTitleArray() {
360        return $this->disp_name;
361    }
362
363    // 項目数を返す
364    function getCount() {
365        $count = count($this->keyname);
366        return $count;
367    }
368
369    // フォームに渡す用のパラメータを返す
370    function getFormParamList() {
371        $cnt = 0;
372        foreach($this->keyname as $val) {
373
374            // キー名
375            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
376            // 文字数制限
377            $arrRet[$val]['length'] = $this->length[$cnt];
378            // 入力値
379            if (isset($this->param[$cnt])) {
380                $arrRet[$val]['value'] = $this->param[$cnt];
381            }
382
383            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
384
385            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
386                $arrRet[$val]['value'] = $this->default[$cnt];
387            }
388
389            $cnt++;
390        }
391        return $arrRet;
392    }
393
394    // キー名の一覧を返す
395    function getKeyList() {
396        foreach($this->keyname as $val) {
397            $arrRet[] = $val;
398        }
399        return $arrRet;
400    }
401
402    // キー名と一致した値を返す
403    function getValue($keyname) {
404        $cnt = 0;
405        foreach($this->keyname as $val) {
406            if($val == $keyname) {
407                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
408                break;
409            }
410            $cnt++;
411        }
412        return $ret;
413    }
414
415    /**
416     * @deprecated
417     */
418    function splitParamCheckBoxes($keyname) {
419        $cnt = 0;
420        foreach($this->keyname as $val) {
421            if($val == $keyname) {
422                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
423                    $this->param[$cnt] = split("-", $this->param[$cnt]);
424                }
425            }
426            $cnt++;
427        }
428    }
429
430    /**
431     * 入力パラメータの先頭及び末尾にある空白文字を削除する.
432     *
433     * @param boolean $has_wide_space 全角空白も削除する場合 true
434     * @return void
435     */
436    function trimParam($has_wide_space = true) {
437        $cnt = 0;
438        foreach ($this->keyname as $val) {
439            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
440            $this->recursionTrim($this->param[$cnt], $has_wide_space);
441            $cnt++;
442        }
443    }
444
445    /**
446     * 再帰的に入力パラメータの先頭及び末尾にある空白文字を削除する.
447     *
448     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
449     * @param boolean $has_wide_space 全角空白も削除する場合 true
450     * @return void
451     */
452    function recursionTrim(&$value, $has_wide_space = true) {
453        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
454        if (is_array($value)) {
455            foreach (array_keys($value) as $key) {
456                $this->recursionTrim($value[$key], $convert);
457            }
458        } else {
459            if (!SC_Utils_Ex::isBlank($value)) {
460                if ($has_wide_space) {
461                    $value = preg_replace($pattern, '$1', $value);
462                }
463                $value = trim($value);
464            }
465        }
466    }
467
468    /**
469     * 検索結果引き継ぎ用の連想配列を取得する.
470     *
471     * 引数で指定した文字列で始まるパラメータ名の入力値を連想配列で取得する.
472     *
473     * @param string $prefix パラメータ名の接頭辞
474     * @return array 検索結果引き継ぎ用の連想配列.
475     */
476    function getSearchArray($prefix = 'search_') {
477        $cnt = 0;
478        $arrResults = array();
479        foreach ($this->keyname as $key) {
480            if (preg_match('/^' . $prefix . '/', $key)) {
481                $arrResults[$key] = isset($this->param[$cnt])
482                    ? $this->param[$cnt] : "";
483            }
484            $cnt++;
485        }
486        return $arrResults;
487    }
488}
489?>
Note: See TracBrowser for help on using the repository browser.