1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2011 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_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.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_REALFILE", DATA_REALDIR . "downloads/KEN_ALL.CSV"); |
---|
35 | |
---|
36 | /** UTF-8 変換済みの郵便番号CSV ファイルのパス */ |
---|
37 | define("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 | */ |
---|
46 | class 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 | // }}} |
---|
55 | // {{{ functions |
---|
56 | |
---|
57 | /** |
---|
58 | * Page を初期化する. |
---|
59 | * |
---|
60 | * @return void |
---|
61 | */ |
---|
62 | function init() { |
---|
63 | parent::init(); |
---|
64 | $this->tpl_mainpage = 'basis/zip_install.tpl'; |
---|
65 | $this->tpl_subnavi = 'basis/subnavi.tpl'; |
---|
66 | $this->tpl_subno = 'zip_install'; |
---|
67 | $this->tpl_subtitle = '郵便番号DB登録'; |
---|
68 | $this->tpl_mainno = 'basis'; |
---|
69 | |
---|
70 | $this->tpl_mode = $this->getMode(); |
---|
71 | $this->exec = (boolean)$_GET['exec']; |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * Page のプロセス. |
---|
76 | * |
---|
77 | * @return void |
---|
78 | */ |
---|
79 | function process() { |
---|
80 | $this->action(); |
---|
81 | $this->sendResponse(); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Page のアクション. |
---|
86 | * |
---|
87 | * @return void |
---|
88 | */ |
---|
89 | function action() { |
---|
90 | // パラメータ管理クラス |
---|
91 | $objFormParam = new SC_FormParam_Ex(); |
---|
92 | // パラメータ情報の初期化 |
---|
93 | $this->lfInitParam($this->tpl_mode, $objFormParam); |
---|
94 | $objFormParam->setParam($_GET); |
---|
95 | $this->arrErr = $objFormParam->checkError(); |
---|
96 | $this->arrForm = $objFormParam->getHashArray(); |
---|
97 | |
---|
98 | if ($this->exec) { |
---|
99 | if (!empty($this->arrErr)) { |
---|
100 | SC_Utils_Ex::sfDispException(); |
---|
101 | } |
---|
102 | switch ($this->tpl_mode) { |
---|
103 | // 自動登録 |
---|
104 | case 'auto': |
---|
105 | $this->lfAutoCommitZip(); |
---|
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 | $this->lfDeleteZip(); |
---|
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 | |
---|
130 | /** |
---|
131 | * デストラクタ. |
---|
132 | * |
---|
133 | * @return void |
---|
134 | */ |
---|
135 | function destroy() { |
---|
136 | parent::destroy(); |
---|
137 | } |
---|
138 | |
---|
139 | function lfAutoCommitZip() { |
---|
140 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
141 | |
---|
142 | $objQuery->begin(); |
---|
143 | $objQuery->delete('mtb_zip'); |
---|
144 | $this->insertMtbZip(); |
---|
145 | $objQuery->commit(); |
---|
146 | } |
---|
147 | |
---|
148 | function lfDeleteZip() { |
---|
149 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
150 | |
---|
151 | $objQuery->delete('mtb_zip'); |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * パラメータ情報の初期化 |
---|
156 | * |
---|
157 | * @return void |
---|
158 | */ |
---|
159 | function lfInitParam($tpl_mode, &$objFormParam) { |
---|
160 | if ($tpl_mode == 'manual') { |
---|
161 | $objFormParam->addParam("開始行", 'startRowNum', INT_LEN, 'n', array("EXIST_CHECK", "MAX_LENGTH_CHECK", "NUM_CHECK")); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * DB登録 |
---|
167 | * |
---|
168 | * @return void |
---|
169 | */ |
---|
170 | function insertMtbZip($start = 1) { |
---|
171 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
172 | |
---|
173 | $img_path = USER_URL . USER_PACKAGE_DIR . "admin/img/basis/"; // 画像パスは admin 固定 |
---|
174 | |
---|
175 | ?> |
---|
176 | <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> |
---|
177 | <head> |
---|
178 | <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHAR_CODE ?>" /> |
---|
179 | </head> |
---|
180 | <body> |
---|
181 | <p>進捗状況</p> |
---|
182 | <div style="background-color: #494E5F;"> |
---|
183 | <?php |
---|
184 | // 一部のIEは256バイト以上受け取ってから表示を開始する。 |
---|
185 | SC_Utils_Ex::sfFlush(true); |
---|
186 | |
---|
187 | echo '<img src="' . $img_path . 'zip_install_progress.gif"><br />'; |
---|
188 | echo '<img src="' . $img_path . 'space_w.gif">'; |
---|
189 | SC_Utils_Ex::sfFlush(); |
---|
190 | |
---|
191 | // 画像を一個表示する件数を求める。 |
---|
192 | $line_all = $this->countZipCsv(); |
---|
193 | $disp_line = intval($line_all / IMAGE_MAX); |
---|
194 | |
---|
195 | /** 現在行(CSV形式。空行は除く。) */ |
---|
196 | $cntCurrentLine = 0; |
---|
197 | /** 挿入した行数 */ |
---|
198 | $cntInsert = 0; |
---|
199 | $img_cnt = 0; |
---|
200 | $safe_mode = (boolean)ini_get('safe_mode'); |
---|
201 | $max_execution_time |
---|
202 | = is_numeric(ini_get('max_execution_time')) |
---|
203 | ? intval(ini_get('max_execution_time')) |
---|
204 | : intval(get_cfg_var('max_execution_time')) |
---|
205 | ; |
---|
206 | |
---|
207 | $fp = $this->openZipCsv(); |
---|
208 | while (!feof($fp)) { |
---|
209 | $arrCSV = fgetcsv($fp, ZIP_CSV_LINE_MAX); |
---|
210 | if (empty($arrCSV)) continue; |
---|
211 | $cntCurrentLine++; |
---|
212 | if ($cntCurrentLine >= $start) { |
---|
213 | $sqlval = array(); |
---|
214 | // $sqlval['code'] = $arrCSV[0]; |
---|
215 | // $sqlval['old_zipcode'] = $arrCSV[1]; |
---|
216 | $sqlval['zipcode'] = $arrCSV[2]; |
---|
217 | // $sqlval['state_kana'] = $arrCSV[3]; |
---|
218 | // $sqlval['city_kana'] = $arrCSV[4]; |
---|
219 | // $sqlval['town_kana'] = $arrCSV[5]; |
---|
220 | $sqlval['state'] = $arrCSV[6]; |
---|
221 | $sqlval['city'] = $arrCSV[7]; |
---|
222 | $sqlval['town'] = $arrCSV[8]; |
---|
223 | // $sqlval['flg1'] = $arrCSV[9]; |
---|
224 | // $sqlval['flg2'] = $arrCSV[10]; |
---|
225 | // $sqlval['flg3'] = $arrCSV[11]; |
---|
226 | // $sqlval['flg4'] = $arrCSV[12]; |
---|
227 | // $sqlval['flg5'] = $arrCSV[13]; |
---|
228 | // $sqlval['flg6'] = $arrCSV[14]; |
---|
229 | $objQuery->insert("mtb_zip", $sqlval); |
---|
230 | $cntInsert++; |
---|
231 | } |
---|
232 | |
---|
233 | // $disp_line件ごとに進捗表示する |
---|
234 | if($cntCurrentLine % $disp_line == 0 && $img_cnt < IMAGE_MAX) { |
---|
235 | echo '<img src="' . $img_path . 'graph_1_w.gif">'; |
---|
236 | SC_Utils_Ex::sfFlush(); |
---|
237 | $img_cnt++; |
---|
238 | } |
---|
239 | // 暴走スレッドが残留する確率を軽減したタイムアウト防止のロジック |
---|
240 | // TODO 動作が安定していれば、SC_Utils 辺りに移動したい。 |
---|
241 | if (!$safe_mode) { |
---|
242 | // タイムアウトをリセット |
---|
243 | set_time_limit($max_execution_time); |
---|
244 | } |
---|
245 | } |
---|
246 | fclose($fp); |
---|
247 | |
---|
248 | echo '<img src="' . $img_path . 'space_w.gif">'; |
---|
249 | echo '</div>' . "\n"; |
---|
250 | |
---|
251 | ?> |
---|
252 | <script type="text/javascript" language="javascript"> |
---|
253 | <!-- |
---|
254 | // 完了画面 |
---|
255 | function complete() { |
---|
256 | document.open("text/html","replace"); |
---|
257 | document.clear(); |
---|
258 | document.write("<p>完了しました。<br />"); |
---|
259 | document.write("<?php echo $cntInsert ?> 件を追加しました。</p>"); |
---|
260 | document.write("<p><a href='?' target='_top'>戻る</a></p>"); |
---|
261 | document.close(); |
---|
262 | } |
---|
263 | // コンテンツを削除するため、タイムアウトで呼び出し。 |
---|
264 | setTimeout("complete()", 0); |
---|
265 | // --> |
---|
266 | </script> |
---|
267 | </body> |
---|
268 | </html> |
---|
269 | <?php |
---|
270 | } |
---|
271 | |
---|
272 | function openZipCsv() { |
---|
273 | // http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh |
---|
274 | $this->convertZipCsv(); |
---|
275 | $fp = fopen(ZIP_CSV_UTF8_REALFILE, 'r'); |
---|
276 | if (!$fp) { |
---|
277 | SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE . ' の読み込みに失敗しました。'); |
---|
278 | } |
---|
279 | return $fp; |
---|
280 | } |
---|
281 | |
---|
282 | function convertZipCsv() { |
---|
283 | if (file_exists(ZIP_CSV_UTF8_REALFILE)) return; |
---|
284 | |
---|
285 | $fpr = fopen(ZIP_CSV_REALFILE, 'r'); |
---|
286 | if (!$fpr) { |
---|
287 | SC_Utils_Ex::sfDispException(ZIP_CSV_REALFILE . ' の読み込みに失敗しました。'); |
---|
288 | } |
---|
289 | |
---|
290 | $fpw = fopen(ZIP_CSV_UTF8_REALFILE, 'w'); |
---|
291 | if (!$fpw) { |
---|
292 | SC_Utils_Ex::sfDispException(ZIP_CSV_UTF8_REALFILE . ' を開けません。'); |
---|
293 | } |
---|
294 | |
---|
295 | while (!feof($fpr)) { |
---|
296 | fwrite($fpw, mb_convert_encoding(fgets($fpr, ZIP_CSV_LINE_MAX), CHAR_CODE, 'sjis-win')); |
---|
297 | } |
---|
298 | |
---|
299 | fclose($fpw); |
---|
300 | fclose($fpr); |
---|
301 | } |
---|
302 | |
---|
303 | function countMtbZip() { |
---|
304 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
305 | return $objQuery->count('mtb_zip'); |
---|
306 | } |
---|
307 | |
---|
308 | function countZipCsv() { |
---|
309 | $line = 0; |
---|
310 | $fp = $this->openZipCsv(); |
---|
311 | |
---|
312 | // CSVの行数を数える |
---|
313 | while (!feof($fp)) { |
---|
314 | /* |
---|
315 | // 正確にカウントする |
---|
316 | $tmp = fgetcsv($fp, ZIP_CSV_LINE_MAX); |
---|
317 | */ |
---|
318 | // 推測でカウントする |
---|
319 | $tmp = fgets($fp, ZIP_CSV_LINE_MAX); |
---|
320 | if (empty($tmp)) continue; |
---|
321 | $line++; |
---|
322 | } |
---|
323 | fclose($fp); |
---|
324 | |
---|
325 | return $line; |
---|
326 | } |
---|
327 | } |
---|
328 | ?> |
---|