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

Revision 17048, 13.7 KB checked in by satou, 16 years ago (diff)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php
RevLine 
[17047]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
[17048]82        switch ($_POST['mode']) {
[17047]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
[17048]101                if (empty($arrErr['csv_file'])) {
[17047]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
[17048]120                    while (!feof($fp) && !$err) {
[17047]121                        $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
122
123                        // 行カウント
124                        $line++;
125
[17048]126                        if ($line <= 1) {
[17047]127                            continue;
128                        }
129
130                        // 項目数カウント
131                        $max = count($arrCSV);
132
133                        // 項目数が1以下の場合は無視する
[17048]134                        if ($max <= 1) {
[17047]135                            continue;
136                        }
137
138                        // 項目数チェック
[17048]139                        if ($max != $colmax) {
[17047]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                        // 入力エラーチェック
[17048]154                        if (count($arrCSVErr) > 0) {
[17047]155                            echo "<font color=\"red\">■" . $line . "行目でエラーが発生しました。</font></br>\n";
156                            foreach($arrCSVErr as $val) {
157                                $this->printError($val);
158                            }
159                            $err = true;
160                        }
161
[17048]162                        if (!$err) {
[17047]163                            $this->lfRegistProduct($objQuery, $line);
164                            $regist++;
165                        }
166                        $arrParam = $this->objFormParam->getHashArray();
167
[17048]168                        if (!$err) echo $line." / ".$rec_count. "行目 (カテゴリID:".$arrParam['category_id']." / カテゴリ名:".$arrParam['category_name'].")\n<br />";
[17047]169                        flush();
170                    }
171                    fclose($fp);
172
[17048]173                    if (!$err) {
[17047]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() {
[17048]214        $this->objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
[17047]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 = "") {
236        $objDb = new SC_Helper_DB_Ex();
237        $arrRet = $this->objFormParam->getHashArray();
238       
239        //カテゴリID
[17048]240        if ($arrRet['category_id'] == 0) {
241            $category_id = $objQuery->max("dtb_category", "category_id") + 1;
242            $sqlval['category_id'] = $category_id;
243            $update = false;
244        } else {
[17047]245            $sqlval['category_id'] = $arrRet['category_id'];
246            $update = true;
247        }
248       
249        // カテゴリ名
250        $sqlval['category_name'] = $arrRet['category_name'];
251
252        // 親カテゴリID、レベル
253        if ($arrRet['parent_category_id'] == 0) {
254            $sqlval['parent_category_id'] = "0";
255            $sqlval['level'] = 1;
256        } else {
257            $sqlval['parent_category_id'] = $arrRet['parent_category_id'];
[17048]258            $parent_level = $objQuery->get("dtb_category", "level", "category_id = ?", array($sqlval['parent_category_id']));
259            $sqlval['level'] = $parent_level + 1;
[17047]260        }
261       
262        // その他
263        $time = date("Y-m-d H:i:s");
[17048]264        if ($line != "") {
[17047]265            $microtime = sprintf("%06d", $line);
266            $time .= ".$microtime";
267        }
268        $sqlval['update_date'] = $time;
269        $sqlval['creator_id'] = $_SESSION['member_id'];
[17048]270       
271        // 更新
272        if ($update) {
[17047]273            echo "UPDATE ";
274            $where = "category_id = ?";
275            $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id']));
276       
277        // 新規登録
278        } else {
[17048]279            echo "INSERT ";
[17047]280            $sqlval['create_date'] = $time;
281            // ランク
282            if ($sqlval['parent_category_id'] == 0) {
283                // ROOT階層で最大のランクを取得する。
284                $where = "parent_category_id = ?";
285                $sqlval['rank'] = $objQuery->max("dtb_category", "rank", $where, array($sqlval['parent_category_id'])) + 1;
286            } else {
287                // 親のランクを自分のランクとする。
288                $where = "category_id = ?";
289                $sqlval['rank'] = $objQuery->get("dtb_category", "rank", $where, array($sqlval['parent_category_id']));
290                // 追加レコードのランク以上のレコードを一つあげる。
291                $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
292                $objQuery->exec($sqlup, array($sqlval['rank']));
293            }
294            $objQuery->insert("dtb_category", $sqlval);
295        }
296    }
297
298    /**
299     * 入力チェックを行う.
300     *
301     * @return void
302     */
303    function lfCheckError() {
304        $arrRet =  $this->objFormParam->getHashArray();
305        $objQuery = new SC_Query();
306        $objErr = new SC_CheckError($arrRet);
307        $objErr->arrErr = $this->objFormParam->checkError(false);
308       
309        // 親カテゴリID設定
310        if ($arrRet['parent_category_id'] == 0) {
311            $parent_category_id = "0";
312        } else {
313            $parent_category_id = $arrRet['parent_category_id'];
314        }
315       
316        // 存在する親カテゴリIDかチェック
[17048]317        if (count($objErr->arrErr) == 0) {
318            if ($parent_category_id != 0){
[17047]319                $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id));
[17048]320                if ($count == 0) {
[17047]321                    $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。";
322                }
323            }
324        }
325       
326        // 階層チェック
[17048]327        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
[17047]328            $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($parent_category_id));
[17048]329            if ($level >= LEVEL_MAX) {
[17047]330                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
331            }
332        }
333
334        // 重複チェック
[17048]335        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
[17047]336            $where = "parent_category_id = ? AND category_name = ?";
337            $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name']));
338            if (empty($arrCat)) {
339                $arrCat = array(array("category_id" => "", "category_name" => ""));
340            }
341            // 編集中のレコード以外に同じ名称が存在する場合
342            if ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $_POST['category_name']) {
343                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。<br>";
344            }
345        }
346        return $objErr->arrErr;
347    }
348
349    /**
350     * CSVのカウント数を得る.
351     *
352     * @param string $file_name ファイルパス
353     * @return integer CSV のカウント数
354     */
355    function lfCSVRecordCount($file_name) {
356        $count = 0;
357        $fp = fopen($file_name, "r");
358        while(!feof($fp)) {
359            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
360            $count++;
361        }
362
363        return $count-1;
364    }
365
366    /**
367     * 引数の文字列をエラー出力する.
368     *
369     * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ
370     *
371     * @param string $val 出力する文字列
372     * @return void
373     */
374    function printError($val) {
375        echo "<font color=\"red\">"
376        . htmlspecialchars($val, ENT_QUOTES)
377        . "</font></br>\n";
378    }
379}
380?>
Note: See TracBrowser for help on using the repository browser.