/sop/2.0beta1/ThinkPHP/Lib/Think/Core/Action.class.php
PHP | 385 lines | 135 code | 20 blank | 230 comment | 27 complexity | 6fcf8e796349eeaa4599a93b5b71bf85 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: liu21st <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // $Id$
- /**
- +------------------------------------------------------------------------------
- * ThinkPHP Action????? ???
- +------------------------------------------------------------------------------
- * @category Think
- * @package Think
- * @subpackage Core
- * @author liu21st <liu21st@gmail.com>
- * @version $Id$
- +------------------------------------------------------------------------------
- */
- abstract class Action extends Think
- {//?????
- // ??????
- protected $view = null;
- // ??Action??
- private $name = '';
- /**
- +----------------------------------------------------------
- * ???? ????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- */
- public function __construct()
- {
- //??????
- $this->view = Think::instance('View');
- //??????
- if(method_exists($this,'_initialize'))
- $this->_initialize();
- }
- /**
- +----------------------------------------------------------
- * ????Action??
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- */
- protected function getActionName() {
- if(empty($this->name)) {
- // ??Action??
- $this->name = substr(get_class($this),0,-6);
- }
- return $this->name;
- }
- /**
- +----------------------------------------------------------
- * ??AJAX??
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @return bool
- +----------------------------------------------------------
- */
- protected function isAjax() {
- if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
- if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
- return true;
- }
- if(!empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')]))
- // ??Ajax????
- return true;
- return false;
- }
- /**
- +----------------------------------------------------------
- * ????
- * ??????????????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $templateFile ??????????
- * ???? ???????????
- * @param string $charset ????
- * @param string $contentType ????
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- protected function display($templateFile='',$charset='',$contentType='text/html')
- {
- $this->view->display($templateFile,$charset,$contentType);
- }
- /**
- +----------------------------------------------------------
- * ????????
- * ?????????fetch???
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $templateFile ??????????
- * ???? ???????????
- * @param string $charset ????
- * @param string $contentType ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- protected function fetch($templateFile='',$charset='',$contentType='text/html')
- {
- return $this->view->fetch($templateFile,$charset,$contentType);
- }
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @htmlfile ?????????
- * @htmlpath ?????????
- * @param string $templateFile ??????????
- * ???? ???????????
- * @param string $charset ????
- * @param string $contentType ????
- +----------------------------------------------------------
- * @return string
- +----------------------------------------------------------
- */
- protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='',$charset='',$contentType='text/html') {
- return $this->view->buildHtml($htmlfile,$htmlpath,$templateFile,$charset,$contentType);
- }
- /**
- +----------------------------------------------------------
- * ??????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param mixed $name ????????
- * @param mixed $value ????
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- protected function assign($name,$value='')
- {
- $this->view->assign($name,$value);
- }
- public function __set($name,$value) {
- $this->view->assign($name,$value);
- }
- /**
- +----------------------------------------------------------
- * ??????????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $name ??????
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- protected function get($name)
- {
- return $this->view->get($name);
- }
- /**
- +----------------------------------------------------------
- * Trace????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param mixed $name ????????
- * @param mixed $value ????
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- protected function trace($name,$value='')
- {
- $this->view->trace($name,$value);
- }
- /**
- +----------------------------------------------------------
- * ???? ????????????
- +----------------------------------------------------------
- * @access public
- +----------------------------------------------------------
- * @param string $method ???
- * @param array $parms ??
- +----------------------------------------------------------
- * @return mixed
- +----------------------------------------------------------
- */
- public function __call($method,$parms) {
- if( 0 === strcasecmp($method,ACTION_NAME)) {
- // ????????
- $_action = C('_actions_');
- if($_action) {
- // 'module:action'=>'callback'
- if(isset($_action[MODULE_NAME.':'.ACTION_NAME])) {
- $action = $_action[MODULE_NAME.':'.ACTION_NAME];
- }elseif(isset($_action[ACTION_NAME])){
- // 'action'=>'callback'
- $action = $_action[ACTION_NAME];
- }
- if(!empty($action)) {
- call_user_func($action);
- return ;
- }
- }
- // ?????_empty?? ???
- if(method_exists($this,'_empty')) {
- $this->_empty($method,$parms);
- }else {
- // ?????????? ?????????
- if(file_exists_case(C('TMPL_FILE_NAME')))
- $this->display();
- else
- // ????
- throw_exception(L('_ERROR_ACTION_').ACTION_NAME);
- }
- }elseif(in_array(strtolower($method),array('ispost','isget','ishead','isdelete','isput'))){
- return strtolower($_SERVER['REQUEST_METHOD']) == strtolower(substr($method,2));
- }else{
- throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
- }
- }
- /**
- +----------------------------------------------------------
- * ???????????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $message ????
- * @param Boolean $ajax ???Ajax??
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- protected function error($message,$ajax=false)
- {
- $this->_dispatch_jump($message,0,$ajax);
- }
- /**
- +----------------------------------------------------------
- * ???????????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $message ????
- * @param Boolean $ajax ???Ajax??
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- protected function success($message,$ajax=false)
- {
- $this->_dispatch_jump($message,1,$ajax);
- }
- /**
- +----------------------------------------------------------
- * Ajax??????????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param mixed $data ??????
- * @param String $info ????
- * @param boolean $status ????
- * @param String $status ajax???? JSON XML
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- protected function ajaxReturn($data,$info='',$status=1,$type='')
- {
- // ??AJAX?????????
- if(C('LOG_RECORD')) Log::save();
- $result = array();
- $result['status'] = $status;
- $result['info'] = $info;
- $result['data'] = $data;
- if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
- if(strtoupper($type)=='JSON') {
- // ??JSON???????? ??????
- header("Content-Type:text/html; charset=utf-8");
- exit(json_encode($result));
- }elseif(strtoupper($type)=='XML'){
- // ??xml????
- header("Content-Type:text/xml; charset=utf-8");
- exit(xml_encode($result));
- }elseif(strtoupper($type)=='EVAL'){
- // ??????js??
- header("Content-Type:text/html; charset=utf-8");
- exit($data);
- }else{
- // TODO ??????
- }
- }
- /**
- +----------------------------------------------------------
- * Action??(URL???? ???????????
- +----------------------------------------------------------
- * @access protected
- +----------------------------------------------------------
- * @param string $url ???URL???
- * @param array $params ??URL??
- * @param integer $delay ??????? ????
- * @param string $msg ??????
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- protected function redirect($url,$params=array(),$delay=0,$msg='') {
- if(C('LOG_RECORD')) Log::save();
- $url = U($url,$params);
- redirect($url,$delay,$msg);
- }
- /**
- +----------------------------------------------------------
- * ?????? ???????????
- * ?????? ???public?????success??
- * ???????? ??????
- +----------------------------------------------------------
- * @param string $message ????
- * @param Boolean $status ??
- * @param Boolean $ajax ???Ajax??
- +----------------------------------------------------------
- * @access private
- +----------------------------------------------------------
- * @return void
- +----------------------------------------------------------
- */
- private function _dispatch_jump($message,$status=1,$ajax=false)
- {
- // ?????AJAX??
- if($ajax || $this->isAjax()) $this->ajaxReturn('',$message,$status);
- // ????
- $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
- //??????????????????????
- if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
- $this->assign('status',$status); // ??
- $this->assign('message',$message);// ????
- //????????????
- C('HTML_CACHE_ON',false);
- if($status) { //??????
- // ?????????1?
- if(!$this->get('waitSecond')) $this->assign('waitSecond',"1");
- // ???????????????
- if(!$this->get('jumpUrl')) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
- $this->display(C('TMPL_ACTION_SUCCESS'));
- }else{
- //??????????3?
- if(!$this->get('waitSecond')) $this->assign('waitSecond',"3");
- // ??????????????
- if(!$this->get('jumpUrl')) $this->assign('jumpUrl',"javascript:history.back(-1);");
- $this->display(C('TMPL_ACTION_ERROR'));
- }
- if(C('LOG_RECORD')) Log::save();
- // ???? ?????????
- exit ;
- }
- }//?????
- ?>