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

Revision 18772, 18.4 KB checked in by nanasess, 14 years ago (diff)

r18700 の続き

  • SC_DbConn のインスタンスを直接使用している個所を SC_Query に変更(#565)
    • 削除予定の機能については未対応
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id Revision Date
  • 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/LC_Page.php");
26
27/**
28 * カテゴリ管理 のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_Admin_Products_Category extends LC_Page {
35
36    // {{{ properties
37
38    /** フォームパラメータ */
39    var $objFormParam;
40
41    // }}}
42    // {{{ functions
43
44    /**
45     * Page を初期化する.
46     *
47     * @return void
48     */
49    function init() {
50        parent::init();
51        $this->tpl_subtitle = 'カテゴリー登録';
52        $this->tpl_mainpage = 'products/category.tpl';
53        $this->tpl_subnavi = 'products/subnavi.tpl';
54        $this->tpl_mainno = 'products';
55        $this->tpl_subno = 'category';
56        $this->tpl_onload = " fnSetFocus('category_name'); ";
57    }
58
59    /**
60     * Page のプロセス.
61     *
62     * @return void
63     */
64    function process() {
65        $objView = new SC_AdminView();
66        $objSess = new SC_Session();
67        $objDb = new SC_Helper_DB_Ex();
68
69        // 認証可否の判定
70        SC_Utils_Ex::sfIsSuccess($objSess);
71
72        // パラメータ管理クラス
73        $this->objFormParam = new SC_FormParam();
74        // パラメータ情報の初期化
75        $this->lfInitParam();
76        // POST値の取得
77        $this->objFormParam->setParam($_POST);
78
79        // 通常時は親カテゴリを0に設定する。
80        $this->arrForm['parent_category_id'] =
81            isset($_POST['parent_category_id']) ? $_POST['parent_category_id'] : "";
82
83        if (!isset($_POST['mode'])) $_POST['mode'] = "";
84
85        switch($_POST['mode']) {
86        case 'edit':
87            $this->objFormParam->convParam();
88            $arrRet =  $this->objFormParam->getHashArray();
89            $this->arrErr = $this->lfCheckError($arrRet);
90
91            if(count($this->arrErr) == 0) {
92                if($_POST['category_id'] == "") {
93                    $objQuery = new SC_Query();
94                    $count = $objQuery->count("dtb_category");
95                    if($count < CATEGORY_MAX) {
96                        $this->lfInsertCat($_POST['parent_category_id']);
97                    } else {
98                        print("カテゴリの登録最大数を超えました。");
99                    }
100                } else {
101                    $this->lfUpdateCat($_POST['category_id']);
102                }
103            } else {
104                $this->arrForm = array_merge($this->arrForm, $this->objFormParam->getHashArray());
105                $this->arrForm['category_id'] = $_POST['category_id'];
106            }
107            break;
108        case 'pre_edit':
109            // 編集項目のカテゴリ名をDBより取得する。
110            $oquery = new SC_Query();
111            $where = "category_id = ?";
112            $cat_name = $oquery->get("dtb_category", "category_name", $where, array($_POST['category_id']));
113            // 入力項目にカテゴリ名を入力する。
114            $this->arrForm['category_name'] = $cat_name;
115            // POSTデータを引き継ぐ
116            $this->arrForm['category_id'] = $_POST['category_id'];
117            break;
118        case 'delete':
119            $objQuery = new SC_Query();
120            // 子カテゴリのチェック
121            $where = "parent_category_id = ? AND del_flg = 0";
122            $count = $objQuery->count("dtb_category", $where, array($_POST['category_id']));
123            if($count != 0) {
124                $this->arrErr['category_name'] = "※ 子カテゴリが存在するため削除できません。<br>";
125            }
126            // 登録商品のチェック
127            $table = "dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id";
128            $where = "T1.category_id = ? AND T2.del_flg = 0";
129            $count = $objQuery->count($table, $where, array($_POST['category_id']));
130            if($count != 0) {
131                $this->arrErr['category_name'] = "※ カテゴリ内に商品が存在するため削除できません。<br>";
132            }
133
134            if(!isset($this->arrErr['category_name'])) {
135                // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
136                $objDb->sfDeleteRankRecord("dtb_category", "category_id", $_POST['category_id'], "", true);
137            }
138            break;
139        case 'up':
140            $objQuery = new SC_Query();
141            $objQuery->begin();
142            $up_id = $this->lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
143            if($up_id != "") {
144                // 上のグループのrankから減算する数
145                $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
146                // 自分のグループのrankに加算する数
147                $up_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id);
148                if($my_count > 0 && $up_count > 0) {
149                    // 自分のグループに加算
150                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $up_count);
151                    // 上のグループから減算
152                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count);
153                }
154            }
155            $objQuery->commit();
156            break;
157        case 'down':
158            $objQuery = new SC_Query();
159            $objQuery->begin();
160            $down_id = $this->lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
161            if($down_id != "") {
162                // 下のグループのrankに加算する数
163                $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
164                // 自分のグループのrankから減算する数
165                $down_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id);
166                if($my_count > 0 && $down_count > 0) {
167                    // 自分のグループから減算
168                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count);
169                    // 下のグループに加算
170                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $down_count);
171                }
172            }
173            $objQuery->commit();
174            break;
175        case 'moveByDnD':
176            // DnDしたカテゴリと移動先のセットを分解する
177            $keys = explode("-", $_POST['keySet']);
178            if ($keys[0] && $keys[1]) {
179                $objQuery = new SC_Query();
180                $objQuery->begin();
181       
182                // 移動したデータのrank、level、parent_category_idを取得
183                $rank   = $objQuery->get("dtb_category", "rank", "category_id = ?", array($keys[0]));
184                $level  = $objQuery->get("dtb_category", "level", "category_id = ?", array($keys[0]));
185                $parent = $objQuery->get("dtb_category", "parent_category_id", "category_id = ?", array($keys[0]));
186
187                // 同一level内のrank配列を作成
188                $objQuery->setOption("ORDER BY rank DESC");
189                if ($level == 1) {
190                    // 第1階層の時
191                    $arrRet = $objQuery->select("rank", "dtb_category", "level = ?", array($level));
192                } else {
193                    // 第2階層以下の時
194                    $arrRet = $objQuery->select("rank", "dtb_category", "level = ? AND parent_category_id = ?", array($level, $parent));
195                }
196                for ($i = 0; $i < sizeof($arrRet); $i++) {
197                    $rankAry[$i + 1] = $arrRet[$i]['rank'];
198                }
199
200                // 移動したデータのグループ内データ数
201                $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0]);
202                if ($rankAry[$keys[1]] > $rank) {
203                    // データが今の位置より上がった時
204                    $up_count = $rankAry[$keys[1]] - $rank;
205                    $decAry   = $objQuery->select("category_id", "dtb_category", "level = ? AND rank > ? AND rank <= ?", array($level, $rank, $rankAry[$keys[1]]));
206                    foreach($decAry as $value){
207                        // 上のグループから減算
208                        $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"], $my_count);
209                    }
210                    // 自分のグループに加算
211                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0], $up_count);
212                } else if($rankAry[$keys[1]] < $rank) {
213                    // データが今の位置より下がった時
214                    $down_count = 0;
215                    $incAry     = $objQuery->select("category_id", "dtb_category", "level = ? AND rank < ? AND rank >= ?", array($level, $rank, $rankAry[$keys[1]]));
216                    foreach ($incAry as $value) {
217                        // 下のグループに加算
218                        $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"], $my_count);
219                        // 合計減算値
220                        $down_count += $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"]);
221                    }
222                    // 自分のグループから減算
223                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0], $down_count);
224                }
225                $objQuery->commit();
226            }
227            break;
228        case 'tree':
229            break;
230        case 'csv':
231            require_once(CLASS_EX_PATH . "helper_extends/SC_Helper_CSV_Ex.php");
232
233            $objCSV = new SC_Helper_CSV_Ex();
234
235            // CSVを送信する。正常終了の場合、終了。
236            $objCSV->sfDownloadCategoryCsv() && exit;
237
238            break;
239        default:
240            $this->arrForm['parent_category_id'] = 0;
241            break;
242        }
243
244        $this->arrList = $this->lfGetCat($this->arrForm['parent_category_id']);
245        $this->arrTree = $objDb->sfGetCatTree($this->arrForm['parent_category_id']);
246
247        $objView->assignobj($this);
248        $objView->display(MAIN_FRAME);
249    }
250
251    /**
252     * デストラクタ.
253     *
254     * @return void
255     */
256    function destroy() {
257        parent::destroy();
258    }
259
260
261
262    // カテゴリの新規追加
263    function lfInsertCat($parent_category_id) {
264
265        $objQuery = new SC_Query();
266        $objQuery->begin(); // トランザクションの開始
267
268
269        if($parent_category_id == 0) {
270            // ROOT階層で最大のランクを取得する。
271            $where = "parent_category_id = ?";
272            $rank = $objQuery->max("dtb_category", "rank", $where, array($parent_category_id)) + 1;
273        } else {
274            // 親のランクを自分のランクとする。
275            $where = "category_id = ?";
276            $rank = $objQuery->get("dtb_category", "rank", $where, array($parent_category_id));
277            // 追加レコードのランク以上のレコードを一つあげる。
278            $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
279            $objQuery->exec($sqlup, array($rank));
280        }
281
282        $where = "category_id = ?";
283        // 自分のレベルを取得する(親のレベル + 1)
284        $level = $objQuery->get("dtb_category", "level", $where, array($parent_category_id)) + 1;
285
286        // 入力データを渡す。
287        $sqlval = $this->objFormParam->getHashArray();
288        $sqlval['create_date'] = "Now()";
289        $sqlval['update_date'] = "Now()";
290        $sqlval['creator_id'] = $_SESSION['member_id'];
291        $sqlval['parent_category_id'] = $parent_category_id;
292        $sqlval['rank'] = $rank;
293        $sqlval['level'] = $level;
294
295        // INSERTの実行
296        $objQuery->insert("dtb_category", $sqlval);
297
298        $objQuery->commit();    // トランザクションの終了
299    }
300
301    // カテゴリの編集
302    function lfUpdateCat($category_id) {
303        $objQuery = new SC_Query();
304        // 入力データを渡す。
305        $sqlval = $this->objFormParam->getHashArray();
306        $sqlval['update_date'] = "Now()";
307        $where = "category_id = ?";
308        $objQuery->update("dtb_category", $sqlval, $where, array($category_id));
309    }
310
311    // カテゴリの取得
312    function lfGetCat($parent_category_id) {
313        $objQuery = new SC_Query();
314
315        if($parent_category_id == "") {
316            $parent_category_id = '0';
317        }
318
319        $col = "category_id, category_name, level, rank";
320        $where = "del_flg = 0 AND parent_category_id = ?";
321        $objQuery->setOption("ORDER BY rank DESC");
322        $arrRet = $objQuery->select($col, "dtb_category", $where, array($parent_category_id));
323        return $arrRet;
324    }
325
326    /* パラメータ情報の初期化 */
327    function lfInitParam() {
328        $this->objFormParam->addParam("カテゴリ名", "category_name", STEXT_LEN, "KVa", array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
329    }
330
331    /* 入力内容のチェック */
332    function lfCheckError($array) {
333
334        $objErr = new SC_CheckError($array);
335        $objErr->arrErr = $this->objFormParam->checkError();
336
337        // 階層チェック
338        if(!isset($objErr->arrErr['category_name'])) {
339            $objQuery = new SC_Query();
340            $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($_POST['parent_category_id']));
341
342            if($level >= LEVEL_MAX) {
343                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
344            }
345        }
346
347        if (!isset($_POST['category_id'])) $_POST['category_id'] = "";
348
349        //
350
351        // 重複チェック
352        if(!isset($objErr->arrErr['category_name'])) {
353            $objQuery = new SC_Query();
354            $where = "parent_category_id = ? AND category_name = ?";
355            $arrRet = $objQuery->select("category_id, category_name", "dtb_category", $where, array($_POST['parent_category_id'], $array['category_name']));
356
357            if (empty($arrRet)) {
358                $arrRet = array(array("category_id" => "", "category_name" => ""));
359            }
360
361            // 編集中のレコード以外に同じ名称が存在する場合
362            if ($arrRet[0]['category_id'] != $_POST['category_id']
363                && $arrRet[0]['category_name'] == $_POST['category_name']) {
364                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。<br>";
365            }
366        }
367
368        return $objErr->arrErr;
369    }
370
371
372    // 並びが1つ下のIDを取得する。
373    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
374        // 親IDを取得する。
375        $col = "$pid_name";
376        $where = "$id_name = ?";
377        $pid = $objQuery->get($table, $col, $where, $id);
378        // すべての子を取得する。
379        $col = "$id_name";
380        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
381        $arrRet = $objQuery->select($col, $table, $where, array($pid));
382        $max = count($arrRet);
383        $down_id = "";
384        for($cnt = 0; $cnt < $max; $cnt++) {
385            if($arrRet[$cnt][$id_name] == $id) {
386                $down_id = $arrRet[($cnt + 1)][$id_name];
387                break;
388            }
389        }
390        return $down_id;
391    }
392
393    // 並びが1つ上のIDを取得する。
394    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
395        // 親IDを取得する。
396        $col = "$pid_name";
397        $where = "$id_name = ?";
398        $pid = $objQuery->get($table, $col, $where, $id);
399        // すべての子を取得する。
400        $col = "$id_name";
401        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
402        $arrRet = $objQuery->select($col, $table, $where, array($pid));
403        $max = count($arrRet);
404        $up_id = "";
405        for($cnt = 0; $cnt < $max; $cnt++) {
406            if($arrRet[$cnt][$id_name] == $id) {
407                $up_id = $arrRet[($cnt - 1)][$id_name];
408                break;
409            }
410        }
411        return $up_id;
412    }
413
414    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
415        $objDb = new SC_Helper_DB_Ex();
416        // 子ID一覧を取得
417        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
418        return count($arrRet);
419    }
420
421    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
422        $objDb = new SC_Helper_DB_Ex();
423        // 子ID一覧を取得
424        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
425        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
426        $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
427        $sql.= "AND del_flg = 0";
428        $ret = $objQuery->exec($sql);
429        return $ret;
430    }
431
432    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
433        $objDb = new SC_Helper_DB_Ex();
434        // 子ID一覧を取得
435        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
436        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
437        $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
438        $sql.= "AND del_flg = 0";
439        $ret = $objQuery->exec($sql);
440        return $ret;
441    }
442}
443?>
Note: See TracBrowser for help on using the repository browser.