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

Revision 22597, 22.7 KB checked in by pineray, 11 years ago (diff)

#2166 商品並び替えページのツリーとパンくずを調整.

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