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

Revision 23124, 22.7 KB checked in by kimoto, 11 years ago (diff)

#2043 typo修正・ソース整形・ソースコメントの改善 for 2.13.0
PHP4的な書き方の修正

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