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

Revision 19805, 14.0 KB checked in by Seasoft, 13 years ago (diff)

#834(パラメータの定数名に「URL」を含むにもかかわらず、パスのみのものがある) 一部実装

  • 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 = new SC_CheckError($arrRet);
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 'SPTAB_CHECK':
193                case 'ZERO_CHECK':
194                case 'ALPHA_CHECK':
195                case 'ZERO_START':
196                case 'FIND_FILE':
197                case 'NO_SPTAB':
198                case 'DIR_CHECK':
199                case 'DOMAIN_CHECK':
200                case 'FILE_NAME_CHECK':
201                case 'MOBILE_EMAIL_CHECK':
202
203                    if(!is_array($this->param[$cnt])) {
204                        $objErr->doFunc(array($this->disp_name[$cnt], $val), array($func));
205                    } else {
206                        $max = count($this->param[$cnt]);
207                        for($i = 0; $i < $max; $i++) {
208                            $objSubErr = new SC_CheckError($this->param[$cnt]);
209                            $objSubErr->doFunc(array($this->disp_name[$cnt], $i), array($func));
210                            if(count($objSubErr->arrErr) > 0) {
211                                foreach($objSubErr->arrErr as $mess) {
212                                    if($mess != "") {
213                                        $objErr->arrErr[$val] = $mess;
214                                    }
215                                }
216                            }
217                        }
218                    }
219                    break;
220                case 'MAX_LENGTH_CHECK':
221                case 'MIN_LENGTH_CHECK':
222                case 'NUM_COUNT_CHECK':
223                    if(!is_array($this->param[$cnt])) {
224                        $objErr->doFunc(array($this->disp_name[$cnt], $val, $this->length[$cnt]), array($func));
225                    } else {
226                        $max = count($this->param[$cnt]);
227                        for($i = 0; $i < $max; $i++) {
228                            $objSubErr = new SC_CheckError($this->param[$cnt]);
229                            $objSubErr->doFunc(array($this->disp_name[$cnt], $i, $this->length[$cnt]), array($func));
230                            if(count($objSubErr->arrErr) > 0) {
231                                foreach($objSubErr->arrErr as $mess) {
232                                    if($mess != "") {
233                                        $objErr->arrErr[$val] = $mess;
234                                    }
235                                }
236                            }
237                        }
238                    }
239                    break;
240                // 小文字に変換
241                case 'CHANGE_LOWER':
242                    $this->param[$cnt] = strtolower($this->param[$cnt]);
243                    break;
244                // ファイルの存在チェック
245                case 'FILE_EXISTS':
246                    if($this->param[$cnt] != "" && !file_exists($this->check_dir . $this->param[$cnt])) {
247                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
248                    }
249                    break;
250                // ダウンロード用ファイルの存在チェック
251                case 'DOWN_FILE_EXISTS':
252                    if($this->param[$cnt] != "" && !file_exists(DOWN_SAVE_REALDIR . $this->param[$cnt])) {
253                        $objErr->arrErr[$val] = "※ " . $this->disp_name[$cnt] . "のファイルが存在しません。<br>";
254                    }
255                    break;
256                default:
257                    $objErr->arrErr[$val] = "※※ エラーチェック形式($func)には対応していません ※※ <br>";
258                    break;
259                }
260            }
261
262            if (isset($objErr->arrErr[$val]) && !$br) {
263                $objErr->arrErr[$val] = ereg_replace("<br>$", "", $objErr->arrErr[$val]);
264            }
265            $cnt++;
266        }
267        return $objErr->arrErr;
268    }
269
270    // 入力文字の変換
271    function convParam() {
272        /*
273         *  文字列の変換
274         *  K :  「半角(ハンカク)片仮名」を「全角片仮名」に変換
275         *  C :  「全角ひら仮名」を「全角かた仮名」に変換
276         *  V :  濁点付きの文字を一文字に変換。"K","H"と共に使用します
277         *  n :  「全角」数字を「半角(ハンカク)」に変換
278         *  a :  「全角」英字を「半角」英字に変換
279         */
280        $cnt = 0;
281        foreach ($this->keyname as $val) {
282            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
283
284            if(!is_array($this->param[$cnt])) {
285                if($this->convert[$cnt] != "") {
286                    $this->param[$cnt] = mb_convert_kana($this->param[$cnt] ,$this->convert[$cnt]);
287                }
288            } else {
289                $max = count($this->param[$cnt]);
290                for($i = 0; $i < $max; $i++) {
291                    if($this->convert[$cnt] != "") {
292                        $this->param[$cnt][$i] = mb_convert_kana($this->param[$cnt][$i] ,$this->convert[$cnt]);
293                    }
294                }
295            }
296            $cnt++;
297        }
298    }
299
300    // 連想配列の作成
301    function getHashArray($keyname = "") {
302        $arrRet = array();
303        $cnt = 0;
304        foreach($this->keyname as $val) {
305            if($keyname == "" || $keyname == $val) {
306                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
307                $cnt++;
308            }
309        }
310        return $arrRet;
311    }
312
313    // DB格納用配列の作成
314    function getDbArray() {
315        $cnt = 0;
316        foreach ($this->keyname as $val) {
317            if ($this->input_db[$cnt]) {
318                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
319            }
320            $cnt++;
321        }
322        return $arrRet;
323    }
324
325    // 配列の縦横を入れ替えて返す
326    function getSwapArray($arrKey) {
327        foreach($arrKey as $keyname) {
328            $arrVal = $this->getValue($keyname);
329            $max = count($arrVal);
330            for($i = 0; $i < $max; $i++) {
331                $arrRet[$i][$keyname] = $arrVal[$i];
332            }
333        }
334        return $arrRet;
335    }
336
337    // 項目名一覧の取得
338    function getTitleArray() {
339        return $this->disp_name;
340    }
341
342    // 項目数を返す
343    function getCount() {
344        $count = count($this->keyname);
345        return $count;
346    }
347
348    // フォームに渡す用のパラメータを返す
349    function getFormParamList() {
350        $cnt = 0;
351        foreach($this->keyname as $val) {
352
353            // キー名
354            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
355            // 文字数制限
356            $arrRet[$val]['length'] = $this->length[$cnt];
357            // 入力値
358            if (isset($this->param[$cnt])) {
359                $arrRet[$val]['value'] = $this->param[$cnt];
360            }
361
362            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
363
364            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
365                $arrRet[$val]['value'] = $this->default[$cnt];
366            }
367
368            $cnt++;
369        }
370        return $arrRet;
371    }
372
373    // キー名の一覧を返す
374    function getKeyList() {
375        foreach($this->keyname as $val) {
376            $arrRet[] = $val;
377        }
378        return $arrRet;
379    }
380
381    // キー名と一致した値を返す
382    function getValue($keyname) {
383        $cnt = 0;
384        foreach($this->keyname as $val) {
385            if($val == $keyname) {
386                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
387                break;
388            }
389            $cnt++;
390        }
391        return $ret;
392    }
393
394    function splitCheckBoxes($keyname) {
395        $cnt = 0;
396        foreach($this->keyname as $val) {
397            if($val == $keyname) {
398                $this->param[$cnt] = sfSplitCheckBoxes($this->param[$cnt]);
399            }
400            $cnt++;
401        }
402    }
403
404    function splitParamCheckBoxes($keyname) {
405        $cnt = 0;
406        foreach($this->keyname as $val) {
407            if($val == $keyname) {
408                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
409                    $this->param[$cnt] = split("-", $this->param[$cnt]);
410                }
411            }
412            $cnt++;
413        }
414    }
415}
416?>
Note: See TracBrowser for help on using the repository browser.