source: branches/version-2_13_3/data/class/helper/SC_Helper_Category.php @ 23649

Revision 23649, 14.0 KB checked in by kim, 10 years ago (diff)

#2554 r23439 をリバート

検証不十分、既存関数の削除が行われているため、2.13.3での実装は見送り。
一部コメントの修正などは採用

Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2014 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/**
25 * カテゴリーを管理するヘルパークラス.
26 *
27 * @package Helper
28 * @author pineray
29 * @version $Id:$
30 */
31class SC_Helper_Category
32{
33    private $count_check;
34
35    /**
36     * コンストラクター
37     *
38     * @param boolean $count_check 登録商品数をチェックする場合はtrue
39     */
40    public function __construct($count_check = FALSE)
41    {
42        $this->count_check = $count_check;
43    }
44
45    /**
46     * カテゴリーの情報を取得.
47     *
48     * @param  integer $category_id カテゴリーID
49     * @return array
50     */
51    public function get($category_id)
52    {
53        $objQuery =& SC_Query_Ex::getSingletonInstance();
54        $col = 'dtb_category.*, dtb_category_total_count.product_count';
55        $from = 'dtb_category left join dtb_category_total_count ON dtb_category.category_id = dtb_category_total_count.category_id';
56        $where = 'dtb_category.category_id = ? AND del_flg = 0';
57        // 登録商品数のチェック
58        if ($this->count_check) {
59            $where .= ' AND product_count > 0';
60        }
61        $arrRet = $objQuery->getRow($col, $from, $where, array($category_id));
62
63        return $arrRet;
64    }
65
66    /**
67     * カテゴリー一覧の取得.
68     *
69     * @param bool $cid_to_key 配列のキーをカテゴリーIDにする場合はtrue
70     * @param bool $reset スタティック変数をリセットする場合はtrue
71     * @return array   カテゴリー一覧の配列
72     */
73    public function getList($cid_to_key = FALSE, $reset = FALSE)
74    {
75        static $arrCategory = array(), $cidIsKey = array();
76
77        if ($reset) {
78            $arrCategory = array();
79            $cidIsKey = array();
80        }
81
82        if (!isset($arrCategory[$this->count_check])) {
83            $objQuery =& SC_Query_Ex::getSingletonInstance();
84            $col = 'dtb_category.*, dtb_category_total_count.product_count';
85            $from = 'dtb_category left join dtb_category_total_count ON dtb_category.category_id = dtb_category_total_count.category_id';
86            // 登録商品数のチェック
87            if ($this->count_check) {
88                $where = 'del_flg = 0 AND product_count > 0';
89            } else {
90                $where = 'del_flg = 0';
91            }
92            $objQuery->setOption('ORDER BY rank DESC');
93            $arrTmp = $objQuery->select($col, $from, $where);
94
95            $arrCategory[$this->count_check] = $arrTmp;
96        }
97
98        if ($cid_to_key) {
99            if (!isset($cidIsKey[$this->count_check])) {
100                // 配列のキーをカテゴリーIDに
101                $cidIsKey[$this->count_check] = SC_Utils_Ex::makeArrayIDToKey('category_id', $arrCategory[$this->count_check]);
102            }
103
104            return $cidIsKey[$this->count_check];
105        }
106
107        return $arrCategory[$this->count_check];
108    }
109
110    /**
111     * カテゴリーツリーの取得.
112     *
113     * @param bool $reset スタティック変数をリセットする場合はtrue
114     * @return array
115     */
116    public function getTree($reset = false)
117    {
118        static $arrTree = array();
119
120        if ($reset) {
121            $arrTree = array();
122        }
123
124        if (!isset($arrTree[$this->count_check])) {
125            $arrList = $this->getList(false, $reset);
126            $arrTree[$this->count_check] = SC_Utils_Ex::buildTree('category_id', 'parent_category_id', LEVEL_MAX, $arrList);
127        }
128
129        return $arrTree[$this->count_check];
130    }
131
132    /**
133     * 親カテゴリーIDの配列を取得.
134     *
135     * @param  integer $category_id 起点のカテゴリーID
136     * @param  boolean $id_only     IDだけの配列を返す場合はtrue
137     * @return array
138     */
139    public function getTreeTrail($category_id, $id_only = TRUE)
140    {
141        $arrCategory = $this->getList(TRUE);
142        $arrTrailID = SC_Utils_Ex::getTreeTrail($category_id, 'category_id', 'parent_category_id', $arrCategory, TRUE, 0, $id_only);
143
144        return $arrTrailID;
145    }
146
147    /**
148     * 指定カテゴリーの子孫カテゴリーを取得
149     *
150     * @param int $category_id カテゴリーID
151     * @return array
152     */
153    public function getTreeBranch($category_id)
154    {
155        $arrTree = $this->getTree();
156        $arrTrail = $this->getTreeTrail($category_id, true);
157
158        // 指定カテゴリーがルートの場合は、ツリーをそのまま返す.
159        if ($category_id == 0) {
160            return $arrTree;
161        } else {
162            // ルートから指定カテゴリーまでたどる.
163            foreach ($arrTrail as $parent_id) {
164                $nextTree = array();
165                foreach ($arrTree as $branch) {
166                    if ($branch['category_id'] == $parent_id && isset($branch['children'])) {
167                        $nextTree = $branch['children'];
168                    }
169                }
170                $arrTree = $nextTree;
171            }
172            return $arrTree;
173        }
174    }
175
176    /**
177     * カテゴリーの登録.
178     *
179     * @param array $data
180     * @return void
181     */
182    public function save($data)
183    {
184        $objQuery =& SC_Query_Ex::getSingletonInstance();
185
186        $category_id = $data['category_id'];
187        $query = array('update_date' => 'CURRENT_TIMESTAMP');
188        $objQuery->begin();
189
190        if ($category_id == '') {
191            // 新規登録
192            $parent_category_id = $data['parent_category_id'];
193            $rank = null;
194            if ($parent_category_id == 0) {
195                // ROOT階層で最大のランクを取得する。
196                $where = 'parent_category_id = ?';
197                $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1;
198            } else {
199                // 親のランクを自分のランクとする。
200                $where = 'category_id = ?';
201                $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id));
202                // 追加レコードのランク以上のレコードを一つあげる。
203                $where = 'rank >= ?';
204                $arrRawSql = array(
205                    'rank' => '(rank + 1)',
206                );
207                $objQuery->update('dtb_category', array(), $where, array($rank), $arrRawSql);
208            }
209
210            $where = 'category_id = ?';
211            // 自分のレベルを取得する(親のレベル + 1)
212            $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1;
213
214            $query['category_id'] = $objQuery->nextVal('dtb_category_category_id');
215            $query['category_name'] = $data['category_name'];
216            $query['parent_category_id'] = $data['parent_category_id'];
217            $query['create_date'] = 'CURRENT_TIMESTAMP';
218            $query['creator_id']  = $_SESSION['member_id'];
219            $query['rank']        = $rank;
220            $query['level']       = $level;
221
222            $objQuery->insert('dtb_category', $query);
223        } else {
224            // 既存編集
225            $query['parent_category_id'] = $data['parent_category_id'];
226            $query['category_name'] = $data['category_name'];
227            $where = 'category_id = ?';
228            $objQuery->update('dtb_category', $query, $where, array($category_id));
229        }
230
231        $objQuery->commit();
232    }
233
234    /**
235     * カテゴリーの削除
236     *
237     * @param int $category_id カテゴリーID
238     * @return void
239     */
240    public function delete($category_id)
241    {
242        $objDb = new SC_Helper_DB_Ex();
243        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。)
244        $objDb->sfDeleteRankRecord('dtb_category', 'category_id', $category_id, '', true);
245    }
246
247    /**
248     * カテゴリーの表示順をひとつ上げる.
249     *
250     * @param int $category_id カテゴリーID
251     * @return void
252     */
253    public function rankUp($category_id)
254    {
255        $objQuery =& SC_Query_Ex::getSingletonInstance();
256        $objQuery->begin();
257        $up_id = $this->getNeighborRankId('upper', $category_id);
258        if ($up_id != '') {
259            // 上のグループのrankから減算する数
260            $my_count = $this->countAllBranches($category_id);
261            // 自分のグループのrankに加算する数
262            $up_count = $this->countAllBranches($up_id);
263            if ($my_count > 0 && $up_count > 0) {
264                // 自分のグループに加算
265                $this->raiseBranchRank($objQuery, $category_id, $up_count);
266                // 上のグループから減算
267                $this->reduceBranchRank($objQuery, $up_id, $my_count);
268            }
269        }
270        $objQuery->commit();
271    }
272
273    /**
274     * カテゴリーの表示順をひとつ下げる.
275     *
276     * @param int $category_id カテゴリーID
277     * @return void
278     */
279    public function rankDown($category_id)
280    {
281        $objQuery =& SC_Query_Ex::getSingletonInstance();
282        $objQuery->begin();
283        $down_id = $this->getNeighborRankId('lower', $category_id);
284        if ($down_id != '') {
285            // 下のグループのrankに加算する数
286            $my_count = $this->countAllBranches($category_id);
287            // 自分のグループのrankから減算する数
288            $down_count = $this->countAllBranches($down_id);
289            if ($my_count > 0 && $down_count > 0) {
290                // 自分のグループから減算
291                $this->raiseBranchRank($objQuery, $down_id, $my_count);
292                // 下のグループに加算
293                $this->reduceBranchRank($objQuery, $category_id, $down_count);
294            }
295        }
296        $objQuery->commit();
297    }
298
299    /**
300     * 並びがとなりのIDを取得する。
301     *
302     * @param string $side 上 upper か下 down か
303     * @param int $category_id カテゴリーID
304     * @return int
305     */
306    private function getNeighborRankId($side, $category_id)
307    {
308        $arrCategory = $this->get($category_id);
309        $parent_id = $arrCategory['parent_category_id'];
310
311        if ($parent_id == 0) {
312            $arrBrother = $this->getTree();
313        } else {
314            $arrBrother = $this->getTreeBranch($parent_id);
315        }
316
317        // 全ての子を取得する。
318        $max = count($arrBrother);
319        $upper_id = '';
320        for ($cnt = 0; $cnt < $max; $cnt++) {
321            if ($arrBrother[$cnt]['category_id'] == $category_id) {
322                if ($side == 'upper') {
323                    $index = $cnt - 1;
324                } else {
325                    $index = $cnt + 1;
326                }
327                $upper_id = $arrBrother[$index]['category_id'];
328                break;
329            }
330        }
331
332        return $upper_id;
333    }
334
335    /**
336     * 指定カテゴリーを含めた子孫カテゴリーの数を取得する.
337     *
338     * @param int $category_id カテゴリーID
339     * @return int
340     */
341    private function countAllBranches($category_id)
342    {
343        $objDb = new SC_Helper_DB_Ex();
344        // 子ID一覧を取得
345        $arrRet = $objDb->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $category_id);
346
347        return count($arrRet);
348    }
349
350    /**
351     * 子孫カテゴリーの表示順を一括して上げる.
352     *
353     * @param SC_Query $objQuery
354     * @param int $category_id
355     * @param int $count
356     * @return array|bool
357     */
358    private function raiseBranchRank(SC_Query $objQuery, $category_id, $count)
359    {
360        $table = 'dtb_category';
361        $objDb = new SC_Helper_DB_Ex();
362        // 子ID一覧を取得
363        $arrRet = $objDb->sfGetChildrenArray($table, 'parent_category_id', 'category_id', $category_id);
364        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
365        $where = "category_id IN ($line) AND del_flg = 0";
366        $arrRawVal = array(
367            'rank' => "(rank + $count)",
368        );
369
370        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
371    }
372
373    /**
374     * 子孫カテゴリーの表示順を一括して下げる.
375     *
376     * @param SC_Query $objQuery
377     * @param int $category_id
378     * @param int $count
379     * @return array|bool
380     */
381    private function reduceBranchRank(SC_Query $objQuery, $category_id, $count)
382    {
383        $table = 'dtb_category';
384        $objDb = new SC_Helper_DB_Ex();
385        // 子ID一覧を取得
386        $arrRet = $objDb->sfGetChildrenArray($table, 'parent_category_id', 'category_id', $category_id);
387        $line = SC_Utils_Ex::sfGetCommaList($arrRet);
388        $where = "category_id IN ($line) AND del_flg = 0";
389        $arrRawVal = array(
390            'rank' => "(rank - $count)",
391        );
392
393        return $objQuery->update($table, array(), $where, array(), $arrRawVal);
394    }
395
396    /**
397     * 有効なカテゴリーIDかチェックする.
398     *
399     * @param int $category_id
400     * @param bool $include_deleted
401     * @return bool
402     */
403    public function isValidCategoryId($category_id, $include_deleted = false) {
404        if ($include_deleted) {
405            $where = '';
406        } else {
407            $where = 'del_flg = 0';
408        }
409        if (
410            SC_Utils_Ex::sfIsInt($category_id)
411            && !SC_Utils_Ex::sfIsZeroFilling($category_id)
412            && SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', array($category_id), $where)
413        ) {
414            return true;
415        }
416        return false;
417    }
418}
Note: See TracBrowser for help on using the repository browser.