<?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     General.Itemview.Itemlist
 * @author      netnee T.Otogawa <tomokazu.otogawa@netnee.jp>
 * @access      public
 */
class General_Itemview_Itemlist
{
    var $title;
    var $pagename;

    /**
     * Pager設定
     */
    var $Pager_perPage_img_s;
    var $Pager_perPage_img;
    var $Pager_perPage_list;
    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 $Pager_curPageSpanPre;
    var $Pager_curPageSpanPost;

    var $img_s_line;
    var $row;


    var $view;
    var $auth;
    var $itemsearch;
    var $t_tag;
    var $session;

    var $bodyid;

    var $results;
    var $genre;
    var $sellingprice;
    var $exhibitcount;
    var $reserveflg;
    var $img;
    var $pagedata;
    var $nowpages;
    var $totalpages;
    var $links;
    var $rc;
    var $from;

    var $start;
    var $end;

    var $list;
    var $sort;
    var $gnr;
    var $etc;
    var $tag;
    var $s;
    var $t;
    var $d;

    var $q_select;
    var $q;


    var $max_count;
    var $tpl;

    var $topicpath;
    var $pageindex;
    var $pageindeximg;


    var $itemnm;
    var $mkrcd;
    var $jancd;
    var $itemexp;

    /**
     * execute
     *
     * @access  public
     */
    function execute()
    {
        $log =& LogFactory::getLog();
        $func_name = "General_Itemview_Itemlist#execute";
        $log->debug("General_Itemview_Itemlist が実行されました", $func_name);

        // ページタイトル　トップページなら
        if ($_SERVER["QUERY_STRING"] == "") {
            $this->title = "";
            $this->pagename = "";
        }


        if ($this->list != "") {
            $this->session->setParameter("view", array("list"=> $this->list));
        }
        $sess_view = $this->session->getParameter("view");
        if ($sess_view["list"] == "list") {
            $this->list = "list";
        } else if ($sess_view["list"] == "img_s") {
            $this->list = "img_s";
        } else {
            $this->list = "img";
        }

        // １ページの行数
        if ($this->list == 'list') {
            $perPage = $this->Pager_perPage_list;
        } else if ($this->list == "img") {
            $perPage = $this->Pager_perPage_img;
        } else {
            $perPage = $this->Pager_perPage_img_s;
        }

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


        if (is_array($this->gnr)) {
            foreach($this->gnr as $val) if(!is_numeric($val)) $this->gnr = array();
        } else if (!is_numeric($this->gnr) || empty($this->gnr)) {
            $this->gnr = array();
        } else {
            $this->gnr = array($this->gnr);
        }

        // メニューから絞り込みをしていたら、ページタイトルに追加
        $this->makePageTitleANDTopicPath();
        //--

        if (!isset($this->t)) {
            $this->t = 'alldata';
        }

        if (!isset($this->s)) {
            $this->s = 'stk';
        }
        if (!isset($this->d)) {
            $this->d = 'all';
        }
        if (empty($this->etc)) {
            $this->etc["type"] = $this->t;
            $this->etc["stock"] = $this->s;
            $this->etc["saledate"] = $this->d;
        }

        if (!isset($this->sort)) {
            $this->sort = "-udate";
        }

        if (!isset($this->q_select)) {
            $this->q_select = "all";
        }


        // 詳細検索用
        if (!isset($this->itemnm)) {
            $this->itemnm = NULL;
        }
        if (!isset($this->mkrcd)) {
            $this->mkrcd = NULL;
        }
        if (!isset($this->jancd)) {
            $this->jancd = NULL;
        }
        if (!isset($this->itemexp)) {
            $this->itemexp = NULL;
        }
        //--

        $parameters = array("genre"       => $this->gnr,
                            "etc"         => $this->etc,
                            // 詳細検索用
                            "itemnm"      => $this->itemnm,
                            "makeritemcd" => $this->mkrcd,
                            "jancd"       => $this->jancd,
                            "itemexp"     => $this->itemexp
                           );

        $this->results = $this->view->getNewitemList($this->sort,
                                                     $perPage,
                                                     $from,
                                                     $this->q,
                                                     $parameters,
                                                     $this->tag,
                                                     $this->q_select,
                                                     $this->max_count);

        if ($this->results["recordcount"] > 0) {
            $this->itemsearch->SearchKeyInsert(NULL, NULL, NULL, $this->q, '');
        }
        // レコードカウント
        $this->rc = ($this->results["recordcount"] > 0) ? $this->results["recordcount"] : 0;
        unset($this->results["recordcount"]);


        // Pager設定
        $pager_ini = array(
            'totalItems'            => $this->rc,
            'perPage'               => $perPage,                 // 1ページ表示件数
            'delta'                 => $this->Pager_delta,       // 表示ページから、前後に表示するページ数
            'append'                => $this->Pager_append,
            'clearIfVoid'           => $this->Pager_clearIfVoid,
            'linkClass'             => $this->Pager_linkClass,
            'urlVar'                => $this->Pager_urlVar,
            'useSessions'           => $this->Pager_useSessions,
            'closeSession'          => $this->Pager_closeSession,
            'mode'                  => $this->Pager_mode,
            'prevImg'               => $this->Pager_prevImg,
            'nextImg'               => $this->Pager_nextImg,
            'altPage'               => $this->Pager_altPage,
            'altPrev'               => $this->Pager_altPrev,
            'altNext'               => $this->Pager_altNext,
            'altFirst'              => $this->Pager_altFirst,
            'altLast'               => $this->Pager_altLast,
            'firstPagePre'          => $this->Pager_firstPagePre,
            'firstPageText'         => $this->Pager_firstPageText,
            'firstPagePost'         => $this->Pager_firstPagePost,
            'lastPagePre'           => $this->Pager_lastPagePre,
            'lastPageText'          => $this->Pager_lastPageText,
            'lastPagePost'          => $this->Pager_lastPagePost,
            'separator'             => $this->Pager_separator,
            'spacesBeforeSeparator' => $this->Pager_spacesBeforeSeparator,
            'spacesAfterSeparator'  => $this->Pager_spacesAfterSeparator,
            'curPageSpanPre'        => $this->Pager_curPageSpanPre,
            'curPageSpanPost'       => $this->Pager_curPageSpanPost
        );

        // Pager出力
        $pager =& Pager::factory($pager_ini);
        $this->nowpages = $pager->getCurrentPageID();
        $this->totalpages = $pager->numPages();
        $this->links = $pager->getLinks();
        $this->pagedata = $pager->getPageData();


        $row = ceil(($this->rc-$from) / $this->img_s_line);
        $max_r = ($perPage/$this->img_s_line);
        $this->row = ($max_r<$row) ? $max_r : $row;

        // 表示件数 ○～○件
        $this->start = $from+1;
        $this->end = ($this->rc < ($from+$perPage)) ? $this->rc : ($from+$perPage);

        if ($this->list == 'list') {
            $gnr = $this->view->getGenreForItemcd();
            foreach ($gnr as $key => $val) {
                foreach ($val as $v) {
                    $c = array($v[0]); // ジャンルコード取得
                    if (is_array($tmp["$c"])) { // 既に同じ物が保管されている
                        $g = $tmp["$c"];
                    } else {
                        // ジャンルCから親を探す
                        $g = $this->view->getGenreTree($c);
                        $tmp["$c"] = $g;    // ジャンルを一時的に保管
                    }
                    $this->genre[$key][] = array_pop($g);
                }
            }

            $this->reserveflg = $this->view->getReserveflgForItemcd();
        }

        return 'list';
    }

    function getLink()
    {
        return $this->links;
    }

    function getGenreList($params)
    {
        return $this->genre[$params["itemcd"]];
    }
    function getSellingpriceList($params)
    {
        return $this->sellingprice[$params["itemcd"]][0];
    }
    function getExhibitcountList($params)
    {
        return $this->exhibitcount[$params["itemcd"]][0];
    }
    function getReserveflgList($params)
    {
        return $this->reserveflg[$params["itemcd"]][0];
    }
    function getCd($params)
    {
        return $this->view->getCd2($params["itemcd"],$params["sellingprice"]);
    }



    // 文字列 str を文字数 lmt 以下に丸める
    function strTrimWidth($params){
        return mb_strimwidth($params["str"], 0, $params["lmt"], " …", "UTF-8");
    }


    /**
     * 絞り込み用
     */
    function getSearchGenre() {
        return $this->view->getSearchGenre($this->gnr);
    }


    function getItemTagList($params)
    {
        return $this->t_tag->getItemTag($params["itemcd"]);
    }

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

        $g = $this->view->getGenreTree($this->gnr);
        // ジャンル
        if (is_array($g)) {
            $pn = "";
            $tp = '<li>&nbsp;&gt&nbsp;<a href="'. URL_ROOT.$dirname .'/?action=general_itemview_itemlist">商品一覧</a></li>';
            $str = "";
            $g = array_pop($g);
            for ($i = 0; $i < count($g); $i++) {
                $con = "";
                if (is_array($g[$i+1])) {
                    // パンくず
                    $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>';
                    // ページタイトル
                    $str .= $g[$i]["genrenm"] .' > ';
                } else {
                    // パンくず
                    $tp .= '<li class="last">&nbsp;&gt&nbsp;';
                    $tp .= $g[$i]["genrenm"] .'</li>';
                    // ページタイトル
                    $str .= $g[$i]["genrenm"];
                    // ページ見出し
                    $this->pageindex = $g[$i]["genrenm"];
                    // ページ見出し画像（画像があるときのみ
                    $this->pageindeximg = $this->view->getGenreImage($g[$i]["genrecd"]);
                }
            }
            if (!empty($str)) {
                $pn  = " [" . $str ."]";
            }

            $this->pagename  .= $pn;
            $this->topicpath .= $tp;

            return;
        }

        if (!empty($this->tag)) {
            // パンくず
            $tp  = '<li class="last">&nbsp;&gt;&nbsp;タグ「';
            $tp .= htmlspecialchars($this->tag, ENT_QUOTES) .'」</li>';
            // ページタイトル
            $this->pagename  .= ' [タグ「'. htmlspecialchars($this->tag, ENT_QUOTES) .'」]';
            // ページ見出し
            $this->pageindex = 'タグ「'. $this->tag .'」';
        }
        $this->pagename  .= $pn;
        $this->topicpath .= $tp;
    }
    function getTopicPath() {
        return $this->topicpath;
    }
}
?>
