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

Revision 21514, 22.5 KB checked in by Seasoft, 12 years ago (diff)

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

  • 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-2011 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        $objDb      = new SC_Helper_DB_Ex();
73        $objFormParam = new SC_FormParam_Ex();
74
75        // 入力パラメーター初期化
76        $this->initParam($objFormParam);
77        $objFormParam->setParam($_POST);
78        $objFormParam->convParam();
79
80        // フックポイントを実行.
81        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance();
82        $objPlugin->doAction('lc_page_admin_products_category_action_start', array($this));
83
84        switch ($this->getMode()) {
85        // カテゴリ登録/編集実行
86        case 'edit':
87            $this->doEdit($objFormParam);
88            break;
89        // 入力ボックスへ編集対象のカテゴリ名をセット
90        case 'pre_edit':
91            $this->doPreEdit($objFormParam);
92            break;
93        // カテゴリ削除
94        case 'delete':
95            $this->doDelete($objFormParam, $objDb);
96            break;
97        // 表示順を上へ
98        case 'up':
99            $this->doUp($objFormParam);
100            break;
101        // 表示順を下へ
102        case 'down':
103            $this->doDown($objFormParam);
104            break;
105        // FIXME r19909 によってテンプレートが削除されている
106        case 'moveByDnD':
107            // DnDしたカテゴリと移動先のセットを分解する
108            $keys = explode('-', $_POST['keySet']);
109            if ($keys[0] && $keys[1]) {
110                $objQuery = new SC_Query_Ex();
111                $objQuery->begin();
112
113                // 移動したデータのrank、level、parent_category_idを取得
114                $rank   = $objQuery->get('rank', 'dtb_category', "category_id = ?", array($keys[0]));
115                $level  = $objQuery->get('level', 'dtb_category', "category_id = ?", array($keys[0]));
116                $parent = $objQuery->get('parent_category_id', 'dtb_category', "category_id = ?", array($keys[0]));
117
118                // 同一level内のrank配列を作成
119                $objQuery->setOption('ORDER BY rank DESC');
120                if ($level == 1) {
121                    // 第1階層の時
122                    $arrRet = $objQuery->select('rank', 'dtb_category', "level = ?", array($level));
123                } else {
124                    // 第2階層以下の時
125                    $arrRet = $objQuery->select('rank', 'dtb_category', "level = ? AND parent_category_id = ?", array($level, $parent));
126                }
127                for ($i = 0; $i < sizeof($arrRet); $i++) {
128                    $rankAry[$i + 1] = $arrRet[$i]['rank'];
129                }
130
131                // 移動したデータのグループ内データ数
132                $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $keys[0]);
133                if ($rankAry[$keys[1]] > $rank) {
134                    // データが今の位置より上がった時
135                    $up_count = $rankAry[$keys[1]] - $rank;
136                    $decAry   = $objQuery->select('category_id', 'dtb_category', "level = ? AND rank > ? AND rank <= ?", array($level, $rank, $rankAry[$keys[1]]));
137                    foreach ($decAry as $value) {
138                        // 上のグループから減算
139                        $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $value['category_id'], $my_count);
140                    }
141                    // 自分のグループに加算
142                    $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $keys[0], $up_count);
143                } else if ($rankAry[$keys[1]] < $rank) {
144                    // データが今の位置より下がった時
145                    $down_count = 0;
146                    $incAry     = $objQuery->select('category_id', 'dtb_category', "level = ? AND rank < ? AND rank >= ?", array($level, $rank, $rankAry[$keys[1]]));
147                    foreach ($incAry as $value) {
148                        // 下のグループに加算
149                        $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $value['category_id'], $my_count);
150                        // 合計減算値
151                        $down_count += $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $value['category_id']);
152                    }
153                    // 自分のグループから減算
154                    $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $keys[0], $down_count);
155                }
156                $objQuery->commit();
157            }
158            break;
159        // カテゴリツリークリック時
160        case 'tree':
161            break;
162         // CSVダウンロード
163        case 'csv':
164            // CSVを送信する
165            $objCSV = new SC_Helper_CSV_Ex();
166            $objCSV->sfDownloadCsv('5', '', array(), "", true);
167            exit;
168            break;
169        default:
170            break;
171        }
172
173        $parent_category_id = $objFormParam->getValue('parent_category_id');
174        // 空の場合は親カテゴリを0にする
175        if (empty($parent_category_id)) {
176            $parent_category_id = 0;
177        }
178        // 親カテゴリIDの保持
179        $this->arrForm['parent_category_id'] = $parent_category_id;
180        // カテゴリ一覧を取得
181        $this->arrList = $this->findCategoiesByParentCategoryId($parent_category_id);
182        // カテゴリツリーを取得
183        $this->arrTree = $objDb->sfGetCatTree($parent_category_id);
184        // ぱんくずの生成
185        $arrBread = array();
186        $objDb->findTree($this->arrTree, $parent_category_id, $arrBread);
187        $this->tpl_bread_crumbs = SC_Utils_Ex::jsonEncode($arrBread);
188
189        // フックポイントを実行.
190        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance();
191        $objPlugin->doAction('lc_page_admin_products_category_action_end', array($this));
192    }
193
194    /**
195     * カテゴリの削除を実行する.
196     *
197     * 下記の場合は削除を実施せず、エラーメッセージを表示する.
198     *
199     * - 削除対象のカテゴリに、子カテゴリが1つ以上ある場合
200     * - 削除対象のカテゴリを、登録商品が使用している場合
201     *
202     * カテゴリの削除は、物理削除で行う.
203     *
204     * @param SC_FormParam $objFormParam
205     * @param SC_Helper_Db $objDb
206     * @return void
207     */
208    function doDelete(&$objFormParam, &$objDb) {
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        $category_id = $objFormParam->getValue('category_id');
240
241        $objQuery =& SC_Query_Ex::getSingletonInstance();
242
243        // 編集対象のカテゴリ名をDBより取得する
244        $where = 'category_id = ?';
245        $arrRes = $objQuery->getRow('*', 'dtb_category', $where, array($category_id));
246
247        $objFormParam->setParam($arrRes);
248
249        $this->arrForm = $objFormParam->getHashArray();
250    }
251
252    /**
253     * カテゴリの登録・編集を実行する.
254     *
255     * 下記の場合は, 登録・編集を実行せず、エラーメッセージを表示する
256     *
257     * - カテゴリ名がすでに使用されている場合
258     * - 階層登録数の上限を超える場合 (登録時のみ評価)
259     * - カテゴリ名がすでに使用されている場合 (登録時のみ評価)
260     *
261     * @param SC_FormParam $objFormParam
262     * @return void
263     */
264    function doEdit(&$objFormParam) {
265        $category_id = $objFormParam->getValue('category_id');
266
267        // 追加か
268        $add = strlen($category_id) === 0;
269
270        // エラーチェック
271        $this->arrErr = $this->checkError($objFormParam, $add);
272
273        // エラーがない場合、追加・更新処理
274        if (empty($this->arrErr)) {
275            $arrCategory = $objFormParam->getDbArray();
276
277            // 追加
278            if ($add) {
279                $this->registerCategory($arrCategory);
280            }
281            // 更新
282            else {
283                unset($arrCategory['category_id']);
284                $this->updateCategory($category_id, $arrCategory);
285            }
286        }
287        // エラーがある場合、入力値の再表示
288        else {
289            $this->arrForm = $objFormParam->getHashArray();
290        }
291    }
292
293    /**
294     * エラーチェック
295     *
296     * @param SC_FormParam $objFormParam
297     * @param boolean $add 追加か
298     * @return void
299     */
300    function checkError(&$objFormParam, $add) {
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        $category_id = $objFormParam->getValue('category_id');
357
358        $objQuery =& SC_Query_Ex::getSingletonInstance();
359        $objQuery->begin();
360        $up_id = $this->lfGetUpRankID($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
361        if ($up_id != '') {
362            // 上のグループのrankから減算する数
363            $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
364                // 自分のグループのrankに加算する数
365                $up_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id);
366                if ($my_count > 0 && $up_count > 0) {
367                    // 自分のグループに加算
368                    $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id, $up_count);
369                    // 上のグループから減算
370                    $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id, $my_count);
371                }
372        }
373        $objQuery->commit();
374    }
375
376    /**
377     * カテゴリの表示順序を下へ移動する.
378     *
379     * @param SC_FormParam $objFormParam
380     * @return void
381     */
382    function doDown(&$objFormParam) {
383        $category_id = $objFormParam->getValue('category_id');
384
385        $objQuery =& SC_Query_Ex::getSingletonInstance();
386        $objQuery->begin();
387        $down_id = $this->lfGetDownRankID($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
388        if ($down_id != '') {
389            // 下のグループのrankに加算する数
390            $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
391            // 自分のグループのrankから減算する数
392            $down_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $down_id);
393            if ($my_count > 0 && $down_count > 0) {
394                // 自分のグループから減算
395                $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $down_id, $my_count);
396                // 下のグループに加算
397                $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id, $down_count);
398            }
399        }
400        $objQuery->commit();
401    }
402
403    /**
404     * パラメーターの初期化を行う
405     *
406     * @param SC_FormParam $objFormParam
407     * @return void
408     */
409    function initParam(&$objFormParam) {
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    function findCategoiesByParentCategoryId($parent_category_id) {
426        if (!$parent_category_id) {
427            $parent_category_id = 0;
428        }
429        $objQuery =& SC_Query_Ex::getSingletonInstance();
430        $col   = 'category_id, category_name, level, rank';
431        $where = 'del_flg = 0 AND parent_category_id = ?';
432        $objQuery->setOption('ORDER BY rank DESC');
433        return $objQuery->select($col, 'dtb_category', $where, array($parent_category_id));
434    }
435
436    /**
437     * カテゴリを更新する
438     *
439     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
440     * @return void
441     */
442    function updateCategory($category_id, $arrCategory) {
443        $objQuery =& SC_Query_Ex::getSingletonInstance();
444
445        $arrCategory['update_date']   = 'CURRENT_TIMESTAMP';
446
447        $objQuery->begin();
448        $where = 'category_id = ?';
449        $objQuery->update('dtb_category', $arrCategory, $where, array($category_id));
450        $objQuery->commit();
451    }
452
453    /**
454     * カテゴリを登録する
455     *
456     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
457     * @return void
458     */
459    function registerCategory($arrCategory) {
460        $objQuery =& SC_Query_Ex::getSingletonInstance();
461
462        $parent_category_id = $arrCategory['parent_category_id'];
463
464        $objQuery->begin();
465
466        $rank = null;
467        if ($parent_category_id == 0) {
468            // ROOT階層で最大のランクを取得する。
469            $where = 'parent_category_id = ?';
470            $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1;
471        } else {
472            // 親のランクを自分のランクとする。
473            $where = 'category_id = ?';
474            $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id));
475            // 追加レコードのランク以上のレコードを一つあげる。
476            $sqlup = 'UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?';
477            $objQuery->exec($sqlup, array($rank));
478        }
479
480        $where = 'category_id = ?';
481        // 自分のレベルを取得する(親のレベル + 1)
482        $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1;
483
484        $arrCategory['create_date'] = 'CURRENT_TIMESTAMP';
485        $arrCategory['update_date'] = 'CURRENT_TIMESTAMP';
486        $arrCategory['creator_id']  = $_SESSION['member_id'];
487        $arrCategory['rank']        = $rank;
488        $arrCategory['level']       = $level;
489        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
490
491        $objQuery->insert('dtb_category', $arrCategory);
492
493        $objQuery->commit();    // トランザクションの終了
494    }
495
496    /**
497     * カテゴリの階層が上限を超えているかを判定する
498     *
499     * @param integer 親カテゴリID
500     * @param 超えている場合 true
501     */
502    function isOverLevel($parent_category_id) {
503        $objQuery =& SC_Query_Ex::getSingletonInstance();
504        $level = $objQuery->get('level', 'dtb_category', "category_id = ?", array($parent_category_id));
505        return $level >= LEVEL_MAX;
506    }
507
508    /**
509     * デストラクタ.
510     *
511     * @return void
512     */
513    function destroy() {
514        parent::destroy();
515    }
516
517    // 並びが1つ下のIDを取得する。
518    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
519        // 親IDを取得する。
520        $col = "$pid_name";
521        $where = "$id_name = ?";
522        $pid = $objQuery->get($col, $table, $where, $id);
523        // すべての子を取得する。
524        $col = "$id_name";
525        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
526        $arrRet = $objQuery->select($col, $table, $where, array($pid));
527        $max = count($arrRet);
528        $down_id = '';
529        for ($cnt = 0; $cnt < $max; $cnt++) {
530            if ($arrRet[$cnt][$id_name] == $id) {
531                $down_id = $arrRet[($cnt + 1)][$id_name];
532                break;
533            }
534        }
535        return $down_id;
536    }
537
538    // 並びが1つ上のIDを取得する。
539    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
540        // 親IDを取得する。
541        $col = "$pid_name";
542        $where = "$id_name = ?";
543        $pid = $objQuery->get($col, $table, $where, $id);
544        // すべての子を取得する。
545        $col = "$id_name";
546        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
547        $arrRet = $objQuery->select($col, $table, $where, array($pid));
548        $max = count($arrRet);
549        $up_id = '';
550        for ($cnt = 0; $cnt < $max; $cnt++) {
551            if ($arrRet[$cnt][$id_name] == $id) {
552                $up_id = $arrRet[($cnt - 1)][$id_name];
553                break;
554            }
555        }
556        return $up_id;
557    }
558
559    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
560        $objDb = new SC_Helper_DB_Ex();
561        // 子ID一覧を取得
562        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
563        return count($arrRet);
564    }
565
566    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
567        $objDb = new SC_Helper_DB_Ex();
568        // 子ID一覧を取得
569        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
570        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
571        $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
572        $sql.= 'AND del_flg = 0';
573        $ret = $objQuery->exec($sql);
574        return $ret;
575    }
576
577    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
578        $objDb = new SC_Helper_DB_Ex();
579        // 子ID一覧を取得
580        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
581        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
582        $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
583        $sql.= 'AND del_flg = 0';
584        $ret = $objQuery->exec($sql);
585        return $ret;
586    }
587}
Note: See TracBrowser for help on using the repository browser.