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

Revision 17503, 14.9 KB checked in by miningbrownie, 16 years ago (diff)

登録処理がPOSTからのデータになっていたのをCSVからのデータで処理するように変更

  • 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                        flush();
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                flush();
190                exit;
191                break;
192            default:
193                break;
194        }
195
196        $objView->assignobj($this);
197        $objView->display(MAIN_FRAME);
198    }
199
200    /**
201     * デストラクタ.
202     *
203     * @return void
204     */
205    function destroy() {
206        parent::destroy();
207    }
208
209
210    /**
211     * ファイル情報の初期化を行う.
212     *
213     * @return void
214     */
215    function lfInitFile() {
216        $this->objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
217    }
218
219    /**
220     * 入力情報の初期化を行う.
221     *
222     * @return void
223     */
224    function lfInitParam() {
225        $this->objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
226        $this->objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
227        $this->objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
228    }
229   
230    /**
231     * カテゴリ登録を行う.
232     *
233     * @param SC_Query $objQuery SC_Queryインスタンス
234     * @param string|integer $line 処理中の行数
235     * @return void
236     */
237    function lfRegistProduct($objQuery, $line = "",$arrCSV) {
238       
239        $objDb = new SC_Helper_DB_Ex();
240        $arrRet['category_id'] = $arrCSV[0];
241        $arrRet['category_name'] = $arrCSV[1];
242        $arrRet['parent_category_id'] = $arrCSV[2];
243       
244        //存在確認
245        $count = $objQuery->count("dtb_category","category_id = ?",array($arrRet['category_id']));
246        if($count == 0){
247            $update = false;
248        }else if($count == 1){
249            $update = true;
250        }else{
251            echo "ERROR";
252            return;
253        }
254        $sqlval['category_id'] = $arrRet['category_id'];
255        // カテゴリ名
256        $sqlval['category_name'] = $arrRet['category_name'];
257
258        // 親カテゴリID、レベル
259        if ($arrRet['parent_category_id'] == 0) {
260            $sqlval['parent_category_id'] = "0";
261            $sqlval['level'] = 1;
262        } else {
263            $sqlval['parent_category_id'] = $arrRet['parent_category_id'];
264            $parent_level = $objQuery->get("dtb_category", "level", "category_id = ?", array($sqlval['parent_category_id']));
265            $sqlval['level'] = $parent_level + 1;
266        }
267       
268        // その他
269        $time = date("Y-m-d H:i:s");
270        if ($line != "") {
271            $microtime = sprintf("%06d", $line);
272            $time .= ".$microtime";
273        }
274        $sqlval['update_date'] = $time;
275        $sqlval['creator_id'] = $_SESSION['member_id'];
276       
277        // 更新
278        if ($update) {
279            echo "UPDATE ";
280            $where = "category_id = ?";
281            $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id']));
282       
283        // 新規登録
284        } else {
285            echo "INSERT ";
286            $sqlval['create_date'] = $time;
287//            var_dump($sqlval);
288            // ランク
289            if ($sqlval['parent_category_id'] == 0) {
290                // ROOT階層で最大のランクを取得する。
291                $where = "parent_category_id = ?";
292                $sqlval['rank'] = $objQuery->max("dtb_category", "rank", $where, array($sqlval['parent_category_id'])) + 1;
293            } else {
294                // 親のランクを自分のランクとする。
295                $where = "category_id = ?";
296                $sqlval['rank'] = $objQuery->get("dtb_category", "rank", $where, array($sqlval['parent_category_id']));
297                // 追加レコードのランク以上のレコードを一つあげる。
298                $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
299                $objQuery->exec($sqlup, array($sqlval['rank']));
300               
301            }
302            $objQuery->insert("dtb_category", $sqlval);
303            $objQuery->getLastQuery();
304        }
305    }
306
307    /**
308     * 入力チェックを行う.
309     *
310     * @return void
311     */
312    function lfCheckError($arrCSV) {
313//        $arrRet =  $this->objFormParam->getHashArray();
314        $arrRet['category_id'] = $arrCSV[0];
315        $arrRet['category_name'] = $arrCSV[1];
316        $arrRet['parent_category_id'] = $arrCSV[2];
317       
318        $objQuery = new SC_Query();
319       
320        $objErr = new SC_CheckError($arrRet);
321        $objErr->arrErr = $this->objFormParam->checkError(false);
322       
323        // 親カテゴリID設定
324        if ($arrRet['parent_category_id'] == 0) {
325            $parent_category_id = "0";
326        } else {
327            $parent_category_id = $arrRet['parent_category_id'];
328        }
329       
330        // 存在する親カテゴリIDかチェック
331        if (count($objErr->arrErr) == 0) {
332            if ($parent_category_id != 0){
333                $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id));
334                if ($count == 0) {
335                    $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。";
336                }
337            }
338        }
339       
340        // 階層チェック
341        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
342            $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($parent_category_id));
343            if ($level >= LEVEL_MAX) {
344                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
345            }
346        }
347
348        // 重複チェック
349        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
350            $where = "parent_category_id = ? AND category_name = ?";
351            $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name']));
352            if (empty($arrCat)) {
353                $arrCat = array(array("category_id" => "", "category_name" => ""));
354            }
355            // 編集中のレコード以外に同じ名称が存在する場合
356            if ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $arrRet['category_name']) {
357                echo $arrCat[0]['category_id'];
358                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
359                       
360                echo $arrRet['category_id'];
361                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
362                       
363                echo  $arrCat[0]['category_name'] ;
364                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
365                        echo  $arrRet['category_name'];
366                        echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
367                               
368               
369                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。</br>";
370               
371            }
372        }
373        return $objErr->arrErr;
374    }
375
376    /**
377     * CSVのカウント数を得る.
378     *
379     * @param string $file_name ファイルパス
380     * @return integer CSV のカウント数
381     */
382    function lfCSVRecordCount($file_name) {
383        $count = 0;
384        $fp = fopen($file_name, "r");
385        while(!feof($fp)) {
386            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
387            $count++;
388        }
389
390        return $count-1;
391    }
392
393    /**
394     * 引数の文字列をエラー出力する.
395     *
396     * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ
397     *
398     * @param string $val 出力する文字列
399     * @return void
400     */
401    function printError($val) {
402        echo "<font color=\"red\">"
403        . htmlspecialchars($val, ENT_QUOTES)
404        . "</font></br>\n";
405    }
406}
407?>
Note: See TracBrowser for help on using the repository browser.