source: branches/version-2_11-dev/data/class/SC_FormParam.php @ 20970

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

#1288 (「-er」カタカナ表記の統一)

  • 現状で混在が発生しているもののみ改修。
  • 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, $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        $arrRet = array();
353        foreach ($arrKey as $keyname) {
354            $arrRet[$keyname] = $this->getValue($keyname);
355        }
356        return SC_Utils_Ex::sfSwapArray($arrRet);
357    }
358
359    // 項目名一覧の取得
360    function getTitleArray() {
361        return $this->disp_name;
362    }
363
364    // 項目数を返す
365    function getCount() {
366        $count = count($this->keyname);
367        return $count;
368    }
369
370    // フォームに渡す用のパラメーターを返す
371    function getFormParamList() {
372        $cnt = 0;
373        foreach($this->keyname as $val) {
374
375            // キー名
376            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
377            // 文字数制限
378            $arrRet[$val]['length'] = $this->length[$cnt];
379            // 入力値
380            if (isset($this->param[$cnt])) {
381                $arrRet[$val]['value'] = $this->param[$cnt];
382            }
383
384            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
385
386            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
387                $arrRet[$val]['value'] = $this->default[$cnt];
388            }
389
390            $cnt++;
391        }
392        return $arrRet;
393    }
394
395    // キー名の一覧を返す
396    function getKeyList() {
397        foreach($this->keyname as $val) {
398            $arrRet[] = $val;
399        }
400        return $arrRet;
401    }
402
403    // キー名と一致した値を返す
404    function getValue($keyname,$default="") {
405        $cnt = 0;
406        $ret = null;
407        foreach($this->keyname as $val) {
408            if($val == $keyname) {
409                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
410                break;
411            }
412            $cnt++;
413        }
414        if(SC_Utils_Ex::isBlank($ret)){
415            $ret = $default;
416        }
417        return $ret;
418    }
419
420    /**
421     * @deprecated
422     */
423    function splitParamCheckBoxes($keyname) {
424        $cnt = 0;
425        foreach($this->keyname as $val) {
426            if($val == $keyname) {
427                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
428                    $this->param[$cnt] = explode("-", $this->param[$cnt]);
429                }
430            }
431            $cnt++;
432        }
433    }
434
435    /**
436     * 入力パラメーターの先頭及び末尾にある空白文字を削除する.
437     *
438     * @param boolean $has_wide_space 全角空白も削除する場合 true
439     * @return void
440     */
441    function trimParam($has_wide_space = true) {
442        $cnt = 0;
443        foreach ($this->keyname as $val) {
444            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
445            $this->recursionTrim($this->param[$cnt], $has_wide_space);
446            $cnt++;
447        }
448    }
449
450    /**
451     * 再帰的に入力パラメーターの先頭及び末尾にある空白文字を削除する.
452     *
453     * @param mixed $value 変換する値. 配列の場合は再帰的に実行する.
454     * @param boolean $has_wide_space 全角空白も削除する場合 true
455     * @return void
456     */
457    function recursionTrim(&$value, $has_wide_space = true) {
458        $pattern = '/^[  \r\n\t]*(.*?)[  \r\n\t]*$/u';
459        if (is_array($value)) {
460            foreach (array_keys($value) as $key) {
461                $this->recursionTrim($value[$key], $convert);
462            }
463        } else {
464            if (!SC_Utils_Ex::isBlank($value)) {
465                if ($has_wide_space) {
466                    $value = preg_replace($pattern, '$1', $value);
467                }
468                $value = trim($value);
469            }
470        }
471    }
472
473    /**
474     * 検索結果引き継ぎ用の連想配列を取得する.
475     *
476     * 引数で指定した文字列で始まるパラメーター名の入力値を連想配列で取得する.
477     *
478     * @param string $prefix パラメーター名の接頭辞
479     * @return array 検索結果引き継ぎ用の連想配列.
480     */
481    function getSearchArray($prefix = 'search_') {
482        $cnt = 0;
483        $arrResults = array();
484        foreach ($this->keyname as $key) {
485            if (preg_match('/^' . $prefix . '/', $key)) {
486                $arrResults[$key] = isset($this->param[$cnt])
487                    ? $this->param[$cnt] : "";
488            }
489            $cnt++;
490        }
491        return $arrResults;
492    }
493
494
495    // addParam の内容をそのまま返す
496    function getFormDispArray() {
497        $cnt = 0;
498        foreach($this->keyname as $val) {
499            // キー名
500            $arrRet[$cnt]['keyname'] = $this->keyname[$cnt];
501            // 文字数制限
502            $arrRet[$cnt]['length'] = $this->length[$cnt];
503
504            $arrRet[$cnt]['disp_name']  = $this->disp_name[$cnt];
505            // 入力値
506            if (isset($this->param[$cnt])) {
507                $arrRet[$cnt]['value'] = $this->param[$cnt];
508            }
509
510            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
511
512            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
513                $arrRet[$cnt]['value'] = $this->default[$cnt];
514            }
515            $cnt++;
516        }
517        return $arrRet;
518    }
519
520
521}
522?>
Note: See TracBrowser for help on using the repository browser.