<?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 temmoku;
use \temmoku\lib\cookie;
use \temmoku\lib\user;
use \temmoku\lib\template;
class controller
{
	protected $user=[];
	protected static $variables=[];
    function __construct()
    {
        if(!cookie::get('USER_First_Visit')){
        	cookie::set('USER_First_Visit',$_SERVER['REQUEST_TIME_FLOAT']);
        }
		$lock=APP_PATH.'conf/install.lock';
		if(is_file($lock)){
			
			if('admin'!=MODULE && 'user'!=MODULE){
				hook_listen('index_power_begin');
			}
			//验证普通会员状态
			$this->user=(new user)->User_Check();
			if('admin'!=MODULE && 'user'!=MODULE){
				hook_listen('index_power_end');
			}
		}
   	}
   	
    
    
    // 分配变量到视图
    public function assign($name, $value)
    {
        self::$variables[$name] = $value;
    }
	
	
    //模板渲染
	public function display($dir=''){
		ob_start();
		hook_listen('display_begin',['dir'=>$dir]);
		if(isset(self::$variables['json']) &&  self::$variables['json'] == true && ((isset($_POST['json']) && $_POST['json']==true) || (isset($_GET['json']) && $_GET['json']==true))){
			unset(self::$variables['json']);
			$json=json_encode(self::$variables);
			if( $_GET['callback']){
				exit($_GET['callback']."($json)");
			}
			exit( $json);
		}
		extract(self::$variables);
		//将友情链接循环出来
		if(C('Friend_List')){
		    foreach(C('Friend_List') AS $_KEY=>$VALUE){
			    $Friend['id_'.$_KEY]=$VALUE;
			}
		}
		//将导航取出来
		$HOME_MENU=C('HOME_MENU');
	    //加载模板引擎
		$dir=(new template)->display($dir);
	    include $dir;
		hook_listen('display_end');
	}
	
	//成功页面返回信息
    public function success($msg,$url=FromUrl,$time='10'){
    	if(!empty($_GET['json']) || !empty($_POST['json'])){
    		echo json_encode(['code'=>'0','text'=>$msg]);
    		exit;
    	}
    	if(cookie::get('From_url')){
			$url=cookie::get('From_url');
			cookie::del('From_url');
		}elseif(!$url){
    		$url=WEBURL;
    	}
    	$this->assign('msg', $msg);
    	$this->assign('url', $url);
    	$this->assign('time', $time);
    	$default= Main_View('success');
    	if(!is_file($default)){
    		$default=Temmoku.'/tpl/success.tpl';
    	}
    	$this->display($default);
		exit;
    }
    //错误页面
    public function err($msg){
    	if(!empty($_GET['json']) || !empty($_POST['json'])){
    		echo json_encode(['code'=>'9999','text'=>$msg]);
    	}else{
    		$this->assign('msg', $msg);
    		$default= Main_View('err');
    		if(!$default || !is_file($default)){
    			$default=Temmoku.'tpl/err.tpl';
    		}
	    	$this->display($default);
    	}
		exit;
    }
}
?>