<?php

class module_manageMod extends commonMod
{
    // 显示后台管理菜单
    public function index()
    {
        $tpl_list=glob(__ADMINDIR__.'/module/*/setting.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.'/setting.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));  //安装数据库
        }

        //安装到模块
        $mod_data['NAME']=$setting['NAME'];
        $mod_data['FILE_NAME']=$setting['FILE_NAME'];
        $mod_data['VER']=$setting['VER'];
        $mod_data['AUTHOR']=$setting['AUTHOR'];
        $this->model->table('module')->data($mod_data)->insert();
        //安装到模块结束

        //安装添加权限
        $data['name']=$setting['NAME'];
        $data['module']=$file;
        $data['pid']=$setting['PRID'];//上级权限ID
        $rid=$this->model->table('resource')->data($data)->insert();//添加主权限
        $this->modulesetting($file,'RID',$rid); //设置模块权限

        $data5['pid']=$setting['PMID'];//上级菜单ID
        $data5['name']=$setting['NAME'];//模块名称
        $data5['url']='/'.$setting['FILE_NAME'];//模块URL
        $data5['target']=2;//模板菜单
        $data5['rid']=$rid;//权限
        $data5['mark']=$setting['FILE_NAME'];//标识
        $menuid=$this->model->table('admin_menu')->data($data5)->insert();
        $this->modulesetting($file,'MID',$menuid); //设置模块菜单

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

    // 模块卸载
    public function uninstall()
    {
        //获取模块信息
        $file=in($_GET[0]);

        $condition['FILE_NAME']=$file; //获取模块信息
        $info=$this->model->table('module')->where($condition)->find(); 
        if(empty($info)){
            $tihs->error('获取模块信息失败!');
        }
        
        //卸载菜单
        $condition2['id']=$info['MID'];
        $this->model->table('admin_menu')->where($condition2)->delete();

        //卸载权限
        $condition3['id']=$info['RID'];
        $this->model->table('resource')->where($condition3)->delete();

        //卸载模块
        $mod_condition['id']=$info['id'];
        $this->model->table('module')->where($mod_condition)->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]);
        $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->modulesetting($file,'OPEN',false); //改变状态

        $condition3 = array();
        $condition3['id'] = $this->modulesetting($file,'MID'); 
        $data3['status']=0;
        $this->model->table('admin_menu')->data($data3)->where($condition3)->update(); //修改菜单状态

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

    }  

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

        $this->modulesetting($file,'OPEN',true); //改变状态

        $condition3 = array();
        $condition3['id'] = $this->modulesetting($file,'MID'); 
        $data3['status']=1;
        $this->model->table('admin_menu')->data($data3)->where($condition3)->update(); //修改菜单状态

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

    } 


}

?>