1 <?php
2 3 4 5 6 7 8
9
10 defined('JPATH_PLATFORM') or die;
11
12 13 14 15 16 17 18 19
20 class JHelperRoute
21 {
22 23 24 25
26 protected static $lookup;
27
28 29 30 31
32 protected $extension;
33
34 35 36 37
38 protected $id;
39
40 41 42 43
44 protected $view;
45
46 47 48 49 50 51 52 53 54 55 56 57 58
59 public function getRoute($id, $typealias, $link = '', $language = null, $catid = null)
60 {
61 $typeExploded = explode('.', $typealias);
62
63 if (isset($typeExploded[1]))
64 {
65 $this->view = $typeExploded[1];
66 $this->extension = $typeExploded[0];
67 }
68 else
69 {
70 $this->view = JFactory::getApplication()->input->getString('view');
71 $this->extension = JFactory::getApplication()->input->getCmd('option');
72 }
73
74 $name = ucfirst(substr_replace($this->extension, '', 0, 4));
75
76 $needles = array();
77
78 if (isset($this->view))
79 {
80 $needles[$this->view] = array((int) $id);
81 }
82
83 if (empty($link))
84 {
85
86 $link = 'index.php?option=' . $this->extension . '&view=' . $this->view . '&id=' . $id;
87 }
88
89 if ($catid > 1)
90 {
91 $categories = JCategories::getInstance($name);
92
93 if ($categories)
94 {
95 $category = $categories->get((int) $catid);
96
97 if ($category)
98 {
99 $needles['category'] = array_reverse($category->getPath());
100 $needles['categories'] = $needles['category'];
101 $link .= '&catid=' . $catid;
102 }
103 }
104 }
105
106
107 if (!empty($language) && $language !== '*' && JLanguageMultilang::isEnabled())
108 {
109 $link .= '&lang=' . $language;
110 $needles['language'] = $language;
111 }
112
113 if ($item = $this->findItem($needles))
114 {
115 $link .= '&Itemid=' . $item;
116 }
117
118 return $link;
119 }
120
121 122 123 124 125 126 127 128 129
130 protected function findItem($needles = array())
131 {
132 $app = JFactory::getApplication();
133 $menus = $app->getMenu('site');
134 $language = isset($needles['language']) ? $needles['language'] : '*';
135
136
137 if ($this->extension === null)
138 {
139 $this->extension = $app->input->getCmd('option');
140 }
141
142
143 if (!isset(static::$lookup[$language]))
144 {
145 static::$lookup[$language] = array();
146
147 $component = JComponentHelper::getComponent($this->extension);
148
149 $attributes = array('component_id');
150 $values = array($component->id);
151
152 if ($language !== '*')
153 {
154 $attributes[] = 'language';
155 $values[] = array($needles['language'], '*');
156 }
157
158 $items = $menus->getItems($attributes, $values);
159
160 foreach ($items as $item)
161 {
162 if (isset($item->query) && isset($item->query['view']))
163 {
164 $view = $item->query['view'];
165
166 if (!isset(static::$lookup[$language][$view]))
167 {
168 static::$lookup[$language][$view] = array();
169 }
170
171 if (isset($item->query['id']))
172 {
173 if (is_array($item->query['id']))
174 {
175 $item->query['id'] = $item->query['id'][0];
176 }
177
178 179 180 181 182
183 if ($item->language !== '*' || !isset(static::$lookup[$language][$view][$item->query['id']]))
184 {
185 static::$lookup[$language][$view][$item->query['id']] = $item->id;
186 }
187 }
188 }
189 }
190 }
191
192 if ($needles)
193 {
194 foreach ($needles as $view => $ids)
195 {
196 if (isset(static::$lookup[$language][$view]))
197 {
198 foreach ($ids as $id)
199 {
200 if (isset(static::$lookup[$language][$view][(int) $id]))
201 {
202 return static::$lookup[$language][$view][(int) $id];
203 }
204 }
205 }
206 }
207 }
208
209 $active = $menus->getActive();
210
211 if ($active && $active->component === $this->extension && ($active->language === '*' || !JLanguageMultilang::isEnabled()))
212 {
213 return $active->id;
214 }
215
216
217 $default = $menus->getDefault($language);
218
219 return !empty($default->id) ? $default->id : null;
220 }
221
222 223 224 225 226 227 228 229 230 231 232 233 234
235 public static function getCategoryRoute($catid, $language = 0, $extension = '')
236 {
237
238 if (empty($extension))
239 {
240 throw new InvalidArgumentException('$extension is a required argument in JHelperRoute::getCategoryRoute');
241 }
242
243 if ($catid instanceof JCategoryNode)
244 {
245 $id = $catid->id;
246 $category = $catid;
247 }
248 else
249 {
250 $extensionName = ucfirst(substr($extension, 4));
251 $id = (int) $catid;
252 $category = JCategories::getInstance($extensionName)->get($id);
253 }
254
255 if ($id < 1)
256 {
257 $link = '';
258 }
259 else
260 {
261 $link = 'index.php?option=' . $extension . '&view=category&id=' . $id;
262
263 $needles = array(
264 'category' => array($id),
265 );
266
267 if ($language && $language !== '*' && JLanguageMultilang::isEnabled())
268 {
269 $link .= '&lang=' . $language;
270 $needles['language'] = $language;
271 }
272
273
274 if ($category)
275 {
276 $catids = array_reverse($category->getPath());
277 $needles['category'] = $catids;
278 $needles['categories'] = $catids;
279
280 }
281
282 if ($item = static::lookupItem($needles))
283 {
284 $link .= '&Itemid=' . $item;
285 }
286 }
287
288 return $link;
289 }
290
291 292 293 294 295 296 297 298 299
300 protected static function lookupItem($needles = array())
301 {
302 $instance = new static;
303
304 return $instance->findItem($needles);
305 }
306 }
307