source: branches/feature-module-update/data/class/pages/admin/products/LC_Page_Admin_Products_Category.php @ 15518

Revision 15518, 13.9 KB checked in by nanasess, 17 years ago (diff)

クラス化対応

  • Property svn:keywords set to Id Revision Date
RevLine 
[15518]1<?php
2  /*
3   * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
4   *
5   * http://www.lockon.co.jp/
6   */
7
8  // {{{ requires
9require_once(CLASS_PATH . "pages/LC_Page.php");
10
11/**
12 * カテゴリ管理 のページクラス.
13 *
14 * @package Page
15 * @author LOCKON CO.,LTD.
16 * @version $Id$
17 */
18class LC_Page_Admin_Products_Category extends LC_Page {
19
20    // {{{ properties
21
22    /** フォームパラメータ */
23    var $objFormParam;
24
25    // }}}
26    // {{{ functions
27
28    /**
29     * Page を初期化する.
30     *
31     * @return void
32     */
33    function init() {
34        parent::init();
35        $this->tpl_subtitle = 'カテゴリー登録';
36        $this->tpl_mainpage = 'products/category.tpl';
37        $this->tpl_subnavi = 'products/subnavi.tpl';
38        $this->tpl_mainno = 'products';
39        $this->tpl_subno = 'category';
40        $this->tpl_onload = " fnSetFocus('category_name'); ";
41    }
42
43    /**
44     * Page のプロセス.
45     *
46     * @return void
47     */
48    function process() {
49        $conn = new SC_DBConn();
50        $objView = new SC_AdminView();
51        $objSess = new SC_Session();
52        $objDb = new SC_Helper_DB_Ex();
53
54        // 認証可否の判定
55        SC_Utils_Ex::sfIsSuccess($objSess);
56
57        // パラメータ管理クラス
58        $this->objFormParam = new SC_FormParam();
59        // パラメータ情報の初期化
60        $this->lfInitParam();
61        // POST値の取得
62        $this->objFormParam->setParam($_POST);
63
64        // 通常時は親カテゴリを0に設定する。
65        $this->arrForm['parent_category_id'] =
66            isset($_POST['parent_category_id']) ? $_POST['parent_category_id'] : "";
67
68        if (!isset($_POST['mode'])) $_POST['mode'] = "";
69
70        switch($_POST['mode']) {
71        case 'edit':
72            $this->objFormParam->convParam();
73            $arrRet =  $this->objFormParam->getHashArray();
74            $this->arrErr = $this->lfCheckError($arrRet);
75
76            if(count($this->arrErr) == 0) {
77                if($_POST['category_id'] == "") {
78                    $objQuery = new SC_Query();
79                    $count = $objQuery->count("dtb_category");
80                    if($count < CATEGORY_MAX) {
81                        $this->lfInsertCat($_POST['parent_category_id']);
82                    } else {
83                        print("カテゴリの登録最大数を超えました。");
84                    }
85                } else {
86                    $this->lfUpdateCat($_POST['category_id']);
87                }
88            } else {
89                $this->arrForm = array_merge($this->arrForm, $this->objFormParam->getHashArray());
90                $this->arrForm['category_id'] = $_POST['category_id'];
91            }
92            break;
93        case 'pre_edit':
94            // 編集項目のカテゴリ名をDBより取得する。
95            $oquery = new SC_Query();
96            $where = "category_id = ?";
97            $cat_name = $oquery->get("dtb_category", "category_name", $where, array($_POST['category_id']));
98            // 入力項目にカテゴリ名を入力する。
99            $this->arrForm['category_name'] = $cat_name;
100            // POSTデータを引き継ぐ
101            $this->arrForm['category_id'] = $_POST['category_id'];
102            break;
103        case 'delete':
104            $objQuery = new SC_Query();
105            // 子カテゴリのチェック
106            $where = "parent_category_id = ? AND del_flg = 0";
107            $count = $objQuery->count("dtb_category", $where, array($_POST['category_id']));
108            if($count != 0) {
109                $this->arrErr['category_name'] = "※ 子カテゴリが存在するため削除できません。<br>";
110            }
111            // 登録商品のチェック
112            $where = "category_id = ? AND del_flg = 0";
113            $count = $objQuery->count("dtb_products", $where, array($_POST['category_id']));
114            if($count != 0) {
115                $this->arrErr['category_name'] = "※ カテゴリ内に商品が存在するため削除できません。<br>";
116            }
117
118            if(!isset($this->arrErr['category_name'])) {
119                // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
120                $objDb->sfDeleteRankRecord("dtb_category", "category_id", $_POST['category_id'], "", true);
121            }
122            break;
123        case 'up':
124            $objQuery = new SC_Query();
125            $objQuery->begin();
126            $up_id = $this->lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
127            if($up_id != "") {
128                // 上のグループのrankから減算する数
129                $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
130                // 自分のグループのrankに加算する数
131                $up_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id);
132                if($my_count > 0 && $up_count > 0) {
133                    // 自分のグループに加算
134                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $up_count);
135                    // 上のグループから減算
136                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count);
137                }
138            }
139            $objQuery->commit();
140            break;
141        case 'down':
142            $objQuery = new SC_Query();
143            $objQuery->begin();
144            $down_id = $this->lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
145            if($down_id != "") {
146                // 下のグループのrankに加算する数
147                $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id']);
148                // 自分のグループのrankから減算する数
149                $down_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id);
150                if($my_count > 0 && $down_count > 0) {
151                    // 自分のグループから減算
152                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count);
153                    // 下のグループに加算
154                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $_POST['category_id'], $down_count);
155                }
156            }
157            $objQuery->commit();
158            break;
159        case 'tree':
160            break;
161        default:
162            $this->arrForm['parent_category_id'] = 0;
163            break;
164        }
165
166        $this->arrList = $this->lfGetCat($this->arrForm['parent_category_id']);
167        $this->arrTree = $objDb->sfGetCatTree($this->arrForm['parent_category_id']);
168
169        $objView->assignobj($this);
170        $objView->display(MAIN_FRAME);
171    }
172
173    /**
174     * デストラクタ.
175     *
176     * @return void
177     */
178    function destroy() {
179        parent::destroy();
180    }
181
182
183
184    // カテゴリの新規追加
185    function lfInsertCat($parent_category_id) {
186
187        $objQuery = new SC_Query();
188        $objQuery->begin(); // トランザクションの開始
189
190
191        if($parent_category_id == 0) {
192            // ROOT階層で最大のランクを取得する。
193            $where = "parent_category_id = ?";
194            $rank = $objQuery->max("dtb_category", "rank", $where, array($parent_category_id)) + 1;
195        } else {
196            // 親のランクを自分のランクとする。
197            $where = "category_id = ?";
198            $rank = $objQuery->get("dtb_category", "rank", $where, array($parent_category_id));
199            // 追加レコードのランク以上のレコードを一つあげる。
200            $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
201            $objQuery->exec($sqlup, array($rank));
202        }
203
204        $where = "category_id = ?";
205        // 自分のレベルを取得する(親のレベル + 1)
206        $level = $objQuery->get("dtb_category", "level", $where, array($parent_category_id)) + 1;
207
208        // 入力データを渡す。
209        $sqlval = $this->objFormParam->getHashArray();
210        $sqlval['create_date'] = "Now()";
211        $sqlval['update_date'] = "Now()";
212        $sqlval['creator_id'] = $_SESSION['member_id'];
213        $sqlval['parent_category_id'] = $parent_category_id;
214        $sqlval['rank'] = $rank;
215        $sqlval['level'] = $level;
216
217        // INSERTの実行
218        $objQuery->insert("dtb_category", $sqlval);
219
220        $objQuery->commit();    // トランザクションの終了
221    }
222
223    // カテゴリの編集
224    function lfUpdateCat($category_id) {
225        $objQuery = new SC_Query();
226        // 入力データを渡す。
227        $sqlval = $this->objFormParam->getHashArray();
228        $sqlval['update_date'] = "Now()";
229        $where = "category_id = ?";
230        $objQuery->update("dtb_category", $sqlval, $where, array($category_id));
231    }
232
233    // カテゴリの取得
234    function lfGetCat($parent_category_id) {
235        $objQuery = new SC_Query();
236
237        if($parent_category_id == "") {
238            $parent_category_id = '0';
239        }
240
241        $col = "category_id, category_name, level, rank";
242        $where = "del_flg = 0 AND parent_category_id = ?";
243        $objQuery->setoption("ORDER BY rank DESC");
244        $arrRet = $objQuery->select($col, "dtb_category", $where, array($parent_category_id));
245        return $arrRet;
246    }
247
248    /* パラメータ情報の初期化 */
249    function lfInitParam() {
250        $this->objFormParam->addParam("カテゴリ名", "category_name", STEXT_LEN, "KVa", array("EXIST_CHECK","SPTAB_CHECK","MAX_LENGTH_CHECK"));
251    }
252
253    /* 入力内容のチェック */
254    function lfCheckError($array) {
255
256        $objErr = new SC_CheckError($array);
257        $objErr->arrErr = $this->objFormParam->checkError();
258
259        // 階層チェック
260        if(!isset($objErr->arrErr['category_name'])) {
261            $objQuery = new SC_Query();
262            $level = $objQuery->get("dtb_category", "level", "category_id = ?", array($_POST['parent_category_id']));
263
264            if($level >= LEVEL_MAX) {
265                $objErr->arrErr['category_name'] = "※ ".LEVEL_MAX."階層以上の登録はできません。<br>";
266            }
267        }
268
269        if (!isset($_POST['category_id'])) $_POST['category_id'] = "";
270
271        //
272
273        // 重複チェック
274        if(!isset($objErr->arrErr['category_name'])) {
275            $objQuery = new SC_Query();
276            $where = "parent_category_id = ? AND category_name = ?";
277            $arrRet = $objQuery->select("category_id, category_name", "dtb_category", $where, array($_POST['parent_category_id'], $array['category_name']));
278
279            if (empty($arrRet)) {
280                $arrRet = array(array("category_id" => "", "category_name" => ""));
281            }
282
283            // 編集中のレコード以外に同じ名称が存在する場合
284            if ($arrRet[0]['category_id'] != $_POST['category_id']
285                && $arrRet[0]['category_name'] == $_POST['category_name']) {
286                $objErr->arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。<br>";
287            }
288        }
289
290        return $objErr->arrErr;
291    }
292
293
294    // 並びが1つ下のIDを取得する。
295    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
296        // 親IDを取得する。
297        $col = "$pid_name";
298        $where = "$id_name = ?";
299        $pid = $objQuery->get($table, $col, $where, $id);
300        // すべての子を取得する。
301        $col = "$id_name";
302        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
303        $arrRet = $objQuery->select($col, $table, $where, array($pid));
304        $max = count($arrRet);
305        $down_id = "";
306        for($cnt = 0; $cnt < $max; $cnt++) {
307            if($arrRet[$cnt][$id_name] == $id) {
308                $down_id = $arrRet[($cnt + 1)][$id_name];
309                break;
310            }
311        }
312        return $down_id;
313    }
314
315    // 並びが1つ上のIDを取得する。
316    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
317        // 親IDを取得する。
318        $col = "$pid_name";
319        $where = "$id_name = ?";
320        $pid = $objQuery->get($table, $col, $where, $id);
321        // すべての子を取得する。
322        $col = "$id_name";
323        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
324        $arrRet = $objQuery->select($col, $table, $where, array($pid));
325        $max = count($arrRet);
326        $up_id = "";
327        for($cnt = 0; $cnt < $max; $cnt++) {
328            if($arrRet[$cnt][$id_name] == $id) {
329                $up_id = $arrRet[($cnt - 1)][$id_name];
330                break;
331            }
332        }
333        return $up_id;
334    }
335
336    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
337        $objDb = new SC_Helper_DB_Ex();
338        // 子ID一覧を取得
339        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
340        return count($arrRet);
341    }
342
343    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
344        $objDb = new SC_Helper_DB_Ex();
345        // 子ID一覧を取得
346        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
347        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
348        $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
349        $sql.= "AND del_flg = 0";
350        $ret = $objQuery->exec($sql);
351        return $ret;
352    }
353
354    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
355        $objDb = new SC_Helper_DB_Ex();
356        // 子ID一覧を取得
357        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
358        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
359        $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
360        $sql.= "AND del_flg = 0";
361        $ret = $objQuery->exec($sql);
362        return $ret;
363    }
364}
365?>
Note: See TracBrowser for help on using the repository browser.