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


/**
 * 検索キーワード テーブル関連
 *
 * @package     Onedepo.Db.Searchkey
 * @author      netnee H.Watanabe <hiroshi.watanabe@netnee.jp>
 * @access      public
 */
class Onedepo_Db_Searchkey
{
    /**
     * DBアクセスオブジェクト
     *
     * @var     object
     * @access  private
     */
    var $dbaccess = null;

    /**
     * 検索キーワード追加
     * 
     * @param string $table テーブル名
     * @return object 取得レコード
     */
    function insert($itemcd, $usercd, $serchkey, $upduser) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Searchkey#insert";

        $db = $this->dbaccess->doConnect();
        $db->autoCommit();
        $params = array($itemcd, $usercd, $serchkey, $upduser, $_SERVER['REMOTE_ADDR'] );
        $sql  = "select * from f_search_key_insert(?, ?, ?, ?, ?);";

        $res = $db->getOne($sql, $params);
        
        if (DB::isError($res) || $res < 0) {
            $log->error("rollback ". $res->userinfo, $func_name);
            $log->error("rollback ". print_r($res,true), $func_name);
            $db->rollback();
            $this->dbaccess->doClose($db);
            return false;
        }
        $log->debug("commit", $func_name);

        $db->commit();
        $this->dbaccess->doClose($db);
        return true;
    }
    
    /**
     * ポータル用人気キーワード取得
     * 
     * @param   int     $recordlmt   取得件数制限値
     * @return object 取得レコード
     */
    function selectPopularKeyword($shopdiv=null, $recordlmt=SIDEMENU_KEYWORD_RECORD_LIMIT) {
        $log =& LogFactory::getLog();
        $func_name = "Onedepo_Db_Searchkey#selectPopularKeyword";
        
        $db = $this->dbaccess->doConnect();
        $db->setFetchMode(DB_FETCHMODE_ASSOC);
        
        $params = array($recordlmt);
        
        $sql  = "   SELECT searchkey, COUNT(*) as count ";
        $sql .= "     FROM t_search_key ";
        $sql .= "    WHERE delflg = FALSE ";
        $sql .= " GROUP BY searchkey ";
        $sql .= " ORDER BY count DESC ";
        $sql .= "    LIMIT ?; ";
        
        $data = $db->getAll($sql, $params);
        if (DB::isError($data)) {
            $log->debug($data->userinfo, $func_name);
            die($data->getMessage());
        }
        
        $this->dbaccess->doClose($db);
        
        return $data;
    
    }
    
}
?>
