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

/**
 * 商品検索2 (出品用)
 *
 * @package     itemview2
 * @author      netnee S.Oguma <shinichiro.oguma@netnee.jp>
 * @access      public
 */
class Onedepo_Logic_Itemview2 {

    var $item;
    var $name;
    var $struct;
    var $access;
    var $stock;
    var $tag;

    var $jancode;
    var $hinban;
    var $itemtitle;
    var $media;
    
    var $cd;
    var $structcd;

    var $data;
    
    var $from;


    function getSearchResults($to,$genre_select)
    {
        $where = array();
        $params = array();

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview2#getSearchResults");
        

        // AND検索の為に半角空白で分割
        $this->itemtitle = mb_convert_kana($this->itemtitle, "s", "UTF-8");
        $title = explode(" ", $this->itemtitle);
        $this->cast = mb_convert_kana($this->cast, "s", "UTF-8");
        $cast = explode(" ", $this->cast);

        // WHERE句で「=」を使うもの
        $eq = array("jancd"         => $this->jancode, 
                    "makercd"       => $this->maker_name, 
                    "mediacd"       => $this->media);
        $this->makeEqualParams($eq, $where, $params);
        //--

        // LIKEを使うもの
        $like = array("makeritemcd" => $this->hinban, 
                      "itemnm"      => $title);
        $this->makeLikeParams($like, $where, $params);
        //--

        $cg_itemcd = array();
        if (!empty($cast_where)) {

            $cg_itemcd[] = $this->tcast->getItemcdForCast($cast_where, $cast_params);
            
        }
        
        
        // ジャンル
//        $g_itemcd = array();
        if (!empty($genre_select)) {
            $gnr_w = $gnr_p = array();
            $gnr_cnt = 0;
            foreach($genre_select as $val){
                if ($gnr_cnt++ == 0){
                    $gnr_w[] = " genrecd = ? ";
                    $gnr_p[] = $val;
                }else{
                    $gnr_w[] = " AND itemcd IN (SELECT DISTINCT itemcd FROM t_genre_item WHERE genrecd = ? ) ";
                    $gnr_p[] = $val;
                }
            }
            if (!empty($gnr_w)) {
                $cg_itemcd[] = $this->genre->getItemcdForGenre($gnr_w, $gnr_p);
            }
        }
        
        //--
        //$log->debug("ICD : ". print_r($cg_itemcd, true), "===== getSearchResults =====");
        if (!empty($cg_itemcd)) {
            $intersect = $cg_itemcd[0]; // 基準
            //$log->debug("itemcd = ". print_r($itemcd, true), "Onedepo_Logic_Itemview#getNewitemList");
            if (count($cg_itemcd) > 1) { // 重複するコードのみ
                foreach ($cg_itemcd as $val) {
                    $intersect = array_intersect($intersect, $val);
                }
            }
            //$log->debug("intersect = ". print_r($intersect, true), "Onedepo_Logic_Itemview#getNewitemList");
            //$log->debug("ICT : ". print_r($intersect, true), "===== getSearchResults =====");
            $intersect = implode(",", $intersect);

            if (!empty($intersect)) {
                $where[] = "itemcd in (!)";
                $params[] = $intersect;
            } else {
                $where[] = "itemcd in (!)";
                $params[] = "0";
            }
        }

        if ($this->situation=="none") $where[] = "item_web_attr_updcnt IS NULL";
        if ($this->photo_situation=="is") {
            $where[] .= "image_count > 0";
        }
        if ($this->photo_situation=="none") {
            $where[] .= "image_count = 0";
        }
        if ($this->publish_situation=="none") {
            $where[] .= "(releasedate > ? OR releasedate IS NULL)";
            $params[] = date('Y-m-d');
        }
        if ($this->publish_situation=="is") {
            $where[] .= "releasedate <= ?";
            $params[] = date('Y-m-d');
        }


        if ($this->sort == "title")         $sortkey = "itemnm";
        else if ($this->sort == "release")  $sortkey = "salestartdate desc";
        else if ($this->sort == "publish")  $sortkey = "releasedate desc";
        else                                $sortkey = "makernm, makeritemcd";

        
        if (!is_numeric($this->from)) {
        $this->from = 1;
        }
        $from = ($this->from-1)*$to;

        $this->data = $this->item->getSearchResults($where, 
                                                    $params, 
                                                    $sortkey,
                                                    $to,
                                                    $from);
        return $this->data;
    }

    function getNewitemList($sort, $to, $from, $q="", $parameters=array(), $tag="")
    {
        $where = array();
        $params = array();
        $itemcd = array();

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview2#getNewitemList");

        $i = 0;

        // 選択されたタグ
        if ($tag != "") {
            $c = $this->tag->getItemcdForTagnm($tag);
            $c = implode(",", $c);
            $where[$i][] = "itemcd in (!)";
            $params[] = $c;
            $i++;
        } else if ($q != "") {
            $qry = "(";

            // AND検索の為に半角空白で分割
            $q = mb_convert_kana($q, "s", "UTF-8");
            $q_arr = explode(" ", $q);
            //--

            // キャスト
            $cast = array("castnm" => $q_arr);
            $this->makeLikeParams($cast, $cast_where, $cast_params);
            //--

            if (!empty($cast_where)) {
                $itemcd[] = $this->tcast->getItemcdForCast($cast_where, $cast_params);
            }
            if (!empty($itemcd[0])) {
                $itemcd = implode(",", $itemcd[0]);
                $qry .= "itemcd in (!) OR ";
                $params[] = $itemcd;
            }
            $itemcd = array();

            // LIKEを使うもの
            $like = array("itemnm"      => $q_arr,
                          "itemnmkana"  => $q_arr,
                          "subtitle"    => $q_arr,
                          "makernm"     => $q_arr,
                          "labelnm"     => $q_arr,
                          "iteminfo"    => $q_arr,
                          "comment"     => $q_arr);
            $j = 0;
            foreach ($like as $key => $val) {
                if(!empty($val)) {
                    if (is_array($val)) {
                        foreach ($val as $k => $v) {
                            if (!empty($v)) {
                                if ($j > 0) $qry .= " OR ";
                                $qry .= $key ." LIKE ?";
                                $params[] = '%'. $v .'%';
                            }
                        }
                    } else {
                        if ($j > 0) $qry .= " OR ";
                        $qry .=  $key ." LIKE ?";
                        $params[] = '%'. $val .'%';
                    }
                }
                $j++;
            }
            $qry .= ")";
            $where[$i][] = $qry;
            $i++;
        }

        /**
         * 絞り込み
         *
         */
        if (!empty($parameters)) {

        // 種別
        $qy1 = $qy2 = $qy3 = array();
        // DVD
        if (!empty($parameters["etc"]["dvd"])) {
            $qy1[] = " dvd_count > 0";
        }
        // VHS
        if (!empty($parameters["etc"]["vhs"])) {
            $qy1[] = " vhs_count > 0";
        }
        //--
        $re1 = implode (' or', $qy1);
        if (!empty($re1)) $where[$i][] = "(".$re1.")";

        }
        /**
         */

        if (!empty($itemcd)) {
            $intersect = $itemcd[0]; // 基準
            //$log->debug("itemcd = ". print_r($itemcd, true), "Onedepo_Logic_Itemview#getNewitemList");
            if (count($itemcd) > 1) { // 重複するコードのみ
                foreach ($itemcd as $val) {
                    $intersect = array_intersect($intersect, $val);
                }
            }
            //$log->debug("intersect = ". print_r($intersect, true), "Onedepo_Logic_Itemview#getNewitemList");
            
            $intersect = implode(",", $intersect);

            if (!empty($intersect)) {
                $where[$i][] = "itemcd in (!)";
                $params[] = $intersect;
            } else {
                return array();
            }
            $i++;
        }


        $sortkey = "salestartdate desc";
        if ($sort == "rank")          $sortkey = "rankingrate desc";
        else if ($sort == "-release") $sortkey = "salestartdate";
        else if ($sort == "release")  $sortkey = "salestartdate desc";

        $this->data = $this->item->getNewitemList($to,
                                                  $from,
                                                  $where, 
                                                  $params, 
                                                  $sortkey);
        return $this->data;
    }

    function getOneItem()
    {
        $this->data = $this->item->getOneItem($this->cd);
        return $this->data;
    }
    
    function getOneItemRow()
    {
        $this->data = $this->item->getOneItemRow($this->cd);
        return $this->data;
    }

    function getOneItemForUsersite()
    {
        $where = " AND image_count > 0 AND releasedate <= CURRENT_DATE";
        $this->data = $this->item->getOneItem($this->cd, $where);
        return $this->data;
    }
    
    function getOneStruct()
    {
        $this->data = $this->struct->getOneItem($this->cd, $this->structcd);
        return $this->data;
    }
    
    function getOneItemOneStruct()
    {
        $this->data = $this->struct->getOneItemOneStruct($this->cd, $this->structcd);
        return $this->data;
    }
    

    /**
     * = を使用するクエリ
     *
     */
    function makeEqualParams($basearr, &$where, &$params)
    {
        foreach ($basearr as $key => $val) {
            if(!empty($val)) {
                if (is_array($val)) {

                    foreach ($val as $v) {
                        $where[] = $key ." = ?";
                        $params[] = $v;
                    }

                } else {
                    $where[] = $key ." = ?";
                    $params[] = $val;
                }
            }
        }
    }

    /**
     * LIKE 部分一致
     *
     */
    function makeLikeParams($basearr, &$where, &$params)
    {
        foreach ($basearr as $key => $val) {
            if(!empty($val)) {
                if (is_array($val)) {

                    foreach ($val as $v) {
                        if (!empty($v)) {
                            $where[] = $key ." LIKE ?";
                            $params[] = '%'. $v .'%';
                        }
                    }

                } else {
                    $where[] = $key ." LIKE ?";
                    $params[] = '%'. $val .'%';
                }
            }
        }
    }

    function getCastnmForItemcd()
    {
        $where = array();
        $params = array();
        $cd = "";

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview#getCastnmForItemcd");

        $itemcd = array();
        foreach ($this->data as $key => $val) 
        {
            if (!empty($val["itemcd"]))
                $itemcd[] = $val["itemcd"];
        }
        $cd = implode(",", $itemcd);

        //$log->debug($cd, "Onedepo_Logic_Itemview#getCastnmForItemcd");
        if (empty($cd)) return;

        $list = $this->tcast->getCastnmForItemcd($cd);

        $castnm = array();
        foreach ($list as $key => $val)
        {
            $castnm[$key] = implode(" ", $val);
        }
        return $castnm;
    }

    function getCastListForItemcd()
    {
        $where = array();
        $params = array();
        $cd = "";

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview#getCastListForItemcd");

        $itemcd = array();
        foreach ($this->data as $key => $val) 
        {
            if (!empty($val["itemcd"]))
                $itemcd[] = $val["itemcd"];
        }
        $cd = implode(",", $itemcd);

        //$log->debug($cd, "Onedepo_Logic_Itemview#getCastListForItemcd");
        if (empty($cd)) return;

        $list = $this->tcast->getCastListForItemcd($cd);

        $cast = array();
        foreach ($list as $key => $val)
        {
            $cast[$key] = $val;
        }
        return $cast;
    }

    function getCastForItemcd($itemcd)
    {
        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview#getCastnmForItemcd");

        return $this->tcast->getCastForItemcd($itemcd);;
    }

    function getGenrenmForItemcd()
    {
        $where = array();
        $params = array();
        $cd = "";

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview#getGenrenmForItemcd");

        $itemcd = array();
        foreach ($this->data as $key => $val) 
        {
            if (!empty($val["itemcd"]))
                $itemcd[] = $val["itemcd"];
        }
        $cd = implode(",", $itemcd);

        //$log->debug($cd, "Onedepo_Logic_Itemview#getGenrenmForItemcd");
        if (empty($cd)) return;

        $list = $this->genre->getGenrenmForItemcd($cd);

        $genrenm = array();
        foreach ($list as $key => $val)
        {
            $genrenm[$key] = implode(" ", $val);
        }
        return $genrenm;
    }

    function getGenreForItemcd()
    {
        $where = array();
        $params = array();
        $cd = "";

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview#getGenreForItemcd");

        $itemcd = array();
        foreach ($this->data as $key => $val) 
        {
            if (!empty($val["itemcd"]))
                $itemcd[] = $val["itemcd"];
        }
        $cd = implode(",", $itemcd);

        //$log->debug($cd, "Onedepo_Logic_Itemview#getGenrenmForItemcd");
        if (empty($cd)) return;

        $list = $this->genre->getGenreForItemcd($cd);

        $genre = array();
        foreach ($list as $key => $val)
        {
            $genre[$key] = $val;
        }
        return $genre;
    }

    function getItemImageForItemcd()
    {
        $where = array();
        $params = array();
        $cd = "";

        $log =& LogFactory::getLog();
        $log->debug("debug", "Onedepo_Logic_Itemview#getImageForItemcd");

        $itemcd = array();
        foreach ($this->data as $key => $val) 
        {
            if (!empty($val["itemcd"]))
                $itemcd[] = $val["itemcd"];
        }
        $cd = implode(",", $itemcd);

        //$log->debug($cd, "Onedepo_Logic_Itemview#getImageForItemcd");
        if (empty($cd)) return;

        $list = $this->image->getItemImageForItemcd($cd);

        $imagenm = array();
        foreach ($list as $key => $val) {
            foreach ($val as $imgsize) {
                if (is_file(IMAGE_DIR ."/1/". $key . $imgsize . ".jpg"))
                    $imagenm[$key][$imgsize] = "img.php?c=". $key ."&s=". $imgsize ."&d=1";
            }
        }
        return $imagenm;
    }
    
    function getStockForItemcd()
    {
        $where = " ORDER BY sellingprice ASC";
        $data = $this->stock->getStockSelectForItemcd($this->cd, $where);
        return $data;
    }
    
    function getMediacd($itemcd, $structcd)
    {
        $this->data = $this->struct->getOneItem($itemcd, $structcd);
        return $this->data[0]["mediacd"];
    }
    

}
?>