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

Revision 20507, 12.9 KB checked in by shutta, 13 years ago (diff)

SC_Queryクラスのclass_extends対応

  • 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_EX_REALDIR . "page_extends/admin/LC_Page_Admin_Ex.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_Ex {
37
38    // }}}
39    // {{{ functions
40
41    var $arrErr;
42
43    var $arrTitle;
44
45    var $arrRowResult;
46
47    var $arrRowErr;
48
49    /**
50     * Page を初期化する.
51     *
52     * @return void
53     */
54    function init() {
55        parent::init();
56        $this->tpl_mainpage = 'products/upload_csv_category.tpl';
57        $this->tpl_subnavi  = 'products/subnavi.tpl';
58        $this->tpl_mainno   = 'products';
59        $this->tpl_subno    = 'upload_csv_category';
60        $this->tpl_subtitle = 'カテゴリ登録CSV';
61    }
62
63    /**
64     * Page のプロセス.
65     *
66     * @return void
67     */
68    function process() {
69        $this->action();
70        $this->sendResponse();
71    }
72
73    /**
74     * Page のアクション.
75     *
76     * @return void
77     */
78    function action() {
79        $objDb        = new SC_Helper_DB_Ex();
80        $objUpFile    = new SC_UploadFile_Ex(IMAGE_TEMP_REALDIR, IMAGE_SAVE_REALDIR);
81        $objFormParam = new SC_FormParam_Ex();
82
83        // ファイルオブジェクト初期化
84        $this->initFile($objUpFile);
85
86        // 入力パラメータ初期化
87        $this->initParam($objFormParam);
88        $objFormParam->setHtmlDispNameArray();
89        $this->arrTitle = $objFormParam->getHtmlDispNameArray();
90
91        switch ($this->getMode()) {
92        case 'csv_upload':
93            $this->doUploadCsv($objFormParam, $objUpFile, $objDb);
94        break;
95        default:
96        }
97    }
98
99    /**
100     * CSVアップロードを実行する
101     *
102     * @param SC_FormParam  $objFormParam
103     * @param SC_UploadFile $objUpFile
104     * @param SC_Helper_DB  $objDb
105     * @return void
106     */
107    function doUploadCsv(&$objFormParam, &$objUpFile, &$objDb) {
108        // ファイルアップロードのチェック
109        $objUpFile->makeTempFile('csv_file');
110        $arrErr = $objUpFile->checkExists();
111        if (count($arrErr) > 0) {
112            $this->arrErr = $arrErr;
113            return;
114        }
115
116        // 一時ファイル名の取得
117        $filepath = $objUpFile->getTempFilePath('csv_file');
118        // CSVファイルの文字コード変換
119        $enc_filepath = SC_Utils_Ex::sfEncodeFile($filepath, CHAR_CODE, CSV_TEMP_REALDIR);
120        // CSVファイルのオープン
121        $fp = fopen($enc_filepath, "r");
122        // 失敗した場合はエラー表示
123        if (!$fp) {
124             SC_Utils_Ex::sfDispError("");
125        }
126
127        // 登録対象の列数
128        $col_max_count = $objFormParam->getCount();
129        // 行数
130        $line_count = 0;
131
132        $objQuery = SC_Query_Ex::getSingletonInstance();
133        $objQuery->begin();
134
135        $errFlg = false;
136       
137        while (!feof($fp)) {
138            $arrRow = fgetcsv($fp, CSV_LINE_MAX);
139            $line_count++;
140
141            // ヘッダ行はスキップ
142            if ($line_count == 1) {
143                continue;
144            }
145            // 空行はスキップ
146            if (empty($arrRow)) {
147                continue;
148            }
149            // 列数が異なる場合はエラー
150            $col_count = count($arrRow);
151            if ($col_max_count != $col_count) {
152                $errFlg = true;
153                $this->addRowErr($line_count, "※ 項目数が" . $col_count . "個検出されました。項目数は" . $col_max_count . "個になります。");
154                break;
155            }
156            // 数値インデックスから, カラム名 => 値の連想配列へ変換
157            $objFormParam->setParam($arrRow, true);
158            $arrRow = $objFormParam->getHashArray();
159            $objFormParam->setParam($arrRow);
160            $objFormParam->convParam();
161            // 入力項目チェック
162            $arrErr = $objFormParam->checkError();
163            if (count($arrErr) > 0) {
164                foreach ($arrErr as $err) {
165                    $this->addRowErr($line_count, $err);
166                }
167                $errFlg = true;
168                break;
169            }
170
171            // 親カテゴリIDがない場合はルートのカテゴリIDをセット
172            if ($arrRow['parent_category_id'] == '') {
173                $arrRow['parent_category_id'] = 0;
174            }
175
176            // 親カテゴリIDの存在チェック
177            $count = $objQuery->count("dtb_category", "category_id = ?", array($arrRow['parent_category_id']));
178            if ($arrRow['parent_category_id'] != 0 && $count == 0) {
179                $errFlg = true;
180                $this->addRowErr($line_count, "指定の親カテゴリID(" . $arrRow['parent_category_id'] . ")は、存在しません。");
181                break;
182            }
183           
184            $count = $objQuery->count("dtb_category", "category_id = ?", array($arrRow['category_id']));
185
186            // 編集
187            if ($count > 0) {
188                // 重複チェック
189                $where = "parent_category_id = ? AND category_id <> ? AND category_name = ?";
190                $count = $objQuery->count("dtb_category",
191                                $where,
192                                array($arrRow['parent_category_id'],
193                                      $arrRow['category_id'],
194                                      $arrRow['category_name']));
195                if ($count > 0) {
196                    $errFlg = true;
197                    $this->addRowErr($line_count, "既に同じ内容の登録が存在します。");
198                    break;
199                }
200
201                // カテゴリ更新
202                $arrCategory = array();
203                $arrCategory['category_name'] = $arrRow['category_name'];
204                $arrCategory['update_date'] = 'NOW()';
205                $where = "category_id = ?";
206                $objQuery->update("dtb_category", $arrCategory, $where, array($arrRow['category_id']));
207               
208                $message = "[更新] カテゴリID: " . $arrRow['category_id'] . " カテゴリ名 : " . $arrRow['category_name'];
209                $this->addRowResult($line_count, $message);
210            // 登録
211            } else {
212                // 登録数上限チェック
213                $where = "del_flg = 0";
214                $count = $objQuery->count("dtb_category", $where);
215                if ($count >= CATEGORY_MAX) {
216                    $errFlg = true;
217                    $this->addRowErr($line_count, "カテゴリの登録最大数を超えました。");
218                    break;
219                }
220                // 階層上限チェック
221                if ($this->isOverLevel($arrRow['parent_category_id'])) {
222                    $errFlg = true;
223                    $this->addRowErr($line_count, LEVEL_MAX . "階層以上の登録はできません。");
224                    break;
225                }
226                // 重複チェック
227                $where = "parent_category_id = ? AND category_name = ?";
228                $count = $objQuery->count("dtb_category",
229                                $where,
230                                array($arrRow['parent_category_id'],
231                                      $arrRow['category_name']));
232                if ($count > 0) {
233                    $errFlg = true;
234                    $this->addRowErr($line_count, "既に同じ内容の登録が存在します。");
235                    break;
236                }
237                // カテゴリ登録
238                $this->registerCategory($arrRow['parent_category_id'],
239                                        $arrRow['category_name'],
240                                        $_SESSION['member_id']);
241
242                $message = "[登録] カテゴリ名 : " . $arrRow['category_name'];
243                $this->addRowResult($line_count, $message);
244            }
245        }
246
247        fclose($fp);
248
249        // 実行結果画面を表示
250        $this->tpl_mainpage = 'products/upload_csv_category_complete.tpl';
251
252        if ($errFlg) {
253            $objQuery->rollback();
254            return;
255        }
256
257        $objQuery->commit();
258
259        // カテゴリ件数を更新
260        $objDb->sfCountCategory($objQuery);
261        $objDb->sfCountMaker($objQuery);
262    }
263
264    /**
265     * 登録/編集結果のメッセージをプロパティへ追加する
266     *
267     * @param integer $line_count 行数
268     * @param stirng $message メッセージ
269     * @return void
270     */
271    function addRowResult($line_count, $message) {
272        $this->arrRowResult[] = $line_count . "行目:" . $message;
273    }
274
275    /**
276     * 登録/編集結果のエラーメッセージをプロパティへ追加する
277     *
278     * @param integer $line_count 行数
279     * @param stirng $message メッセージ
280     * @return void
281     */
282    function addRowErr($line_count, $message) {
283        $this->arrRowErr[] = $line_count . "行目:" . $message;
284    }
285
286    /**
287     * カテゴリの階層が上限を超えているかを判定する
288     *
289     * @param integer 親カテゴリID
290     * @param 超えている場合 true
291     */
292    function isOverLevel($parent_category_id) {
293        $objQuery =& SC_Query_Ex::getSingletonInstance();
294        $level = $objQuery->get("level", "dtb_category", "category_id = ?", array($parent_category_id));
295        return $level >= LEVEL_MAX;
296    }
297
298    /**
299     * デストラクタ.
300     *
301     * @return void
302     */
303    function destroy() {
304        parent::destroy();
305    }
306
307    /**
308     * ファイル情報の初期化を行う.
309     *
310     * @return void
311     */
312    function initFile(&$objUpFile) {
313        $objUpFile->addFile("CSVファイル", 'csv_file', array('csv'), CSV_SIZE, true, 0, 0, false);
314    }
315
316    /**
317     * 入力情報の初期化を行う.
318     *
319     * @param SC_FormParam $objFormParam
320     * @return void
321     */
322    function initParam(&$objFormParam) {
323        $objFormParam->addParam("カテゴリID","category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
324        $objFormParam->addParam("カテゴリ名","category_name",STEXT_LEN,"KVa",array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
325        $objFormParam->addParam("親カテゴリID","parent_category_id",INT_LEN,"n",array("MAX_LENGTH_CHECK","NUM_CHECK"));
326    }
327
328    /**
329     * カテゴリを登録する
330     *
331     * @param integer 親カテゴリID
332     * @param string カテゴリ名
333     * @param integer 作成者のID
334     * @return void
335     */
336    function registerCategory($parent_category_id, $category_name, $creator_id) {
337        $objQuery =& SC_Query_Ex::getSingletonInstance();
338
339        $rank = null;
340        if ($parent_category_id == 0) {
341            // ROOT階層で最大のランクを取得する。
342            $where = "parent_category_id = ?";
343            $rank = $objQuery->max("rank", "dtb_category", $where, array($parent_category_id)) + 1;
344        } else {
345            // 親のランクを自分のランクとする。
346            $where = "category_id = ?";
347            $rank = $objQuery->get("rank", "dtb_category", $where, array($parent_category_id));
348            // 追加レコードのランク以上のレコードを一つあげる。
349            $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
350            $objQuery->exec($sqlup, array($rank));
351        }
352
353        $where = "category_id = ?";
354        // 自分のレベルを取得する(親のレベル + 1)
355        $level = $objQuery->get("level", "dtb_category", $where, array($parent_category_id)) + 1;
356
357        $arrCategory = array();
358        $arrCategory['category_name'] = $category_name;
359        $arrCategory['parent_category_id'] = $parent_category_id;
360        $arrCategory['create_date'] = "Now()";
361        $arrCategory['update_date'] = "Now()";
362        $arrCategory['creator_id']  = $creator_id;
363        $arrCategory['rank']        = $rank;
364        $arrCategory['level']       = $level;
365        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
366        $objQuery->insert("dtb_category", $arrCategory);
367    }
368}
Note: See TracBrowser for help on using the repository browser.