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

Revision 22796, 22.6 KB checked in by h_yoshimoto, 11 years ago (diff)

#2236 2.12.3リリース以降の2.12-devへのコミット差し戻し

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