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

Revision 20688, 23.5 KB checked in by nanasess, 13 years ago (diff)

#1000 (管理画面 ->ファイル管理, カテゴリー登録, 商品並び替えのパンクズにリンクをつける)

  • jQuery のプラグインにしました
  • 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';
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_subtitle = 'カテゴリー登録';
50        $this->tpl_mainpage = 'products/category.tpl';
51        $this->tpl_subnavi  = 'products/subnavi.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->sfDownloadCategoryCsv() && exit;
169            break;
170        default:
171            break;
172        }
173
174        $parent_category_id = $objFormParam->getValue('parent_category_id');
175        // 空の場合は親カテゴリを0にする
176        if (empty($parent_category_id)) {
177            $parent_category_id = 0;
178        }
179        // 親カテゴリIDの保持
180        $this->arrForm['parent_category_id'] = $parent_category_id;
181        // カテゴリ一覧を取得
182        $this->arrList = $this->findCategoiesByParentCategoryId($parent_category_id);
183        // カテゴリツリーを取得
184        $this->arrTree = $objDb->sfGetCatTree($parent_category_id);
185        // ぱんくずの生成
186        $arrBread = array();
187        $objDb->findTree($this->arrTree, $parent_category_id, $arrBread);
188        $this->tpl_bread_crumbs = SC_Utils_Ex::jsonEncode($arrBread);
189    }
190
191    /**
192     * カテゴリの削除を実行する.
193     *
194     * 下記の場合は削除を実施せず、エラーメッセージを表示する.
195     *
196     * - 削除対象のカテゴリに、子カテゴリが1つ以上ある場合
197     * - 削除対象のカテゴリを、登録商品が使用している場合
198     *
199     * カテゴリの削除は、物理削除で行う.
200     *
201     * @param SC_FormParam $objFormParam
202     * @param SC_Helper_Db $objDb
203     * @return void
204     */
205    function doDelete(&$objFormParam, &$objDb) {
206        $category_id = $objFormParam->getValue('category_id');
207        $objQuery =& SC_Query_Ex::getSingletonInstance();
208
209        // 子カテゴリのチェック
210        $where = "parent_category_id = ? AND del_flg = 0";
211        $count = $objQuery->count("dtb_category", $where, array($category_id));
212        if ($count > 0) {
213             $this->arrErr['category_name'] = "※ 子カテゴリが存在するため削除できません。<br/>";
214             return;
215        }
216        // 登録商品のチェック
217        $table = "dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id";
218        $where = "T1.category_id = ? AND T2.del_flg = 0";
219        $count = $objQuery->count($table, $where, array($category_id));
220        if ($count > 0) {
221            $this->arrErr['category_name'] = "※ カテゴリ内に商品が存在するため削除できません。<br/>";
222            return;
223        }
224
225        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
226        $objDb->sfDeleteRankRecord("dtb_category", "category_id", $category_id, "", true);
227    }
228
229    /**
230     * 編集対象のカテゴリ名を, 入力ボックスへ表示する.
231     *
232     * @param SC_FormParam $objFormParam
233     * @return void
234     */
235    function doPreEdit(&$objFormParam) {
236        $category_id = $objFormParam->getValue('category_id');
237
238        $objQuery =& SC_Query_Ex::getSingletonInstance();
239
240        // 編集対象のカテゴリ名をDBより取得する
241        $where = "category_id = ?";
242        $category_name = $objQuery->get("category_name", "dtb_category", $where, array($category_id));
243
244        // 入力ボックスへカテゴリ名を保持する.
245        $this->arrForm['category_name'] = $category_name;
246        // カテゴリIDを保持する.
247        $this->arrForm['category_id']   = $category_id;
248    }
249
250    /**
251     * カテゴリの編集を実行する.
252     *
253     * 下記の場合は, 編集を実行せず、エラーメッセージを表示する
254     *
255     * - カテゴリ名がすでに使用されている場合
256     *
257     * @param SC_FormParam $objFormParam
258     * @return void
259     */
260    function doEdit(&$objFormParam) {
261        // 入力項目チェック
262        $arrErr = $objFormParam->checkError();
263        if (count($arrErr) > 0) {
264            $this->arrErr = $arrErr;
265            $this->arrForm['category_id']   = $objFormParam->getValue('category_id');
266            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
267            return;
268        }
269
270        // 重複チェック
271        $objQuery =& SC_Query_Ex::getSingletonInstance();
272        $where = "parent_category_id = ? AND category_id <> ? AND category_name = ?";
273        $count = $objQuery->count("dtb_category",
274                                  $where,
275                                  array($objFormParam->getValue('parent_category_id'),
276                                        $objFormParam->getValue('category_id'),
277                                        $objFormParam->getValue('category_name')));
278        if ($count > 0) {
279            $this->arrErr['category_name']  = "※ 既に同じ内容の登録が存在します。<br/>";
280            $this->arrForm['category_id']   = $objFormParam->getValue('category_id');
281            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
282            return;
283        }
284
285        // カテゴリ更新
286        $arrCategory = array();
287        $arrCategory['category_name'] = $objFormParam->getValue('category_name');
288        $arrCategory['update_date']   = 'NOW()';
289        $this->updateCategory($objFormParam->getValue('category_id'), $arrCategory);
290    }
291
292    /**
293     * カテゴリの登録を実行する.
294     *
295     * 下記の場合は, 登録を実行せず、エラーメッセージを表示する
296     *
297     * - カテゴリ登録数の上限を超える場合
298     * - 階層登録数の上限を超える場合
299     * - カテゴリ名がすでに使用されている場合
300     *
301     * @param SC_FormParam $objFormParam
302     * @return void
303     */
304    function doRegister(&$objFormParam) {
305        // 入力項目チェック
306        $arrErr = $objFormParam->checkError();
307        if (count($arrErr) > 0) {
308            $this->arrErr = $arrErr;
309            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
310            return;
311        }
312
313        // 登録数上限チェック
314        $objQuery =& SC_Query_Ex::getSingletonInstance();
315        $where = "del_flg = 0";
316        $count = $objQuery->count("dtb_category", $where);
317        if ($count >= CATEGORY_MAX) {
318            $this->arrErr['category_name']  = "※ カテゴリの登録最大数を超えました。<br/>";
319            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
320            return;
321        }
322
323        // 階層上限チェック
324        if ($this->isOverLevel($objFormParam->getValue('parent_category_id'))) {
325            $this->arrErr['category_name']  = "※ " . LEVEL_MAX . "階層以上の登録はできません。<br/>";
326            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
327            return;
328        }
329
330        // 重複チェック
331        $where = "parent_category_id = ? AND category_name = ?";
332        $count = $objQuery->count("dtb_category",
333                                  $where,
334                                  array($objFormParam->getValue('parent_category_id'),
335                                        $objFormParam->getValue('category_name')));
336        if ($count > 0) {
337            $this->arrErr['category_name']  = "※ 既に同じ内容の登録が存在します。<br/>";
338            $this->arrForm['category_name'] = $objFormParam->getValue('category_name');
339            return;
340        }
341
342        // カテゴリ登録
343        $this->registerCategory($objFormParam->getValue('parent_category_id'),
344                                $objFormParam->getValue('category_name'),
345                                $_SESSION['member_id']);
346    }
347
348    /**
349     * カテゴリの表示順序を上へ移動する.
350     *
351     * @param SC_FormParam $objFormParam
352     * @return void
353     */
354    function doUp(&$objFormParam) {
355        $category_id = $objFormParam->getValue('category_id');
356
357        $objQuery =& SC_Query_Ex::getSingletonInstance();
358        $objQuery->begin();
359        $up_id = $this->lfGetUpRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
360        if ($up_id != "") {
361            // 上のグループのrankから減算する数
362            $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
363                // 自分のグループのrankに加算する数
364                $up_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id);
365                if ($my_count > 0 && $up_count > 0) {
366                    // 自分のグループに加算
367                    $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id, $up_count);
368                    // 上のグループから減算
369                    $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $up_id, $my_count);
370                }
371        }
372        $objQuery->commit();
373    }
374
375    /**
376     * カテゴリの表示順序を下へ移動する.
377     *
378     * @param SC_FormParam $objFormParam
379     * @return void
380     */
381    function doDown(&$objFormParam) {
382        $category_id = $objFormParam->getValue('category_id');
383
384        $objQuery =& SC_Query_Ex::getSingletonInstance();
385        $objQuery->begin();
386        $down_id = $this->lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
387        if ($down_id != "") {
388            // 下のグループのrankに加算する数
389            $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
390            // 自分のグループのrankから減算する数
391            $down_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id);
392            if ($my_count > 0 && $down_count > 0) {
393                // 自分のグループから減算
394                $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count);
395                // 下のグループに加算
396                $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id, $down_count);
397            }
398        }
399        $objQuery->commit();
400    }
401
402    /**
403     * パラメータの初期化を行う
404     *
405     * @param SC_FormParam $objFormParam
406     * @return void
407     */
408    function initParam(&$objFormParam) {
409        $objFormParam->addParam("親カテゴリID", "parent_category_id", null, null, array());
410        $objFormParam->addParam("カテゴリID", "category_id", null, null, array());
411        $objFormParam->addParam("カテゴリ名", "category_name", STEXT_LEN, 'KVa', array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
412    }
413
414    /**
415     * 親カテゴリIDでカテゴリを検索する.
416     *
417     * - 表示順の降順でソートする
418     * - 有効なカテゴリを返す(del_flag = 0)
419     *
420     * @param SC_Query $objQuery
421     * @param int $parent_category_id 親カテゴリID
422     * @return array カテゴリの配列
423     */
424    function findCategoiesByParentCategoryId($parent_category_id) {
425        if (!$parent_category_id) {
426            $parent_category_id = 0;
427        }
428        $objQuery =& SC_Query_Ex::getSingletonInstance();
429        $col   = "category_id, category_name, level, rank";
430        $where = "del_flg = 0 AND parent_category_id = ?";
431        $objQuery->setOption("ORDER BY rank DESC");
432        return $objQuery->select($col, "dtb_category", $where, array($parent_category_id));
433    }
434
435    /**
436     * カテゴリを更新する
437     *
438     * @param integer $category_id 更新対象のカテゴリID
439     * @param array 更新する カラム名 => 値 の連想配列
440     * @return void
441     */
442    function updateCategory($category_id, $arrCategory) {
443        $objQuery =& SC_Query_Ex::getSingletonInstance();
444        $objQuery->begin();
445        $where = "category_id = ?";
446        $objQuery->update("dtb_category", $arrCategory, $where, array($category_id));
447        $objQuery->commit();
448    }
449
450    /**
451     * カテゴリを登録する
452     *
453     * @param integer 親カテゴリID
454     * @param string カテゴリ名
455     * @param integer 作成者のID
456     * @return void
457     */
458    function registerCategory($parent_category_id, $category_name, $creator_id) {
459        $objQuery =& SC_Query_Ex::getSingletonInstance();
460        $objQuery->begin();
461
462        $rank = null;
463        if ($parent_category_id == 0) {
464            // ROOT階層で最大のランクを取得する。
465            $where = "parent_category_id = ?";
466            $rank = $objQuery->max('rank', "dtb_category", $where, array($parent_category_id)) + 1;
467        } else {
468            // 親のランクを自分のランクとする。
469            $where = "category_id = ?";
470            $rank = $objQuery->get('rank', "dtb_category", $where, array($parent_category_id));
471            // 追加レコードのランク以上のレコードを一つあげる。
472            $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
473            $objQuery->exec($sqlup, array($rank));
474        }
475
476        $where = "category_id = ?";
477        // 自分のレベルを取得する(親のレベル + 1)
478        $level = $objQuery->get('level', "dtb_category", $where, array($parent_category_id)) + 1;
479
480        $arrCategory = array();
481        $arrCategory['category_name'] = $category_name;
482        $arrCategory['parent_category_id'] = $parent_category_id;
483        $arrCategory['create_date'] = "Now()";
484        $arrCategory['update_date'] = "Now()";
485        $arrCategory['creator_id']  = $creator_id;
486        $arrCategory['rank']        = $rank;
487        $arrCategory['level']       = $level;
488        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
489        $objQuery->insert("dtb_category", $arrCategory);
490
491        $objQuery->commit();    // トランザクションの終了
492    }
493
494    /**
495     * カテゴリの階層が上限を超えているかを判定する
496     *
497     * @param integer 親カテゴリID
498     * @param 超えている場合 true
499     */
500    function isOverLevel($parent_category_id) {
501        $objQuery =& SC_Query_Ex::getSingletonInstance();
502        $level = $objQuery->get('level', "dtb_category", "category_id = ?", array($parent_category_id));
503        return $level >= LEVEL_MAX;
504    }
505
506    /**
507     * デストラクタ.
508     *
509     * @return void
510     */
511    function destroy() {
512        parent::destroy();
513    }
514
515    // 並びが1つ下のIDを取得する。
516    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
517        // 親IDを取得する。
518        $col = "$pid_name";
519        $where = "$id_name = ?";
520        $pid = $objQuery->get($col, $table, $where, $id);
521        // すべての子を取得する。
522        $col = "$id_name";
523        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
524        $arrRet = $objQuery->select($col, $table, $where, array($pid));
525        $max = count($arrRet);
526        $down_id = "";
527        for($cnt = 0; $cnt < $max; $cnt++) {
528            if($arrRet[$cnt][$id_name] == $id) {
529                $down_id = $arrRet[($cnt + 1)][$id_name];
530                break;
531            }
532        }
533        return $down_id;
534    }
535
536    // 並びが1つ上のIDを取得する。
537    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
538        // 親IDを取得する。
539        $col = "$pid_name";
540        $where = "$id_name = ?";
541        $pid = $objQuery->get($col, $table, $where, $id);
542        // すべての子を取得する。
543        $col = "$id_name";
544        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
545        $arrRet = $objQuery->select($col, $table, $where, array($pid));
546        $max = count($arrRet);
547        $up_id = "";
548        for($cnt = 0; $cnt < $max; $cnt++) {
549            if($arrRet[$cnt][$id_name] == $id) {
550                $up_id = $arrRet[($cnt - 1)][$id_name];
551                break;
552            }
553        }
554        return $up_id;
555    }
556
557    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
558        $objDb = new SC_Helper_DB_Ex();
559        // 子ID一覧を取得
560        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
561        return count($arrRet);
562    }
563
564    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
565        $objDb = new SC_Helper_DB_Ex();
566        // 子ID一覧を取得
567        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
568        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
569        $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
570        $sql.= "AND del_flg = 0";
571        $ret = $objQuery->exec($sql);
572        return $ret;
573    }
574
575    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
576        $objDb = new SC_Helper_DB_Ex();
577        // 子ID一覧を取得
578        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
579        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
580        $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
581        $sql.= "AND del_flg = 0";
582        $ret = $objQuery->exec($sql);
583        return $ret;
584    }
585}
Note: See TracBrowser for help on using the repository browser.