- Timestamp:
- 2014/10/12 15:17:12 (9 years ago)
- Location:
- branches/version-2_13_3/data/class
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13_3/data/class/helper/SC_Helper_Category.php
r23649 r23650 67 67 * カテゴリー一覧の取得. 68 68 * 69 * @param bool $cid_to_key 配列のキーをカテゴリーIDにする場合はtrue 70 * @param bool $reset スタティック変数をリセットする場合はtrue 69 * @param boolean $cid_to_key 配列のキーをカテゴリーIDにする場合はtrue 71 70 * @return array カテゴリー一覧の配列 72 71 */ 73 public function getList($cid_to_key = FALSE , $reset = FALSE)72 public function getList($cid_to_key = FALSE) 74 73 { 75 74 static $arrCategory = array(), $cidIsKey = array(); 76 77 if ($reset) {78 $arrCategory = array();79 $cidIsKey = array();80 }81 75 82 76 if (!isset($arrCategory[$this->count_check])) { … … 111 105 * カテゴリーツリーの取得. 112 106 * 113 * @param bool $reset スタティック変数をリセットする場合はtrue 114 * @return array 107 * @return type 115 108 */ 116 public function getTree( $reset = false)109 public function getTree() 117 110 { 118 111 static $arrTree = array(); 119 120 if ($reset) {121 $arrTree = array();122 }123 124 112 if (!isset($arrTree[$this->count_check])) { 125 $arrList = $this->getList( false, $reset);113 $arrList = $this->getList(); 126 114 $arrTree[$this->count_check] = SC_Utils_Ex::buildTree('category_id', 'parent_category_id', LEVEL_MAX, $arrList); 127 115 } … … 151 139 * @return array 152 140 */ 153 public function getTreeBranch($category_id) 154 { 141 public function getTreeBranch($category_id) { 155 142 $arrTree = $this->getTree(); 156 143 $arrTrail = $this->getTreeTrail($category_id, true); 157 144 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 } 145 // ルートから指定カテゴリーまでたどる. 146 foreach ($arrTrail as $parent_id) { 147 $nextTree = array(); 148 foreach ($arrTree as $branch) { 149 if ($branch['category_id'] == $parent_id && isset($branch['children'])) { 150 $nextTree = $branch['children']; 169 151 } 170 $arrTree = $nextTree;171 152 } 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)); 153 $arrTree = $nextTree; 229 154 } 230 155 231 $objQuery->commit();156 return $arrTree; 232 157 } 233 158 … … 238 163 * @return void 239 164 */ 240 public function delete($category_id) 241 { 165 public function delete($category_id) { 242 166 $objDb = new SC_Helper_DB_Ex(); 243 167 // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。) 244 168 $objDb->sfDeleteRankRecord('dtb_category', 'category_id', $category_id, '', true); 245 }246 247 /**248 * カテゴリーの表示順をひとつ上げる.249 *250 * @param int $category_id カテゴリーID251 * @return void252 */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 カテゴリーID277 * @return void278 */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 カテゴリーID304 * @return int305 */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 カテゴリーID339 * @return int340 */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 $objQuery354 * @param int $category_id355 * @param int $count356 * @return array|bool357 */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 $objQuery377 * @param int $category_id378 * @param int $count379 * @return array|bool380 */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 169 } 395 170 -
branches/version-2_13_3/data/class/pages/admin/products/LC_Page_Admin_Products_Category.php
r23605 r23650 172 172 // 親カテゴリIDの保持 173 173 $this->arrForm['parent_category_id'] = $parent_category_id; 174 // カテゴリ一覧を取得 175 $this->arrList = $this->findCategoiesByParentCategoryId($parent_category_id); 174 176 // カテゴリツリーを取得 175 $this->arrTree = $objCategory->getTree( true);177 $this->arrTree = $objCategory->getTree(); 176 178 $this->arrParentID = $objCategory->getTreeTrail($parent_category_id); 177 // カテゴリ一覧を取得178 $this->arrList = $objCategory->getTreeBranch($parent_category_id);179 179 // ぱんくずの生成 180 180 $arrBread = $objCategory->getTreeTrail($this->arrForm['parent_category_id'], FALSE); … … 249 249 public function doEdit(&$objFormParam) 250 250 { 251 $category_id = $objFormParam->getValue('category_id'); 252 253 // 追加か 254 $add = strlen($category_id) === 0; 255 251 256 // エラーチェック 252 $this->arrErr = $this->checkError($objFormParam );257 $this->arrErr = $this->checkError($objFormParam, $add); 253 258 254 259 // エラーがない場合、追加・更新処理 255 260 if (empty($this->arrErr)) { 256 261 $arrCategory = $objFormParam->getDbArray(); 257 $objCategory = new SC_Helper_Category_Ex(); 258 $objCategory->save($arrCategory); 262 263 // 追加 264 if ($add) { 265 $this->registerCategory($arrCategory); 266 } 267 // 更新 268 else { 269 unset($arrCategory['category_id']); 270 $this->updateCategory($category_id, $arrCategory); 271 } 259 272 // エラーがある場合、入力値の再表示 260 273 } else { … … 267 280 * 268 281 * @param SC_FormParam $objFormParam 269 * @return array 270 */ 271 public function checkError(&$objFormParam) 272 { 273 $objCategory = new SC_Helper_Category_Ex(); 282 * @param boolean $add 追加か 283 * @return void 284 */ 285 public function checkError(&$objFormParam, $add) 286 { 287 $objQuery =& SC_Query_Ex::getSingletonInstance(); 274 288 275 289 // 入力項目チェック … … 284 298 285 299 // 追加の場合に固有のチェック 286 if ( !$category_id) {300 if ($add) { 287 301 // 登録数上限チェック 288 $count = count($objCategory->getList()); 302 $where = 'del_flg = 0'; 303 $count = $objQuery->count('dtb_category', $where); 289 304 if ($count >= CATEGORY_MAX) { 290 305 $arrErr['category_name'] = '※ カテゴリの登録最大数を超えました。<br/>'; … … 294 309 295 310 // 階層上限チェック 296 $arrParent = $objCategory->get($parent_category_id); 297 if ($arrParent['level'] >= LEVEL_MAX) { 311 if ($this->isOverLevel($parent_category_id)) { 298 312 $arrErr['category_name'] = '※ ' . LEVEL_MAX . '階層以上の登録はできません。<br/>'; 299 313 … … 303 317 304 318 // 重複チェック 305 $exists = false; 306 $arrBrother = $objCategory->getTreeBranch($parent_category_id); 307 foreach ($arrBrother as $brother) { 308 if ($brother['category_name'] == $category_name && $brother['category_id'] != $category_id) { 309 $exists = true; 310 } 311 } 319 $arrWhereVal = array(); 320 $where = 'del_flg = 0 AND parent_category_id = ? AND category_name = ?'; 321 $arrWhereVal[] = $parent_category_id; 322 $arrWhereVal[] = $category_name; 323 // 更新の場合、抽出対象から自己を除外する 324 if (!$add) { 325 $where .= ' AND category_id <> ?'; 326 $arrWhereVal[] = $category_id; 327 } 328 $exists = $objQuery->exists('dtb_category', $where, $arrWhereVal); 312 329 if ($exists) { 313 330 $arrErr['category_name'] = '※ 既に同じ内容の登録が存在します。<br/>'; … … 327 344 public function doUp(&$objFormParam) 328 345 { 329 $objCategory = new SC_Helper_Category_Ex(false);330 346 $category_id = $objFormParam->getValue('category_id'); 331 $objCategory->rankUp($category_id); 347 348 $objQuery =& SC_Query_Ex::getSingletonInstance(); 349 $objQuery->begin(); 350 $up_id = $this->lfGetUpRankID($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id); 351 if ($up_id != '') { 352 // 上のグループのrankから減算する数 353 $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id); 354 // 自分のグループのrankに加算する数 355 $up_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id); 356 if ($my_count > 0 && $up_count > 0) { 357 // 自分のグループに加算 358 $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id, $up_count); 359 // 上のグループから減算 360 $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $up_id, $my_count); 361 } 362 } 363 $objQuery->commit(); 332 364 } 333 365 … … 340 372 public function doDown(&$objFormParam) 341 373 { 342 $objCategory = new SC_Helper_Category_Ex(false);343 374 $category_id = $objFormParam->getValue('category_id'); 344 $objCategory->rankDown($category_id); 375 376 $objQuery =& SC_Query_Ex::getSingletonInstance(); 377 $objQuery->begin(); 378 $down_id = $this->lfGetDownRankID($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id); 379 if ($down_id != '') { 380 // 下のグループのrankに加算する数 381 $my_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id); 382 // 自分のグループのrankから減算する数 383 $down_count = $this->lfCountChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $down_id); 384 if ($my_count > 0 && $down_count > 0) { 385 // 自分のグループから減算 386 $this->lfUpRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $down_id, $my_count); 387 // 下のグループに加算 388 $this->lfDownRankChilds($objQuery, 'dtb_category', 'parent_category_id', 'category_id', $category_id, $down_count); 389 } 390 } 391 $objQuery->commit(); 345 392 } 346 393 … … 359 406 360 407 /** 361 * @param SC_Query $objQuery 362 * @param string $table 363 * @param string $pid_name 364 * @param string $id_name 365 */ 408 * 親カテゴリIDでカテゴリを検索する. 409 * 410 * - 表示順の降順でソートする 411 * - 有効なカテゴリを返す(del_flag = 0) 412 * 413 * @param SC_Query $objQuery 414 * @param int $parent_category_id 親カテゴリID 415 * @return array カテゴリの配列 416 */ 417 public function findCategoiesByParentCategoryId($parent_category_id) 418 { 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 427 return $objQuery->select($col, 'dtb_category', $where, array($parent_category_id)); 428 } 429 430 /** 431 * カテゴリを更新する 432 * 433 * @param SC_FormParam $objFormParam SC_FormParam インスタンス 434 * @return void 435 */ 436 public function updateCategory($category_id, $arrCategory) 437 { 438 $objQuery =& SC_Query_Ex::getSingletonInstance(); 439 440 $arrCategory['update_date'] = 'CURRENT_TIMESTAMP'; 441 442 $objQuery->begin(); 443 $where = 'category_id = ?'; 444 $objQuery->update('dtb_category', $arrCategory, $where, array($category_id)); 445 $objQuery->commit(); 446 } 447 448 /** 449 * カテゴリを登録する 450 * 451 * @param SC_FormParam $objFormParam SC_FormParam インスタンス 452 * @return void 453 */ 454 public function registerCategory($arrCategory) 455 { 456 $objQuery =& SC_Query_Ex::getSingletonInstance(); 457 458 $parent_category_id = $arrCategory['parent_category_id']; 459 460 $objQuery->begin(); 461 462 $rank = null; 463 if ($parent_category_id == 0) { 464 // ROOT階層で最大のランクを取得する。 465 $where = 'parent_category_id = ?'; 466 $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1; 467 } else { 468 // 親のランクを自分のランクとする。 469 $where = 'category_id = ?'; 470 $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id)); 471 // 追加レコードのランク以上のレコードを一つあげる。 472 $where = 'rank >= ?'; 473 $arrRawSql = array( 474 'rank' => '(rank + 1)', 475 ); 476 $objQuery->update('dtb_category', array(), $where, array($rank), $arrRawSql); 477 } 478 479 $where = 'category_id = ?'; 480 // 自分のレベルを取得する(親のレベル + 1) 481 $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1; 482 483 $arrCategory['create_date'] = 'CURRENT_TIMESTAMP'; 484 $arrCategory['update_date'] = 'CURRENT_TIMESTAMP'; 485 $arrCategory['creator_id'] = $_SESSION['member_id']; 486 $arrCategory['rank'] = $rank; 487 $arrCategory['level'] = $level; 488 $arrCategory['category_id'] = $objQuery->nextVal('dtb_category_category_id'); 489 490 $objQuery->insert('dtb_category', $arrCategory); 491 492 $objQuery->commit(); // トランザクションの終了 493 } 494 495 /** 496 * カテゴリの階層が上限を超えているかを判定する 497 * 498 * @param integer 親カテゴリID 499 * @param 超えている場合 true 500 */ 501 public function isOverLevel($parent_category_id) 502 { 503 $objQuery =& SC_Query_Ex::getSingletonInstance(); 504 $level = $objQuery->get('level', 'dtb_category', 'category_id = ?', array($parent_category_id)); 505 506 return $level >= LEVEL_MAX; 507 } 508 509 // 並びが1つ下のIDを取得する。 510 public function lfGetDownRankID($objQuery, $table, $pid_name, $id_name, $id) 511 { 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 529 return $down_id; 530 } 531 532 // 並びが1つ上のIDを取得する。 533 public function lfGetUpRankID($objQuery, $table, $pid_name, $id_name, $id) 534 { 535 // 親IDを取得する。 536 $col = "$pid_name"; 537 $where = "$id_name = ?"; 538 $pid = $objQuery->get($col, $table, $where, $id); 539 // 全ての子を取得する。 540 $col = "$id_name"; 541 $where = "del_flg = 0 AND $pid_name = ? ORDER BY rank DESC"; 542 $arrRet = $objQuery->select($col, $table, $where, array($pid)); 543 $max = count($arrRet); 544 $up_id = ''; 545 for ($cnt = 0; $cnt < $max; $cnt++) { 546 if ($arrRet[$cnt][$id_name] == $id) { 547 $up_id = $arrRet[($cnt - 1)][$id_name]; 548 break; 549 } 550 } 551 552 return $up_id; 553 } 554 366 555 public function lfCountChilds($objQuery, $table, $pid_name, $id_name, $id) 367 556 {
Note: See TracChangeset
for help on using the changeset viewer.