<?php

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

    private $db;
    private $manage_table;
    private $admintype_table;

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

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

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

    public function checkManageName($username, $id)
    {
        $sql = "SELECT * FROM $this->manage_table WHERE username = '{$username}'";
        if ($id <> 0)
            $sql .= " AND id <> $id";
        $sql .= " LIMIT 0,1";
        $rs = $this->db->getRow($sql);
        if ($rs) {
            $this->redirect('管理员名称重复啦!');
            exit;
        }
    }

    /**
     * 获取所有管理员
     * return article_class,pages
     */
    public function getManageList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->manage_table");
        if ($url == '')
            $url = PHP_SELF . '?m=manage&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(10);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT * FROM $this->manage_table ORDER BY id ASC LIMIT $limit");

        //取管理员类型option数组
        $admintype_ary = $this->getAdminType();
        $admintype_array = array();
        foreach ($admintype_ary AS $kk => $vv) {
            $admintype_array[$vv['rank']] = $vv['typename'];
        }

        foreach ($rs AS $k => $v) {
            $rs[$k]['typename'] = $admintype_array[$v['rank']];
            $rs[$k]['time'] = date('Y-m-d H:i:s', $v['logintime']);
        }

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

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

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

    /**
     * 取管理员类别
     */
    public function getAdminType()
    {
        $rs = $this->db->cacheGetAll("SELECT * FROM $this->admintype_table ORDER BY rank ASC", 86400);
        return $rs;
    }

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

}