PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Zend/Navigation/Page/Mvc.php

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