<?php

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

    private $db;
    private $flink_table;

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

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

    /**
     * 获取一个友情链接信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getFlinkInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->flink_table WHERE flink_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getFlinkList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->flink_table");
        if ($url == '')
            $url = PHP_SELF . '?m=flink&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(10);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT flink_id, flink_title, flink_link, flink_type_radio, flink_state_radio, flink_addtime FROM $this->flink_table ORDER BY flink_id DESC LIMIT $limit");

        //友情状态数组
        $flink_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        //友情类型数组
        $flink_type_radio_ary = UtilityConfig::flink_type_radio_ary();
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['state_name'] = $flink_state_radio_ary[$v['flink_state_radio']];
                $rs[$k]['type_name'] = $flink_type_radio_ary[$v['flink_type_radio']];
                $rs[$k]['time'] = date('Y-m-d H:i:s', $v['flink_addtime']);
            }
        }

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

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

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

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

}