<?php

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

    private $db;
    private $keywords_table;

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

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

    /**
     * 获取一个关键字的详细信息
     * @param int $k_id 栏目id
     * return array
     */
    public function getKeywordsInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->keywords_table WHERE k_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有关键字
     * return article_class,pages
     */
    public function getKeywordsList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->keywords_table");
        if ($url == '')
            $url = PHP_SELF . '?m=keywords&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(15);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT k_id, k_pagename, k_page, k_time FROM $this->keywords_table ORDER BY k_id ASC LIMIT $limit");
        foreach ($rs AS $k => $v) {
            $rs[$k]['time'] = date('Y-m-d H:i:s', $v['k_time']);
        }

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

        $ErrorMsg = '';
        if ($count == 0) {
            $ErrorMsg = "暂无关键字!   <a href='" . PHP_SELF . "?m=keywords.add' class='link_a'>点我添加关键字</a>";
        }

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

}