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

Revision 23435, 22.3 KB checked in by pineray, 10 years ago (diff)

#2554 カテゴリーの削除処理を SC_Helper_Category へ移動

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