PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_weblinks/router.php

https://github.com/joebushi/joomla
PHP | 298 lines | 199 code | 40 blank | 59 comment | 51 complexity | af24c4d0eb2f9ffe05789373206a6b41 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla
  5. * @copyright Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
  6. * @license GNU General Public License version 2 or later; see LICENSE.txt
  7. */
  8. /* Weblinks Component Route Helper
  9. *
  10. * @package Joomla.Site
  11. * @subpackage com_weblinks
  12. * @since 1.6
  13. */
  14. defined('_JEXEC') or die;
  15. class WeblinksRoute
  16. {
  17. /**
  18. * @var array A cache of the menu items pertaining to com_weblinks
  19. */
  20. protected static $lookup = null;
  21. /**
  22. * @param int $id The id of the weblink.
  23. * @param int $categoryId An optional category id.
  24. *
  25. * @return string The routed link.
  26. */
  27. public static function weblink($id, $categoryId = null)
  28. {
  29. $needles = array(
  30. 'weblink' => (int) $id,
  31. 'category' => (int) $categoryId
  32. );
  33. //Create the link
  34. $link = 'index.php?option=com_weblinks&view=weblink&id='. $id;
  35. if ($categoryId) {
  36. $link .= '&catid='.$categoryId;
  37. }
  38. if ($itemId = self::_findItemId($needles)) {
  39. $link .= '&Itemid='.$itemId;
  40. };
  41. return $link;
  42. }
  43. /**
  44. * @param int $id The id of the weblink.
  45. * @param int $categoryId An optional category id.
  46. *
  47. * @return string The routed link.
  48. */
  49. public static function category($catid, $parentId = null)
  50. {
  51. $needles = array(
  52. 'category' => (int) $catid
  53. );
  54. //Create the link
  55. $link = 'index.php?option=com_weblinks&view=category&id='.$catid;
  56. if ($itemId = self::_findItemId($needles)) {
  57. // TODO: The following should work automatically??
  58. //if (isset($item->query['layout'])) {
  59. // $link .= '&layout='.$item->query['layout'];
  60. //}
  61. $link .= '&Itemid='.$itemId;
  62. };
  63. return $link;
  64. }
  65. protected static function _findItemId($needles)
  66. {
  67. // Prepare the reverse lookup array.
  68. if (self::$lookup === null)
  69. {
  70. self::$lookup = array();
  71. $component = &JComponentHelper::getComponent('com_weblinks');
  72. $menus = &JApplication::getMenu('site', array());
  73. $items = $menus->getItems('component_id', $component->id);
  74. foreach ($items as &$item)
  75. {
  76. if (isset($item->query) && isset($item->query['view']))
  77. {
  78. $view = $item->query['view'];
  79. if (!isset(self::$lookup[$view])) {
  80. self::$lookup[$view] = array();
  81. }
  82. if (isset($item->query['id'])) {
  83. self::$lookup[$view][$item->query['id']] = $item->id;
  84. }
  85. }
  86. }
  87. }
  88. $match = null;
  89. foreach ($needles as $view => $id)
  90. {
  91. if (isset(self::$lookup[$view]))
  92. {
  93. if (isset(self::$lookup[$view][$id])) {
  94. return self::$lookup[$view][$id];
  95. }
  96. }
  97. }
  98. return null;
  99. }
  100. }
  101. /**
  102. * Build the route for the com_weblinks component
  103. *
  104. * @param array An array of URL arguments
  105. *
  106. * @return array The URL arguments to use to assemble the subsequent URL.
  107. */
  108. function WeblinksBuildRoute(&$query)
  109. {
  110. $segments = array();
  111. // get a menu item based on Itemid or currently active
  112. $menu = &JSite::getMenu();
  113. if (empty($query['Itemid'])) {
  114. $menuItem = &$menu->getActive();
  115. }
  116. else {
  117. $menuItem = &$menu->getItem($query['Itemid']);
  118. }
  119. $mView = (empty($menuItem->query['view'])) ? null : $menuItem->query['view'];
  120. $mCatid = (empty($menuItem->query['catid'])) ? null : $menuItem->query['catid'];
  121. $mId = (empty($menuItem->query['id'])) ? null : $menuItem->query['id'];
  122. if (isset($query['view']))
  123. {
  124. $view = $query['view'];
  125. if (empty($query['Itemid'])) {
  126. $segments[] = $query['view'];
  127. }
  128. unset($query['view']);
  129. };
  130. // are we dealing with an weblink that is attached to a menu item?
  131. if (($mView == 'weblink') and (isset($query['id'])) and ($mId == intval($query['id']))) {
  132. unset($query['view']);
  133. unset($query['catid']);
  134. unset($query['id']);
  135. }
  136. if (isset($view) and $view == 'category') {
  137. if ($mId != intval($query['id']) || $mView != $view) {
  138. $segments[] = $query['id'];
  139. }
  140. unset($query['id']);
  141. }
  142. if (isset($query['catid'])) {
  143. // if we are routing a weblink or category where the category id matches the menu catid, don't include the category segment
  144. if ((($view == 'weblink') and ($mView != 'category') and ($mView != 'weblink') and ($mCatid != intval($query['catid'])))) {
  145. $segments[] = $query['catid'];
  146. }
  147. unset($query['catid']);
  148. };
  149. if (isset($query['id']))
  150. {
  151. if (empty($query['Itemid'])) {
  152. $segments[] = $query['id'];
  153. }
  154. else
  155. {
  156. if (isset($menuItem->query['id']))
  157. {
  158. if ($query['id'] != $mId) {
  159. $segments[] = $query['id'];
  160. }
  161. }
  162. else {
  163. $segments[] = $query['id'];
  164. }
  165. }
  166. unset($query['id']);
  167. };
  168. if (isset($query['year']))
  169. {
  170. if (!empty($query['Itemid'])) {
  171. $segments[] = $query['year'];
  172. unset($query['year']);
  173. }
  174. };
  175. if (isset($query['month']))
  176. {
  177. if (!empty($query['Itemid'])) {
  178. $segments[] = $query['month'];
  179. unset($query['month']);
  180. }
  181. };
  182. if (isset($query['layout']))
  183. {
  184. if (!empty($query['Itemid']) && isset($menuItem->query['layout']))
  185. {
  186. if ($query['layout'] == $menuItem->query['layout']) {
  187. unset($query['layout']);
  188. }
  189. }
  190. else
  191. {
  192. if ($query['layout'] == 'default') {
  193. unset($query['layout']);
  194. }
  195. }
  196. };
  197. return $segments;
  198. }
  199. /**
  200. * Parse the segments of a URL.
  201. *
  202. * @param array The segments of the URL to parse.
  203. *
  204. * @return array The URL attributes to be used by the application.
  205. */
  206. function WeblinksParseRoute($segments)
  207. {
  208. $vars = array();
  209. //Get the active menu item.
  210. $menu = &JSite::getMenu();
  211. $item = &$menu->getActive();
  212. // Count route segments
  213. $count = count($segments);
  214. // Standard routing for weblinks.
  215. if (!isset($item))
  216. {
  217. $vars['view'] = $segments[0];
  218. $vars['id'] = $segments[$count - 1];
  219. return $vars;
  220. }
  221. // Handle View and Identifier.
  222. switch ($item->query['view'])
  223. {
  224. case 'categories':
  225. // From the categories view, we can only jump to a category.
  226. if ($count > 1)
  227. {
  228. if (intval($segments[0]) && intval($segments[$count-1]))
  229. {
  230. // 123-path/to/category/456-article
  231. $vars['id'] = $segments[$count-1];
  232. $vars['view'] = 'weblink';
  233. }
  234. else
  235. {
  236. // 123-path/to/category
  237. $vars['id'] = $segments[0];
  238. $vars['view'] = 'category';
  239. }
  240. }
  241. else
  242. {
  243. // 123-category
  244. $vars['id'] = $segments[0];
  245. $vars['view'] = 'category';
  246. }
  247. break;
  248. case 'category':
  249. $vars['id'] = $segments[$count-1];
  250. $vars['view'] = 'weblink';
  251. break;
  252. case 'weblink':
  253. $vars['id'] = $segments[$count-1];
  254. $vars['view'] = 'weblink';
  255. break;
  256. }
  257. return $vars;
  258. }