<?php
namespace KIF\Core;

/**
 * 定义MVC里的C抽象类,即Controller
 * @author gaoxiaogang@gmail.com
 */
use KIF\String\Filter;

use KIF\Core\View;
use Exception;

abstract class Controller {
	protected $action;
	
	/**
	 * 
	 * 所有输出到模版的变量,都存放到这里面,以便统一管理
	 * @var array
	 */
	protected $output = array();
	
	protected $tpl;
	
	/**
     * 导航菜单
     *
     * @var Array
     */
    protected $navMenus = array();
	
	/**
	 * 
	 * 控制器入口方法
	 * 
	 */
	abstract public function run();
	
	/**
	 * 
	 * 设定指定的名、值,模版里可以使用到
	 * @param string $name
	 * @param mixed $value
	 * @throws Exception
	 */
	protected function setOutput($name, $value) {
        if (!is_string($name)) {
            throw new Exception('set output to template error, name not string !');
        }
        $this->output[$name] = $value;
    }
    
    /**
     * 
     * 设置行为
     * @param string $action
     */
    public function setAction($action) {
    	if (empty($action)) {
    		$action = 'default';
    	}
    	$this->action = 'do' . ucfirst($action);
    }
    
	/**
     * 添加导航栏
     *
     * @param string $title
     * @param string $href
     * @param string $target
     * @param string $icon
     * @return \KIF\Core\Controller
     */
    protected function addNavMenu($title, $href = null, $target = '_self', $icon = null) {
        $this->navMenus[] = array(
            'title' => $title,
            'href' => $href,
        	'target'	=> $target,
            'icon' => $icon,
        );
        return $this;
    }
    
    /**
     * 
     * 设置一批输出
     * @param array $outputs
     * 
     */
    protected function setOutputs(array $outputs) {
    	$this->output = array_merge($this->output, $outputs);
    }
	
	/**
	 * 
	 * 渲染结果
	 * @param boolean $return 是否返回。true:返回渲染结果;false:直接输出结果
	 * @return string | NULL
	 */
	public function render($return = false) {
		if (!$this->tpl) {// 没有指定模文件
			return null;
		}
		$objView = new View();
		
		$this->output['navMenus'] = $this->navMenus;
		
		$config = Config::getInstance()->current();
		$this->output['web_cfg'] = $config['web_cfg'];
		
		$objView->assign($this->output);
		
		if ($return) {
			return $objView->r($this->tpl);
		} else {
			$objView->d($this->tpl);
		}
	}
	
	/**
	 *
	 * 操作失败
	 * !!如果页面请求的参数里含有 cross_cb,则认为是需要做跨域ajax的支持,跳转到 cross_cb 参数里指定的url
	 * @param string $msg 失败描述
	 */
	protected function ajax_fail_exit($msg) {
		$return = array(
			'ok'	=> false,
			'msg'	=> $msg,
		);

		# 支持跨域的AJAX GET,其实是基于jQuery的$.getJson实现的。之所以把AJAX POST、GET做不同实现,是出于性能和可用性考虑。
		$jsonp_cb = Request::r('jsonp_cb', Filter::TRIM);
		if ($jsonp_cb) {
			$jsonp_cb = Filter::htmlspecialchars($jsonp_cb);// 防止css漏洞
			$this->echo_exit("{$jsonp_cb}(".json_encode($return).")");
		}

		$this->echo_exit(json_encode($return));
	}
	
	/**
	 *
	 * 操作成功
	 * !!如果页面请求的参数里含有 cross_cb,则认为是需要做跨域ajax的支持,跳转到 cross_cb 参数里指定的url
	 * @param string $msg 成功描述
	 */
	protected function ajax_success_exit($msg) {
		$return = array(
			'ok'	=> true,
			'msg'	=> $msg,
		);

		# 支持跨域的AJAX GET,其实是基于jQuery的$.getJson实现的。之所以把AJAX POST、GET做不同实现,是出于性能和可用性考虑。
		$jsonp_cb = Request::r('jsonp_cb', Filter::TRIM);
		if ($jsonp_cb) {
			$jsonp_cb = Filter::htmlspecialchars($jsonp_cb);// 防止css漏洞
			$this->echo_exit("{$jsonp_cb}(".json_encode($return).")");
		}

		$this->echo_exit(json_encode($return));
	}
	
	protected function echo_exit($msg) {
		echo $msg;
		exit;
	}
	
	protected function echo_msg($msg) {
		echo '['.date('Y-m-d H:i:s').'] '. $msg;
		$this->newline();
	}
	
	/**
	 * 
	 * 输出新行。
	 */
	protected function newline() {
		if (Request::isCLI()) {
			echo "\r\n";
		} else {
			echo "<br />";
		}
	}
	
	/**
	 * 链接跳转
	 * @param string $url 跳转的url
	 * @return void
	 **/
	protected function redirect($url,$status ='') {
		if ($status == '301') {
			header("HTTP/1.1 301 Moved Permanently");
		}
		if (!empty($url)) {
			header("Location: ".$url."");
		}
		exit;
	}
	
	/**
	 * 输出错误消息 - 后台
	 * @author li.shuming@kimiss.com
	 * @param string $msg
	 */
	protected function fail_exit_bs($msg = null) {
		$permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
		$this->tpl = $permission_template_dir . '/prompt_message';
		$this->setOutputs(array(
				'type'	=> 'fail',
				'msg'	=> $msg,
				'referer'	=> Request::referer(),
				'header_tpl'=> $permission_template_dir . '/header.html',
				'bottom_tpl'=> $permission_template_dir . '/bottom.html',
		));
		$this->render();
		exit;
	}
	
	/**
	 * 输出成功消息 - 后台
	 * @author li.shuming@kimiss.com
	 * @param string $msg
	 */
	protected function success_exit_bs($msg = null) {
		$permission_template_dir = Config::getInstance()->get('App_Path') . DS . 'template_dir';
		$this->tpl = $permission_template_dir . '/prompt_message';
		$this->setOutputs(array(
				'type'	=> 'success',
				'msg'	=> $msg,
				'referer'	=> Request::referer(),
				'header_tpl'=> $permission_template_dir . '/header.html',
				'bottom_tpl'=> $permission_template_dir . '/bottom.html',
		));
		$this->render();
		exit;
	}
}