source: branches/feature-module-update/data/class/SC_FormParam.php @ 16741

Revision 16741, 12.8 KB checked in by adachi, 16 years ago (diff)

set eol-style:LF

  • 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-2007 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/* パラメータ管理クラス */
25class SC_FormParam {
26
27    var $param;
28    var $disp_name;
29    var $keyname;
30    var $length;
31    var $convert;
32    var $arrCheck;
33    var $default;   // 何も入力されていないときに表示する値
34    var $input_db;  // DBにそのまま挿入可能か否か
35    var $html_disp_name;
36
37    // コンストラクタ
38    function SC_FormParam() {
39        $this->check_dir = IMAGE_SAVE_DIR;
40        $this->disp_name = array();
41        $this->keyname = array();
42        $this->length = array();
43        $this->convert = array();
44        $this->arrCheck = array();
45        $this->default = array();
46        $this->input_db = array();
47    }
48
49    // パラメータの追加
50    function addParam($disp_name, $keyname, $length="", $convert="", $arrCheck=array(), $default="", $input_db="true") {
51        $this->disp_name[] = $disp_name;
52        $this->keyname[] = $keyname;
53        $this->length[] = $length;
54        $this->convert[] = $convert;
55        $this->arrCheck[] = $arrCheck;
56        $this->default[] = $default;
57        $this->input_db[] = $input_db;
58    }
59
60    // パラメータの入力
61    // $arrVal  :$arrVal['keyname']・・の配列を一致したキーのインスタンスに格納する
62    // $seq     :trueの場合、$arrVal[0]~の配列を登録順にインスタンスに格納する
63    function setParam($arrVal, $seq = false) {
64        $cnt = 0;
65        if(!$seq){
66            foreach($this->keyname as $val) {
67                if(isset($arrVal[$val])) {
68                    $this->setValue($val, $arrVal[$val]);
69                }
70            }
71        } else {
72            foreach($this->keyname as $val) {
73                $this->param[$cnt] = $arrVal[$cnt];
74                $cnt++;
75            }
76        }
77    }
78
79    // 画面表示用タイトル生成
80    function setHtmlDispNameArray() {
81        $cnt = 0;
82        foreach($this->keyname as $val) {
83            $find = false;
84            foreach($this->arrCheck[$cnt] as $val) {
85                if($val == "EXIST_CHECK") {
86                    $find = true;
87                }
88            }
89
90            if($find) {
91                $this->html_disp_name[$cnt] = $this->disp_name[$cnt] . "<span class='red'>(※ 必須)</span>";
92            } else {
93                $this->html_disp_name[$cnt] = $this->disp_name[$cnt];
94            }
95            $cnt++;
96        }
97    }
98
99    // 画面表示用タイトル取得
100    function getHtmlDispNameArray() {
101        return $this->html_disp_name;
102    }
103
104    // 複数列パラメータの取得
105    function setParamList($arrVal, $keyname) {
106        // DBの件数を取得する。
107        $count = count($arrVal);
108        $no = 1;
109        for($cnt = 0; $cnt < $count; $cnt++) {
110            $key = $keyname.$no;
111            if($arrVal[$cnt][$keyname] != "") {
112                $this->setValue($key, $arrVal[$cnt][$keyname]);
113            }
114            $no++;
115        }
116    }
117
118    function setDBDate($db_date, $year_key = 'year', $month_key = 'month', $day_key = 'day') {
119
120        if (!empty($db_date)) {
121            list($y, $m, $d) = split("[- ]", $db_date);
122            $this->setValue($year_key, $y);
123            $this->setValue($month_key, $m);
124            $this->setValue($day_key, $d);
125        }
126    }
127
128    // キーに対応した値をセットする。
129    function setValue($key, $param) {
130        $cnt = 0;
131        foreach($this->keyname as $val) {
132            if($val == $key) {
133                $this->param[$cnt] = $param;
134                break;
135            }
136            $cnt++;
137        }
138    }
139
140    function toLower($key) {
141        $cnt = 0;
142        foreach($this->keyname as $val) {
143            if($val == $key) {
144                $this->param[$cnt] = strtolower($this->param[$cnt]);
145                break;
146            }
147            $cnt++;
148        }
149    }
150
151    // エラーチェック
152    function checkError($br = true, $keyname = "") {
153        // 連想配列の取得
154        $arrRet = $this->getHashArray($keyname);
155        $objErr = new SC_CheckError($arrRet);
156        $cnt = 0;
157        foreach($this->keyname as $val) {
158            foreach($this->arrCheck[$cnt] as $func) {
159                if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
160                switch($func) {
161                case 'EXIST_CHECK':
162                case 'NUM_CHECK':
163                case 'EMAIL_CHECK':
164                case 'EMAIL_CHAR_CHECK':
165                case 'ALNUM_CHECK':
166                case 'KANA_CHECK':
167                case 'URL_CHECK':
168                case 'SPTAB_CHECK':
169                case 'ZERO_CHECK':
170                case 'ALPHA_CHECK':
171                case 'ZERO_START':
172                case 'FIND_FILE':
173                case 'NO_SPTAB':
174                case 'DIR_CHECK':
175                case 'DOMAIN_CHECK':
176                case 'FILE_NAME_CHECK':
177                case 'MOBILE_EMAIL_CHECK':
178
179                    if(!is_array($this->param[$cnt])) {
180                        $objErr->doFunc(array($this->disp_name[$cnt], $val), array($func));
181                    } else {
182                        $max = count($this->param[$cnt]);
183                        for($i = 0; $i < $max; $i++) {
184                            $objSubErr = new SC_CheckError($this->param[$cnt]);
185                            $objSubErr->doFunc(array($this->disp_name[$cnt], $i), array($func));
186                            if(count($objSubErr->arrErr) > 0) {
187                                foreach($objSubErr->arrErr as $mess) {
188                                    if($mess != "") {
189                                        $objErr->arrErr[$val] = $mess;
190                                    }
191                                }
192                            }
193                        }
194                    }
195                    break;
196                case 'MAX_LENGTH_CHECK':
197                case 'NUM_COUNT_CHECK':
198                    if(!is_array($this->param[$cnt])) {
199                        $objErr->doFunc(array($this->disp_name[$cnt], $val, $this->length[$cnt]), array($func));
200                    } else {
201                        $max = count($this->param[$cnt]);
202                        for($i = 0; $i < $max; $i++) {
203                            $objSubErr = new SC_CheckError($this->param[$cnt]);
204                            $objSubErr->doFunc(array($this->disp_name[$cnt], $i, $this->length[$cnt]), array($func));
205                            if(count($objSubErr->arrErr) > 0) {
206                                foreach($objSubErr->arrErr as $mess) {
207                                    if($mess != "") {
208                                        $objErr->arrErr[$val] = $mess;
209                                    }
210                                }
211                            }
212                        }
213                    }
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                default:
226                    $objErr->arrErr[$val] = "※※ エラーチェック形式($func)には対応していません ※※ <br>";
227                    break;
228                }
229            }
230
231            if (isset($objErr->arrErr[$val]) && !$br) {
232                $objErr->arrErr[$val] = ereg_replace("<br>$", "", $objErr->arrErr[$val]);
233            }
234            $cnt++;
235        }
236        return $objErr->arrErr;
237    }
238
239    // 入力文字の変換
240    function convParam() {
241        /*
242         *  文字列の変換
243         *  K :  「半角(ハンカク)片仮名」を「全角片仮名」に変換
244         *  C :  「全角ひら仮名」を「全角かた仮名」に変換
245         *  V :  濁点付きの文字を一文字に変換。"K","H"と共に使用します
246         *  n :  「全角」数字を「半角(ハンカク)」に変換
247         *  a :  「全角」英字を「半角」英字に変換
248         */
249        $cnt = 0;
250        foreach ($this->keyname as $val) {
251            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
252
253            if(!is_array($this->param[$cnt])) {
254                if($this->convert[$cnt] != "") {
255                    $this->param[$cnt] = mb_convert_kana($this->param[$cnt] ,$this->convert[$cnt]);
256                }
257            } else {
258                $max = count($this->param[$cnt]);
259                for($i = 0; $i < $max; $i++) {
260                    if($this->convert[$cnt] != "") {
261                        $this->param[$cnt][$i] = mb_convert_kana($this->param[$cnt][$i] ,$this->convert[$cnt]);
262                    }
263                }
264            }
265            $cnt++;
266        }
267    }
268
269    // 連想配列の作成
270    function getHashArray($keyname = "") {
271        $arrRet = array();
272        $cnt = 0;
273        foreach($this->keyname as $val) {
274            if($keyname == "" || $keyname == $val) {
275                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
276                $cnt++;
277            }
278        }
279        return $arrRet;
280    }
281
282    // DB格納用配列の作成
283    function getDbArray() {
284        $cnt = 0;
285        foreach ($this->keyname as $val) {
286            if ($this->input_db[$cnt]) {
287                $arrRet[$val] = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
288            }
289            $cnt++;
290        }
291        return $arrRet;
292    }
293
294    // 配列の縦横を入れ替えて返す
295    function getSwapArray($arrKey) {
296        foreach($arrKey as $keyname) {
297            $arrVal = $this->getValue($keyname);
298            $max = count($arrVal);
299            for($i = 0; $i < $max; $i++) {
300                $arrRet[$i][$keyname] = $arrVal[$i];
301            }
302        }
303        return $arrRet;
304    }
305
306    // 項目名一覧の取得
307    function getTitleArray() {
308        return $this->disp_name;
309    }
310
311    // 項目数を返す
312    function getCount() {
313        $count = count($this->keyname);
314        return $count;
315    }
316
317    // フォームに渡す用のパラメータを返す
318    function getFormParamList() {
319        $cnt = 0;
320        foreach($this->keyname as $val) {
321
322            // キー名
323            $arrRet[$val]['keyname'] = $this->keyname[$cnt];
324            // 文字数制限
325            $arrRet[$val]['length'] = $this->length[$cnt];
326            // 入力値
327            if (isset($this->param[$cnt])) {
328                $arrRet[$val]['value'] = $this->param[$cnt];
329            }
330
331            if (!isset($this->param[$cnt])) $this->param[$cnt] = "";
332
333            if($this->default[$cnt] != "" && $this->param[$cnt] == "") {
334                $arrRet[$val]['value'] = $this->default[$cnt];
335            }
336
337            $cnt++;
338        }
339        return $arrRet;
340    }
341
342    // キー名の一覧を返す
343    function getKeyList() {
344        foreach($this->keyname as $val) {
345            $arrRet[] = $val;
346        }
347        return $arrRet;
348    }
349
350    // キー名と一致した値を返す
351    function getValue($keyname) {
352        $cnt = 0;
353        foreach($this->keyname as $val) {
354            if($val == $keyname) {
355                $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : "";
356                break;
357            }
358            $cnt++;
359        }
360        return $ret;
361    }
362
363    function splitCheckBoxes($keyname) {
364        $cnt = 0;
365        foreach($this->keyname as $val) {
366            if($val == $keyname) {
367                $this->param[$cnt] = sfSplitCheckBoxes($this->param[$cnt]);
368            }
369            $cnt++;
370        }
371    }
372
373    function splitParamCheckBoxes($keyname) {
374        $cnt = 0;
375        foreach($this->keyname as $val) {
376            if($val == $keyname) {
377                if(isset($this->param[$cnt]) && !is_array($this->param[$cnt])) {
378                    $this->param[$cnt] = split("-", $this->param[$cnt]);
379                }
380            }
381            $cnt++;
382        }
383    }
384}
385?>
Note: See TracBrowser for help on using the repository browser.