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

/**
 * ヘッダ関連
 *
 * @package     Onedepo.Logic.Header
 * @author      netnee
 * @access      public
 */
class Onedepo_Logic_Header
{
    var $c_cart;
    var $c_preorder;
    var $c_member;
    var $c_systeminfo;
    var $imgupd;

    function Onedepo_Logic_Header() {
        $renderer =& Smarty4Maple::getInstance();
        $renderer->register_object("header", $this);
    }

    /**
    * ヘッダで使用するのデータ取得
    * 
    * @return   object
    */
    function getHeaderData($params)
    {
        $log =& LogFactory::getLog();

        $returndata = array();
        // ショップ情報
        $returndata["systeminfo"] = $this->getSystemInfo();
        // ショップ画像
        $returndata["systemimg"] = $this->getSystemImg();

        $log->debug(print_r($returndata,true), "");

        if (empty($params["membercd"])) return $returndata;

 
        // ニックネームor姓名
        $returndata["auth"] = $this->getLoginInfo($params["membercd"]);
        // カート
        $returndata["cart"] = $this->getCartInfo($params["membercd"]);
        return $returndata;
    }

    function getSystemInfo() {
        $info = array();
        $s = $this->c_systeminfo->selectAll();
        $info["shopnm"] = (empty($s[0]["shopnm"])) ? SHOP_NAME : $s[0]["shopnm"];
        $info["shopcontent"] = (empty($s[0]["shopcontent"])) ? SHOP_NAME : $s[0]["shopcontent"];

        $info["meta_keywords"] = $s[0]["meta_keywords"];
        $info["meta_description"] = $s[0]["meta_description"];

        // デザイン用CSS
        $info["design_css"] = (empty($s[0]["design_css"])) ? "base_" : $s[0]["design_css"] ."_";
        
        // 許可・届出にデータがあればtrue
        $info["permit_rule"] = (empty($s[0]["permit_rule"]) ? false : true);
        
        return $info;
    }

    function getSystemImg() {
        $img = array();
        $i = $this->imgupd->getImageUrl(5, '', 'shop', '');
        $img["logo"] = (empty($i["shop"]["logo"])) ? SHOP_LOGO : $i["shop"]["logo"];
        $img["main"] = (empty($i["shop"]["main"])) ? SHOP_TOP_IMG : $i["shop"]["main"];
        return $img;
    }


    function getLoginInfo($membercd) {
        $login = array();
        $info = $this->c_member->getMemberData($membercd);

        // アカウント停止
        if (!$info) {
            // ログアウト
            $container =& DIContainerFactory::getContainer();
            $auth =& $container->getComponent("Auth");
            $auth->logout();
            return array();
        }

        if ($info["nicknm"] != "") {
            $login["nickname"] = $info["nicknm"];
        } else {
            $login["nickname"] = $info["lastnm"] . $info["firstnm"];
        }
        $login["memberstate"] = $info["memberstate"];

        return $login;
    }

    function getCartInfo($membercd) {
        $cart = array("totalcount"=>0, "totalprice"=>0);
        $items = $this->c_preorder->selectAll($membercd);

        $log =& LogFactory::getLog();
        $souryou = array();
        foreach ($items as $cartitem) {
            $cart["totalcount"] += intval($cartitem['count']);
            $item = $this -> c_cart -> selectOne($cartitem['itemcd'],
                                                 $cartitem['structcd'],
                                                 $cartitem['stockcd']);
            $price = intval($item['sellingprice']);
            //if($cartitem['takkyubin']==1) $price += intval($item['takkyubinprc']);
            $cart["totalprice"] += intval($cartitem['count']) * $price;

            if (empty($item["sellernm"]) && $item["reserveflg"]==0) { // 在庫
                //$souryou["normal"] = SITE_SOURYOU;
                $souryou["normal"] = 0;
            } else if (empty($item["sellernm"]) && $item["reserveflg"]==1) { // 予約
                //$souryou["reserve"] += SITE_SOURYOU;
                $souryou["reserve"] += 0;
            } else { // 出品
                $souryou[$item["sellercd"]] = 0;
            }
        }
        $cart["totalprice"] += array_sum($souryou);
        return $cart;
    }

}
?>