<?php

class plus_labelMod extends commonMod
{
    
    // 标签管理
    public function index()
    {
        $list = $this->model->table('plus_label')->order('id desc')->select();
        $this->assign('list', $list);
        $this->display();
    }
    // 添加
    public function class_add()
    {
        $modlist = $this->model->table('model')->select();
        $this->assign('modlist', $modlist);  //模型列表
        $this->assign('cat', $this->getCat());  //栏目
        $this->display();
    }

    // 添加
    public function content_add()
    {
        $modlist = $this->model->table('model')->select();
        $this->assign('modlist', $modlist);  //模型列表
        $this->assign('cat', $this->getCat());  //栏目
        $this->display();
    }

    // 编辑
    public function class_edit()
    {
        $id=intval($_GET[0]);
        $condition['id']=$id;
        $info = $this->model->table('plus_label')->where($condition)->find();
        $modlist = $this->model->table('model')->select();
        $this->assign('modlist', $modlist);  //模型列表
        $this->assign('cat', $this->getCat());  //栏目
        $this->assign('info', $info);  //标签信息
        $this->assign('config', unserialize($info['config']));  //标签信息

        $this->display();
    }

    // 添加
    public function other_add()
    {
        $sql="SHOW TABLES FROM {$this->config['DB_NAME']}";
        $list = $this->model->query($sql); //执行查询
        $tables = array(); 
        while ($row = mysql_fetch_row($list)) {  
        $tables[] = str_replace($this->model->pre,'', $row[0]);
        }
        $this->assign('tables', $tables);  //模型列表
        $this->display();
    }

    // 编辑
    public function other_edit()
    {
        $id=intval($_GET[0]);
        $condition['id']=$id;
        $info = $this->model->table('plus_label')->where($condition)->find();

        $sql="SHOW TABLES FROM {$this->config['DB_NAME']}";
        $list = $this->model->query($sql); //执行查询
        $tables = array(); 
        while ($row = mysql_fetch_row($list)) {  
        $tables[] = str_replace($this->model->pre,'', $row[0]);
        }
        $this->assign('tables', $tables);  //模型列表

        $this->assign('info', $info);  //标签信息
        $this->assign('config', unserialize(html_out($info['config'])));  //标签信息
        $this->display();
    }

    public function getfields()
    {
        $table=$_POST['table'];
        $sql="describe {$this->model->pre}{$table}";
        $list = $this->model->query($sql); //执行查询
        while($rs=mysql_fetch_array($list)) 
        { 
        $fields.=$rs["Field"].','; 
        }
        echo substr($fields, 0,-1);
    }

    // 编辑
    public function content_edit()
    {
        $id=intval($_GET[0]);
        $condition['id']=$id;
        $info = $this->model->table('plus_label')->where($condition)->find();

        $modlist = $this->model->table('model')->select();
        $this->assign('modlist', $modlist);  //模型列表
        $this->assign('cat', $this->getCat());  //栏目
        $this->assign('info', $info);  //标签信息
        $this->assign('config', unserialize($info['config']));  //标签信息
        $this->display();
    }

    //录入
    public function add()
    {
        if(empty($_POST)){
            $this->jserror('数据传输错误！');
        }
        $data = array();
        $data['name'] = in($_POST['name']); //标签名称
        $data['label'] = in($_POST['label']); //标签标识

        if(empty($data['name'])){
            $this->jserror('名称不能为空！');
        }

        if(empty($data['label'])){
            $this->jserror('标签标识不能为空！');
        }

        $condition['label'] = $data['label'];
        $infocun = $count=$this->model->table('plus_label')->where($condition)->count();
        if ($infocun>0) {
            $this->jserror('标签标识不能重复！');
            return;
        }

        $data['type'] = intval($_POST['type']); //类型
        $data['content'] = html_in($_POST['content']); //内容

        foreach ($_POST as $key => $value) {
           if($key<>'content'){
           $config[$key] = html_in($value);
           }
        }
        $data['config']=serialize($config); //配置文件
        $this->model->table('plus_label')->data($data)->insert();
        $this->jssuccess('标签添加成功！');
    }

    //修改
    public function edit()
    {
        if(empty($_POST)){
            $this->jserror('数据传输错误！');
        }
        $id=intval($_POST['id']);
        $data = array();
        $data['name'] = in($_POST['name']); //标签名称
        $data['label'] = in($_POST['label']); //标签标识

        if(empty($data['name'])){
            $this->jserror('名称不能为空！');
        }

        if(empty($data['label'])){
            $this->jserror('标签标识不能为空！');
        }

        $condition3 = 'label="'.$data['label'].'" AND id<>'.$id;
        $info = $this->model->table('plus_label')->where($condition3)->find();

        if (!empty($info)) {
            $this->jserror('标签标识不能重复！');
        }

        $data['type'] = intval($_POST['type']); //类型
        $data['content'] = html_in($_POST['content']); //内容

        foreach ($_POST as $key => $value) {
           if($key<>'content'){
           $config[$key] = html_in($value);
           }
        }
        $data['config']=serialize($config); //配置文件
        $condition['id'] = $id;
        $this->model->table('plus_label')->data($data)->where($condition)->update();
        $this->jssuccess('标签编辑成功！');
    }


    //删除
    public function del()
    {
        $id=intval($_GET[0]);
        if(empty($id)){
            $this->error('数据传输错误！');
        }
        $condition['id'] = $id;
        $this->model->table('plus_label')->where($condition)->delete(); 
        $this->success('标签删除成功！');
        
    }

    public function getCat($id = 0)
    {
        $langid=$this->getlangid();
        if(!empty($langid)){
            $condition['lang']=$langid;
        }

        $data = $this->model->field('id,pid,name')->table('channel')->where($condition)->select();
        $cat = new Category(array('id', 'pid', 'name', 'cname')); 
        return $cat->getTree($data, $id);
    }

}

?>