2
0

Navigation.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2009 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. throw 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. }
  168. return null;
  169. }
  170. $this->_inject($helper);
  171. $this->_helpers[$proxy] = $helper;
  172. return $helper;
  173. }
  174. /**
  175. * Injects container, ACL, and translator to the given $helper if this
  176. * helper is configured to do so
  177. *
  178. * @param Zend_View_Helper_Navigation_Helper $helper helper instance
  179. * @return void
  180. */
  181. protected function _inject(Zend_View_Helper_Navigation_Helper $helper)
  182. {
  183. if ($this->getInjectContainer() && !$helper->hasContainer()) {
  184. $helper->setContainer($this->getContainer());
  185. }
  186. if ($this->getInjectAcl()) {
  187. if (!$helper->hasAcl()) {
  188. $helper->setAcl($this->getAcl());
  189. }
  190. if (!$helper->hasRole()) {
  191. $helper->setRole($this->getRole());
  192. }
  193. }
  194. if ($this->getInjectTranslator() && !$helper->hasTranslator()) {
  195. $helper->setTranslator($this->getTranslator());
  196. }
  197. }
  198. // Accessors:
  199. /**
  200. * Sets the default proxy to use in {@link render()}
  201. *
  202. * @param string $proxy default proxy
  203. * @return Zend_View_Helper_Navigation fluent interface, returns self
  204. */
  205. public function setDefaultProxy($proxy)
  206. {
  207. $this->_defaultProxy = (string) $proxy;
  208. return $this;
  209. }
  210. /**
  211. * Returns the default proxy to use in {@link render()}
  212. *
  213. * @return string the default proxy to use in {@link render()}
  214. */
  215. public function getDefaultProxy()
  216. {
  217. return $this->_defaultProxy;
  218. }
  219. /**
  220. * Sets whether container should be injected when proxying
  221. *
  222. * @param bool $injectContainer [optional] whether container should
  223. * be injected when proxying. Default
  224. * is true.
  225. * @return Zend_View_Helper_Navigation fluent interface, returns self
  226. */
  227. public function setInjectContainer($injectContainer = true)
  228. {
  229. $this->_injectContainer = (bool) $injectContainer;
  230. return $this;
  231. }
  232. /**
  233. * Returns whether container should be injected when proxying
  234. *
  235. * @return bool whether container should be injected when proxying
  236. */
  237. public function getInjectContainer()
  238. {
  239. return $this->_injectContainer;
  240. }
  241. /**
  242. * Sets whether ACL should be injected when proxying
  243. *
  244. * @param bool $injectAcl [optional] whether ACL should be
  245. * injected when proxying. Default is
  246. * true.
  247. * @return Zend_View_Helper_Navigation fluent interface, returns self
  248. */
  249. public function setInjectAcl($injectAcl = true)
  250. {
  251. $this->_injectAcl = (bool) $injectAcl;
  252. return $this;
  253. }
  254. /**
  255. * Returns whether ACL should be injected when proxying
  256. *
  257. * @return bool whether ACL should be injected when proxying
  258. */
  259. public function getInjectAcl()
  260. {
  261. return $this->_injectAcl;
  262. }
  263. /**
  264. * Sets whether translator should be injected when proxying
  265. *
  266. * @param bool $injectTranslator [optional] whether translator should
  267. * be injected when proxying. Default
  268. * is true.
  269. * @return Zend_View_Helper_Navigation fluent interface, returns self
  270. */
  271. public function setInjectTranslator($injectTranslator = true)
  272. {
  273. $this->_injectTranslator = (bool) $injectTranslator;
  274. return $this;
  275. }
  276. /**
  277. * Returns whether translator should be injected when proxying
  278. *
  279. * @return bool whether translator should be injected when proxying
  280. */
  281. public function getInjectTranslator()
  282. {
  283. return $this->_injectTranslator;
  284. }
  285. // Zend_View_Helper_Navigation_Helper:
  286. /**
  287. * Renders helper
  288. *
  289. * @param Zend_Navigation_Container $container [optional] container to
  290. * render. Default is to
  291. * render the container
  292. * registered in the helper.
  293. * @return string helper output
  294. * @throws Zend_Loader_PluginLoader_Exception if helper cannot be found
  295. * @throws Zend_View_Exception if helper doesn't implement
  296. * the interface specified in
  297. * {@link findHelper()}
  298. */
  299. public function render(Zend_Navigation_Container $container = null)
  300. {
  301. $helper = $this->findHelper($this->getDefaultProxy());
  302. return $helper->render($container);
  303. }
  304. }