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


/**
 * お勧め情報関連
 *
 * @package     Onedepo.Db.Recommend
 */
class Onedepo_Db_Recommend
{

    var $dbaccess;

    function getOneRecommend($cd, $where="")
    {
        $log =& LogFactory::getLog();

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


        $sql =     "SELECT itemcd,"
                ." itemnm,"
                ." to_char(startdate, 'YYYY/MM/DD') as startdate,"
                ." to_char(closedate, 'YYYY/MM/DD') as closedate,"
                ." priority,"
                ." delflg,"
                ." updcnt"
                ." FROM t_recommend "
                ."LEFT JOIN "
                ."(SELECT itemcd, itemnm "
                ."FROM t_item_basic_attr ) "
                ."AS tt_item_basic_attr  USING (itemcd) "
                ."WHERE itemcd = ? ";
        $sql .= $where;         // リプレースメントを使用する場合は変更すること。
        $params = array($cd);

        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }

    function getOneItem($cd, $where="")
    {
        $log =& LogFactory::getLog();

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


        $sql =     "SELECT * FROM t_item_basic_attr "
                ."LEFT JOIN "
                ."(SELECT makercd, makernm "
                ."FROM t_maker) "
                ."AS tt_maker USING (makercd) "
                ."WHERE itemcd = ? ";
        $sql .= $where;
        $params = array($cd);

        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
                        die($data->getMessage());
        }

        $this->dbaccess->doClose($db);

        return $data;
    }
    
    function getRecommendList($to, $from, $where = array(), $params = array(), $sortkey)
    {
        $log =& LogFactory::getLog();

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

        $limitquery =    "SELECT itemcd,"
                        ." itemnm,"
                        ." to_char(startdate, 'YYYY/MM/DD') as startdate,"
                        ." to_char(closedate, 'YYYY/MM/DD') as closedate,"
                        ." priority,"
                        ." delflg,"
                        ." updcnt"
                        ." FROM t_recommend "
                        ."LEFT JOIN "
                        ."(SELECT itemcd, itemnm, itemnmkana, delflg AS b_del "
                        ."FROM t_item_basic_attr ) "
                        ."AS tt_item_basic_attr  USING (itemcd) "
                        ."WHERE b_del=false ";
        
        $countquery = "SELECT count(itemcd) as cnt FROM t_recommend"
                        ." LEFT JOIN "
                        ."(SELECT itemcd, itemnm, delflg AS b_del "
                        ."FROM t_item_basic_attr ) "
                        ."AS tt_item_basic_attr  USING (itemcd) "
                        ."WHERE b_del=false ";

        $qry = "";
        foreach ($where as $key => $val) {
            if (is_array($val)) {
                if ($key==0)    $qry .= " AND ( ";
                else            $qry .= " OR";

                foreach($val as $k => $v) {
                    if ($k > 0) $qry .= " AND";
                    $qry .= " $v";
                }
            }
        }
        if( $qry != "")
            $qry .= ")";
        
        $limitquery .= $qry;
        $limitquery .= " ORDER BY ". $sortkey;
        $limitquery .=  " LIMIT ". $to ." OFFSET ". $from;


        $data = $db->getAll($limitquery, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }

        $countquery .= $qry;

        $cnt = $db->getOne($countquery, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }
        $data += array("recordcount" => $cnt);


        $this->dbaccess->doClose($db);

        return $data;
    }

/**
 * 追加
 *
 */
    function insert($itemcd, $startdate, $closedate, $priority, $updusr)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Recommend#insert";
        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array( $itemcd, $startdate, $closedate, $priority, $updusr, $_SERVER['REMOTE_ADDR'], 0);

        $sql  = "SELECT f_recommend_update(1, ?, ?, ?, ?, ?, ?, ?);";
        $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($itemcd, $startdate, $closedate, $priority, $updusr, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Recommend#update";

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

        $sql  = "SELECT f_recommend_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($itemcd, $upduser, $updcnt)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Recommend#delete";

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

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

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