PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/sop/2.0beta1/ThinkPHP/Lib/Think/Core/Action.class.php

http://iiccms.googlecode.com/
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
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ThinkPHP Action????? ???
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Core
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. abstract class Action extends Think
  24. {//?????
  25. // ??????
  26. protected $view = null;
  27. // ??Action??
  28. private $name = '';
  29. /**
  30. +----------------------------------------------------------
  31. * ???? ????????
  32. +----------------------------------------------------------
  33. * @access public
  34. +----------------------------------------------------------
  35. */
  36. public function __construct()
  37. {
  38. //??????
  39. $this->view = Think::instance('View');
  40. //??????
  41. if(method_exists($this,'_initialize'))
  42. $this->_initialize();
  43. }
  44. /**
  45. +----------------------------------------------------------
  46. * ????Action??
  47. +----------------------------------------------------------
  48. * @access protected
  49. +----------------------------------------------------------
  50. */
  51. protected function getActionName() {
  52. if(empty($this->name)) {
  53. // ??Action??
  54. $this->name = substr(get_class($this),0,-6);
  55. }
  56. return $this->name;
  57. }
  58. /**
  59. +----------------------------------------------------------
  60. * ??AJAX??
  61. +----------------------------------------------------------
  62. * @access protected
  63. +----------------------------------------------------------
  64. * @return bool
  65. +----------------------------------------------------------
  66. */
  67. protected function isAjax() {
  68. if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
  69. if('xmlhttprequest' == strtolower($_SERVER['HTTP_X_REQUESTED_WITH']))
  70. return true;
  71. }
  72. if(!empty($_POST[C('VAR_AJAX_SUBMIT')]) || !empty($_GET[C('VAR_AJAX_SUBMIT')]))
  73. // ??Ajax????
  74. return true;
  75. return false;
  76. }
  77. /**
  78. +----------------------------------------------------------
  79. * ????
  80. * ??????????????
  81. +----------------------------------------------------------
  82. * @access protected
  83. +----------------------------------------------------------
  84. * @param string $templateFile ??????????
  85. * ???? ???????????
  86. * @param string $charset ????
  87. * @param string $contentType ????
  88. +----------------------------------------------------------
  89. * @return void
  90. +----------------------------------------------------------
  91. */
  92. protected function display($templateFile='',$charset='',$contentType='text/html')
  93. {
  94. $this->view->display($templateFile,$charset,$contentType);
  95. }
  96. /**
  97. +----------------------------------------------------------
  98. * ????????
  99. * ?????????fetch???
  100. +----------------------------------------------------------
  101. * @access protected
  102. +----------------------------------------------------------
  103. * @param string $templateFile ??????????
  104. * ???? ???????????
  105. * @param string $charset ????
  106. * @param string $contentType ????
  107. +----------------------------------------------------------
  108. * @return string
  109. +----------------------------------------------------------
  110. */
  111. protected function fetch($templateFile='',$charset='',$contentType='text/html')
  112. {
  113. return $this->view->fetch($templateFile,$charset,$contentType);
  114. }
  115. /**
  116. +----------------------------------------------------------
  117. * ??????
  118. +----------------------------------------------------------
  119. * @access protected
  120. +----------------------------------------------------------
  121. * @htmlfile ?????????
  122. * @htmlpath ?????????
  123. * @param string $templateFile ??????????
  124. * ???? ???????????
  125. * @param string $charset ????
  126. * @param string $contentType ????
  127. +----------------------------------------------------------
  128. * @return string
  129. +----------------------------------------------------------
  130. */
  131. protected function buildHtml($htmlfile='',$htmlpath='',$templateFile='',$charset='',$contentType='text/html') {
  132. return $this->view->buildHtml($htmlfile,$htmlpath,$templateFile,$charset,$contentType);
  133. }
  134. /**
  135. +----------------------------------------------------------
  136. * ??????
  137. +----------------------------------------------------------
  138. * @access protected
  139. +----------------------------------------------------------
  140. * @param mixed $name ????????
  141. * @param mixed $value ????
  142. +----------------------------------------------------------
  143. * @return void
  144. +----------------------------------------------------------
  145. */
  146. protected function assign($name,$value='')
  147. {
  148. $this->view->assign($name,$value);
  149. }
  150. public function __set($name,$value) {
  151. $this->view->assign($name,$value);
  152. }
  153. /**
  154. +----------------------------------------------------------
  155. * ??????????
  156. +----------------------------------------------------------
  157. * @access protected
  158. +----------------------------------------------------------
  159. * @param string $name ??????
  160. +----------------------------------------------------------
  161. * @return mixed
  162. +----------------------------------------------------------
  163. */
  164. protected function get($name)
  165. {
  166. return $this->view->get($name);
  167. }
  168. /**
  169. +----------------------------------------------------------
  170. * Trace????
  171. +----------------------------------------------------------
  172. * @access protected
  173. +----------------------------------------------------------
  174. * @param mixed $name ????????
  175. * @param mixed $value ????
  176. +----------------------------------------------------------
  177. * @return void
  178. +----------------------------------------------------------
  179. */
  180. protected function trace($name,$value='')
  181. {
  182. $this->view->trace($name,$value);
  183. }
  184. /**
  185. +----------------------------------------------------------
  186. * ???? ????????????
  187. +----------------------------------------------------------
  188. * @access public
  189. +----------------------------------------------------------
  190. * @param string $method ???
  191. * @param array $parms ??
  192. +----------------------------------------------------------
  193. * @return mixed
  194. +----------------------------------------------------------
  195. */
  196. public function __call($method,$parms) {
  197. if( 0 === strcasecmp($method,ACTION_NAME)) {
  198. // ????????
  199. $_action = C('_actions_');
  200. if($_action) {
  201. // 'module:action'=>'callback'
  202. if(isset($_action[MODULE_NAME.':'.ACTION_NAME])) {
  203. $action = $_action[MODULE_NAME.':'.ACTION_NAME];
  204. }elseif(isset($_action[ACTION_NAME])){
  205. // 'action'=>'callback'
  206. $action = $_action[ACTION_NAME];
  207. }
  208. if(!empty($action)) {
  209. call_user_func($action);
  210. return ;
  211. }
  212. }
  213. // ?????_empty?? ???
  214. if(method_exists($this,'_empty')) {
  215. $this->_empty($method,$parms);
  216. }else {
  217. // ?????????? ?????????
  218. if(file_exists_case(C('TMPL_FILE_NAME')))
  219. $this->display();
  220. else
  221. // ????
  222. throw_exception(L('_ERROR_ACTION_').ACTION_NAME);
  223. }
  224. }elseif(in_array(strtolower($method),array('ispost','isget','ishead','isdelete','isput'))){
  225. return strtolower($_SERVER['REQUEST_METHOD']) == strtolower(substr($method,2));
  226. }else{
  227. throw_exception(__CLASS__.':'.$method.L('_METHOD_NOT_EXIST_'));
  228. }
  229. }
  230. /**
  231. +----------------------------------------------------------
  232. * ???????????
  233. +----------------------------------------------------------
  234. * @access protected
  235. +----------------------------------------------------------
  236. * @param string $message ????
  237. * @param Boolean $ajax ???Ajax??
  238. +----------------------------------------------------------
  239. * @return void
  240. +----------------------------------------------------------
  241. */
  242. protected function error($message,$ajax=false)
  243. {
  244. $this->_dispatch_jump($message,0,$ajax);
  245. }
  246. /**
  247. +----------------------------------------------------------
  248. * ???????????
  249. +----------------------------------------------------------
  250. * @access protected
  251. +----------------------------------------------------------
  252. * @param string $message ????
  253. * @param Boolean $ajax ???Ajax??
  254. +----------------------------------------------------------
  255. * @return void
  256. +----------------------------------------------------------
  257. */
  258. protected function success($message,$ajax=false)
  259. {
  260. $this->_dispatch_jump($message,1,$ajax);
  261. }
  262. /**
  263. +----------------------------------------------------------
  264. * Ajax??????????
  265. +----------------------------------------------------------
  266. * @access protected
  267. +----------------------------------------------------------
  268. * @param mixed $data ??????
  269. * @param String $info ????
  270. * @param boolean $status ????
  271. * @param String $status ajax???? JSON XML
  272. +----------------------------------------------------------
  273. * @return void
  274. +----------------------------------------------------------
  275. */
  276. protected function ajaxReturn($data,$info='',$status=1,$type='')
  277. {
  278. // ??AJAX?????????
  279. if(C('LOG_RECORD')) Log::save();
  280. $result = array();
  281. $result['status'] = $status;
  282. $result['info'] = $info;
  283. $result['data'] = $data;
  284. if(empty($type)) $type = C('DEFAULT_AJAX_RETURN');
  285. if(strtoupper($type)=='JSON') {
  286. // ??JSON???????? ??????
  287. header("Content-Type:text/html; charset=utf-8");
  288. exit(json_encode($result));
  289. }elseif(strtoupper($type)=='XML'){
  290. // ??xml????
  291. header("Content-Type:text/xml; charset=utf-8");
  292. exit(xml_encode($result));
  293. }elseif(strtoupper($type)=='EVAL'){
  294. // ??????js??
  295. header("Content-Type:text/html; charset=utf-8");
  296. exit($data);
  297. }else{
  298. // TODO ??????
  299. }
  300. }
  301. /**
  302. +----------------------------------------------------------
  303. * Action??(URL???? ???????????
  304. +----------------------------------------------------------
  305. * @access protected
  306. +----------------------------------------------------------
  307. * @param string $url ???URL???
  308. * @param array $params ??URL??
  309. * @param integer $delay ??????? ????
  310. * @param string $msg ??????
  311. +----------------------------------------------------------
  312. * @return void
  313. +----------------------------------------------------------
  314. */
  315. protected function redirect($url,$params=array(),$delay=0,$msg='') {
  316. if(C('LOG_RECORD')) Log::save();
  317. $url = U($url,$params);
  318. redirect($url,$delay,$msg);
  319. }
  320. /**
  321. +----------------------------------------------------------
  322. * ?????? ???????????
  323. * ?????? ???public?????success??
  324. * ???????? ??????
  325. +----------------------------------------------------------
  326. * @param string $message ????
  327. * @param Boolean $status ??
  328. * @param Boolean $ajax ???Ajax??
  329. +----------------------------------------------------------
  330. * @access private
  331. +----------------------------------------------------------
  332. * @return void
  333. +----------------------------------------------------------
  334. */
  335. private function _dispatch_jump($message,$status=1,$ajax=false)
  336. {
  337. // ?????AJAX??
  338. if($ajax || $this->isAjax()) $this->ajaxReturn('',$message,$status);
  339. // ????
  340. $this->assign('msgTitle',$status? L('_OPERATION_SUCCESS_') : L('_OPERATION_FAIL_'));
  341. //??????????????????????
  342. if($this->get('closeWin')) $this->assign('jumpUrl','javascript:window.close();');
  343. $this->assign('status',$status); // ??
  344. $this->assign('message',$message);// ????
  345. //????????????
  346. C('HTML_CACHE_ON',false);
  347. if($status) { //??????
  348. // ?????????1?
  349. if(!$this->get('waitSecond')) $this->assign('waitSecond',"1");
  350. // ???????????????
  351. if(!$this->get('jumpUrl')) $this->assign("jumpUrl",$_SERVER["HTTP_REFERER"]);
  352. $this->display(C('TMPL_ACTION_SUCCESS'));
  353. }else{
  354. //??????????3?
  355. if(!$this->get('waitSecond')) $this->assign('waitSecond',"3");
  356. // ??????????????
  357. if(!$this->get('jumpUrl')) $this->assign('jumpUrl',"javascript:history.back(-1);");
  358. $this->display(C('TMPL_ACTION_ERROR'));
  359. }
  360. if(C('LOG_RECORD')) Log::save();
  361. // ???? ?????????
  362. exit ;
  363. }
  364. }//?????
  365. ?>