<?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: */

/**
 * メールマガジン
 *
 * @package     Onedepo.Db.Mailmagazine
 * @author      
 * @access      public
 */
class Onedepo_Db_Mailmagazine
{
    var $dbaccess;
    
    var $data;
    
    
    /**
     * メールマガジン全取得
     * 
     * @return object 取得レコード
     */
    function selectAll()
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailmagazine#selectAll";
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $params = array();
        
        $sql  = "  SELECT mailcd, title as mailtitle, body as mailbody, senddate, target, sendstate ";
        $sql .= "    FROM t_mail ";
        $sql .= "   WHERE delflg = FALSE ";
        $sql .= "ORDER BY senddate DESC ,mailcd DESC ; ";
        
        $this->data = $db->getAll($sql, $params);
        $this->dbaccess->doClose($db);
        
        $this->dataExchange();
        
//        $log->debug("data: ". print_r($this->data), $func_name);
        
        return $this->data;
    }
    
    
    /**
     * メールマガジン取得
     * 
     * @param int $cd メールコード
     * @return object 取得レコード
     */
    function selectKey($cd)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailmagazine#selectKey";
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $params = array($cd);
        
        $sql  = "  SELECT mailcd, title as mailtitle, body as mailbody, senddate, target, sendstate, updcnt ";
        $sql .= "    FROM t_mail ";
        $sql .= "   WHERE mailcd = ? ";
        $sql .= "     AND delflg = FALSE ";
        
        $this->data = $db->getAll($sql, $params);
        $this->dbaccess->doClose($db);
        
        $this->dataExchange();
        
//        $log->debug("data: ". print_r($this->data), $func_name);
        
        return $this->data;
    }
    
    
    /**
     * メールマガジン登録
     * 
     * @param var $title    メールタイトル
     * @param var $body     メール本文
     * @param var $date     送信日
     * @param var $state    送信状態
     * @param var $target   対象者
     * @param var $updusr   更新ユーザ
     * @return bool 実行結果
     */
    function insert($title, $body, $date, $state, $target, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailtemplate#insert";
        
        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--
        
        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        
        $params = array($title, $body, $date, $target, $state, $upduser, $_SERVER['REMOTE_ADDR']);
        $sql  = " INSERT INTO t_mail(title, body, senddate, target, sendstate, updusrnm, updpcnm)  ";
        $sql .= "             VALUES(    ?,    ?,        ?,      ?,         ?,        ?,       ?); ";
        
        $res = $db->query($sql, $params);
        if (DB::isError($res)) {
            $log->error("rollback", $func_name);
            $log->error("execute sql = ". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            
            return false;
        }
        
        $db->commit();
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        
        return true;
    }
    
    
    /**
     * メールマガジン更新
     * 
     * @param var $code     メールコード
     * @param var $title    メールタイトル
     * @param var $body     メール本文
     * @param var $date     送信日
     * @param var $state    送信状態
     * @param var $target   対象者
     * @param var $updcnt   更新カウント
     * @param var $updusr   更新ユーザ
     * @return bool 実行結果
     */
    function update($code, $title, $body, $date, $state, $target, $updcnt, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailtemplate#update";
        
        // 更新ユーザ
        $adminauth =& AdminAuthext::getInstance();
        $upduser = $adminauth->getAuthData("membernm");
        $log->debug("membernm", $upduser);
        //--
        
        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        
        $params = array($title, $body, $date, $target, $state, $upduser, $_SERVER['REMOTE_ADDR'], $code, $updcnt);
        $sql  = " UPDATE t_mail ";
        $sql .= "    SET title = ?, body = ?, senddate = ?, target = ?, sendstate = ?, updusrnm = ?, updpcnm = ? ";
        $sql .= "  WHERE mailcd = ? AND updcnt = ? AND delflg = false ; ";
        
        $res = $db->query($sql, $params);
        if (DB::isError($res)) {
            $log->error("rollback", $func_name);
            $log->error("execute sql = ". $res->userinfo, $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            
            return false;
        }
        
        $db->commit();
        $log->debug("commit", $func_name);
        $this->dbaccess->doClose($db);
        
        return true;
    }
    
    
    /**
     * ユーザメールアドレスリスト取得
     * 
     */
    function selectUserMailaddr($target){
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailmagazine#selectUserMailaddr";
        
        $db = $this->dbaccess->doConnect();
//        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        
        // 送信対象者
        switch ($target) {
            case 1:
                // 全員
                $sql  = "  SELECT mailaddr ";
                $sql .= "    FROM t_member_basic_attr ";
                $sql .= "   WHERE memberstate < 8 AND delflg = FALSE ; ";
                break;
            case 2:
                // 希望者のみ
                $sql  = "  SELECT mailaddr ";
                $sql .= "    FROM t_member_basic_attr ";
                $sql .= "   WHERE maildiv = 1 AND memberstate < 8 AND delflg = FALSE ; ";
                break;
            case 3:
                // 出品者のみ
                return "";
            default:
                return "";
        }
        
        $params = array();
        
        $this->data = $db->getCol($sql, 0, $params);
        $this->dbaccess->doClose($db);
        
//        $log->debug("data: ". print_r($This->data), $func_name);
        
        return $this->data;
    }
    
    
    /**
     * データ変換
     * 
     * 
     */
    function dataExchange(){
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailmagazine#dataExchange";
        
        if (!empty($this->data)){
            foreach ($this->data as $key => $val) {
                // 送信対象者
                switch ($val['target']) {
                    case 1:
                        $this->data[$key]['target_str'] = '全員';
                        break;
                    case 2:
                        $this->data[$key]['target_str'] = '希望者のみ';
                        break;
                    case 3:
                        $this->data[$key]['target_str'] = '出品者のみ';
                        break;
                }
                // 送信状態
                switch ($val['sendstate']) {
                    case '0':
                        $this->data[$key]['sendstate_str'] = '未送信';
                        break;
                    case '1':
                        $this->data[$key]['sendstate_str'] = '送信済';
                        break;
                }
            }
        }
        
//        $log->debug("data: ". print_r($this->data), $func_name);
        
    }
}
?>
