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

Revision 18432, 13.3 KB checked in by kajiwara, 14 years ago (diff)

EC-CUBE Ver2.4.2 分コミット。詳細はこちら( http://www.ec-cube.net/release/detail.php?release_id=207

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