View.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. *
  4. * The view object. This object renders the view if found. It also provides functionality for
  5. * stripping tags and html escaping. It holds a dynamic data store for controllers to add items
  6. * to the view on the fly.
  7. *
  8. * @author Ironpilot
  9. * @copyright Copywrite (c) 2011, STAPLE CODE
  10. *
  11. * This file is part of the STAPLE Framework.
  12. *
  13. * The STAPLE Framework is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Lesser General Public License as published by the
  15. * Free Software Foundation, either version 3 of the License, or (at your option)
  16. * any later version.
  17. *
  18. * The STAPLE Framework is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  20. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  21. * more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public License
  24. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  25. */
  26. class Staple_View
  27. {
  28. use Staple_Trait_Helpers;
  29. /**
  30. * Whether or not to render the view. Set to false to skip view rendering.
  31. * @var bool
  32. */
  33. protected $_render = true;
  34. /**
  35. * The dynamic datastore.
  36. * @var array
  37. */
  38. protected $_store = array();
  39. /**
  40. * A string containing the name of the view to build.
  41. * @var string
  42. */
  43. protected $view;
  44. /**
  45. * A string containing the name of the controller under which to look for the view.
  46. * @var string
  47. */
  48. protected $controller;
  49. /**
  50. * Overloaded __set allows for dynamic addition of properties.
  51. * @param string | int $key
  52. * @param mixed $value
  53. */
  54. public function __set($key,$value)
  55. {
  56. $this->_store[$key] = $value;
  57. }
  58. /**
  59. * Retrieves a stored property.
  60. * @param string | int $key
  61. */
  62. public function __get($key)
  63. {
  64. if(array_key_exists($key,$this->_store))
  65. {
  66. return $this->_store[$key];
  67. }
  68. else
  69. {
  70. return NULL;
  71. }
  72. }
  73. /**
  74. *
  75. * Don't save any data in the session.
  76. */
  77. public function __sleep()
  78. {
  79. return array();
  80. }
  81. /**
  82. * Converts the object into a string by calling the build() method.
  83. * @return string
  84. */
  85. public function __toString()
  86. {
  87. try {
  88. ob_start();
  89. $this->build();
  90. $buffer = ob_get_contents();
  91. ob_end_clean();
  92. return $buffer;
  93. }
  94. catch (Exception $e)
  95. {
  96. $msg = '<p class=\"viewerror\">The View threw an Uncaught Exception when converting to a string....</p>';
  97. if(Staple_Config::getValue('errors', 'devmode'))
  98. {
  99. $msg .= '<p>'.$e->getMessage().'</p>';
  100. }
  101. return $msg;
  102. }
  103. }
  104. /**
  105. * This function allows you to disable the rendering of the view from the controller.
  106. */
  107. public function noRender()
  108. {
  109. $this->_render = false;
  110. }
  111. /**
  112. * Sets the view string
  113. * @param string $view
  114. */
  115. public function setView($view)
  116. {
  117. $this->view = $view;
  118. return $this;
  119. }
  120. /**
  121. * Returns the view string
  122. * @return string
  123. */
  124. public function getView()
  125. {
  126. return $this->view;
  127. }
  128. /**
  129. * Returns isset on the $view parameter
  130. * @return boolean
  131. */
  132. public function hasView()
  133. {
  134. return isset($this->view);
  135. }
  136. /**
  137. * Sets the controller string
  138. * @param string $controller
  139. */
  140. public function setController($controller)
  141. {
  142. $this->controller = $controller;
  143. return $this;
  144. }
  145. /**
  146. * Returns the controller string.
  147. * @return string
  148. */
  149. public function getController()
  150. {
  151. return $this->controller;
  152. }
  153. /**
  154. * Returns isset on the $controller parameter.
  155. * @return boolean
  156. */
  157. public function hasController()
  158. {
  159. return isset($this->controller);
  160. }
  161. /**
  162. *
  163. * This function renders the view. If accepts a string representing the controller and
  164. * a string representing the requested action. With this information the correct view
  165. * is selected and rendered.
  166. * @param string $class
  167. * @param string $view
  168. */
  169. public function build()
  170. {
  171. if($this->_render === true)
  172. {
  173. //Load the view from the default loader
  174. $view = Staple_Main::get()->getLoader()->loadView($this->controller,$this->view);
  175. if(strlen($view) >= 1)
  176. {
  177. include $view;
  178. }
  179. }
  180. else
  181. {
  182. //skip rendering of an additional views
  183. $this->_render = false;
  184. }
  185. }
  186. }