Mvc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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_Navigation
  17. * @subpackage Page
  18. * @copyright Copyright (c) 2005-2010 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_Navigation_Page
  24. */
  25. require_once 'Zend/Navigation/Page.php';
  26. /**
  27. * @see Zend_Controller_Action_HelperBroker
  28. */
  29. require_once 'Zend/Controller/Action/HelperBroker.php';
  30. /**
  31. * Used to check if page is active
  32. *
  33. * @see Zend_Controller_Front
  34. */
  35. require_once 'Zend/Controller/Front.php';
  36. /**
  37. * Represents a page that is defined using module, controller, action, route
  38. * name and route params to assemble the href
  39. *
  40. * @category Zend
  41. * @package Zend_Navigation
  42. * @subpackage Page
  43. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. */
  46. class Zend_Navigation_Page_Mvc extends Zend_Navigation_Page
  47. {
  48. /**
  49. * Action name to use when assembling URL
  50. *
  51. * @var string
  52. */
  53. protected $_action;
  54. /**
  55. * Controller name to use when assembling URL
  56. *
  57. * @var string
  58. */
  59. protected $_controller;
  60. /**
  61. * Module name to use when assembling URL
  62. *
  63. * @var string
  64. */
  65. protected $_module;
  66. /**
  67. * Params to use when assembling URL
  68. *
  69. * @see getHref()
  70. * @var array
  71. */
  72. protected $_params = array();
  73. /**
  74. * Route name to use when assembling URL
  75. *
  76. * @see getHref()
  77. * @var string
  78. */
  79. protected $_route;
  80. /**
  81. * Whether params should be reset when assembling URL
  82. *
  83. * @see getHref()
  84. * @var bool
  85. */
  86. protected $_resetParams = true;
  87. /**
  88. * Cached href
  89. *
  90. * The use of this variable minimizes execution time when getHref() is
  91. * called more than once during the lifetime of a request. If a property
  92. * is updated, the cache is invalidated.
  93. *
  94. * @var string
  95. */
  96. protected $_hrefCache;
  97. /**
  98. * Action helper for assembling URLs
  99. *
  100. * @see getHref()
  101. * @var Zend_Controller_Action_Helper_Url
  102. */
  103. protected static $_urlHelper = null;
  104. // Accessors:
  105. /**
  106. * Returns whether page should be considered active or not
  107. *
  108. * This method will compare the page properties against the request object
  109. * that is found in the front controller.
  110. *
  111. * @param bool $recursive [optional] whether page should be considered
  112. * active if any child pages are active. Default is
  113. * false.
  114. * @return bool whether page should be considered active or not
  115. */
  116. public function isActive($recursive = false)
  117. {
  118. if (!$this->_active) {
  119. $front = Zend_Controller_Front::getInstance();
  120. $reqParams = $front->getRequest()->getParams();
  121. if (!array_key_exists('module', $reqParams)) {
  122. $reqParams['module'] = $front->getDefaultModule();
  123. }
  124. $myParams = $this->_params;
  125. if (null !== $this->_module) {
  126. $myParams['module'] = $this->_module;
  127. } else {
  128. $myParams['module'] = $front->getDefaultModule();
  129. }
  130. if (null !== $this->_controller) {
  131. $myParams['controller'] = $this->_controller;
  132. } else {
  133. $myParams['controller'] = $front->getDefaultControllerName();
  134. }
  135. if (null !== $this->_action) {
  136. $myParams['action'] = $this->_action;
  137. } else {
  138. $myParams['action'] = $front->getDefaultAction();
  139. }
  140. if (count(array_intersect_assoc($reqParams, $myParams)) ==
  141. count($myParams)) {
  142. $this->_active = true;
  143. return true;
  144. }
  145. }
  146. return parent::isActive($recursive);
  147. }
  148. /**
  149. * Returns href for this page
  150. *
  151. * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble
  152. * the href based on the page's properties.
  153. *
  154. * @return string page href
  155. */
  156. public function getHref()
  157. {
  158. if ($this->_hrefCache) {
  159. return $this->_hrefCache;
  160. }
  161. if (null === self::$_urlHelper) {
  162. self::$_urlHelper =
  163. Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  164. }
  165. $params = $this->getParams();
  166. if ($param = $this->getModule()) {
  167. $params['module'] = $param;
  168. }
  169. if ($param = $this->getController()) {
  170. $params['controller'] = $param;
  171. }
  172. if ($param = $this->getAction()) {
  173. $params['action'] = $param;
  174. }
  175. $url = self::$_urlHelper->url($params,
  176. $this->getRoute(),
  177. $this->getResetParams());
  178. return $this->_hrefCache = $url;
  179. }
  180. /**
  181. * Sets action name to use when assembling URL
  182. *
  183. * @see getHref()
  184. *
  185. * @param string $action action name
  186. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  187. * @throws Zend_Navigation_Exception if invalid $action is given
  188. */
  189. public function setAction($action)
  190. {
  191. if (null !== $action && !is_string($action)) {
  192. require_once 'Zend/Navigation/Exception.php';
  193. throw new Zend_Navigation_Exception(
  194. 'Invalid argument: $action must be a string or null');
  195. }
  196. $this->_action = $action;
  197. $this->_hrefCache = null;
  198. return $this;
  199. }
  200. /**
  201. * Returns action name to use when assembling URL
  202. *
  203. * @see getHref()
  204. *
  205. * @return string|null action name
  206. */
  207. public function getAction()
  208. {
  209. return $this->_action;
  210. }
  211. /**
  212. * Sets controller name to use when assembling URL
  213. *
  214. * @see getHref()
  215. *
  216. * @param string|null $controller controller name
  217. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  218. * @throws Zend_Navigation_Exception if invalid controller name is given
  219. */
  220. public function setController($controller)
  221. {
  222. if (null !== $controller && !is_string($controller)) {
  223. require_once 'Zend/Navigation/Exception.php';
  224. throw new Zend_Navigation_Exception(
  225. 'Invalid argument: $controller must be a string or null');
  226. }
  227. $this->_controller = $controller;
  228. $this->_hrefCache = null;
  229. return $this;
  230. }
  231. /**
  232. * Returns controller name to use when assembling URL
  233. *
  234. * @see getHref()
  235. *
  236. * @return string|null controller name or null
  237. */
  238. public function getController()
  239. {
  240. return $this->_controller;
  241. }
  242. /**
  243. * Sets module name to use when assembling URL
  244. *
  245. * @see getHref()
  246. *
  247. * @param string|null $module module name
  248. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  249. * @throws Zend_Navigation_Exception if invalid module name is given
  250. */
  251. public function setModule($module)
  252. {
  253. if (null !== $module && !is_string($module)) {
  254. require_once 'Zend/Navigation/Exception.php';
  255. throw new Zend_Navigation_Exception(
  256. 'Invalid argument: $module must be a string or null');
  257. }
  258. $this->_module = $module;
  259. $this->_hrefCache = null;
  260. return $this;
  261. }
  262. /**
  263. * Returns module name to use when assembling URL
  264. *
  265. * @see getHref()
  266. *
  267. * @return string|null module name or null
  268. */
  269. public function getModule()
  270. {
  271. return $this->_module;
  272. }
  273. /**
  274. * Sets params to use when assembling URL
  275. *
  276. * @see getHref()
  277. *
  278. * @param array|null $params [optional] page params. Default is null
  279. * which sets no params.
  280. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  281. */
  282. public function setParams(array $params = null)
  283. {
  284. if (null === $params) {
  285. $this->_params = array();
  286. } else {
  287. // TODO: do this more intelligently?
  288. $this->_params = $params;
  289. }
  290. $this->_hrefCache = null;
  291. return $this;
  292. }
  293. /**
  294. * Returns params to use when assembling URL
  295. *
  296. * @see getHref()
  297. *
  298. * @return array page params
  299. */
  300. public function getParams()
  301. {
  302. return $this->_params;
  303. }
  304. /**
  305. * Sets route name to use when assembling URL
  306. *
  307. * @see getHref()
  308. *
  309. * @param string $route route name to use when assembling URL
  310. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  311. * @throws Zend_Navigation_Exception if invalid $route is given
  312. */
  313. public function setRoute($route)
  314. {
  315. if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
  316. require_once 'Zend/Navigation/Exception.php';
  317. throw new Zend_Navigation_Exception(
  318. 'Invalid argument: $route must be a non-empty string or null');
  319. }
  320. $this->_route = $route;
  321. $this->_hrefCache = null;
  322. return $this;
  323. }
  324. /**
  325. * Returns route name to use when assembling URL
  326. *
  327. * @see getHref()
  328. *
  329. * @return string route name
  330. */
  331. public function getRoute()
  332. {
  333. return $this->_route;
  334. }
  335. /**
  336. * Sets whether params should be reset when assembling URL
  337. *
  338. * @see getHref()
  339. *
  340. * @param bool $resetParams whether params should be reset when
  341. * assembling URL
  342. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  343. */
  344. public function setResetParams($resetParams)
  345. {
  346. $this->_resetParams = (bool) $resetParams;
  347. $this->_hrefCache = null;
  348. return $this;
  349. }
  350. /**
  351. * Returns whether params should be reset when assembling URL
  352. *
  353. * @see getHref()
  354. *
  355. * @return bool whether params should be reset when assembling URL
  356. */
  357. public function getResetParams()
  358. {
  359. return $this->_resetParams;
  360. }
  361. /**
  362. * Sets action helper for assembling URLs
  363. *
  364. * @see getHref()
  365. *
  366. * @param Zend_Controller_Action_Helper_Url $uh URL helper
  367. * @return void
  368. */
  369. public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh)
  370. {
  371. self::$_urlHelper = $uh;
  372. }
  373. // Public methods:
  374. /**
  375. * Returns an array representation of the page
  376. *
  377. * @return array associative array containing all page properties
  378. */
  379. public function toArray()
  380. {
  381. return array_merge(
  382. parent::toArray(),
  383. array(
  384. 'action' => $this->getAction(),
  385. 'controller' => $this->getController(),
  386. 'module' => $this->getModule(),
  387. 'params' => $this->getParams(),
  388. 'route' => $this->getRoute(),
  389. 'reset_params' => $this->getResetParams()
  390. ));
  391. }
  392. }