PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_contact/router.php

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