[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Site 5 * @subpackage com_tags 6 * 7 * @copyright (C) 2013 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\Service; 12 13 use Joomla\CMS\Application\SiteApplication; 14 use Joomla\CMS\Categories\CategoryFactoryInterface; 15 use Joomla\CMS\Component\Router\RouterBase; 16 use Joomla\CMS\Menu\AbstractMenu; 17 use Joomla\Database\DatabaseInterface; 18 use Joomla\Utilities\ArrayHelper; 19 20 // phpcs:disable PSR1.Files.SideEffects 21 \defined('_JEXEC') or die; 22 // phpcs:enable PSR1.Files.SideEffects 23 24 /** 25 * Routing class from com_tags 26 * 27 * @since 3.3 28 */ 29 class Router extends RouterBase 30 { 31 /** 32 * The db 33 * 34 * @var DatabaseInterface 35 * 36 * @since 4.0.0 37 */ 38 private $db; 39 40 /** 41 * Tags Component router constructor 42 * 43 * @param SiteApplication $app The application object 44 * @param AbstractMenu $menu The menu object to work with 45 * @param CategoryFactoryInterface $categoryFactory The category object 46 * @param DatabaseInterface $db The database object 47 * 48 * @since 4.0.0 49 */ 50 public function __construct(SiteApplication $app, AbstractMenu $menu, ?CategoryFactoryInterface $categoryFactory, DatabaseInterface $db) 51 { 52 $this->db = $db; 53 54 parent::__construct($app, $menu); 55 } 56 57 /** 58 * Build the route for the com_tags component 59 * 60 * @param array &$query An array of URL arguments 61 * 62 * @return array The URL arguments to use to assemble the subsequent URL. 63 * 64 * @since 3.3 65 */ 66 public function build(&$query) 67 { 68 $segments = array(); 69 70 // Get a menu item based on Itemid or currently active 71 72 // We need a menu item. Either the one specified in the query, or the current active one if none specified 73 if (empty($query['Itemid'])) { 74 $menuItem = $this->menu->getActive(); 75 } else { 76 $menuItem = $this->menu->getItem($query['Itemid']); 77 } 78 79 $mView = empty($menuItem->query['view']) ? null : $menuItem->query['view']; 80 $mId = empty($menuItem->query['id']) ? null : $menuItem->query['id']; 81 82 if (is_array($mId)) { 83 $mId = ArrayHelper::toInteger($mId); 84 } 85 86 $view = ''; 87 88 if (isset($query['view'])) { 89 $view = $query['view']; 90 91 if (empty($query['Itemid'])) { 92 $segments[] = $view; 93 } 94 95 unset($query['view']); 96 } 97 98 // Are we dealing with a tag that is attached to a menu item? 99 if ($mView == $view && isset($query['id']) && $mId == $query['id']) { 100 unset($query['id']); 101 102 return $segments; 103 } 104 105 if ($view === 'tag') { 106 $notActiveTag = is_array($mId) ? (count($mId) > 1 || $mId[0] != (int) $query['id']) : ($mId != (int) $query['id']); 107 108 if ($notActiveTag || $mView != $view) { 109 // ID in com_tags can be either an integer, a string or an array of IDs 110 $id = is_array($query['id']) ? implode(',', $query['id']) : $query['id']; 111 $segments[] = $id; 112 } 113 114 unset($query['id']); 115 } 116 117 if (isset($query['layout'])) { 118 if ( 119 (!empty($query['Itemid']) && isset($menuItem->query['layout']) 120 && $query['layout'] == $menuItem->query['layout']) 121 || $query['layout'] === 'default' 122 ) { 123 unset($query['layout']); 124 } 125 } 126 127 $total = count($segments); 128 129 for ($i = 0; $i < $total; $i++) { 130 $segments[$i] = str_replace(':', '-', $segments[$i]); 131 $position = strpos($segments[$i], '-'); 132 133 if ($position) { 134 // Remove id from segment 135 $segments[$i] = substr($segments[$i], $position + 1); 136 } 137 } 138 139 return $segments; 140 } 141 142 /** 143 * Parse the segments of a URL. 144 * 145 * @param array &$segments The segments of the URL to parse. 146 * 147 * @return array The URL attributes to be used by the application. 148 * 149 * @since 3.3 150 */ 151 public function parse(&$segments) 152 { 153 $total = count($segments); 154 $vars = array(); 155 156 for ($i = 0; $i < $total; $i++) { 157 $segments[$i] = preg_replace('/-/', ':', $segments[$i], 1); 158 } 159 160 // Get the active menu item. 161 $item = $this->menu->getActive(); 162 163 // Count route segments 164 $count = count($segments); 165 166 // Standard routing for tags. 167 if (!isset($item)) { 168 $vars['view'] = $segments[0]; 169 $vars['id'] = $this->fixSegment($segments[$count - 1]); 170 unset($segments[0]); 171 unset($segments[$count - 1]); 172 173 return $vars; 174 } 175 176 $vars['id'] = $this->fixSegment($segments[0]); 177 $vars['view'] = 'tag'; 178 unset($segments[0]); 179 180 return $vars; 181 } 182 183 /** 184 * Try to add missing id to segment 185 * 186 * @param string $segment One piece of segment of the URL to parse 187 * 188 * @return string The segment with founded id 189 * 190 * @since 3.7 191 */ 192 protected function fixSegment($segment) 193 { 194 // Try to find tag id 195 $alias = str_replace(':', '-', $segment); 196 197 $query = $this->db->getQuery(true) 198 ->select($this->db->quoteName('id')) 199 ->from($this->db->quoteName('#__tags')) 200 ->where($this->db->quoteName('alias') . ' = :alias') 201 ->bind(':alias', $alias); 202 203 $id = $this->db->setQuery($query)->loadResult(); 204 205 if ($id) { 206 $segment = "$id:$alias"; 207 } 208 209 return $segment; 210 } 211 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |