Completed
Pull Request — development (#3050)
by John
09:17
created

MenuOptions   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 281
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 96.51%

Importance

Changes 0
Metric Value
dl 0
loc 281
ccs 83
cts 86
cp 0.9651
rs 9.3999
c 0
b 0
f 0
wmc 33
lcom 1
cbo 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A getAction() 0 4 1
A setAction() 0 4 1
A getArea() 0 4 1
A setArea() 0 4 1
A getExtraUrlParameters() 0 4 1
A setExtraUrlParameters() 0 4 1
A isUrlSessionCheckDisabled() 0 4 1
A setDisableUrlSessionCheck() 0 4 1
A getBaseUrl() 0 4 1
A setBaseUrl() 0 4 1
A getMenuType() 0 4 1
A setMenuType() 0 4 1
A isDropDownToggleable() 0 4 1
A setCanToggleDropDown() 0 4 1
A getTemplateName() 0 4 1
A setTemplateName() 0 4 1
A getLayerName() 0 4 1
A setLayerName() 0 4 1
A getCounters() 0 4 1
A setCounters() 0 4 1
A buildFromArray() 0 15 3
A buildTemplateVars() 0 12 4
A buildBaseUrl() 0 14 3
B buildAdditionalParams() 0 24 3
1
<?php
2
3
/**
4
 * This class contains a standard way of displaying side/drop down menus.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 * license:   BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version   2.0 dev
15
 */
16
17
declare(strict_types=1);
18
19
namespace ElkArte\Menu;
20
21
/**
22
 * Class MenuOptions
23
 *
24
 * This class implements a standard way of creating menus
25
 *
26
 * @package ElkArte\Menu
27
 */
28
class MenuOptions
29
{
30
	/** @var string $action overrides the default action */
31
	private $action = '';
32
33
	/** @var string $area overrides the current area */
34
	private $area = '';
35
36
	/** @var array $extraUrlParameters an array or pairs or parameters to be added to the url */
37
	private $extraUrlParameters = [];
38
39
	/** @var boolean $disableUrlSessionCheck if true the session var/id are omitted from the url */
40
	private $disableUrlSessionCheck = false;
41
42
	/** @var string $baseUrl an alternative base url */
43
	private $baseUrl = '';
44
45
	/** @var string $menuType alternative menu type to replace the usual sidebar/dropdown. */
46
	private $menuType = '';
47
48
	/** @var boolean $canToggleDropDown if the menu can toggle between sidebar and dropdown. */
49
	private $canToggleDropDown = true;
50
51
	/** @var string $templateName an alternative template to load (instead of Generic) */
52
	private $templateName = 'GenericMenu';
53
54
	/** @var string $layerName alternative layer name for the menu */
55
	private $layerName = 'generic_menu';
56
57
	/** @var array $counters All the counters to be used for a menu. See Menu::parseCounter() */
58
	private $counters = [];
59
60
	/**
61
	 * @return string
62
	 */
63 41
	public function getAction(): string
64
	{
65 41
		return $this->action;
66
	}
67
68
	/**
69
	 * @param string $action
70
	 */
71 41
	public function setAction(string $action)
72
	{
73 41
		$this->action = $action;
74 41
	}
75
76
	/**
77
	 * @return string
78
	 */
79 40
	public function getArea(): string
80
	{
81 40
		return $this->area;
82
	}
83
84
	/**
85
	 * @param string $area
86
	 */
87 6
	public function setArea(string $area)
88
	{
89 6
		$this->area = $area;
90 6
	}
91
92
	/**
93
	 * @return array
94
	 */
95 40
	public function getExtraUrlParameters(): array
96
	{
97 40
		return $this->extraUrlParameters;
98
	}
99
100
	/**
101
	 * @param array $extraUrlParameters
102
	 */
103 40
	public function setExtraUrlParameters(array $extraUrlParameters)
104
	{
105 40
		$this->extraUrlParameters = $extraUrlParameters;
106 40
	}
107
108
	/**
109
	 * @return bool
110
	 */
111 40
	public function isUrlSessionCheckDisabled(): bool
112
	{
113 40
		return $this->disableUrlSessionCheck;
114
	}
115
116
	/**
117
	 * @param bool $disableUrlSessionCheck
118
	 */
119 1
	public function setDisableUrlSessionCheck(bool $disableUrlSessionCheck)
120
	{
121 1
		$this->disableUrlSessionCheck = $disableUrlSessionCheck;
122 1
	}
123
124
	/**
125
	 * @return string
126
	 */
127 41
	public function getBaseUrl(): string
128
	{
129 41
		return $this->baseUrl;
130
	}
131
132
	/**
133
	 * @param string $baseUrl
134
	 */
135 41
	public function setBaseUrl(string $baseUrl)
136
	{
137 41
		$this->baseUrl = $baseUrl;
138 41
	}
139
140
	/**
141
	 * @return string
142
	 */
143 41
	public function getMenuType(): string
144
	{
145 41
		return $this->menuType;
146
	}
147
148
	/**
149
	 * @param string $menuType
150
	 */
151 41
	public function setMenuType(string $menuType)
152
	{
153 41
		$this->menuType = $menuType;
154 41
	}
155
156
	/**
157
	 * @return bool
158
	 */
159 38
	public function isDropDownToggleable(): bool
160
	{
161 38
		return $this->canToggleDropDown;
162
	}
163
164
	/**
165
	 * @param bool $canToggleDropDown
166
	 */
167 41
	public function setCanToggleDropDown(bool $canToggleDropDown)
168
	{
169 41
		$this->canToggleDropDown = $canToggleDropDown;
170 41
	}
171
172
	/**
173
	 * @return string
174
	 */
175 38
	public function getTemplateName(): string
176
	{
177 38
		return $this->templateName;
178
	}
179
180
	/**
181
	 * @param string $templateName
182
	 */
183
	public function setTemplateName(string $templateName)
184
	{
185
		$this->templateName = $templateName;
186
	}
187
188
	/**
189
	 * @return string
190
	 */
191 41
	public function getLayerName(): string
192
	{
193 41
		return $this->layerName;
194
	}
195
196
	/**
197
	 * @param string $layerName
198
	 */
199 41
	public function setLayerName(string $layerName)
200
	{
201 41
		$this->layerName = $layerName;
202 41
	}
203
204
	/**
205
	 * @return array
206
	 */
207 38
	public function getCounters(): array
208
	{
209 38
		return $this->counters;
210
	}
211
212
	/**
213
	 * @param array $counters
214
	 */
215 2
	public function setCounters(array $counters)
216
	{
217 2
		$this->counters = $counters;
218 2
	}
219
220
	/**
221
	 * Add an array of options for a menu.
222
	 *
223
	 * @param array $arr Options as an array. Keys should match properties
224
	 *                   of MenuOptions and can be either in snake_case or in camelCase,
225
	 *                   depending on your style.
226
	 *
227
	 * @return MenuOptions
228
	 */
229 41
	public static function buildFromArray(array $arr): MenuOptions
230
	{
231 41
		$obj = new self;
232 41
		foreach ($arr as $var => $val)
233
		{
234 41
			if (is_callable([$obj, $call = 'set' . str_replace('_', '', ucwords($var, '_'))]))
235
			{
236 41
				$obj->{$call}($val);
237
			}
238
		}
239 41
		$obj->buildBaseUrl();
240 41
		$obj->buildTemplateVars();
241
242 41
		return $obj;
243
	}
244
245
	/**
246
	 * The theme needs some love, too.
247
	 */
248 41
	private function buildTemplateVars(): void
249
	{
250 41
		global $user_info, $options;
251
252 41
		if (empty($this->getMenuType()))
253
		{
254 41
			$this->setMenuType(empty($options['use_sidebar_menu']) ? 'dropdown' : 'sidebar');
255
		}
256 41
		$this->setCanToggleDropDown(!$user_info['is_guest'] && $this->isDropDownToggleable());
257
258 41
		$this->setLayerName($this->getLayerName() . '_' . $this->getMenuType());
259 41
	}
260
261
	/**
262
	 * Process the array of MenuOptions passed to the class
263
	 */
264 41
	protected function buildBaseUrl(): void
265
	{
266 41
		global $context, $scripturl;
267
268 41
		$this->setAction($this->getAction() ?: $context['current_action']);
269
270 41
		$this->setBaseUrl(
271 41
			$this->getBaseUrl() ?: sprintf(
272 41
				'%s?action=%s',
273 41
				$scripturl,
274 41
				$this->getAction()
275
			)
276
		);
277 41
	}
278
279
	/**
280
	 * Build the additional parameters for use in the url
281
	 *
282
	 * @return string
283
	 */
284 40
	public function buildAdditionalParams(): string
285
	{
286 40
		global $context;
287
288 40
		$arr = $this->getExtraUrlParameters();
289
290
		// Only include the session ID in the URL if it's strictly necessary.
291 40
		if (!$this->isUrlSessionCheckDisabled())
292
		{
293 39
			$arr[$context['session_var']] = $context['session_id'];
294
		}
295
296 40
		$extraUrlParameters = '';
297 40
		foreach ($arr as $key => $value)
298
		{
299 40
			$extraUrlParameters .= sprintf(
300 40
				';%s=%s',
301 40
				$key,
302 40
				$value
303
			);
304
		}
305
306 40
		return $extraUrlParameters;
307
	}
308
}
309