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

Revision 17047, 13.9 KB checked in by satou, 16 years ago (diff)

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