/core/classes/TBGAction.class.php

https://github.com/pb30/thebuggenie · PHP · 288 lines · 131 code · 25 blank · 132 comment · 9 complexity · b29d91de923b113725c7986ade663ae2 MD5 · raw file

  1. <?php
  2. /**
  3. * Action class used in the MVC part of the framework
  4. *
  5. * @author Daniel Andre Eikeland <zegenie@zegeniestudios.net>
  6. * @version 3.1
  7. * @license http://www.opensource.org/licenses/mozilla1.1.php Mozilla Public License 1.1 (MPL 1.1)
  8. * @package thebuggenie
  9. * @subpackage mvc
  10. */
  11. /**
  12. * Action class used in the MVC part of the framework
  13. *
  14. * @package thebuggenie
  15. * @subpackage mvc
  16. */
  17. class TBGAction extends TBGParameterholder
  18. {
  19. /**
  20. * Forward the user to a specified url
  21. *
  22. * @param string $url The URL to forward to
  23. * @param integer $code[optional] HTTP status code
  24. * @param integer $method[optional] 2 for meta redirect instead of header
  25. */
  26. public function forward($url, $code = 200)
  27. {
  28. if (TBGContext::getRequest()->isAjaxCall() || TBGContext::getRequest()->getRequestedFormat() == 'json')
  29. {
  30. $this->getResponse()->ajaxResponseText($code, TBGContext::getMessageAndClear('forward'));
  31. }
  32. TBGLogging::log("Forwarding to url {$url}");
  33. TBGLogging::log('Triggering header redirect function');
  34. $this->getResponse()->headerRedirect($url, $code);
  35. }
  36. /**
  37. * Function that is executed before any actions in an action class
  38. *
  39. * @param TBGRequest $request The request object
  40. * @param string $action The action that is being triggered
  41. */
  42. public function preExecute(TBGRequest $request, $action)
  43. {
  44. }
  45. /**
  46. * Redirect from one action method to another in the same action
  47. *
  48. * @param string $redirect_to The method to redirect to
  49. */
  50. public function redirect($redirect_to)
  51. {
  52. $actionName = 'run' . ucfirst($redirect_to);
  53. $this->getResponse()->setTemplate(mb_strtolower($redirect_to) . '.' . TBGContext::getRequest()->getRequestedFormat() . '.php');
  54. if (method_exists($this, $actionName))
  55. {
  56. return $this->$actionName(TBGContext::getRequest());
  57. }
  58. throw new Exception("The action \"{$actionName}\" does not exist in ".get_class($this));
  59. }
  60. /**
  61. * Render a string
  62. *
  63. * @param string $text The text to render
  64. *
  65. * @return boolean
  66. */
  67. public function renderText($text)
  68. {
  69. echo $text;
  70. return true;
  71. }
  72. /**
  73. * Renders JSON output, also takes care of setting the correct headers
  74. *
  75. * @param array $content The array to render
  76. *
  77. * @return boolean
  78. */
  79. public function renderJSON($text = array())
  80. {
  81. $this->getResponse()->setContentType('application/json');
  82. $this->getResponse()->setDecoration(TBGResponse::DECORATE_NONE);
  83. echo json_encode($text);
  84. return true;
  85. }
  86. /**
  87. * Return the response object
  88. *
  89. * @return TBGResponse
  90. */
  91. protected function getResponse()
  92. {
  93. return TBGContext::getResponse();
  94. }
  95. /**
  96. * Return the routing object
  97. *
  98. * @return TBGRouting
  99. */
  100. protected function getRouting()
  101. {
  102. return TBGContext::getRouting();
  103. }
  104. /**
  105. * Return the i18n object
  106. *
  107. * @return TBGI18n
  108. */
  109. protected function getI18n()
  110. {
  111. return TBGContext::getI18n();
  112. }
  113. /**
  114. * Return the current logged in user
  115. *
  116. * @return TBGUser
  117. */
  118. protected function getUser()
  119. {
  120. return TBGContext::getUser();
  121. }
  122. /**
  123. * Sets the response to 404 and shows an error, with an optional message
  124. *
  125. * @param string $message[optional] The message
  126. */
  127. public function return404($message = null)
  128. {
  129. if (TBGContext::getRequest()->isAjaxCall() || TBGContext::getRequest()->getRequestedFormat() == 'json')
  130. {
  131. $this->getResponse()->ajaxResponseText(404, $message);
  132. }
  133. $this->message = $message;
  134. $this->getResponse()->setHttpStatus(404);
  135. $this->getResponse()->setTemplate('main/notfound');
  136. return false;
  137. }
  138. /**
  139. * Forward the user with HTTP status code 403 and an (optional) message
  140. *
  141. * @param string $message[optional] The message
  142. */
  143. public function forward403($message = null)
  144. {
  145. $this->forward403unless(false, $message);
  146. }
  147. /**
  148. * Forward the user with HTTP status code 403 and an (optional) message
  149. * based on a boolean check
  150. *
  151. * @param boolean $condition
  152. * @param string $message[optional] The message
  153. */
  154. public function forward403unless($condition, $message = null)
  155. {
  156. if (!$condition)
  157. {
  158. $message = ($message === null) ? TBGContext::getI18n()->__("You are not allowed to access to this page") : $message;
  159. if (TBGContext::getUser()->isGuest())
  160. {
  161. TBGContext::setMessage('login_message_err', $message);
  162. TBGContext::setMessage('login_force_redirect', true);
  163. TBGContext::setMessage('login_referer', TBGContext::getRouting()->generate(TBGContext::getRouting()->getCurrentRouteName(), TBGContext::getRequest()->getParameters()));
  164. $this->forward(TBGContext::getRouting()->generate('login_page'), 403);
  165. }
  166. else
  167. {
  168. $this->getResponse()->setHttpStatus(403);
  169. $this->getResponse()->setTemplate('main/forbidden', array('message' => $message));
  170. }
  171. }
  172. }
  173. public function forward403if($condition, $message = null)
  174. {
  175. $this->forward403unless(!$condition, $message);
  176. }
  177. /**
  178. * Render a template
  179. *
  180. * @param string $template the template name
  181. * @param array $params template parameters
  182. *
  183. * @return boolean
  184. */
  185. public function renderTemplate($template, $params = array())
  186. {
  187. echo TBGActionComponent::includeTemplate($template, $params);
  188. return true;
  189. }
  190. /**
  191. * Render a component
  192. *
  193. * @param string $template the component name
  194. * @param array $params component parameters
  195. *
  196. * @return boolean
  197. */
  198. public function renderComponent($template, $params = array())
  199. {
  200. echo TBGActionComponent::includeComponent($template, $params);
  201. return true;
  202. }
  203. /**
  204. * Returns the HTML output from a component, but doesn't render it
  205. *
  206. * @param string $template the component name
  207. * @param array $params component parameters
  208. *
  209. * @return boolean
  210. */
  211. public static function returnComponentHTML($template, $params = array())
  212. {
  213. $current_content = ob_get_clean();
  214. (TBGContext::isCLI()) ? ob_start() : ob_start('mb_output_handler');
  215. echo TBGActionComponent::includeComponent($template, $params);
  216. $component_content = ob_get_clean();
  217. (TBGContext::isCLI()) ? ob_start() : ob_start('mb_output_handler');
  218. echo $current_content;
  219. return $component_content;
  220. }
  221. /**
  222. * Returns the HTML output from a component, but doesn't render it
  223. *
  224. * @param string $template the component name
  225. * @param array $params component parameters
  226. *
  227. * @return boolean
  228. */
  229. public function getComponentHTML($template, $params = array())
  230. {
  231. return self::returnComponentHTML($template, $params);
  232. }
  233. /**
  234. * Returns the HTML output from a template, but doesn't render it
  235. *
  236. * @param string $template the template name
  237. * @param array $params template parameters
  238. *
  239. * @return boolean
  240. */
  241. public static function returnTemplateHTML($template, $params = array())
  242. {
  243. $current_content = ob_get_clean();
  244. (TBGContext::isCLI()) ? ob_start() : ob_start('mb_output_handler');
  245. echo TBGActionComponent::includeTemplate($template, $params);
  246. $template_content = ob_get_clean();
  247. (TBGContext::isCLI()) ? ob_start() : ob_start('mb_output_handler');
  248. echo $current_content;
  249. return $template_content;
  250. }
  251. /**
  252. * Returns the HTML output from a template, but doesn't render it
  253. *
  254. * @param string $template the template name
  255. * @param array $params template parameters
  256. *
  257. * @return boolean
  258. */
  259. public function getTemplateHTML($template, $params = array())
  260. {
  261. return self::returnTemplateHTML($template, $params);
  262. }
  263. }