Mvc.php 14 KB

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