<?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.Itemranking
 * @author      netnee
 * @access      public
 */
class General_Itemview_Itemranking
{
    var $title;
    var $pagename;

    var $i_item;
    var $row;
    var $unrec;

    var $bodyid;

    var $srcword;

    var $view;

    var $cont;
    var $pagedata;  // Pagerデータ
    var $links;     // Pagerリンク
    var $pageID;    // 現在のページID

    var $Pager_perPage;
    var $Pager_delta;
    var $Pager_append;
    var $Pager_clearIfVoid;
    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 $currentpageid;
    var $start;
    var $end;


    var $genre;
    var $gnr;
    var $gnr_d;


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

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

        $c = "";
        $tree = array();
        $this->genre->makeTree($g, $c, $tree, true);
        if (!empty($this->gnr)) {
            // ジャンルデータ
            $this->gnr_d = $this->genre->selectKey(NULL, $this->gnr);
            $this->pagename .= " [". $this->gnr_d["genrenm"] ."]";

            $this->gnr = array($this->gnr);
            $data = $tree;
            $del = true;
            //$log->error(print_r($tree, true), "");
            $this->genre->selectGenreOnly($data, $this->gnr, $tree, $del, true);
            //$log->error(print_r($tree, true), "");

            $c="";
            foreach ($tree as $key => $val) {
                $c[] = $val["genrecd"];
                if (!empty($val["child"])) {
                    $r = $this->getGenrecd($this->gnr, $val["child"], $c);
                }
                if ($r===false) {
                    break;
                }
            }
            //$log->error(print_r($c, true), "");
        }

        $this->row = $this->i_item->getItemRanking($c, 100);


        //取得したレコード数
        $this->cont = count($this->row);

                // Pager設定
        $params = array(
            'itemData'      => $this->row,
            'perPage'       => $this->Pager_perPage,     // １ページの行数
            'delta'         => $this->Pager_delta,       // 表示ページから、前後に表示するページ数
            'append'        => $this->Pager_append,
            'clearIfVoid'   => $this->Pager_clearIfVoid,
            '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($params);
        $this->pagedata = $pager->getPageData();
        $this->links = $pager->getLinks();

        $lastpage = $pager->numPages();                        //ページ数カウント
        $this->currentpageid = $pager->getCurrentPageID ();    //現在のカレントページID取得

            //xx～xx件用
        if($this->currentpageid == 1){
            $this->start = 1;

            if($lastpage == 1){
            $this->end = $this->cont;
            }
            else{
            $this->end = $this->Pager_perPage;
            }
        }
        else{
            $this->start = (($this->currentpageid - 1) * $this->Pager_perPage) + 1;

            if($this->currentpageid <> $lastpage){
            $this->end = ($this->currentpageid * $this->Pager_perPage);
            }
            else{
            $this->end = $this->cont;
            }
        }

        return 'success';
    }

    /**
     * ページリンク取得
     *
     * @return object ページリンク
     */
    function getLink()
    {
        return $this->links;
    }


    function getGenrecd($genrecd="", $genrelist, &$c) {
       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;
           }
       }
    }
}
?>
