source: trunk/data/class/pages/admin/products/LC_Page_Admin_Products_UploadCSVCategory.php @ 18758

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

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

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