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


/**
 * お知らせテーブル関連
 *
 * @package     Onedepo.Db.News
 * @author      netnee 
 * @access      public
 */
class Onedepo_Db_News
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess;


    /**
     * 全件取得
     * 
     * @param string $sort ソートキー
     * @return object 取得レコード
     */
    function selectAll($sort="newscd")
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_News#selectAll";

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

        $params = array($sort);

        $sql  = "SELECT newscd, date, title, body, releasediv ";
        $sql .= "  FROM t_news ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= " ORDER BY %s";
        $sql = sprintf($sql, $sort);

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

        return $data;
    }

    /**
     * 条件取得
     * 
     * @param string $code コード
     * @return object 取得レコード
     */
    function selectKey($code)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_News#selectKey";

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

        $params = array($code);

        $sql  = "SELECT newscd, date, title, body, releasediv ";
        $sql .= "  FROM t_News ";
        $sql .= " WHERE delflg = FALSE ";
        $sql .= "   AND newscd = ?;";

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

        return $data;
    }

    /**
     * 公開レコード取得
     * 
     * @param int $datelmt   日付制限値
     * @param int $recordlmt レコード制限値
     * @return object 取得レコード
     */
    function selectRelease($datelmt=NEWS_DATE_LIMIT, $recordlmt=NEWS_RECORD_LIMIT)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_News#selectRelease";
        
        $olddate = date('Y/m/d', (time() - (60 * 60 * 24 * $datelmt)));

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

        $params = array($olddate, $recordlmt);

        $sql  = "   SELECT newscd, date, title, body ";
        $sql .= "     FROM t_News ";
        $sql .= "    WHERE delflg = FALSE ";
        $sql .= "      AND releasediv = 1 ";
        $sql .= "      AND date >= ? ";
        $sql .= " ORDER BY upddate DESC ";
        $sql .= "    LIMIT ?; ";
        
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            die($data->getMessage());
        }
        
        $this->dbaccess->doClose($db);

        return $data;
    }
    
    /***
     * 以下管理用
     * 
     */

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

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


        $sql  = "SELECT  newscd, to_char(date, 'YYYY/MM/DD') as date, title, body, releasediv, delflg, updcnt"
               ." FROM t_news WHERE newscd = ?";
        $sql .= $where;         // リプレースメントを使用する場合は変更すること。
        $params = array($cd);

        $p = print_r($params, true);

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

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

        return $data;
    }

    function getNewsList($to, $from, $where = array(), $params = array(), $sortkey)
    {
        $log =& LogFactory::getLog();

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

        $limitquery = "SELECT  newscd, to_char(date, 'YYYY/MM/DD') as date, title, body, releasediv, delflg, updcnt"
                     ." FROM t_news";
        $countquery = "SELECT count(newscd) as cnt FROM t_news";


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

                foreach($val as $k => $v) {
                    if ($k > 0) $qry .= " AND";
                    $qry .= " $v";
                }
            }
        }

        $limitquery .= $qry;
        $limitquery .= " ORDER BY ". $sortkey;
        $limitquery .=  " LIMIT ". $to ." OFFSET ". $from;

        $p = print_r($params, true);

        $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($date, $title, $body, $releasediv, $updusr)
    {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_News#insert";
        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array( $date, $title, $body, $releasediv, $updusr, $_SERVER['REMOTE_ADDR'], 0);

        $sql  = "SELECT f_news_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($newscd, $date, $title, $body, $releasediv, $updusr, $updcnt)
    {
                    $log =& LogFactory::getLog();
                    $func_name = "Onedepo_Db_News#update";

                    $db = $this->dbaccess->doConnect();
                    $db->autoCommit(false);
        $params = array($newscd, $date, $title, $body, $releasediv, $updusr, $_SERVER['REMOTE_ADDR'], $updcnt);

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

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

        $sql  = "SELECT f_news_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;
        }

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

}
?>
