PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/OntoWiki/Navigation.php

https://code.google.com/p/ontowiki/
PHP | 318 lines | 155 code | 46 blank | 117 comment | 42 complexity | 573830a50e803f01735de8327d2803a6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /* vim: sw=4:sts=4:expandtab */
  3. /**
  4. * This file is part of the {@link http://ontowiki.net OntoWiki} project.
  5. *
  6. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  7. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  8. */
  9. /**
  10. * OntoWiki navigation registry.
  11. *
  12. * @category OntoWiki
  13. * @package Navigation
  14. * @copyright Copyright (c) 2008, {@link http://aksw.org AKSW}
  15. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  16. * @author Norman Heino <norman.heino@gmail.com>
  17. */
  18. class OntoWiki_Navigation
  19. {
  20. /**
  21. * Array with navigation elements
  22. * @var array
  23. */
  24. protected static $_navigation = array();
  25. /**
  26. * Array for the default navigation element group
  27. * @var array
  28. */
  29. protected static $_defaultGroups = array();
  30. /**
  31. * Array of navigation elements with configured position
  32. * @var array
  33. */
  34. protected static $_ordered = array();
  35. /**
  36. * Array of navigation elements without a configured position
  37. * @var array
  38. */
  39. protected static $_unordered = array();
  40. /**
  41. * Key of the currently active navigation element
  42. * @var string
  43. */
  44. protected static $_activeKey = null;
  45. /**
  46. * Array of parameters that should be kept when switching navigation elements.
  47. * @var array
  48. */
  49. protected static $_keepParams = array(
  50. 'r'
  51. );
  52. /** @var boolean */
  53. protected static $_isDisabled = false;
  54. /**
  55. * Disables the navigation for the current view.
  56. */
  57. public static function disableNavigation()
  58. {
  59. self::$_isDisabled = true;
  60. }
  61. /**
  62. * Returns the currently active navigation component.
  63. *
  64. * @return array
  65. */
  66. public static function getActive()
  67. {
  68. if (!self::$_activeKey) {
  69. return null;
  70. }
  71. return self::$_navigation[self::$_activeKey];
  72. }
  73. /**
  74. * Returns whether the navigation is disabled for the current view
  75. *
  76. * @return boolean
  77. */
  78. public static function isDisabled()
  79. {
  80. return self::$_isDisabled;
  81. }
  82. /**
  83. * Registers a component with the navigation
  84. *
  85. * @param string $key the identifier for the component
  86. * @param array $options An options array for the navigation entry.
  87. * The following keys are recognized:
  88. * name – The name displayed on the tab
  89. * route – A Zend route name (internal OntoWiki route;
  90. * mapped automatically to a controller and action name
  91. * by Zend). Controller and action keys are ignored if a
  92. * route is given.
  93. * controller – Controller name for the URL
  94. * action – Action name for the URL
  95. * priority – Priority of the tab
  96. * @param boolean $replace Whether to replace previously registered tabs
  97. * with the same name
  98. * @todo Implement functionality to maintain a preferred order
  99. */
  100. public static function register($key, array $options, $replace = false)
  101. {
  102. if (array_key_exists($key, self::$_navigation) && !$replace) {
  103. throw new OntoWiki_Exception("Navigation component with key '$key' already registered.");
  104. }
  105. if (!array_key_exists('name', $options)) {
  106. $options['name'] = $key;
  107. }
  108. // merge defaults
  109. $options = array_merge(array(
  110. 'route' => null,
  111. 'controller' => null,
  112. 'action' => null,
  113. 'name' => null
  114. ), $options);
  115. // add registrant
  116. self::$_navigation[$key] = $options;
  117. // store order request
  118. if (!$replace) {
  119. if (array_key_exists('priority', $options) && is_numeric($options['priority'])) {
  120. $position = (int)$options['priority'];
  121. while (array_key_exists((string)$position, self::$_ordered)) {
  122. $position++;
  123. }
  124. self::$_ordered[$position] = $key;
  125. } else {
  126. self::$_unordered[] = $key;
  127. }
  128. }
  129. // set activation state
  130. // if ((array_key_exists('active', $options) && $options['active'])) {
  131. // self::setActive($key);
  132. // }
  133. }
  134. /**
  135. * Used by the application to register default components
  136. *
  137. * @param string $controllerName
  138. * @param array $actionNames
  139. */
  140. public static function registerDefaultGroup($controllerName, array $actionNames)
  141. {
  142. $groupArray = array();
  143. foreach ($actionNames as $action) {
  144. $groups[$action] = array(
  145. 'controller' => $controllerName,
  146. 'action' => $action,
  147. 'name' => ucfirst($action)
  148. );
  149. }
  150. self::$_defaultGroups[$controllerName] = $groupArray;
  151. }
  152. /**
  153. * Resets the Navigation by deleting all tabs
  154. *
  155. */
  156. public static function reset()
  157. {
  158. self::$_navigation = array();
  159. }
  160. /**
  161. * Sets the currently active navigation component.
  162. * make sure there is only one active
  163. *
  164. * @param string $key the identifier for the component
  165. */
  166. public static function setActive($key)
  167. {
  168. if (!array_key_exists($key, self::$_navigation)) {
  169. throw new OntoWiki_Exception("Navigation component with key '$key' not registered.");
  170. }
  171. // set the current active to unactive
  172. if (self::$_activeKey != null) {
  173. self::$_navigation[self::$_activeKey]['active'] = 'inactive';
  174. }
  175. // set new active
  176. self::$_navigation[$key]['active'] = 'active';
  177. // remember new
  178. self::$_activeKey = $key;
  179. }
  180. /**
  181. * Checks if a navigation components with the given key has been.
  182. *
  183. * @return boolean
  184. */
  185. public static function isRegistered($key)
  186. {
  187. if (array_key_exists($key, self::$_navigation)) {
  188. return true;
  189. }
  190. return false;
  191. }
  192. /**
  193. * Returns an array of registered navigation components
  194. *
  195. * @return array
  196. */
  197. public static function toArray()
  198. {
  199. if (!self::$_isDisabled) {
  200. $return = array();
  201. $session = new Zend_Session_Namespace(_OWSESSION . 'ONTOWIKI_NAVIGATION');
  202. if (isset($session->tabOrder)) {
  203. $over = array_diff(self::$_ordered, $session->tabOrder);
  204. ksort($over);
  205. self::$_ordered = array_merge($session->tabOrder, $over);
  206. }
  207. $request = Zend_Controller_Front::getInstance()->getRequest();
  208. $currentController = $request->getControllerName();
  209. $currentAction = $request->getActionName();
  210. ksort(self::$_ordered);
  211. // first the order requests
  212. foreach (self::$_ordered as $orderKey => $elementKey) {
  213. if (array_key_exists($elementKey, self::$_navigation)) {
  214. self::$_navigation[$elementKey]['url'] = self::_getUrl($elementKey, $currentController, $currentAction);
  215. // set active if current
  216. if ($currentController == self::$_navigation[$elementKey]['controller'] &&
  217. $currentAction == self::$_navigation[$elementKey]['action']) {
  218. self::setActive($elementKey);
  219. }
  220. $return[$elementKey] = self::$_navigation[$elementKey];
  221. }
  222. }
  223. // finally the unordered
  224. foreach (self::$_unordered as $name => $elementKey) {
  225. self::$_navigation[$elementKey]['url'] = self::_getUrl($elementKey, $currentController, $currentAction);
  226. // set active if current
  227. if ($currentController == self::$_navigation[$elementKey]['controller'] &&
  228. $currentAction == self::$_navigation[$elementKey]['action']) {
  229. self::setActive($elementKey);
  230. }
  231. $return[$elementKey] = self::$_navigation[$elementKey];
  232. }
  233. return $return;
  234. }
  235. }
  236. /**
  237. * Returns the URL of the given navigation element
  238. *
  239. * @return OntoWiki_Url
  240. */
  241. protected static function _getUrl($elementKey, $currentController, $currentAction)
  242. {
  243. $request = Zend_Controller_Front::getInstance()->getRequest();
  244. $router = Zend_Controller_Front::getInstance()->getRouter();
  245. $current = self::$_navigation[$elementKey];
  246. $hasRoute = false;
  247. $currentController = $request->getControllerName();
  248. $currentAction = $request->getActionName();
  249. if (isset($current['route'])) {
  250. if ($router->hasRoute($current['route'])) {
  251. $route = $router->getRoute($current['route']);
  252. $defaults = $route->getDefaults();
  253. if ($defaults['controller'] == $current['controller'] && $defaults['action'] == $current['action']) {
  254. $hasRoute = true;
  255. }
  256. }
  257. }
  258. if ($hasRoute) {
  259. $url = new OntoWiki_Url(array('route' => $current['route']), self::$_keepParams);
  260. } else {
  261. $controller = $current['controller'];
  262. $action = $current['action'] ? $current['action'] : null;
  263. $url = new OntoWiki_Url(array('controller' => $controller, 'action' => $action), self::$_keepParams);
  264. }
  265. foreach($current as $key => $value){
  266. if($key != 'route' && $key != 'controller' && $key != 'action' && $key != 'priority' && $key != 'name'){
  267. $url->setParam($key, $value);
  268. }
  269. }
  270. return $url;
  271. }
  272. }