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

Revision 23436, 18.8 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
RevLine 
[15518]1<?php
[16582]2/*
3 * This file is part of EC-CUBE
4 *
[22206]5 * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved.
[16582]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 */
[15518]23
[20534]24require_once CLASS_EX_REALDIR . 'page_extends/admin/LC_Page_Admin_Ex.php';
[15518]25
26/**
27 * カテゴリ管理 のページクラス.
28 *
29 * @package Page
30 * @author LOCKON CO.,LTD.
31 * @version $Id$
32 */
[22856]33class LC_Page_Admin_Products_Category extends LC_Page_Admin_Ex
[22567]34{
[15518]35    /**
36     * Page を初期化する.
37     *
38     * @return void
39     */
[23124]40    public function init()
[22567]41    {
[15518]42        parent::init();
[20911]43        $this->tpl_maintitle = '商品管理';
[21317]44        $this->tpl_subtitle = 'カテゴリ登録';
[15518]45        $this->tpl_mainpage = 'products/category.tpl';
46        $this->tpl_mainno = 'products';
[20210]47        $this->tpl_subno  = 'category';
[23066]48        $this->tpl_onload = " eccube.setFocus('category_name'); ";
[15518]49    }
50
51    /**
52     * Page のプロセス.
53     *
54     * @return void
55     */
[23124]56    public function process()
[22567]57    {
[19661]58        $this->action();
59        $this->sendResponse();
60    }
61
62    /**
63     * Page のアクション.
64     *
65     * @return void
66     */
[23124]67    public function action()
[22567]68    {
[20501]69        $objFormParam = new SC_FormParam_Ex();
[22596]70        $objCategory = new SC_Helper_Category_Ex();
[15518]71
[20970]72        // 入力パラメーター初期化
[20210]73        $this->initParam($objFormParam);
74        $objFormParam->setParam($_POST);
75        $objFormParam->convParam();
[21478]76
[21441]77        switch ($this->getMode()) {
[21526]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':
[23435]88                $this->doDelete($objFormParam);
[21526]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]) {
[21750]103                    $objQuery =& SC_Query_Ex::getSingletonInstance();
[21526]104                    $objQuery->begin();
[20041]105
[21526]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]));
[17872]110
[21526]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                    }
[17872]123
[21526]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);
[23124]136                    } elseif ($rankAry[$keys[1]] < $rank) {
[21526]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);
[17872]148                    }
[21526]149                    $objQuery->commit();
[17872]150                }
[21526]151                break;
152            // カテゴリツリークリック時
153            case 'tree':
154                break;
[21527]155            // CSVダウンロード
[21526]156            case 'csv':
157                // CSVを送信する
158                $objCSV = new SC_Helper_CSV_Ex();
[21592]159
[21526]160                $objCSV->sfDownloadCsv('5', '', array(), '', true);
[21743]161                SC_Response_Ex::actionExit();
[21526]162                break;
163            default:
164                break;
[15518]165        }
166
[20210]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        // カテゴリツリーを取得
[22596]177        $this->arrTree = $objCategory->getTree();
178        $this->arrParentID = $objCategory->getTreeTrail($parent_category_id);
[20210]179        // ぱんくずの生成
[22597]180        $arrBread = $objCategory->getTreeTrail($this->arrForm['parent_category_id'], FALSE);
181        $this->tpl_bread_crumbs = SC_Utils_Ex::jsonEncode(array_reverse($arrBread));
[15518]182    }
183
184    /**
[20210]185     * カテゴリの削除を実行する.
[15518]186     *
[20210]187     * 下記の場合は削除を実施せず、エラーメッセージを表示する.
188     *
189     * - 削除対象のカテゴリに、子カテゴリが1つ以上ある場合
190     * - 削除対象のカテゴリを、登録商品が使用している場合
191     *
192     * カテゴリの削除は、物理削除で行う.
193     *
[23124]194     * @param  SC_FormParam $objFormParam
[15518]195     * @return void
196     */
[23435]197    public function doDelete(&$objFormParam)
[22567]198    {
[23435]199        $objCategory = new SC_Helper_Category_Ex(false);
[20210]200        $category_id = $objFormParam->getValue('category_id');
201
202        // 子カテゴリのチェック
[23435]203        $arrBranch = $objCategory->getTreeBranch($category_id);
204        if (count($arrBranch) > 0) {
[21527]205            $this->arrErr['category_name'] = '※ 子カテゴリが存在するため削除できません。<br/>';
206            return;
[20210]207        }
208        // 登録商品のチェック
[23435]209        $arrCategory = $objCategory->get($category_id);
210        if ($arrCategory['product_count'] > 0) {
[21514]211            $this->arrErr['category_name'] = '※ カテゴリ内に商品が存在するため削除できません。<br/>';
[20210]212            return;
213        }
214
215        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
[23435]216        $objCategory->delete($category_id);
[15518]217    }
218
[20210]219    /**
220     * 編集対象のカテゴリ名を, 入力ボックスへ表示する.
221     *
[23124]222     * @param  SC_FormParam $objFormParam
[20210]223     * @return void
224     */
[23124]225    public function doPreEdit(&$objFormParam)
[22567]226    {
[20210]227        $category_id = $objFormParam->getValue('category_id');
[15518]228
[22595]229        $objCategory = new SC_Helper_Category_Ex();
230        $arrRes = $objCategory->get($category_id);
[15518]231
[21356]232        $objFormParam->setParam($arrRes);
233
234        $this->arrForm = $objFormParam->getHashArray();
[20210]235    }
[15518]236
[20210]237    /**
[21356]238     * カテゴリの登録・編集を実行する.
[20210]239     *
[21356]240     * 下記の場合は, 登録・編集を実行せず、エラーメッセージを表示する
[20210]241     *
242     * - カテゴリ名がすでに使用されている場合
[21356]243     * - 階層登録数の上限を超える場合 (登録時のみ評価)
244     * - カテゴリ名がすでに使用されている場合 (登録時のみ評価)
[20210]245     *
[23124]246     * @param  SC_FormParam $objFormParam
[20210]247     * @return void
248     */
[23124]249    public function doEdit(&$objFormParam)
[22567]250    {
[21357]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     *
[23124]282     * @param  SC_FormParam $objFormParam
283     * @param  boolean      $add          追加か
[21357]284     * @return void
285     */
[23124]286    public function checkError(&$objFormParam, $add)
[22567]287    {
[21356]288        $objQuery =& SC_Query_Ex::getSingletonInstance();
289
[20210]290        // 入力項目チェック
291        $arrErr = $objFormParam->checkError();
[21357]292        if (!empty($arrErr)) {
293            return $arrErr;
[20210]294        }
[15518]295
[21356]296        $category_id = $objFormParam->getValue('category_id');
297        $parent_category_id = $objFormParam->getValue('parent_category_id');
298        $category_name = $objFormParam->getValue('category_name');
[15518]299
[21356]300        // 追加の場合に固有のチェック
301        if ($add) {
302            // 登録数上限チェック
[21514]303            $where = 'del_flg = 0';
[21481]304            $count = $objQuery->count('dtb_category', $where);
[21356]305            if ($count >= CATEGORY_MAX) {
[21514]306                $arrErr['category_name'] = '※ カテゴリの登録最大数を超えました。<br/>';
[23124]307
[21357]308                return $arrErr;
[21356]309            }
[15518]310
[21356]311            // 階層上限チェック
312            if ($this->isOverLevel($parent_category_id)) {
[21515]313                $arrErr['category_name'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。<br/>';
[23124]314
[21357]315                return $arrErr;
[21356]316            }
[20210]317        }
[15518]318
[21356]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;
[20210]328        }
[21376]329        $exists = $objQuery->exists('dtb_category', $where, $arrWhereVal);
330        if ($exists) {
[21514]331            $arrErr['category_name'] = '※ 既に同じ内容の登録が存在します。<br/>';
[23124]332
[21357]333            return $arrErr;
[20210]334        }
335
[21357]336        return $arrErr;
[15518]337    }
[20540]338
[20210]339    /**
340     * カテゴリの表示順序を上へ移動する.
341     *
[23124]342     * @param  SC_FormParam $objFormParam
[20210]343     * @return void
344     */
[23124]345    public function doUp(&$objFormParam)
[22567]346    {
[23436]347        $objCategory = new SC_Helper_Category_Ex(false);
[20210]348        $category_id = $objFormParam->getValue('category_id');
[23436]349        $objCategory->rankUp($category_id);
[15518]350    }
351
[20210]352    /**
353     * カテゴリの表示順序を下へ移動する.
354     *
[23124]355     * @param  SC_FormParam $objFormParam
[20210]356     * @return void
357     */
[23124]358    public function doDown(&$objFormParam)
[22567]359    {
[23436]360        $objCategory = new SC_Helper_Category_Ex(false);
[20210]361        $category_id = $objFormParam->getValue('category_id');
[23436]362        $objCategory->rankDown($category_id);
[20210]363    }
[15518]364
[20210]365    /**
[20970]366     * パラメーターの初期化を行う
[20210]367     *
[23124]368     * @param  SC_FormParam $objFormParam
[20210]369     * @return void
370     */
[23124]371    public function initParam(&$objFormParam)
[22567]372    {
[21514]373        $objFormParam->addParam('親カテゴリID', 'parent_category_id', null, null, array());
374        $objFormParam->addParam('カテゴリID', 'category_id', null, null, array());
375        $objFormParam->addParam('カテゴリ名', 'category_name', STEXT_LEN, 'KVa', array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
[20210]376    }
[20540]377
[20210]378    /**
379     * 親カテゴリIDでカテゴリを検索する.
380     *
381     * - 表示順の降順でソートする
382     * - 有効なカテゴリを返す(del_flag = 0)
383     *
[23124]384     * @param  SC_Query $objQuery
385     * @param  int      $parent_category_id 親カテゴリID
386     * @return array    カテゴリの配列
[20210]387     */
[23124]388    public function findCategoiesByParentCategoryId($parent_category_id)
[22567]389    {
[20210]390        if (!$parent_category_id) {
391            $parent_category_id = 0;
392        }
[20507]393        $objQuery =& SC_Query_Ex::getSingletonInstance();
[21514]394        $col   = 'category_id, category_name, level, rank';
395        $where = 'del_flg = 0 AND parent_category_id = ?';
396        $objQuery->setOption('ORDER BY rank DESC');
[22856]397
[21481]398        return $objQuery->select($col, 'dtb_category', $where, array($parent_category_id));
[15518]399    }
400
[20210]401    /**
402     * カテゴリを更新する
403     *
[23124]404     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
[20210]405     * @return void
406     */
[23124]407    public function updateCategory($category_id, $arrCategory)
[22567]408    {
[20507]409        $objQuery =& SC_Query_Ex::getSingletonInstance();
[21356]410
411        $arrCategory['update_date']   = 'CURRENT_TIMESTAMP';
412
[20210]413        $objQuery->begin();
[21514]414        $where = 'category_id = ?';
[21481]415        $objQuery->update('dtb_category', $arrCategory, $where, array($category_id));
[20210]416        $objQuery->commit();
[15518]417    }
418
[20210]419    /**
420     * カテゴリを登録する
421     *
[23124]422     * @param  SC_FormParam $objFormParam SC_FormParam インスタンス
[20210]423     * @return void
424     */
[23124]425    public function registerCategory($arrCategory)
[22567]426    {
[20507]427        $objQuery =& SC_Query_Ex::getSingletonInstance();
[21356]428
429        $parent_category_id = $arrCategory['parent_category_id'];
430
[20210]431        $objQuery->begin();
[15518]432
[20210]433        $rank = null;
434        if ($parent_category_id == 0) {
435            // ROOT階層で最大のランクを取得する。
[21514]436            $where = 'parent_category_id = ?';
[21481]437            $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1;
[20210]438        } else {
439            // 親のランクを自分のランクとする。
[21514]440            $where = 'category_id = ?';
[21481]441            $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id));
[20210]442            // 追加レコードのランク以上のレコードを一つあげる。
[22012]443            $where = 'rank >= ?';
444            $arrRawSql = array(
445                'rank' => '(rank + 1)',
446            );
447            $objQuery->update('dtb_category', array(), $where, array($rank), $arrRawSql);
[15518]448        }
449
[21514]450        $where = 'category_id = ?';
[20210]451        // 自分のレベルを取得する(親のレベル + 1)
[21481]452        $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1;
[15518]453
[21185]454        $arrCategory['create_date'] = 'CURRENT_TIMESTAMP';
455        $arrCategory['update_date'] = 'CURRENT_TIMESTAMP';
[21356]456        $arrCategory['creator_id']  = $_SESSION['member_id'];
[20210]457        $arrCategory['rank']        = $rank;
458        $arrCategory['level']       = $level;
459        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
[21356]460
[21481]461        $objQuery->insert('dtb_category', $arrCategory);
[15518]462
[20210]463        $objQuery->commit();    // トランザクションの終了
464    }
[20540]465
[20210]466    /**
467     * カテゴリの階層が上限を超えているかを判定する
468     *
[23436]469     * @param int $parent_category_id 親カテゴリID
470     * @return bool 超えている場合 true
[20210]471     */
[23124]472    public function isOverLevel($parent_category_id)
[22567]473    {
[23436]474        $objCategory = new SC_Helper_Category_Ex();
475        $arrCategory = $objCategory->get($parent_category_id);
[22856]476
[23436]477        return $arrCategory['level'] >= LEVEL_MAX;
[20210]478    }
[15518]479
[23124]480    public function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id)
[22567]481    {
[15518]482        $objDb = new SC_Helper_DB_Ex();
483        // 子ID一覧を取得
484        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
[22856]485
[15518]486        return count($arrRet);
487    }
488
[23124]489    public function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count)
[22567]490    {
[15518]491        $objDb = new SC_Helper_DB_Ex();
492        // 子ID一覧を取得
493        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
494        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
[22012]495        $where = "$id_name IN ($line) AND del_flg = 0";
496        $arrRawVal = array(
497            'rank' => "(rank + $count)",
498        );
[22856]499
[22012]500        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
[15518]501    }
502
[23124]503    public function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count)
[22567]504    {
[15518]505        $objDb = new SC_Helper_DB_Ex();
506        // 子ID一覧を取得
507        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
508        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
[22012]509        $where = "$id_name IN ($line) AND del_flg = 0";
510        $arrRawVal = array(
511            'rank' => "(rank - $count)",
512        );
[22856]513
[22012]514        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
[15518]515    }
516}
Note: See TracBrowser for help on using the repository browser.