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

Revision 22567, 22.7 KB checked in by shutta, 11 years ago (diff)

#2043 (typo修正・ソース整形・ソースコメントの改善 for 2.12.4)
Zend Framework PHP 標準コーディング規約のコーディングスタイルへ準拠。
classおよびfunctionの開始波括弧「{」のスタイルを修正。

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