Issues (1686)

sources/ElkArte/Menu/MenuOptions.php (1 issue)

1
<?php
2
3
/**
4
 * This class contains a standard way of displaying side/drop down menus.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 dev
14
 *
15
 */
16
17
namespace ElkArte\Menu;
18
19
use ElkArte\User;
20
21
/**
22
 * Class MenuOptions
23
 *
24
 * This class will set and access the menu options. The supported options are:
25
 *
26
 *  - action                    => overrides the default action
27
 *  - current_area              => overrides the current area
28
 *  - extra_url_parameters      => an array or pairs or parameters to be added to the url
29
 *  - disable_url_session_check => (boolean) if true the session var/id are omitted from the url
30
 *  - base_url                  => an alternative base url
31
 *  - menu_type                 => alternative menu types?
32
 *  - can_toggle_drop_down      => (boolean) if the menu can "toggle"
33
 *  - template_name             => an alternative template to load (instead of Generic)
34
 *  - layer_name                => alternative layer name for the menu
35
 *  - hook                      => hook name to call integrate_ . 'hook name' . '_areas'
36
 *  - counters					=> menu counter value
37
 *
38
 * @package ElkArte\Menu
39
 */
40
class MenuOptions
41
{
42
	/** @var string $action overrides the default action */
43
	private $action = '';
44
45
	/** @var string $currentArea overrides the current area */
46
	private $currentArea = '';
47
48
	/** @var array $extraUrlParameters an array or pairs or parameters to be added to the url */
49
	private $extraUrlParameters = [];
50
51
	/** @var bool $disableUrlSessionCheck if true the session var/id are omitted from the url */
52
	private $disableUrlSessionCheck = false;
53
54
	/** @var string $baseUrl an alternative base url */
55
	private $baseUrl = '';
56
57
	/** @var string $menuType alternative menu type to replace the usual sidebar/dropdown. */
58
	private $menuType = '';
59
60
	/** @var bool $canToggleDropDown if the menu can toggle between sidebar and dropdown. */
61
	private $canToggleDropDown = true;
62
63
	/** @var string $templateName an alternative template to load (instead of Generic) */
64
	private $templateName = 'GenericMenu';
65
66
	/** @var string $layerName alternative layer name for the menu */
67
	private $layerName = 'generic_menu';
68
69
	/** @var string $hook name to call integrate_ . $hook . '_areas' */
70
	private $hook = '';
71
72
	/** @var array $counters All the counters to be used for a menu. See Menu::parseCounter() */
73
	private $counters = [];
74
75
	/**
76
	 * Add an array of options for a menu.
77
	 *
78
	 * @param array $arr Options as an array. Keys should match properties
79
	 *                   of MenuOptions and can be either in snake_case or in camelCase,
80
	 *                   depending on your style.
81
	 *
82 62
	 * @return MenuOptions
83
	 */
84 62
	public static function buildFromArray($arr)
85
	{
86
		$obj = new self();
87 62
88
		// For each passed option, call the value setter method
89 62
		foreach ($arr as $var => $val)
90
		{
91 62
			if (is_callable([$obj, $call = 'set' . str_replace('_', '', ucwords($var, '_'))]))
92
			{
93
				$obj->{$call}($val);
94
			}
95 62
		}
96 62
97
		$obj->buildBaseUrl();
98 62
		$obj->buildTemplateVars();
99
100
		return $obj;
101
	}
102
103
	/**
104 62
	 * Process the array of MenuOptions passed to the class
105
	 */
106 62
	protected function buildBaseUrl()
107
	{
108 62
		global $context;
109
110 62
		$this->setAction($this->getAction() ?: $context['current_action']);
111 62
112
		$this->setBaseUrl($this->getBaseUrl() ?: getUrl('action', ['action' => $this->getAction()]));
113
	}
114
115
	/**
116
	 * Get action value
117
	 *
118 62
	 * @return string
119
	 */
120 62
	public function getAction()
121
	{
122
		return $this->action;
123
	}
124
125
	/**
126
	 * Set action value
127
	 *
128 62
	 * @param string $action
129
	 */
130 62
	private function setAction($action)
131 62
	{
132
		$this->action = $action;
133
	}
134
135
	/**
136
	 * Get base URL
137
	 *
138 62
	 * @return string
139
	 */
140 62
	public function getBaseUrl()
141
	{
142
		return $this->baseUrl;
143
	}
144
145
	/**
146
	 * Set base URL
147
	 *
148 62
	 * @param string $baseUrl
149
	 */
150 62
	private function setBaseUrl($baseUrl)
151 62
	{
152
		$this->baseUrl = $baseUrl;
153
	}
154
155
	/**
156 62
	 * The theme needs some love, too.
157
	 */
158 62
	private function buildTemplateVars()
159
	{
160 62
		global $options;
161
162 62
		if (empty($this->getMenuType()))
163
		{
164
			$this->setMenuType(empty($options['use_sidebar_menu']) ? 'dropdown' : 'sidebar');
165 62
		}
166
167 62
		$this->setCanToggleDropDown(!User::$info->is_guest && $this->isDropDownToggleable());
0 ignored issues
show
Bug Best Practice introduced by
The property is_guest does not exist on ElkArte\Helper\ValuesContainer. Since you implemented __get, consider adding a @property annotation.
Loading history...
168 62
169
		$this->setLayerName($this->getLayerName() . '_' . $this->getMenuType());
170
	}
171
172
	/**
173
	 * Get menu type
174
	 *
175 62
	 * @return string
176
	 */
177 62
	public function getMenuType()
178
	{
179
		return $this->menuType;
180
	}
181
182
	/**
183
	 * Set menu type
184
	 *
185 62
	 * @param string $menuType
186
	 */
187 62
	private function setMenuType($menuType)
188 62
	{
189
		$this->menuType = $menuType;
190
	}
191
192
	/**
193 56
	 * @return bool
194
	 */
195 56
	public function isDropDownToggleable()
196
	{
197
		return $this->canToggleDropDown;
198
	}
199
200
	/**
201
	 * Set toggle dropdown
202
	 *
203 62
	 * @param bool $canToggleDropDown
204
	 */
205 62
	private function setCanToggleDropDown($canToggleDropDown)
206 62
	{
207
		$this->canToggleDropDown = $canToggleDropDown;
208
	}
209
210
	/**
211
	 * Get Layer name
212
	 *
213 62
	 * @return string
214
	 */
215 62
	public function getLayerName()
216
	{
217
		return $this->layerName;
218
	}
219
220
	/**
221
	 * Set Layer name
222
	 *
223 62
	 * @param string $layerName
224
	 */
225 62
	private function setLayerName($layerName)
226 62
	{
227
		$this->layerName = $layerName;
228
	}
229
230
	/**
231
	 * Get area value
232
	 *
233 60
	 * @return string
234
	 */
235 60
	public function getCurrentArea()
236
	{
237
		return $this->currentArea;
238
	}
239
240
	/**
241
	 * Set area value
242
	 *
243 18
	 * @param string $currentArea
244
	 */
245 18
	private function setCurrentArea($currentArea)
246 18
	{
247
		$this->currentArea = $currentArea;
248
	}
249
250
	/**
251
	 * Get session check
252
	 *
253 60
	 * @return bool
254
	 */
255 60
	public function isUrlSessionCheckDisabled()
256
	{
257
		return $this->disableUrlSessionCheck;
258
	}
259
260
	/**
261
	 * Set session check
262
	 *
263 8
	 * @param bool $disableUrlSessionCheck
264
	 */
265 8
	private function setDisableUrlSessionCheck($disableUrlSessionCheck)
266 8
	{
267
		$this->disableUrlSessionCheck = $disableUrlSessionCheck;
268
	}
269
270
	/**
271
	 * Get template name
272
	 *
273 56
	 * @return string
274
	 */
275 56
	public function getTemplateName()
276
	{
277
		return $this->templateName;
278
	}
279
280
	/**
281
	 * Set template name
282
	 *
283
	 * @param string $templateName
284
	 */
285
	private function setTemplateName($templateName)
286
	{
287
		$this->templateName = $templateName;
288
	}
289
290
	/**
291
	 * Get counter
292
	 *
293 56
	 * @return array
294
	 */
295 56
	public function getCounters()
296
	{
297
		return $this->counters;
298
	}
299
300
	/**
301
	 * Set Counter
302
	 *
303 4
	 * @param array $counters
304
	 */
305 4
	private function setCounters($counters)
306 4
	{
307
		$this->counters = $counters;
308
	}
309
310
	/**
311
	 * Build the additional parameters for use in the url
312
	 *
313 60
	 * @return string
314
	 */
315 60
	public function buildAdditionalParams()
316
	{
317 60
		global $context;
318
319
		$arr = $this->getExtraUrlParameters();
320 60
321
		// Only include the session ID in the URL if it's strictly necessary.
322 52
		if (!$this->isUrlSessionCheckDisabled())
323
		{
324
			$arr[$context['session_var']] = $context['session_id'];
325 60
		}
326 60
327
		$extraUrlParameters = '';
328 52
		foreach ($arr as $key => $value)
329
		{
330
			$extraUrlParameters .= sprintf(';%s=%s', $key, $value);
331 60
		}
332
333
		return $extraUrlParameters;
334
	}
335
336
	/**
337
	 * Get URL parameters
338
	 *
339 60
	 * @return array
340
	 */
341 60
	public function getExtraUrlParameters()
342
	{
343
		return $this->extraUrlParameters;
344
	}
345
346
	/**
347
	 * Set URL parameters
348
	 *
349 52
	 * @param array $extraUrlParameters
350
	 */
351 52
	private function setExtraUrlParameters($extraUrlParameters)
352 52
	{
353
		$this->extraUrlParameters = $extraUrlParameters;
354
	}
355
356
	/**
357
	 * Get the hook name
358
	 *
359 60
	 * @return string
360
	 */
361 60
	public function getHook()
362
	{
363
		return $this->hook;
364
	}
365
366
	/**
367
	 * Set the hook name
368
	 *
369 20
	 * @param string $hook
370
	 */
371 20
	private function setHook($hook)
372 20
	{
373
		$this->hook = 'integrate_' . $hook . '_areas';
374
	}
375
}
376