/components/com_contact/helpers/route.php

https://github.com/pjwiseman/joomla-cms · PHP · 212 lines · 129 code · 28 blank · 55 comment · 33 complexity · 62330e6306b79d6442aefd9c528bdab3 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_contact
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. defined('_JEXEC') or die;
  10. /**
  11. * Contact Component Route Helper
  12. *
  13. * @static
  14. * @package Joomla.Site
  15. * @subpackage com_contact
  16. * @since 1.5
  17. */
  18. abstract class ContactHelperRoute
  19. {
  20. protected static $lookup;
  21. /**
  22. * Get the URL route for a contact from a contact ID, contact category ID and language
  23. *
  24. * @param integer $id The id of the contact
  25. * @param integer $catid The id of the contact's category
  26. * @param mixed $language The id of the language being used.
  27. *
  28. * @return string The link to the contact
  29. *
  30. * @since 1.5
  31. */
  32. public static function getContactRoute($id, $catid, $language = 0)
  33. {
  34. $needles = array(
  35. 'contact' => array((int) $id)
  36. );
  37. // Create the link
  38. $link = 'index.php?option=com_contact&view=contact&id=' . $id;
  39. if ($catid > 1)
  40. {
  41. $categories = JCategories::getInstance('Contact');
  42. $category = $categories->get($catid);
  43. if ($category)
  44. {
  45. $needles['category'] = array_reverse($category->getPath());
  46. $needles['categories'] = $needles['category'];
  47. $link .= '&catid=' . $catid;
  48. }
  49. }
  50. if ($language && $language != "*" && JLanguageMultilang::isEnabled())
  51. {
  52. $link .= '&lang=' . $language;
  53. $needles['language'] = $language;
  54. }
  55. if ($item = self::_findItem($needles))
  56. {
  57. $link .= '&Itemid=' . $item;
  58. }
  59. return $link;
  60. }
  61. /**
  62. * Get the URL route for a contact category from a contact category ID and language
  63. *
  64. * @param mixed $catid The id of the contact's category either an integer id or a instance of JCategoryNode
  65. * @param mixed $language The id of the language being used.
  66. *
  67. * @return string The link to the contact
  68. *
  69. * @since 1.5
  70. */
  71. public static function getCategoryRoute($catid, $language = 0)
  72. {
  73. if ($catid instanceof JCategoryNode)
  74. {
  75. $id = $catid->id;
  76. $category = $catid;
  77. }
  78. else
  79. {
  80. $id = (int) $catid;
  81. $category = JCategories::getInstance('Contact')->get($id);
  82. }
  83. if ($id < 1 || !($category instanceof JCategoryNode))
  84. {
  85. $link = '';
  86. }
  87. else
  88. {
  89. $needles = array();
  90. // Create the link
  91. $link = 'index.php?option=com_contact&view=category&id=' . $id;
  92. $catids = array_reverse($category->getPath());
  93. $needles['category'] = $catids;
  94. $needles['categories'] = $catids;
  95. if ($language && $language != "*" && JLanguageMultilang::isEnabled())
  96. {
  97. $link .= '&lang=' . $language;
  98. $needles['language'] = $language;
  99. }
  100. if ($item = self::_findItem($needles))
  101. {
  102. $link .= '&Itemid=' . $item;
  103. }
  104. }
  105. return $link;
  106. }
  107. /**
  108. * Find an item ID.
  109. *
  110. * @param array $needles An array of language codes.
  111. *
  112. * @return mixed The ID found or null otherwise.
  113. *
  114. * @since 1.6
  115. */
  116. protected static function _findItem($needles = null)
  117. {
  118. $app = JFactory::getApplication();
  119. $menus = $app->getMenu('site');
  120. $language = isset($needles['language']) ? $needles['language'] : '*';
  121. // Prepare the reverse lookup array.
  122. if (!isset(self::$lookup[$language]))
  123. {
  124. self::$lookup[$language] = array();
  125. $component = JComponentHelper::getComponent('com_contact');
  126. $attributes = array('component_id');
  127. $values = array($component->id);
  128. if ($language != '*')
  129. {
  130. $attributes[] = 'language';
  131. $values[] = array($needles['language'], '*');
  132. }
  133. $items = $menus->getItems($attributes, $values);
  134. foreach ($items as $item)
  135. {
  136. if (isset($item->query) && isset($item->query['view']))
  137. {
  138. $view = $item->query['view'];
  139. if (!isset(self::$lookup[$language][$view]))
  140. {
  141. self::$lookup[$language][$view] = array();
  142. }
  143. if (isset($item->query['id']))
  144. {
  145. /**
  146. * Here it will become a bit tricky
  147. * language != * can override existing entries
  148. * language == * cannot override existing entries
  149. */
  150. if (!isset(self::$lookup[$language][$view][$item->query['id']]) || $item->language != '*')
  151. {
  152. self::$lookup[$language][$view][$item->query['id']] = $item->id;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. if ($needles)
  159. {
  160. foreach ($needles as $view => $ids)
  161. {
  162. if (isset(self::$lookup[$language][$view]))
  163. {
  164. foreach ($ids as $id)
  165. {
  166. if (isset(self::$lookup[$language][$view][(int) $id]))
  167. {
  168. return self::$lookup[$language][$view][(int) $id];
  169. }
  170. }
  171. }
  172. }
  173. }
  174. // Check if the active menuitem matches the requested language
  175. $active = $menus->getActive();
  176. if ($active && ($language == '*' || in_array($active->language, array('*', $language)) || !JLanguageMultilang::isEnabled()))
  177. {
  178. return $active->id;
  179. }
  180. // If not found, return language specific home link
  181. $default = $menus->getDefault($language);
  182. return !empty($default->id) ? $default->id : null;
  183. }
  184. }