Mvc.php 18 KB

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