Changeset 15295 for branches/feature-module-update
- Timestamp:
- 2007/08/16 19:44:58 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/feature-module-update/data/class/helper/SC_Helper_DB.php
r15279 r15295 442 442 return $arrRet; 443 443 } 444 445 /** 446 * ランキングを上げる. 447 * 448 * @param string $table テーブル名 449 * @param string $colname カラム名 450 * @param string|integer $id テーブルのキー 451 * @param string $andwhere SQL の AND 条件である WHERE 句 452 * @return void 453 */ 454 function sfRankUp($table, $colname, $id, $andwhere = "") { 455 $objQuery = new SC_Query(); 456 $objQuery->begin(); 457 $where = "$colname = ?"; 458 if($andwhere != "") { 459 $where.= " AND $andwhere"; 460 } 461 // 対象項目のランクを取得 462 $rank = $objQuery->get($table, "rank", $where, array($id)); 463 // ランクの最大値を取得 464 $maxrank = $objQuery->max($table, "rank", $andwhere); 465 // ランクが最大値よりも小さい場合に実行する。 466 if($rank < $maxrank) { 467 // ランクが一つ上のIDを取得する。 468 $where = "rank = ?"; 469 if($andwhere != "") { 470 $where.= " AND $andwhere"; 471 } 472 $uprank = $rank + 1; 473 $up_id = $objQuery->get($table, $colname, $where, array($uprank)); 474 // ランク入れ替えの実行 475 $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?"; 476 $objQuery->exec($sqlup, array($rank + 1, $id)); 477 $objQuery->exec($sqlup, array($rank, $up_id)); 478 } 479 $objQuery->commit(); 480 } 481 482 /** 483 * ランキングを下げる. 484 * 485 * @param string $table テーブル名 486 * @param string $colname カラム名 487 * @param string|integer $id テーブルのキー 488 * @param string $andwhere SQL の AND 条件である WHERE 句 489 * @return void 490 */ 491 function sfRankDown($table, $colname, $id, $andwhere = "") { 492 $objQuery = new SC_Query(); 493 $objQuery->begin(); 494 $where = "$colname = ?"; 495 if($andwhere != "") { 496 $where.= " AND $andwhere"; 497 } 498 // 対象項目のランクを取得 499 $rank = $objQuery->get($table, "rank", $where, array($id)); 500 501 // ランクが1(最小値)よりも大きい場合に実行する。 502 if($rank > 1) { 503 // ランクが一つ下のIDを取得する。 504 $where = "rank = ?"; 505 if($andwhere != "") { 506 $where.= " AND $andwhere"; 507 } 508 $downrank = $rank - 1; 509 $down_id = $objQuery->get($table, $colname, $where, array($downrank)); 510 // ランク入れ替えの実行 511 $sqlup = "UPDATE $table SET rank = ?, update_date = Now() WHERE $colname = ?"; 512 $objQuery->exec($sqlup, array($rank - 1, $id)); 513 $objQuery->exec($sqlup, array($rank, $down_id)); 514 } 515 $objQuery->commit(); 516 } 517 518 /** 519 * 指定順位へ移動する. 520 * 521 * @param string $tableName テーブル名 522 * @param string $keyIdColumn キーを保持するカラム名 523 * @param string|integer $keyId キーの値 524 * @param integer $pos 指定順位 525 * @param string $where SQL の AND 条件である WHERE 句 526 * @return void 527 */ 528 function sfMoveRank($tableName, $keyIdColumn, $keyId, $pos, $where = "") { 529 $objQuery = new SC_Query(); 530 $objQuery->begin(); 531 532 // 自身のランクを取得する 533 $rank = $objQuery->get($tableName, "rank", "$keyIdColumn = ?", array($keyId)); 534 $max = $objQuery->max($tableName, "rank", $where); 535 536 // 値の調整(逆順) 537 if($pos > $max) { 538 $position = 1; 539 } else if($pos < 1) { 540 $position = $max; 541 } else { 542 $position = $max - $pos + 1; 543 } 544 545 if( $position > $rank ) $term = "rank - 1"; //入れ替え先の順位が入れ換え元の順位より大きい場合 546 if( $position < $rank ) $term = "rank + 1"; //入れ替え先の順位が入れ換え元の順位より小さい場合 547 548 // 指定した順位の商品から移動させる商品までのrankを1つずらす 549 $sql = "UPDATE $tableName SET rank = $term, update_date = NOW() WHERE rank BETWEEN ? AND ? AND del_flg = 0"; 550 if($where != "") { 551 $sql.= " AND $where"; 552 } 553 554 if( $position > $rank ) $objQuery->exec( $sql, array( $rank + 1, $position )); 555 if( $position < $rank ) $objQuery->exec( $sql, array( $position, $rank - 1 )); 556 557 // 指定した順位へrankを書き換える。 558 $sql = "UPDATE $tableName SET rank = ?, update_date = NOW() WHERE $keyIdColumn = ? AND del_flg = 0 "; 559 if($where != "") { 560 $sql.= " AND $where"; 561 } 562 563 $objQuery->exec( $sql, array( $position, $keyId ) ); 564 $objQuery->commit(); 565 } 566 567 /** 568 * ランクを含むレコードを削除する. 569 * 570 * レコードごと削除する場合は、$deleteをtrueにする 571 * 572 * @param string $table テーブル名 573 * @param string $colname カラム名 574 * @param string|integer $id テーブルのキー 575 * @param string $andwhere SQL の AND 条件である WHERE 句 576 * @param bool $delete レコードごと削除する場合 true, 577 * レコードごと削除しない場合 false 578 * @return void 579 */ 580 function sfDeleteRankRecord($table, $colname, $id, $andwhere = "", 581 $delete = false) { 582 $objQuery = new SC_Query(); 583 $objQuery->begin(); 584 // 削除レコードのランクを取得する。 585 $where = "$colname = ?"; 586 if($andwhere != "") { 587 $where.= " AND $andwhere"; 588 } 589 $rank = $objQuery->get($table, "rank", $where, array($id)); 590 591 if(!$delete) { 592 // ランクを最下位にする、DELフラグON 593 $sqlup = "UPDATE $table SET rank = 0, del_flg = 1, update_date = Now() "; 594 $sqlup.= "WHERE $colname = ?"; 595 // UPDATEの実行 596 $objQuery->exec($sqlup, array($id)); 597 } else { 598 $objQuery->delete($table, "$colname = ?", array($id)); 599 } 600 601 // 追加レコードのランクより上のレコードを一つずらす。 602 $where = "rank > ?"; 603 if($andwhere != "") { 604 $where.= " AND $andwhere"; 605 } 606 $sqlup = "UPDATE $table SET rank = (rank - 1) WHERE $where"; 607 $objQuery->exec($sqlup, array($rank)); 608 $objQuery->commit(); 609 } 610 611 /** 612 * レコードの存在チェックを行う. 613 * 614 * @param string $table テーブル名 615 * @param string $col カラム名 616 * @param array $arrval 要素の配列 617 * @param array $addwhere SQL の AND 条件である WHERE 句 618 * @return bool レコードが存在する場合 true 619 */ 620 function sfIsRecord($table, $col, $arrval, $addwhere = "") { 621 $objQuery = new SC_Query(); 622 $arrCol = split("[, ]", $col); 623 624 $where = "del_flg = 0"; 625 626 if($addwhere != "") { 627 $where.= " AND $addwhere"; 628 } 629 630 foreach($arrCol as $val) { 631 if($val != "") { 632 if($where == "") { 633 $where = "$val = ?"; 634 } else { 635 $where.= " AND $val = ?"; 636 } 637 } 638 } 639 $ret = $objQuery->get($table, $col, $where, $arrval); 640 641 if($ret != "") { 642 return true; 643 } 644 return false; 645 } 444 646 } 445 647 ?>
Note: See TracChangeset
for help on using the changeset viewer.