<?php

class model_manageMod extends commonMod
{
    // 模型管理
    public function index()
    {
        $tpl_list=glob(__ADMINDIR__.'/module/*/modsetting.php');
        foreach($tpl_list as $key){
        $tpl_arry[]=require($key);
        }
        $this->assign('list', $tpl_arry);
        $this->display();
    }

    // 模型安装
    public function install()
    {
        
        //获取模型信息
        $file=in($_GET[0]);

        $setting = empty($setting) ? require (__ADMINDIR__.'/module/'.$file.'/modsetting.php') :$setting; //获取模型信息
        if(empty($setting)){
            $this->error('模块信息获取失败！');
        }

        $db_file = __ADMINDIR__.'/module/'.$file.'/install.sql';
        if(file_exists($db_file)){
        $this->_runSql(Install::mysql($db_file));  //安装数据库
        }

        $this->success('安装成功！');
    }

    // 模块卸载
    public function uninstall()
    {
        $file=in($_GET[0]);
        
        $setting = empty($setting) ? require (__ADMINDIR__.'/module/'.$file.'/modsetting.php') :$setting; //获取模型信息
        if(empty($setting)){
            $this->error('模块信息获取失败！');
        }

        $condition['mark']=$setting['FILE_NAME'];
        $info=$this->model->table('model')->where($condition)->find();

        $condition2['mid']=$info['id'];
        $count=$this->model->table('channel')->where($condition2)->count(); //检测栏目
        if($count>0){
           $this->error('请先删除该模型下的栏目！');
           return;
        }
        $condition3['id']=$info['id'];
        $this->model->table('model')->where($condition3)->delete();

        $db_file = __ADMINDIR__.'/module/'.$file.'/uninstall.sql';
        if(file_exists($db_file)){
        $this->_runSql(Install::mysql($db_file));  //安装数据库
        }

        //删除模型文件
        del_dir(__ADMINDIR__.'/module/'.$file);  //删除模块文件文件夹
        del_dir(__ADMINDIR__.'/template/'.$file); //删除模板文件夹

        $this->success('卸载成功！');
        
    }

    // 模型升级
    public function update()
    {
        //获取模型信息
        $file=in($_GET[0]);
        
        $setting = empty($setting) ? require (__ADMINDIR__.'/module/'.$file.'/modsetting.php') :$setting; //获取模型信息
        if(empty($setting)){
            $this->error('模块信息获取失败！');
        }

        $db_file = __ADMINDIR__.'/module/'.$file.'/upinstall.sql';
        if(file_exists($db_file)){
        $this->_runSql(Install::mysql($db_file));  //安装数据库
        }

        $this->success('升级成功！');
        
    }

   // 模型关闭
    public function switchoff()
    {
        $file = in($_GET[0]);
        if (empty($file)) {
            $this->error('参数传递错误！');
            return;
        }
        $this->modelsetting($file,'OPEN',false); 

        $condition3 = array();
        $condition3['mark'] = $file;
        $data3['switch']=0;
        $this->model->table('model')->data($data3)->where($condition3)->update(); //修改菜单状态

        $this->success('模型停用成功！');

    }  

    // 模型开启
    public function switchon()
    {
        $file = in($_GET[0]);
        if (empty($file)) {
            $this->error('参数传递错误！');
            return;
        }

        $this->modelsetting($file,'OPEN',true); 

        $condition3 = array();
        $condition3['mark'] = $file;
        $data3['switch']=1;
        $this->model->table('model')->data($data3)->where($condition3)->update(); //修改菜单状态

        $this->success('模型开启成功！');

    } 

}

?>