Mvc.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. * Scheme to use when assembling URL
  102. *
  103. * @see getHref()
  104. * @var string
  105. */
  106. protected $_scheme;
  107. /**
  108. * Cached href
  109. *
  110. * The use of this variable minimizes execution time when getHref() is
  111. * called more than once during the lifetime of a request. If a property
  112. * is updated, the cache is invalidated.
  113. *
  114. * @var string
  115. */
  116. protected $_hrefCache;
  117. /**
  118. * Action helper for assembling URLs
  119. *
  120. * @see getHref()
  121. * @var Zend_Controller_Action_Helper_Url
  122. */
  123. protected static $_urlHelper = null;
  124. /**
  125. * View helper for assembling URLs with schemes
  126. *
  127. * @see getHref()
  128. * @var Zend_View_Helper_ServerUrl
  129. */
  130. protected static $_schemeHelper = null;
  131. // Accessors:
  132. /**
  133. * Returns whether page should be considered active or not
  134. *
  135. * This method will compare the page properties against the request object
  136. * that is found in the front controller.
  137. *
  138. * @param bool $recursive [optional] whether page should be considered
  139. * active if any child pages are active. Default is
  140. * false.
  141. * @return bool whether page should be considered active or not
  142. */
  143. public function isActive($recursive = false)
  144. {
  145. if (null === $this->_active) {
  146. $front = Zend_Controller_Front::getInstance();
  147. $request = $front->getRequest();
  148. $reqParams = array();
  149. if ($request) {
  150. $reqParams = $request->getParams();
  151. if (!array_key_exists('module', $reqParams)) {
  152. $reqParams['module'] = $front->getDefaultModule();
  153. }
  154. }
  155. $myParams = $this->_params;
  156. if ($this->_route) {
  157. $route = $front->getRouter()->getRoute($this->_route);
  158. if(method_exists($route, 'getDefaults')) {
  159. $myParams = array_merge($route->getDefaults(), $myParams);
  160. }
  161. }
  162. if (null !== $this->_module) {
  163. $myParams['module'] = $this->_module;
  164. } elseif(!array_key_exists('module', $myParams)) {
  165. $myParams['module'] = $front->getDefaultModule();
  166. }
  167. if (null !== $this->_controller) {
  168. $myParams['controller'] = $this->_controller;
  169. } elseif(!array_key_exists('controller', $myParams)) {
  170. $myParams['controller'] = $front->getDefaultControllerName();
  171. }
  172. if (null !== $this->_action) {
  173. $myParams['action'] = $this->_action;
  174. } elseif(!array_key_exists('action', $myParams)) {
  175. $myParams['action'] = $front->getDefaultAction();
  176. }
  177. foreach($myParams as $key => $value) {
  178. if(null === $value) {
  179. unset($myParams[$key]);
  180. }
  181. }
  182. if (count(array_intersect_assoc($reqParams, $myParams)) ==
  183. count($myParams)) {
  184. $this->_active = true;
  185. return true;
  186. }
  187. $this->_active = false;
  188. }
  189. return parent::isActive($recursive);
  190. }
  191. /**
  192. * Returns href for this page
  193. *
  194. * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble
  195. * the href based on the page's properties.
  196. *
  197. * @return string page href
  198. */
  199. public function getHref()
  200. {
  201. if ($this->_hrefCache) {
  202. return $this->_hrefCache;
  203. }
  204. if (null === self::$_urlHelper) {
  205. self::$_urlHelper =
  206. Zend_Controller_Action_HelperBroker::getStaticHelper('Url');
  207. }
  208. $params = $this->getParams();
  209. if ($param = $this->getModule()) {
  210. $params['module'] = $param;
  211. }
  212. if ($param = $this->getController()) {
  213. $params['controller'] = $param;
  214. }
  215. if ($param = $this->getAction()) {
  216. $params['action'] = $param;
  217. }
  218. $url = self::$_urlHelper->url($params,
  219. $this->getRoute(),
  220. $this->getResetParams(),
  221. $this->getEncodeUrl());
  222. // Use scheme?
  223. $scheme = $this->getScheme();
  224. if (null !== $scheme) {
  225. if (null === self::$_schemeHelper) {
  226. require_once 'Zend/View/Helper/ServerUrl.php';
  227. self::$_schemeHelper = new Zend_View_Helper_ServerUrl();
  228. }
  229. $url = self::$_schemeHelper->setScheme($scheme)->serverUrl($url);
  230. }
  231. // Add the fragment identifier if it is set
  232. $fragment = $this->getFragment();
  233. if (null !== $fragment) {
  234. $url .= '#' . $fragment;
  235. }
  236. return $this->_hrefCache = $url;
  237. }
  238. /**
  239. * Sets action name to use when assembling URL
  240. *
  241. * @see getHref()
  242. *
  243. * @param string $action action name
  244. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  245. * @throws Zend_Navigation_Exception if invalid $action is given
  246. */
  247. public function setAction($action)
  248. {
  249. if (null !== $action && !is_string($action)) {
  250. require_once 'Zend/Navigation/Exception.php';
  251. throw new Zend_Navigation_Exception(
  252. 'Invalid argument: $action must be a string or null');
  253. }
  254. $this->_action = $action;
  255. $this->_hrefCache = null;
  256. return $this;
  257. }
  258. /**
  259. * Returns action name to use when assembling URL
  260. *
  261. * @see getHref()
  262. *
  263. * @return string|null action name
  264. */
  265. public function getAction()
  266. {
  267. return $this->_action;
  268. }
  269. /**
  270. * Sets controller name to use when assembling URL
  271. *
  272. * @see getHref()
  273. *
  274. * @param string|null $controller controller name
  275. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  276. * @throws Zend_Navigation_Exception if invalid controller name is given
  277. */
  278. public function setController($controller)
  279. {
  280. if (null !== $controller && !is_string($controller)) {
  281. require_once 'Zend/Navigation/Exception.php';
  282. throw new Zend_Navigation_Exception(
  283. 'Invalid argument: $controller must be a string or null');
  284. }
  285. $this->_controller = $controller;
  286. $this->_hrefCache = null;
  287. return $this;
  288. }
  289. /**
  290. * Returns controller name to use when assembling URL
  291. *
  292. * @see getHref()
  293. *
  294. * @return string|null controller name or null
  295. */
  296. public function getController()
  297. {
  298. return $this->_controller;
  299. }
  300. /**
  301. * Sets module name to use when assembling URL
  302. *
  303. * @see getHref()
  304. *
  305. * @param string|null $module module name
  306. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  307. * @throws Zend_Navigation_Exception if invalid module name is given
  308. */
  309. public function setModule($module)
  310. {
  311. if (null !== $module && !is_string($module)) {
  312. require_once 'Zend/Navigation/Exception.php';
  313. throw new Zend_Navigation_Exception(
  314. 'Invalid argument: $module must be a string or null');
  315. }
  316. $this->_module = $module;
  317. $this->_hrefCache = null;
  318. return $this;
  319. }
  320. /**
  321. * Returns module name to use when assembling URL
  322. *
  323. * @see getHref()
  324. *
  325. * @return string|null module name or null
  326. */
  327. public function getModule()
  328. {
  329. return $this->_module;
  330. }
  331. /**
  332. * Set multiple parameters (to use when assembling URL) at once
  333. *
  334. * URL options passed to the url action helper for assembling URLs.
  335. * Overwrites any previously set parameters!
  336. *
  337. * @see getHref()
  338. *
  339. * @param array|null $params [optional] paramters as array
  340. * ('name' => 'value'). Default is null
  341. * which clears all params.
  342. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  343. */
  344. public function setParams(array $params = null)
  345. {
  346. $this->clearParams();
  347. if (is_array($params)) {
  348. $this->addParams($params);
  349. }
  350. return $this;
  351. }
  352. /**
  353. * Set parameter (to use when assembling URL)
  354. *
  355. * URL option passed to the url action helper for assembling URLs.
  356. *
  357. * @see getHref()
  358. *
  359. * @param string $name parameter name
  360. * @param mixed $value parameter value
  361. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  362. */
  363. public function setParam($name, $value)
  364. {
  365. $name = (string) $name;
  366. $this->_params[$name] = $value;
  367. $this->_hrefCache = null;
  368. return $this;
  369. }
  370. /**
  371. * Add multiple parameters (to use when assembling URL) at once
  372. *
  373. * URL options passed to the url action helper for assembling URLs.
  374. *
  375. * @see getHref()
  376. *
  377. * @param array $params paramters as array ('name' => 'value')
  378. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  379. */
  380. public function addParams(array $params)
  381. {
  382. foreach ($params as $name => $value) {
  383. $this->setParam($name, $value);
  384. }
  385. return $this;
  386. }
  387. /**
  388. * Remove parameter (to use when assembling URL)
  389. *
  390. * @see getHref()
  391. *
  392. * @param string $name
  393. * @return bool
  394. */
  395. public function removeParam($name)
  396. {
  397. if (array_key_exists($name, $this->_params)) {
  398. unset($this->_params[$name]);
  399. $this->_hrefCache = null;
  400. return true;
  401. }
  402. return false;
  403. }
  404. /**
  405. * Clear all parameters (to use when assembling URL)
  406. *
  407. * @see getHref()
  408. *
  409. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  410. */
  411. public function clearParams()
  412. {
  413. $this->_params = array();
  414. $this->_hrefCache = null;
  415. return $this;
  416. }
  417. /**
  418. * Retrieve all parameters (to use when assembling URL)
  419. *
  420. * @see getHref()
  421. *
  422. * @return array parameters as array ('name' => 'value')
  423. */
  424. public function getParams()
  425. {
  426. return $this->_params;
  427. }
  428. /**
  429. * Retrieve a single parameter (to use when assembling URL)
  430. *
  431. * @see getHref()
  432. *
  433. * @param string $name parameter name
  434. * @return mixed
  435. */
  436. public function getParam($name)
  437. {
  438. $name = (string) $name;
  439. if (!array_key_exists($name, $this->_params)) {
  440. return null;
  441. }
  442. return $this->_params[$name];
  443. }
  444. /**
  445. * Sets route name to use when assembling URL
  446. *
  447. * @see getHref()
  448. *
  449. * @param string $route route name to use when assembling URL
  450. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  451. * @throws Zend_Navigation_Exception if invalid $route is given
  452. */
  453. public function setRoute($route)
  454. {
  455. if (null !== $route && (!is_string($route) || strlen($route) < 1)) {
  456. require_once 'Zend/Navigation/Exception.php';
  457. throw new Zend_Navigation_Exception(
  458. 'Invalid argument: $route must be a non-empty string or null');
  459. }
  460. $this->_route = $route;
  461. $this->_hrefCache = null;
  462. return $this;
  463. }
  464. /**
  465. * Returns route name to use when assembling URL
  466. *
  467. * @see getHref()
  468. *
  469. * @return string route name
  470. */
  471. public function getRoute()
  472. {
  473. return $this->_route;
  474. }
  475. /**
  476. * Sets whether params should be reset when assembling URL
  477. *
  478. * @see getHref()
  479. *
  480. * @param bool $resetParams whether params should be reset when
  481. * assembling URL
  482. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  483. */
  484. public function setResetParams($resetParams)
  485. {
  486. $this->_resetParams = (bool) $resetParams;
  487. $this->_hrefCache = null;
  488. return $this;
  489. }
  490. /**
  491. * Returns whether params should be reset when assembling URL
  492. *
  493. * @see getHref()
  494. *
  495. * @return bool whether params should be reset when assembling URL
  496. */
  497. public function getResetParams()
  498. {
  499. return $this->_resetParams;
  500. }
  501. /**
  502. * Sets whether href should be encoded when assembling URL
  503. *
  504. * @see getHref()
  505. *
  506. * @param bool $resetParams whether href should be encoded when
  507. * assembling URL
  508. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  509. */
  510. public function setEncodeUrl($encodeUrl)
  511. {
  512. $this->_encodeUrl = (bool) $encodeUrl;
  513. $this->_hrefCache = null;
  514. return $this;
  515. }
  516. /**
  517. * Returns whether herf should be encoded when assembling URL
  518. *
  519. * @see getHref()
  520. *
  521. * @return bool whether herf should be encoded when assembling URL
  522. */
  523. public function getEncodeUrl()
  524. {
  525. return $this->_encodeUrl;
  526. }
  527. /**
  528. * Sets scheme to use when assembling URL
  529. *
  530. * @see getHref()
  531. *
  532. * @param string|null $scheme scheme
  533. * @return Zend_Navigation_Page_Mvc fluent interface, returns self
  534. */
  535. public function setScheme($scheme)
  536. {
  537. if (null !== $scheme && !is_string($scheme)) {
  538. require_once 'Zend/Navigation/Exception.php';
  539. throw new Zend_Navigation_Exception(
  540. 'Invalid argument: $scheme must be a string or null'
  541. );
  542. }
  543. $this->_scheme = $scheme;
  544. return $this;
  545. }
  546. /**
  547. * Returns scheme to use when assembling URL
  548. *
  549. * @see getHref()
  550. *
  551. * @return string|null scheme or null
  552. */
  553. public function getScheme()
  554. {
  555. return $this->_scheme;
  556. }
  557. /**
  558. * Sets action helper for assembling URLs
  559. *
  560. * @see getHref()
  561. *
  562. * @param Zend_Controller_Action_Helper_Url $uh URL helper
  563. * @return void
  564. */
  565. public static function setUrlHelper(Zend_Controller_Action_Helper_Url $uh)
  566. {
  567. self::$_urlHelper = $uh;
  568. }
  569. /**
  570. * Sets view helper for assembling URLs with schemes
  571. *
  572. * @see getHref()
  573. *
  574. * @param Zend_View_Helper_ServerUrl $sh scheme helper
  575. * @return void
  576. */
  577. public static function setSchemeHelper(Zend_View_Helper_ServerUrl $sh)
  578. {
  579. self::$_schemeHelper = $sh;
  580. }
  581. // Public methods:
  582. /**
  583. * Returns an array representation of the page
  584. *
  585. * @return array associative array containing all page properties
  586. */
  587. public function toArray()
  588. {
  589. return array_merge(
  590. parent::toArray(),
  591. array(
  592. 'action' => $this->getAction(),
  593. 'controller' => $this->getController(),
  594. 'module' => $this->getModule(),
  595. 'params' => $this->getParams(),
  596. 'route' => $this->getRoute(),
  597. 'reset_params' => $this->getResetParams(),
  598. 'encodeUrl' => $this->getEncodeUrl(),
  599. 'scheme' => $this->getScheme(),
  600. )
  601. );
  602. }
  603. }