123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- namespace Think;
- abstract class Controller {
-
-
- protected $view = null;
-
-
- protected $config = array();
-
- public function __construct() {
-
- $this->view = Think::instance('Think\View');
-
- if(method_exists($this,'_initialize'))
- $this->_initialize();
- }
-
- protected function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {
- $this->view->display($templateFile,$charset,$contentType,$content,$prefix);
- }
-
- protected function show($content,$charset='',$contentType='',$prefix='') {
- $this->view->display('',$charset,$contentType,$content,$prefix);
- }
-
- protected function fetch($templateFile='',$content='',$prefix='') {
- return $this->view->fetch($templateFile,$content,$prefix);
- }
-
- protected function theme($theme){
- $this->view->theme($theme);
- return $this;
- }
-
- protected function assign($name,$value='') {
- $this->view->assign($name,$value);
- return $this;
- }
- public function __set($name,$value) {
- $this->assign($name,$value);
- }
-
- public function get($name='') {
- return $this->view->get($name);
- }
- public function __get($name) {
- return $this->get($name);
- }
-
- public function __isset($name) {
- return $this->get($name);
- }
-
- public function __call($method,$args) {
- if( 0 === strcasecmp($method,ACTION_NAME.C('ACTION_SUFFIX'))) {
- if(method_exists($this,'_empty')) {
-
- $this->_empty($method,$args);
- }elseif(file_exists_case($this->view->parseTemplate())){
-
- $this->display();
- }else{
- E(L('_ERROR_ACTION_').':'.ACTION_NAME);
- }
- }else{
- E(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
- return;
- }
- }
-
- protected function error($message='',$jumpUrl='',$ajax=false) {
- $this->dispatchJump($message,0,$jumpUrl,$ajax);
- }
-
- protected function success($message='',$jumpUrl='',$ajax=false) {
- $this->dispatchJump($message,1,$jumpUrl,$ajax);
- }
-
- protected function ajaxReturn($data,$type='',$json_option=0) {
- if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
- switch (strtoupper($type)){
- case 'JSON' :
-
- header('Content-Type:application/json; charset=utf-8');
- $data = json_encode($data,$json_option);
- break;
- case 'JSONP':
-
- header('Content-Type:application/json; charset=utf-8');
- $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER');
- $data = $handler.'('.json_encode($data,$json_option).');';
- break;
- case 'EVAL' :
-
- header('Content-Type:text/html; charset=utf-8');
- break;
- }
- exit($data);
- }
-
- protected function redirect($url,$params=array(),$delay=0,$msg='') {
- $url = U($url,$params);
- redirect($url,$delay,$msg);
- }
-
- private function dispatchJump($message,$status=1,$jumpUrl='',$ajax=false) {
- if(true === $ajax || IS_AJAX) {
- $data = is_array($ajax)?$ajax:array();
- $data['info'] = $message;
- $data['status'] = $status;
- $data['url'] = $jumpUrl;
- $this->ajaxReturn($data);
- }
- if(is_int($ajax)) $this->assign('waitSecond',$ajax);
- if(!empty($jumpUrl)) $this->assign('jumpUrl',$jumpUrl);
-
- $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
-
- if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
- $this->assign('status',$status);
-
- C('HTML_CACHE_ON',false);
- if($status) {
- $this->assign('message',$message);
-
- if(!isset($this->waitSecond)) $this->assign('waitSecond','1');
-
- if(!isset($this->jumpUrl)) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
- $this->display(C('TMPL_ACTION_SUCCESS'));
- }else{
- $this->assign('error',$message);
-
- if(!isset($this->waitSecond)) $this->assign('waitSecond','3');
-
- if(!isset($this->jumpUrl)) $this->assign('jumpUrl',"javascript:history.back(-1);");
- $this->display(C('TMPL_ACTION_ERROR'));
-
- exit ;
- }
- }
- }
|