1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2009 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 |
---|
25 | require_once(CLASS_PATH . "pages/LC_Page.php"); |
---|
26 | |
---|
27 | /** CSV ファイルの最大行数 */ |
---|
28 | define("ZIP_CSV_LINE_MAX", 8192); |
---|
29 | |
---|
30 | /** 画像の表示数量 */ |
---|
31 | define("IMAGE_MAX", 680); |
---|
32 | |
---|
33 | /** 郵便番号CSV ファイルのパス */ |
---|
34 | define("ZIP_CSV_FILE_PATH", DATA_PATH . "downloads/KEN_ALL.CSV"); |
---|
35 | |
---|
36 | /** UTF-8 変換済みの郵便番号CSV ファイルのパス */ |
---|
37 | define("ZIP_CSV_UTF8_FILE_PATH", DATA_PATH . "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 | */ |
---|
46 | class LC_Page_Admin_Basis_ZipInstall extends LC_Page { |
---|
47 | |
---|
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 | |
---|
56 | // }}} |
---|
57 | // {{{ functions |
---|
58 | |
---|
59 | /** |
---|
60 | * Page を初期化する. |
---|
61 | * |
---|
62 | * @return void |
---|
63 | */ |
---|
64 | function init() { |
---|
65 | 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']; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Page のプロセス. |
---|
78 | * |
---|
79 | * @return void |
---|
80 | */ |
---|
81 | function process() { |
---|
82 | $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(); |
---|
162 | $objSess = new SC_Session(); |
---|
163 | |
---|
164 | // 認証可否の判定 |
---|
165 | SC_Utils_Ex::sfIsSuccess($objSess); |
---|
166 | |
---|
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 |
---|
178 | // 一部のIEは256バイト以上受け取ってから表示を開始する。 |
---|
179 | SC_Utils_Ex::sfFlush(true); |
---|
180 | |
---|
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(); |
---|
208 | // $sqlval['code'] = $arrCSV[0]; |
---|
209 | // $sqlval['old_zipcode'] = $arrCSV[1]; |
---|
210 | $sqlval['zipcode'] = $arrCSV[2]; |
---|
211 | // $sqlval['state_kana'] = $arrCSV[3]; |
---|
212 | // $sqlval['city_kana'] = $arrCSV[4]; |
---|
213 | // $sqlval['town_kana'] = $arrCSV[5]; |
---|
214 | $sqlval['state'] = $arrCSV[6]; |
---|
215 | $sqlval['city'] = $arrCSV[7]; |
---|
216 | $sqlval['town'] = $arrCSV[8]; |
---|
217 | // $sqlval['flg1'] = $arrCSV[9]; |
---|
218 | // $sqlval['flg2'] = $arrCSV[10]; |
---|
219 | // $sqlval['flg3'] = $arrCSV[11]; |
---|
220 | // $sqlval['flg4'] = $arrCSV[12]; |
---|
221 | // $sqlval['flg5'] = $arrCSV[13]; |
---|
222 | // $sqlval['flg6'] = $arrCSV[14]; |
---|
223 | $objQuery->insert("mtb_zip", $sqlval); |
---|
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(); |
---|
256 | } |
---|
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; |
---|
320 | } |
---|
321 | } |
---|
322 | ?> |
---|