<?php 
defined('IN_MET') or exit ('No permission');
load::sys_class('admin');
load::sys_func('file');
class install extends admin{

    public $appno = 50002;
    public $ver = '1.0';
    public $appname = 'met_template';
    public $apptitle= '模板管理工具';
    public $description = '模板管理工具';
    
    public function __construct() {
        global $_M;
        parent::__construct();
    }

     /**
     * 注册应用
     * @return [type]
     */
    public function dosql() {

        global $_M;

        $hasapp = DB::get_one("SELECT * FROM {$_M['table']['applist']} WHERE no = '{$this->appno}'");

        if($hasapp) {
           
           $this->update();

        } else {
            
            $time = time();
            $query = "INSERT INTO `{$_M['table']['applist']}` SET
                    `id`        =  '',
                    `no`        =  '{$this->appno}',
                    `ver`       =  '{$this->ver}',                   
                    `m_name`    =  '{$this->appname}',
                    `m_class`   =  'temtool',
                    `m_action`  =  'dotemlist',
                    `appname`   =  '{$this->apptitle}', 
                    `info`      =  '{$this->description}',
                    `addtime`   =  {$time},
                    `updatetime`=  {$time}";
            DB::query($query);
            
        }   

    }

    /**
     * 如果存在app 只对版本号做更新
     * @param  [type] $ver 版本号
     * @return [type] 
     */
    public function update() {
        
        global $_M;

        DB::query("UPDATE {$_M['table']['applist']} SET ver = '{$this->ver}' WHERE no = '{$this->appno}'");
    }

    public function addConfig($name,$value)
    {
        global $_M;
        $query = "SELECT * FROM {$_M['table']['config']} WHERE  name='{$name}'";
        if(!DB::get_one($query))
        {
            $query = "INSERT INTO {$_M['table']['config']} (name,value,mobile_value,columnid,flashid,lang)VALUES ('{$name}', '{$value}', '', '0', '0', 'metinfo')";
            DB::query($query);
        }
    }


     /**
     * 卸载应用
     */
    public function dodel(){
        global $_M;

        $del_applist = "DELETE FROM {$_M['table']['applist']} WHERE no = {$this->appno}"; 
       
        DB::query($del_applist);
        deldir(PATH_WEB.$_M['config']['met_adminfile'].'/update/app/'.$this->appno);
        deldir('../app/app/'.$this->appname);
    }

    public function delConfig($name) {
        global $_M;
        $query = "DELETE FROM {$_M['table']['config']} WHERE name = '{$name}'";
        DB::query($query);

    }

}