<?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     [[package名]]
 * @author      T.Shimano
 * @access      public
 */
class Admin_Usermanage_Itemexhibit_List
{
	// ページ情報
	var $pagename;
	var $title;

	//DB
	var $itemexhibit;
	var $order;
	var $data;
	var $cnt;
	
	// 入力パラメータ
	var $sort;
	var $pageID;
	var $listsize;
	
	// 検索条件のURLパラメータ
    var $extrastr;
    var $extratag;

	// 検索パラメータ
	var $exno;
	var $titlenm;
	var $scdcompdate;
	var $membercd;
	var $memberstate;
	var $membernm;
	var $mailaddr;
	var $image;
	var $status;
	
	// リストのページング
	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;
    var $extraval;

	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_merge($this->extraval, array(
    	                          "sort"=>$this->sort,
    	                          "listsize"=>$this->listsize,
    	                          "action"=>"admin_usermanage_itemexhibit_list"))
    	);
        
        // Pager出力
        $pager = & Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();
        $link =	$pager->getLinks();
        return $link["all"];
	}


    function execute()
    {
		// ページングデフォルト値設定 
		$this->pageID = isset($this->pageID)  ?$this->pageID  :1;
		if(!empty($this->listsize)) $this->Pager_perPage = $this->listsize;

        // 並び替え
        $sorttype = array(
        "exno"         => "exno ",
        "-exno"        => "exno DESC ",
        "title"        => "itemnm ",
        "-title"       => "itemnm DESC ",
        "scdcompdate"  => "scdcompdate ",
        "-scdcompdate" => "scdcompdate DESC ",
        "status"       => "status ",
        "-status"      => "status DESC ",
        "image"        => "generalcd ",
        "-image"       => "generalcd DESC ",
        "date"         => "date ",
        "-date"        => "date DESC ",
        "membercd"     => "sellercd",
        "-membercd"    => "sellercd DESC ",
        "membernm"     => "membernmkana",
        "-membernm"    => "membernmkana DESC "
        );
        if (empty($sorttype[$this->sort]))
            $this->sort = "exno";

        $sort = $sorttype[$this->sort];
        
        // 検索パラメータ
        $extraval = array();   // ソーティング、ページングなどで検索パラメータの保持に使用
        $where = array();  //　問い合わせに使用
        if( isset($this->exno) && $this->exno!="" ) {
            $where[] = " exno = ".$this->exno;
            $extraval["exno"]=$this->exno;
        }
        if( isset($this->titlenm) && $this->titlenm!="" ) {
            $where[] = " itemnm like '%".$this->titlenm."%'";
            $extraval["titlenm"]=$this->titlenm;
        }
        if( isset($this->scdcompdate) && $this->scdcompdate!="" ) {
            $where[] = " DATE(scdcompdate) ='".$this->scdcompdate."'";
            $extraval["scdcompdate"]=$this->scdcompdate;
        }
        if( isset($this->membercd) && $this->membercd!="" ) {
            $where[] = " sellercd =".$this->membercd;
            $extraval["membercd"]=$this->membercd;
        }
        if( isset($this->memberstate) && $this->memberstate!="" ) {
            $where[] = " memberstate =".$this->memberstate;
            $extraval["memberstate"]=$this->memberstate;
        }
        if( isset($this->membernm) && $this->membernm!="" ) {
            $where[] = " (membernm     like '%".$this->membernm."%' ".
                      "OR membernmkana like '%".$this->membernm."%') ";
            $extraval["membernm"]=$this->membernm;
        }
        if( isset($this->titlenm) && $this->titlenm!="" ) {
            $where[] = "itemnm like '%".$this->titlenm."%'";
            $extraval["titlenm"]=$this->titlenm;
        }
        if( isset($this->mailaddr) && $this->mailaddr!="") {
            $where[] = " (mailaddr like '%".$this->mailaddr."%' OR mobmailaddr like '%".$this->mailaddr."%') ";
            $extraval["mailaddr"]=$this->mailaddr;
        }
        
        if( isset($this->image) && $this->image!="") {
            if($this->image == 1) {
                $where[] = " generalcd IS NOT NULL ";
                $extraval["image"]=1;
            }
            elseif($this->image == 2) {
                $where[] = " generalcd IS NULL ";
                $extraval["image"]=2;
            }
        }
        if( isset($this->status) && $this->status!="") {
            if($this->status == 1) {
                $where[] = " scdcompdate > 'now' ";
                $extraval["status"]=1;
            }
            elseif($this->status == 2) {
                $where[] = " scdcompdate <= 'now' ";
                $extraval["status"]=2;
            }
        }
     
        $this->data = $this->itemexhibit->getItemexhibitList($this->Pager_perPage,
                                                             $this->Pager_perPage*($this->pageID-1),
                                                             $where,
                                                             array(),
                                                             array(),
                                                             $sort);
        
        $this->cnt = array_pop($this -> data);
        $this->extraval = $extraval;

        //受注明細から総個数を取得し、出品在庫明細の出品数から引く
        $dataarray = array();
        foreach($this->data as $key => $item){
        	$ordercount = $this -> order -> getSumOrderCount($item["itemcd"], $item["structcd"], $item["stockcd"]);
        	$item["exhibitcount"] -= $ordercount;
        	if($item["exhibitcount"] > 0){
        		$dataarray[] = $item;
        	}
        }

        $this->data = $dataarray;
    	//$this->cnt  = count($this->data);
    	
    	// URLパラメータ・タグ作成
    	$this->extrastr ="";
    	$this->extratag ="";
        foreach( $extraval as $key=>$val ) {
            if($this->extrastr=="")	$this->extrastr = "$key=$val";
            else $this->extrastr .="&$key=$val";
            $this->extratag .= "<input type='hidden' name='$key' value='$val'>\n";
        }
    	
    	// ログ出力
        $log =& LogFactory::getLog();
        $log->debug("Admin_Usermanage_Itemexhibit_Listが実行されました", "Admin_Usermanage_Itemexhibit_List#execute");
        return 'success';
    }
    /**
     * ページリンク取得
     *
     * @return object ページリンク
     */
    function getLink()
    {
    	return $this->links;
    }
    
    /**
     * タグ取得
     * 
     * @return 検索条件分の<input type="hidden"...>タグ
     */
    function getExtraTag()
    {
        return $this->extratag;
    }
    
    /**
     * URLパラメータ取得
     *
     * @return 検索条件分の<input type="hidden"...>タグ
     */
    function getExtraStr()
    {
        return $this->extrastr;
    }
}
?>