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

Revision 21185, 23.5 KB checked in by shutta, 13 years ago (diff)

refs #800 (SQL標準関数を使用する)
CURRENT_TIMESTAMP を使用するように now() を置換。

  • 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-2011 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';
26require_once CLASS_EX_REALDIR . 'helper_extends/SC_Helper_CSV_Ex.php';
27
28/**
29 * カテゴリ管理 のページクラス.
30 *
31 * @package Page
32 * @author LOCKON CO.,LTD.
33 * @version $Id$
34 */
35class LC_Page_Admin_Products_Category extends LC_Page_Admin_Ex {
36
37    // {{{ properties
38
39    // }}}
40    // {{{ functions
41
42    /**
43     * Page を初期化する.
44     *
45     * @return void
46     */
47    function init() {
48        parent::init();
49        $this->tpl_maintitle = '商品管理';
50        $this->tpl_subtitle = 'カテゴリー登録';
51        $this->tpl_mainpage = 'products/category.tpl';
52        $this->tpl_mainno = 'products';
53        $this->tpl_subno  = 'category';
54        $this->tpl_onload = " fnSetFocus('category_name'); ";
55    }
56
57    /**
58     * Page のプロセス.
59     *
60     * @return void
61     */
62    function process() {
63        $this->action();
64        $this->sendResponse();
65    }
66
67    /**
68     * Page のアクション.
69     *
70     * @return void
71     */
72    function action() {
73        $objDb      = new SC_Helper_DB_Ex();
74        $objFormParam = new SC_FormParam_Ex();
75
76        // 入力パラメーター初期化
77        $this->initParam($objFormParam);
78        $objFormParam->setParam($_POST);
79        $objFormParam->convParam();
80
81        switch($this->getMode()) {
82        // カテゴリー登録/編集実行
83        case 'edit':
84            $category_id = $objFormParam->getValue('category_id');
85            if ($category_id == '') {
86                $this->doRegister($objFormParam);
87            } else {
88                $this->doEdit($objFormParam);
89            }
90            break;
91        // 入力ボックスへ編集対象のカテゴリ名をセット
92        case 'pre_edit':
93            $this->doPreEdit($objFormParam);
94            break;
95        // カテゴリ削除
96        case 'delete':
97            $this->doDelete($objFormParam, $objDb);
98            break;
99        // 表示順を上へ
100        case 'up':
101            $this->doUp($objFormParam);
102            break;
103        // 表示順を下へ
104        case 'down':
105            $this->doDown($objFormParam);
106            break;
107        // XXX 使われていないコード?
108        case 'moveByDnD':
109            // DnDしたカテゴリと移動先のセットを分解する
110            $keys = explode("-", $_POST['keySet']);
111            if ($keys[0] && $keys[1]) {
112                $objQuery = new SC_Query_Ex();
113                $objQuery->begin();
114
115                // 移動したデータのrank、level、parent_category_idを取得
116                $rank   = $objQuery->get('rank', "dtb_category", "category_id = ?", array($keys[0]));
117                $level  = $objQuery->get('level', "dtb_category", "category_id = ?", array($keys[0]));
118                $parent = $objQuery->get("parent_category_id", "dtb_category", "category_id = ?", array($keys[0]));
119
120                // 同一level内のrank配列を作成
121                $objQuery->setOption("ORDER BY rank DESC");
122                if ($level == 1) {
123                    // 第1階層の時
124                    $arrRet = $objQuery->select('rank', "dtb_category", "level = ?", array($level));
125                } else {
126                    // 第2階層以下の時
127                    $arrRet = $objQuery->select('rank', "dtb_category", "level = ? AND parent_category_id = ?", array($level, $parent));
128                }
129                for ($i = 0; $i < sizeof($arrRet); $i++) {
130                    $rankAry[$i + 1] = $arrRet[$i]['rank'];
131                }
132
133                // 移動したデータのグループ内データ数
134                $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0]);
135                if ($rankAry[$keys[1]] > $rank) {
136                    // データが今の位置より上がった時
137                    $up_count = $rankAry[$keys[1]] - $rank;
138                    $decAry   = $objQuery->select("category_id", "dtb_category", "level = ? AND rank > ? AND rank <= ?", array($level, $rank, $rankAry[$keys[1]]));
139                    foreach($decAry as $value){
140                        // 上のグループから減算
141                        $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"], $my_count);
142                    }
143                    // 自分のグループに加算
144                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0], $up_count);
145                } else if($rankAry[$keys[1]] < $rank) {
146                    // データが今の位置より下がった時
147                    $down_count = 0;
148                    $incAry     = $objQuery->select("category_id", "dtb_category", "level = ? AND rank < ? AND rank >= ?", array($level, $rank, $rankAry[$keys[1]]));
149                    foreach ($incAry as $value) {
150                        // 下のグループに加算
151                        $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"], $my_count);
152                        // 合計減算値
153                        $down_count += $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $value["category_id"]);
154                    }
155                    // 自分のグループから減算
156                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $keys[0], $down_count);
157                }
158                $objQuery->commit();
159            }
160            break;
161        // カテゴリツリークリック時
162        case 'tree':
163            break;
164         // CSVダウンロード
165        case 'csv':
166            // CSVを送信する
167            $objCSV = new SC_Helper_CSV_Ex();
168            $objCSV->sfDownloadCsv("5", "", array(), "", true);
169            exit;
170            break;
171        default:
172            break;
173        }
174
175        $parent_category_id = $objFormParam->getValue('parent_category_id');
176        // 空の場合は親カテゴリを0にする
177        if (empty($parent_category_id)) {
178            $parent_category_id = 0;
179        }
180        // 親カテゴリIDの保持
181        $this->arrForm['parent_category_id'] = $parent_category_id;
182        // カテゴリ一覧を取得
183        $this->arrList = $this->findCategoiesByParentCategoryId($parent_category_id);
184        // カテゴリツリーを取得
185        $this->arrTree = $objDb->sfGetCatTree($parent_category_id);
186        // ぱんくずの生成
187        $arrBread = array();
188        $objDb->findTree($this->arrTree, $parent_category_id, $arrBread);
189        $this->tpl_bread_crumbs = SC_Utils_Ex::jsonEncode($arrBread);
190    }
191
192    /**
193     * カテゴリの削除を実行する.
194     *
195     * 下記の場合は削除を実施せず、エラーメッセージを表示する.
196     *
197     * - 削除対象のカテゴリに、子カテゴリが1つ以上ある場合
198     * - 削除対象のカテゴリを、登録商品が使用している場合
199     *
200     * カテゴリの削除は、物理削除で行う.
201     *
202     * @param SC_FormParam $objFormParam
203     * @param SC_Helper_Db $objDb
204     * @return void
205     */
206    function doDelete(&$objFormParam, &$objDb) {
207        $category_id = $objFormParam->getValue('category_id');
208        $objQuery =& SC_Query_Ex::getSingletonInstance();
209
210        // 子カテゴリのチェック
211        $where = "parent_category_id = ? AND del_flg = 0";
212        $count = $objQuery->count("dtb_category", $where, array($category_id));
213        if ($count > 0) {
214             $this->arrErr['category_name'] = "※ 子カテゴリが存在するため削除できません。<br/>";
215             return;
216        }
217        // 登録商品のチェック
218        $table = "dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id";
219        $where = "T1.category_id = ? AND T2.del_flg = 0";
220        $count = $objQuery->count($table, $where, array($category_id));
221        if ($count > 0) {
222            $this->arrErr['category_name'] = "※ カテゴリ内に商品が存在するため削除できません。<br/>";
223            return;
224        }
225
226        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
227        $objDb->sfDeleteRankRecord("dtb_category", "category_id", $category_id, "", true);
228    }
229
230    /**
231     * 編集対象のカテゴリ名を, 入力ボックスへ表示する.
232     *
233     * @param SC_FormParam $objFormParam
234     * @return void
235     */
236    function doPreEdit(&$objFormParam) {
237        $category_id = $objFormParam->getValue('category_id');
238
239        $objQuery =& SC_Query_Ex::getSingletonInstance();
240
241        // 編集対象のカテゴリ名をDBより取得する
242        $where = "category_id = ?";
243        $category_name = $objQuery->get("category_name", "dtb_category", $where, array($category_id));
244
245        // 入力ボックスへカテゴリ名を保持する.
246        $this->arrForm['category_name'] = $category_name;
247        // カテゴリIDを保持する.
248        $this->arrForm['category_id']   = $category_id;
249    }
250
251    /**
252     * カテゴリの編集を実行する.
253     *
254     * 下記の場合は, 編集を実行せず、エラーメッセージを表示する
255     *
256     * - カテゴリ名がすでに使用されている場合
257     *
258     * @param SC_FormParam $objFormParam
259     * @return void
260     */
261    function doEdit(&$objFormParam) {
262        // 入力項目チェック
263        $arrErr = $objFormParam->checkError();
264        if (count($arrErr) > 0) {
265            $this->arrErr = $arrErr;
266            $this->arrForm['category_id']   = $objFormParam->getValue('category_id');
267            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
268            return;
269        }
270
271        // 重複チェック
272        $objQuery =& SC_Query_Ex::getSingletonInstance();
273        $where = "parent_category_id = ? AND category_id <> ? AND category_name = ?";
274        $count = $objQuery->count("dtb_category",
275                                  $where,
276                                  array($objFormParam->getValue('parent_category_id'),
277                                        $objFormParam->getValue('category_id'),
278                                        $objFormParam->getValue('category_name')));
279        if ($count > 0) {
280            $this->arrErr['category_name']  = "※ 既に同じ内容の登録が存在します。<br/>";
281            $this->arrForm['category_id']   = $objFormParam->getValue('category_id');
282            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
283            return;
284        }
285
286        // カテゴリ更新
287        $arrCategory = array();
288        $arrCategory['category_name'] = $objFormParam->getValue('category_name');
289        $arrCategory['update_date']   = 'CURRENT_TIMESTAMP';
290        $this->updateCategory($objFormParam->getValue('category_id'), $arrCategory);
291    }
292
293    /**
294     * カテゴリの登録を実行する.
295     *
296     * 下記の場合は, 登録を実行せず、エラーメッセージを表示する
297     *
298     * - カテゴリー登録数の上限を超える場合
299     * - 階層登録数の上限を超える場合
300     * - カテゴリ名がすでに使用されている場合
301     *
302     * @param SC_FormParam $objFormParam
303     * @return void
304     */
305    function doRegister(&$objFormParam) {
306        // 入力項目チェック
307        $arrErr = $objFormParam->checkError();
308        if (count($arrErr) > 0) {
309            $this->arrErr = $arrErr;
310            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
311            return;
312        }
313
314        // 登録数上限チェック
315        $objQuery =& SC_Query_Ex::getSingletonInstance();
316        $where = "del_flg = 0";
317        $count = $objQuery->count("dtb_category", $where);
318        if ($count >= CATEGORY_MAX) {
319            $this->arrErr['category_name']  = "※ カテゴリの登録最大数を超えました。<br/>";
320            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
321            return;
322        }
323
324        // 階層上限チェック
325        if ($this->isOverLevel($objFormParam->getValue('parent_category_id'))) {
326            $this->arrErr['category_name']  = "※ " . LEVEL_MAX . "階層以上の登録はできません。<br/>";
327            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
328            return;
329        }
330
331        // 重複チェック
332        $where = "parent_category_id = ? AND category_name = ?";
333        $count = $objQuery->count("dtb_category",
334                                  $where,
335                                  array($objFormParam->getValue('parent_category_id'),
336                                        $objFormParam->getValue('category_name')));
337        if ($count > 0) {
338            $this->arrErr['category_name']  = "※ 既に同じ内容の登録が存在します。<br/>";
339            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
340            return;
341        }
342
343        // カテゴリー登録
344        $this->registerCategory($objFormParam->getValue('parent_category_id'),
345                                $objFormParam->getValue('category_name'),
346                                $_SESSION['member_id']);
347    }
348
349    /**
350     * カテゴリの表示順序を上へ移動する.
351     *
352     * @param SC_FormParam $objFormParam
353     * @return void
354     */
355    function doUp(&$objFormParam) {
356        $category_id = $objFormParam->getValue('category_id');
357
358        $objQuery =& SC_Query_Ex::getSingletonInstance();
359        $objQuery->begin();
360        $up_id = $this->lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
361        if ($up_id != "") {
362            // 上のグループのrankから減算する数
363            $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
364                // 自分のグループのrankに加算する数
365                $up_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id);
366                if ($my_count > 0 && $up_count > 0) {
367                    // 自分のグループに加算
368                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id, $up_count);
369                    // 上のグループから減算
370                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count);
371                }
372        }
373        $objQuery->commit();
374    }
375
376    /**
377     * カテゴリの表示順序を下へ移動する.
378     *
379     * @param SC_FormParam $objFormParam
380     * @return void
381     */
382    function doDown(&$objFormParam) {
383        $category_id = $objFormParam->getValue('category_id');
384
385        $objQuery =& SC_Query_Ex::getSingletonInstance();
386        $objQuery->begin();
387        $down_id = $this->lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
388        if ($down_id != "") {
389            // 下のグループのrankに加算する数
390            $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
391            // 自分のグループのrankから減算する数
392            $down_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id);
393            if ($my_count > 0 && $down_count > 0) {
394                // 自分のグループから減算
395                $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count);
396                // 下のグループに加算
397                $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id, $down_count);
398            }
399        }
400        $objQuery->commit();
401    }
402
403    /**
404     * パラメーターの初期化を行う
405     *
406     * @param SC_FormParam $objFormParam
407     * @return void
408     */
409    function initParam(&$objFormParam) {
410        $objFormParam->addParam("親カテゴリID", "parent_category_id", null, null, array());
411        $objFormParam->addParam("カテゴリID", "category_id", null, null, array());
412        $objFormParam->addParam("カテゴリ名", "category_name", STEXT_LEN, 'KVa', array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
413    }
414
415    /**
416     * 親カテゴリIDでカテゴリを検索する.
417     *
418     * - 表示順の降順でソートする
419     * - 有効なカテゴリを返す(del_flag = 0)
420     *
421     * @param SC_Query $objQuery
422     * @param int $parent_category_id 親カテゴリID
423     * @return array カテゴリの配列
424     */
425    function findCategoiesByParentCategoryId($parent_category_id) {
426        if (!$parent_category_id) {
427            $parent_category_id = 0;
428        }
429        $objQuery =& SC_Query_Ex::getSingletonInstance();
430        $col   = "category_id, category_name, level, rank";
431        $where = "del_flg = 0 AND parent_category_id = ?";
432        $objQuery->setOption("ORDER BY rank DESC");
433        return $objQuery->select($col, "dtb_category", $where, array($parent_category_id));
434    }
435
436    /**
437     * カテゴリを更新する
438     *
439     * @param integer $category_id 更新対象のカテゴリID
440     * @param array 更新する カラム名 => 値 の連想配列
441     * @return void
442     */
443    function updateCategory($category_id, $arrCategory) {
444        $objQuery =& SC_Query_Ex::getSingletonInstance();
445        $objQuery->begin();
446        $where = "category_id = ?";
447        $objQuery->update("dtb_category", $arrCategory, $where, array($category_id));
448        $objQuery->commit();
449    }
450
451    /**
452     * カテゴリを登録する
453     *
454     * @param integer 親カテゴリID
455     * @param string カテゴリ名
456     * @param integer 作成者のID
457     * @return void
458     */
459    function registerCategory($parent_category_id, $category_name, $creator_id) {
460        $objQuery =& SC_Query_Ex::getSingletonInstance();
461        $objQuery->begin();
462
463        $rank = null;
464        if ($parent_category_id == 0) {
465            // ROOT階層で最大のランクを取得する。
466            $where = "parent_category_id = ?";
467            $rank = $objQuery->max('rank', "dtb_category", $where, array($parent_category_id)) + 1;
468        } else {
469            // 親のランクを自分のランクとする。
470            $where = "category_id = ?";
471            $rank = $objQuery->get('rank', "dtb_category", $where, array($parent_category_id));
472            // 追加レコードのランク以上のレコードを一つあげる。
473            $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
474            $objQuery->exec($sqlup, array($rank));
475        }
476
477        $where = "category_id = ?";
478        // 自分のレベルを取得する(親のレベル + 1)
479        $level = $objQuery->get('level', "dtb_category", $where, array($parent_category_id)) + 1;
480
481        $arrCategory = array();
482        $arrCategory['category_name'] = $category_name;
483        $arrCategory['parent_category_id'] = $parent_category_id;
484        $arrCategory['create_date'] = 'CURRENT_TIMESTAMP';
485        $arrCategory['update_date'] = 'CURRENT_TIMESTAMP';
486        $arrCategory['creator_id']  = $creator_id;
487        $arrCategory['rank']        = $rank;
488        $arrCategory['level']       = $level;
489        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
490        $objQuery->insert("dtb_category", $arrCategory);
491
492        $objQuery->commit();    // トランザクションの終了
493    }
494
495    /**
496     * カテゴリの階層が上限を超えているかを判定する
497     *
498     * @param integer 親カテゴリID
499     * @param 超えている場合 true
500     */
501    function isOverLevel($parent_category_id) {
502        $objQuery =& SC_Query_Ex::getSingletonInstance();
503        $level = $objQuery->get('level', "dtb_category", "category_id = ?", array($parent_category_id));
504        return $level >= LEVEL_MAX;
505    }
506
507    /**
508     * デストラクタ.
509     *
510     * @return void
511     */
512    function destroy() {
513        parent::destroy();
514    }
515
516    // 並びが1つ下のIDを取得する。
517    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
518        // 親IDを取得する。
519        $col = "$pid_name";
520        $where = "$id_name = ?";
521        $pid = $objQuery->get($col, $table, $where, $id);
522        // すべての子を取得する。
523        $col = "$id_name";
524        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
525        $arrRet = $objQuery->select($col, $table, $where, array($pid));
526        $max = count($arrRet);
527        $down_id = "";
528        for($cnt = 0; $cnt < $max; $cnt++) {
529            if($arrRet[$cnt][$id_name] == $id) {
530                $down_id = $arrRet[($cnt + 1)][$id_name];
531                break;
532            }
533        }
534        return $down_id;
535    }
536
537    // 並びが1つ上のIDを取得する。
538    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
539        // 親IDを取得する。
540        $col = "$pid_name";
541        $where = "$id_name = ?";
542        $pid = $objQuery->get($col, $table, $where, $id);
543        // すべての子を取得する。
544        $col = "$id_name";
545        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
546        $arrRet = $objQuery->select($col, $table, $where, array($pid));
547        $max = count($arrRet);
548        $up_id = "";
549        for($cnt = 0; $cnt < $max; $cnt++) {
550            if($arrRet[$cnt][$id_name] == $id) {
551                $up_id = $arrRet[($cnt - 1)][$id_name];
552                break;
553            }
554        }
555        return $up_id;
556    }
557
558    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
559        $objDb = new SC_Helper_DB_Ex();
560        // 子ID一覧を取得
561        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
562        return count($arrRet);
563    }
564
565    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
566        $objDb = new SC_Helper_DB_Ex();
567        // 子ID一覧を取得
568        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
569        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
570        $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
571        $sql.= "AND del_flg = 0";
572        $ret = $objQuery->exec($sql);
573        return $ret;
574    }
575
576    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
577        $objDb = new SC_Helper_DB_Ex();
578        // 子ID一覧を取得
579        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
580        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
581        $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
582        $sql.= "AND del_flg = 0";
583        $ret = $objQuery->exec($sql);
584        return $ret;
585    }
586}
Note: See TracBrowser for help on using the repository browser.