<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */

/**
 * サイドメニュー関連
 *
 * @package     Onedepo.Logic.Sidemenu
 * @author      netnee <mail@example.com>
 * @access      public
 */
class Onedepo_Logic_Sidemenu
{
    var $genre;
    var $search;
    var $tag;
    var $itembasic;
    var $actionChain;
    
    function Onedepo_Logic_Sidemenu() {
        $renderer =& Smarty4Maple::getInstance();
        $renderer->register_object("sidemenu", $this);
    }

    
    /**
    * サイドメニュー用データ取得
    * 
    * @return   object
    */
    function getSideMenuData($params)
    {
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Sidemenu#getSideNavi");
        
        $returndata = array();

        // ジャンル
        $genre = $this->makeGenreTree($params["genrecd"]);
        $returndata["genre"] = $genre;

        // キーワード
        $returndata["keyword"] = $this->search->selectPopularKeyword(NULL);

        // タグ
        $params["itemcd"] = (!empty($params["itemcd"])) ? $params["itemcd"] : NULL;
        $returndata["tag"] = $this->makePopularTag(NULL, $params["itemcd"]);

        // ランキング
        // 商品ランキング
        if ($this->actionChain->getCurActionName()!="itemdetail") {
            // ジャンルデータ
            $returndata["rank_gnr"] = $this->genre->selectKey(NULL, $params["genrecd"][0]);
            $returndata["i_rank"] = $this->getItemRanking($params["genrecd"], $genre);
        } else {
            $returndata["i_rank"] = $this->getItemRanking();
        }


        return $returndata;
    }

    function makeGenreTree($cd="") {
        $log =& LogFactory::getLog();

        $g = $this->genre->selectGenreRelationship();

        $c = "";
        $tree = array();
        $this->genre->makeTree($g, $c, $tree, true);

        if (!empty($cd)) {
            $data = $tree;
            $del = true;
            //$log->error(print_r($tree, true), "");
            $this->genre->selectGenreOnly($data, $cd, $tree, $del, true);
            //$log->error(print_r($tree, true), "");
        }

        return $tree;
    }

    function makePopularTag($shopdiv, $itemcd) {
        $tags = $this->tag->getPopularTag($shopdiv, $itemcd);
        $log =& LogFactory::getLog();
        //$log->debug(print_r($tags, true), "Onedepo_Logic_Sidemenu#makePopularTag");

        $fontsize_constant = "FONTSIZE_PERCENT_";
        $rank_split = ceil(POPULAR_TAG_LIMIT / 8); // 注目タグ表示件数 / 8(フォントサイズのランク数)
        $rank = 1;
        for ($i = 0; $i < count($tags); $i++) {
            $tags[$i]["style"] = "font-size:". 
                                 constant($fontsize_constant . $rank) . "%;";
            if (($i+1) % $rank_split == 0) $rank++;
        }
        shuffle($tags);

        return $tags;
    }


    function getCd($params)
    {
        $c = (strlen($params["itemcd"])>0) ? $params["itemcd"] : NULL;
        $p = (strlen($params["sellingprice"])>0) ? $params["sellingprice"] : NULL;
        return $this->itembasic->getCartCd($c,$p);
    }


    function getItemRanking($genrecd="", $genrelist="") {
        //$log =& LogFactory::getLog();
        if (!empty($genrecd) && count($genrecd)<2) {
            foreach ($genrelist as $key => $val) {
                $c[] = $val["genrecd"];
                if (!empty($val["child"])) {
                    $r = $this->getGenrecd($genrecd, $val["child"], $c);
                }
                if ($r===false) {
                    break;
                }
            }
        }
        //$log->error(print_r($c, true), "");

        return $this->itembasic->getItemRanking($c);
    }
    function getGenrecd($genrecd="", $genrelist, &$c) {
        $r = true;
        foreach ($genrelist as $key => $val) {
            $c[] = $val["genrecd"];
            if (!empty($val["child"])) {
                $r = $this->getGenrecd($genrecd, $val["child"], $c);
            } else {
                if (in_array($val["genrecd"], $genrecd)) {
                    $c = array($val["genrecd"]);
                    return false;
                }
            }
            if ($r===false) {
                return false;
            }
        }
    }
}
?>