Ignore:
Timestamp:
2014/05/21 12:33:38 (10 years ago)
Author:
pineray
Message:

#2554 カテゴリーの表示順を上下させる処理を SC_Helper_Category へ移動

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_13-dev/data/class/helper/SC_Helper_Category.php

    r23435 r23436  
    105105     * カテゴリーツリーの取得. 
    106106     * 
    107      * @return type 
     107     * @return array 
    108108     */ 
    109109    public function getTree() 
     
    139139     * @return array 
    140140     */ 
    141     public function getTreeBranch($category_id) { 
     141    public function getTreeBranch($category_id) 
     142    { 
    142143        $arrTree = $this->getTree(); 
    143144        $arrTrail = $this->getTreeTrail($category_id, true); 
     
    163164     * @return void 
    164165     */ 
    165     public function delete($category_id) { 
     166    public function delete($category_id) 
     167    { 
    166168        $objDb = new SC_Helper_DB_Ex(); 
    167169        // ランク付きレコードの削除(※処理負荷を考慮してレコードごと削除する。) 
    168170        $objDb->sfDeleteRankRecord('dtb_category', 'category_id', $category_id, '', true); 
    169171    } 
     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    } 
    170321} 
Note: See TracChangeset for help on using the changeset viewer.