source: branches/comu-ver2/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php @ 18508

Revision 18508, 14.7 KB checked in by nanasess, 14 years ago (diff)

r18501 merged

  • CSVアップロードで無限ループの可能性があるのを修正(#566)
  • 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-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        $this->tpl_subtitle = 'カテゴリ登録CSV';
53    }
54
55    /**
56     * Page のプロセス.
57     *
58     * @return void
59     */
60    function process() {
61        $conn = new SC_DBConn();
62        $objView = new SC_AdminView();
63        $objSess = new SC_Session();
64        $objDb = new SC_Helper_DB_Ex();
65
66        // 認証可否の判定
67        SC_Utils_Ex::sfIsSuccess($objSess);
68
69        // ファイル管理クラス
70        $this->objUpFile = new SC_UploadFile(IMAGE_TEMP_DIR, IMAGE_SAVE_DIR);
71        // ファイル情報の初期化
72        $this->lfInitFile();
73        // パラメータ管理クラス
74        $this->objFormParam = new SC_FormParam();
75        // パラメータ情報の初期化
76        $this->lfInitParam();
77        $colmax = $this->objFormParam->getCount();
78        $this->objFormParam->setHtmlDispNameArray();
79        $this->arrTitle = $this->objFormParam->getHtmlDispNameArray();
80
81        if (!isset($_POST['mode'])) $_POST['mode'] = "";
82
83        switch ($_POST['mode']) {
84            case 'csv_upload':
85                $err = false;
86                // エラーチェック
87                $arrErr['csv_file'] = $this->objUpFile->makeTempFile('csv_file');
88
89                if($arrErr['css_file'] == "") {
90                    $arrErr = $this->objUpFile->checkEXISTS();
91                }
92
93                // 実行時間を制限しない
94                set_time_limit(0);
95
96                // 出力をバッファリングしない(==日本語自動変換もしない)
97                ob_end_clean();
98
99                // IEのために256バイト空文字出力
100                echo str_pad('',256);
101
102                if (empty($arrErr['csv_file'])) {
103                    // 一時ファイル名の取得
104                    $filepath = $this->objUpFile->getTempFilePath('csv_file');
105                    // エンコード
106                    $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath,
107                    CHAR_CODE, CSV_TEMP_DIR);
108
109                    $fp = fopen($enc_filepath, "r");
110
111                    // 無効なファイルポインタが渡された場合はエラー表示
112                    if ($fp === false) {
113                        SC_Utils_Ex::sfDispError("");
114                    }
115
116                    // レコード数を得る
117                    $rec_count = $this->lfCSVRecordCount($fp);
118
119                    $line = 0;      // 行数
120                    $regist = 0;    // 登録数
121
122                    $objQuery = new SC_Query();
123                    $objQuery->begin();
124
125                    echo "■ CSV登録進捗状況 <br/><br/>\n";
126
127                    while (!feof($fp) && !$err) {
128                        $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
129
130                        // 行カウント
131                        $line++;
132
133                        if ($line <= 1) {
134                            continue;
135                        }
136
137                        // 項目数カウント
138                        $max = count($arrCSV);
139
140                        // 項目数が1以下の場合は無視する
141                        if ($max <= 1) {
142                            continue;
143                        }
144
145                        // 項目数チェック
146                        if ($max != $colmax) {
147                            echo "※ 項目数が" . $max . "個検出されました。項目数は" . $colmax . "個になります。</br>\n";
148                            $err = true;
149                        } else {
150                            // シーケンス配列を格納する。
151                            $this->objFormParam->setParam($arrCSV, true);
152                            $arrRet = $this->objFormParam->getHashArray();
153                            $this->objFormParam->setParam($arrRet);
154                            // 入力値の変換
155                            $this->objFormParam->convParam();
156                            // <br>なしでエラー取得する。
157                            $arrCSVErr = $this->lfCheckError($arrCSV);
158                        }
159
160                        // 入力エラーチェック
161                        if (count($arrCSVErr) > 0) {
162                            echo "<font color=\"red\">■" . $line . "行目でエラーが発生しました。</font></br>\n";
163                            foreach($arrCSVErr as $val) {
164                                $this->printError($val);
165                            }
166                            $err = true;
167                        }
168
169                        if (!$err) {
170                            $this->lfRegistProduct($objQuery, $line,$arrCSV);
171                            $regist++;
172                        }
173                        $arrParam = $this->objFormParam->getHashArray();
174
175                        if (!$err) echo $line." / ".$rec_count. "行目 (カテゴリID:".$arrParam['category_id']." / カテゴリ名:".$arrParam['category_name'].")\n<br />";
176                        SC_Utils_Ex::sfFlush();
177                    }
178                    fclose($fp);
179
180                    if (!$err) {
181                        $objQuery->commit();
182                        echo "■" . $regist . "件のレコードを登録しました。";
183                        // カテゴリ件数カウント関数の実行
184                        $objDb->sfCategory_Count($objQuery);
185                        $objDb->sfMaker_Count($objQuery);
186                    } else {
187                        $objQuery->rollback();
188                    }
189                } else {
190                    foreach($arrErr as $val) {
191                        $this->printError($val);
192                    }
193                }
194                echo "<br/><a href=\"javascript:window.close()\">→閉じる</a>";
195                exit;
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 $line 処理中の行数
239     * @return void
240     */
241    function lfRegistProduct($objQuery, $line = "",$arrCSV) {
242       
243        $objDb = new SC_Helper_DB_Ex();
244        $sqlval['category_id'] = $arrCSV[0];
245        $sqlval['category_name'] = $arrCSV[1];
246        $sqlval['parent_category_id'] = strlen($arrCSV[2]) ? $arrCSV[2] : 0;
247       
248        //存在確認
249        $count = $objQuery->count("dtb_category","category_id = ?",array($sqlval['category_id']));
250        $update = $count != 0;
251
252        // 親カテゴリID、レベル
253        if ($sqlval['parent_category_id'] == 0) {
254            $sqlval['level'] = 1;
255        } else {
256            $parent_level = $objQuery->get("dtb_category", "level", "category_id = ?", array($sqlval['parent_category_id']));
257            $sqlval['level'] = $parent_level + 1;
258        }
259       
260        // その他
261        $time = date("Y-m-d H:i:s");
262        if ($line != "") {
263            $microtime = sprintf("%06d", $line);
264            $time .= ".$microtime";
265        }
266        $sqlval['update_date'] = $time;
267        $sqlval['creator_id'] = $_SESSION['member_id'];
268       
269        // 更新
270        if ($update) {
271            echo "更新 ";
272            $where = "category_id = ?";
273            $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id']));
274       
275        // 新規登録
276        } else {
277            echo "登録 ";
278            $sqlval['create_date'] = $time;
279            // ランク
280            if ($sqlval['parent_category_id'] == 0) {
281                // ROOT階層で最大のランクを取得する。
282                $where = "parent_category_id = ?";
283                $sqlval['rank'] = $objQuery->max("dtb_category", "rank", $where, array($sqlval['parent_category_id'])) + 1;
284            } else {
285                // 親のランクを自分のランクとする。
286                $where = "category_id = ?";
287                $sqlval['rank'] = $objQuery->get("dtb_category", "rank", $where, array($sqlval['parent_category_id']));
288                // 追加レコードのランク以上のレコードを一つあげる。
289                $sqlup = "UPDATE dtb_category SET rank = rank + 1 WHERE rank >= ?";
290                $objQuery->exec($sqlup, array($sqlval['rank']));
291               
292            }
293            $objQuery->insert("dtb_category", $sqlval);
294        }
295    }
296
297    /**
298     * 入力チェックを行う.
299     *
300     * @return void
301     */
302    function lfCheckError($arrCSV) {
303//        $arrRet =  $this->objFormParam->getHashArray();
304        $arrRet['category_id'] = $arrCSV[0];
305        $arrRet['category_name'] = $arrCSV[1];
306        $arrRet['parent_category_id'] = $arrCSV[2];
307       
308        $objQuery = new SC_Query();
309       
310        $objErr = new SC_CheckError($arrRet);
311        $objErr->arrErr = $this->objFormParam->checkError(false);
312       
313        // 親カテゴリID設定
314        if ($arrRet['parent_category_id'] == 0) {
315            $parent_category_id = "0";
316        } else {
317            $parent_category_id = $arrRet['parent_category_id'];
318        }
319       
320        // 存在する親カテゴリIDかチェック
321        if (count($objErr->arrErr) == 0) {
322            if ($parent_category_id != 0){
323                $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id));
324                if ($count == 0) {
325                    $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。";
326                }
327            }
328        }
329       
330        // 階層チェック
331        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
332            $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($parent_category_id));
333            if ($level >= LEVEL_MAX) {
334                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
335            }
336        }
337
338        // 重複チェック
339        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
340            $where = "parent_category_id = ? AND category_name = ?";
341            $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name']));
342            if (empty($arrCat)) {
343                $arrCat = array(array("category_id" => "", "category_name" => ""));
344            }
345            // 編集中のレコード以外に同じ名称が存在する場合
346            if ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $arrRet['category_name']) {
347                echo $arrCat[0]['category_id'];
348                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
349                       
350                echo $arrRet['category_id'];
351                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
352                       
353                echo  $arrCat[0]['category_name'] ;
354                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
355                        echo  $arrRet['category_name'];
356                        echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
357                               
358               
359                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。</br>";
360               
361            }
362        }
363        return $objErr->arrErr;
364    }
365
366    /**
367     * CSVのカウント数を得る.
368     *
369     * @param string $fp fopenを使用して作成したファイルポインタ
370     * @return mixed CSV のカウント数; $file_name が無効な場合は false
371     */
372    function lfCSVRecordCount($fp) {
373        $count = 0;
374        while(!feof($fp)) {
375            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
376            $count++;
377        }
378        // ファイルポインタを戻す
379        if (rewind($fp)) {
380            return $count-1;
381        } else {
382            SC_Utils_Ex::sfDispError("");
383        }
384    }
385
386    /**
387     * 引数の文字列をエラー出力する.
388     *
389     * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ
390     *
391     * @param string $val 出力する文字列
392     * @return void
393     */
394    function printError($val) {
395        echo "<font color=\"red\">"
396        . htmlspecialchars($val, ENT_QUOTES)
397        . "</font></br>\n";
398    }
399}
400?>
Note: See TracBrowser for help on using the repository browser.