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

Revision 21750, 22.5 KB checked in by shutta, 12 years ago (diff)

#1579 SC_Query::getSingletonInstance()への置き換え

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