source: branches/version-2_4-dev/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php @ 18477

Revision 18477, 13.6 KB checked in by nanasess, 14 years ago (diff)

無限ループの可能性のあるロジックを修正(#566)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2007 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_PATH . "pages/LC_Page.php");
26
27/**
28 * カテゴリ登録CSVのページクラス
29 *
30 * LC_Page_Admin_Products_UploadCSV をカスタマイズする場合はこのクラスを編集する.
31 *
32 * @package Page
33 * @author LOCKON CO.,LTD.
34 * @version $$Id$$
35 */
36class LC_Page_Admin_Products_UploadCSVCategory extends LC_Page {
37
38    // }}}
39    // {{{ functions
40
41    /**
42     * Page を初期化する.
43     *
44     * @return void
45     */
46    function init() {
47        parent::init();
48        $this->tpl_mainpage = 'products/upload_csv_category.tpl';
49        $this->tpl_subnavi = 'products/subnavi.tpl';
50        $this->tpl_mainno = 'products';
51        $this->tpl_subno = 'upload_csv_category';
52    }
53
54    /**
55     * Page のプロセス.
56     *
57     * @return void
58     */
59    function process() {
60        $conn = new SC_DBConn();
61        $objView = new SC_AdminView();
62        $objSess = new SC_Session();
63        $objDb = new SC_Helper_DB_Ex();
64
65        // 認証可否の判定
66        SC_Utils_Ex::sfIsSuccess($objSess);
67
68        // ファイル管理クラス
69        $this->objUpFile = new SC_UploadFile(IMAGE_TEMP_DIR, IMAGE_SAVE_DIR);
70        // ファイル情報の初期化
71        $this->lfInitFile();
72        // パラメータ管理クラス
73        $this->objFormParam = new SC_FormParam();
74        // パラメータ情報の初期化
75        $this->lfInitParam();
76        $colmax = $this->objFormParam->getCount();
77        $this->objFormParam->setHtmlDispNameArray();
78        $this->arrTitle = $this->objFormParam->getHtmlDispNameArray();
79
80        if (!isset($_POST['mode'])) $_POST['mode'] = "";
81
82        switch ($_POST['mode']) {
83            case 'csv_upload':
84                $err = false;
85                // エラーチェック
86                $arrErr['csv_file'] = $this->objUpFile->makeTempFile('csv_file');
87
88                if($arrErr['css_file'] == "") {
89                    $arrErr = $this->objUpFile->checkEXISTS();
90                }
91
92                // 実行時間を制限しない
93                set_time_limit(0);
94
95                // 出力をバッファリングしない(==日本語自動変換もしない)
96                ob_end_clean();
97
98                // IEのために256バイト空文字出力
99                echo str_pad('',256);
100
101                if (empty($arrErr['csv_file'])) {
102                    // 一時ファイル名の取得
103                    $filepath = $this->objUpFile->getTempFilePath('csv_file');
104                    // エンコード
105                    $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath,
106                    CHAR_CODE, CSV_TEMP_DIR);
107
108                    // レコード数を得る
109                    $rec_count = $this->lfCSVRecordCount($enc_filepath);
110                    $fp = fopen($enc_filepath, "r");
111
112                    if ($rec_count === false || $fp === false) {
113                        $err = false;
114                        $arrErr['bad_file_pointer'] = "※ 不正なファイルポインタが検出されました";
115                    }
116
117                    $line = 0;      // 行数
118                    $regist = 0;    // 登録数
119
120                    $objQuery = new SC_Query();
121                    $objQuery->begin();
122
123                    echo "■ CSV登録進捗状況 <br/><br/>\n";
124                    if ($fp !== false) {
125                        while (!feof($fp) && !$err) {
126                            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
127
128                            // 行カウント
129                            $line++;
130
131                            if ($line <= 1) {
132                                continue;
133                            }
134
135                            // 項目数カウント
136                            $max = count($arrCSV);
137
138                            // 項目数が1以下の場合は無視する
139                            if ($max <= 1) {
140                                continue;
141                            }
142
143                            // 項目数チェック
144                            if ($max != $colmax) {
145                                echo "※ 項目数が" . $max . "個検出されました。項目数は" . $colmax . "個になります。</br>\n";
146                                $err = true;
147                            } else {
148                                // シーケンス配列を格納する。
149                                $this->objFormParam->setParam($arrCSV, true);
150                                $arrRet = $this->objFormParam->getHashArray();
151                                $this->objFormParam->setParam($arrRet);
152                                // 入力値の変換
153                                $this->objFormParam->convParam();
154                                // <br>なしでエラー取得する。
155                                $arrCSVErr = $this->lfCheckError();
156                            }
157
158                            // 入力エラーチェック
159                            if (count_chars(string[, int mode])($arrCSVErr) > 0) {
160                                echo "<font color=\"red\">■" . $line . "行目でエラーが発生しました。</font></br>\n";
161                                foreach($arrCSVErr as $val) {
162                                    $this->printError($val);
163                                }
164                                $err = true;
165                            }
166
167                            if (!$err) {
168                                $this->lfRegistProduct($objQuery, $rec_count, $line);
169                                $regist++;
170                            }
171                            $arrParam = $this->objFormParam->getHashArray();
172
173                            if (!$err) echo $line." / ".$rec_count. "行目 (カテゴリID:".$arrParam['category_id']." / カテゴリ名:".$arrParam['category_name'].")\n<br />";
174                            flush();
175                        }
176                        fclose($fp);
177                    }
178
179                    if (!$err) {
180                        $objQuery->commit();
181                        echo "■" . $regist . "件のレコードを登録しました。";
182                        // カテゴリ件数カウント関数の実行
183                        $objDb->sfCategory_Count($objQuery);
184                    } else {
185                        $objQuery->rollback();
186                    }
187                } else {
188                    foreach($arrErr as $val) {
189                        $this->printError($val);
190                    }
191                }
192                echo "<br/><a href=\"javascript:window.close()\">→閉じる</a>";
193                flush();
194                exit;
195                break;
196        default:
197            break;
198        }
199
200        $objView->assignobj($this);
201        $objView->display(MAIN_FRAME);
202    }
203
204    /**
205     * デストラクタ.
206     *
207     * @return void
208     */
209    function destroy() {
210        parent::destroy();
211    }
212
213
214    /**
215     * ファイル情報の初期化を行う.
216     *
217     * @return void
218     */
219    function lfInitFile() {
220        $this->objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
221    }
222
223    /**
224     * 入力情報の初期化を行う.
225     *
226     * @return void
227     */
228    function lfInitParam() {
229        $this->objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
230        $this->objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
231        $this->objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
232    }
233
234    /**
235     * カテゴリ登録を行う.
236     *
237     * @param SC_Query $objQuery SC_Queryインスタンス
238     * @param string|integer $rec_count 処理総数|integer $line 処理中の行数
239     * @return void
240     */
241    function lfRegistProduct($objQuery, $rec_count, $line = "") {
242        $objDb = new SC_Helper_DB_Ex();
243        $arrRet = $this->objFormParam->getHashArray();
244
245        //カテゴリID
246        if ($arrRet['category_id'] == 0) {
247            $category_id = $objQuery->max("dtb_category", "category_id") + 1;
248            $sqlval['category_id'] = $category_id;
249            $update = false;
250        } else {
251            $sqlval['category_id'] = $arrRet['category_id'];
252            $update = true;
253        }
254
255        // カテゴリ名
256        $sqlval['category_name'] = $arrRet['category_name'];
257        //表示ランク(上から順に表示順を自動割り当て)
258        $sqlval['rank'] = ($rec_count + 1) - $line ;
259
260        // 親カテゴリID、レベル
261        if ($arrRet['parent_category_id'] == 0) {
262            $sqlval['parent_category_id'] = "0";
263            $sqlval['level'] = 1;
264        } else {
265            $sqlval['parent_category_id'] = $arrRet['parent_category_id'];
266            $parent_level = $objQuery->get("dtb_category", "level", "category_id = ?", array($sqlval['parent_category_id']));
267            $sqlval['level'] = $parent_level + 1;
268        }
269
270        // その他
271        $time = date("Y-m-d H:i:s");
272        if ($line != "") {
273            $microtime = sprintf("%06d", $line);
274            $time .= ".$microtime";
275        }
276        $sqlval['update_date'] = $time;
277        $sqlval['creator_id'] = $_SESSION['member_id'];
278
279        // 更新
280        if ($update) {
281            echo "UPDATE ";
282            $where = "category_id = ?";
283            $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id']));
284
285        // 新規登録
286        } else {
287            echo "INSERT ";
288            $sqlval['create_date'] = $time;
289            $objQuery->insert("dtb_category", $sqlval);
290        }
291    }
292
293    /**
294     * 入力チェックを行う.
295     *
296     * @return void
297     */
298    function lfCheckError() {
299        $arrRet =  $this->objFormParam->getHashArray();
300        $objQuery = new SC_Query();
301        $objErr = new SC_CheckError($arrRet);
302        $objErr->arrErr = $this->objFormParam->checkError(false);
303
304        // 親カテゴリID設定
305        if ($arrRet['parent_category_id'] == 0) {
306            $parent_category_id = "0";
307        } else {
308            $parent_category_id = $arrRet['parent_category_id'];
309        }
310
311        // 存在する親カテゴリIDかチェック
312        if (count($objErr->arrErr) == 0) {
313            if ($parent_category_id != 0){
314                $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id));
315                if ($count == 0) {
316                    $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。";
317                }
318            }
319        }
320
321        // 階層チェック
322        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
323            $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($parent_category_id));
324            if ($level >= LEVEL_MAX) {
325                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
326            }
327        }
328
329        // 重複チェック
330        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
331            $where = "parent_category_id = ? AND category_name = ?";
332            $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name']));
333            if (empty($arrCat)) {
334                $arrCat = array(array("category_id" => "", "category_name" => ""));
335            // 編集中のレコード以外に同じ名称が存在する場合
336            }elseif ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $arrRet['category_name']) {
337                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。\n";
338            }
339        }
340        return $objErr->arrErr;
341    }
342
343    /**
344     * CSVのカウント数を得る.
345     *
346     * @param string $file_name ファイルパス
347     * @return mixed CSV のカウント数; $file_name が無効な場合は false
348     */
349    function lfCSVRecordCount($file_name) {
350        $count = 0;
351        $fp = fopen($file_name, "r");
352        if ($fp !== false) {
353            while(!feof($fp)) {
354                $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
355                $count++;
356            }
357        } else {
358            return false;
359        }
360        return $count-1;
361    }
362
363    /**
364     * 引数の文字列をエラー出力する.
365     *
366     * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ
367     *
368     * @param string $val 出力する文字列
369     * @return void
370     */
371    function printError($val) {
372        echo "<font color=\"red\">"
373        . htmlspecialchars($val, ENT_QUOTES)
374        . "</font></br>\n";
375    }
376}
377?>
Note: See TracBrowser for help on using the repository browser.