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

Revision 22857, 22.6 KB checked in by Seasoft, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.13.0)

  • 主に空白・空白行の調整。
  • 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    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 = " fnSetFocus('category_name'); ";
49    }
50
51    /**
52     * Page のプロセス.
53     *
54     * @return void
55     */
56    function process()
57    {
58        $this->action();
59        $this->sendResponse();
60    }
61
62    /**
63     * Page のアクション.
64     *
65     * @return void
66     */
67    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                    } else if ($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    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            return;
210        }
211        // 登録商品のチェック
212        $table = 'dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id';
213        $where = 'T1.category_id = ? AND T2.del_flg = 0';
214        $exists = $objQuery->exists($table, $where, array($category_id));
215        if ($exists) {
216            $this->arrErr['category_name'] = '※ カテゴリ内に商品が存在するため削除できません。<br/>';
217            return;
218        }
219
220        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
221        $objDb->sfDeleteRankRecord('dtb_category', 'category_id', $category_id, '', true);
222    }
223
224    /**
225     * 編集対象のカテゴリ名を, 入力ボックスへ表示する.
226     *
227     * @param SC_FormParam $objFormParam
228     * @return void
229     */
230    function doPreEdit(&$objFormParam)
231    {
232        $category_id = $objFormParam->getValue('category_id');
233
234        $objCategory = new SC_Helper_Category_Ex();
235        $arrRes = $objCategory->get($category_id);
236
237        $objFormParam->setParam($arrRes);
238
239        $this->arrForm = $objFormParam->getHashArray();
240    }
241
242    /**
243     * カテゴリの登録・編集を実行する.
244     *
245     * 下記の場合は, 登録・編集を実行せず、エラーメッセージを表示する
246     *
247     * - カテゴリ名がすでに使用されている場合
248     * - 階層登録数の上限を超える場合 (登録時のみ評価)
249     * - カテゴリ名がすでに使用されている場合 (登録時のみ評価)
250     *
251     * @param SC_FormParam $objFormParam
252     * @return void
253     */
254    function doEdit(&$objFormParam)
255    {
256        $category_id = $objFormParam->getValue('category_id');
257
258        // 追加か
259        $add = strlen($category_id) === 0;
260
261        // エラーチェック
262        $this->arrErr = $this->checkError($objFormParam, $add);
263
264        // エラーがない場合、追加・更新処理
265        if (empty($this->arrErr)) {
266            $arrCategory = $objFormParam->getDbArray();
267
268            // 追加
269            if ($add) {
270                $this->registerCategory($arrCategory);
271            }
272            // 更新
273            else {
274                unset($arrCategory['category_id']);
275                $this->updateCategory($category_id, $arrCategory);
276            }
277        }
278        // エラーがある場合、入力値の再表示
279        else {
280            $this->arrForm = $objFormParam->getHashArray();
281        }
282    }
283
284    /**
285     * エラーチェック
286     *
287     * @param SC_FormParam $objFormParam
288     * @param boolean $add 追加か
289     * @return void
290     */
291    function checkError(&$objFormParam, $add)
292    {
293        $objQuery =& SC_Query_Ex::getSingletonInstance();
294
295        // 入力項目チェック
296        $arrErr = $objFormParam->checkError();
297        if (!empty($arrErr)) {
298            return $arrErr;
299        }
300
301        $category_id = $objFormParam->getValue('category_id');
302        $parent_category_id = $objFormParam->getValue('parent_category_id');
303        $category_name = $objFormParam->getValue('category_name');
304
305        // 追加の場合に固有のチェック
306        if ($add) {
307            // 登録数上限チェック
308            $where = 'del_flg = 0';
309            $count = $objQuery->count('dtb_category', $where);
310            if ($count >= CATEGORY_MAX) {
311                $arrErr['category_name'] = '※ カテゴリの登録最大数を超えました。<br/>';
312                return $arrErr;
313            }
314
315            // 階層上限チェック
316            if ($this->isOverLevel($parent_category_id)) {
317                $arrErr['category_name'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。<br/>';
318                return $arrErr;
319            }
320        }
321
322        // 重複チェック
323        $arrWhereVal = array();
324        $where = 'del_flg = 0 AND parent_category_id = ? AND category_name = ?';
325        $arrWhereVal[] = $parent_category_id;
326        $arrWhereVal[] = $category_name;
327        // 更新の場合、抽出対象から自己を除外する
328        if (!$add) {
329            $where .= ' AND category_id <> ?';
330            $arrWhereVal[] = $category_id;
331        }
332        $exists = $objQuery->exists('dtb_category', $where, $arrWhereVal);
333        if ($exists) {
334            $arrErr['category_name'] = '※ 既に同じ内容の登録が存在します。<br/>';
335            return $arrErr;
336        }
337
338        return $arrErr;
339    }
340
341    /**
342     * カテゴリの表示順序を上へ移動する.
343     *
344     * @param SC_FormParam $objFormParam
345     * @return void
346     */
347    function doUp(&$objFormParam)
348    {
349        $category_id = $objFormParam->getValue('category_id');
350
351        $objQuery =& SC_Query_Ex::getSingletonInstance();
352        $objQuery->begin();
353        $up_id = $this->lfGetUpRankID($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
354        if ($up_id != '') {
355            // 上のグループのrankから減算する数
356            $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
357                // 自分のグループのrankに加算する数
358                $up_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id);
359                if ($my_count > 0 && $up_count > 0) {
360                    // 自分のグループに加算
361                    $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id, $up_count);
362                    // 上のグループから減算
363                    $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id, $my_count);
364                }
365        }
366        $objQuery->commit();
367    }
368
369    /**
370     * カテゴリの表示順序を下へ移動する.
371     *
372     * @param SC_FormParam $objFormParam
373     * @return void
374     */
375    function doDown(&$objFormParam)
376    {
377        $category_id = $objFormParam->getValue('category_id');
378
379        $objQuery =& SC_Query_Ex::getSingletonInstance();
380        $objQuery->begin();
381        $down_id = $this->lfGetDownRankID($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
382        if ($down_id != '') {
383            // 下のグループのrankに加算する数
384            $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
385            // 自分のグループのrankから減算する数
386            $down_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $down_id);
387            if ($my_count > 0 && $down_count > 0) {
388                // 自分のグループから減算
389                $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $down_id, $my_count);
390                // 下のグループに加算
391                $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id, $down_count);
392            }
393        }
394        $objQuery->commit();
395    }
396
397    /**
398     * パラメーターの初期化を行う
399     *
400     * @param SC_FormParam $objFormParam
401     * @return void
402     */
403    function initParam(&$objFormParam)
404    {
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    {
422        if (!$parent_category_id) {
423            $parent_category_id = 0;
424        }
425        $objQuery =& SC_Query_Ex::getSingletonInstance();
426        $col   = 'category_id, category_name, level, rank';
427        $where = 'del_flg = 0 AND parent_category_id = ?';
428        $objQuery->setOption('ORDER BY rank DESC');
429
430        return $objQuery->select($col, 'dtb_category', $where, array($parent_category_id));
431    }
432
433    /**
434     * カテゴリを更新する
435     *
436     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
437     * @return void
438     */
439    function updateCategory($category_id, $arrCategory)
440    {
441        $objQuery =& SC_Query_Ex::getSingletonInstance();
442
443        $arrCategory['update_date']   = 'CURRENT_TIMESTAMP';
444
445        $objQuery->begin();
446        $where = 'category_id = ?';
447        $objQuery->update('dtb_category', $arrCategory, $where, array($category_id));
448        $objQuery->commit();
449    }
450
451    /**
452     * カテゴリを登録する
453     *
454     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
455     * @return void
456     */
457    function registerCategory($arrCategory)
458    {
459        $objQuery =& SC_Query_Ex::getSingletonInstance();
460
461        $parent_category_id = $arrCategory['parent_category_id'];
462
463        $objQuery->begin();
464
465        $rank = null;
466        if ($parent_category_id == 0) {
467            // ROOT階層で最大のランクを取得する。
468            $where = 'parent_category_id = ?';
469            $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1;
470        } else {
471            // 親のランクを自分のランクとする。
472            $where = 'category_id = ?';
473            $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id));
474            // 追加レコードのランク以上のレコードを一つあげる。
475            $where = 'rank >= ?';
476            $arrRawSql = array(
477                'rank' => '(rank + 1)',
478            );
479            $objQuery->update('dtb_category', array(), $where, array($rank), $arrRawSql);
480        }
481
482        $where = 'category_id = ?';
483        // 自分のレベルを取得する(親のレベル + 1)
484        $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1;
485
486        $arrCategory['create_date'] = 'CURRENT_TIMESTAMP';
487        $arrCategory['update_date'] = 'CURRENT_TIMESTAMP';
488        $arrCategory['creator_id']  = $_SESSION['member_id'];
489        $arrCategory['rank']        = $rank;
490        $arrCategory['level']       = $level;
491        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
492
493        $objQuery->insert('dtb_category', $arrCategory);
494
495        $objQuery->commit();    // トランザクションの終了
496    }
497
498    /**
499     * カテゴリの階層が上限を超えているかを判定する
500     *
501     * @param integer 親カテゴリID
502     * @param 超えている場合 true
503     */
504    function isOverLevel($parent_category_id)
505    {
506        $objQuery =& SC_Query_Ex::getSingletonInstance();
507        $level = $objQuery->get('level', 'dtb_category', 'category_id = ?', array($parent_category_id));
508
509        return $level >= LEVEL_MAX;
510    }
511
512    /**
513     * デストラクタ.
514     *
515     * @return void
516     */
517    function destroy()
518    {
519        parent::destroy();
520    }
521
522    // 並びが1つ下のIDを取得する。
523    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id)
524    {
525        // 親IDを取得する。
526        $col = "$pid_name";
527        $where = "$id_name = ?";
528        $pid = $objQuery->get($col, $table, $where, $id);
529        // すべての子を取得する。
530        $col = "$id_name";
531        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
532        $arrRet = $objQuery->select($col, $table, $where, array($pid));
533        $max = count($arrRet);
534        $down_id = '';
535        for ($cnt = 0; $cnt < $max; $cnt++) {
536            if ($arrRet[$cnt][$id_name] == $id) {
537                $down_id = $arrRet[($cnt + 1)][$id_name];
538                break;
539            }
540        }
541
542        return $down_id;
543    }
544
545    // 並びが1つ上のIDを取得する。
546    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id)
547    {
548        // 親IDを取得する。
549        $col = "$pid_name";
550        $where = "$id_name = ?";
551        $pid = $objQuery->get($col, $table, $where, $id);
552        // すべての子を取得する。
553        $col = "$id_name";
554        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
555        $arrRet = $objQuery->select($col, $table, $where, array($pid));
556        $max = count($arrRet);
557        $up_id = '';
558        for ($cnt = 0; $cnt < $max; $cnt++) {
559            if ($arrRet[$cnt][$id_name] == $id) {
560                $up_id = $arrRet[($cnt - 1)][$id_name];
561                break;
562            }
563        }
564
565        return $up_id;
566    }
567
568    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id)
569    {
570        $objDb = new SC_Helper_DB_Ex();
571        // 子ID一覧を取得
572        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
573
574        return count($arrRet);
575    }
576
577    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count)
578    {
579        $objDb = new SC_Helper_DB_Ex();
580        // 子ID一覧を取得
581        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
582        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
583        $where = "$id_name IN ($line) AND del_flg = 0";
584        $arrRawVal = array(
585            'rank' => "(rank + $count)",
586        );
587
588        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
589    }
590
591    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count)
592    {
593        $objDb = new SC_Helper_DB_Ex();
594        // 子ID一覧を取得
595        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
596        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
597        $where = "$id_name IN ($line) AND del_flg = 0";
598        $arrRawVal = array(
599            'rank' => "(rank - $count)",
600        );
601
602        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
603    }
604}
Note: See TracBrowser for help on using the repository browser.