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

Revision 21402, 22.2 KB checked in by Seasoft, 12 years ago (diff)

2.12系へマイルストーン変更となったチケット分を差し戻し
r21376 #1582,#1526,#1449 作業を簡単にするため、本来対象外のチケット(#1526,#1449)も含めた
r21375 #1581
r21374 #797
r21373 #1583,#1526 作業を簡単にするため、本来対象外のチケット(#1526)も含めた
r21372 #1576
r21371 #1581

  • 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';
26require_once CLASS_EX_REALDIR . 'helper_extends/SC_Helper_CSV_Ex.php';
27
28/**
29 * カテゴリ管理 のページクラス.
30 *
31 * @package Page
32 * @author LOCKON CO.,LTD.
33 * @version $Id$
34 */
35class LC_Page_Admin_Products_Category extends LC_Page_Admin_Ex {
36
37    // {{{ properties
38
39    // }}}
40    // {{{ functions
41
42    /**
43     * Page を初期化する.
44     *
45     * @return void
46     */
47    function init() {
48        parent::init();
49        $this->tpl_maintitle = '商品管理';
50        $this->tpl_subtitle = 'カテゴリ登録';
51        $this->tpl_mainpage = 'products/category.tpl';
52        $this->tpl_mainno = 'products';
53        $this->tpl_subno  = 'category';
54        $this->tpl_onload = " fnSetFocus('category_name'); ";
55    }
56
57    /**
58     * Page のプロセス.
59     *
60     * @return void
61     */
62    function process() {
63        $this->action();
64        $this->sendResponse();
65    }
66
67    /**
68     * Page のアクション.
69     *
70     * @return void
71     */
72    function action() {
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 = new SC_Query_Ex();
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            $objCSV->sfDownloadCsv("5", "", array(), "", true);
164            exit;
165            break;
166        default:
167            break;
168        }
169
170        $parent_category_id = $objFormParam->getValue('parent_category_id');
171        // 空の場合は親カテゴリを0にする
172        if (empty($parent_category_id)) {
173            $parent_category_id = 0;
174        }
175        // 親カテゴリIDの保持
176        $this->arrForm['parent_category_id'] = $parent_category_id;
177        // カテゴリ一覧を取得
178        $this->arrList = $this->findCategoiesByParentCategoryId($parent_category_id);
179        // カテゴリツリーを取得
180        $this->arrTree = $objDb->sfGetCatTree($parent_category_id);
181        // ぱんくずの生成
182        $arrBread = array();
183        $objDb->findTree($this->arrTree, $parent_category_id, $arrBread);
184        $this->tpl_bread_crumbs = SC_Utils_Ex::jsonEncode($arrBread);
185    }
186
187    /**
188     * カテゴリの削除を実行する.
189     *
190     * 下記の場合は削除を実施せず、エラーメッセージを表示する.
191     *
192     * - 削除対象のカテゴリに、子カテゴリが1つ以上ある場合
193     * - 削除対象のカテゴリを、登録商品が使用している場合
194     *
195     * カテゴリの削除は、物理削除で行う.
196     *
197     * @param SC_FormParam $objFormParam
198     * @param SC_Helper_Db $objDb
199     * @return void
200     */
201    function doDelete(&$objFormParam, &$objDb) {
202        $category_id = $objFormParam->getValue('category_id');
203        $objQuery =& SC_Query_Ex::getSingletonInstance();
204
205        // 子カテゴリのチェック
206        $where = "parent_category_id = ? AND del_flg = 0";
207        $count = $objQuery->count("dtb_category", $where, array($category_id));
208        if ($count > 0) {
209             $this->arrErr['category_name'] = "※ 子カテゴリが存在するため削除できません。<br/>";
210             return;
211        }
212        // 登録商品のチェック
213        $table = "dtb_product_categories AS T1 LEFT JOIN dtb_products AS T2 ON T1.product_id = T2.product_id";
214        $where = "T1.category_id = ? AND T2.del_flg = 0";
215        $count = $objQuery->count($table, $where, array($category_id));
216        if ($count > 0) {
217            $this->arrErr['category_name'] = "※ カテゴリ内に商品が存在するため削除できません。<br/>";
218            return;
219        }
220
221        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
222        $objDb->sfDeleteRankRecord("dtb_category", "category_id", $category_id, "", true);
223    }
224
225    /**
226     * 編集対象のカテゴリ名を, 入力ボックスへ表示する.
227     *
228     * @param SC_FormParam $objFormParam
229     * @return void
230     */
231    function doPreEdit(&$objFormParam) {
232        $category_id = $objFormParam->getValue('category_id');
233
234        $objQuery =& SC_Query_Ex::getSingletonInstance();
235
236        // 編集対象のカテゴリ名をDBより取得する
237        $where = "category_id = ?";
238        $arrRes = $objQuery->getRow("*", "dtb_category", $where, array($category_id));
239
240        $objFormParam->setParam($arrRes);
241
242        $this->arrForm = $objFormParam->getHashArray();
243    }
244
245    /**
246     * カテゴリの登録・編集を実行する.
247     *
248     * 下記の場合は, 登録・編集を実行せず、エラーメッセージを表示する
249     *
250     * - カテゴリ名がすでに使用されている場合
251     * - 階層登録数の上限を超える場合 (登録時のみ評価)
252     * - カテゴリ名がすでに使用されている場合 (登録時のみ評価)
253     *
254     * @param SC_FormParam $objFormParam
255     * @return void
256     */
257    function doEdit(&$objFormParam) {
258        $category_id = $objFormParam->getValue('category_id');
259
260        // 追加か
261        $add = strlen($category_id) === 0;
262
263        // エラーチェック
264        $this->arrErr = $this->checkError($objFormParam, $add);
265
266        // エラーがない場合、追加・更新処理
267        if (empty($this->arrErr)) {
268            $arrCategory = $objFormParam->getDbArray();
269
270            // 追加
271            if ($add) {
272                $this->registerCategory($arrCategory);
273            }
274            // 更新
275            else {
276                unset($arrCategory['category_id']);
277                $this->updateCategory($category_id, $arrCategory);
278            }
279        }
280        // エラーがある場合、入力値の再表示
281        else {
282            $this->arrForm = $objFormParam->getHashArray();
283        }
284    }
285
286    /**
287     * エラーチェック
288     *
289     * @param SC_FormParam $objFormParam
290     * @param boolean $add 追加か
291     * @return void
292     */
293    function checkError(&$objFormParam, $add) {
294        $objQuery =& SC_Query_Ex::getSingletonInstance();
295
296        // 入力項目チェック
297        $arrErr = $objFormParam->checkError();
298        if (!empty($arrErr)) {
299            return $arrErr;
300        }
301
302        $category_id = $objFormParam->getValue('category_id');
303        $parent_category_id = $objFormParam->getValue('parent_category_id');
304        $category_name = $objFormParam->getValue('category_name');
305
306        // 追加の場合に固有のチェック
307        if ($add) {
308            // 登録数上限チェック
309            $where = "del_flg = 0";
310            $count = $objQuery->count("dtb_category", $where);
311            if ($count >= CATEGORY_MAX) {
312                $arrErr['category_name'] = "※ カテゴリの登録最大数を超えました。<br/>";
313                return $arrErr;
314            }
315
316            // 階層上限チェック
317            if ($this->isOverLevel($parent_category_id)) {
318                $arrErr['category_name'] = "※ " . LEVEL_MAX . "階層以上の登録はできません。<br/>";
319                return $arrErr;
320            }
321        }
322
323        // 重複チェック
324        $arrWhereVal = array();
325        $where = 'del_flg = 0 AND parent_category_id = ? AND category_name = ?';
326        $arrWhereVal[] = $parent_category_id;
327        $arrWhereVal[] = $category_name;
328        // 更新の場合、抽出対象から自己を除外する
329        if (!$add) {
330            $where .= ' AND category_id <> ?';
331            $arrWhereVal[] = $category_id;
332        }
333        $count = $objQuery->count('dtb_category', $where, $arrWhereVal);
334        if ($count > 0) {
335            $arrErr['category_name'] = "※ 既に同じ内容の登録が存在します。<br/>";
336            return $arrErr;
337        }
338
339        return $arrErr;
340    }
341
342    /**
343     * カテゴリの表示順序を上へ移動する.
344     *
345     * @param SC_FormParam $objFormParam
346     * @return void
347     */
348    function doUp(&$objFormParam) {
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        $category_id = $objFormParam->getValue('category_id');
377
378        $objQuery =& SC_Query_Ex::getSingletonInstance();
379        $objQuery->begin();
380        $down_id = $this->lfGetDownRankID($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
381        if ($down_id != "") {
382            // 下のグループのrankに加算する数
383            $my_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id);
384            // 自分のグループのrankから減算する数
385            $down_count = $this->lfCountChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id);
386            if ($my_count > 0 && $down_count > 0) {
387                // 自分のグループから減算
388                $this->lfUpRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $down_id, $my_count);
389                // 下のグループに加算
390                $this->lfDownRankChilds($objQuery, "dtb_category", "parent_category_id", "category_id", $category_id, $down_count);
391            }
392        }
393        $objQuery->commit();
394    }
395
396    /**
397     * パラメーターの初期化を行う
398     *
399     * @param SC_FormParam $objFormParam
400     * @return void
401     */
402    function initParam(&$objFormParam) {
403        $objFormParam->addParam("親カテゴリID", "parent_category_id", null, null, array());
404        $objFormParam->addParam("カテゴリID", "category_id", null, null, array());
405        $objFormParam->addParam("カテゴリ名", "category_name", STEXT_LEN, 'KVa', array("EXIST_CHECK", "SPTAB_CHECK", "MAX_LENGTH_CHECK"));
406    }
407
408    /**
409     * 親カテゴリIDでカテゴリを検索する.
410     *
411     * - 表示順の降順でソートする
412     * - 有効なカテゴリを返す(del_flag = 0)
413     *
414     * @param SC_Query $objQuery
415     * @param int $parent_category_id 親カテゴリID
416     * @return array カテゴリの配列
417     */
418    function findCategoiesByParentCategoryId($parent_category_id) {
419        if (!$parent_category_id) {
420            $parent_category_id = 0;
421        }
422        $objQuery =& SC_Query_Ex::getSingletonInstance();
423        $col   = "category_id, category_name, level, rank";
424        $where = "del_flg = 0 AND parent_category_id = ?";
425        $objQuery->setOption("ORDER BY rank DESC");
426        return $objQuery->select($col, "dtb_category", $where, array($parent_category_id));
427    }
428
429    /**
430     * カテゴリを更新する
431     *
432     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
433     * @return void
434     */
435    function updateCategory($category_id, $arrCategory) {
436        $objQuery =& SC_Query_Ex::getSingletonInstance();
437
438        $arrCategory['update_date']   = 'CURRENT_TIMESTAMP';
439
440        $objQuery->begin();
441        $where = "category_id = ?";
442        $objQuery->update("dtb_category", $arrCategory, $where, array($category_id));
443        $objQuery->commit();
444    }
445
446    /**
447     * カテゴリを登録する
448     *
449     * @param SC_FormParam $objFormParam SC_FormParam インスタンス
450     * @return void
451     */
452    function registerCategory($arrCategory) {
453        $objQuery =& SC_Query_Ex::getSingletonInstance();
454
455        $parent_category_id = $arrCategory['parent_category_id'];
456
457        $objQuery->begin();
458
459        $rank = null;
460        if ($parent_category_id == 0) {
461            // ROOT階層で最大のランクを取得する。
462            $where = "parent_category_id = ?";
463            $rank = $objQuery->max('rank', "dtb_category", $where, array($parent_category_id)) + 1;
464        } else {
465            // 親のランクを自分のランクとする。
466            $where = "category_id = ?";
467            $rank = $objQuery->get('rank', "dtb_category", $where, array($parent_category_id));
468            // 追加レコードのランク以上のレコードを一つあげる。
469            $sqlup = "UPDATE dtb_category SET rank = (rank + 1) WHERE rank >= ?";
470            $objQuery->exec($sqlup, array($rank));
471        }
472
473        $where = "category_id = ?";
474        // 自分のレベルを取得する(親のレベル + 1)
475        $level = $objQuery->get('level', "dtb_category", $where, array($parent_category_id)) + 1;
476
477        $arrCategory['create_date'] = 'CURRENT_TIMESTAMP';
478        $arrCategory['update_date'] = 'CURRENT_TIMESTAMP';
479        $arrCategory['creator_id']  = $_SESSION['member_id'];
480        $arrCategory['rank']        = $rank;
481        $arrCategory['level']       = $level;
482        $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id');
483
484        $objQuery->insert("dtb_category", $arrCategory);
485
486        $objQuery->commit();    // トランザクションの終了
487    }
488
489    /**
490     * カテゴリの階層が上限を超えているかを判定する
491     *
492     * @param integer 親カテゴリID
493     * @param 超えている場合 true
494     */
495    function isOverLevel($parent_category_id) {
496        $objQuery =& SC_Query_Ex::getSingletonInstance();
497        $level = $objQuery->get('level', "dtb_category", "category_id = ?", array($parent_category_id));
498        return $level >= LEVEL_MAX;
499    }
500
501    /**
502     * デストラクタ.
503     *
504     * @return void
505     */
506    function destroy() {
507        parent::destroy();
508    }
509
510    // 並びが1つ下のIDを取得する。
511    function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) {
512        // 親IDを取得する。
513        $col = "$pid_name";
514        $where = "$id_name = ?";
515        $pid = $objQuery->get($col, $table, $where, $id);
516        // すべての子を取得する。
517        $col = "$id_name";
518        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
519        $arrRet = $objQuery->select($col, $table, $where, array($pid));
520        $max = count($arrRet);
521        $down_id = "";
522        for($cnt = 0; $cnt < $max; $cnt++) {
523            if($arrRet[$cnt][$id_name] == $id) {
524                $down_id = $arrRet[($cnt + 1)][$id_name];
525                break;
526            }
527        }
528        return $down_id;
529    }
530
531    // 並びが1つ上のIDを取得する。
532    function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) {
533        // 親IDを取得する。
534        $col = "$pid_name";
535        $where = "$id_name = ?";
536        $pid = $objQuery->get($col, $table, $where, $id);
537        // すべての子を取得する。
538        $col = "$id_name";
539        $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC";
540        $arrRet = $objQuery->select($col, $table, $where, array($pid));
541        $max = count($arrRet);
542        $up_id = "";
543        for($cnt = 0; $cnt < $max; $cnt++) {
544            if($arrRet[$cnt][$id_name] == $id) {
545                $up_id = $arrRet[($cnt - 1)][$id_name];
546                break;
547            }
548        }
549        return $up_id;
550    }
551
552    function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) {
553        $objDb = new SC_Helper_DB_Ex();
554        // 子ID一覧を取得
555        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
556        return count($arrRet);
557    }
558
559    function lfUpRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
560        $objDb = new SC_Helper_DB_Ex();
561        // 子ID一覧を取得
562        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
563        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
564        $sql = "UPDATE $table SET rank = (rank + $count) WHERE $id_name IN ($line) ";
565        $sql.= "AND del_flg = 0";
566        $ret = $objQuery->exec($sql);
567        return $ret;
568    }
569
570    function lfDownRankChilds($objQuery, $table, $pid_name, $id_name, $id, $count) {
571        $objDb = new SC_Helper_DB_Ex();
572        // 子ID一覧を取得
573        $arrRet = $objDb->sfGetChildrenArray($table, $pid_name, $id_name, $id);
574        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
575        $sql = "UPDATE $table SET rank = (rank - $count) WHERE $id_name IN ($line) ";
576        $sql.= "AND del_flg = 0";
577        $ret = $objQuery->exec($sql);
578        return $ret;
579    }
580}
Note: See TracBrowser for help on using the repository browser.