<?php

/**
 * 后台广告管理模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: ad.class.php 50 2011-12-05 02:56:32Z zhangwentao $
 * http://www.t-mac.org；
 */
class ad extends Model
{

    private $db;
    private $ad_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->ad_table = DB_PREFIX . 'ad';
    }

    /**
     * 保存
     * @param string $info
     * return Boole
     */
    public function addAd($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->ad_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个广告的详细信息
     * @param int $ad_id 栏目id
     * return array
     */
    public function getAdInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->ad_table WHERE ad_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有广告
     * return article_class,pages
     */
    public function getAdList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->ad_table");
        if ($url == '')
            $url = PHP_SELF . '?m=ad&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(10);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT ad_id, ad_area, ad_title, ad_link, ad_state_radio, ad_type_radio, ad_addtime FROM $this->ad_table ORDER BY ad_id DESC LIMIT $limit");
        //广告状态数组
        $ad_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        //广告类型数组
        $ad_type_radio_ary = UtilityConfig::ad_type_radio_ary();
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['state_name'] = $ad_state_radio_ary[$v['ad_state_radio']];
                $rs[$k]['type_name'] = $ad_type_radio_ary[$v['ad_type_radio']];
                $rs[$k]['time'] = date('Y-m-d H:i:s', $v['ad_addtime']);
            }
        }
        //把文章的当前page写到cookies里
        HttpResponse::setCookie('ad_page', $pages->getNowPage());

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无资讯文章!   <a href='" . PHP_SELF . "?m=ad.add' class='link_a'>点我添加广告</a>";
        }

        $result = array('rs' => $rs, 'pageCurrent' => $pages->getNowPage(), 'page' => $pages->show(), 'ErrorMsg' => $ErrorMsg);
        return $result;
    }

    /**
     * del
     * @param int $id
     */
    public function delAd($id)
    {
        $sql = "DELETE FROM $this->ad_table WHERE ad_id in ($id)";
        $rs = $this->db->execute($sql);
        return $rs;
    }

    //取出configbody字段里的值
    public function getCfgBody($value, $configbody)
    {
        preg_match_all('/{' . $value . ' key="(.*)" url="(.*)"}(.*){\/' . $value . '}/isU', $configbody, $result);
        return $result;
    }

    /**
     * 判断ad_name唯一性的
     * @param type $ad_name
     * @return type 
     */
    public function checkAdName($ad_name)
    {
        $sql = "SELECT ad_id FROM $this->ad_table WHERE ad_name = '$ad_name'";
        $ad_id = $this->db->getOne($sql);
        return $ad_id;
    }

}