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


/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

require_once 'Pager.php';

/**
 * 商品詳細
 *
 * @package     Itemdetail
 * @author      netnee <mail@example.com>
 * @access      public
 */
class Itemdetail
{
    var $title;
    var $pagename;

    var $view;
    var $auth;
    var $sidemenu;
    var $review;
    var $tag;
    var $c_mypage;
    var $seller;
    var $image;
    var $itemupdate;
    var $dlitem;
    var $mov;

    var $bodyid;

    var $result;
    var $castwomen;
    var $castdirec;
    var $genre;
    var $tags;
    var $resstock;
    var $img;
    
    var $cd;
    var $scd;
    var $tab;
    var $review_list;
    var $status;
    
    var $from;
    
    
    var $Pager_perPage;
    var $Pager_delta;
    var $Pager_append;
    var $Pager_clearIfVoid;
    var $Pager_linkClass;
    var $Pager_urlVar;
    var $Pager_useSessions;
    var $Pager_closeSession;
    var $Pager_mode;
    var $Pager_prevImg;
    var $Pager_nextImg;
    var $Pager_altPage;
    var $Pager_altPrev;
    var $Pager_altNext;
    var $Pager_altFirst;
    var $Pager_altLast;
    var $Pager_firstPagePre;
    var $Pager_firstPageText;
    var $Pager_firstPagePost;
    var $Pager_lastPagePre;
    var $Pager_lastPageText;
    var $Pager_lastPagePost;
    var $Pager_separator;
    var $Pager_spacesBeforeSeparator;
    var $Pager_spacesAfterSeparator;
    
    //レビュー 出力
    var $review_list_pager;
    var $pagedata;
    var $nowpages;
    var $totalpages;
    var $links;
    
    var $review_cnt;
    var $avg_rank;

    var $data;
    var $linkdata;
    var $selldata;

    var $stock_cnt;

    var $backurl;

    var $topicpath;
    var $gnr;

    var $img_l;
    var $img_m;

    var $dl;

    /**
     * [[機能説明]]
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Itemdetailが実行されました", "Itemdetail#execute");

        $this->result = $this->view->getOneItemForUsersite();
        if (empty($this->result)) return "nodata";

        // 構成コードがない場合は、最低価格を持つ構成を設定
        if (empty($this->scd)) {
            $d = $this->view->getLowprice($this->cd);

            if (!empty($d)) {
                $this->scd = $d["structcd"];
            } else {
                $this->scd = $this->result[0]["structcd"];
                foreach ($this->result as $val) {
                    if ($this->scd >= $val["structcd"]) $this->scd = $val["structcd"];
                }
            }
        }
        if (empty($this->img_l) && empty($this->img_m)) {
            if (is_file(IMAGE_DIR ."/3/". $this->cd ."-". $this->scd . "L.jpg") &&
                is_file(IMAGE_DIR ."/3/". $this->cd ."-". $this->scd . "M.jpg")) {
                $this->img_l = "img.php?c=". $this->cd ."&sc=". $this->scd ."&s=L&d=3";
                $this->img_m = "img.php?c=". $this->cd ."&sc=". $this->scd ."&s=M&d=3";
            } else {
                // 構成情報の画像が登録されていない場合、基本情報の画像を使用
                $this->img_l = "img.php?c=" . $this->cd . "&s=L&d=1";
                $this->img_m = "img.php?c=" . $this->cd . "&s=M&d=1";
            }
        }


        $membercd = $this->auth->getAuthData("membercd");
        if (!empty($membercd)) {
            $this->status = $this->c_mypage->checkStatus($membercd);
        }

        // ジャンル
        $gnr = $this->view->getGenreForItemcd();
        foreach ($gnr[$this->result[0]['itemcd']] as $val) $c[] = $val[0];
        $this->gnr = $c;
        $this->genre = $this->view->getGenreTree($c);
        // タグ
        $this->tags = $this->tag->getPopularTag(NULL, $this->cd);
        
        // データ整形
        $this->makeData();

        // ページ名に作品名を設定
        $this->pagename = $this->data['itemnm'] ." ". $this->data['variationnm'];

        // ダウンロード

        $this->dl = false;
        $dl = $this->dlitem->getDownloadItem($this->cd, $this->scd);
        if ($dl!==FALSE && !is_null($dl)) {
            $this->dl = true;
            // 出品者情報を取得
            $seller = $this->dlitem->getSellerName($this->cd);
            $this->data["seller"]["sellernm"] = $seller['lastnm'] . $seller['firstnm'];
            $this->data["seller"]["sellercd"] = $seller['sellercd'];

            if ($dl["fileexistence"]==="f") {
                $this->data["stock"] = array();
            }
        }
        //--

        /*
        $this->resstock = $this->view->getStockForItemcd('all');
        // 出品/販売数取得
        $this->stock_cnt = count($this->resstock);
        */

        // レビュー
        $this->review_list = $this->review->getItemAllReview($this->cd);
        // レビュー数取得
        $this->review_cnt = count($this->review_list);


        // カウンタ
        $this->view->incrementAccessCount('更新ユーザ');

        // パンくず
        $this->makeTopicPath();

        return 'itemdetail';
    }

    function makeData() {
        if (USE_FUNCTIONAL_EXHIBIT) {
            // 出品
            $this->selldata = $this->view->getStockSellData($this->cd,
                                                            $this->scd);
        }

        $key = 0;
        // 表示する情報
        for ($i = 0; $i < count($this->result); $i++) {
            if ($this->cd==$this->result[$i]["itemcd"] && $this->scd==$this->result[$i]["structcd"]) {

                // メインとなる情報
                $this->data = $this->result[$i];
                // 在庫
                if (USE_FREE_EXHIBIT===TRUE) {
                    //ショップ在庫のみ
                    $this->data["stock"] = $this->view->getStockDetail($this->result[$i]["itemcd"],
                                                                       $this->result[$i]["structcd"],
                                                                       "onedepo");
                } else {
                    $this->data["stock"] = $this->view->getStockDetail($this->result[$i]["itemcd"],
                                                                       $this->result[$i]["structcd"]);

                    foreach ($this->selldata as $value) {
                        if ($value["itemcd"]   == $this->data["stock"]["itemcd"] &&
                            $value["structcd"] == $this->data["stock"]["structcd"] &&
                            $value["stockcd"]  == $this->data["stock"]["stockcd"]) {

                            $this->data["seller"] = $value;
                        }
                    }
                }

            } else {

                // 関連商品
                $d = array();
                $d = $this->result[$i];
                // 在庫
                if (USE_FREE_EXHIBIT===TRUE) {
                    // ショップ在庫のみ
                    $d["stock"] = $this->view->getStockDetail($this->result[$i]["itemcd"],
                                                              $this->result[$i]["structcd"],
                                                              "onedepo");
                } else {
                    $d["stock"] = $this->view->getStockDetail($this->result[$i]["itemcd"],
                                                              $this->result[$i]["structcd"]);
                }
                $this->linkdata[] = $d;
            }
        }
    }
    function getItemexp()
    {
        return $this->data["itemexp"];
    }

    function getReview()
    {
        return $this->review_list_pager;
    }
    
    function getPager()
    {
        return $this->links;
    }

    function getGenreList($params)
    {
        return $this->genre[$params["itemcd"]];
    }
    
    function getStructData($params)
    {
        $struct = $this->view->getStructData($this->cd, $params["structcd"]);
        return $struct;
    }

    function getSellerValCount($params)
    {
        $val = $this->seller->getSellerValCount($params["membercd"]);
        return $val;
    }
    
    // 出品に画像があるかチェック
    function checkSellerImg($params)
    {
        $val = $this->image->selectKey(2, 'o', $params['itemcd'], $params['structcd'], $params['seller'], $params['stockcd']);
        if( isset($val['generalcd']) ) {
            return 1;
        } else {
            return 0;
        }
    }

    // サイドメニュー用データ取得
    function getSideMenuData(){
        return $this->sidemenu->getSideMenuData(NULL, $this->cd);
    }

    function checkExhibit($params) {
        if (strtotime($params["salestartdate"]) <= time()) return true;
        return false;
    }

    function checkDate($params) {
        // 予約中
        if ((strtotime($params["sdate"]) > time() && strtotime($params["odate"]) >= time()))
            return 1;
        // 発売中
        if ((strtotime($params["sdate"]) <= time() && strtotime($params["odate"]) <= time()) ||
            (empty($params["sdate"]) && strtotime($params["odate"]) <= time()))
            return 0;
        // どちらでもない
        return -1;
    }


    function getBakurl() {
        $referer = $_SERVER["HTTP_REFERER"];
        if (!empty($this->backurl)) $referer = $this->backurl;//urldecode($this->backurl);
        if (empty($referer)) return;

        $this->backurl = "";

        $scriptname = htmlspecialchars($_SERVER["SCRIPT_NAME"], ENT_QUOTES);
        $dirname = dirname($_SERVER['SCRIPT_NAME']);
        $dirname = ($dirname=="/") ? "/" : $dirname."/";
        $ptn = "@^(?:".SSL_URL_ROOT.$scriptname."|".URL_ROOT.$scriptname."|";
        $ptn .= SSL_URL_ROOT.$dirname."|".URL_ROOT.$dirname;
        $ptn .= ")@i";
        $f = preg_match($ptn, $referer, $url);

        if (!$f) {
            $this->backurl = URL_ROOT.$scriptname;
        } else {
            $referer = urlencode($referer);
            $this->backurl = urldecode($referer);
        }

        return $this->backurl;
    }
    function getTags() {
        return $this->tags;
    }

    function getBasicOption() {
        return $this->itemupdate->getOptionList(100,200);
    }
    function getStructOption() {
        return $this->itemupdate->getOptionList(200,300);
    }

    // パンくず作成
    function makeTopicPath() {
        $referer = $_SERVER["HTTP_REFERER"];
        preg_match("@^[". URL_ROOT ."|". SSL_URL_ROOT ."](.*?)*$@i", $referer, $qry);
        $q = explode("&", $qry[0]);
        foreach ($q as $v) {
            list($key, $val) = explode("=", $v);
            if ($key=="gnr") {
                if (is_numeric($val)) {
                    $gnr = $val;
                }
            }
            if ($key=="tag") {
                $tag = urldecode($val);
            }
        }
        if (empty($gnr) && empty($tag)) return;


        $dirname = dirname($_SERVER['SCRIPT_NAME']);
        $dirname = ($dirname=="/") ? "" : $dirname;
        $this->topicpath  = '<li><a href="'. URL_ROOT.$dirname .'/">トップ</a></li>';
        $tp  = '<li>&nbsp;&gt;&nbsp;';
        $tp .= '<a href="'. URL_ROOT.$dirname .'/?action=general_itemview_itemlist">商品一覧</a></li>';

        $g = $this->view->getGenreTree(array($gnr));
        // ジャンル
        if (is_array($g)) {
            $tp  = '';
            $g = array_pop($g);
            for ($i = 0; $i < count($g); $i++) {
                // パンくず
                $tp .= '<li>&nbsp;&gt&nbsp;';
                $tp .= '<a href="'. URL_ROOT.$dirname .'/?action=general_itemview_itemlist&gnr='. $g[$i]["genrecd"].'">';
                $tp .= $g[$i]["genrenm"] .'</a></li>';
            }

            $this->topicpath .= $tp;
            $this->topicpath .= '<li class="last">&nbsp;&gt;&nbsp;'. htmlspecialchars($this->data["itemnm"],ENT_QUOTES) .'</li>';

            if (!empty($gnr)) $this->gnr = array($gnr);
            return;
        }

        if (!empty($tag)) {
            // パンくず
            $tp  = '<li>&nbsp;&gt;&nbsp;';
            $tp .= '<a href="'. URL_ROOT.$dirname .'/?action=general_itemview_itemlist&tag='. urlencode($tag) .'">';
            $tp .= 'タグ「'. htmlspecialchars($tag, ENT_QUOTES) .'」</a></li>';
        }
        $this->topicpath .= $tp;
        $this->topicpath .= '<li class="last">&nbsp;&gt;&nbsp;'. htmlspecialchars($this->data["itemnm"],ENT_QUOTES) .'</li>';
    }
    function getTopicPath() {
        return $this->topicpath;
    }

    // 構成情報の画像があるかチェック
    function checkVariationImg($params)
    {
        if (is_file(IMAGE_DIR ."/3/". $params['cd'] ."-". $params['scd'] . "S.jpg")) {
            return "img.php?c=". $params['cd'] ."&sc=". $params['scd'] ."&s=S&d=3";
        } else {
            // 基本情報の画像
            return "img.php?c=". $params['cd'] ."&s=S&d=1";
        }
    }

    // 商品紹介用動画
    function getItemMovie($params)
    {
        $html = '';
        if (!empty($params["cd"]) && !empty($params["sc"])) {
            $movfile = $this->mov->getMovieUrl($params["cd"], $params["sc"]);

            if (!empty($movfile)) {
                $html  = '<div id="flvplayer"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>';
                $html .= '<script type="text/javascript" src="js/swfobject.js"></script>';
                $html .= '<script type="text/javascript">';
                $html .= 'var s1 = new SWFObject("swf/mediaplayer.swf","mediaplayer","350","280","8");';
                $html .= 's1.addParam("allowfullscreen","true");';
                $html .= 's1.addVariable("width","350");';
                $html .= 's1.addVariable("height","280");';
                $html .= 's1.addVariable("lightcolor", "0x00CCCC");';
                $html .= 's1.addVariable("backcolor", "0x888888");';
                $html .= 's1.addVariable("frontcolor", "0xFFFFFF");';
                $html .= 's1.addVariable("file","'. $movfile .'");';
                $html .= 's1.write("flvplayer");';
                $html .= '</script>';
            }
        }
        return $html;
    }
}
?>
