<?php

/**
 * 后台文章模块 Model
 * ============================================================================
 * zhuna_php 住哪网酒店分销联盟程序php版　BY Tmac PHP MVC framework
 * $Author: zhangwentao $  <zwttmac@qq.com>
 * $Id: article.class.php 50 2011-12-05 02:56:32Z zhangwentao $
 * http://un.zhuna.cn；
 */
class article extends Model
{

    private $db;
    private $class_table;
    private $article_table;
    private $city_table;

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

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

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getArticleInfo($aid)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->article_table WHERE aid = $aid LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取资讯栏目
     * return article_class,pages
     */
    public function getClass()
    {
        $rs = $this->db->cacheGetAll("SELECT * FROM $this->class_table ORDER BY orderid DESC, class_id DESC", 10);
        return $rs;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getArticleList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->article_table");
        if ($url == '')
            $url = PHP_SELF . '?m=article&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(15);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT aid, class_id, title, content, state_radio, time FROM $this->article_table ORDER BY aid DESC LIMIT $limit");
        //取所有的资讯栏目 不用LEFT JOIN取class_name
        $rs_class = $this->getClass();
        $ary_class = array();
        foreach ($rs_class AS $kk => $vv) {
            $ary_class[$vv['class_id']] = $vv['class_name'];
        }
        //推荐状态数组
        $state_radio_ary = UtilityConfig::state_radio_ary();
        //遍历通过class_id取class_name
        foreach ($rs AS $k => $v) {
            $rs[$k]['class_name'] = $ary_class[$v['class_id']];
            $rs[$k]['state_name'] = $state_radio_ary[$v['state_radio']];
            $rs[$k]['time'] = date('Y/m/d H:i:s', $v['time']);
        }

        //把文章的当前page写到cookies里
        HttpResponse::setCookie('article_page', $pages->getNowPage());

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

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

    /**
     * 取省列表 或根据省id取城市列表
     * @param <type> $type  默认c取city列表 p取province列表
     * @param <int>  $pid   省级id
     * @return array
     */
    public function getCity($type = 'c', $pid = null)
    {
        if ($type == 'p') {
            $rs = $this->db->getAll("SELECT Pid, Pname, suoxie FROM $this->city_table WHERE province = 1");
        } else {
            $rs = $this->db->getAll("SELECT cityid, cityname, suoxie FROM $this->city_table WHERE pid = $pid ORDER BY province DESC, abcd ASC");
        }
        return $rs;
    }

    /**
     * del
     * @param int $class_id
     */
    public function delArticle($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->article_table WHERE aid in ($id)");
        return $rs;
    }

}