2
0

Navigation.php 11 KB

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