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

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