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


/**
 * ウォッチリスト関連
 *
 * @package     Onedepo.Db.Watchlist
 * @author      FUCO T.Hoshino<hoshino@fuco.jp>
 * @access      public
 */
class Onedepo_Db_Watchlist
{
    var $dbaccess;


    function selectAll($membercd)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Watchlist#selectAll";

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

        $params = array($membercd);
        
        $sql  = "SELECT watch.watchlistcd, watch.membercd, watch.itemcd, ";
        $sql .= "       tag.tagcd, tag.tagnm, item.itemnm, watch.updcnt ";
        $sql .= "  FROM (t_watch_list watch ";
        $sql .= "          LEFT JOIN t_tag tag ON watch.itemcd = tag.itemcd and watch.membercd=tag.usercd), ";
        $sql .= "          t_item_basic_attr item ";
        $sql .= " WHERE watch.itemcd = item.itemcd ";
        $sql .= "   AND watch.membercd = ? ";
        $sql .= "   AND watch.delflg = FALSE ";
        $sql .= "   AND item.delflg = FALSE ";

        //$sql .= " ORDER BY tag.createdate";
        
        $log->debug($sql, $func_name);

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

        return $data;
    }
    
    /**
     * 追加
     * 
     */
   function insert($usercd, $itemcd, $comment, $updusr)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Watchlist#insert";
        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($usercd, $itemcd, $comment,  $updusr, $_SERVER['REMOTE_ADDR'], 0);

        $sql  = "SELECT f_watch_list_update(1, 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);
            //echo $res->getMessage() ;
            return false;
        }

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

    /**
     * 更新取得
     *
     */
    function update($watchlistcd, $usercd, $itemcd, $comment, $updusr, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Watchlist#update";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit(false);
        $params = array($watchlistcd, $usercd, $itemcd, $comment,  $updusr, $_SERVER['REMOTE_ADDR'], $updcnt);

        $sql  = "SELECT f_watch_list_update(2, ?, ?, ?, ?, ?, ?, ?);";
        $res = $db->getOne($sql, $params);

        if (DB::isError($res) || $res < 0) {
            $log->error("rollback ".$res->userinfo, $func_name);
            $db->rollback();
            return false;
        }

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

    /**
     * 削除
     *
     */
    function delete($watchlistcd, $usercd, $updusr, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Recommend#delete";

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

        $sql  = "SELECT f_watch_list_update(3, ?, ?, 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;
        }

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

}
?>
