Ignore:
Timestamp:
2009/09/24 21:47:41 (15 years ago)
Author:
Seasoft
Message:

郵便番号DB登録の改修。

  • ポップアップしないように改善(一部 IFRAME 使用)
  • タイムアウトする環境も考慮(r17934による影響をカバー)
  • fgetcsv 関数の不具合を回避するため、UTF-8 に変換する処理を追加
  • IE6, FX3.0 でデバッグ実施
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/comu-ver2/data/class/pages/admin/basis/LC_Page_Admin_Basis_ZipInstall.php

    r17954 r18317  
    33 * This file is part of EC-CUBE 
    44 * 
    5  * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved. 
     5 * Copyright(c) 2000-2009 LOCKON CO.,LTD. All Rights Reserved. 
    66 * 
    77 * http://www.lockon.co.jp/ 
     
    3333/** 郵便番号CSV ファイルのパス */ 
    3434define("ZIP_CSV_FILE_PATH", DATA_PATH . "downloads/KEN_ALL.CSV"); 
     35 
     36/** UTF-8 変換済みの郵便番号CSV ファイルのパス */ 
     37define("ZIP_CSV_UTF8_FILE_PATH", DATA_PATH . "downloads/KEN_ALL_utf-8.CSV"); 
    3538 
    3639/** 
     
    4346class LC_Page_Admin_Basis_ZipInstall extends LC_Page { 
    4447 
     48    /** CSVの行数 */ 
     49    var $tpl_line = 0; 
     50    var $tpl_mode; 
     51    var $exec; 
     52    var $tpl_count_mtb_zip; 
     53    /** フォームパラメータの配列 */ 
     54    var $objFormParam; 
     55 
    4556    // }}} 
    4657    // {{{ functions 
     
    5364    function init() { 
    5465        parent::init(); 
     66        $this->tpl_mainpage = 'basis/zip_install.tpl'; 
     67        $this->tpl_subnavi = 'basis/subnavi.tpl'; 
     68        $this->tpl_subno = 'zip_install'; 
     69        $this->tpl_subtitle = '郵便番号DB登録'; 
     70        $this->tpl_mainno = 'basis'; 
     71 
     72        $this->tpl_mode = $_GET['mode']; 
     73        $this->exec = (boolean)$_GET['exec']; 
    5574    } 
    5675 
     
    6281    function process() { 
    6382        $objQuery = new SC_Query(); 
     83 
     84        SC_Utils_Ex::sfIsSuccess(new SC_Session); 
     85 
     86        // パラメータ管理クラス 
     87        $this->objFormParam = new SC_FormParam(); 
     88        // パラメータ情報の初期化 
     89        $this->lfInitParam(); 
     90        // POST値の取得 
     91        $this->objFormParam->setParam($_GET); 
     92        $this->arrErr = $this->objFormParam->checkError(); 
     93        $this->arrForm = $this->objFormParam->getHashArray(); 
     94 
     95        if ($this->exec) { 
     96            if (!empty($this->arrErr)) { 
     97                SC_Utils_Ex::sfDispException(); 
     98            } 
     99            switch ($this->tpl_mode) { 
     100                // 自動登録 
     101                case 'auto': 
     102                    $objQuery->begin(); 
     103                    $objQuery->delete('mtb_zip'); 
     104                    $this->insertMtbZip(); 
     105                    $objQuery->commit(); 
     106                    break; 
     107                // 手動登録 
     108                case 'manual': 
     109                    $this->insertMtbZip($this->arrForm['startRowNum']); 
     110                    break; 
     111            } 
     112            exit; 
     113        } 
     114 
     115        switch ($this->tpl_mode) { 
     116            // 手動削除 
     117            case 'delete': 
     118                $objQuery->delete('mtb_zip'); 
     119 
     120                // 進捗・完了画面を表示しない 
     121                $this->tpl_mode = null; 
     122 
     123                break; 
     124        } 
     125 
     126        $this->tpl_line = $this->countZipCsv(); 
     127        $this->tpl_count_mtb_zip = $this->countMtbZip(); 
     128 
     129        $objView = new SC_AdminView(); 
     130 
     131        $objView->assignobj($this); 
     132        $objView->display(MAIN_FRAME); 
     133    } 
     134 
     135    /** 
     136     * デストラクタ. 
     137     * 
     138     * @return void 
     139     */ 
     140    function destroy() { 
     141        parent::destroy(); 
     142    } 
     143 
     144    /** 
     145     * パラメータ情報の初期化 
     146     * 
     147     * @return void 
     148     */ 
     149    function lfInitParam() { 
     150        if ($this->tpl_mode == 'manual') { 
     151            $this->objFormParam->addParam("開始行", "startRowNum", INT_LEN, "n", array("EXIST_CHECK", "MAX_LENGTH_CHECK", "NUM_CHECK")); 
     152        } 
     153    } 
     154 
     155    /** 
     156     * DB登録 
     157     * 
     158     * @return void 
     159     */ 
     160    function insertMtbZip($start = 1) { 
     161        $objQuery = new SC_Query(); 
    64162        $objSess = new SC_Session(); 
    65163 
     
    67165        SC_Utils_Ex::sfIsSuccess($objSess); 
    68166 
    69         $fp = fopen(ZIP_CSV_FILE_PATH, "r"); 
    70         $img_path = USER_URL . "packages/" . TEMPLATE_NAME . "/img/"; 
    71  
    72         echo ('<html>'); 
    73         echo ('<body bgcolor="#494E5F">'); 
     167        $img_path = USER_URL . USER_PACKAGE_DIR . DEFAULT_TEMPLATE_NAME . "/img/"; 
     168 
     169        ?> 
     170        <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> 
     171        <head> 
     172            <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHAR_CODE ?>" /> 
     173        </head> 
     174        <body> 
     175        <p>進捗状況</p> 
     176        <div style="background-color: #494E5F;"> 
     177        <?php 
    74178        // 一部のIEは256バイト以上受け取ってから表示を開始する。 
    75179        SC_Utils_Ex::sfFlush(true); 
    76180 
    77 #('http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh') 
    78         if(!$fp) { 
    79             SC_Utils_Ex::sfErrorHeader(">> " . ZIP_CSV_FILE_PATH . "の取得に失敗しました。"); 
    80         } else { 
    81             print("<img src='". $img_path . "install/main_w.jpg'><br>"); 
    82             SC_Utils_Ex::sfFlush(); 
    83  
    84             // CSVの件数を数える 
    85             $line = 0; 
    86             while(!feof($fp)) { 
    87                 fgets($fp, ZIP_CSV_LINE_MAX); 
    88                 $line++; 
    89             } 
    90  
    91             print("<img src='". $img_path ."install/space_w.gif'>"); 
    92             SC_Utils_Ex::sfFlush(); 
    93  
    94             // ファイルポインタを戻す 
    95             fseek($fp, 0); 
    96  
    97             // 画像を一個表示する件数を求める。 
    98             $disp_line = intval($line / IMAGE_MAX); 
    99  
    100             $objQuery->begin(); 
    101             $objQuery->delete('mtb_zip'); 
    102             $cnt = 1; 
    103             $img_cnt = 0; 
    104             $safe_mode = (boolean)ini_get('safe_mode'); 
    105             $max_execution_time 
    106                 = is_numeric(ini_get('max_execution_time')) 
    107                 ? intval(ini_get('max_execution_time')) 
    108                 : intval(get_cfg_var('max_execution_time')) 
    109             ; 
    110             while (!feof($fp)) { 
    111                 $arrCSV = fgetcsv($fp, ZIP_CSV_LINE_MAX); 
     181        echo "<img src='" . $img_path . "admin/basis/zip_install_progress.gif'><br />"; 
     182        echo "<img src='" . $img_path . "install/space_w.gif'>"; 
     183        SC_Utils_Ex::sfFlush(); 
     184 
     185        // 画像を一個表示する件数を求める。 
     186        $line_all = $this->countZipCsv(); 
     187        $disp_line = intval($line_all / IMAGE_MAX); 
     188 
     189        /** 現在行(CSV形式。空行は除く。) */ 
     190        $cntCurrentLine = 0; 
     191        /** 挿入した行数 */ 
     192        $cntInsert = 0; 
     193        $img_cnt = 0; 
     194        $safe_mode = (boolean)ini_get('safe_mode'); 
     195        $max_execution_time 
     196            = is_numeric(ini_get('max_execution_time')) 
     197            ? intval(ini_get('max_execution_time')) 
     198            : intval(get_cfg_var('max_execution_time')) 
     199        ; 
     200 
     201        $fp = $this->openZipCsv(); 
     202        while (!feof($fp)) { 
     203            $arrCSV = fgetcsv($fp, ZIP_CSV_LINE_MAX); 
     204            if (empty($arrCSV)) continue; 
     205            $cntCurrentLine++; 
     206            if ($cntCurrentLine >= $start) { 
     207                $sqlval = array(); 
    112208                // $sqlval['code'] = $arrCSV[0]; 
    113209                // $sqlval['old_zipcode'] = $arrCSV[1]; 
     
    116212                // $sqlval['city_kana'] = $arrCSV[4]; 
    117213                // $sqlval['town_kana'] = $arrCSV[5]; 
    118                 $sqlval['state'] = mb_convert_encoding($arrCSV[6], CHAR_CODE, 'sjis-win'); 
    119                 $sqlval['city'] = mb_convert_encoding($arrCSV[7], CHAR_CODE, 'sjis-win'); 
    120                 $sqlval['town'] = mb_convert_encoding($arrCSV[8], CHAR_CODE, 'sjis-win'); 
     214                $sqlval['state'] = $arrCSV[6]; 
     215                $sqlval['city'] = $arrCSV[7]; 
     216                $sqlval['town'] = $arrCSV[8]; 
    121217                // $sqlval['flg1'] = $arrCSV[9]; 
    122218                // $sqlval['flg2'] = $arrCSV[10]; 
     
    126222                // $sqlval['flg6'] = $arrCSV[14]; 
    127223                $objQuery->insert("mtb_zip", $sqlval); 
    128                 $cnt++; 
    129                 // $disp_line件ごとに進捗表示する 
    130                 if($cnt % $disp_line == 0 && $img_cnt < IMAGE_MAX) { 
    131                     print("<img src='". $img_path ."install/graph_1_w.gif'>"); 
    132                     SC_Utils_Ex::sfFlush(); 
    133                     $img_cnt++; 
     224                $cntInsert++; 
     225            } 
     226 
     227            // $disp_line件ごとに進捗表示する 
     228            if($cntCurrentLine % $disp_line == 0 && $img_cnt < IMAGE_MAX) { 
     229                print("<img src='". $img_path ."install/graph_1_w.gif'>"); 
     230                SC_Utils_Ex::sfFlush(); 
     231                $img_cnt++; 
     232            } 
     233            // 暴走スレッドが残留する確率を軽減したタイムアウト防止のロジック 
     234            // TODO 動作が安定していれば、SC_Utils 辺りに移動したい。 
     235            if (!$safe_mode) { 
     236                // タイムアウトをリセット 
     237                set_time_limit($max_execution_time); 
     238            } 
     239        } 
     240        fclose($fp); 
     241 
     242        echo "<img src='". $img_path ."install/space_w.gif'>"; 
     243        echo "</div>\n"; 
     244         
     245        ?> 
     246        <script type="text/javascript" language="javascript"> 
     247            <!-- 
     248                // 完了画面 
     249                function complete() { 
     250                    document.open("text/html","replace"); 
     251                    document.clear(); 
     252                    document.write("<p>完了しました。<br />"); 
     253                    document.write("<?php echo $cntInsert ?> 件を追加しました。</p>"); 
     254                    document.write("<p><a href='?' target='_top'>戻る</a></p>"); 
     255                    document.close(); 
    134256                } 
    135                 // 暴走スレッドが残留する確率を軽減したタイムアウト防止のロジック 
    136                 // TODO 動作が安定していれば、SC_Utils 辺りに移動したい。 
    137                 if (!$safe_mode) { 
    138                     // タイムアウトをリセット 
    139                     set_time_limit($max_execution_time); 
    140                 } 
    141             } 
    142             fclose($fp); 
    143             $objQuery->commit(); 
    144  
    145             print("<img src='". $img_path ."install/space_w.gif'><br>\n"); 
    146             print("<table width='700' height='50' border='0' cellpadding='0' cellspacing='0'>\n"); 
    147             print("<tr>\n"); 
    148             print("<td align='center'><a href='javascript:window.close()'><img src='". $img_path ."install/close.gif' alt='CLOSE' width='85' height='22' border='0' /></a></td>\n"); 
    149             print("</tr>\n"); 
    150             print("</table>\n"); 
    151         } 
    152     } 
    153  
    154     /** 
    155      * デストラクタ. 
    156      * 
    157      * @return void 
    158      */ 
    159     function destroy() { 
    160         parent::destroy(); 
     257                // コンテンツを削除するため、タイムアウトで呼び出し。 
     258                setTimeout("complete()", 0); 
     259            // --> 
     260        </script> 
     261        </body> 
     262        </html> 
     263        <?php 
     264    } 
     265 
     266    function openZipCsv() { 
     267        // http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh 
     268        $this->convertZipCsv(); 
     269        $fp = fopen(ZIP_CSV_UTF8_FILE_PATH, "r"); 
     270        if (!$fp) { 
     271            SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_FILE_PATH . ' の読み込みに失敗しました。'); 
     272        } 
     273        return $fp; 
     274    } 
     275 
     276    function convertZipCsv() { 
     277        if (file_exists(ZIP_CSV_UTF8_FILE_PATH)) return; 
     278 
     279        $fpr = fopen(ZIP_CSV_FILE_PATH, "r"); 
     280        if (!$fpr) { 
     281            SC_Utils_Ex::sfDispException(ZIP_CSV_FILE_PATH . ' の読み込みに失敗しました。'); 
     282        } 
     283 
     284        $fpw = fopen(ZIP_CSV_UTF8_FILE_PATH, "w"); 
     285        if (!$fpw) { 
     286            SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_FILE_PATH . ' を開けません。'); 
     287        } 
     288 
     289        while (!feof($fpr)) { 
     290            fwrite($fpw, mb_convert_encoding(fgets($fpr, ZIP_CSV_LINE_MAX), CHAR_CODE, 'sjis-win')); 
     291        } 
     292 
     293        fclose($fpw); 
     294        fclose($fpr); 
     295    } 
     296 
     297    function countMtbZip() { 
     298        $objQuery = new SC_Query(); 
     299        return $objQuery->count('mtb_zip'); 
     300    } 
     301 
     302    function countZipCsv() { 
     303        $line = 0; 
     304        $fp = $this->openZipCsv(); 
     305 
     306        // CSVの行数を数える 
     307        while (!feof($fp)) { 
     308            /* 
     309            // 正確にカウントする 
     310            $tmp = fgetcsv($fp, ZIP_CSV_LINE_MAX); 
     311            */ 
     312            // 推測でカウントする 
     313            $tmp = fgets($fp, ZIP_CSV_LINE_MAX); 
     314            if (empty($tmp)) continue; 
     315            $line++; 
     316        } 
     317        fclose($fp); 
     318 
     319        return $line; 
    161320    } 
    162321} 
Note: See TracChangeset for help on using the changeset viewer.