/environment/classes/controller/PageController.class.php

https://gitlab.com/x33n/ProjectPier-Core · PHP · 337 lines · 124 code · 42 blank · 171 comment · 15 complexity · bfcc48f369b9220abef802913dc998d3 MD5 · raw file

  1. <?php
  2. /**
  3. * Page controller is special controller that is able to map controller name
  4. * and actions name with layout and template and automatically display them.
  5. * This behaviour is present only when action has not provided any exit by
  6. * itself (redirect to another page, render template and die etc)
  7. *
  8. * @http://www.projectpier.org/
  9. */
  10. abstract class PageController extends Controller {
  11. /**
  12. * Template name. If it is empty this controller will use action name.php
  13. *
  14. * @var string
  15. */
  16. private $template;
  17. /**
  18. * Layout name. If it is empty this controller will use its name.php
  19. *
  20. * @var string
  21. */
  22. private $layout;
  23. /**
  24. * Array of helpers that will be automatically loaded when render method is called
  25. *
  26. * @var array
  27. */
  28. private $helpers = array();
  29. /**
  30. * Automatically render template / layout if action ends without exit
  31. *
  32. * @var boolean
  33. */
  34. private $auto_render = true;
  35. /**
  36. * Construct controller
  37. *
  38. * @param void
  39. * @return null
  40. */
  41. function __construct() {
  42. parent::__construct();
  43. $this->setSystemControllerClass('PageController');;
  44. $this->addHelper('common');
  45. $this->addHelper('page');
  46. $this->addHelper('form');
  47. $this->addHelper('format');
  48. $this->addHelper('pagination');
  49. // autoload helper with name equal to controller
  50. $cn = $this->getControllerName();
  51. if (Env::helperExists($cn, $cn)) {
  52. $this->addHelper($cn, $cn); // controller name helper
  53. }
  54. } // __construct
  55. /**
  56. * Execute action
  57. *
  58. * @param string $action
  59. * @return null
  60. */
  61. function execute($action) {
  62. ob_start();
  63. parent::execute($action);
  64. if ($this->getAutoRender()) $render = $this->render(); // Auto render?
  65. return true;
  66. } // execute
  67. /**
  68. * Render content... If template and/layout are NULL script will resolve
  69. * their names based on controller name and action.
  70. *
  71. * PageController::index will map with:
  72. * - template => views/page/index.php
  73. * - layout => layouts/page.php
  74. *
  75. * @param string $template
  76. * @param string $layout
  77. * @param boolean $die
  78. * @return boolean
  79. * @throws FileDnxError
  80. */
  81. function render($template = null, $layout = null, $die = true) {
  82. trace(__FILE__, "render($template, $layout, $die)" );
  83. // Set template and layout...
  84. if (!is_null($template)) {
  85. $this->setTemplate($template);
  86. } // if
  87. if (!is_null($layout)) {
  88. $this->setLayout($layout);
  89. } // if
  90. // Get template and layout paths
  91. $template_path = $this->getTemplatePath();
  92. $layout_path = $this->getLayoutPath();
  93. trace(__FILE__, "tpl_fetch($template_path)" );
  94. // Fetch content...
  95. $content = tpl_fetch($template_path);
  96. trace(__FILE__, "renderLayout($layout_path <xmp>$content</xmp>)" );
  97. // Assign content and render layout
  98. $this->renderLayout($layout_path, $content);
  99. // Die!
  100. if ($die) {
  101. session_write_close();
  102. die();
  103. } // if
  104. // We are done here...
  105. return true;
  106. } // render
  107. /**
  108. * Assign content and render layout
  109. *
  110. * @param string $layout_path Path to the layout file
  111. * @param string $content Value that will be assigned to the $content_for_layout
  112. * variable
  113. * @return boolean
  114. * @throws FileDnxError
  115. */
  116. function renderLayout($layout_path, $content = null) {
  117. tpl_assign('content_for_layout', $content);
  118. return tpl_display($layout_path);
  119. } // renderLayout
  120. /**
  121. * Shortcut method for printing text and setting auto_render option
  122. *
  123. * @param string $text Text that need to be rendered
  124. * @param boolean $render_layout Render controller layout. Default is false for
  125. * simple and fast text rendering
  126. * @return null
  127. */
  128. function renderText($text, $render_layout = false) {
  129. $this->setAutoRender(false); // Turn off auto render because we will render whole thing now...
  130. if ($render_layout) {
  131. $layout_path = $this->getLayoutPath();
  132. $this->renderLayout($layout_path, $text);
  133. } else {
  134. print $text;
  135. } // if
  136. } // renderText
  137. /**
  138. * Redirect. Params are same as get_url function
  139. *
  140. * @param string $controller
  141. * @param string $action
  142. * @param array $params
  143. * @param string $anchor
  144. * @return null
  145. */
  146. function redirectTo($controller = DEFAULT_CONTROLLER, $action = 'index', $params = null, $anchor = null) {
  147. redirect_to(get_url($controller, $action, $params, $anchor));
  148. } // redirectTo
  149. /**
  150. * Redirect to URL
  151. *
  152. * @param string $url
  153. * @return null
  154. */
  155. function redirectToUrl($url) {
  156. redirect_to($url);
  157. } // redirectToUrl
  158. /**
  159. * Redirect to referer. If referer is no valid this function will use $alternative URL
  160. *
  161. * @param string $alternative Alternative URL
  162. * @return null
  163. */
  164. function redirectToReferer($alternative) {
  165. redirect_to_referer($alternative);
  166. } // redirectToReferer
  167. // -------------------------------------------------------
  168. // Getters and setters
  169. // -------------------------------------------------------
  170. /**
  171. * Get template
  172. *
  173. * @param null
  174. * @return string
  175. */
  176. function getTemplate() {
  177. return $this->template;
  178. } // getTemplate
  179. /**
  180. * Set template value
  181. *
  182. * @param string $value
  183. * @return null
  184. */
  185. function setTemplate($value) {
  186. $this->template = $value;
  187. } // setTemplate
  188. /**
  189. * Get layout
  190. *
  191. * @param null
  192. * @return string
  193. */
  194. function getLayout() {
  195. return $this->layout;
  196. } // getLayout
  197. /**
  198. * Set layout value
  199. *
  200. * @param string $value
  201. * @return null
  202. */
  203. function setLayout($value) {
  204. $this->layout = $value;
  205. } // setLayout
  206. /**
  207. * Return helper / helpers array
  208. *
  209. * @param null
  210. * @return array
  211. */
  212. function getHelpers() {
  213. return is_array($this->helpers) ? $this->helpers : array($this->helpers);
  214. } // getHelpers
  215. /**
  216. * Add one or many helpers
  217. *
  218. * @param string $helper This param can be array of helpers
  219. * @return null
  220. */
  221. function addHelper($helper, $controller_name = null) {
  222. trace(__FILE__,"addHelper($helper, $controller_name) start");
  223. if (!in_array($helper, $this->helpers)) {
  224. if (Env::useHelper($helper, $controller_name)) {
  225. $this->helpers[] = $helper;
  226. } // if
  227. } // if
  228. trace(__FILE__,"addHelper($helper, $controller_name) end");
  229. return true;
  230. } // addHelper
  231. /**
  232. * Get auto_render
  233. *
  234. * @param null
  235. * @return boolean
  236. */
  237. function getAutoRender() {
  238. return $this->auto_render;
  239. } // getAutoRender
  240. /**
  241. * Set auto_render value
  242. *
  243. * @param boolean $value
  244. * @return null
  245. */
  246. function setAutoRender($value) {
  247. $this->auto_render = (boolean) $value;
  248. } // setAutoRender
  249. /**
  250. * Return path of the template. If template dnx throw exception
  251. *
  252. * @param void
  253. * @return string
  254. * @throws FileDnxError
  255. */
  256. function getTemplatePath() {
  257. // Filename of template
  258. $template = trim($this->getTemplate()) == '' ?
  259. $this->getAction() :
  260. $this->getTemplate();
  261. // Prepare path...
  262. if (is_file($this->getTemplate())) {
  263. $path = $this->getTemplate();
  264. } else {
  265. $path = get_template_path($template, $this->getControllerName());
  266. } // if
  267. // Template dnx?
  268. if (!is_file($path)) {
  269. throw new FileDnxError($path);
  270. } // if
  271. // Return path
  272. return $path;
  273. } // getTemplatePath
  274. /**
  275. * Return path of the layout file.
  276. *
  277. * @param void
  278. * @return string
  279. * @throws FileDnxError
  280. */
  281. function getLayoutPath() {
  282. $layout_name = trim($this->getLayout()) == '' ?
  283. $this->getControllerName() :
  284. $this->getLayout();
  285. // Path of the layout
  286. $path = Env::getLayoutPath($layout_name);
  287. // File dnx? Throw exception
  288. if (!is_file($path)) {
  289. throw new FileDnxError($path);
  290. } // if
  291. // Return path
  292. return $path;
  293. } // getLayoutPath
  294. } // PageController
  295. ?>