Ignore:
Timestamp:
2012/03/14 14:37:35 (12 years ago)
Author:
Seasoft
Message:

#1700 (SC_FormParam#addParam 第6引数が一部メソッドにしか適用されていない)
#1701 (SC_FormParam#getFormParamList 空文字がデフォルト値に書き換えられる)
#1702 (SC_FormParam#initParam 一部クラス変数の初期化漏れ)
#1684 (PHPの言語特性に合わせた簡素な実装に改善する)
#1613 (typo修正・ソース整形・ソースコメントの改善)

Location:
branches/version-2_12-dev/data/class
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_12-dev/data/class/SC_FormParam.php

    r21593 r21645  
    3232class SC_FormParam { 
    3333 
    34     var $param; 
    35     var $disp_name; 
    36     var $keyname; 
    37     var $length; 
    38     var $convert; 
    39     var $arrCheck; 
    40     /** 何も入力されていないときに表示する値 */ 
    41     var $default; 
     34    /** 
     35     * 何も入力されていないときに表示する値 
     36     * キーはキー名 
     37     */ 
     38    var $arrValue = array(); 
     39 
     40    /** 表示名 */ 
     41    var $disp_name = array(); 
     42 
     43    /** キー名 */ 
     44    var $keyname = array(); 
     45 
     46    var $length = array(); 
     47    var $convert = array(); 
     48    var $arrCheck = array(); 
     49 
     50    /** 
     51     * 何も入力されていないときに表示する値 
     52     * キーはキー名 
     53     */ 
     54    var $arrDefault = array(); 
     55 
    4256    /** DBにそのまま挿入可能か否か */ 
    43     var $input_db; 
    44     var $html_disp_name; 
    45  
    46     // コンストラクタ 
    47     function SC_FormParam() { 
     57    var $input_db = array(); 
     58 
     59    var $html_disp_name = array(); 
     60 
     61    /** 
     62     * コンストラクタ 
     63     */ 
     64    function __construct() { 
    4865        $this->check_dir = IMAGE_SAVE_REALDIR; 
    49         $this->initParam(); 
    50     } 
    51  
    52     /** 
    53      * パラメーターの初期化 
    54      * 
    55      * @return void 
     66    } 
     67 
     68    /** 
     69     * 前方互換用 
     70     * 
     71     * @deprecated 2.12.0 #1702 
    5672     */ 
    5773    function initParam() { 
     
    6177        $this->convert = array(); 
    6278        $this->arrCheck = array(); 
    63         $this->default = array(); 
     79        $this->arrDefault = array(); 
    6480        $this->input_db = array(); 
    6581    } 
    6682 
    6783    // パラメーターの追加 
    68     function addParam($disp_name, $keyname, $length = '', $convert = '', $arrCheck = array(), $default = '', $input_db = 'true') { 
     84    function addParam($disp_name, $keyname, $length = '', $convert = '', $arrCheck = array(), $default = '', $input_db = true) { 
    6985        $this->disp_name[] = $disp_name; 
    7086        $this->keyname[] = $keyname; 
     
    7288        $this->convert[] = $convert; 
    7389        $this->arrCheck[] = $arrCheck; 
    74         $this->default[] = $default; 
     90        // XXX このタイミングで arrValue へ格納するほうがスマートかもしれない。しかし、バリデーションや変換の対象となるので、その良し悪しは気になる。 
     91        $this->arrDefault[$keyname] = $default; 
    7592        $this->input_db[] = $input_db; 
    7693    } 
     
    8198    function setParam($arrVal, $seq = false) { 
    8299        if (!is_array($arrVal)) return; 
    83         $cnt = 0; 
    84100        if (!$seq) { 
    85             foreach ($this->keyname as $val) { 
    86                 if (array_key_exists($val, $arrVal)) { 
    87                     $this->setValue($val, $arrVal[$val]); 
    88                 } 
     101            foreach ($arrVal as $key => $val) { 
     102                $this->setValue($key, $val); 
    89103            } 
    90104        } else { 
    91             foreach ($this->keyname as $val) { 
    92                 $this->param[$cnt] = $arrVal[$cnt]; 
    93                 $cnt++; 
     105            foreach ($this->keyname as $index => $key) { 
     106                $this->setValue($key, $arrVal[$index]); 
    94107            } 
    95108        } 
     
    98111    // 画面表示用タイトル生成 
    99112    function setHtmlDispNameArray() { 
    100         $cnt = 0; 
    101         foreach ($this->keyname as $val) { 
     113        foreach ($this->keyname as $index => $key) { 
    102114            $find = false; 
    103             foreach ($this->arrCheck[$cnt] as $val) { 
     115            foreach ($this->arrCheck[$index] as $val) { 
    104116                if ($val == 'EXIST_CHECK') { 
    105117                    $find = true; 
     
    108120 
    109121            if ($find) { 
    110                 $this->html_disp_name[$cnt] = $this->disp_name[$cnt] . '<span class="red">(※ 必須)</span>'; 
     122                $this->html_disp_name[$index] = $this->disp_name[$index] . '<span class="red">(※ 必須)</span>'; 
    111123            } else { 
    112                 $this->html_disp_name[$cnt] = $this->disp_name[$cnt]; 
    113             } 
    114             if ($this->default[$cnt] != '') { 
    115                 $this->html_disp_name[$cnt] .= ' [省略時初期値: ' . $this->default[$cnt] . ']'; 
    116             } 
    117             if ($this->input_db[$cnt] == false) { 
    118                 $this->html_disp_name[$cnt] .= ' [登録・更新不可] '; 
    119             } 
    120             $cnt++; 
     124                $this->html_disp_name[$index] = $this->disp_name[$index]; 
     125            } 
     126            if ($this->arrDefault[$key] != '') { 
     127                $this->html_disp_name[$index] .= ' [省略時初期値: ' . $this->arrDefault[$key] . ']'; 
     128            } 
     129            if ($this->input_db[$index] == false) { 
     130                $this->html_disp_name[$index] .= ' [登録・更新不可] '; 
     131            } 
    121132        } 
    122133    } 
     
    128139 
    129140    // 複数列パラメーターの取得 
    130     function setParamList($arrVal, $keyname) { 
     141    function setParamList($arrVal2d, $keyname) { 
    131142        // DBの件数を取得する。 
    132         $count = count($arrVal); 
    133143        $no = 1; 
    134         for ($cnt = 0; $cnt < $count; $cnt++) { 
    135             $key = $keyname.$no; 
    136             if ($arrVal[$cnt][$keyname] != '') { 
    137                 $this->setValue($key, $arrVal[$cnt][$keyname]); 
    138             } 
     144        foreach ($arrVal2d as $arrVal) { 
     145            $key = $keyname . $no; 
     146            $this->setValue($key, $arrVal[$keyname]); 
    139147            $no++; 
    140148        } 
     
    142150 
    143151    function setDBDate($db_date, $year_key = 'year', $month_key = 'month', $day_key = 'day') { 
    144  
    145         if (!empty($db_date)) { 
    146             list($y, $m, $d) = preg_split('/[- ]/', $db_date); 
    147             $this->setValue($year_key, $y); 
    148             $this->setValue($month_key, $m); 
    149             $this->setValue($day_key, $d); 
    150         } 
     152        if (empty($db_date)) { 
     153            return; 
     154        } 
     155        list($y, $m, $d) = preg_split('/[- ]/', $db_date); 
     156        $this->setValue($year_key, $y); 
     157        $this->setValue($month_key, $m); 
     158        $this->setValue($day_key, $d); 
    151159    } 
    152160 
    153161    // キーに対応した値をセットする。 
    154     function setValue($key, $param) { 
    155         $cnt = 0; 
    156         foreach ($this->keyname as $val) { 
    157             if ($val == $key) { 
    158                 $this->param[$cnt] = $param; 
    159                 // 複数一致の場合もあるので break してはいけない。 
    160             } 
    161             $cnt++; 
    162         } 
     162    function setValue($key, $value) { 
     163        if (!in_array($key, $this->keyname)) { 
     164            // TODO 警告発生 
     165            return; 
     166        } 
     167        $this->arrValue[$key] = $value; 
    163168    } 
    164169 
    165170    function toLower($key) { 
    166         $cnt = 0; 
    167         foreach ($this->keyname as $val) { 
    168             if ($val == $key) { 
    169                 $this->param[$cnt] = strtolower($this->param[$cnt]); 
    170                 // 複数一致の場合もあるので break してはいけない。 
    171             } 
    172             $cnt++; 
     171        if (isset($this->arrValue[$key])) { 
     172            $this->arrValue[$key] = strtolower($this->arrValue[$key]); 
    173173        } 
    174174    } 
     
    178178        $objErr->arrErr = array(); 
    179179 
    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] = ''; 
     180        foreach ($this->keyname as $index => $key) { 
     181            foreach ($this->arrCheck[$index] as $func) { 
     182                $value = $this->getValue($key); 
    184183                switch ($func) { 
    185184                    case 'EXIST_CHECK': 
     
    208207                    case 'SELECT_CHECK': 
    209208                    case 'FILE_NAME_CHECK_BY_NOUPLOAD': 
    210                         $this->recursionCheck($this->disp_name[$cnt], $func, 
    211                                               $this->param[$cnt], $objErr->arrErr, 
    212                                               $val, $this->length[$cnt]); 
     209                        $this->recursionCheck($this->disp_name[$index], $func, 
     210                            $value, $objErr->arrErr, $key, $this->length[$index]); 
    213211                        break; 
    214212                    // 小文字に変換 
    215213                    case 'CHANGE_LOWER': 
    216                         $this->param[$cnt] = strtolower($this->param[$cnt]); 
     214                        $this->toLower($key); 
    217215                        break; 
    218216                    // ファイルの存在チェック 
    219217                    case 'FILE_EXISTS': 
    220                         if ($this->param[$cnt] != '' && !file_exists($this->check_dir . $this->param[$cnt])) { 
    221                             $objErr->arrErr[$val] = '※ ' . $this->disp_name[$cnt] . 'のファイルが存在しません。<br>'; 
     218                        if ($value != '' && !file_exists($this->check_dir . $value)) { 
     219                            $objErr->arrErr[$key] = '※ ' . $this->disp_name[$index] . 'のファイルが存在しません。<br>'; 
    222220                        } 
    223221                        break; 
    224222                    // ダウンロード用ファイルの存在チェック 
    225223                    case 'DOWN_FILE_EXISTS': 
    226                         if ($this->param[$cnt] != '' && !file_exists(DOWN_SAVE_REALDIR . $this->param[$cnt])) { 
    227                             $objErr->arrErr[$val] = '※ ' . $this->disp_name[$cnt] . 'のファイルが存在しません。<br>'; 
     224                        if ($value != '' && !file_exists(DOWN_SAVE_REALDIR . $value)) { 
     225                            $objErr->arrErr[$key] = '※ ' . $this->disp_name[$index] . 'のファイルが存在しません。<br>'; 
    228226                        } 
    229227                        break; 
    230228                    default: 
    231                         $objErr->arrErr[$val] = "※※ エラーチェック形式($func)には対応していません ※※ <br>"; 
     229                        $objErr->arrErr[$key] = "※※ エラーチェック形式($func)には対応していません ※※ <br>"; 
    232230                        break; 
    233231                } 
    234232            } 
    235233 
    236             if (isset($objErr->arrErr[$val]) && !$br) { 
    237                 $objErr->arrErr[$val] = preg_replace("/<br(\s+\/)?>/i", '', $objErr->arrErr[$val]); 
    238             } 
    239             $cnt++; 
     234            if (isset($objErr->arrErr[$key]) && !$br) { 
     235                $objErr->arrErr[$key] = preg_replace("/<br(\s+\/)?>/i", '', $objErr->arrErr[$key]); 
     236            } 
    240237        } 
    241238        return $objErr->arrErr; 
     
    260257     */ 
    261258    function recursionCheck($disp_name, $func, $value, &$arrErr, $error_key, 
    262                             $length = 0, $depth = 0, $error_last_key = null) { 
     259        $length = 0, $depth = 0, $error_last_key = null 
     260    ) { 
    263261        if (is_array($value)) { 
    264262            $depth++; 
     
    290288     * フォームの入力パラメーターに応じて, 再帰的に mb_convert_kana 関数を実行する. 
    291289     * 
    292      * @return voi 
     290     * @return void 
    293291     * @see mb_convert_kana 
    294292     */ 
    295293    function convParam() { 
    296         $cnt = 0; 
    297         foreach ($this->keyname as $val) { 
    298             if (!isset($this->param[$cnt])) $this->param[$cnt] = ''; 
    299             $this->recursionConvParam($this->param[$cnt], $this->convert[$cnt]); 
    300             $cnt++; 
     294        foreach ($this->keyname as $index => $key) { 
     295            if (isset($this->arrValue[$key])) { 
     296                $this->recursionConvParam($this->arrValue[$key], $this->convert[$index]); 
     297            } 
    301298        } 
    302299    } 
     
    330327        foreach ($this->keyname as $index => $keyname) { 
    331328            if (empty($arrKey) || in_array($keyname, $arrKey)) { 
    332                 $arrRet[$keyname] = isset($this->param[$index]) ? $this->param[$index] : ''; 
     329                $arrRet[$keyname] = $this->getValue($keyname); 
    333330            } 
    334331        } 
     
    338335    // DB格納用配列の作成 
    339336    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++; 
     337        foreach ($this->keyname as $index => $key) { 
     338            if ($this->input_db[$index]) { 
     339                $arrRet[$key] = $this->getValue($key); 
     340            } 
    346341        } 
    347342        return $arrRet; 
     
    372367    // フォームに渡す用のパラメーターを返す 
    373368    function getFormParamList() { 
    374         $cnt = 0; 
    375         foreach ($this->keyname as $val) { 
    376  
     369        foreach ($this->keyname as $index => $key) { 
    377370            // キー名 
    378             $arrRet[$val]['keyname'] = $this->keyname[$cnt]; 
     371            $arrRet[$key]['keyname'] = $key; 
     372            // 表示名 
     373            $arrRet[$key]['disp_name'] = $this->disp_name[$index]; 
    379374            // 文字数制限 
    380             $arrRet[$val]['length'] = $this->length[$cnt]; 
     375            $arrRet[$key]['length'] = $this->length[$index]; 
    381376            // 入力値 
    382             if (isset($this->param[$cnt])) { 
    383                 $arrRet[$val]['value'] = $this->param[$cnt]; 
    384             } 
    385  
    386             if (!isset($this->param[$cnt])) $this->param[$cnt] = ''; 
    387  
    388             if ($this->default[$cnt] != '' && $this->param[$cnt] == '') { 
    389                 $arrRet[$val]['value'] = $this->default[$cnt]; 
    390             } 
    391  
    392             $cnt++; 
     377            $arrRet[$key]['value'] = $this->getValue($key); 
    393378        } 
    394379        return $arrRet; 
     
    406391    // キー名と一致した値を返す 
    407392    function getValue($keyname, $default = '') { 
    408         $cnt = 0; 
    409393        $ret = null; 
    410         foreach ($this->keyname as $val) { 
    411             if ($val == $keyname) { 
    412                 $ret = isset($this->param[$cnt]) ? $this->param[$cnt] : ''; 
     394        foreach ($this->keyname as $index => $key) { 
     395            if ($key == $keyname) { 
     396                $ret = isset($this->arrValue[$key]) ? $this->arrValue[$key] : $this->arrDefault[$key]; 
    413397                break; 
    414398            } 
    415             $cnt++; 
    416399        } 
    417400 
     
    434417     */ 
    435418    function splitParamCheckBoxes($keyname) { 
    436         $cnt = 0; 
    437         foreach ($this->keyname as $val) { 
    438             if ($val == $keyname) { 
    439                 if (isset($this->param[$cnt]) && !is_array($this->param[$cnt])) { 
    440                     $this->param[$cnt] = explode('-', $this->param[$cnt]); 
     419        foreach ($this->keyname as $key) { 
     420            if ($key == $keyname) { 
     421                if (isset($this->arrValue[$key]) && !is_array($this->arrValue[$key])) { 
     422                    $this->arrValue[$key] = explode('-', $this->arrValue[$key]); 
    441423                } 
    442424            } 
    443             $cnt++; 
    444425        } 
    445426    } 
     
    452433     */ 
    453434    function trimParam($has_wide_space = true) { 
    454         $cnt = 0; 
    455         foreach ($this->keyname as $val) { 
    456             if (!isset($this->param[$cnt])) $this->param[$cnt] = ''; 
    457             $this->recursionTrim($this->param[$cnt], $has_wide_space); 
    458             $cnt++; 
     435        foreach ($this->arrValue as &$value) { 
     436            $this->recursionTrim($value, $has_wide_space); 
    459437        } 
    460438    } 
     
    492470     */ 
    493471    function getSearchArray($prefix = 'search_') { 
    494         $cnt = 0; 
    495472        $arrResults = array(); 
    496         foreach ($this->keyname as $key) { 
     473        foreach ($this->keyname as $index => $key) { 
    497474            if (preg_match('/^' . $prefix . '/', $key)) { 
    498                 $arrResults[$key] = isset($this->param[$cnt]) 
    499                     ? $this->param[$cnt] : ''; 
    500             } 
    501             $cnt++; 
     475                $arrResults[$key] = $this->getValue($key); 
     476            } 
    502477        } 
    503478        return $arrResults; 
    504479    } 
    505480 
    506  
    507     // addParam の内容をそのまま返す 
     481    /** 
     482     * 前方互換用 
     483     * 
     484     * 1次キーが添字なのが特徴だったと思われる。 
     485     * @deprecated 2.12.0 必要ならば getFormParamList メソッドに引数を追加するなどで実現可能 
     486     */ 
    508487    function getFormDispArray() { 
    509         $cnt = 0; 
    510         foreach ($this->keyname as $val) { 
     488        foreach ($this->keyname as $index => $key) { 
    511489            // キー名 
    512             $arrRet[$cnt]['keyname'] = $this->keyname[$cnt]; 
     490            $arrRet[$index]['keyname'] = $key; 
     491            // 表示名 
     492            $arrRet[$index]['disp_name']  = $this->disp_name[$index]; 
    513493            // 文字数制限 
    514             $arrRet[$cnt]['length'] = $this->length[$cnt]; 
    515  
    516             $arrRet[$cnt]['disp_name']  = $this->disp_name[$cnt]; 
     494            $arrRet[$index]['length'] = $this->length[$index]; 
    517495            // 入力値 
    518             if (isset($this->param[$cnt])) { 
    519                 $arrRet[$cnt]['value'] = $this->param[$cnt]; 
    520             } 
    521  
    522             if (!isset($this->param[$cnt])) $this->param[$cnt] = ''; 
    523  
    524             if ($this->default[$cnt] != '' && $this->param[$cnt] == '') { 
    525                 $arrRet[$cnt]['value'] = $this->default[$cnt]; 
    526             } 
    527             $cnt++; 
     496            $arrRet[$index]['value'] = $this->getValue($key); 
    528497        } 
    529498        return $arrRet; 
    530499    } 
    531  
    532  
    533500} 
  • branches/version-2_12-dev/data/class/pages/admin/basis/LC_Page_Admin_Basis_DeliveryInput.php

    r21592 r21645  
    7575        $objPlugin->doAction('lc_page_admin_basis_deliveryinput_action_start', array($this)); 
    7676 
    77         $objFormParam = new SC_FormParam_Ex(); 
    7877        $this->lfInitParam($this->mode, $objFormParam); 
    7978        $objFormParam->setParam($_POST); 
     
    118117    /* パラメーター情報の初期化 */ 
    119118    function lfInitParam($mode, &$objFormParam) { 
    120         $objFormParam->initParam(); 
     119        $objFormParam = new SC_FormParam_Ex(); 
    121120 
    122121        switch ($mode) { 
Note: See TracChangeset for help on using the changeset viewer.