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

Revision 21689, 23.1 KB checked in by h_yoshimoto, 12 years ago (diff)

#1692 インスタンスを呼び出す処理を統一

  • 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        // フックポイント.
73        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
74        $objPlugin->doAction('lc_page_admin_products_category_action_start', array($this));
75
76        $objDb      = new SC_Helper_DB_Ex();
77        $objFormParam = new SC_FormParam_Ex();
78
79        // 入力パラメーター初期化
80        $this->initParam($objFormParam);
81        $objFormParam->setParam($_POST);
82        $objFormParam->convParam();
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
167                // フックポイント.
168                $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
169                $objPlugin->doAction('lc_page_admin_products_category_action_csv', array($this));
170
171                $objCSV->sfDownloadCsv('5', '', array(), '', true);
172                exit;
173                break;
174            default:
175                break;
176        }
177
178        $parent_category_id = $objFormParam->getValue('parent_category_id');
179        // 空の場合は親カテゴリを0にする
180        if (empty($parent_category_id)) {
181            $parent_category_id = 0;
182        }
183        // 親カテゴリIDの保持
184        $this->arrForm['parent_category_id'] = $parent_category_id;
185        // カテゴリ一覧を取得
186        $this->arrList = $this->findCategoiesByParentCategoryId($parent_category_id);
187        // カテゴリツリーを取得
188        $this->arrTree = $objDb->sfGetCatTree($parent_category_id);
189        // ぱんくずの生成
190        $arrBread = array();
191        $objDb->findTree($this->arrTree, $parent_category_id, $arrBread);
192        $this->tpl_bread_crumbs = SC_Utils_Ex::jsonEncode($arrBread);
193
194        // フックポイント.
195        $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance($this->plugin_activate_flg);
196        $objPlugin->doAction('lc_page_admin_products_category_action_end', array($this));
197    }
198
199    /**
200     * カテゴリの削除を実行する.
201     *
202     * 下記の場合は削除を実施せず、エラーメッセージを表示する.
203     *
204     * - 削除対象のカテゴリに、子カテゴリが1つ以上ある場合
205     * - 削除対象のカテゴリを、登録商品が使用している場合
206     *
207     * カテゴリの削除は、物理削除で行う.
208     *
209     * @param SC_FormParam $objFormParam
210     * @param SC_Helper_Db $objDb
211     * @return void
212     */
213    function doDelete(&$objFormParam, &$objDb) {
214        $category_id = $objFormParam->getValue('category_id');
215        $objQuery =& SC_Query_Ex::getSingletonInstance();
216
217        // 子カテゴリのチェック
218        $where = 'parent_category_id = ? AND del_flg = 0';
219        $exists = $objQuery->exists('dtb_category', $where, array($category_id));
220        if ($exists) {
221            $this->arrErr['category_name'] = '※ 子カテゴリが存在するため削除できません。<br/>';
222            return;
223        }
224        // 登録商品のチェック
225        $table = 'dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id';
226        $where = 'T1.category_id = ? AND T2.del_flg = 0';
227        $exists = $objQuery->exists($table, $where, array($category_id));
228        if ($exists) {
229            $this->arrErr['category_name'] = '※ カテゴリ内に商品が存在するため削除できません。<br/>';
230            return;
231        }
232
233        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
234        $objDb->sfDeleteRankRecord('dtb_category', 'category_id', $category_id, '', true);
235    }
236
237    /**
238     * 編集対象のカテゴリ名を, 入力ボックスへ表示する.
239     *
240     * @param SC_FormParam $objFormParam
241     * @return void
242     */
243    function doPreEdit(&$objFormParam) {
244        $category_id = $objFormParam->getValue('category_id');
245
246        $objQuery =& SC_Query_Ex::getSingletonInstance();
247
248        // 編集対象のカテゴリ名をDBより取得する
249        $where = 'category_id = ?';
250        $arrRes = $objQuery->getRow('*', 'dtb_category', $where, array($category_id));
251
252        $objFormParam->setParam($arrRes);
253
254        $this->arrForm = $objFormParam->getHashArray();
255    }
256
257    /**
258     * カテゴリの登録・編集を実行する.
259     *
260     * 下記の場合は, 登録・編集を実行せず、エラーメッセージを表示する
261     *
262     * - カテゴリ名がすでに使用されている場合
263     * - 階層登録数の上限を超える場合 (登録時のみ評価)
264     * - カテゴリ名がすでに使用されている場合 (登録時のみ評価)
265     *
266     * @param SC_FormParam $objFormParam
267     * @return void
268     */
269    function doEdit(&$objFormParam) {
270        $category_id = $objFormParam->getValue('category_id');
271
272        // 追加か
273        $add = strlen($category_id) === 0;
274
275        // エラーチェック
276        $this->arrErr = $this->checkError($objFormParam, $add);
277
278        // エラーがない場合、追加・更新処理
279        if (empty($this->arrErr)) {
280            $arrCategory = $objFormParam->getDbArray();
281
282            // 追加
283            if ($add) {
284                $this->registerCategory($arrCategory);
285            }
286            // 更新
287            else {
288                unset($arrCategory['category_id']);
289                $this->updateCategory($category_id, $arrCategory);
290            }
291        }
292        // エラーがある場合、入力値の再表示
293        else {
294            $this->arrForm = $objFormParam->getHashArray();
295        }
296    }
297
298    /**
299     * エラーチェック
300     *
301     * @param SC_FormParam $objFormParam
302     * @param boolean $add 追加か
303     * @return void
304     */
305    function checkError(&$objFormParam, $add) {
306        $objQuery =& SC_Query_Ex::getSingletonInstance();
307
308        // 入力項目チェック
309        $arrErr = $objFormParam->checkError();
310        if (!empty($arrErr)) {
311            return $arrErr;
312        }
313
314        $category_id = $objFormParam->getValue('category_id');
315        $parent_category_id = $objFormParam->getValue('parent_category_id');
316        $category_name = $objFormParam->getValue('category_name');
317
318        // 追加の場合に固有のチェック
319        if ($add) {
320            // 登録数上限チェック
321            $where = 'del_flg = 0';
322            $count = $objQuery->count('dtb_category', $where);
323            if ($count >= CATEGORY_MAX) {
324                $arrErr['category_name'] = '※ カテゴリの登録最大数を超えました。<br/>';
325                return $arrErr;
326            }
327
328            // 階層上限チェック
329            if ($this->isOverLevel($parent_category_id)) {
330                $arrErr['category_name'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。<br/>';
331                return $arrErr;
332            }
333        }
334
335        // 重複チェック
336        $arrWhereVal = array();
337        $where = 'del_flg = 0 AND parent_category_id = ? AND category_name = ?';
338        $arrWhereVal[] = $parent_category_id;
339        $arrWhereVal[] = $category_name;
340        // 更新の場合、抽出対象から自己を除外する
341        if (!$add) {
342            $where .= ' AND category_id <> ?';
343            $arrWhereVal[] = $category_id;
344        }
345        $exists = $objQuery->exists('dtb_category', $where, $arrWhereVal);
346        if ($exists) {
347            $arrErr['category_name'] = '※ 既に同じ内容の登録が存在します。<br/>';
348            return $arrErr;
349        }
350
351        return $arrErr;
352    }
353
354    /**
355     * カテゴリの表示順序を上へ移動する.
356     *
357     * @param SC_FormParam $objFormParam
358     * @return void
359     */
360    function doUp(&$objFormParam) {
361        $category_id = $objFormParam->getValue('category_id');
362
363        $objQuery =& SC_Query_Ex::getSingletonInstance();
364        $objQuery->begin();
365        $up_id = $this->lfGetUpRankID($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
366        if ($up_id != '') {
367            // 上のグループのrankから減算する数
368            $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id);
369                // 自分のグループのrankに加算する数
370                $up_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id);
371                if ($my_count > 0 && $up_count > 0) {
372                    // 自分のグループに加算
373                    $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id, $up_count);
374                    // 上のグループから減算
375                    $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id, $my_count);
376                }
377        }
378        $objQuery->commit();
379    }
380
381    /**
382     * カテゴリの表示順序を下へ移動する.
383     *
384     * @param SC_FormParam $objFormParam
385     * @return void
386     */
387    function doDown(&$objFormParam) {
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        $objFormParam->addParam('親カテゴリID', 'parent_category_id', null, null, array());
416        $objFormParam->addParam('カテゴリID', 'category_id', null, null, array());
417        $objFormParam->addParam('カテゴリ名', 'category_name', STEXT_LEN, 'KVa', array('EXIST_CHECK', 'SPTAB_CHECK', 'MAX_LENGTH_CHECK'));
418    }
419
420    /**
421     * 親カテゴリIDでカテゴリを検索する.
422     *
423     * - 表示順の降順でソートする
424     * - 有効なカテゴリを返す(del_flag = 0)
425     *
426     * @param SC_Query $objQuery
427     * @param int $parent_category_id 親カテゴリID
428     * @return array カテゴリの配列
429     */
430    function findCategoiesByParentCategoryId($parent_category_id) {
431        if (!$parent_category_id) {
432            $parent_category_id = 0;
433        }
434        $objQuery =& SC_Query_Ex::getSingletonInstance();
435        $col   = 'category_id, category_name, level, rank';
436        $where = 'del_flg = 0 AND parent_category_id = ?';
437        $objQuery->setOption('ORDER BY rank DESC');
438        return $objQuery->select($col, 'dtb_category', $where, array($parent_category_id));
439    }
440
441    /**
442     * カテゴリを更新する
443     *
444     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
445     * @return void
446     */
447    function updateCategory($category_id, $arrCategory) {
448        $objQuery =& SC_Query_Ex::getSingletonInstance();
449
450        $arrCategory['update_date']   = 'CURRENT_TIMESTAMP';
451
452        $objQuery->begin();
453        $where = 'category_id = ?';
454        $objQuery->update('dtb_category', $arrCategory, $where, array($category_id));
455        $objQuery->commit();
456    }
457
458    /**
459     * カテゴリを登録する
460     *
461     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
462     * @return void
463     */
464    function registerCategory($arrCategory) {
465        $objQuery =& SC_Query_Ex::getSingletonInstance();
466
467        $parent_category_id = $arrCategory['parent_category_id'];
468
469        $objQuery->begin();
470
471        $rank = null;
472        if ($parent_category_id == 0) {
473            // ROOT階層で最大のランクを取得する。
474            $where = 'parent_category_id = ?';
475            $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1;
476        } else {
477            // 親のランクを自分のランクとする。
478            $where = 'category_id = ?';
479            $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id));
480            // 追加レコードのランク以上のレコードを一つあげる。
481            $sqlup = 'UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?';
482            $objQuery->exec($sqlup, array($rank));
483        }
484
485        $where = 'category_id = ?';
486        // 自分のレベルを取得する(親のレベル + 1)
487        $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1;
488
489        $arrCategory['create_date'] = 'CURRENT_TIMESTAMP';
490        $arrCategory['update_date'] = 'CURRENT_TIMESTAMP';
491        $arrCategory['creator_id']  = $_SESSION['member_id'];
492        $arrCategory['rank']        = $rank;
493        $arrCategory['level']       = $level;
494        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
495
496        $objQuery->insert('dtb_category', $arrCategory);
497
498        $objQuery->commit();    // トランザクションの終了
499    }
500
501    /**
502     * カテゴリの階層が上限を超えているかを判定する
503     *
504     * @param integer 親カテゴリID
505     * @param 超えている場合 true
506     */
507    function isOverLevel($parent_category_id) {
508        $objQuery =& SC_Query_Ex::getSingletonInstance();
509        $level = $objQuery->get('level', 'dtb_category', 'category_id = ?', array($parent_category_id));
510        return $level >= LEVEL_MAX;
511    }
512
513    /**
514     * デストラクタ.
515     *
516     * @return void
517     */
518    function destroy() {
519        parent::destroy();
520    }
521
522    // 並びが1つ下のIDを取得する。
523    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
524        // 親IDを取得する。
525        $col = "$pid_name";
526        $where = "$id_name = ?";
527        $pid = $objQuery->get($col, $table, $where, $id);
528        // すべての子を取得する。
529        $col = "$id_name";
530        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
531        $arrRet = $objQuery->select($col, $table, $where, array($pid));
532        $max = count($arrRet);
533        $down_id = '';
534        for ($cnt = 0; $cnt < $max; $cnt++) {
535            if ($arrRet[$cnt][$id_name] == $id) {
536                $down_id = $arrRet[($cnt + 1)][$id_name];
537                break;
538            }
539        }
540        return $down_id;
541    }
542
543    // 並びが1つ上のIDを取得する。
544    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
545        // 親IDを取得する。
546        $col = "$pid_name";
547        $where = "$id_name = ?";
548        $pid = $objQuery->get($col, $table, $where, $id);
549        // すべての子を取得する。
550        $col = "$id_name";
551        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
552        $arrRet = $objQuery->select($col, $table, $where, array($pid));
553        $max = count($arrRet);
554        $up_id = '';
555        for ($cnt = 0; $cnt < $max; $cnt++) {
556            if ($arrRet[$cnt][$id_name] == $id) {
557                $up_id = $arrRet[($cnt - 1)][$id_name];
558                break;
559            }
560        }
561        return $up_id;
562    }
563
564    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
565        $objDb = new SC_Helper_DB_Ex();
566        // 子ID一覧を取得
567        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
568        return count($arrRet);
569    }
570
571    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
572        $objDb = new SC_Helper_DB_Ex();
573        // 子ID一覧を取得
574        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
575        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
576        $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
577        $sql.= 'AND del_flg = 0';
578        $ret = $objQuery->exec($sql);
579        return $ret;
580    }
581
582    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
583        $objDb = new SC_Helper_DB_Ex();
584        // 子ID一覧を取得
585        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
586        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
587        $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
588        $sql.= 'AND del_flg = 0';
589        $ret = $objQuery->exec($sql);
590        return $ret;
591    }
592}
Note: See TracBrowser for help on using the repository browser.