/components/com_tags/helpers/route.php

https://bitbucket.org/pastor399/newcastleunifc · PHP · 178 lines · 119 code · 14 blank · 45 comment · 28 complexity · 6be0f8ecedc8d39ead99f7b2bb71ade2 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_tags
  5. *
  6. * @copyright Copyright (C) 2005 - 2013 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. * Tags Component Route Helper
  12. *
  13. * @static
  14. * @package Joomla.Site
  15. * @subpackage com_tags
  16. * @since 3.1
  17. */
  18. class TagsHelperRoute extends JHelperRoute
  19. {
  20. protected static $lookup;
  21. /**
  22. * Tries to load the router for the component and calls it. Otherwise uses getTagRoute.
  23. *
  24. * @param integer $contentItemId Component item id
  25. * @param string $contentItemAlias Component item alias
  26. * @param integer $contentCatId Component item category id
  27. * @param string $language Component item language
  28. * @param string $typeAlias Component type alias
  29. * @param string $routerName Component router
  30. *
  31. * @return string URL link to pass to JRoute
  32. *
  33. * @since 3.1
  34. */
  35. public static function getItemRoute($contentItemId, $contentItemAlias, $contentCatId, $language, $typeAlias, $routerName)
  36. {
  37. $link = '';
  38. $explodedAlias = explode('.', $typeAlias);
  39. $explodedRouter = explode('::', $routerName);
  40. if (file_exists($routerFile = JPATH_BASE . '/components/' . $explodedAlias[0] . '/helpers/route.php'))
  41. {
  42. JLoader::register($explodedRouter[0], $routerFile);
  43. $routerClass = $explodedRouter[0];
  44. $routerMethod = $explodedRouter[1];
  45. if (class_exists($routerClass) && method_exists($routerClass, $routerMethod))
  46. {
  47. if ($routerMethod == 'getCategoryRoute')
  48. {
  49. $link = $routerClass::$routerMethod($contentItemId, $language);
  50. }
  51. else
  52. {
  53. $link = $routerClass::$routerMethod($contentItemId . ':' . $contentItemAlias, $contentCatId, $language);
  54. }
  55. }
  56. }
  57. if ($link == '')
  58. {
  59. // create a fallback link in case we can't find the component router
  60. $router = new JHelperRoute;
  61. $link = $router->getRoute($contentItemId, $typeAlias, $link, $language, $contentCatId);
  62. }
  63. return $link;
  64. }
  65. /**
  66. * Tries to load the router for the component and calls it. Otherwise calls getRoute.
  67. *
  68. * @param integer $id The ID of the tag
  69. *
  70. * @return string URL link to pass to JRoute
  71. *
  72. * @since 3.1
  73. */
  74. public static function getTagRoute($id)
  75. {
  76. $needles = array(
  77. 'tag' => array((int) $id)
  78. );
  79. if ($id < 1)
  80. {
  81. $link = '';
  82. }
  83. else
  84. {
  85. if (!empty($needles) && $item = self::_findItem($needles))
  86. {
  87. $link = 'index.php?Itemid=' . $item;
  88. }
  89. else
  90. {
  91. // Create the link
  92. $link = 'index.php?option=com_tags&view=tag&id=' . $id;
  93. }
  94. }
  95. return $link;
  96. }
  97. protected static function _findItem($needles = null)
  98. {
  99. $app = JFactory::getApplication();
  100. $menus = $app->getMenu('site');
  101. $language = isset($needles['language']) ? $needles['language'] : '*';
  102. // Prepare the reverse lookup array.
  103. if (self::$lookup === null)
  104. {
  105. self::$lookup = array();
  106. $component = JComponentHelper::getComponent('com_tags');
  107. $items = $menus->getItems('component_id', $component->id);
  108. if ($items) {
  109. foreach ($items as $item)
  110. {
  111. if (isset($item->query) && isset($item->query['view']))
  112. {
  113. $view = $item->query['view'];
  114. if (!isset(self::$lookup[$view]))
  115. {
  116. self::$lookup[$view] = array();
  117. }
  118. // Only match menu items that list one tag
  119. if (isset($item->query['id'][0]) && count($item->query['id']) == 1)
  120. {
  121. // Here it will become a bit tricky
  122. // language != * can override existing entries
  123. // language == * cannot override existing entries
  124. if (!isset(self::$lookup[$language][$view][$item->query['id'][0]]) || $item->language != '*')
  125. {
  126. self::$lookup[$language][$view][$item->query['id'][0]] = $item->id;
  127. }
  128. self::$lookup[$view][$item->query['id'][0]] = $item->id;
  129. }
  130. if (isset($item->query["tag_list_language_filter"]) && $item->query["tag_list_language_filter"] != '')
  131. {
  132. $language = $item->query["tag_list_language_filter"];
  133. }
  134. }
  135. }
  136. }
  137. }
  138. if ($needles)
  139. {
  140. foreach ($needles as $view => $ids)
  141. {
  142. if (isset(self::$lookup[$view]))
  143. {
  144. foreach($ids as $id)
  145. {
  146. if (isset(self::$lookup[$view][(int) $id]))
  147. {
  148. return self::$lookup[$view][(int) $id];
  149. }
  150. }
  151. }
  152. }
  153. }
  154. else
  155. {
  156. $active = $menus->getActive();
  157. if ($active)
  158. {
  159. return $active->id;
  160. }
  161. }
  162. return null;
  163. }
  164. }