PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_content/router.php

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