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

Revision 23438, 14.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
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->arrTree = $objCategory->getTree(true);
176        $this->arrParentID = $objCategory->getTreeTrail($parent_category_id);
177        // カテゴリ一覧を取得
178        $this->arrList = $objCategory->getTreeBranch($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        // エラーチェック
252        $this->arrErr = $this->checkError($objFormParam);
253
254        // エラーがない場合、追加・更新処理
255        if (empty($this->arrErr)) {
256            $arrCategory = $objFormParam->getDbArray();
257            $objCategory = new SC_Helper_Category_Ex();
258            $objCategory->save($arrCategory);
259        }
260        // エラーがある場合、入力値の再表示
261        else {
262            $this->arrForm = $objFormParam->getHashArray();
263        }
264    }
265
266    /**
267     * エラーチェック
268     *
269     * @param  SC_FormParam $objFormParam
270     * @return array
271     */
272    public function checkError(&$objFormParam)
273    {
274        $objCategory = new SC_Helper_Category_Ex();
275
276        // 入力項目チェック
277        $arrErr = $objFormParam->checkError();
278        if (!empty($arrErr)) {
279            return $arrErr;
280        }
281
282        $category_id = $objFormParam->getValue('category_id');
283        $parent_category_id = $objFormParam->getValue('parent_category_id');
284        $category_name = $objFormParam->getValue('category_name');
285
286        // 追加の場合に固有のチェック
287        if (!$category_id) {
288            // 登録数上限チェック
289            $count = count($objCategory->getList());
290            if ($count >= CATEGORY_MAX) {
291                $arrErr['category_name'] = '※ カテゴリの登録最大数を超えました。<br/>';
292
293                return $arrErr;
294            }
295
296            // 階層上限チェック
297            $arrParent = $objCategory->get($parent_category_id);
298            if ($arrParent['level'] >= LEVEL_MAX) {
299                $arrErr['category_name'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。<br/>';
300
301                return $arrErr;
302            }
303        }
304
305        // 重複チェック
306        $exists = false;
307        $arrBrother = $objCategory->getTreeBranch($parent_category_id);
308        foreach($arrBrother as $brother) {
309            if ($brother['category_name'] == $category_name && $brother['category_id'] != $category_id) {
310                $exists = true;
311            }
312        }
313        if ($exists) {
314            $arrErr['category_name'] = '※ 既に同じ内容の登録が存在します。<br/>';
315
316            return $arrErr;
317        }
318
319        return $arrErr;
320    }
321
322    /**
323     * カテゴリの表示順序を上へ移動する.
324     *
325     * @param  SC_FormParam $objFormParam
326     * @return void
327     */
328    public function doUp(&$objFormParam)
329    {
330        $objCategory = new SC_Helper_Category_Ex(false);
331        $category_id = $objFormParam->getValue('category_id');
332        $objCategory->rankUp($category_id);
333    }
334
335    /**
336     * カテゴリの表示順序を下へ移動する.
337     *
338     * @param  SC_FormParam $objFormParam
339     * @return void
340     */
341    public function doDown(&$objFormParam)
342    {
343        $objCategory = new SC_Helper_Category_Ex(false);
344        $category_id = $objFormParam->getValue('category_id');
345        $objCategory->rankDown($category_id);
346    }
347
348    /**
349     * パラメーターの初期化を行う
350     *
351     * @param  SC_FormParam $objFormParam
352     * @return void
353     */
354    public function initParam(&$objFormParam)
355    {
356        $objFormParam->addParam('親カテゴリID', 'parent_category_id', null, null, array());
357        $objFormParam->addParam('カテゴリID', 'category_id', null, null, array());
358        $objFormParam->addParam('カテゴリ名', 'category_name', STEXT_LEN, 'KVa', array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
359    }
360
361    public function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id)
362    {
363        $objDb = new SC_Helper_DB_Ex();
364        // 子ID一覧を取得
365        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
366
367        return count($arrRet);
368    }
369
370    public function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count)
371    {
372        $objDb = new SC_Helper_DB_Ex();
373        // 子ID一覧を取得
374        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
375        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
376        $where = "$id_name IN ($line) AND del_flg = 0";
377        $arrRawVal = array(
378            'rank' => "(rank + $count)",
379        );
380
381        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
382    }
383
384    public function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count)
385    {
386        $objDb = new SC_Helper_DB_Ex();
387        // 子ID一覧を取得
388        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
389        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
390        $where = "$id_name IN ($line) AND del_flg = 0";
391        $arrRawVal = array(
392            'rank' => "(rank - $count)",
393        );
394
395        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
396    }
397}
Note: See TracBrowser for help on using the repository browser.