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

Revision 19732, 14.8 KB checked in by Seasoft, 13 years ago (diff)

#855(SC_Query の #select, #getRow, #getCol, #get, #min, #max の引数順を統一する)

  • SC_Query#max を改訂
  • SC_Query#min を改訂
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
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/admin/LC_Page_Admin.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_Admin {
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        $this->action();
62        $this->sendResponse();
63    }
64
65    /**
66     * Page のアクション.
67     *
68     * @return void
69     */
70    function action() {
71        $objSess = new SC_Session();
72        $objDb = new SC_Helper_DB_Ex();
73
74        // 認証可否の判定
75        SC_Utils_Ex::sfIsSuccess($objSess);
76
77        // ファイル管理クラス
78        $this->objUpFile = new SC_UploadFile(IMAGE_TEMP_DIR, IMAGE_SAVE_DIR);
79        // ファイル情報の初期化
80        $this->lfInitFile();
81        // パラメータ管理クラス
82        $this->objFormParam = new SC_FormParam();
83        // パラメータ情報の初期化
84        $this->lfInitParam();
85        $colmax = $this->objFormParam->getCount();
86        $this->objFormParam->setHtmlDispNameArray();
87        $this->arrTitle = $this->objFormParam->getHtmlDispNameArray();
88
89        if (!isset($_POST['mode'])) $_POST['mode'] = "";
90
91        switch ($_POST['mode']) {
92            case 'csv_upload':
93                $err = false;
94                // エラーチェック
95                $arrErr['csv_file'] = $this->objUpFile->makeTempFile('csv_file');
96
97                if($arrErr['csv_file'] == "") {
98                    $arrErr = $this->objUpFile->checkEXISTS();
99                }
100
101                // 実行時間を制限しない
102                set_time_limit(0);
103
104                // 出力をバッファリングしない(==日本語自動変換もしない)
105                ob_end_clean();
106
107                // IEのために256バイト空文字出力
108                echo str_pad('',256);
109
110                if (empty($arrErr['csv_file'])) {
111                    // 一時ファイル名の取得
112                    $filepath = $this->objUpFile->getTempFilePath('csv_file');
113                    // エンコード
114                    $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath,
115                    CHAR_CODE, CSV_TEMP_DIR);
116
117                    $fp = fopen($enc_filepath, "r");
118
119                    // 無効なファイルポインタが渡された場合はエラー表示
120                    if ($fp === false) {
121                        SC_Utils_Ex::sfDispError("");
122                    }
123
124                    // レコード数を得る
125                    $rec_count = $this->lfCSVRecordCount($fp);
126
127                    $line = 0;      // 行数
128                    $regist = 0;    // 登録数
129
130                    $objQuery = new SC_Query();
131                    $objQuery->begin();
132
133                    echo "■ CSV登録進捗状況 <br/><br/>\n";
134                    while (!feof($fp) && !$err) {
135                        $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
136
137                        // 行カウント
138                        $line++;
139
140                        if ($line <= 1) {
141                            continue;
142                        }
143
144                        // 項目数カウント
145                        $max = count($arrCSV);
146
147                        // 項目数が1以下の場合は無視する
148                        if ($max <= 1) {
149                            continue;
150                        }
151
152                        // 項目数チェック
153                        if ($max != $colmax) {
154                            echo "※ 項目数が" . $max . "個検出されました。項目数は" . $colmax . "個になります。</br>\n";
155                            $err = true;
156                        } else {
157                            // シーケンス配列を格納する。
158                            $this->objFormParam->setParam($arrCSV, true);
159                            $arrRet = $this->objFormParam->getHashArray();
160                            $this->objFormParam->setParam($arrRet);
161                            // 入力値の変換
162                            $this->objFormParam->convParam();
163                            // <br>なしでエラー取得する。
164                            $arrCSVErr = $this->lfCheckError($arrCSV);
165                        }
166
167                        // 入力エラーチェック
168                        if (count($arrCSVErr) > 0) {
169                            echo "<font color=\"red\">■" . $line . "行目でエラーが発生しました。</font></br>\n";
170                            foreach($arrCSVErr as $val) {
171                                $this->printError($val);
172                            }
173                            $err = true;
174                        }
175
176                        if (!$err) {
177                            $this->lfRegistProduct($objQuery, $line,$arrCSV);
178                            $regist++;
179                        }
180                        $arrParam = $this->objFormParam->getHashArray();
181
182                        if (!$err) echo $line." / ".$rec_count. "行目 (カテゴリID:".$arrParam['category_id']." / カテゴリ名:".$arrParam['category_name'].")\n<br />";
183                        SC_Utils_Ex::sfFlush();
184                    }
185                    fclose($fp);
186
187                    if (!$err) {
188                        $objQuery->commit();
189                        echo "■" . $regist . "件のレコードを登録しました。";
190                        // カテゴリ件数カウント関数の実行
191                        $objDb->sfCategory_Count($objQuery);
192                        $objDb->sfMaker_Count($objQuery);
193                    } else {
194                        $objQuery->rollback();
195                    }
196                } else {
197                    foreach($arrErr as $val) {
198                        $this->printError($val);
199                    }
200                }
201                echo "<br/><a href=\"javascript:window.close()\">→閉じる</a>";
202                exit;
203            default:
204                break;
205        }
206    }
207
208    /**
209     * デストラクタ.
210     *
211     * @return void
212     */
213    function destroy() {
214        parent::destroy();
215    }
216
217
218    /**
219     * ファイル情報の初期化を行う.
220     *
221     * @return void
222     */
223    function lfInitFile() {
224        $this->objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
225    }
226
227    /**
228     * 入力情報の初期化を行う.
229     *
230     * @return void
231     */
232    function lfInitParam() {
233        $this->objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
234        $this->objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
235        $this->objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
236    }
237   
238    /**
239     * カテゴリ登録を行う.
240     *
241     * @param SC_Query $objQuery SC_Queryインスタンス
242     * @param string|integer $line 処理中の行数
243     * @return void
244     */
245    function lfRegistProduct($objQuery, $line = "",$arrCSV) {
246       
247        $objDb = new SC_Helper_DB_Ex();
248        $sqlval['category_id'] = $arrCSV[0];
249        $sqlval['category_name'] = $arrCSV[1];
250        $sqlval['parent_category_id'] = strlen($arrCSV[2]) ? $arrCSV[2] : 0;
251       
252        //存在確認
253        $count = $objQuery->count("dtb_category","category_id = ?",array($sqlval['category_id']));
254        $update = $count != 0;
255
256        // 親カテゴリID、レベル
257        if ($sqlval['parent_category_id'] == 0) {
258            $sqlval['level'] = 1;
259        } else {
260            $parent_level = $objQuery->get("level", "dtb_category", "category_id = ?", array($sqlval['parent_category_id']));
261            $sqlval['level'] = $parent_level + 1;
262        }
263       
264        // その他
265        $time = date("Y-m-d H:i:s");
266        if ($line != "") {
267            $microtime = sprintf("%06d", $line);
268            $time .= ".$microtime";
269        }
270        $sqlval['update_date'] = $time;
271        $sqlval['creator_id'] = $_SESSION['member_id'];
272       
273        // 更新
274        if ($update) {
275            echo "更新 ";
276            $where = "category_id = ?";
277            $objQuery->update("dtb_category", $sqlval, $where, array($sqlval['category_id']));
278       
279        // 新規登録
280        } else {
281            echo "登録 ";
282            $sqlval['create_date'] = $time;
283            // ランク
284            if ($sqlval['parent_category_id'] == 0) {
285                // ROOT階層で最大のランクを取得する。
286                $where = "parent_category_id = ?";
287                $sqlval['rank'] = $objQuery->max("rank", "dtb_category", $where, array($sqlval['parent_category_id'])) + 1;
288            } else {
289                // 親のランクを自分のランクとする。
290                $where = "category_id = ?";
291                $sqlval['rank'] = $objQuery->get("rank", "dtb_category", $where, array($sqlval['parent_category_id']));
292                // 追加レコードのランク以上のレコードを一つあげる。
293                $sqlup = "UPDATE dtb_category SET rank = rank + 1 WHERE rank >= ?";
294                $objQuery->exec($sqlup, array($sqlval['rank']));
295               
296            }
297            $sqlval['category_id'] = $objQuery->nextVal('dtb_category_category_id');
298            $objQuery->insert("dtb_category", $sqlval);
299        }
300    }
301
302    /**
303     * 入力チェックを行う.
304     *
305     * @return void
306     */
307    function lfCheckError($arrCSV) {
308//        $arrRet =  $this->objFormParam->getHashArray();
309        $arrRet['category_id'] = $arrCSV[0];
310        $arrRet['category_name'] = $arrCSV[1];
311        $arrRet['parent_category_id'] = $arrCSV[2];
312       
313        $objQuery = new SC_Query();
314       
315        $objErr = new SC_CheckError($arrRet);
316        $objErr->arrErr = $this->objFormParam->checkError(false);
317       
318        // 親カテゴリID設定
319        if ($arrRet['parent_category_id'] == 0) {
320            $parent_category_id = "0";
321        } else {
322            $parent_category_id = $arrRet['parent_category_id'];
323        }
324       
325        // 存在する親カテゴリIDかチェック
326        if (count($objErr->arrErr) == 0) {
327            if ($parent_category_id != 0){
328                $count = $objQuery->count("dtb_category", "category_id = ?", array($parent_category_id));
329                if ($count == 0) {
330                    $objErr->arrErr['parent_category_id'] = "※ 指定の親カテゴリID(".$parent_category_id.")は、存在しません。";
331                }
332            }
333        }
334       
335        // 階層チェック
336        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
337            $level = $objQuery->get("level", "dtb_category", "category_id = ?", array($parent_category_id));
338            if ($level >= LEVEL_MAX) {
339                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
340            }
341        }
342
343        // 重複チェック
344        if (!isset($objErr->arrErr['category_name']) && !isset($objErr->arrErr['parent_category_id'])) {
345            $where = "parent_category_id = ? AND category_name = ?";
346            $arrCat = $objQuery->select("category_id, category_name", "dtb_category", $where, array($parent_category_id, $arrRet['category_name']));
347            if (empty($arrCat)) {
348                $arrCat = array(array("category_id" => "", "category_name" => ""));
349            }
350            // 編集中のレコード以外に同じ名称が存在する場合
351            if ($arrCat[0]['category_id'] != $arrRet['category_id'] && $arrCat[0]['category_name'] == $arrRet['category_name']) {
352                echo $arrCat[0]['category_id'];
353                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
354                       
355                echo $arrRet['category_id'];
356                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
357                       
358                echo  $arrCat[0]['category_name'] ;
359                echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
360                        echo  $arrRet['category_name'];
361                        echo "#######--------- line is ".__LINE__." on ".__FILE__."--------########<br/>";
362                               
363               
364                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。</br>";
365               
366            }
367        }
368        return $objErr->arrErr;
369    }
370
371    /**
372     * CSVのカウント数を得る.
373     *
374     * @param resource $fp fopenを使用して作成したファイルポインタ
375     * @return integer CSV のカウント数
376     */
377    function lfCSVRecordCount($fp) {
378        $count = 0;
379        while(!feof($fp)) {
380            $arrCSV = fgetcsv($fp, CSV_LINE_MAX);
381            $count++;
382        }
383        // ファイルポインタを戻す
384        if (rewind($fp)) {
385            return $count-1;
386        } else {
387            SC_Utils_Ex::sfDispError("");
388        }
389    }
390
391    /**
392     * 引数の文字列をエラー出力する.
393     *
394     * 引数 $val の内容は, htmlspecialchars() によってサニタイズされ
395     *
396     * @param string $val 出力する文字列
397     * @return void
398     */
399    function printError($val) {
400        echo "<font color=\"red\">"
401        . htmlspecialchars($val, ENT_QUOTES)
402        . "</font></br>\n";
403    }
404}
405?>
Note: See TracBrowser for help on using the repository browser.