source: branches/version-2_12-multilang/data/class/pages/admin/basis/LC_Page_Admin_Basis_ZipInstall.php @ 22509

Revision 22509, 14.0 KB checked in by kim, 11 years ago (diff)

#2084 ID振り直し 重複テキストの調整
#2100 郵便番号登録完了後の画面でメッセージIDの切り出しが間違っていた箇所を修正

  • 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-2012 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// {{{ requires
25require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
26
27/** CSV ファイルの最大行数 */
28define('ZIP_CSV_LINE_MAX', 8192);
29
30/** 画像の表示数量 */
31define('IMAGE_MAX', 680);
32
33/** 郵便番号CSV ファイルのパス */
34define('ZIP_CSV_REALFILE', DATA_REALDIR . 'downloads/KEN_ALL.CSV');
35
36/** UTF-8 変換済みの郵便番号CSV ファイルのパス */
37define('ZIP_CSV_UTF8_REALFILE', DATA_REALDIR . 'downloads/KEN_ALL_utf-8.CSV');
38
39/**
40 * 郵便番号DB登録 のページクラス.
41 *
42 * @package Page
43 * @author LOCKON CO.,LTD.
44 * @version $Id:LC_Page_Admin_Basis_ZipInstall.php 16741 2007-11-08 00:43:24Z adachi $
45 */
46class LC_Page_Admin_Basis_ZipInstall extends LC_Page_Admin_Ex {
47
48    /** CSVの行数 */
49    var $tpl_line = 0;
50    var $tpl_mode;
51    var $exec;
52    var $tpl_count_mtb_zip;
53
54    /** CSV の更新日時 */
55    var $tpl_csv_datetime;
56
57    /** 日本郵便から取得した ZIP アーカイブファイルの保管パス */
58    var $zip_csv_temp_realfile;
59
60    // }}}
61    // {{{ functions
62
63    /**
64     * Page を初期化する.
65     *
66     * @return void
67     */
68    function init() {
69        parent::init();
70        $this->tpl_mainpage = 'basis/zip_install.tpl';
71        $this->tpl_subno = 'zip_install';
72        $this->tpl_maintitle = t('c_Basic information_01');
73        $this->tpl_subtitle = t('c_Postal code registration_01');
74        $this->tpl_mainno = 'basis';
75
76        $this->tpl_mode = $this->getMode();
77        $this->exec = (boolean)$_GET['exec'];
78        $this->zip_csv_temp_realfile = DATA_REALDIR . 'downloads/tmp/ken_all.zip';
79    }
80
81    /**
82     * Page のプロセス.
83     *
84     * @return void
85     */
86    function process() {
87        $this->action();
88        $this->sendResponse();
89    }
90
91    /**
92     * Page のアクション.
93     *
94     * @return void
95     */
96    function action() {
97
98        // パラメーター管理クラス
99        $objFormParam = new SC_FormParam_Ex();
100        // パラメーター情報の初期化
101        $this->lfInitParam($this->tpl_mode, $objFormParam);
102        $objFormParam->setParam($_GET);
103        $this->arrErr = $objFormParam->checkError();
104        $this->arrForm = $objFormParam->getHashArray();
105        $this->tpl_zip_download_url_empty = !defined('ZIP_DOWNLOAD_URL') || strlen(ZIP_DOWNLOAD_URL) === 0 || ZIP_DOWNLOAD_URL === false;
106        $this->tpl_zip_function_not_exists = !function_exists('zip_open');
107        $this->tpl_skip_update_csv = $this->tpl_zip_download_url_empty || $this->tpl_zip_function_not_exists;
108
109        if ($this->exec) {
110            if (!empty($this->arrErr)) {
111                trigger_error('', E_USER_ERROR);
112            }
113            switch ($this->tpl_mode) {
114                // 自動登録
115                case 'auto':
116                    $this->lfAutoCommitZip();
117                    break;
118                // DB手動登録
119                case 'manual':
120                    $this->insertMtbZip($this->arrForm['startRowNum']);
121                    break;
122            }
123            SC_Response_Ex::actionExit();
124        }
125
126        switch ($this->tpl_mode) {
127            // 削除
128            case 'delete':
129                $this->lfDeleteZip();
130
131                // 進捗・完了画面を表示しない
132                $this->tpl_mode = null;
133                break;
134
135            // 郵便番号CSV更新
136            case 'update_csv';
137                $this->lfDownloadZipFileFromJp();
138                $this->lfExtractZipFile();
139
140                // 進捗・完了画面を表示しない
141                $this->tpl_mode = null;
142                break;
143
144            // 自動登録時の郵便番号CSV更新
145            // XXX iframe内にエラー表示しない様、ここでlfDownloadZipFileFromJp()を呼ぶ。
146            case 'auto';
147                if (!$this->tpl_skip_update_csv) {
148                    $this->lfDownloadZipFileFromJp();
149                    $this->lfExtractZipFile();
150                }
151                break;
152        }
153
154        $this->tpl_line = $this->countZipCsv();
155        $this->tpl_count_mtb_zip = $this->countMtbZip();
156        $this->tpl_csv_datetime = $this->lfGetCsvDatetime();
157        // XXX PHP4 を切捨てたら、ダウンロードの必要性チェックなども行いたい
158        // $arrHeader = get_headers(ZIP_DOWNLOAD_URL, 1);
159
160    }
161
162    /**
163     * デストラクタ.
164     *
165     * @return void
166     */
167    function destroy() {
168        parent::destroy();
169    }
170
171    function lfAutoCommitZip() {
172        $objQuery =& SC_Query_Ex::getSingletonInstance();
173
174        // DB更新
175        $objQuery->begin();
176        $this->lfDeleteZip();
177        $this->insertMtbZip();
178        $objQuery->commit();
179    }
180
181    /**
182     * テーブルデータと UTF-8 変換済みの郵便番号 CSV を削除
183     *
184     * @return void
185     */
186    function lfDeleteZip() {
187        $objQuery =& SC_Query_Ex::getSingletonInstance();
188
189        // DB
190        $objQuery->delete('mtb_zip');
191
192        // UTF-8 変換済みの郵便番号 CSV
193        unlink(ZIP_CSV_UTF8_REALFILE);
194    }
195
196    /**
197     * パラメーター情報の初期化
198     *
199     * @return void
200     */
201    function lfInitParam($tpl_mode, &$objFormParam) {
202        if ($tpl_mode == 'manual') {
203            $objFormParam->addParam(t('c_Start line_01'), 'startRowNum', INT_LEN, 'n', array('EXIST_CHECK', 'MAX_LENGTH_CHECK', 'NUM_CHECK'));
204        }
205    }
206
207    /**
208     * DB登録
209     *
210     * @return void
211     */
212    function insertMtbZip($start = 1) {
213        $objQuery =& SC_Query_Ex::getSingletonInstance();
214
215        $img_path = USER_URL . USER_PACKAGE_DIR . 'admin/img/basis/'; // 画像パスは admin 固定
216
217        ?>
218        <html xmlns='http://www.w3.org/1999/xhtml' lang='ja' xml:lang='ja'>
219        <head>
220            <meta http-equiv='Content-Type' content='text/html; charset=<?php echo CHAR_CODE ?>' />
221        </head>
222        <body>
223        <p>DB 登録進捗状況</p>
224        <div style='background-color: #494E5F;'>
225        <?php
226        // 一部のIEは256バイト以上受け取ってから表示を開始する。
227        SC_Utils_Ex::sfFlush(true);
228
229        echo '<img src="' . $img_path . 'zip_install_progress.gif"><br />';
230        echo '<img src="' . $img_path . 'space_w.gif">';
231        SC_Utils_Ex::sfFlush();
232
233        // 画像を一個表示する件数を求める。
234        $line_all = $this->countZipCsv();
235        $disp_line = intval($line_all / IMAGE_MAX);
236
237        /** 現在行(CSV形式。空行は除く。) */
238        $cntCurrentLine = 0;
239        /** 挿入した行数 */
240        $cntInsert = 0;
241        $img_cnt = 0;
242
243        $fp = $this->openZipCsv();
244        while (!feof($fp)) {
245            $arrCSV = fgetcsv($fp, ZIP_CSV_LINE_MAX);
246            if (empty($arrCSV)) continue;
247            $cntCurrentLine++;
248            if ($cntCurrentLine >= $start) {
249                $sqlval = array();
250                $sqlval['zip_id'] = $cntCurrentLine;
251                $sqlval['zipcode'] = $arrCSV[2];
252                $sqlval['state'] = $arrCSV[6];
253                $sqlval['city'] = $arrCSV[7];
254                $sqlval['town'] = $arrCSV[8];
255                $objQuery->insert('mtb_zip', $sqlval);
256                $cntInsert++;
257            }
258
259            // $disp_line件ごとに進捗表示する
260            if ($cntCurrentLine % $disp_line == 0 && $img_cnt < IMAGE_MAX) {
261                echo '<img src="' . $img_path . 'graph_1_w.gif">';
262                SC_Utils_Ex::sfFlush();
263                $img_cnt++;
264            }
265            SC_Utils_Ex::extendTimeOut();
266        }
267        fclose($fp);
268
269        echo '<img src="' . $img_path . 'space_w.gif">';
270
271        ?>
272        </div>
273        <script type='text/javascript' language='javascript'>
274            <!--
275                // 完了画面
276                function complete() {
277                    document.open('text/html','replace');
278                    document.clear();
279                    document.write('<?php t("c_<p>Completed.<br /> T_ARG1 items were added.</p>_01", array("T_ARG1" => $cntInsert))?>');
280                    document.write('<?php t("c_<p><a href='?' target='_top'>Go back</a></p>_01"); ?>');
281                    document.close();
282                }
283                // コンテンツを削除するため、タイムアウトで呼び出し。
284                setTimeout('complete()', 0);
285            // -->
286        </script>
287        </body>
288        </html>
289        <?php
290    }
291
292    function openZipCsv() {
293        $this->convertZipCsv();
294        $fp = fopen(ZIP_CSV_UTF8_REALFILE, 'r');
295        if (!$fp) {
296            trigger_error(t('c_T_ARG1 reading failed._01', array('T_ARG1', ZIP_CSV_UTF8_REALFILE)), E_USER_ERROR);
297        }
298        return $fp;
299    }
300
301    function convertZipCsv() {
302        if (file_exists(ZIP_CSV_UTF8_REALFILE)) return;
303
304        $fpr = fopen(ZIP_CSV_REALFILE, 'r');
305        if (!$fpr) {
306            trigger_error(t('c_T_ARG1 reading failed._01', array('T_ARG1', ZIP_CSV_REALFILE)), E_USER_ERROR);
307        }
308
309        $fpw = fopen(ZIP_CSV_UTF8_REALFILE, 'w');
310        if (!$fpw) {
311            trigger_error(t('c_T_ARG1 cannot be opened._01', array('T_ARG1' => ZIP_CSV_UTF8_REALFILE)), E_USER_ERROR);
312        }
313
314        while (!feof($fpr)) {
315            fwrite($fpw, mb_convert_encoding(fgets($fpr, ZIP_CSV_LINE_MAX), CHAR_CODE, 'sjis-win'));
316        }
317
318        fclose($fpw);
319        fclose($fpr);
320    }
321
322    function countMtbZip() {
323        $objQuery =& SC_Query_Ex::getSingletonInstance();
324        return $objQuery->count('mtb_zip');
325    }
326
327    function countZipCsv() {
328        $line = 0;
329        $fp = $this->openZipCsv();
330
331        // CSVの行数を数える
332        while (!feof($fp)) {
333            /*
334            // 正確にカウントする
335            $tmp = fgetcsv($fp, ZIP_CSV_LINE_MAX);
336            */
337            // 推測でカウントする
338            $tmp = fgets($fp, ZIP_CSV_LINE_MAX);
339            if (empty($tmp)) continue;
340            $line++;
341        }
342        fclose($fp);
343
344        return $line;
345    }
346
347    /**
348     * 日本郵便から郵便番号 CSV の ZIP アーカイブファイルを取得
349     *
350     * @return void
351     */
352    function lfDownloadZipFileFromJp() {
353        // Proxy経由を可能とする。
354        // TODO Proxyの設定は「data/module/HTTP/Request.php」内の「function HTTP_Request」へ記述する。いずれは、外部設定としたい。
355        $req = new HTTP_Request();
356
357        $req->setURL(ZIP_DOWNLOAD_URL);
358
359        // 郵便番号CSVをdownloadする。
360        $res = $req->sendRequest();
361        if (!$res || strlen($res) > 1) {
362            trigger_error(t('c_T_ARG1 retrieval failed._01', array('T_ARG1', ZIP_DOWNLOAD_URL)), E_USER_ERROR);
363        }
364
365        // 郵便番号CSV(zip file)を保存する。
366        $fp = fopen($this->zip_csv_temp_realfile, 'w');
367        if (!$fp) {
368            trigger_error(t('c_T_ARG1 cannot be opened._01', array('T_ARG1' => $this->zip_csv_temp_realfile)), E_USER_ERROR);
369        }
370        $res = fwrite($fp, $req->getResponseBody());
371        if (!$res) {
372            trigger_error(t('c_T_ARG1 writing failed._01', array('T_ARG1' => $this->zip_csv_temp_realfile)), E_USER_ERROR);
373        }
374    }
375
376    /**
377     * ZIP アーカイブファイルを展開して、郵便番号 CSV を上書き
378     *
379     * @return void
380     */
381    function lfExtractZipFile() {
382        $zip = zip_open($this->zip_csv_temp_realfile);
383        if (!is_resource($zip)) {
384            trigger_error(t(t('c_T_ARG1 cannot be opened._02', array('T_ARG1' => $this->zip_csv_temp_realfile))), E_USER_ERROR);
385        }
386
387        do {
388            $entry = zip_read($zip);
389        } while ($entry && zip_entry_name($entry) != 'KEN_ALL.CSV');
390
391        if (!$entry) {
392            trigger_error(t(t('c_The target file was not found in T_ARG1._01', array('T_ARG1' => $this->zip_csv_temp_realfile))), E_USER_ERROR);
393        }
394
395        // 展開時の破損を考慮し、別名で一旦展開する。
396        $tmp_csv_realfile = ZIP_CSV_REALFILE . '.tmp';
397
398        $res = zip_entry_open($zip, $entry, 'rb');
399        if (!$res) {
400            trigger_error(t('c_T_ARG1 decompression failed._01', array('T_ARG1' => $this->zip_csv_temp_realfile)), E_USER_ERROR);
401        }
402
403        $fp = fopen($tmp_csv_realfile, 'w');
404        if (!$fp) {
405           
406            trigger_error(t('c_T_ARG1 cannot be opened._01', array('T_ARG1' => $tmp_csv_realfile)), E_USER_ERROR);
407        }
408
409        $res = fwrite($fp, zip_entry_read($entry, zip_entry_filesize($entry)));
410        if ($res === FALSE) {
411            trigger_error(t('c_T_ARG1 writing failed._01', array('T_ARG1' => $tmp_csv_realfile)), E_USER_ERROR);
412        }
413
414        fclose($fp);
415        zip_close($zip);
416
417        // CSV 削除
418        $res = unlink(ZIP_CSV_REALFILE);
419        if (!$res) {
420            trigger_error(t('c_T_ARG1 cannot be deleted._01', array('T_ARG1' => ZIP_CSV_REALFILE)), E_USER_ERROR);
421        }
422
423        // CSV ファイル名変更
424        $res = rename($tmp_csv_realfile, ZIP_CSV_REALFILE);
425        if (!$res) {
426            trigger_error(t('c_The file name cannot be changed.: T_ARG1 -> TFIELF2_01', array('T_ARG1' => $tmp_csv_realfile, 'T_ARG2' => ZIP_CSV_REALFILE)), E_USER_ERROR);
427        }
428    }
429
430    /**
431     * CSV の更新日時を取得
432     *
433     * @return string CSV の更新日時 (整形済みテキスト)
434     */
435    function lfGetCsvDatetime() {
436        return date('Y/m/d H:i:s', filemtime(ZIP_CSV_REALFILE));
437    }
438}
Note: See TracBrowser for help on using the repository browser.