PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/library/QFrame/View.php

https://github.com/humansky/qframe
PHP | 341 lines | 142 code | 32 blank | 167 comment | 25 complexity | a5af266bf29a30d891189aed08db9475 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0
  1. <?php
  2. /**
  3. * This file is part of the CSI QFrame.
  4. *
  5. * The CSI QFrame is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * The CSI QFrame is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @category QFrame
  19. * @package QFrame
  20. * @copyright Copyright (c) 2007 Collaborative Software Initiative (CSI)
  21. * @license http://www.gnu.org/licenses/ GNU General Public License v3
  22. */
  23. /**
  24. * Haml_Engine
  25. */
  26. require_once 'Haml/Engine.php';
  27. /**
  28. * @category QFrame
  29. * @package QFrame
  30. * @copyright Copyright (c) 2007 Collaborative Software Initiative (CSI)
  31. * @license http://www.gnu.org/licenses/ GNU General Public License v3
  32. */
  33. class QFrame_View implements Zend_View_Interface {
  34. /**
  35. * Haml_Engine object used to do the actual work
  36. * @var Haml_Engine
  37. */
  38. private $_haml;
  39. /**
  40. * Current list of helper paths
  41. * @var array
  42. */
  43. private $_helpers = array();
  44. /**
  45. * Determines whether a layout will be rendered
  46. * @var bool
  47. */
  48. private $_renderLayout = true;
  49. /**
  50. * Class constructor
  51. *
  52. * @param string template path
  53. */
  54. public function __construct($templatePath = '') {
  55. $this->_haml = new Haml_Engine($templatePath);
  56. $this->_haml->setUndefined($this);
  57. }
  58. /**
  59. * Return the template engine object
  60. *
  61. * @return Haml_Engine
  62. */
  63. public function getEngine() {
  64. return $this->_haml;
  65. }
  66. /**
  67. * Set the path to the templates
  68. *
  69. * @param string $path Directory to set as the path
  70. */
  71. public function setScriptPath($path) {
  72. if(is_readable($path)) $this->_haml->setBase($path);
  73. else throw new Exception('Invalid path provided');
  74. return;
  75. }
  76. /**
  77. * Retrieves the current template directory
  78. *
  79. * @return string
  80. */
  81. public function getScriptPaths() {
  82. return array($this->_haml->getBase());
  83. }
  84. /**
  85. * Alias for setScriptPath
  86. *
  87. * @param string $path
  88. * @param string $prefix Unused
  89. */
  90. public function setBasePath($path, $prefix = 'Zend_View') {
  91. return $this->setScriptPath($path);
  92. }
  93. /**
  94. * Alias for setScriptPath
  95. *
  96. * @param string $path
  97. * @param string $prefix Unused
  98. */
  99. public function addBasePath($path, $prefix = 'Zend_View') {
  100. return $this->setScriptPath($path);
  101. }
  102. /**
  103. * Sets the helper path to a single path or list of paths
  104. *
  105. * @param string|array path(s) to set
  106. * @param string (optional) prefix to use for path(s)
  107. */
  108. public function setHelperPath($path, $prefix = 'QFrame_View_Helper') {
  109. array_splice($this->_helpers, 0);
  110. if(is_array($path)) {
  111. foreach($path as $p) $this->addHelperPath($p, $prefix);
  112. }
  113. else {
  114. $this->addHelperPath($path, $prefix);
  115. }
  116. }
  117. /**
  118. * Adds a single path to the list of helpers
  119. *
  120. * @param string path to add
  121. * @param string (optional) prefix to assign to this path
  122. */
  123. public function addHelperPath($path, $prefix = 'QFrame_View_Helper') {
  124. $this->_helpers[$path] = $prefix;
  125. }
  126. /**
  127. * Turn on/off view rendering
  128. *
  129. * @param bool should layout rendering be on
  130. */
  131. public function setRenderLayout($r) {
  132. $this->_renderLayout = $r;
  133. }
  134. /**
  135. * Sets an assigned variable
  136. *
  137. * @param string variable name
  138. * @param string variable value
  139. */
  140. public function __set($key, $val) {
  141. $this->_haml->assign($key, $val);
  142. }
  143. /**
  144. * Gets an assigned variable
  145. *
  146. * @param string variable name
  147. * @return mixed
  148. */
  149. public function __get($key) {
  150. return $this->_haml->getAssign($key);
  151. }
  152. /**
  153. * Unsets an assigned variable
  154. *
  155. * @param string variable name
  156. */
  157. public function __unset($key) {
  158. $this->_haml->unassign($key);
  159. }
  160. /**
  161. * Checks whether or not an assigned variable has been set
  162. *
  163. * @param string variable name
  164. */
  165. public function __isset($key) {
  166. return $this->_haml->is_assigned($key);
  167. }
  168. /**
  169. * Assigns one or more variables to the template
  170. *
  171. * @param string|array either a variable name or array of variable => value pairs
  172. * @param mixed if first param is a string, the value of this variable
  173. */
  174. public function assign($spec, $value = null) {
  175. if(is_array($spec)) {
  176. foreach($spec as $key => $val) $this->__set($key, $val);
  177. }
  178. elseif(!is_null($value)) {
  179. $this->__set($spec, $value);
  180. }
  181. else throw new Exception("You must specify either an array or a variable *and* value");
  182. return;
  183. }
  184. /**
  185. * Clear all assigned variables
  186. */
  187. public function clearVars() {
  188. $this->_haml->clear_assigns();
  189. return;
  190. }
  191. /**
  192. * Renders a specified template
  193. *
  194. * @param string template name
  195. * @param Array (optional) array of local variables to assign before rendering
  196. * @return string
  197. */
  198. public function render($name, $locals = array()) {
  199. ob_start();
  200. $this->_haml->render($name, $this->_renderLayout, $locals);
  201. $html = ob_get_clean();
  202. /*if(function_exists('tidy_repair_string')) {
  203. $tidy_config = array(
  204. 'indent' => true,
  205. 'wrap' => 120,
  206. 'hide-comments' => true
  207. );
  208. $html = tidy_repair_string($html, $tidy_config);
  209. }*/
  210. return $html;
  211. }
  212. /**
  213. * Renders a partial template
  214. *
  215. * @param string template name (will be prepended with '_' to make filename)
  216. * @param mixed|Array object or array of objects to provide to the partial
  217. * @param boolean (optional) treat object argument as a collection
  218. * @param boolean (optional) render application wide partial
  219. * @param Array array of local variables to set before rendering
  220. * @return string
  221. */
  222. public function renderPartial($name, $object, $arr = false, $appl = false, $locals = array()) {
  223. if($arr) {
  224. $content = '';
  225. foreach($object as $individual)
  226. $content .= $this->renderPartial($name, $individual, false, $appl, $locals);
  227. return $content;
  228. }
  229. // Figure out the relative filename of the partial template to render (based on
  230. // whether or not the user requested an application global)
  231. if($appl) {
  232. $filename = implode(DIRECTORY_SEPARATOR, array('scripts', 'application', "_{$name}.haml"));
  233. }
  234. else {
  235. $filename = implode(DIRECTORY_SEPARATOR, array(
  236. 'scripts',
  237. Zend_Controller_Front::getInstance()->getRequest()->getControllerName(),
  238. "_{$name}.haml"
  239. ));
  240. }
  241. ob_start();
  242. if($this->_haml->is_assigned($name)) $originalValue = $this->_haml->getAssign($name);
  243. $this->_haml->assign($name, $object);
  244. $this->_haml->render($filename, false, $locals);
  245. $html = ob_get_clean();
  246. if(isset($originalValue)) $this->_haml->assign($name, $originalValue);
  247. return $html;
  248. }
  249. /**
  250. * Escapes special HTML characters in strings (required for some ZF helpers)
  251. *
  252. * @param string string to escape
  253. * @return string
  254. */
  255. public function escape($str) {
  256. return htmlspecialchars($str);
  257. }
  258. /**
  259. * Handle calls to undefined methods
  260. *
  261. * @param string method name
  262. * @param array arguments
  263. */
  264. public function __call($method, $args) {
  265. // First, check to see if there is a standard style Zend helper (class and method)
  266. // named the same as the helper
  267. foreach($this->_helpers as $path => $prefix) {
  268. if(substr($prefix, -1, 1) != '_') $prefix .= '_';
  269. $helperFile = $path . DIRECTORY_SEPARATOR . ucfirst($method) . '.php';
  270. if(file_exists($helperFile)) {
  271. require_once($helperFile);
  272. $class = new ReflectionClass($prefix . ucfirst($method));
  273. $object = $class->newInstance();
  274. if(method_exists($object, 'setView')) $object->setView($this);
  275. return call_user_func_array(array(&$object, $method), $args);
  276. }
  277. }
  278. // Second, check to see if there is a class named something of the form "ControllerHelpers"
  279. // that has a method with the same name as what has been called
  280. $controllerName = Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
  281. foreach($this->_helpers as $path => $prefix) {
  282. if(substr($prefix, -1, 1) != '_') $prefix .= '_';
  283. $globalHelperFile = $path . DIRECTORY_SEPARATOR . ucfirst($controllerName) . 'Helpers.php';
  284. if(file_exists($globalHelperFile)) {
  285. require_once($globalHelperFile);
  286. $class = new ReflectionClass($prefix . ucfirst($controllerName) . 'Helpers');
  287. $object = $class->newInstance();
  288. if(method_exists($object, 'setView')) $object->setView($this);
  289. if(method_exists($object, $method))
  290. return call_user_func_array(array(&$object, $method), $args);
  291. }
  292. }
  293. // Lastly, check to see if there is a class named ApplicationHelpers with a method named the
  294. // same thing as the helper that was called.
  295. foreach($this->_helpers as $path => $prefix) {
  296. if(substr($prefix, -1, 1) != '_') $prefix .= '_';
  297. $applicationHelperFile = $path . DIRECTORY_SEPARATOR . 'ApplicationHelpers.php';
  298. if(file_exists($applicationHelperFile)) {
  299. require_once($applicationHelperFile);
  300. $class = new ReflectionClass($prefix . 'ApplicationHelpers');
  301. $object = $class->newInstance();
  302. if(method_exists($object, 'setView')) $object->setView($this);
  303. if(method_exists($object, $method))
  304. return call_user_func_array(array(&$object, $method), $args);
  305. }
  306. }
  307. throw new Exception('Helper method ' . $method . ' is not defined');
  308. }
  309. }