1 <?php
2 /**
3 * @package Joomla.Libraries
4 * @subpackage Menu
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE.txt
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 use Joomla\Registry\Registry;
13
14 /**
15 * Object representing a menu item
16 *
17 * @since 3.7.0
18 * @note This class will no longer extend stdClass in Joomla 4
19 */
20 class JMenuItem extends stdClass
21 {
22 /**
23 * Primary key
24 *
25 * @var integer
26 * @since 3.7.0
27 */
28 public $id;
29
30 /**
31 * The type of menu this item belongs to
32 *
33 * @var integer
34 * @since 3.7.0
35 */
36 public $menutype;
37
38 /**
39 * The display title of the menu item
40 *
41 * @var string
42 * @since 3.7.0
43 */
44 public $title;
45
46 /**
47 * The SEF alias of the menu item
48 *
49 * @var string
50 * @since 3.7.0
51 */
52 public $alias;
53
54 /**
55 * A note associated with the menu item
56 *
57 * @var string
58 * @since 3.7.0
59 */
60 public $note;
61
62 /**
63 * The computed path of the menu item based on the alias field, this is populated from the `path` field in the `#__menu` table
64 *
65 * @var string
66 * @since 3.7.0
67 */
68 public $route;
69
70 /**
71 * The actual link the menu item refers to
72 *
73 * @var string
74 * @since 3.7.0
75 */
76 public $link;
77
78 /**
79 * The type of link
80 *
81 * @var string
82 * @since 3.7.0
83 */
84 public $type;
85
86 /**
87 * The relative level in the tree
88 *
89 * @var integer
90 * @since 3.7.0
91 */
92 public $level;
93
94 /**
95 * The assigned language for this item
96 *
97 * @var string
98 * @since 3.7.0
99 */
100 public $language;
101
102 /**
103 * The click behaviour of the link
104 *
105 * @var string
106 * @since 3.7.0
107 */
108 public $browserNav;
109
110 /**
111 * The access level required to view the menu item
112 *
113 * @var integer
114 * @since 3.7.0
115 */
116 public $access;
117
118 /**
119 * The menu item parameters
120 *
121 * @var string|Registry
122 * @since 3.7.0
123 * @note This field is protected to require reading this field to proxy through the getter to convert the params to a Registry instance
124 */
125 protected $params;
126
127 /**
128 * Indicates if this menu item is the home or default page
129 *
130 * @var integer
131 * @since 3.7.0
132 */
133 public $home;
134
135 /**
136 * The image of the menu item
137 *
138 * @var string
139 * @since 3.7.0
140 */
141 public $img;
142
143 /**
144 * The optional template style applied to this menu item
145 *
146 * @var integer
147 * @since 3.7.0
148 */
149 public $template_style_id;
150
151 /**
152 * The extension ID of the component this menu item is for
153 *
154 * @var integer
155 * @since 3.7.0
156 */
157 public $component_id;
158
159 /**
160 * The parent menu item in the menu tree
161 *
162 * @var integer
163 * @since 3.7.0
164 */
165 public $parent_id;
166
167 /**
168 * The name of the component this menu item is for
169 *
170 * @var string
171 * @since 3.7.0
172 */
173 public $component;
174
175 /**
176 * The tree of parent menu items
177 *
178 * @var array
179 * @since 3.7.0
180 */
181 public $tree = array();
182
183 /**
184 * An array of the query string values for this item
185 *
186 * @var array
187 * @since 3.7.0
188 */
189 public $query = array();
190
191 /**
192 * Class constructor
193 *
194 * @param array $data The menu item data to load
195 *
196 * @since 3.7.0
197 */
198 public function __construct($data = array())
199 {
200 foreach ((array) $data as $key => $value)
201 {
202 $this->$key = $value;
203 }
204 }
205
206 /**
207 * Method to get certain otherwise inaccessible properties from the form field object.
208 *
209 * @param string $name The property name for which to the the value.
210 *
211 * @return mixed The property value or null.
212 *
213 * @since 3.7.0
214 * @deprecated 4.0 Access the item parameters through the `getParams()` method
215 */
216 public function __get($name)
217 {
218 if ($name === 'params')
219 {
220 return $this->getParams();
221 }
222
223 return $this->get($name);
224 }
225
226 /**
227 * Method to set certain otherwise inaccessible properties of the form field object.
228 *
229 * @param string $name The property name for which to the the value.
230 * @param mixed $value The value of the property.
231 *
232 * @return void
233 *
234 * @since 3.7.0
235 * @deprecated 4.0 Set the item parameters through the `setParams()` method
236 */
237 public function __set($name, $value)
238 {
239 if ($name === 'params')
240 {
241 $this->setParams($value);
242
243 return;
244 }
245
246 $this->set($name, $value);
247 }
248
249 /**
250 * Method check if a certain otherwise inaccessible properties of the form field object is set.
251 *
252 * @param string $name The property name to check.
253 *
254 * @return boolean
255 *
256 * @since 3.7.1
257 * @deprecated 4.0 Deprecated without replacement
258 */
259 public function __isset($name)
260 {
261 if ($name === 'params')
262 {
263 return !($this->params instanceof Registry);
264 }
265
266 return $this->get($name) !== null;
267 }
268
269 /**
270 * Returns the menu item parameters
271 *
272 * @return Registry
273 *
274 * @since 3.7.0
275 */
276 public function getParams()
277 {
278 if (!($this->params instanceof Registry))
279 {
280 try
281 {
282 $this->params = new Registry($this->params);
283 }
284 catch (RuntimeException $e)
285 {
286 /*
287 * Joomla shipped with a broken sample json string for 4 years which caused fatals with new
288 * error checks. So for now we catch the exception here - but one day we should remove it and require
289 * valid JSON.
290 */
291 $this->params = new Registry;
292 }
293 }
294
295 return $this->params;
296 }
297
298 /**
299 * Sets the menu item parameters
300 *
301 * @param Registry|string $params The data to be stored as the parameters
302 *
303 * @return void
304 *
305 * @since 3.7.0
306 */
307 public function setParams($params)
308 {
309 $this->params = $params;
310 }
311
312 /**
313 * Returns a property of the object or the default value if the property is not set.
314 *
315 * @param string $property The name of the property.
316 * @param mixed $default The default value.
317 *
318 * @return mixed The value of the property.
319 *
320 * @since 3.7.0
321 * @deprecated 4.0
322 */
323 public function get($property, $default = null)
324 {
325 if (isset($this->$property))
326 {
327 return $this->$property;
328 }
329
330 return $default;
331 }
332
333 /**
334 * Modifies a property of the object, creating it if it does not already exist.
335 *
336 * @param string $property The name of the property.
337 * @param mixed $value The value of the property to set.
338 *
339 * @return mixed Previous value of the property.
340 *
341 * @since 3.7.0
342 * @deprecated 4.0
343 */
344 public function set($property, $value = null)
345 {
346 $previous = isset($this->$property) ? $this->$property : null;
347 $this->$property = $value;
348
349 return $previous;
350 }
351 }
352