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

Revision 17880, 14.4 KB checked in by Seasoft, 15 years ago (diff)

・処理の効率化。
・ポップアップで、処理が全て終了するまで画面が描画されない不具合を改善。
・一部の用語を日本語表記に変更。
・SQL文の出力を抑止。

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