source: branches/version-2_12-dev/data/class/pages/frontparts/bloc/LC_Page_FrontParts_Bloc_Category.php @ 22586

Revision 22586, 6.5 KB checked in by pineray, 11 years ago (diff)

#2166 カテゴリーツリーのデータをツリー状に取得

  • 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-2013 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/frontparts/bloc/LC_Page_FrontParts_Bloc_Ex.php';
26
27/**
28 * カテゴリ のページクラス.
29 *
30 * @package Page
31 * @author LOCKON CO.,LTD.
32 * @version $Id$
33 */
34class LC_Page_FrontParts_Bloc_Category extends LC_Page_FrontParts_Bloc_Ex
35{
36
37    // }}}
38    // {{{ functions
39
40    /**
41     * Page を初期化する.
42     *
43     * @return void
44     */
45    function init()
46    {
47        parent::init();
48    }
49
50    /**
51     * Page のプロセス.
52     *
53     * @return void
54     */
55    function process()
56    {
57        $this->action();
58        $this->sendResponse();
59    }
60
61    /**
62     * Page のアクション.
63     *
64     * @return void
65     */
66    function action()
67    {
68
69        // モバイル判定
70        switch (SC_Display_Ex::detectDevice()) {
71            case DEVICE_TYPE_MOBILE:
72                // メインカテゴリの取得
73                $this->arrCat = $this->lfGetMainCat(true);
74                break;
75            default:
76                // 選択中のカテゴリID
77                $this->tpl_category_id = $this->lfGetSelectedCategoryId($_GET);
78                // カテゴリツリーの取得
79                $this->arrTree = $this->lfGetCatTree($this->tpl_category_id, true);
80                break;
81        }
82
83
84    }
85
86    /**
87     * デストラクタ.
88     *
89     * @return void
90     */
91    function destroy()
92    {
93        parent::destroy();
94    }
95
96    /**
97     * 選択中のカテゴリIDを取得する.
98     *
99     * @param array $arrRequest リクエスト配列
100     * @return array $arrCategoryId 選択中のカテゴリID
101     */
102    function lfGetSelectedCategoryId($arrRequest)
103    {
104            // 商品ID取得
105        $product_id = '';
106        if (isset($arrRequest['product_id']) && $arrRequest['product_id'] != '' && is_numeric($arrRequest['product_id'])) {
107            $product_id = $arrRequest['product_id'];
108        }
109        // カテゴリID取得
110        $category_id = '';
111        if (isset($arrRequest['category_id']) && $arrRequest['category_id'] != '' && is_numeric($arrRequest['category_id'])) {
112            $category_id = $arrRequest['category_id'];
113        }
114        // 選択中のカテゴリIDを判定する
115        $objDb = new SC_Helper_DB_Ex();
116        $arrCategoryId = $objDb->sfGetCategoryId($product_id, $category_id);
117        if (empty($arrCategoryId)) {
118            $arrCategoryId = array(0);
119        }
120        return $arrCategoryId;
121    }
122
123    /**
124     * カテゴリツリーの取得.
125     *
126     * @param array $arrParentCategoryId 親カテゴリの配列
127     * @param boolean $count_check 登録商品数をチェックする場合はtrue
128     * @return array $arrRet カテゴリツリーの配列を返す
129     */
130    function lfGetCatTree($arrParentCategoryId, $count_check = false)
131    {
132        $objQuery =& SC_Query_Ex::getSingletonInstance();
133        $objDb = new SC_Helper_DB_Ex();
134
135        $col = '*';
136        $from = 'dtb_category left join dtb_category_total_count ON dtb_category.category_id = dtb_category_total_count.category_id';
137        // 登録商品数のチェック
138        if ($count_check) {
139            $where = 'del_flg = 0 AND product_count > 0';
140        } else {
141            $where = 'del_flg = 0';
142        }
143        $objQuery->setOption('ORDER BY rank DESC');
144        $arrRet = $objQuery->select($col, $from, $where);
145
146        $arrTree = SC_Utils_Ex::buildTree('category_id', 'parent_category_id', 10, $arrRet);
147
148        foreach ($arrParentCategoryId as $category_id) {
149            $arrParentID = $objDb->sfGetParents(
150                'dtb_category',
151                'parent_category_id',
152                'category_id',
153                $category_id
154            );
155            $arrBrothersID = SC_Utils_Ex::sfGetBrothersArray(
156                $arrRet,
157                'parent_category_id',
158                'category_id',
159                $arrParentID
160            );
161            $arrChildrenID = SC_Utils_Ex::sfGetUnderChildrenArray(
162                $arrRet,
163                'parent_category_id',
164                'category_id',
165                $category_id
166            );
167            $this->root_parent_id[] = $arrParentID[0];
168            $this->arrDispID = array_merge($arrBrothersID, $arrChildrenID);
169        }
170
171        return $arrTree;
172    }
173
174    /**
175     * メインカテゴリの取得.
176     *
177     * @param boolean $count_check 登録商品数をチェックする場合はtrue
178     * @return array $arrMainCat メインカテゴリの配列を返す
179     */
180    function lfGetMainCat($count_check = false)
181    {
182        $objQuery =& SC_Query_Ex::getSingletonInstance();
183        $col = '*';
184        $from = 'dtb_category left join dtb_category_total_count ON dtb_category.category_id = dtb_category_total_count.category_id';
185        // メインカテゴリとその直下のカテゴリを取得する。
186        $where = 'level <= 2 AND del_flg = 0';
187        // 登録商品数のチェック
188        if ($count_check) {
189            $where .= ' AND product_count > 0';
190        }
191        $objQuery->setOption('ORDER BY rank DESC');
192        $arrRet = $objQuery->select($col, $from, $where);
193        // メインカテゴリを抽出する。
194        $arrMainCat = array();
195        foreach ($arrRet as $cat) {
196            if ($cat['level'] != 1) {
197                continue;
198            }
199            // 子カテゴリを持つかどうかを調べる。
200            $arrChildrenID = SC_Utils_Ex::sfGetUnderChildrenArray(
201                $arrRet,
202                'parent_category_id',
203                'category_id',
204                $cat['category_id']
205            );
206            $cat['has_children'] = count($arrChildrenID) > 0;
207            $arrMainCat[] = $cat;
208        }
209        return $arrMainCat;
210    }
211}
Note: See TracBrowser for help on using the repository browser.