[22588] | 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 | /** |
---|
| 25 | * カテゴリーを管理するヘルパークラス. |
---|
| 26 | * |
---|
| 27 | * @package Helper |
---|
| 28 | * @author pineray |
---|
| 29 | * @version $Id:$ |
---|
| 30 | */ |
---|
| 31 | class SC_Helper_Category |
---|
| 32 | { |
---|
[22589] | 33 | private $count_check; |
---|
| 34 | |
---|
[22588] | 35 | /** |
---|
[22589] | 36 | * コンストラクター |
---|
[22856] | 37 | * |
---|
[22589] | 38 | * @param boolean $count_check 登録商品数をチェックする場合はtrue |
---|
| 39 | */ |
---|
[23124] | 40 | public function __construct($count_check = FALSE) |
---|
[22589] | 41 | { |
---|
| 42 | $this->count_check = $count_check; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
[22595] | 46 | * カテゴリーの情報を取得. |
---|
[22856] | 47 | * |
---|
[23124] | 48 | * @param integer $category_id カテゴリーID |
---|
[22595] | 49 | * @return array |
---|
| 50 | */ |
---|
| 51 | public function get($category_id) |
---|
| 52 | { |
---|
| 53 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[22777] | 54 | $col = 'dtb_category.*, dtb_category_total_count.product_count'; |
---|
[22595] | 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)); |
---|
[22856] | 62 | |
---|
[22595] | 63 | return $arrRet; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
[22588] | 67 | * カテゴリー一覧の取得. |
---|
[22856] | 68 | * |
---|
[23124] | 69 | * @param boolean $cid_to_key 配列のキーをカテゴリーIDにする場合はtrue |
---|
| 70 | * @return array カテゴリー一覧の配列 |
---|
[22588] | 71 | */ |
---|
[22589] | 72 | public function getList($cid_to_key = FALSE) |
---|
[22588] | 73 | { |
---|
[22603] | 74 | static $arrCategory = array(), $cidIsKey = array(); |
---|
[22594] | 75 | |
---|
| 76 | if (!isset($arrCategory[$this->count_check])) { |
---|
| 77 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[22777] | 78 | $col = 'dtb_category.*, dtb_category_total_count.product_count'; |
---|
[22594] | 79 | $from = 'dtb_category left join dtb_category_total_count ON dtb_category.category_id = dtb_category_total_count.category_id'; |
---|
| 80 | // 登録商品数のチェック |
---|
| 81 | if ($this->count_check) { |
---|
| 82 | $where = 'del_flg = 0 AND product_count > 0'; |
---|
| 83 | } else { |
---|
| 84 | $where = 'del_flg = 0'; |
---|
| 85 | } |
---|
| 86 | $objQuery->setOption('ORDER BY rank DESC'); |
---|
| 87 | $arrTmp = $objQuery->select($col, $from, $where); |
---|
| 88 | |
---|
| 89 | $arrCategory[$this->count_check] = $arrTmp; |
---|
[22588] | 90 | } |
---|
| 91 | |
---|
| 92 | if ($cid_to_key) { |
---|
[22603] | 93 | if (!isset($cidIsKey[$this->count_check])) { |
---|
| 94 | // 配列のキーをカテゴリーIDに |
---|
| 95 | $cidIsKey[$this->count_check] = SC_Utils_Ex::makeArrayIDToKey('category_id', $arrCategory[$this->count_check]); |
---|
| 96 | } |
---|
[23124] | 97 | |
---|
[22603] | 98 | return $cidIsKey[$this->count_check]; |
---|
[22588] | 99 | } |
---|
[22594] | 100 | |
---|
| 101 | return $arrCategory[$this->count_check]; |
---|
[22588] | 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * カテゴリーツリーの取得. |
---|
[22856] | 106 | * |
---|
[23436] | 107 | * @return array |
---|
[22588] | 108 | */ |
---|
[22589] | 109 | public function getTree() |
---|
[22588] | 110 | { |
---|
[22603] | 111 | static $arrTree = array(); |
---|
| 112 | if (!isset($arrTree[$this->count_check])) { |
---|
| 113 | $arrList = $this->getList(); |
---|
| 114 | $arrTree[$this->count_check] = SC_Utils_Ex::buildTree('category_id', 'parent_category_id', LEVEL_MAX, $arrList); |
---|
| 115 | } |
---|
[22856] | 116 | |
---|
[22603] | 117 | return $arrTree[$this->count_check]; |
---|
[22588] | 118 | } |
---|
[22595] | 119 | |
---|
| 120 | /** |
---|
| 121 | * 親カテゴリーIDの配列を取得. |
---|
[22856] | 122 | * |
---|
[23124] | 123 | * @param integer $category_id 起点のカテゴリーID |
---|
| 124 | * @param boolean $id_only IDだけの配列を返す場合はtrue |
---|
[22595] | 125 | * @return array |
---|
| 126 | */ |
---|
[22597] | 127 | public function getTreeTrail($category_id, $id_only = TRUE) |
---|
[22595] | 128 | { |
---|
[22603] | 129 | $arrCategory = $this->getList(TRUE); |
---|
| 130 | $arrTrailID = SC_Utils_Ex::getTreeTrail($category_id, 'category_id', 'parent_category_id', $arrCategory, TRUE, 0, $id_only); |
---|
[22856] | 131 | |
---|
[22595] | 132 | return $arrTrailID; |
---|
| 133 | } |
---|
[23435] | 134 | |
---|
| 135 | /** |
---|
| 136 | * 指定カテゴリーの子孫カテゴリーを取得 |
---|
| 137 | * |
---|
| 138 | * @param int $category_id カテゴリーID |
---|
| 139 | * @return array |
---|
| 140 | */ |
---|
[23436] | 141 | public function getTreeBranch($category_id) |
---|
| 142 | { |
---|
[23435] | 143 | $arrTree = $this->getTree(); |
---|
| 144 | $arrTrail = $this->getTreeTrail($category_id, true); |
---|
| 145 | |
---|
| 146 | // ルートから指定カテゴリーまでたどる. |
---|
| 147 | foreach ($arrTrail as $parent_id) { |
---|
| 148 | $nextTree = array(); |
---|
| 149 | foreach ($arrTree as $branch) { |
---|
| 150 | if ($branch['category_id'] == $parent_id && isset($branch['children'])) { |
---|
| 151 | $nextTree = $branch['children']; |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | $arrTree = $nextTree; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | return $arrTree; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * カテゴリーの削除 |
---|
| 162 | * |
---|
| 163 | * @param int $category_id カテゴリーID |
---|
| 164 | * @return void |
---|
| 165 | */ |
---|
[23436] | 166 | public function delete($category_id) |
---|
| 167 | { |
---|
[23435] | 168 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 169 | // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。) |
---|
| 170 | $objDb->sfDeleteRankRecord('dtb_category', 'category_id', $category_id, '', true); |
---|
| 171 | } |
---|
[23436] | 172 | |
---|
| 173 | /** |
---|
| 174 | * カテゴリーの表示順をひとつ上げる. |
---|
| 175 | * |
---|
| 176 | * @param int $category_id カテゴリーID |
---|
| 177 | * @return void |
---|
| 178 | */ |
---|
| 179 | public function rankUp($category_id) |
---|
| 180 | { |
---|
| 181 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 182 | $objQuery->begin(); |
---|
| 183 | $up_id = $this->getNeighborRankId('upper', $category_id); |
---|
| 184 | if ($up_id != '') { |
---|
| 185 | // 上のグループのrankから減算する数 |
---|
| 186 | $my_count = $this->countAllBranches($category_id); |
---|
| 187 | // 自分のグループのrankに加算する数 |
---|
| 188 | $up_count = $this->countAllBranches($up_id); |
---|
| 189 | if ($my_count > 0 && $up_count > 0) { |
---|
| 190 | // 自分のグループに加算 |
---|
| 191 | $this->raiseBranchRank($objQuery, $category_id, $up_count); |
---|
| 192 | // 上のグループから減算 |
---|
| 193 | $this->reduceBranchRank($objQuery, $up_id, $my_count); |
---|
| 194 | } |
---|
| 195 | } |
---|
| 196 | $objQuery->commit(); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /** |
---|
| 200 | * カテゴリーの表示順をひとつ下げる. |
---|
| 201 | * |
---|
| 202 | * @param int $category_id カテゴリーID |
---|
| 203 | * @return void |
---|
| 204 | */ |
---|
| 205 | public function rankDown($category_id) |
---|
| 206 | { |
---|
| 207 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 208 | $objQuery->begin(); |
---|
| 209 | $down_id = $this->getNeighborRankId('lower', $category_id); |
---|
| 210 | if ($down_id != '') { |
---|
| 211 | // 下のグループのrankに加算する数 |
---|
| 212 | $my_count = $this->countAllBranches($category_id); |
---|
| 213 | // 自分のグループのrankから減算する数 |
---|
| 214 | $down_count = $this->countAllBranches($down_id); |
---|
| 215 | if ($my_count > 0 && $down_count > 0) { |
---|
| 216 | // 自分のグループから減算 |
---|
| 217 | $this->raiseBranchRank($objQuery, $down_id, $my_count); |
---|
| 218 | // 下のグループに加算 |
---|
| 219 | $this->reduceBranchRank($objQuery, $category_id, $down_count); |
---|
| 220 | } |
---|
| 221 | } |
---|
| 222 | $objQuery->commit(); |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
| 226 | * 並びがとなりのIDを取得する。 |
---|
| 227 | * |
---|
| 228 | * @param string $side 上 upper か下 down か |
---|
| 229 | * @param int $category_id カテゴリーID |
---|
| 230 | * @return int |
---|
| 231 | */ |
---|
| 232 | private function getNeighborRankId($side, $category_id) |
---|
| 233 | { |
---|
| 234 | $arrCategory = $this->get($category_id); |
---|
| 235 | $parent_id = $arrCategory['parent_category_id']; |
---|
| 236 | |
---|
| 237 | if ($parent_id == 0) { |
---|
| 238 | $arrBrother = $this->getTree(); |
---|
| 239 | } else { |
---|
| 240 | $arrBrother = $this->getTreeBranch($parent_id); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | // 全ての子を取得する。 |
---|
| 244 | $max = count($arrBrother); |
---|
| 245 | $upper_id = ''; |
---|
| 246 | for ($cnt = 0; $cnt < $max; $cnt++) { |
---|
| 247 | if ($arrBrother[$cnt]['category_id'] == $category_id) { |
---|
| 248 | if ($side == 'upper') { |
---|
| 249 | $index = $cnt - 1; |
---|
| 250 | } else { |
---|
| 251 | $index = $cnt + 1; |
---|
| 252 | } |
---|
| 253 | $upper_id = $arrBrother[$index]['category_id']; |
---|
| 254 | break; |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | return $upper_id; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * 指定カテゴリーを含めた子孫カテゴリーの数を取得する. |
---|
| 263 | * |
---|
| 264 | * @param int $category_id カテゴリーID |
---|
| 265 | * @return int |
---|
| 266 | */ |
---|
| 267 | private function countAllBranches($category_id) |
---|
| 268 | { |
---|
| 269 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 270 | // 子ID一覧を取得 |
---|
| 271 | $arrRet = $objDb->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $category_id); |
---|
| 272 | |
---|
| 273 | return count($arrRet); |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | /** |
---|
| 277 | * 子孫カテゴリーの表示順を一括して上げる. |
---|
| 278 | * |
---|
| 279 | * @param SC_Query $objQuery |
---|
| 280 | * @param int $category_id |
---|
| 281 | * @param int $count |
---|
| 282 | * @return array|bool |
---|
| 283 | */ |
---|
| 284 | private function raiseBranchRank(SC_Query $objQuery, $category_id, $count) |
---|
| 285 | { |
---|
| 286 | $table = 'dtb_category'; |
---|
| 287 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 288 | // 子ID一覧を取得 |
---|
| 289 | $arrRet = $objDb->sfGetChildrenArray($table, 'parent_category_id', 'category_id', $category_id); |
---|
| 290 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
| 291 | $where = "category_id IN ($line) AND del_flg = 0"; |
---|
| 292 | $arrRawVal = array( |
---|
| 293 | 'rank' => "(rank + $count)", |
---|
| 294 | ); |
---|
| 295 | |
---|
| 296 | return $objQuery->update($table, array(), $where, array(), $arrRawVal); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * 子孫カテゴリーの表示順を一括して下げる. |
---|
| 301 | * |
---|
| 302 | * @param SC_Query $objQuery |
---|
| 303 | * @param int $category_id |
---|
| 304 | * @param int $count |
---|
| 305 | * @return array|bool |
---|
| 306 | */ |
---|
| 307 | private function reduceBranchRank(SC_Query $objQuery, $category_id, $count) |
---|
| 308 | { |
---|
| 309 | $table = 'dtb_category'; |
---|
| 310 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 311 | // 子ID一覧を取得 |
---|
| 312 | $arrRet = $objDb->sfGetChildrenArray($table, 'parent_category_id', 'category_id', $category_id); |
---|
| 313 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
| 314 | $where = "category_id IN ($line) AND del_flg = 0"; |
---|
| 315 | $arrRawVal = array( |
---|
| 316 | 'rank' => "(rank - $count)", |
---|
| 317 | ); |
---|
| 318 | |
---|
| 319 | return $objQuery->update($table, array(), $where, array(), $arrRawVal); |
---|
| 320 | } |
---|
[22588] | 321 | } |
---|