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


require_once 'Pager.php';

class Admin_Portalmanage_News_List
{
	// ページ情報 
	var $pagename;
	var $title;

	// DB
    var $news;
    var $data;
    var $cnt;
    
    // 入力パラメータ
    var $sort;
    var $pageID;

    var $srcword;       //検索値

    var $srcdispnum;    // 表示件数
    var $srcdispnumlist;// 表示件数リスト

    // リストのページング 
    var $pagedata;    // Pagerデータ
    var $links;       // Pagerリンク

    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;
    
    function getPager(){
        // Pager設定
        $pageparams = array(
        'totalItems'    => $this->cnt,
        'perPage'       => $this->Pager_perPage,       // １ページの行数
        'delta'         => $this->Pager_delta,       // 表示ページから、前後に表示するページ数
        'append'        => $this->Pager_append,
        'currentPage'   => $this->pageID,
        '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,
        'extraVars' => array('sort' => $this->sort, 'srcword' => $this->srcword, 'srcdispnum' => $this->srcdispnum)
        );

        // Pager出力
        $pager = & Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();
        $link =	$pager->getLinks();
        return $link["all"];

    }

	function execute()
	{
        // 一覧表示件数
        if (!empty($this->srcdispnum)) $this->Pager_perPage = $this->srcdispnum;
        
        $this->srcdispnumlist = array();
        $this->srcdispnumlist[] = 10;
        $this->srcdispnumlist[] = 20;
        $this->srcdispnumlist[] = 50;
        $this->srcdispnumlist[] = 100;
       
        
        // 並び替え
        $sorttype = array(
           "title"        => "title ",
           "-title"       => "title DESC ",
           "releasediv"   => "releasediv ",
           "-releasediv"  => "releasediv DESC ",
           "date"         => "date ",
           "-date"        => "date DESC "
        );
        if (empty($sorttype[$this->sort]))
            $this->sort = "date";

        $sort = $sorttype[$this->sort];
        
        $where = array();        
        if($this -> srcword){
            $where[0][0] = "title LIKE '%" . $this -> srcword ."%'";
            $where[0][1] = " delflg = false ";
            $where[1][0] = "body  LIKE '%" . $this -> srcword ."%'";
            $where[1][1] = " delflg = false ";
        }
        else
            $where[0][0] = "delflg = false";

        $this->data = $this->getNewsList($this->pageID,$where,$sort);

        if(!empty($this->data))
            $this->cnt = array_pop($this->data);
        else
            $this->cnt = -1;

        $log =& LogFactory::getLog();
        $log->debug("Admin_Portalmanage_News_Listが実行されました", "Admin_Portalmanage_News_List#execute");
        return 'success';
    }
    /**
     * ページリンク取得
     *
     * @return object ページリンク
     */

    function getNewsList($pageID,$where,$sort)
    {
        $res = $this->news->getNewsList(
                          $this->Pager_perPage,
                          $this->Pager_perPage*($pageID - 1),
                          $where,
                          array(),
                          $sort);

        //お知らせテーブルから「総登録件数しかデータを取得できなかった」、かつ「登録件数が０以上」場合
        //PageIDを１マイナスして、再取得
        if(count($res) <= 1 && $res["recordcount"] > 0){
            $pageID -= 1;
            $res = $this->news->getNewsList(
                              $this->Pager_perPage,
                              $this->Pager_perPage*($pageID - 1),
                              $where,
                              array(),
                              $sort);
        }
        return $res;
    }

    function getLink()
    {
        return $this->links;
    }
    
    function getFrom()
    {
        $from = "admin_portalmanage_news_list" .
                "&listsize="   . $this -> listsize . 
                "&sort="       . $this -> sort . 
                "&srcdispnum=" . $this -> srcdispnum .
                "&srcword="    . $this -> srcword . 
                "&pageID="     . $this -> pageID;
        $encodeFrom = urlencode($from);
        return $encodeFrom;
    }
}