Mvc.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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-2012 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-2012 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. * Whether href should be encoded when assembling URL
  89. *
  90. * @see getHref()
  91. * @var bool
  92. */
  93. protected $_encodeUrl = true;
  94. /**
  95. * Cached href
  96. *
  97. * The use of this variable minimizes execution time when getHref() is
  98. * called more than once during the lifetime of a request. If a property
  99. * is updated, the cache is invalidated.
  100. *
  101. * @var string
  102. */
  103. protected $_hrefCache;
  104. /**
  105. * Action helper for assembling URLs
  106. *
  107. * @see getHref()
  108. * @var Zend_Controller_Action_Helper_Url
  109. */
  110. protected static $_urlHelper = null;
  111. // Accessors:
  112. /**
  113. * Returns whether page should be considered active or not
  114. *
  115. * This method will compare the page properties against the request object
  116. * that is found in the front controller.
  117. *
  118. * @param bool $recursive [optional] whether page should be considered
  119. * active if any child pages are active. Default is
  120. * false.
  121. * @return bool whether page should be considered active or not
  122. */
  123. public function isActive($recursive = false)
  124. {
  125. if (!$this->_active) {
  126. $front = Zend_Controller_Front::getInstance();
  127. $request = $front->getRequest();
  128. $reqParams = array();
  129. if ($request) {
  130. $reqParams = $request->getParams();
  131. if (!array_key_exists('module', $reqParams)) {
  132. $reqParams['module'] = $front->getDefaultModule();
  133. }
  134. }
  135. $myParams = $this->_params;
  136. if ($this->_route) {
  137. $route = $front->getRouter()->getRoute($this->_route);
  138. if(method_exists($route, 'getDefaults')) {
  139. $myParams = array_merge($route->getDefaults(), $myParams);
  140. }
  141. }
  142. if (null !== $this->_module) {
  143. $myParams['module'] = $this->_module;
  144. } elseif(!array_key_exists('module', $myParams)) {
  145. $myParams['module'] = $front->getDefaultModule();
  146. }
  147. if (null !== $this->_controller) {
  148. $myParams['controller'] = $this->_controller;
  149. } elseif(!array_key_exists('controller', $myParams)) {
  150. $myParams['controller'] = $front->getDefaultControllerName();
  151. }
  152. if (null !== $this->_action) {
  153. $myParams['action'] = $this->_action;
  154. } elseif(!array_key_exists('action', $myParams)) {
  155. $myParams['action'] = $front->getDefaultAction();
  156. }
  157. foreach($myParams as $key => $value) {
  158. if($value == null) {
  159. unset($myParams[$key]);
  160. }
  161. }
  162. if (count(array_intersect_assoc($reqParams, $myParams)) ==
  163. count($myParams)) {
  164. $this->_active = true;
  165. return true;
  166. }
  167. }
  168. return parent::isActive($recursive);
  169. }
  170. /**
  171. * Returns href for this page
  172. *
  173. * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble
  174. * the href based on the page's properties.
  175. *
  176. * @return string page href
  177. */
  178. public function getHref()
  179. {
  180. if ($this->_hrefCache) {
  181. return $this->_hrefCache;
  182. }
  183. if (null === self::$_urlHelper) {
  184. self::$_urlHelper =
  185. Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  186. }
  187. $params = $this->getParams();
  188. if ($param = $this->getModule()) {
  189. $params['module'] = $param;
  190. }
  191. if ($param = $this->getController()) {
  192. $params['controller'] = $param;
  193. }
  194. if ($param = $this->getAction()) {
  195. $params['action'] = $param;
  196. }
  197. $url = self::$_urlHelper->url($params,
  198. $this->getRoute(),
  199. $this->getResetParams(),
  200. $this->getEncodeUrl());
  201. // Add the fragment identifier if it is set
  202. $fragment = $this->getFragment();
  203. if (null !== $fragment) {
  204. $url .= '#' . $fragment;
  205. }
  206. return $this->_hrefCache = $url;
  207. }
  208. /**
  209. * Sets action name to use when assembling URL
  210. *
  211. * @see getHref()
  212. *
  213. * @param string $action action name
  214. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  215. * @throws Zend_Navigation_Exception if invalid $action is given
  216. */
  217. public function setAction($action)
  218. {
  219. if (null !== $action && !is_string($action)) {
  220. require_once 'Zend/Navigation/Exception.php';
  221. throw new Zend_Navigation_Exception(
  222. 'Invalid argument: $action must be a string or null');
  223. }
  224. $this->_action = $action;
  225. $this->_hrefCache = null;
  226. return $this;
  227. }
  228. /**
  229. * Returns action name to use when assembling URL
  230. *
  231. * @see getHref()
  232. *
  233. * @return string|null action name
  234. */
  235. public function getAction()
  236. {
  237. return $this->_action;
  238. }
  239. /**
  240. * Sets controller name to use when assembling URL
  241. *
  242. * @see getHref()
  243. *
  244. * @param string|null $controller controller name
  245. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  246. * @throws Zend_Navigation_Exception if invalid controller name is given
  247. */
  248. public function setController($controller)
  249. {
  250. if (null !== $controller && !is_string($controller)) {
  251. require_once 'Zend/Navigation/Exception.php';
  252. throw new Zend_Navigation_Exception(
  253. 'Invalid argument: $controller must be a string or null');
  254. }
  255. $this->_controller = $controller;
  256. $this->_hrefCache = null;
  257. return $this;
  258. }
  259. /**
  260. * Returns controller name to use when assembling URL
  261. *
  262. * @see getHref()
  263. *
  264. * @return string|null controller name or null
  265. */
  266. public function getController()
  267. {
  268. return $this->_controller;
  269. }
  270. /**
  271. * Sets module name to use when assembling URL
  272. *
  273. * @see getHref()
  274. *
  275. * @param string|null $module module name
  276. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  277. * @throws Zend_Navigation_Exception if invalid module name is given
  278. */
  279. public function setModule($module)
  280. {
  281. if (null !== $module && !is_string($module)) {
  282. require_once 'Zend/Navigation/Exception.php';
  283. throw new Zend_Navigation_Exception(
  284. 'Invalid argument: $module must be a string or null');
  285. }
  286. $this->_module = $module;
  287. $this->_hrefCache = null;
  288. return $this;
  289. }
  290. /**
  291. * Returns module name to use when assembling URL
  292. *
  293. * @see getHref()
  294. *
  295. * @return string|null module name or null
  296. */
  297. public function getModule()
  298. {
  299. return $this->_module;
  300. }
  301. /**
  302. * Sets params to use when assembling URL
  303. *
  304. * @see getHref()
  305. *
  306. * @param array|null $params [optional] page params. Default is null
  307. * which sets no params.
  308. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  309. */
  310. public function setParams(array $params = null)
  311. {
  312. if (null === $params) {
  313. $this->_params = array();
  314. } else {
  315. // TODO: do this more intelligently?
  316. $this->_params = $params;
  317. }
  318. $this->_hrefCache = null;
  319. return $this;
  320. }
  321. /**
  322. * Returns params to use when assembling URL
  323. *
  324. * @see getHref()
  325. *
  326. * @return array page params
  327. */
  328. public function getParams()
  329. {
  330. return $this->_params;
  331. }
  332. /**
  333. * Sets route name to use when assembling URL
  334. *
  335. * @see getHref()
  336. *
  337. * @param string $route route name to use when assembling URL
  338. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  339. * @throws Zend_Navigation_Exception if invalid $route is given
  340. */
  341. public function setRoute($route)
  342. {
  343. if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
  344. require_once 'Zend/Navigation/Exception.php';
  345. throw new Zend_Navigation_Exception(
  346. 'Invalid argument: $route must be a non-empty string or null');
  347. }
  348. $this->_route = $route;
  349. $this->_hrefCache = null;
  350. return $this;
  351. }
  352. /**
  353. * Returns route name to use when assembling URL
  354. *
  355. * @see getHref()
  356. *
  357. * @return string route name
  358. */
  359. public function getRoute()
  360. {
  361. return $this->_route;
  362. }
  363. /**
  364. * Sets whether params should be reset when assembling URL
  365. *
  366. * @see getHref()
  367. *
  368. * @param bool $resetParams whether params should be reset when
  369. * assembling URL
  370. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  371. */
  372. public function setResetParams($resetParams)
  373. {
  374. $this->_resetParams = (bool) $resetParams;
  375. $this->_hrefCache = null;
  376. return $this;
  377. }
  378. /**
  379. * Returns whether params should be reset when assembling URL
  380. *
  381. * @see getHref()
  382. *
  383. * @return bool whether params should be reset when assembling URL
  384. */
  385. public function getResetParams()
  386. {
  387. return $this->_resetParams;
  388. }
  389. /**
  390. * Sets whether href should be encoded when assembling URL
  391. *
  392. * @see getHref()
  393. *
  394. * @param bool $resetParams whether href should be encoded when
  395. * assembling URL
  396. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  397. */
  398. public function setEncodeUrl($encodeUrl)
  399. {
  400. $this->_encodeUrl = (bool) $encodeUrl;
  401. $this->_hrefCache = null;
  402. return $this;
  403. }
  404. /**
  405. * Returns whether herf should be encoded when assembling URL
  406. *
  407. * @see getHref()
  408. *
  409. * @return bool whether herf should be encoded when assembling URL
  410. */
  411. public function getEncodeUrl()
  412. {
  413. return $this->_encodeUrl;
  414. }
  415. /**
  416. * Sets action helper for assembling URLs
  417. *
  418. * @see getHref()
  419. *
  420. * @param Zend_Controller_Action_Helper_Url $uh URL helper
  421. * @return void
  422. */
  423. public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh)
  424. {
  425. self::$_urlHelper = $uh;
  426. }
  427. // Public methods:
  428. /**
  429. * Returns an array representation of the page
  430. *
  431. * @return array associative array containing all page properties
  432. */
  433. public function toArray()
  434. {
  435. return array_merge(
  436. parent::toArray(),
  437. array(
  438. 'action' => $this->getAction(),
  439. 'controller' => $this->getController(),
  440. 'module' => $this->getModule(),
  441. 'params' => $this->getParams(),
  442. 'route' => $this->getRoute(),
  443. 'reset_params' => $this->getResetParams(),
  444. 'encodeUrl' => $this->getEncodeUrl(),
  445. ));
  446. }
  447. }