/components/com_weblinks/router.php

https://github.com/gpongelli/joomla-cms · PHP · 198 lines · 134 code · 34 blank · 30 comment · 47 complexity · 80f8c4f10920ae7af022653ab4d56f31 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Site
  4. * @subpackage com_weblinks
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 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. * Build the route for the com_weblinks component
  12. *
  13. * @param array An array of URL arguments
  14. *
  15. * @return array The URL arguments to use to assemble the subsequent URL.
  16. */
  17. function WeblinksBuildRoute(&$query)
  18. {
  19. $segments = array();
  20. // get a menu item based on Itemid or currently active
  21. $app = JFactory::getApplication();
  22. $menu = $app->getMenu();
  23. $params = JComponentHelper::getParams('com_weblinks');
  24. $advanced = $params->get('sef_advanced_link', 0);
  25. // we need a menu item. Either the one specified in the query, or the current active one if none specified
  26. if (empty($query['Itemid'])) {
  27. $menuItem = $menu->getActive();
  28. }
  29. else {
  30. $menuItem = $menu->getItem($query['Itemid']);
  31. }
  32. $mView = (empty($menuItem->query['view'])) ? null : $menuItem->query['view'];
  33. $mId = (empty($menuItem->query['id'])) ? null : $menuItem->query['id'];
  34. if (isset($query['view'])) {
  35. $view = $query['view'];
  36. if (empty($query['Itemid'])) {
  37. $segments[] = $query['view'];
  38. }
  39. // We need to keep the view for forms since they never have their own menu item
  40. if ($view != 'form') {
  41. unset($query['view']);
  42. }
  43. }
  44. // are we dealing with an weblink that is attached to a menu item?
  45. if (isset($query['view']) && ($mView == $query['view']) and (isset($query['id'])) and ($mId == (int) $query['id'])) {
  46. unset($query['view']);
  47. unset($query['catid']);
  48. unset($query['id']);
  49. return $segments;
  50. }
  51. if (isset($view) and ($view == 'category' or $view == 'weblink' )) {
  52. if ($mId != (int) $query['id'] || $mView != $view) {
  53. if ($view == 'weblink' && isset($query['catid'])) {
  54. $catid = $query['catid'];
  55. }
  56. elseif (isset($query['id'])) {
  57. $catid = $query['id'];
  58. }
  59. $menuCatid = $mId;
  60. $categories = JCategories::getInstance('Weblinks');
  61. $category = $categories->get($catid);
  62. if ($category) {
  63. //TODO Throw error that the category either not exists or is unpublished
  64. $path = $category->getPath();
  65. $path = array_reverse($path);
  66. $array = array();
  67. foreach($path as $id)
  68. {
  69. if ((int) $id == (int) $menuCatid) {
  70. break;
  71. }
  72. if ($advanced) {
  73. list($tmp, $id) = explode(':', $id, 2);
  74. }
  75. $array[] = $id;
  76. }
  77. $segments = array_merge($segments, array_reverse($array));
  78. }
  79. if ($view == 'weblink') {
  80. if ($advanced) {
  81. list($tmp, $id) = explode(':', $query['id'], 2);
  82. }
  83. else {
  84. $id = $query['id'];
  85. }
  86. $segments[] = $id;
  87. }
  88. }
  89. unset($query['id']);
  90. unset($query['catid']);
  91. }
  92. if (isset($query['layout'])) {
  93. if (!empty($query['Itemid']) && isset($menuItem->query['layout'])) {
  94. if ($query['layout'] == $menuItem->query['layout']) {
  95. unset($query['layout']);
  96. }
  97. }
  98. else {
  99. if ($query['layout'] == 'default') {
  100. unset($query['layout']);
  101. }
  102. }
  103. };
  104. return $segments;
  105. }
  106. /**
  107. * Parse the segments of a URL.
  108. *
  109. * @param array The segments of the URL to parse.
  110. *
  111. * @return array The URL attributes to be used by the application.
  112. */
  113. function WeblinksParseRoute($segments)
  114. {
  115. $vars = array();
  116. //Get the active menu item.
  117. $app = JFactory::getApplication();
  118. $menu = $app->getMenu();
  119. $item = $menu->getActive();
  120. $params = JComponentHelper::getParams('com_weblinks');
  121. $advanced = $params->get('sef_advanced_link', 0);
  122. // Count route segments
  123. $count = count($segments);
  124. // Standard routing for weblinks.
  125. if (!isset($item)) {
  126. $vars['view'] = $segments[0];
  127. $vars['id'] = $segments[$count - 1];
  128. return $vars;
  129. }
  130. // From the categories view, we can only jump to a category.
  131. $id = (isset($item->query['id']) && $item->query['id'] > 1) ? $item->query['id'] : 'root';
  132. $category = JCategories::getInstance('Weblinks')->get($id);
  133. $categories = $category->getChildren();
  134. $found = 0;
  135. foreach($segments as $segment)
  136. {
  137. foreach($categories as $category)
  138. {
  139. if (($category->slug == $segment) || ($advanced && $category->alias == str_replace(':', '-', $segment))) {
  140. $vars['id'] = $category->id;
  141. $vars['view'] = 'category';
  142. $categories = $category->getChildren();
  143. $found = 1;
  144. break;
  145. }
  146. }
  147. if ($found == 0) {
  148. if ($advanced) {
  149. $db = JFactory::getDBO();
  150. $query = 'SELECT id FROM #__weblinks WHERE catid = '.$vars['id'].' AND alias = '.$db->Quote(str_replace(':', '-', $segment));
  151. $db->setQuery($query);
  152. $id = $db->loadResult();
  153. }
  154. else {
  155. $id = $segment;
  156. }
  157. $vars['id'] = $id;
  158. $vars['view'] = 'weblink';
  159. break;
  160. }
  161. $found = 0;
  162. }
  163. return $vars;
  164. }