Mvc.php 12 KB

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