[22588] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | * This file is part of EC-CUBE |
---|
| 4 | * |
---|
[23546] | 5 | * Copyright(c) 2000-2014 LOCKON CO.,LTD. All Rights Reserved. |
---|
[22588] | 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 | { |
---|
[23637] | 33 | protected $count_check; |
---|
[22589] | 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 | * |
---|
[23437] | 69 | * @param bool $cid_to_key 配列のキーをカテゴリーIDにする場合はtrue |
---|
| 70 | * @param bool $reset スタティック変数をリセットする場合はtrue |
---|
[23124] | 71 | * @return array カテゴリー一覧の配列 |
---|
[22588] | 72 | */ |
---|
[23437] | 73 | public function getList($cid_to_key = FALSE, $reset = FALSE) |
---|
[22588] | 74 | { |
---|
[22603] | 75 | static $arrCategory = array(), $cidIsKey = array(); |
---|
[22594] | 76 | |
---|
[23437] | 77 | if ($reset) { |
---|
| 78 | $arrCategory = array(); |
---|
| 79 | $cidIsKey = array(); |
---|
| 80 | } |
---|
| 81 | |
---|
[22594] | 82 | if (!isset($arrCategory[$this->count_check])) { |
---|
| 83 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
[22777] | 84 | $col = 'dtb_category.*, dtb_category_total_count.product_count'; |
---|
[22594] | 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; |
---|
[22588] | 96 | } |
---|
| 97 | |
---|
| 98 | if ($cid_to_key) { |
---|
[22603] | 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 | } |
---|
[23124] | 103 | |
---|
[22603] | 104 | return $cidIsKey[$this->count_check]; |
---|
[22588] | 105 | } |
---|
[22594] | 106 | |
---|
| 107 | return $arrCategory[$this->count_check]; |
---|
[22588] | 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
| 111 | * カテゴリーツリーの取得. |
---|
[22856] | 112 | * |
---|
[23437] | 113 | * @param bool $reset スタティック変数をリセットする場合はtrue |
---|
[23436] | 114 | * @return array |
---|
[22588] | 115 | */ |
---|
[23437] | 116 | public function getTree($reset = false) |
---|
[22588] | 117 | { |
---|
[22603] | 118 | static $arrTree = array(); |
---|
[23437] | 119 | |
---|
| 120 | if ($reset) { |
---|
| 121 | $arrTree = array(); |
---|
| 122 | } |
---|
| 123 | |
---|
[22603] | 124 | if (!isset($arrTree[$this->count_check])) { |
---|
[23437] | 125 | $arrList = $this->getList(false, $reset); |
---|
[22603] | 126 | $arrTree[$this->count_check] = SC_Utils_Ex::buildTree('category_id', 'parent_category_id', LEVEL_MAX, $arrList); |
---|
| 127 | } |
---|
[22856] | 128 | |
---|
[22603] | 129 | return $arrTree[$this->count_check]; |
---|
[22588] | 130 | } |
---|
[22595] | 131 | |
---|
| 132 | /** |
---|
| 133 | * 親カテゴリーIDの配列を取得. |
---|
[22856] | 134 | * |
---|
[23124] | 135 | * @param integer $category_id 起点のカテゴリーID |
---|
| 136 | * @param boolean $id_only IDだけの配列を返す場合はtrue |
---|
[22595] | 137 | * @return array |
---|
| 138 | */ |
---|
[22597] | 139 | public function getTreeTrail($category_id, $id_only = TRUE) |
---|
[22595] | 140 | { |
---|
[22603] | 141 | $arrCategory = $this->getList(TRUE); |
---|
| 142 | $arrTrailID = SC_Utils_Ex::getTreeTrail($category_id, 'category_id', 'parent_category_id', $arrCategory, TRUE, 0, $id_only); |
---|
[22856] | 143 | |
---|
[22595] | 144 | return $arrTrailID; |
---|
| 145 | } |
---|
[23435] | 146 | |
---|
| 147 | /** |
---|
| 148 | * 指定カテゴリーの子孫カテゴリーを取得 |
---|
| 149 | * |
---|
| 150 | * @param int $category_id カテゴリーID |
---|
| 151 | * @return array |
---|
| 152 | */ |
---|
[23436] | 153 | public function getTreeBranch($category_id) |
---|
| 154 | { |
---|
[23435] | 155 | $arrTree = $this->getTree(); |
---|
| 156 | $arrTrail = $this->getTreeTrail($category_id, true); |
---|
| 157 | |
---|
[23438] | 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 | } |
---|
[23435] | 169 | } |
---|
[23438] | 170 | $arrTree = $nextTree; |
---|
[23435] | 171 | } |
---|
[23438] | 172 | return $arrTree; |
---|
[23435] | 173 | } |
---|
[23438] | 174 | } |
---|
[23435] | 175 | |
---|
[23438] | 176 | /** |
---|
| 177 | * カテゴリーの登録. |
---|
| 178 | * |
---|
| 179 | * @param array $data |
---|
[23439] | 180 | * @return int|bool |
---|
[23438] | 181 | */ |
---|
| 182 | public function save($data) |
---|
| 183 | { |
---|
| 184 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 185 | |
---|
| 186 | $category_id = $data['category_id']; |
---|
[23439] | 187 | // ミリ秒付きの時間文字列を取得. CSVへの対応. |
---|
| 188 | // トランザクション内のCURRENT_TIMESTAMPは全てcommit()時の時間に統一されてしまう為. |
---|
| 189 | $query = array('update_date' => SC_Utils_Ex::getFormattedDateWithMicroSecond()); |
---|
[23438] | 190 | $objQuery->begin(); |
---|
| 191 | |
---|
| 192 | if ($category_id == '') { |
---|
| 193 | // 新規登録 |
---|
| 194 | $parent_category_id = $data['parent_category_id']; |
---|
| 195 | $rank = null; |
---|
| 196 | if ($parent_category_id == 0) { |
---|
| 197 | // ROOT階層で最大のランクを取得する。 |
---|
| 198 | $where = 'parent_category_id = ?'; |
---|
| 199 | $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1; |
---|
| 200 | } else { |
---|
| 201 | // 親のランクを自分のランクとする。 |
---|
| 202 | $where = 'category_id = ?'; |
---|
| 203 | $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id)); |
---|
| 204 | // 追加レコードのランク以上のレコードを一つあげる。 |
---|
| 205 | $where = 'rank >= ?'; |
---|
| 206 | $arrRawSql = array( |
---|
| 207 | 'rank' => '(rank + 1)', |
---|
| 208 | ); |
---|
| 209 | $objQuery->update('dtb_category', array(), $where, array($rank), $arrRawSql); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | $where = 'category_id = ?'; |
---|
| 213 | // 自分のレベルを取得する(親のレベル + 1) |
---|
| 214 | $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1; |
---|
| 215 | |
---|
| 216 | $query['category_id'] = $objQuery->nextVal('dtb_category_category_id'); |
---|
| 217 | $query['category_name'] = $data['category_name']; |
---|
| 218 | $query['parent_category_id'] = $data['parent_category_id']; |
---|
[23439] | 219 | $query['create_date'] = $query['update_date']; |
---|
[23438] | 220 | $query['creator_id'] = $_SESSION['member_id']; |
---|
| 221 | $query['rank'] = $rank; |
---|
| 222 | $query['level'] = $level; |
---|
| 223 | |
---|
[23439] | 224 | $result = $objQuery->insert('dtb_category', $query); |
---|
[23438] | 225 | } else { |
---|
| 226 | // 既存編集 |
---|
[23439] | 227 | $query['category_id'] = $category_id; |
---|
[23438] | 228 | $query['parent_category_id'] = $data['parent_category_id']; |
---|
| 229 | $query['category_name'] = $data['category_name']; |
---|
| 230 | $where = 'category_id = ?'; |
---|
[23439] | 231 | $result = $objQuery->update('dtb_category', $query, $where, array($category_id)); |
---|
[23438] | 232 | } |
---|
| 233 | |
---|
| 234 | $objQuery->commit(); |
---|
[23439] | 235 | return ($result) ? $query['category_id'] : FALSE; |
---|
[23435] | 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * カテゴリーの削除 |
---|
| 240 | * |
---|
| 241 | * @param int $category_id カテゴリーID |
---|
| 242 | * @return void |
---|
| 243 | */ |
---|
[23436] | 244 | public function delete($category_id) |
---|
| 245 | { |
---|
[23435] | 246 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 247 | // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。) |
---|
| 248 | $objDb->sfDeleteRankRecord('dtb_category', 'category_id', $category_id, '', true); |
---|
| 249 | } |
---|
[23436] | 250 | |
---|
| 251 | /** |
---|
| 252 | * カテゴリーの表示順をひとつ上げる. |
---|
| 253 | * |
---|
| 254 | * @param int $category_id カテゴリーID |
---|
| 255 | * @return void |
---|
| 256 | */ |
---|
| 257 | public function rankUp($category_id) |
---|
| 258 | { |
---|
| 259 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 260 | $objQuery->begin(); |
---|
| 261 | $up_id = $this->getNeighborRankId('upper', $category_id); |
---|
| 262 | if ($up_id != '') { |
---|
| 263 | // 上のグループのrankから減算する数 |
---|
| 264 | $my_count = $this->countAllBranches($category_id); |
---|
| 265 | // 自分のグループのrankに加算する数 |
---|
| 266 | $up_count = $this->countAllBranches($up_id); |
---|
| 267 | if ($my_count > 0 && $up_count > 0) { |
---|
| 268 | // 自分のグループに加算 |
---|
| 269 | $this->raiseBranchRank($objQuery, $category_id, $up_count); |
---|
| 270 | // 上のグループから減算 |
---|
| 271 | $this->reduceBranchRank($objQuery, $up_id, $my_count); |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | $objQuery->commit(); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * カテゴリーの表示順をひとつ下げる. |
---|
| 279 | * |
---|
| 280 | * @param int $category_id カテゴリーID |
---|
| 281 | * @return void |
---|
| 282 | */ |
---|
| 283 | public function rankDown($category_id) |
---|
| 284 | { |
---|
| 285 | $objQuery =& SC_Query_Ex::getSingletonInstance(); |
---|
| 286 | $objQuery->begin(); |
---|
| 287 | $down_id = $this->getNeighborRankId('lower', $category_id); |
---|
| 288 | if ($down_id != '') { |
---|
| 289 | // 下のグループのrankに加算する数 |
---|
| 290 | $my_count = $this->countAllBranches($category_id); |
---|
| 291 | // 自分のグループのrankから減算する数 |
---|
| 292 | $down_count = $this->countAllBranches($down_id); |
---|
| 293 | if ($my_count > 0 && $down_count > 0) { |
---|
| 294 | // 自分のグループから減算 |
---|
| 295 | $this->raiseBranchRank($objQuery, $down_id, $my_count); |
---|
| 296 | // 下のグループに加算 |
---|
| 297 | $this->reduceBranchRank($objQuery, $category_id, $down_count); |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | $objQuery->commit(); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | /** |
---|
| 304 | * 並びがとなりのIDを取得する。 |
---|
| 305 | * |
---|
| 306 | * @param string $side 上 upper か下 down か |
---|
| 307 | * @param int $category_id カテゴリーID |
---|
| 308 | * @return int |
---|
| 309 | */ |
---|
| 310 | private function getNeighborRankId($side, $category_id) |
---|
| 311 | { |
---|
| 312 | $arrCategory = $this->get($category_id); |
---|
| 313 | $parent_id = $arrCategory['parent_category_id']; |
---|
| 314 | |
---|
| 315 | if ($parent_id == 0) { |
---|
| 316 | $arrBrother = $this->getTree(); |
---|
| 317 | } else { |
---|
| 318 | $arrBrother = $this->getTreeBranch($parent_id); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | // 全ての子を取得する。 |
---|
| 322 | $max = count($arrBrother); |
---|
| 323 | $upper_id = ''; |
---|
| 324 | for ($cnt = 0; $cnt < $max; $cnt++) { |
---|
| 325 | if ($arrBrother[$cnt]['category_id'] == $category_id) { |
---|
| 326 | if ($side == 'upper') { |
---|
| 327 | $index = $cnt - 1; |
---|
| 328 | } else { |
---|
| 329 | $index = $cnt + 1; |
---|
| 330 | } |
---|
| 331 | $upper_id = $arrBrother[$index]['category_id']; |
---|
| 332 | break; |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | return $upper_id; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | /** |
---|
| 340 | * 指定カテゴリーを含めた子孫カテゴリーの数を取得する. |
---|
| 341 | * |
---|
| 342 | * @param int $category_id カテゴリーID |
---|
| 343 | * @return int |
---|
| 344 | */ |
---|
| 345 | private function countAllBranches($category_id) |
---|
| 346 | { |
---|
| 347 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 348 | // 子ID一覧を取得 |
---|
| 349 | $arrRet = $objDb->sfGetChildrenArray('dtb_category', 'parent_category_id', 'category_id', $category_id); |
---|
| 350 | |
---|
| 351 | return count($arrRet); |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | /** |
---|
| 355 | * 子孫カテゴリーの表示順を一括して上げる. |
---|
| 356 | * |
---|
| 357 | * @param SC_Query $objQuery |
---|
| 358 | * @param int $category_id |
---|
| 359 | * @param int $count |
---|
| 360 | * @return array|bool |
---|
| 361 | */ |
---|
| 362 | private function raiseBranchRank(SC_Query $objQuery, $category_id, $count) |
---|
| 363 | { |
---|
| 364 | $table = 'dtb_category'; |
---|
| 365 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 366 | // 子ID一覧を取得 |
---|
| 367 | $arrRet = $objDb->sfGetChildrenArray($table, 'parent_category_id', 'category_id', $category_id); |
---|
| 368 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
| 369 | $where = "category_id IN ($line) AND del_flg = 0"; |
---|
| 370 | $arrRawVal = array( |
---|
| 371 | 'rank' => "(rank + $count)", |
---|
| 372 | ); |
---|
| 373 | |
---|
| 374 | return $objQuery->update($table, array(), $where, array(), $arrRawVal); |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
| 378 | * 子孫カテゴリーの表示順を一括して下げる. |
---|
| 379 | * |
---|
| 380 | * @param SC_Query $objQuery |
---|
| 381 | * @param int $category_id |
---|
| 382 | * @param int $count |
---|
| 383 | * @return array|bool |
---|
| 384 | */ |
---|
| 385 | private function reduceBranchRank(SC_Query $objQuery, $category_id, $count) |
---|
| 386 | { |
---|
| 387 | $table = 'dtb_category'; |
---|
| 388 | $objDb = new SC_Helper_DB_Ex(); |
---|
| 389 | // 子ID一覧を取得 |
---|
| 390 | $arrRet = $objDb->sfGetChildrenArray($table, 'parent_category_id', 'category_id', $category_id); |
---|
| 391 | $line = SC_Utils_Ex::sfGetCommaList($arrRet); |
---|
| 392 | $where = "category_id IN ($line) AND del_flg = 0"; |
---|
| 393 | $arrRawVal = array( |
---|
| 394 | 'rank' => "(rank - $count)", |
---|
| 395 | ); |
---|
| 396 | |
---|
| 397 | return $objQuery->update($table, array(), $where, array(), $arrRawVal); |
---|
| 398 | } |
---|
[23459] | 399 | |
---|
| 400 | /** |
---|
| 401 | * 有効なカテゴリーIDかチェックする. |
---|
| 402 | * |
---|
| 403 | * @param int $category_id |
---|
| 404 | * @param bool $include_deleted |
---|
| 405 | * @return bool |
---|
| 406 | */ |
---|
| 407 | public function isValidCategoryId($category_id, $include_deleted = false) { |
---|
| 408 | if ($include_deleted) { |
---|
| 409 | $where = ''; |
---|
| 410 | } else { |
---|
| 411 | $where = 'del_flg = 0'; |
---|
| 412 | } |
---|
| 413 | if ( |
---|
| 414 | SC_Utils_Ex::sfIsInt($category_id) |
---|
| 415 | && !SC_Utils_Ex::sfIsZeroFilling($category_id) |
---|
[23503] | 416 | && SC_Helper_DB_Ex::sfIsRecord('dtb_category', 'category_id', array($category_id), $where) |
---|
[23459] | 417 | ) { |
---|
| 418 | return true; |
---|
| 419 | } |
---|
| 420 | return false; |
---|
| 421 | } |
---|
[22588] | 422 | } |
---|