Mvc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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-2011 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-2011 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 ($this->_route) {
  126. $route = $front->getRouter()->getRoute($this->_route);
  127. if(method_exists($route, 'getDefaults')) {
  128. $myParams = array_merge($route->getDefaults(), $myParams);
  129. }
  130. }
  131. if (null !== $this->_module) {
  132. $myParams['module'] = $this->_module;
  133. } elseif(!array_key_exists('module', $myParams)) {
  134. $myParams['module'] = $front->getDefaultModule();
  135. }
  136. if (null !== $this->_controller) {
  137. $myParams['controller'] = $this->_controller;
  138. } elseif(!array_key_exists('controller', $myParams)) {
  139. $myParams['controller'] = $front->getDefaultControllerName();
  140. }
  141. if (null !== $this->_action) {
  142. $myParams['action'] = $this->_action;
  143. } elseif(!array_key_exists('action', $myParams)) {
  144. $myParams['action'] = $front->getDefaultAction();
  145. }
  146. foreach($myParams as $key => $value) {
  147. if($value == null) {
  148. unset($myParams[$key]);
  149. }
  150. }
  151. if (count(array_intersect_assoc($reqParams, $myParams)) ==
  152. count($myParams)) {
  153. $this->_active = true;
  154. return true;
  155. }
  156. }
  157. return parent::isActive($recursive);
  158. }
  159. /**
  160. * Returns href for this page
  161. *
  162. * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble
  163. * the href based on the page's properties.
  164. *
  165. * @return string page href
  166. */
  167. public function getHref()
  168. {
  169. if ($this->_hrefCache) {
  170. return $this->_hrefCache;
  171. }
  172. if (null === self::$_urlHelper) {
  173. self::$_urlHelper =
  174. Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  175. }
  176. $params = $this->getParams();
  177. if ($param = $this->getModule()) {
  178. $params['module'] = $param;
  179. }
  180. if ($param = $this->getController()) {
  181. $params['controller'] = $param;
  182. }
  183. if ($param = $this->getAction()) {
  184. $params['action'] = $param;
  185. }
  186. $url = self::$_urlHelper->url($params,
  187. $this->getRoute(),
  188. $this->getResetParams());
  189. return $this->_hrefCache = $url;
  190. }
  191. /**
  192. * Sets action name to use when assembling URL
  193. *
  194. * @see getHref()
  195. *
  196. * @param string $action action name
  197. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  198. * @throws Zend_Navigation_Exception if invalid $action is given
  199. */
  200. public function setAction($action)
  201. {
  202. if (null !== $action && !is_string($action)) {
  203. require_once 'Zend/Navigation/Exception.php';
  204. throw new Zend_Navigation_Exception(
  205. 'Invalid argument: $action must be a string or null');
  206. }
  207. $this->_action = $action;
  208. $this->_hrefCache = null;
  209. return $this;
  210. }
  211. /**
  212. * Returns action name to use when assembling URL
  213. *
  214. * @see getHref()
  215. *
  216. * @return string|null action name
  217. */
  218. public function getAction()
  219. {
  220. return $this->_action;
  221. }
  222. /**
  223. * Sets controller name to use when assembling URL
  224. *
  225. * @see getHref()
  226. *
  227. * @param string|null $controller controller name
  228. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  229. * @throws Zend_Navigation_Exception if invalid controller name is given
  230. */
  231. public function setController($controller)
  232. {
  233. if (null !== $controller && !is_string($controller)) {
  234. require_once 'Zend/Navigation/Exception.php';
  235. throw new Zend_Navigation_Exception(
  236. 'Invalid argument: $controller must be a string or null');
  237. }
  238. $this->_controller = $controller;
  239. $this->_hrefCache = null;
  240. return $this;
  241. }
  242. /**
  243. * Returns controller name to use when assembling URL
  244. *
  245. * @see getHref()
  246. *
  247. * @return string|null controller name or null
  248. */
  249. public function getController()
  250. {
  251. return $this->_controller;
  252. }
  253. /**
  254. * Sets module name to use when assembling URL
  255. *
  256. * @see getHref()
  257. *
  258. * @param string|null $module module name
  259. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  260. * @throws Zend_Navigation_Exception if invalid module name is given
  261. */
  262. public function setModule($module)
  263. {
  264. if (null !== $module && !is_string($module)) {
  265. require_once 'Zend/Navigation/Exception.php';
  266. throw new Zend_Navigation_Exception(
  267. 'Invalid argument: $module must be a string or null');
  268. }
  269. $this->_module = $module;
  270. $this->_hrefCache = null;
  271. return $this;
  272. }
  273. /**
  274. * Returns module name to use when assembling URL
  275. *
  276. * @see getHref()
  277. *
  278. * @return string|null module name or null
  279. */
  280. public function getModule()
  281. {
  282. return $this->_module;
  283. }
  284. /**
  285. * Sets params to use when assembling URL
  286. *
  287. * @see getHref()
  288. *
  289. * @param array|null $params [optional] page params. Default is null
  290. * which sets no params.
  291. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  292. */
  293. public function setParams(array $params = null)
  294. {
  295. if (null === $params) {
  296. $this->_params = array();
  297. } else {
  298. // TODO: do this more intelligently?
  299. $this->_params = $params;
  300. }
  301. $this->_hrefCache = null;
  302. return $this;
  303. }
  304. /**
  305. * Returns params to use when assembling URL
  306. *
  307. * @see getHref()
  308. *
  309. * @return array page params
  310. */
  311. public function getParams()
  312. {
  313. return $this->_params;
  314. }
  315. /**
  316. * Sets route name to use when assembling URL
  317. *
  318. * @see getHref()
  319. *
  320. * @param string $route route name to use when assembling URL
  321. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  322. * @throws Zend_Navigation_Exception if invalid $route is given
  323. */
  324. public function setRoute($route)
  325. {
  326. if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
  327. require_once 'Zend/Navigation/Exception.php';
  328. throw new Zend_Navigation_Exception(
  329. 'Invalid argument: $route must be a non-empty string or null');
  330. }
  331. $this->_route = $route;
  332. $this->_hrefCache = null;
  333. return $this;
  334. }
  335. /**
  336. * Returns route name to use when assembling URL
  337. *
  338. * @see getHref()
  339. *
  340. * @return string route name
  341. */
  342. public function getRoute()
  343. {
  344. return $this->_route;
  345. }
  346. /**
  347. * Sets whether params should be reset when assembling URL
  348. *
  349. * @see getHref()
  350. *
  351. * @param bool $resetParams whether params should be reset when
  352. * assembling URL
  353. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  354. */
  355. public function setResetParams($resetParams)
  356. {
  357. $this->_resetParams = (bool) $resetParams;
  358. $this->_hrefCache = null;
  359. return $this;
  360. }
  361. /**
  362. * Returns whether params should be reset when assembling URL
  363. *
  364. * @see getHref()
  365. *
  366. * @return bool whether params should be reset when assembling URL
  367. */
  368. public function getResetParams()
  369. {
  370. return $this->_resetParams;
  371. }
  372. /**
  373. * Sets action helper for assembling URLs
  374. *
  375. * @see getHref()
  376. *
  377. * @param Zend_Controller_Action_Helper_Url $uh URL helper
  378. * @return void
  379. */
  380. public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh)
  381. {
  382. self::$_urlHelper = $uh;
  383. }
  384. // Public methods:
  385. /**
  386. * Returns an array representation of the page
  387. *
  388. * @return array associative array containing all page properties
  389. */
  390. public function toArray()
  391. {
  392. return array_merge(
  393. parent::toArray(),
  394. array(
  395. 'action' => $this->getAction(),
  396. 'controller' => $this->getController(),
  397. 'module' => $this->getModule(),
  398. 'params' => $this->getParams(),
  399. 'route' => $this->getRoute(),
  400. 'reset_params' => $this->getResetParams()
  401. ));
  402. }
  403. }