<?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.Mailtemplate
 * @author      
 * @access      public
 */
class Onedepo_Db_Mailtemplate
{
    var $dbaccess;

    /**
     * 通知メール取得
     * 
     * @param string $template テンプレート名
     * @return object 取得レコード
     */
    function selectKey($template)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailtemplate#selectKey";

        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($template);
        
        $sql  = "SELECT mailtplcd, template, title as mailtitle, body as mailbody, updcnt ";
        $sql .= "  FROM t_mailtemplate ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= "   AND template = ? ";

        $data = $db->getRow($sql, $params);
        $this->dbaccess->doClose($db);

        return $data;
    }
    
    /**
     * 更新
     * 
     */
    function update($mailtplcd, $template, $title, $body, $updcnt, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Mailtemplate#update";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();

        $params = array($title, $body, $upduser, $_SERVER['REMOTE_ADDR'], $mailtplcd, $template, $updcnt);
        $sql  = "update t_mailtemplate 
                   set title = ?, body = ?, updusrnm = ?, updpcnm = ? 
                   where mailtplcd = ? and template = ? and updcnt = ?;";


/* プロシージャ用
        $res = $db->getOne($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->debug("rollback", $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return 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;
        }

        $log->debug("commit", $func_name);

        $db->commit();
        $this->dbaccess->doClose($db);
        return true;
    }
}
?>
