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


/**
 * レビュー関連
 *
 * @package     Onedepo.Db.Review
 * @author      FUCO H.Tominaga
 * @access      public
 */
class Onedepo_Db_Review
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;

    /**
     * 全件取得
     * 
     * @param string $sort ソートキー
     * @return object 取得レコード
     */
/*
    function selectAll($usercd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($usercd);

        $sql  = "SELECT *";
        $sql .= " FROM t_review t1, t_item_basic_attr t2";
        $sql .= " WHERE t1.delflg = FALSE ";
        $sql .= " AND t1.UserCd = ?";
        $sql .= " AND t1.ItemCd = t2.ItemCd";
        $sql .= " ORDER BY t1.CreateDate";
        $log->debug($sql, "Onedepo_Db_Review#selectAll");
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            $log->trace("rollback", "Onedepo_Db_Review#selectAll");
            //echo $res->toString();
            $db->rollback();
            $data = false;
        }
        $log->trace("commit", "Onedepo_Db_Review#selectAll");
        $this->dbaccess->doClose($db);
        return $data;
    }

    function selectOne($MemberCd, $ReviewCd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($MemberCd,
                         $ReviewCd);
        $sql  = "SELECT *";
        $sql .= " FROM t_review t1, t_item_basic_attr t2";
        $sql .= " WHERE t1.delflg = FALSE ";
        $sql .= " AND t1.UserCd = ?";
        $sql .= " AND t1.ReviewCd = ?";
        $sql .= " AND t1.ItemCd = t2.ItemCd";
        $sql .= " ORDER BY t1.CreateDate";
        $log->debug($sql, "Onedepo_Db_Review#selectOne");
        $data = $db->getRow($sql, $params);
        if (DB::isError($data)) {
            $log->trace("rollback", "Onedepo_Db_Review#selectOne");
            //echo $res->toString();
            $db->rollback();
            $data = false;
        }
        $log->trace("commit", "Onedepo_Db_Review#selectOne");
        $this->dbaccess->doClose($db);
        return $data;
    }
*/

    function selectAll($usercd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($usercd);

        $sql  = "SELECT *";
        $sql .= " FROM t_review t1, t_item_basic_attr t2";
        $sql .= " WHERE t1.delflg = FALSE ";
        $sql .= " AND t1.UserCd = ?";
        $sql .= " AND t1.ItemCd = t2.ItemCd";
        $sql .= " ORDER BY t1.ReviewCd";
        $log->debug($sql, "Onedepo_Db_Review#selectAll");
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            $log->trace("rollback", "Onedepo_Db_Review#selectAll");
            //echo $res->toString();
            $db->rollback();
            $data = false;
        }
        $log->trace("commit", "Onedepo_Db_Review#selectAll");
        $this->dbaccess->doClose($db);
        return $data;
    }

    function selectOne($MemberCd, $ReviewCd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($MemberCd,
                         $ReviewCd);
        $sql  = "SELECT *, t1.updcnt as updcnt ";
        $sql .= " FROM t_review t1, t_item_basic_attr t2";
        $sql .= " WHERE t1.delflg = FALSE ";
        $sql .= " AND t1.UserCd = ?";
        $sql .= " AND t1.ReviewCd = ?";
        $sql .= " AND t1.ItemCd = t2.ItemCd";
        $sql .= " ORDER BY t1.ReviewCd";
        $log->debug($sql, "Onedepo_Db_Review#selectOne");
        $data = $db->getRow($sql, $params);
        if (DB::isError($data)) {
            $log->trace("rollback", "Onedepo_Db_Review#selectOne");
            //echo $res->toString();
            $db->rollback();
            $data = false;
        }
        $log->trace("commit", "Onedepo_Db_Review#selectOne");
        $this->dbaccess->doClose($db);
        return $data;
    }

    function selectOneReview($MemberCd, $ReviewCd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($MemberCd,
                         $ReviewCd);
        $sql  = "SELECT *";
        $sql .= " FROM t_review";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= " AND UserCd = ?";
        $sql .= " AND ReviewCd = ?";
        $log->debug($sql, "Onedepo_Db_Review#selectOneReview");
        $data = $db->getRow($sql, $params);
        if (DB::isError($data)) {
            $log->trace("rollback", "Onedepo_Db_Review#selectOneReview");
            //echo $res->toString();
            $db->rollback();
            $data = false;
        }
        $log->trace("commit", "Onedepo_Db_Review#selectOneReview");
        $this->dbaccess->doClose($db);
        return $data;

    }

    function insert($membercd, $itemcd, $rank, $comment, $upduser)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Review#insert";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($itemcd, $membercd, $rank, $comment, $upduser, $_SERVER['REMOTE_ADDR']);

        $sql  = "select f_review_update(1, NULL, ?, ?, ?, ?, ?, ?, NULL);";

        $res = $db->getOne($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->error("rollback". $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;
    }

    function update($reviewcd, $membercd, $itemcd, $rank, $comment, $upduser, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Review#update";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($reviewcd, $itemcd, $membercd, $rank, $comment, $upduser, $_SERVER['REMOTE_ADDR'], $updcnt);
        
        $sql  = "select * FROM f_review_update(2, ?, ?, ?, ?, ?, ?, ?, ?);";

        $res = $db->getRow($sql, $params);
        
        if (DB::isError($res) || $res['errcd'] < 0) {
            $log->error("rollback". $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;
    }
    
    function delete($reviewcd, $upduser, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Review#delete";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($reviewcd, $upduser, $_SERVER['REMOTE_ADDR'], $updcnt);

        $sql  = "select f_review_update(3, ?, NULL, NULL, NULL, NULL, ?, ?, ?);";

        $res = $db->getOne($sql, $params);
        if (DB::isError($res) || $res < 0) {
            $log->error("rollback". $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;
    }

/*
    function insert($membercd, $itemcd, $rank, $comment)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($itemcd, $membercd, $rank, $comment, date("Y-m-d H:i:s"));
        $sql  = "INSERT INTO t_review(";
        $sql .= "ItemCd, UserCd, RankingRate, ";
        $sql .= "Comment, CreateDate) ";
        $sql .= "values (?, ?, ?, ?, ?);";
        $log->debug($sql, "Onedepo_Db_Cart#insert");
        $data = $db->query($sql, $params);
        $this->dbaccess->doClose($db);
        return $data;
    }

    function update($MemberCd, $ReviewCd, $rank, $comment)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $res = $this -> selectOne($MemberCd, $ReviewCd);
        if($res != false) {
            $cnt = $res['updcnt'] + 1;

            $params = array($rank,
                             $comment,
                             date("Y-m-d H:i:s"),
                             $_SERVER['REMOTE_ADDR'],
                             $cnt,
                             $ReviewCd,
                             $MemberCd);
            $sql  = "UPDATE t_review set ";
            $sql .= "RankingRate = ?, ";
            $sql .= "Comment = ?, ";
            $sql .= "UpdDate = ?, ";
            $sql .= "UpdUsrNm = '', ";
            $sql .= "UpdPcNm = ?, ";
            $sql .= "UpdCnt = ? ";
            $sql .= " WHERE delflg = FALSE ";
            $sql .= " AND ReviewCd = ? ";
            $sql .= " AND UserCd = ? ";
            $log->debug($sql, "Onedepo_Db_Review#Update");
            $data = $db->query($sql, $params);

            if (DB::isError($data)) {
                $log->trace("rollback", "Onedepo_Db_Review#Update");
                //echo $res->toString();
                $db->rollback();
                $data = false;
            }
            $log->trace("commit", "Onedepo_Db_Review#Update");
        } else {
            $data = false;
        }
        $this->dbaccess->doClose($db);
        return $data;
    }

    function delete($MemberCd, $ReviewCd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $res = $this -> selectOne($MemberCd, $ReviewCd);
        if($res != false) {
            $cnt = $res['updcnt'] + 1;

            $params = array(date("Y-m-d H:i:s"),
                             $_SERVER['REMOTE_ADDR'],
                             $cnt,
                             $ReviewCd,
                             $MemberCd);
            $sql  = "UPDATE t_review set";
            $sql .= " RankingRate = NULL,";
            $sql .= " Comment = NULL,";
            $sql .= " delflg = TRUE,";
            $sql .= " UpdDate = ?,";
            $sql .= " UpdUsrNm = '',";
            $sql .= " UpdPcNm = ?,";
            $sql .= " UpdCnt = ?";
            $sql .= " WHERE delflg = FALSE ";
            $sql .= " AND ReviewCd = ? ";
            $sql .= " AND UserCd = ? ";
            $log->debug($sql, "Onedepo_Db_Review#Update");
            $data = $db->query($sql, $params);

            if (DB::isError($data)) {
                $log->trace("rollback", "Onedepo_Db_Review#Update");
                //echo $res->toString();
                $db->rollback();
                $data = false;
            }
            $log->trace("commit", "Onedepo_Db_Review#Update");
        } else {
            $data = false;
        }
        $this->dbaccess->doClose($db);
        return $data;
    }
*/    
    function getItemReview($usercd, $itemcd)
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($usercd, $itemcd);
        $sql  = "SELECT *";
        $sql .= " FROM t_review review ";
        $sql .= " WHERE review.delflg = FALSE ";
        $sql .= " AND   review.usercd = ?";
        $sql .= " AND   review.itemcd = ?";
        
        $log->debug($sql, "Onedepo_Db_Review#getItemReview");
        $data = $db->getRow($sql, $params);
        
        return $data;
    }
    
    function getItemAllReview($itemcd, $sort='createdate')
    {
        $log =& LogFactory::getLog();
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);

        $params = array($itemcd);
        $sql  = "SELECT member.membercd, ";  // 会員C
        $sql .= " member.lastnm, ";          // 姓
        $sql .= " member.firstnm, ";         // 名
        $sql .= " member.nicknm, ";          // ニックネーム
        $sql .= " review.rankingrate, ";     // ランク
        $sql .= " review.comment, ";         // コメント
        $sql .= " review.createdate ";      // 作成日時
        $sql .= " FROM t_review review, t_member_basic_attr member ";
        $sql .= " WHERE review.usercd = member.membercd ";
        $sql .= " AND   review.delflg = FALSE ";
        $sql .= " AND   member.delflg = FALSE ";
        $sql .= " AND   review.itemcd = ?";
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);
        $log->debug($sql, "Onedepo_Db_Review#getItemAllReview");
        $data = $db->getAll($sql, $params);
        
        return $data;
    }
}
?>