Mvc.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. *
  4. * Cube Framework $Id$ jeKCHwmclBnGwr/r4M2EA4s5UIsukd1g879OggypTDc=
  5. *
  6. * @link http://codecu.be/framework
  7. * @copyright Copyright (c) 2014 CodeCube SRL
  8. * @license http://codecu.be/framework/license Commercial License
  9. *
  10. * @version 1.0
  11. */
  12. namespace Cube\Navigation\Page;
  13. use Cube\Controller\Front;
  14. class Mvc extends AbstractPage
  15. {
  16. /**
  17. *
  18. * page module
  19. *
  20. * @var string
  21. */
  22. protected $_module;
  23. /**
  24. *
  25. * page controller
  26. *
  27. * @var string
  28. */
  29. protected $_controller;
  30. /**
  31. *
  32. * page action
  33. *
  34. * @var string
  35. */
  36. protected $_action;
  37. /**
  38. *
  39. * page params
  40. *
  41. * @var array
  42. */
  43. protected $_params = array();
  44. /**
  45. *
  46. * get page module
  47. *
  48. * @return string
  49. */
  50. public function getModule()
  51. {
  52. return $this->_module;
  53. }
  54. /**
  55. *
  56. * set page module
  57. *
  58. * @param string $module
  59. * @return \Cube\Navigation\Page\Mvc
  60. * @throws \InvalidArgumentException
  61. */
  62. public function setModule($module)
  63. {
  64. if (!is_string($module) && $module !== null) {
  65. throw new \InvalidArgumentException(sprintf(
  66. "'module' must be a string or null, %s given.", gettype($module)));
  67. }
  68. $this->_module = $module;
  69. return $this;
  70. }
  71. /**
  72. *
  73. * get page controller
  74. *
  75. * @return string
  76. */
  77. public function getController()
  78. {
  79. return $this->_controller;
  80. }
  81. /**
  82. *
  83. * set page controller
  84. *
  85. * @param string $controller
  86. * @return \Cube\Navigation\Page\Mvc
  87. * @throws \InvalidArgumentException
  88. */
  89. public function setController($controller)
  90. {
  91. if (!is_string($controller) && $controller !== null) {
  92. throw new \InvalidArgumentException(sprintf(
  93. "'controller' must be a string or null, %s given.", gettype($controller)));
  94. }
  95. $this->_controller = $controller;
  96. return $this;
  97. }
  98. /**
  99. *
  100. * get page action
  101. *
  102. * @return string
  103. */
  104. public function getAction()
  105. {
  106. return $this->_action;
  107. }
  108. /**
  109. *
  110. * set page action
  111. *
  112. * @param string $action
  113. * @return \Cube\Navigation\Page\Mvc
  114. * @throws \InvalidArgumentException
  115. */
  116. public function setAction($action)
  117. {
  118. if (!is_string($action) && $action !== null) {
  119. throw new \InvalidArgumentException(sprintf(
  120. "'action' must be a string or null, %s given.", gettype($action)));
  121. }
  122. $this->_action = $action;
  123. return $this;
  124. }
  125. /**
  126. *
  127. * get page params
  128. *
  129. * @return string
  130. */
  131. public function getParams()
  132. {
  133. $params = array(
  134. 'module' => $this->_module,
  135. 'controller' => $this->_controller,
  136. 'action' => $this->_action,
  137. );
  138. foreach ($this->_params as $key => $value) {
  139. $params[$key] = $value;
  140. }
  141. return $params;
  142. }
  143. /**
  144. *
  145. * set page params
  146. *
  147. * @param array $params
  148. * @return \Cube\Navigation\Page\Mvc
  149. */
  150. public function setParams(array $params = null)
  151. {
  152. $this->_params = (array) $params;
  153. return $this;
  154. }
  155. /**
  156. *
  157. * check if the page is active, based on the request uri
  158. *
  159. * @param bool $recursive check in subpages as well, and if a subpage is active, return the current page as active
  160. * @return bool returns active status
  161. */
  162. public function isActive($recursive = false)
  163. {
  164. if (!$this->_active) {
  165. $frontController = Front::getInstance();
  166. $request = $frontController->getRequest();
  167. $router = $frontController->getRouter();
  168. $mvcParams = array(
  169. 'module' => $this->_module,
  170. 'controller' => $this->_controller,
  171. 'action' => $this->_action,
  172. );
  173. $uri = $router->assemble(
  174. array_merge($mvcParams, $this->_params), null, false);
  175. if ($request->matchRequestUri($uri)) {
  176. $this->_active = true;
  177. return true;
  178. }
  179. }
  180. return parent::isActive($recursive);
  181. }
  182. }