source: branches/version-2_5-dev/data/class/pages/admin/basis/LC_Page_Admin_Basis_ZipInstall.php @ 19805

Revision 19805, 10.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 Revision Date
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
RevLine 
[15335]1<?php
2/*
[16582]3 * This file is part of EC-CUBE
4 *
[18701]5 * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
[15335]6 *
7 * http://www.lockon.co.jp/
[16582]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.
[15335]22 */
23
24// {{{ requires
[19805]25require_once(CLASS_REALDIR . "pages/admin/LC_Page_Admin.php");
[15335]26
27/** CSV ファイルの最大行数 */
28define("ZIP_CSV_LINE_MAX", 8192);
29
[17444]30/** 画像の表示数量 */
[15335]31define("IMAGE_MAX", 680);
32
33/** 郵便番号CSV ファイルのパス */
[19805]34define("ZIP_CSV_REALFILE", DATA_REALDIR . "downloads/KEN_ALL.CSV");
[15335]35
[18317]36/** UTF-8 変換済みの郵便番号CSV ファイルのパス */
[19805]37define("ZIP_CSV_UTF8_REALFILE", DATA_REALDIR . "downloads/KEN_ALL_utf-8.CSV");
[18317]38
[15335]39/**
40 * 郵便番号DB登録 のページクラス.
41 *
42 * @package Page
43 * @author LOCKON CO.,LTD.
[16749]44 * @version $Id:LC_Page_Admin_Basis_ZipInstall.php 16741 2007-11-08 00:43:24Z adachi $
[15335]45 */
[19661]46class LC_Page_Admin_Basis_ZipInstall extends LC_Page_Admin {
[15335]47
[18317]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
[15335]56    // }}}
57    // {{{ functions
58
59    /**
60     * Page を初期化する.
61     *
62     * @return void
63     */
64    function init() {
65        parent::init();
[18317]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'];
[15335]74    }
75
76    /**
77     * Page のプロセス.
78     *
79     * @return void
80     */
81    function process() {
[19661]82        $this->action();
83        $this->sendResponse();
84    }
85
86    /**
87     * Page のアクション.
88     *
89     * @return void
90     */
91    function action() {
[15335]92        $objQuery = new SC_Query();
[18317]93
94        SC_Utils_Ex::sfIsSuccess(new SC_Session);
95
96        // パラメータ管理クラス
97        $this->objFormParam = new SC_FormParam();
98        // パラメータ情報の初期化
99        $this->lfInitParam();
100        // POST値の取得
101        $this->objFormParam->setParam($_GET);
102        $this->arrErr = $this->objFormParam->checkError();
103        $this->arrForm = $this->objFormParam->getHashArray();
104
105        if ($this->exec) {
106            if (!empty($this->arrErr)) {
107                SC_Utils_Ex::sfDispException();
108            }
109            switch ($this->tpl_mode) {
110                // 自動登録
111                case 'auto':
112                    $objQuery->begin();
113                    $objQuery->delete('mtb_zip');
114                    $this->insertMtbZip();
115                    $objQuery->commit();
116                    break;
117                // 手動登録
118                case 'manual':
119                    $this->insertMtbZip($this->arrForm['startRowNum']);
120                    break;
121            }
122            exit;
123        }
124
125        switch ($this->tpl_mode) {
126            // 手動削除
127            case 'delete':
128                $objQuery->delete('mtb_zip');
129
130                // 進捗・完了画面を表示しない
131                $this->tpl_mode = null;
132
133                break;
134        }
135
136        $this->tpl_line = $this->countZipCsv();
137        $this->tpl_count_mtb_zip = $this->countMtbZip();
138    }
139
140    /**
141     * デストラクタ.
142     *
143     * @return void
144     */
145    function destroy() {
146        parent::destroy();
147    }
148
149    /**
150     * パラメータ情報の初期化
151     *
152     * @return void
153     */
154    function lfInitParam() {
155        if ($this->tpl_mode == 'manual') {
156            $this->objFormParam->addParam("開始行", "startRowNum", INT_LEN, "n", array("EXIST_CHECK", "MAX_LENGTH_CHECK", "NUM_CHECK"));
157        }
158    }
159
160    /**
161     * DB登録
162     *
163     * @return void
164     */
165    function insertMtbZip($start = 1) {
166        $objQuery = new SC_Query();
[15335]167        $objSess = new SC_Session();
168
169        // 認証可否の判定
170        SC_Utils_Ex::sfIsSuccess($objSess);
171
[18317]172        $img_path = USER_URL . USER_PACKAGE_DIR . DEFAULT_TEMPLATE_NAME . "/img/";
[15335]173
[18317]174        ?>
175        <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja">
176        <head>
177            <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHAR_CODE ?>" />
178        </head>
179        <body>
180        <p>進捗状況</p>
181        <div style="background-color: #494E5F;">
182        <?php
[15335]183        // 一部のIEは256バイト以上受け取ってから表示を開始する。
[17934]184        SC_Utils_Ex::sfFlush(true);
[15335]185
[19738]186        echo "<img src='" . $img_path . ADMIN_DIR . "basis/zip_install_progress.gif'><br />";
[18317]187        echo "<img src='" . $img_path . "install/space_w.gif'>";
188        SC_Utils_Ex::sfFlush();
[15335]189
[18317]190        // 画像を一個表示する件数を求める。
191        $line_all = $this->countZipCsv();
192        $disp_line = intval($line_all / IMAGE_MAX);
[15335]193
[18317]194        /** 現在行(CSV形式。空行は除く。) */
195        $cntCurrentLine = 0;
196        /** 挿入した行数 */
197        $cntInsert = 0;
198        $img_cnt = 0;
199        $safe_mode = (boolean)ini_get('safe_mode');
200        $max_execution_time
201            = is_numeric(ini_get('max_execution_time'))
202            ? intval(ini_get('max_execution_time'))
203            : intval(get_cfg_var('max_execution_time'))
204        ;
[15335]205
[18317]206        $fp = $this->openZipCsv();
207        while (!feof($fp)) {
208            $arrCSV = fgetcsv($fp, ZIP_CSV_LINE_MAX);
209            if (empty($arrCSV)) continue;
210            $cntCurrentLine++;
211            if ($cntCurrentLine >= $start) {
212                $sqlval = array();
[17934]213                // $sqlval['code'] = $arrCSV[0];
214                // $sqlval['old_zipcode'] = $arrCSV[1];
215                $sqlval['zipcode'] = $arrCSV[2];
216                // $sqlval['state_kana'] = $arrCSV[3];
217                // $sqlval['city_kana'] = $arrCSV[4];
218                // $sqlval['town_kana'] = $arrCSV[5];
[18317]219                $sqlval['state'] = $arrCSV[6];
220                $sqlval['city'] = $arrCSV[7];
221                $sqlval['town'] = $arrCSV[8];
[17934]222                // $sqlval['flg1'] = $arrCSV[9];
223                // $sqlval['flg2'] = $arrCSV[10];
224                // $sqlval['flg3'] = $arrCSV[11];
225                // $sqlval['flg4'] = $arrCSV[12];
226                // $sqlval['flg5'] = $arrCSV[13];
227                // $sqlval['flg6'] = $arrCSV[14];
228                $objQuery->insert("mtb_zip", $sqlval);
[18317]229                $cntInsert++;
[15335]230            }
231
[18317]232            // $disp_line件ごとに進捗表示する
233            if($cntCurrentLine % $disp_line == 0 && $img_cnt < IMAGE_MAX) {
234                print("<img src='". $img_path ."install/graph_1_w.gif'>");
235                SC_Utils_Ex::sfFlush();
236                $img_cnt++;
237            }
238            // 暴走スレッドが残留する確率を軽減したタイムアウト防止のロジック
239            // TODO 動作が安定していれば、SC_Utils 辺りに移動したい。
240            if (!$safe_mode) {
241                // タイムアウトをリセット
242                set_time_limit($max_execution_time);
243            }
[15335]244        }
[18317]245        fclose($fp);
246
247        echo "<img src='". $img_path ."install/space_w.gif'>";
248        echo "</div>\n";
249       
250        ?>
251        <script type="text/javascript" language="javascript">
252            <!--
253                // 完了画面
254                function complete() {
255                    document.open("text/html","replace");
256                    document.clear();
257                    document.write("<p>完了しました。<br />");
258                    document.write("<?php echo $cntInsert ?> 件を追加しました。</p>");
259                    document.write("<p><a href='?' target='_top'>戻る</a></p>");
260                    document.close();
261                }
262                // コンテンツを削除するため、タイムアウトで呼び出し。
263                setTimeout("complete()", 0);
264            // -->
265        </script>
266        </body>
267        </html>
268        <?php
[15335]269    }
270
[18317]271    function openZipCsv() {
272        // http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh
273        $this->convertZipCsv();
[19805]274        $fp = fopen(ZIP_CSV_UTF8_REALFILE, "r");
[18317]275        if (!$fp) {
[19805]276            SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE . ' の読み込みに失敗しました。');
[18317]277        }
278        return $fp;
[15335]279    }
[18317]280
281    function convertZipCsv() {
[19805]282        if (file_exists(ZIP_CSV_UTF8_REALFILE)) return;
[18317]283
[19805]284        $fpr = fopen(ZIP_CSV_REALFILE, "r");
[18317]285        if (!$fpr) {
[19805]286            SC_Utils_Ex::sfDispException(ZIP_CSV_REALFILE . ' の読み込みに失敗しました。');
[18317]287        }
288
[19805]289        $fpw = fopen(ZIP_CSV_UTF8_REALFILE, "w");
[18317]290        if (!$fpw) {
[19805]291            SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE . ' を開けません。');
[18317]292        }
293
294        while (!feof($fpr)) {
295            fwrite($fpw, mb_convert_encoding(fgets($fpr, ZIP_CSV_LINE_MAX), CHAR_CODE, 'sjis-win'));
296        }
297
298        fclose($fpw);
299        fclose($fpr);
300    }
301
302    function countMtbZip() {
303        $objQuery = new SC_Query();
304        return $objQuery->count('mtb_zip');
305    }
306
307    function countZipCsv() {
308        $line = 0;
309        $fp = $this->openZipCsv();
310
311        // CSVの行数を数える
312        while (!feof($fp)) {
313            /*
314            // 正確にカウントする
315            $tmp = fgetcsv($fp, ZIP_CSV_LINE_MAX);
316            */
317            // 推測でカウントする
318            $tmp = fgets($fp, ZIP_CSV_LINE_MAX);
319            if (empty($tmp)) continue;
320            $line++;
321        }
322        fclose($fp);
323
324        return $line;
325    }
[15335]326}
327?>
Note: See TracBrowser for help on using the repository browser.