[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Joomla! Content Management System 5 * 6 * @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org> 7 * @license GNU General Public License version 2 or later; see LICENSE.txt 8 */ 9 10 namespace Joomla\CMS\Helper; 11 12 use Joomla\CMS\Categories\Categories; 13 use Joomla\CMS\Categories\CategoryNode; 14 use Joomla\CMS\Component\ComponentHelper; 15 use Joomla\CMS\Factory; 16 use Joomla\CMS\Language\Multilanguage; 17 18 // phpcs:disable PSR1.Files.SideEffects 19 \defined('JPATH_PLATFORM') or die; 20 // phpcs:enable PSR1.Files.SideEffects 21 22 /** 23 * Route Helper 24 * 25 * A class providing basic routing for urls that are for content types found in 26 * the #__content_types table and rows found in the #__ucm_content table. 27 * 28 * @since 3.1 29 */ 30 class RouteHelper 31 { 32 /** 33 * @var array Holds the reverse lookup 34 * @since 3.1 35 */ 36 protected static $lookup; 37 38 /** 39 * @var string Option for the extension (such as com_content) 40 * @since 3.1 41 */ 42 protected $extension; 43 44 /** 45 * @var string Value of the primary key in the content type table 46 * @since 3.1 47 */ 48 protected $id; 49 50 /** 51 * @var string Name of the view for the url 52 * @since 3.1 53 */ 54 protected $view; 55 56 /** 57 * A method to get the route for a specific item 58 * 59 * @param integer $id Value of the primary key for the item in its content table 60 * @param string $typealias The type_alias for the item being routed. Of the form extension.view. 61 * @param string $link The link to be routed 62 * @param string $language The language of the content for multilingual sites 63 * @param integer $catid Optional category id 64 * 65 * @return string The route of the item 66 * 67 * @since 3.1 68 */ 69 public function getRoute($id, $typealias, $link = '', $language = null, $catid = null) 70 { 71 $typeExploded = explode('.', $typealias); 72 73 if (isset($typeExploded[1])) { 74 $this->view = $typeExploded[1]; 75 $this->extension = $typeExploded[0]; 76 } else { 77 $this->view = Factory::getApplication()->input->getString('view'); 78 $this->extension = Factory::getApplication()->input->getCmd('option'); 79 } 80 81 $name = ucfirst(substr_replace($this->extension, '', 0, 4)); 82 83 $needles = array(); 84 85 if (isset($this->view)) { 86 $needles[$this->view] = array((int) $id); 87 } 88 89 if (empty($link)) { 90 // Create the link 91 $link = 'index.php?option=' . $this->extension . '&view=' . $this->view . '&id=' . $id; 92 } 93 94 if ($catid > 1) { 95 $categories = Categories::getInstance($name); 96 97 if ($categories) { 98 $category = $categories->get((int) $catid); 99 100 if ($category) { 101 $needles['category'] = array_reverse($category->getPath()); 102 $needles['categories'] = $needles['category']; 103 $link .= '&catid=' . $catid; 104 } 105 } 106 } 107 108 // Deal with languages only if needed 109 if (!empty($language) && $language !== '*' && Multilanguage::isEnabled()) { 110 $link .= '&lang=' . $language; 111 $needles['language'] = $language; 112 } 113 114 if ($item = $this->findItem($needles)) { 115 $link .= '&Itemid=' . $item; 116 } 117 118 return $link; 119 } 120 121 /** 122 * Method to find the item in the menu structure 123 * 124 * @param array $needles Array of lookup values 125 * 126 * @return mixed 127 * 128 * @since 3.1 129 */ 130 protected function findItem($needles = array()) 131 { 132 $app = Factory::getApplication(); 133 $menus = $app->getMenu('site'); 134 $language = $needles['language'] ?? '*'; 135 136 // $this->extension may not be set if coming from a static method, check it 137 if ($this->extension === null) { 138 $this->extension = $app->input->getCmd('option'); 139 } 140 141 // Prepare the reverse lookup array. 142 if (!isset(static::$lookup[$language])) { 143 static::$lookup[$language] = array(); 144 145 $component = ComponentHelper::getComponent($this->extension); 146 147 $attributes = array('component_id'); 148 $values = array($component->id); 149 150 if ($language !== '*') { 151 $attributes[] = 'language'; 152 $values[] = array($needles['language'], '*'); 153 } 154 155 $items = $menus->getItems($attributes, $values); 156 157 foreach ($items as $item) { 158 if (isset($item->query) && isset($item->query['view'])) { 159 $view = $item->query['view']; 160 161 if (!isset(static::$lookup[$language][$view])) { 162 static::$lookup[$language][$view] = array(); 163 } 164 165 if (isset($item->query['id'])) { 166 if (\is_array($item->query['id'])) { 167 $item->query['id'] = $item->query['id'][0]; 168 } 169 170 /* 171 * Here it will become a bit tricky 172 * $language != * can override existing entries 173 * $language == * cannot override existing entries 174 */ 175 if ($item->language !== '*' || !isset(static::$lookup[$language][$view][$item->query['id']])) { 176 static::$lookup[$language][$view][$item->query['id']] = $item->id; 177 } 178 } 179 } 180 } 181 } 182 183 if ($needles) { 184 foreach ($needles as $view => $ids) { 185 if (isset(static::$lookup[$language][$view])) { 186 foreach ($ids as $id) { 187 if (isset(static::$lookup[$language][$view][(int) $id])) { 188 return static::$lookup[$language][$view][(int) $id]; 189 } 190 } 191 } 192 } 193 } 194 195 $active = $menus->getActive(); 196 197 if ($active && $active->component === $this->extension && ($active->language === '*' || !Multilanguage::isEnabled())) { 198 return $active->id; 199 } 200 201 // If not found, return language specific home link 202 $default = $menus->getDefault($language); 203 204 return !empty($default->id) ? $default->id : null; 205 } 206 207 /** 208 * Fetches the category route 209 * 210 * @param mixed $catid Category ID or CategoryNode instance 211 * @param mixed $language Language code 212 * @param string $extension Extension to lookup 213 * 214 * @return string 215 * 216 * @since 3.2 217 * 218 * @throws \InvalidArgumentException 219 */ 220 public static function getCategoryRoute($catid, $language = 0, $extension = '') 221 { 222 // Note: $extension is required but has to be an optional argument in the function call due to argument order 223 if (empty($extension)) { 224 throw new \InvalidArgumentException(sprintf('$extension is a required argument in %s()', __METHOD__)); 225 } 226 227 if ($catid instanceof CategoryNode) { 228 $id = $catid->id; 229 $category = $catid; 230 } else { 231 $extensionName = ucfirst(substr($extension, 4)); 232 $id = (int) $catid; 233 $category = Categories::getInstance($extensionName)->get($id); 234 } 235 236 if ($id < 1) { 237 $link = ''; 238 } else { 239 $link = 'index.php?option=' . $extension . '&view=category&id=' . $id; 240 241 $needles = array( 242 'category' => array($id), 243 ); 244 245 if ($language && $language !== '*' && Multilanguage::isEnabled()) { 246 $link .= '&lang=' . $language; 247 $needles['language'] = $language; 248 } 249 250 // Create the link 251 if ($category) { 252 $catids = array_reverse($category->getPath()); 253 $needles['category'] = $catids; 254 $needles['categories'] = $catids; 255 } 256 257 if ($item = static::lookupItem($needles)) { 258 $link .= '&Itemid=' . $item; 259 } 260 } 261 262 return $link; 263 } 264 265 /** 266 * Static alias to findItem() used to find the item in the menu structure 267 * 268 * @param array $needles Array of lookup values 269 * 270 * @return mixed 271 * 272 * @since 3.2 273 */ 274 protected static function lookupItem($needles = array()) 275 { 276 $instance = new static(); 277 278 return $instance->findItem($needles); 279 } 280 }
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 |