PageRenderTime 35ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Inc/Lib/Think/Core/Action.class.php

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