[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/components/com_tags/src/Helper/ -> RouteHelper.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Site
   5   * @subpackage  com_tags
   6   *
   7   * @copyright   (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9   */
  10  
  11  namespace Joomla\Component\Tags\Site\Helper;
  12  
  13  use Exception;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Helper\RouteHelper as CMSRouteHelper;
  16  use Joomla\CMS\Menu\AbstractMenu;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Tags Component Route Helper.
  24   *
  25   * @since  3.1
  26   */
  27  class RouteHelper extends CMSRouteHelper
  28  {
  29      /**
  30       * Lookup-table for menu items
  31       *
  32       * @var    array
  33       */
  34      protected static $lookup;
  35  
  36      /**
  37       * Tries to load the router for the component and calls it. Otherwise uses getTagRoute.
  38       *
  39       * @param   integer  $contentItemId     Component item id
  40       * @param   string   $contentItemAlias  Component item alias
  41       * @param   integer  $contentCatId      Component item category id
  42       * @param   string   $language          Component item language
  43       * @param   string   $typeAlias         Component type alias
  44       * @param   string   $routerName        Component router
  45       *
  46       * @return  string  URL link to pass to the router
  47       *
  48       * @since   3.1
  49       */
  50      public static function getItemRoute($contentItemId, $contentItemAlias, $contentCatId, $language, $typeAlias, $routerName)
  51      {
  52          $link = '';
  53          $explodedAlias = explode('.', $typeAlias);
  54          $explodedRouter = explode('::', $routerName);
  55  
  56          if (file_exists($routerFile = JPATH_BASE . '/components/' . $explodedAlias[0] . '/helpers/route.php')) {
  57              \JLoader::register($explodedRouter[0], $routerFile);
  58              $routerClass = $explodedRouter[0];
  59              $routerMethod = $explodedRouter[1];
  60  
  61              if (class_exists($routerClass) && method_exists($routerClass, $routerMethod)) {
  62                  if ($routerMethod === 'getCategoryRoute') {
  63                      $link = $routerClass::$routerMethod($contentItemId, $language);
  64                  } else {
  65                      $link = $routerClass::$routerMethod($contentItemId . ':' . $contentItemAlias, $contentCatId, $language);
  66                  }
  67              }
  68          }
  69  
  70          if ($link === '') {
  71              // Create a fallback link in case we can't find the component router
  72              $router = new CMSRouteHelper();
  73              $link = $router->getRoute($contentItemId, $typeAlias, $link, $language, $contentCatId);
  74          }
  75  
  76          return $link;
  77      }
  78  
  79      /**
  80       * Tries to load the router for the component and calls it. Otherwise calls getRoute.
  81       *
  82       * @param   integer  $id        The ID of the tag
  83       *
  84       * @return  string  URL link to pass to the router
  85       *
  86       * @since      3.1
  87       * @throws     Exception
  88       * @deprecated 5.0.0 Use getComponentTagRoute() instead
  89       */
  90      public static function getTagRoute($id)
  91      {
  92          @trigger_error('This function is replaced by the getComponentTagRoute()', E_USER_DEPRECATED);
  93  
  94          return self::getComponentTagRoute($id);
  95      }
  96  
  97      /**
  98       * Tries to load the router for the component and calls it. Otherwise calls getRoute.
  99       *
 100       * @param   string   $id        The ID of the tag in the format TAG_ID:TAG_ALIAS
 101       * @param   string   $language  The language of the tag
 102       *
 103       * @return  string  URL link to pass to the router
 104       *
 105       * @since   4.2.0
 106       * @throws  Exception
 107       */
 108      public static function getComponentTagRoute(string $id, string $language = '*'): string
 109      {
 110          $needles = [
 111              'tag'      => [(int) $id],
 112              'language' => $language,
 113          ];
 114  
 115          if ($id < 1) {
 116              $link = '';
 117          } else {
 118              $link = 'index.php?option=com_tags&view=tag&id=' . $id;
 119  
 120              if ($item = self::_findItem($needles)) {
 121                  $link .= '&Itemid=' . $item;
 122              } else {
 123                  $needles = [
 124                      'tags'     => [1, 0],
 125                      'language' => $language,
 126                  ];
 127  
 128                  if ($item = self::_findItem($needles)) {
 129                      $link .= '&Itemid=' . $item;
 130                  }
 131              }
 132          }
 133  
 134          return $link;
 135      }
 136  
 137      /**
 138       * Tries to load the router for the tags view.
 139       *
 140       * @return  string  URL link to pass to the router
 141       *
 142       * @since      3.7
 143       * @throws     Exception
 144       * @deprecated 5.0.0
 145       */
 146      public static function getTagsRoute()
 147      {
 148          @trigger_error('This function is replaced by the getComponentTagsRoute()', E_USER_DEPRECATED);
 149  
 150          return self::getComponentTagsRoute();
 151      }
 152  
 153      /**
 154       * Tries to load the router for the tags view.
 155       *
 156       * @param   string  $language  The language of the tag
 157       *
 158       * @return  string  URL link to pass to the router
 159       *
 160       * @since   4.2.0
 161       * @throws  Exception
 162       */
 163      public static function getComponentTagsRoute(string $language = '*'): string
 164      {
 165          $needles = [
 166              'tags'     => [0],
 167              'language' => $language,
 168          ];
 169  
 170          $link = 'index.php?option=com_tags&view=tags';
 171  
 172          if ($item = self::_findItem($needles)) {
 173              $link .= '&Itemid=' . $item;
 174          }
 175  
 176          return $link;
 177      }
 178  
 179      /**
 180       * Find Item static function
 181       *
 182       * @param   array  $needles  Array used to get the language value
 183       *
 184       * @return null
 185       *
 186       * @throws Exception
 187       */
 188      protected static function _findItem($needles = null)
 189      {
 190          $menus    = AbstractMenu::getInstance('site');
 191          $language = $needles['language'] ?? '*';
 192  
 193          // Prepare the reverse lookup array.
 194          if (self::$lookup === null) {
 195              self::$lookup = array();
 196  
 197              $component = ComponentHelper::getComponent('com_tags');
 198              $items     = $menus->getItems('component_id', $component->id);
 199  
 200              if ($items) {
 201                  foreach ($items as $item) {
 202                      if (isset($item->query, $item->query['view'])) {
 203                          $lang = ($item->language != '' ? $item->language : '*');
 204  
 205                          if (!isset(self::$lookup[$lang])) {
 206                              self::$lookup[$lang] = array();
 207                          }
 208  
 209                          $view = $item->query['view'];
 210  
 211                          if (!isset(self::$lookup[$lang][$view])) {
 212                              self::$lookup[$lang][$view] = array();
 213                          }
 214  
 215                          // Only match menu items that list one tag
 216                          if (isset($item->query['id']) && is_array($item->query['id'])) {
 217                              foreach ($item->query['id'] as $position => $tagId) {
 218                                  if (!isset(self::$lookup[$lang][$view][$item->query['id'][$position]]) || count($item->query['id']) == 1) {
 219                                      self::$lookup[$lang][$view][$item->query['id'][$position]] = $item->id;
 220                                  }
 221                              }
 222                          } elseif ($view == 'tags') {
 223                              self::$lookup[$lang]['tags'][] = $item->id;
 224                          }
 225                      }
 226                  }
 227              }
 228          }
 229  
 230          if ($needles) {
 231              foreach ($needles as $view => $ids) {
 232                  if (isset(self::$lookup[$language][$view])) {
 233                      foreach ($ids as $id) {
 234                          if (isset(self::$lookup[$language][$view][(int) $id])) {
 235                              return self::$lookup[$language][$view][(int) $id];
 236                          }
 237                      }
 238                  }
 239              }
 240          } else {
 241              $active = $menus->getActive();
 242  
 243              if ($active) {
 244                  return $active->id;
 245              }
 246          }
 247  
 248          return null;
 249      }
 250  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer