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

Revision 18645, 14.7 KB checked in by Seasoft, 14 years ago (diff)

merge r18525

  • 取得元: version-2_4
  • 本題部分は r18501 で対応済み。コメントをマージ。

【取得元のログメッセージ】
merged r18477, r18501

  • 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                    while (!feof($fp) && !$err) {
127                        $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
128
129                        // 行カウント
130                        $line++;
131
132                        if ($line <= 1) {
133                            continue;
134                        }
135
136                        // 項目数カウント
137                        $max = count($arrCSV);
138
139                        // 項目数が1以下の場合は無視する
140                        if ($max <= 1) {
141                            continue;
142                        }
143
144                        // 項目数チェック
145                        if ($max != $colmax) {
146                            echo "※ 項目数が" . $max . "個検出されました。項目数は" . $colmax . "個になります。</br>\n";
147                            $err = true;
148                        } else {
149                            // シーケンス配列を格納する。
150                            $this->objFormParam->setParam($arrCSV, true);
151                            $arrRet = $this->objFormParam->getHashArray();
152                            $this->objFormParam->setParam($arrRet);
153                            // 入力値の変換
154                            $this->objFormParam->convParam();
155                            // <br>なしでエラー取得する。
156                            $arrCSVErr = $this->lfCheckError($arrCSV);
157                        }
158
159                        // 入力エラーチェック
160                        if (count($arrCSVErr) > 0) {
161                            echo "<font color=\"red\">■" . $line . "行目でエラーが発生しました。</font></br>\n";
162                            foreach($arrCSVErr as $val) {
163                                $this->printError($val);
164                            }
165                            $err = true;
166                        }
167
168                        if (!$err) {
169                            $this->lfRegistProduct($objQuery, $line,$arrCSV);
170                            $regist++;
171                        }
172                        $arrParam = $this->objFormParam->getHashArray();
173
174                        if (!$err) echo $line." / ".$rec_count. "行目 (カテゴリID:".$arrParam['category_id']." / カテゴリ名:".$arrParam['category_name'].")\n<br />";
175                        SC_Utils_Ex::sfFlush();
176                    }
177                    fclose($fp);
178
179                    if (!$err) {
180                        $objQuery->commit();
181                        echo "■" . $regist . "件のレコードを登録しました。";
182                        // カテゴリ件数カウント関数の実行
183                        $objDb->sfCategory_Count($objQuery);
184                        $objDb->sfMaker_Count($objQuery);
185                    } else {
186                        $objQuery->rollback();
187                    }
188                } else {
189                    foreach($arrErr as $val) {
190                        $this->printError($val);
191                    }
192                }
193                echo "<br/><a href=\"javascript:window.close()\">→閉じる</a>";
194                exit;
195            default:
196                break;
197        }
198
199        $objView->assignobj($this);
200        $objView->display(MAIN_FRAME);
201    }
202
203    /**
204     * デストラクタ.
205     *
206     * @return void
207     */
208    function destroy() {
209        parent::destroy();
210    }
211
212
213    /**
214     * ファイル情報の初期化を行う.
215     *
216     * @return void
217     */
218    function lfInitFile() {
219        $this->objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
220    }
221
222    /**
223     * 入力情報の初期化を行う.
224     *
225     * @return void
226     */
227    function lfInitParam() {
228        $this->objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
229        $this->objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
230        $this->objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
231    }
232   
233    /**
234     * カテゴリ登録を行う.
235     *
236     * @param SC_Query $objQuery SC_Queryインスタンス
237     * @param string|integer $line 処理中の行数
238     * @return void
239     */
240    function lfRegistProduct($objQuery, $line = "",$arrCSV) {
241       
242        $objDb = new SC_Helper_DB_Ex();
243        $sqlval['category_id'] = $arrCSV[0];
244        $sqlval['category_name'] = $arrCSV[1];
245        $sqlval['parent_category_id'] = strlen($arrCSV[2]) ? $arrCSV[2] : 0;
246       
247        //存在確認
248        $count = $objQuery->count("dtb_category","category_id = ?",array($sqlval['category_id']));
249        $update = $count != 0;
250
251        // 親カテゴリID、レベル
252        if ($sqlval['parent_category_id'] == 0) {
253            $sqlval['level'] = 1;
254        } else {
255            $parent_level = $objQuery->get("dtb_category", "level", "category_id = ?", array($sqlval['parent_category_id']));
256            $sqlval['level'] = $parent_level + 1;
257        }
258       
259        // その他
260        $time = date("Y-m-d H:i:s");
261        if ($line != "") {
262            $microtime = sprintf("%06d", $line);
263            $time .= ".$microtime";
264        }
265        $sqlval['update_date'] = $time;
266        $sqlval['creator_id'] = $_SESSION['member_id'];
267       
268        // 更新
269        if ($update) {
270            echo "更新 ";
271            $where = "category_id = ?";
272            $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id']));
273       
274        // 新規登録
275        } else {
276            echo "登録 ";
277            $sqlval['create_date'] = $time;
278            // ランク
279            if ($sqlval['parent_category_id'] == 0) {
280                // ROOT階層で最大のランクを取得する。
281                $where = "parent_category_id = ?";
282                $sqlval['rank'] = $objQuery->max("dtb_category", "rank", $where, array($sqlval['parent_category_id'])) + 1;
283            } else {
284                // 親のランクを自分のランクとする。
285                $where = "category_id = ?";
286                $sqlval['rank'] = $objQuery->get("dtb_category", "rank", $where, array($sqlval['parent_category_id']));
287                // 追加レコードのランク以上のレコードを一つあげる。
288                $sqlup = "UPDATE dtb_category SET rank = rank + 1 WHERE rank >= ?";
289                $objQuery->exec($sqlup, array($sqlval['rank']));
290               
291            }
292            $objQuery->insert("dtb_category", $sqlval);
293        }
294    }
295
296    /**
297     * 入力チェックを行う.
298     *
299     * @return void
300     */
301    function lfCheckError($arrCSV) {
302//        $arrRet =  $this->objFormParam->getHashArray();
303        $arrRet['category_id'] = $arrCSV[0];
304        $arrRet['category_name'] = $arrCSV[1];
305        $arrRet['parent_category_id'] = $arrCSV[2];
306       
307        $objQuery = new SC_Query();
308       
309        $objErr = new SC_CheckError($arrRet);
310        $objErr->arrErr = $this->objFormParam->checkError(false);
311       
312        // 親カテゴリID設定
313        if ($arrRet['parent_category_id'] == 0) {
314            $parent_category_id = "0";
315        } else {
316            $parent_category_id = $arrRet['parent_category_id'];
317        }
318       
319        // 存在する親カテゴリIDかチェック
320        if (count($objErr->arrErr) == 0) {
321            if ($parent_category_id != 0){
322                $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id));
323                if ($count == 0) {
324                    $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。";
325                }
326            }
327        }
328       
329        // 階層チェック
330        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
331            $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($parent_category_id));
332            if ($level >= LEVEL_MAX) {
333                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
334            }
335        }
336
337        // 重複チェック
338        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
339            $where = "parent_category_id = ? AND category_name = ?";
340            $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name']));
341            if (empty($arrCat)) {
342                $arrCat = array(array("category_id" => "", "category_name" => ""));
343            }
344            // 編集中のレコード以外に同じ名称が存在する場合
345            if ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $arrRet['category_name']) {
346                echo $arrCat[0]['category_id'];
347                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
348                       
349                echo $arrRet['category_id'];
350                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
351                       
352                echo  $arrCat[0]['category_name'] ;
353                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
354                        echo  $arrRet['category_name'];
355                        echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
356                               
357               
358                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。</br>";
359               
360            }
361        }
362        return $objErr->arrErr;
363    }
364
365    /**
366     * CSVのカウント数を得る.
367     *
368     * @param resource $fp fopenを使用して作成したファイルポインタ
369     * @return integer CSV のカウント数
370     */
371    function lfCSVRecordCount($fp) {
372        $count = 0;
373        while(!feof($fp)) {
374            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
375            $count++;
376        }
377        // ファイルポインタを戻す
378        if (rewind($fp)) {
379            return $count-1;
380        } else {
381            SC_Utils_Ex::sfDispError("");
382        }
383    }
384
385    /**
386     * 引数の文字列をエラー出力する.
387     *
388     * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ
389     *
390     * @param string $val 出力する文字列
391     * @return void
392     */
393    function printError($val) {
394        echo "<font color=\"red\">"
395        . htmlspecialchars($val, ENT_QUOTES)
396        . "</font></br>\n";
397    }
398}
399?>
Note: See TracBrowser for help on using the repository browser.