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

/app/library/Zend/View/Helper/Navigation.php

https://github.com/Zefiryn/graduationprojects
PHP | 338 lines | 119 code | 32 blank | 187 comment | 15 complexity | dd900bcdb449437bdead0a9a4ea852af MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_View
  17. * @subpackage Helper
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Navigation.php 23953 2011-05-03 05:47:39Z ralph $
  21. */
  22. /**
  23. * @see Zend_View_Helper_Navigation_HelperAbstract
  24. */
  25. require_once 'Zend/View/Helper/Navigation/HelperAbstract.php';
  26. /**
  27. * Proxy helper for retrieving navigational helpers and forwarding calls
  28. *
  29. * @category Zend
  30. * @package Zend_View
  31. * @subpackage Helper
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_View_Helper_Navigation
  36. extends Zend_View_Helper_Navigation_HelperAbstract
  37. {
  38. /**
  39. * View helper namespace
  40. *
  41. * @var string
  42. */
  43. const NS = 'Zend_View_Helper_Navigation';
  44. /**
  45. * Default proxy to use in {@link render()}
  46. *
  47. * @var string
  48. */
  49. protected $_defaultProxy = 'menu';
  50. /**
  51. * Contains references to proxied helpers
  52. *
  53. * @var array
  54. */
  55. protected $_helpers = array();
  56. /**
  57. * Whether container should be injected when proxying
  58. *
  59. * @var bool
  60. */
  61. protected $_injectContainer = true;
  62. /**
  63. * Whether ACL should be injected when proxying
  64. *
  65. * @var bool
  66. */
  67. protected $_injectAcl = true;
  68. /**
  69. * Whether translator should be injected when proxying
  70. *
  71. * @var bool
  72. */
  73. protected $_injectTranslator = true;
  74. /**
  75. * Helper entry point
  76. *
  77. * @param Zend_Navigation_Container $container [optional] container to
  78. * operate on
  79. * @return Zend_View_Helper_Navigation fluent interface, returns
  80. * self
  81. */
  82. public function navigation(Zend_Navigation_Container $container = null)
  83. {
  84. if (null !== $container) {
  85. $this->setContainer($container);
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Magic overload: Proxy to other navigation helpers or the container
  91. *
  92. * Examples of usage from a view script or layout:
  93. * <code>
  94. * // proxy to Menu helper and render container:
  95. * echo $this->navigation()->menu();
  96. *
  97. * // proxy to Breadcrumbs helper and set indentation:
  98. * $this->navigation()->breadcrumbs()->setIndent(8);
  99. *
  100. * // proxy to container and find all pages with 'blog' route:
  101. * $blogPages = $this->navigation()->findAllByRoute('blog');
  102. * </code>
  103. *
  104. * @param string $method helper name or method name in
  105. * container
  106. * @param array $arguments [optional] arguments to pass
  107. * @return mixed returns what the proxied call returns
  108. * @throws Zend_View_Exception if proxying to a helper, and the
  109. * helper is not an instance of the
  110. * interface specified in
  111. * {@link findHelper()}
  112. * @throws Zend_Navigation_Exception if method does not exist in container
  113. */
  114. public function __call($method, array $arguments = array())
  115. {
  116. // check if call should proxy to another helper
  117. if ($helper = $this->findHelper($method, false)) {
  118. return call_user_func_array(array($helper, $method), $arguments);
  119. }
  120. // default behaviour: proxy call to container
  121. return parent::__call($method, $arguments);
  122. }
  123. /**
  124. * Returns the helper matching $proxy
  125. *
  126. * The helper must implement the interface
  127. * {@link Zend_View_Helper_Navigation_Helper}.
  128. *
  129. * @param string $proxy helper name
  130. * @param bool $strict [optional] whether
  131. * exceptions should be
  132. * thrown if something goes
  133. * wrong. Default is true.
  134. * @return Zend_View_Helper_Navigation_Helper helper instance
  135. * @throws Zend_Loader_PluginLoader_Exception if $strict is true and
  136. * helper cannot be found
  137. * @throws Zend_View_Exception if $strict is true and
  138. * helper does not implement
  139. * the specified interface
  140. */
  141. public function findHelper($proxy, $strict = true)
  142. {
  143. if (isset($this->_helpers[$proxy])) {
  144. return $this->_helpers[$proxy];
  145. }
  146. if (!$this->view->getPluginLoader('helper')->getPaths(self::NS)) {
  147. $this->view->addHelperPath(
  148. str_replace('_', '/', self::NS),
  149. self::NS);
  150. }
  151. if ($strict) {
  152. $helper = $this->view->getHelper($proxy);
  153. } else {
  154. try {
  155. $helper = $this->view->getHelper($proxy);
  156. } catch (Zend_Loader_PluginLoader_Exception $e) {
  157. return null;
  158. }
  159. }
  160. if (!$helper instanceof Zend_View_Helper_Navigation_Helper) {
  161. if ($strict) {
  162. require_once 'Zend/View/Exception.php';
  163. $e = new Zend_View_Exception(sprintf(
  164. 'Proxy helper "%s" is not an instance of ' .
  165. 'Zend_View_Helper_Navigation_Helper',
  166. get_class($helper)));
  167. $e->setView($this->view);
  168. throw $e;
  169. }
  170. return null;
  171. }
  172. $this->_inject($helper);
  173. $this->_helpers[$proxy] = $helper;
  174. return $helper;
  175. }
  176. /**
  177. * Injects container, ACL, and translator to the given $helper if this
  178. * helper is configured to do so
  179. *
  180. * @param Zend_View_Helper_Navigation_Helper $helper helper instance
  181. * @return void
  182. */
  183. protected function _inject(Zend_View_Helper_Navigation_Helper $helper)
  184. {
  185. if ($this->getInjectContainer() && !$helper->hasContainer()) {
  186. $helper->setContainer($this->getContainer());
  187. }
  188. if ($this->getInjectAcl()) {
  189. if (!$helper->hasAcl()) {
  190. $helper->setAcl($this->getAcl());
  191. }
  192. if (!$helper->hasRole()) {
  193. $helper->setRole($this->getRole());
  194. }
  195. }
  196. if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
  197. $helper->setTranslator($this->getTranslator());
  198. }
  199. }
  200. // Accessors:
  201. /**
  202. * Sets the default proxy to use in {@link render()}
  203. *
  204. * @param string $proxy default proxy
  205. * @return Zend_View_Helper_Navigation fluent interface, returns self
  206. */
  207. public function setDefaultProxy($proxy)
  208. {
  209. $this->_defaultProxy = (string) $proxy;
  210. return $this;
  211. }
  212. /**
  213. * Returns the default proxy to use in {@link render()}
  214. *
  215. * @return string the default proxy to use in {@link render()}
  216. */
  217. public function getDefaultProxy()
  218. {
  219. return $this->_defaultProxy;
  220. }
  221. /**
  222. * Sets whether container should be injected when proxying
  223. *
  224. * @param bool $injectContainer [optional] whether container should
  225. * be injected when proxying. Default
  226. * is true.
  227. * @return Zend_View_Helper_Navigation fluent interface, returns self
  228. */
  229. public function setInjectContainer($injectContainer = true)
  230. {
  231. $this->_injectContainer = (bool) $injectContainer;
  232. return $this;
  233. }
  234. /**
  235. * Returns whether container should be injected when proxying
  236. *
  237. * @return bool whether container should be injected when proxying
  238. */
  239. public function getInjectContainer()
  240. {
  241. return $this->_injectContainer;
  242. }
  243. /**
  244. * Sets whether ACL should be injected when proxying
  245. *
  246. * @param bool $injectAcl [optional] whether ACL should be
  247. * injected when proxying. Default is
  248. * true.
  249. * @return Zend_View_Helper_Navigation fluent interface, returns self
  250. */
  251. public function setInjectAcl($injectAcl = true)
  252. {
  253. $this->_injectAcl = (bool) $injectAcl;
  254. return $this;
  255. }
  256. /**
  257. * Returns whether ACL should be injected when proxying
  258. *
  259. * @return bool whether ACL should be injected when proxying
  260. */
  261. public function getInjectAcl()
  262. {
  263. return $this->_injectAcl;
  264. }
  265. /**
  266. * Sets whether translator should be injected when proxying
  267. *
  268. * @param bool $injectTranslator [optional] whether translator should
  269. * be injected when proxying. Default
  270. * is true.
  271. * @return Zend_View_Helper_Navigation fluent interface, returns self
  272. */
  273. public function setInjectTranslator($injectTranslator = true)
  274. {
  275. $this->_injectTranslator = (bool) $injectTranslator;
  276. return $this;
  277. }
  278. /**
  279. * Returns whether translator should be injected when proxying
  280. *
  281. * @return bool whether translator should be injected when proxying
  282. */
  283. public function getInjectTranslator()
  284. {
  285. return $this->_injectTranslator;
  286. }
  287. // Zend_View_Helper_Navigation_Helper:
  288. /**
  289. * Renders helper
  290. *
  291. * @param Zend_Navigation_Container $container [optional] container to
  292. * render. Default is to
  293. * render the container
  294. * registered in the helper.
  295. * @return string helper output
  296. * @throws Zend_Loader_PluginLoader_Exception if helper cannot be found
  297. * @throws Zend_View_Exception if helper doesn't implement
  298. * the interface specified in
  299. * {@link findHelper()}
  300. */
  301. public function render(Zend_Navigation_Container $container = null)
  302. {
  303. $helper = $this->findHelper($this->getDefaultProxy());
  304. return $helper->render($container);
  305. }
  306. }