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


require_once 'Pager.php';

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * [[機能説明]]
 *
 * @package     [[package名]]
 * @author      Tominaga hitosi <mail@example.com>
 * @access      public
 */
class Admin_Usermanage_Customer_Customerlist
{
    /**
     * [[機能説明]]
     *
     * @access  public
     */

	//Pager
	var $srcdispnum;    // 表示件数
	var $pageID;        // ページ番号

    //DB
    var $c_member;
    var $c_consumer;
    var $c_seller;
    var $c_pref;
    var $session;
    var $user_array;
    
    var $cnt;

    var $prefdatas;
    var $year_cnt;

    //検索条件用変数
    var $mcd;
    var $username;
    var $mailaddr;
    var $prefcd;
    var $sex;
    var $telno;
    var $month;
    var $birth_year1;
    var $birth_month1;
    var $birth_day1;
    var $birth_year2;
    var $birth_month2;
    var $birth_day2;
    var $add_year1;
    var $add_month1;
    var $add_day1;
    var $add_year2;
    var $add_month2;
    var $add_day2;
    var $maildiv;

    var $consumer;
    var $seller;
    var $sort;
    var $conditions;

    var $title;
    var $pagename;
    
    // ページャー
	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('action' => 'admin_usermanage_customer_customerlist',
							   'sort' => $this->sort,
                               'membercd' => $this->membercd,
                               'username' => $this->username,
                               'consumer' => $this->consumer,
                               'seller' => $this->seller),
        );
        
        // Pager出力
        $pager = & Pager::factory($pageparams);
        $this->pagedata = $pager->getPageData();
        $link =	$pager->getLinks();
        return $link["all"];
	}
	
	function getUserlist()
	{
	    return $this->user_array;
	}
	
	function setRequest()
	{
	    $where = $params = array();

	    // メンバーコード
	    if(!empty($this->mcd)) {
    	    $where[0][] = " tba.membercd = ? ";
            $this->mcd = mb_convert_kana($this->mcd, "a", "UTF-8");
            $params[] = $this->mcd;
        }

    	// 会員名/かな
	    if(!empty($this->username)) {
    	    $where[0][] = " tba.lastnm || tba.firstnm || tba.lastnmkana || tba.firstnmkana LIKE ? ";
            $params[] = '%'. $this->username .'%';
        }

    	// メールアドレス
    	if(!empty($this->mailaddr)) { 
    	    $where[0][] = " (tba.mailaddr LIKE ? OR tba.mailaddr LIKE ?) ";
            // 全角→半角
            $this->mailaddr = mb_convert_kana($this->mailaddr, "a", "UTF-8");
            // 大文字、小文字
            $params[] = '%'. mb_strtolower($this->mailaddr) .'%';
            $params[] = '%'. mb_strtoupper($this->mailaddr) .'%';
        }
    	    
    	// 都道府県
    	if(!empty($this->prefcd)) { 
    	    $where[0][] = " tca.prefcd = ?";
            $params[] = $this->prefcd;
        }

    	// 性別
    	if(!empty($this->sex)) {
            if     (in_array("male", $this->sex) && in_array("female", $this->sex)) $where[0][] = " tba.sex in (1,2) ";
            elseif (in_array("male", $this->sex))    $where[0][] = " tba.sex = 1 ";
            elseif (in_array("female", $this->sex))  $where[0][] = " tba.sex = 2 ";
        }

    	// 誕生日(開始)
    	if(checkdate($this->birth_month1, $this->birth_day1, $this->birth_year1)) {
            $where[0][] = " tba.birthdate >= ? ";
            $params[] = date("Y-m-d", mktime(0,0,0,$this->birth_month1, $this->birth_day1, $this->birth_year1));
        }

    	// 誕生日(終了)
    	if(checkdate($this->birth_month2, $this->birth_day2, $this->birth_year2)) {
            $where[0][] = " tba.birthdate <= ? ";
            $params[] = date("Y-m-d", mktime(0,0,0,$this->birth_month2, $this->birth_day2, $this->birth_year2));
        }

    	// 誕生月
    	if(!empty($this->month)) {
            $where[0][] = " tba.birthdate LIKE ? ";
            $params[] = "%-". sprintf("%02d", $this->month) ."-%";
        }

    	// 電話番号
    	if(!empty($this->tel)) {
            $where[0][] = " tca.telno1 LIKE ? ";
            // 全角→半角
            $this->tel = mb_convert_kana($this->tel, "a", "UTF-8");
            $params[] = "%". $this->tel ."%";
        }

    	// 登録日(開始)
    	if(checkdate($this->add_month1, $this->add_day1, $this->add_year1)) {
            $where[0][] = " tba.adddate >= ? ";
            $params[] = date("Y-m-d", mktime(0,0,0,$this->add_month1, $this->add_day1, $this->add_year1));
        }

    	// 登録日(終了)
    	if(checkdate($this->add_month2, $this->add_day2, $this->add_year2)) {
            $where[0][] = " tba.adddate <= ? ";
            $params[] = date("Y-m-d", mktime(0,0,0,$this->add_month2, $this->add_day2, $this->add_year2));
        }

	    // メール配信希望
	    if(!empty($this->maildiv)) {
            if     ($this->maildiv==1) $where[0][] = " tba.maildiv = 1 ";
            elseif ($this->maildiv==0) $where[0][] = " tba.maildiv = 0 ";
        }

    	// 販売者情報の有無
    	if(!empty($this->seller) && $this->seller != "")
    	{
    	    if($this->seller == "registed")
    	        $where[0][] = " tsa.membercd IS NOT NULL ";
    	    if($this->seller == "unregisted")
    	        $where[0][] = " tsa.membercd IS NULL ";
    	}

    	// 購入者情報の有無
    	if(!empty($this->consumer) && $this->consumer != "")
    	{
    	    if($this->consumer == "registed")
    	        $where[0][] = " tca.membercd IS NOT NULL ";
    	    if($this->consumer == "unregisted")
    	        $where[0][] = " tca.membercd IS NULL ";
    	}

    	// ソート
    	if(empty($this->sort) || $this->sort=="membercd") {
    	    $sort = "tba.membercd desc";
        } else {
            $sort = $this->sort;
        }


    	$this->user_array = $this->c_member->getMemberList($this->Pager_perPage, $this->Pager_perPage*($this->pageID-1), $where, $params, $sort);

    	$this->cnt = array_pop($this->user_array);
	}
	
	function getCondictionPOST()
	{
        return  "<input type='hidden' name='mcd' value='". htmlspecialchars($this->mcd, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='username' value='". htmlspecialchars($this->username, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='mailaddr' value='". htmlspecialchars($this->mailaddr, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='prefcd' value='". htmlspecialchars($this->prefcd, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='sex[]' value='". htmlspecialchars($this->sex[0], ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='sex[]' value='". htmlspecialchars($this->sex[1], ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='telno' value='". htmlspecialchars($this->telno, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='month' value='". htmlspecialchars($this->month, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='birth_year1' value='". htmlspecialchars($this->birth_year1, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='birth_month1' value='". htmlspecialchars($this->birth_month1, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='birth_day1' value='". htmlspecialchars($this->birth_day1, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='birth_year2' value='". htmlspecialchars($this->birth_year2, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='birth_month2' value='". htmlspecialchars($this->birth_month2, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='birth_day2' value='". htmlspecialchars($this->birth_day2, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='add_year1' value='". htmlspecialchars($this->add_year1, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='add_month1' value='". htmlspecialchars($this->add_month1, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='add_day1' value='". htmlspecialchars($this->add_day1, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='add_year2' value='". htmlspecialchars($this->add_year2, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='add_month2' value='". htmlspecialchars($this->add_month2, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='add_day2' value='". htmlspecialchars($this->add_day2, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='maildiv' value='". htmlspecialchars($this->maildiv, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='consumer' value='". htmlspecialchars($this->consumer, ENT_QUOTES) ."'>"
	            ."<input type='hidden' name='seller' value='". htmlspecialchars($this->seller, ENT_QUOTES) ."'>";
	}
	function getCondictionGET()
	{
	    return  "mcd=". urlencode($this->mcd)
               ."&username=". urlencode($this->username)
               ."&mailaddr=". urlencode($this->mailaddr)
               ."&prefcd=". urlencode($this->prefcd)
               ."&sex[]=". urlencode($this->sex[0])
               ."&sex[]=". urlencode($this->sex[1])
               ."&telno=". urlencode($this->telno)
               ."&month=". urlencode($this->month)
               ."&birth_year1=". urlencode($this->birth_year1)
               ."&birth_month1=". urlencode($this->birth_year1)
               ."&birth_day1=". urlencode($this->birth_year1)
               ."&birth_year2=". urlencode($this->birth_year2)
               ."&birth_month2=". urlencode($this->birth_month2)
               ."&birth_day2=". urlencode($this->birth_day2)
               ."&add_year1=". urlencode($this->add_year1)
               ."&add_month1=". urlencode($this->add_year1)
               ."&add_day1=". urlencode($this->add_year1)
               ."&add_year2=". urlencode($this->add_year2)
               ."&add_month2=". urlencode($this->add_month2)
               ."&add_day2=". urlencode($this->add_day2)
               ."&maildiv=". urlencode($this->maildiv)
               ."&consumer=". urlencode($this->consumer)
               ."&seller=". urlencode($this->seller);
	}
	
	function setSearchcondition()
	{
	    
	/*
        if(isset($_POST['dispatch_admin_usermanage_customer_customerlist'])) {
            if($_POST['dispatch_admin_usermanage_customer_customerlist'] == "検索") {
                $this -> conditions = array($this -> membercd,
                                     $this -> username,
                                     $this -> mailaddress,
                                     $this -> consumer,
                                     $this -> seller);
                $this -> session -> setParameter('customerConditions', $this -> conditions);
            } elseif($_POST['dispatch_admin_usermanage_customer_customerlist'] == "解除") {
                $this -> conditions = array(null, null, null, array("check", "check"), array("check", "check"));
                $this -> session -> setParameter('customerConditions', $this -> conditions);
            }
        }
        */
	}

    function checkCondition()
    {
       
    /*
        $this -> conditions = $this -> session -> getParameter('customerConditions');
        if($this -> conditions == null){
            $this -> conditions = array(null, null, null, array("check", "check"), array("check", "check"));
            $this -> session -> setParameter('customerConditions', $this -> conditions);
        }
        */
    }
	
    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("Admin_Usermanage_Customer_Requestlistが実行されました", "Admin_Usermanage_Customer_Requestlist#execute");

        $this->prefdatas = $this->c_pref->selectAll();
        $this->year_cnt = date('Y') - 1900 + 1;

        $this -> checkCondition();
        $this -> setSearchcondition();
        $this -> setRequest();
        return 'success';
    }
}
?>