/components/com_weblinks/helpers/route.php

https://github.com/mathc/joomla-cms · PHP · 180 lines · 127 code · 25 blank · 28 comment · 29 complexity · a9cfb8e4e2b667ee564e98acdca8bf76 MD5 · raw file

  1. <?php
  2. /**
  3. * @version $Id$
  4. * @package Joomla.Site
  5. * @subpackage com_weblinks
  6. * @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE.txt
  8. */
  9. // no direct access
  10. defined('_JEXEC') or die;
  11. // Component Helper
  12. jimport('joomla.application.component.helper');
  13. jimport('joomla.application.categories');
  14. /**
  15. * Weblinks Component Route Helper
  16. *
  17. * @static
  18. * @package Joomla.Site
  19. * @subpackage com_weblinks
  20. * @since 1.5
  21. */
  22. abstract class WeblinksHelperRoute
  23. {
  24. protected static $lookup;
  25. /**
  26. * @param int The route of the weblink
  27. */
  28. public static function getWeblinkRoute($id, $catid)
  29. {
  30. $needles = array(
  31. 'weblink' => array((int) $id)
  32. );
  33. //Create the link
  34. $link = 'index.php?option=com_weblinks&view=weblink&id='. $id;
  35. if ($catid > 1) {
  36. $categories = JCategories::getInstance('Weblinks');
  37. $category = $categories->get($catid);
  38. if($category) {
  39. $needles['category'] = array_reverse($category->getPath());
  40. $needles['categories'] = $needles['category'];
  41. $link .= '&catid='.$catid;
  42. }
  43. }
  44. if ($item = self::_findItem($needles)) {
  45. $link .= '&Itemid='.$item;
  46. }
  47. else if ($item = self::_findItem()) {
  48. $link .= '&Itemid='.$item;
  49. }
  50. return $link;
  51. }
  52. /**
  53. * @param int $id The id of the weblink.
  54. * @param string $return The return page variable.
  55. */
  56. public static function getFormRoute($id, $return = null)
  57. {
  58. // Create the link.
  59. if ($id) {
  60. $link = 'index.php?option=com_weblinks&task=weblink.edit&w_id='. $id;
  61. }
  62. else {
  63. $link = 'index.php?option=com_weblinks&task=weblink.add&w_id=0';
  64. }
  65. if ($return) {
  66. $link .= '&return='.$return;
  67. }
  68. return $link;
  69. }
  70. public static function getCategoryRoute($catid)
  71. {
  72. if ($catid instanceof JCategoryNode) {
  73. $id = $catid->id;
  74. $category = $catid;
  75. }
  76. else {
  77. $id = (int) $catid;
  78. $category = JCategories::getInstance('Weblinks')->get($id);
  79. }
  80. if ($id < 1) {
  81. $link = '';
  82. }
  83. else {
  84. $needles = array(
  85. 'category' => array($id)
  86. );
  87. if ($item = self::_findItem($needles)) {
  88. $link = 'index.php?Itemid='.$item;
  89. }
  90. else {
  91. //Create the link
  92. $link = 'index.php?option=com_weblinks&view=category&id='.$id;
  93. if ($category) {
  94. $catids = array_reverse($category->getPath());
  95. $needles = array(
  96. 'category' => $catids,
  97. 'categories' => $catids
  98. );
  99. if ($item = self::_findItem($needles)) {
  100. $link .= '&Itemid='.$item;
  101. }
  102. else if ($item = self::_findItem()) {
  103. $link .= '&Itemid='.$item;
  104. }
  105. }
  106. }
  107. }
  108. return $link;
  109. }
  110. protected static function _findItem($needles = null)
  111. {
  112. $app = JFactory::getApplication();
  113. $menus = $app->getMenu('site');
  114. // Prepare the reverse lookup array.
  115. if (self::$lookup === null) {
  116. self::$lookup = array();
  117. $component = JComponentHelper::getComponent('com_weblinks');
  118. $items = $menus->getItems('component_id', $component->id);
  119. if ($items) {
  120. foreach ($items as $item)
  121. {
  122. if (isset($item->query) && isset($item->query['view'])) {
  123. $view = $item->query['view'];
  124. if (!isset(self::$lookup[$view])) {
  125. self::$lookup[$view] = array();
  126. }
  127. if (isset($item->query['id'])) {
  128. self::$lookup[$view][$item->query['id']] = $item->id;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. if ($needles) {
  135. foreach ($needles as $view => $ids)
  136. {
  137. if (isset(self::$lookup[$view])) {
  138. foreach($ids as $id)
  139. {
  140. if (isset(self::$lookup[$view][(int)$id])) {
  141. return self::$lookup[$view][(int)$id];
  142. }
  143. }
  144. }
  145. }
  146. }
  147. else {
  148. $active = $menus->getActive();
  149. if ($active) {
  150. return $active->id;
  151. }
  152. }
  153. return null;
  154. }
  155. }