PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.8.16
VikBooking Hotel Booking Engine & PMS v1.8.16
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
← All changes | libraries/adapter/menu/menu.php +384 -0 1.6.1 → 1.8.16 View file →
@@ -1,0 +1,384 @@
1 +<?php
2 +/**
3 + * @package VikWP - Libraries
4 + * @subpackage adapter.mvc
5 + * @author E4J s.r.l.
6 + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 + * @link https://vikwp.com
9 + */
10 +
11 +// No direct access
12 +defined('ABSPATH') or die('No script kiddies please!');
13 +
14 +/**
15 + * Menu class.
16 + *
17 + * @since 10.1.19
18 + */
19 +abstract class JMenu
20 +{
21 + /**
22 + * Menu instances container.
23 + *
24 + * @var array
25 + */
26 + protected static $instances = array();
27 +
28 + /**
29 + * Array to hold the menu items.
30 + *
31 + * @var array
32 + */
33 + protected $items = array();
34 +
35 + /**
36 + * Identifier of the default menu item (one for each language).
37 + *
38 + * @var array
39 + */
40 + protected $default = null;
41 +
42 + /**
43 + * Identifier of the active menu item.
44 + *
45 + * @var integer
46 + */
47 + protected $active = 0;
48 +
49 + /**
50 + * User object to check access levels for.
51 + *
52 + * @var JUser
53 + */
54 + protected $user;
55 +
56 + /**
57 + * Application object.
58 + *
59 + * @var JApplication
60 + */
61 + protected $app;
62 +
63 + /**
64 + * Database driver.
65 + *
66 + * @var JDatabase
67 + */
68 + protected $db;
69 +
70 + /**
71 + * Language object.
72 + *
73 + * @var JLanguage
74 + */
75 + protected $language;
76 +
77 + /**
78 + * Returns a Menu object.
79 + *
80 + * @param string $client The name of the client.
81 + * @param array $options An associative array of options.
82 + *
83 + * @return self A menu object.
84 + *
85 + * @throws Exception
86 + */
87 + public static function getInstance($client, $options = array())
88 + {
89 + $client = strtolower($client);
90 +
91 + if (empty(self::$instances[$client]))
92 + {
93 + // try to load file
94 + if (!JLoader::import('adapter.menu.classes.' . $client))
95 + {
96 + throw new Exception(sprintf('Menu [%s] not found', $client), 404);
97 + }
98 +
99 + // create a Menu object
100 + $classname = 'JMenu' . ucfirst($client);
101 +
102 + // make sure the class exists and it is valid
103 + if (!class_exists($classname) || !is_subclass_of($classname, 'JMenu'))
104 + {
105 + throw new Exception(sprintf('Invalid menu [%s] class', $classname), 500);
106 + }
107 +
108 + // instantiate the class and cache it
109 + self::$instances[$client] = new $classname($options);
110 + }
111 +
112 + return self::$instances[$client];
113 + }
114 +
115 + /**
116 + * Class constructor.
117 + *
118 + * @param array $options An array of configuration options.
119 + *
120 + * @uses load()
121 + */
122 + public function __construct($options = array())
123 + {
124 + // load the menu items
125 + $this->load();
126 +
127 + foreach ($this->items as $item)
128 + {
129 + if ($item->home)
130 + {
131 + $this->default[trim($item->language)] = $item->id;
132 + }
133 + }
134 +
135 + // retrieve user from options
136 + if (isset($options['user']) && $options['user'] instanceof JUser)
137 + {
138 + $this->user = $options['user'];
139 + }
140 + else
141 + {
142 + $this->user = JFactory::getUser();
143 + }
144 +
145 + // retrieve app from options
146 + if (isset($options['app']) && $options['app'] instanceof JApplication)
147 + {
148 + $this->app = $options['app'];
149 + }
150 + else
151 + {
152 + $this->app = JFactory::getApplication();
153 + }
154 +
155 + // retrieve database from options
156 + if (isset($options['db']) && $options['db'] instanceof JDatabase)
157 + {
158 + $this->db = $options['db'];
159 + }
160 + else
161 + {
162 + $this->db = JFactory::getDbo();
163 + }
164 +
165 + // retrieve language from options
166 + if (isset($options['app']) && $options['app'] instanceof JLanguage)
167 + {
168 + $this->language = $options['language'];
169 + }
170 + else
171 + {
172 + $this->language = JFactory::getLanguage();
173 + }
174 + }
175 +
176 + /**
177 + * Gets menu item by id.
178 + *
179 + * @param integer $id The item id.
180 + *
181 + * @return mixed The item object if the ID exists or null if not found.
182 + */
183 + public function getItem($id)
184 + {
185 + $result = null;
186 +
187 + if (isset($this->items[$id]))
188 + {
189 + $result = &$this->items[$id];
190 + }
191 +
192 + return $result;
193 + }
194 +
195 + /**
196 + * Sets the default item by id and language code.
197 + *
198 + * @param integer $id The menu item id.
199 + * @param string $language The language code.
200 + *
201 + * @return boolean True if a menu item with the given ID exists.
202 + */
203 + public function setDefault($id, $language = '*')
204 + {
205 + if (isset($this->items[$id]))
206 + {
207 + $this->default[$language] = $id;
208 +
209 + return true;
210 + }
211 +
212 + return false;
213 + }
214 +
215 + /**
216 + * Gets the default item by language code.
217 + *
218 + * @param string $language The language code, default value of * means all.
219 + *
220 + * @return mixed The item object or null when not found for given language.
221 + */
222 + public function getDefault($language = '*')
223 + {
224 + if (array_key_exists($language, $this->default))
225 + {
226 + return $this->items[$this->default[$language]];
227 + }
228 +
229 + if (array_key_exists('*', $this->default))
230 + {
231 + return $this->items[$this->default['*']];
232 + }
233 +
234 + return null;
235 + }
236 +
237 + /**
238 + * Sets the default item by id.
239 + *
240 + * @param integer $id The item id.
241 + *
242 + * @return mixed The menu item representing the given ID if present or null otherwise.
243 + */
244 + public function setActive($id)
245 + {
246 + if (isset($this->items[$id]))
247 + {
248 + $this->active = $id;
249 +
250 + return $this->items[$id];
251 + }
252 +
253 + return;
254 + }
255 +
256 + /**
257 + * Gets menu item by id.
258 + *
259 + * @return mixed The item object if an active menu item has been set or null.
260 + */
261 + public function getActive()
262 + {
263 + if ($this->active)
264 + {
265 + return $this->items[$this->active];
266 + }
267 +
268 + return null;
269 + }
270 +
271 + /**
272 + * Gets menu items by attribute.
273 + *
274 + * @param mixed $attributes The field name(s).
275 + * @param mixed $values The value(s) of the field. If an array, need to match field names
276 + * each attribute may have multiple values to lookup for.
277 + * @param boolean $firstonly If true, only returns the first item found.
278 + *
279 + * @return mixed An array of menu items or a single object if the $firstonly parameter is true.
280 + */
281 + public function getItems($attributes, $values, $firstonly = false)
282 + {
283 + $items = array();
284 + $attributes = (array) $attributes;
285 + $values = (array) $values;
286 + $count = count($attributes);
287 +
288 + foreach ($this->items as $item)
289 + {
290 + if (!is_object($item))
291 + {
292 + continue;
293 + }
294 +
295 + $test = true;
296 +
297 + for ($i = 0; $i < $count; $i++)
298 + {
299 + if (is_array($values[$i]))
300 + {
301 + if (!in_array($item->{$attributes[$i]}, $values[$i]))
302 + {
303 + $test = false;
304 + break;
305 + }
306 + }
307 + else
308 + {
309 + if ($item->{$attributes[$i]} != $values[$i])
310 + {
311 + $test = false;
312 + break;
313 + }
314 + }
315 + }
316 +
317 + if ($test)
318 + {
319 + if ($firstonly)
320 + {
321 + return $item;
322 + }
323 +
324 + $items[] = $item;
325 + }
326 + }
327 +
328 + return $items;
329 + }
330 +
331 + /**
332 + * Gets the parameter object for a certain menu item.
333 + *
334 + * @param integer $id The item id.
335 + *
336 + * @return JRegistry
337 + */
338 + public function getParams($id)
339 + {
340 + if ($menu = $this->getItem($id))
341 + {
342 + return $menu->params;
343 + }
344 +
345 + return new Registry();
346 + }
347 +
348 + /**
349 + * Getter for the menu array.
350 + *
351 + * @return array
352 + */
353 + public function getMenu()
354 + {
355 + return $this->items;
356 + }
357 +
358 + /**
359 + * Method to check Menu object authorization against an access control
360 + * object and optionally an access extension object.
361 + *
362 + * @param integer $id The menu id.
363 + *
364 + * @return boolean
365 + */
366 + public function authorise($id)
367 + {
368 + $menu = $this->getItem($id);
369 +
370 + if ($menu)
371 + {
372 + return in_array((int) $menu->access, (array) $this->user->getAuthorisedViewLevels());
373 + }
374 +
375 + return true;
376 + }
377 +
378 + /**
379 + * Loads the menu items.
380 + *
381 + * @return array
382 + */
383 + abstract public function load();
384 +}