<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\controller;
use \temmoku\admin;
use \admin\model\upgrade as upgrademodel;
class upgrade extends admin{
	
	public function all(){
		$get_modular=(new upgrademodel)->get_modular();
		$get_plugin=(new upgrademodel)->get_plugin();
		$arr=[
			'mvc'=>[
				'VERSION'=>C('VERSION_NUMBER'),
				'home'=>defined('Upgrade_home') ? 1 : 0
			],
			'modular'=>$get_modular,
			'plugin'=>$get_plugin,
			'weburl'=>WEBURL,
			'part'=>'1',
			'onelineip'=>onlineip,
			'cloud_code'=>C('cloud_code')
		];
		
		$_data=http_request(Upgrade_URL.'upgrade/search/',['data'=>serialize($arr)]);
		$_data=json_decode($_data,true);
		$this->assign('mvc', 'mvc');
		$this->assign('data', $_data);
		$this->display(tpl('upgrade/list'));
	}
	
	
	public function test_upgrade(){
		$get_modular=(new upgrademodel)->get_modular();
		$get_plugin=(new upgrademodel)->get_plugin();
		$arr=[
			'mvc'=>[
				'VERSION'=>C('VERSION_NUMBER'),
				'home'=>defined('Upgrade_home') ? 1 : 0
			],
			'modular'=>$get_modular,
			'plugin'=>$get_plugin,
			'weburl'=>WEBURL,
			'part'=>'1',
			'onelineip'=>onlineip,
			'cloud_code'=>C('cloud_code')
		];
		echo $_data=http_request(Upgrade_URL.'upgrade/test_upgrade/',['data'=>serialize($arr)]);
	}
	

	public function mvc(){
		$arr=[
			'mvc'=>[
				'VERSION'=>C('VERSION_NUMBER'),
				'home'=>defined('Upgrade_home') ? 1 : 0
			],
			'weburl'=>WEBURL,
			'part'=>'1',
			'onelineip'=>onlineip,
			'cloud_code'=>C('cloud_code')
		];
		$this->assign('mvc', 'mvc');
		
		$_data=http_request(Upgrade_URL.'upgrade/search/',['data'=>serialize($arr)]);
		$data=json_decode($_data,true);
		
		$this->assign('data', $data);
		$this->display(tpl('upgrade/list'));
	}
	
	public function post(){
		$post=[];
		if($_POST['iddbs']['mvc']){
			@ksort($_POST['iddbs']['mvc']);
			$i=0;
			$aids=[];
			foreach($_POST['iddbs']['mvc'] AS $aid=>$version){
				$i++;
				if($i==1){
					$data=['dir'=>'mvc','id'=>$aid,'version'=>$version];
				}
				$aids[$aid]=$version;
			}
			$post['mvc']=$aids;
		}
		if($_POST['iddbs']['modular']){
			$i=0;
			foreach($_POST['iddbs']['modular'] as $dir=>$row){
				@ksort($row);
				$i++;
				$ii=0;
				
				foreach($row AS $aid=>$version){
					$ii++;
					if($i==1 && $ii==1 && !$_POST['iddbs']['mvc']){
						$data=['dir'=>'modular','id'=>$aid,'name'=>$dir,'version'=>$version];
					}
					$ddd[$aid]=$version;
				}
				$modular[$dir]=$ddd;
				unset($ddd);
			}
			$post['modular']=$modular;
		}
		if($_POST['iddbs']['plugin']){
			$i=0;
			foreach($_POST['iddbs']['plugin'] as $dir=>$row){
				@ksort($row);
				$i++;
				$ii=0;
				foreach($row AS $aid=>$version){
					$ii++;
					if($i==1 && $ii==1 && !$_POST['iddbs']['mvc'] && !$_POST['iddbs']['modular']){
						$data=['dir'=>'plugin','id'=>$aid,'name'=>$dir,'version'=>$version];
					}
					$ddd[$aid]=$version;
				}
				$plugin[$dir]=$ddd;
				unset($ddd);
			}
			$post['plugin']=$plugin;
		}
		$data['upgrade_temporary_number']=$_POST['upgrade_temporary_number'];
		$olddata=json_encode($post);
		$this->assign('olddata', $olddata);

		$this->assign('data', $data);
		$this->display(tpl('upgrade/updata'));
		
	}
	public function upgrade(){
		
		$olddata=json_decode(htmlspecialchars_decode($_POST['olddata']),true);
		if(!$olddata){
			$array=['code'=>'0','text'=>'数据错误'];
			echo json_encode($array);
			exit;
		}
		if($_POST['action']=='mvc'){
			(new upgrademodel)->upgarde_main($olddata);
		}elseif($_POST['action']=='modular'){
			(new upgrademodel)->upgarde_modular($olddata);
		}elseif($_POST['action']=='plugin'){
			(new upgrademodel)->upgarde_plugin($olddata);
		}
	}
	
	public function modular(){
		$get_modular=(new upgrademodel)->get_modular();
		$arr=[
			'modular'=>$get_modular,
			'weburl'=>WEBURL,
			'part'=>'1',
			'onelineip'=>onlineip,
			'cloud_code'=>C('cloud_code')
		];
		
		$_data=http_request(Upgrade_URL.'upgrade/search/',['data'=>serialize($arr)]);
		$data=json_decode($_data,true);
		$this->assign('data', $data);
		$this->display(tpl('upgrade/list'));
	}
	public function plugin(){
		$get_plugin=(new upgrademodel)->get_plugin();
		$arr=[
			'plugin'=>$get_plugin,
			'weburl'=>WEBURL,
			'part'=>'1',
			'onelineip'=>onlineip,
			'cloud_code'=>C('cloud_code')
		];
		
		$_data=http_request(Upgrade_URL.'upgrade/search/',['data'=>serialize($arr)]);
		$data=json_decode($_data,true);
		$this->assign('data', $data);
		$this->display(tpl('upgrade/list'));
	}

}
?>
