<?php
if(!defined('EMLOG_ROOT')) {exit('error!');}
function callback_init(){
    $DB = Database::getInstance();
    $sql = "
	CREATE TABLE IF NOT EXISTS `".DB_PREFIX."Ixc` (
		`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
	    `gid` int(11) NULL COMMENT '数据ID',
	    `type` int(11) NULL DEFAULT '0' COMMENT '类型 (文章:0 分类:1 用户:2)',
	    `value` longtext NULL COMMENT '数据值',
		PRIMARY KEY (`id`)
	)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    $DB->query($sql);
	if($DB->num_rows($DB->query("show columns from ".DB_PREFIX."Ixc like 'type'"))==0){
		$DB->query("ALTER TABLE ".DB_PREFIX."Ixc ADD type int(11) NULL DEFAULT '0' COMMENT '类型 (文章:0 分类:1 用户:2)';");
	}
    $plugin_storage = Storage::getInstance('Ixc_field');
    $r = $plugin_storage->getValue('Ixc');
    if(empty($r)){
        $plugin_storage->setValue('Ixc', ['title'=> '字段选项','true'=> 0,'array'=> 0], 'array');
    }
}
function callback_rm() {
    $plugin_storage = Storage::getInstance('Ixc_field');
    $ak = $plugin_storage->deleteAllName('YES');
    $DB = Database::getInstance();
	$pluginModel = new Plugin_Model();
	$plugins = Option::get('active_plugins');
	$exist = false;
	if(is_array($plugins) && !empty($plugins)){
		foreach($plugins as $item){
			$plugin = $pluginModel->getPluginData($item);
			if($plugin['Plugin']=='Ixc_down' || $plugin['Plugin']=='Ixc_video')$exist = true;
		}
	}
	if($exist==true){
		$DB->query("UPDATE ".DB_PREFIX."Ixc SET value=''");
	}else{
		$DB->query("DROP TABLE IF EXISTS `".DB_PREFIX."Ixc`;");
	}
}
function callback_up(){
    $DB = Database::getInstance();
    $sql = "
	CREATE TABLE IF NOT EXISTS `".DB_PREFIX."Ixc` (
		`id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
	    `gid` int(11) NULL COMMENT '数据ID',
	    `type` int(11) NULL DEFAULT '0' COMMENT '类型 (文章:0 分类:1 用户:2)',
	    `value` longtext NULL COMMENT '数据值',
		PRIMARY KEY (`id`)
	)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    $DB->query($sql);
	$DB = Database::getInstance();
	if($DB->num_rows($DB->query("show columns from ".DB_PREFIX."Ixc like 'type'"))==0){
		$DB->query("ALTER TABLE ".DB_PREFIX."Ixc ADD type int(11) NULL DEFAULT '0' COMMENT '类型 (文章:0 分类:1 用户:2)';");
	}
    $plugin_storage = Storage::getInstance('Ixc_field');
    $r = $plugin_storage->getValue('Ixc');
    if(empty($r)){
        $plugin_storage->setValue('Ixc', ['title'=> '字段选项','true'=> 0,'array'=> 0], 'array');
    }
}