Completed
Pull Request — development (#3050)
by John
23:37
created

MenuOptions::buildTemplateVars()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
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
18
declare(strict_types=1);
19
20
namespace ElkArte\Menu;
21
22
/**
23
 * This class implements a standard way of creating menus
24
 */
25
class MenuOptions
26
{
27
	/** @var string $action                    => overrides the default action */
28
	private $action='';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
29
30
	/** @var string $current_area              => overrides the current area */
31
	private $current_area='';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
32
33
	/** @var array $extra_url_parameters      => an array or pairs or parameters to be added to the url */
34
	private $extra_url_parameters=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
35
36
	/** @var boolean $disable_url_session_check => (boolean) if true the session var/id are omitted from the url */
37
	private $disable_url_session_check=false;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
38
39
	/** @var string $base_url                  => an alternative base url */
40
	private $base_url='';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
41
42
	/** @var string $menu_type                 => alternative menu types? */
43
	private $menu_type='';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
44
45
	/** @var boolean $can_toggle_drop_down      => (boolean) if the menu can "toggle" */
46
	private $can_toggle_drop_down=true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
47
48
	/** @var string $template_name             => an alternative template to load (instead of Generic) */
49
	private $template_name='GenericMenu';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
50
51
	/** @var string $layer_name                => alternative layer name for the menu */
52
	private $layer_name='generic_menu';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
53
54
	/** @var array $hook                      => hook name to call integrate_ . 'hook name' . '_areas' */
55
	private $counters=[];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
56
57
	/**
58
	 * @return string
59
	 */
60 41
	public function getAction(): string
61
	{
62 41
		return $this->action;
63
	}
64
65
	/**
66
	 * @param string $action
67
	 */
68 41
	public function setAction(string $action)
69
	{
70 41
		$this->action = $action;
71 41
	}
72
73
	/**
74
	 * @return string
75
	 */
76 39
	public function getCurrentArea(): string
77
	{
78 39
		return $this->current_area;
79
	}
80
81
	/**
82
	 * @param string $current_area
83
	 */
84 41
	public function setCurrentArea(string $current_area)
85
	{
86 41
		$this->current_area = $current_area;
87 41
	}
88
89
	/**
90
	 * @return array
91
	 */
92 39
	public function getExtraUrlParameters(): array
93
	{
94 39
		return $this->extra_url_parameters;
95
	}
96
97
	/**
98
	 * @param array $extra_url_parameters
99
	 */
100 41
	public function setExtraUrlParameters(array $extra_url_parameters)
101
	{
102 41
		$this->extra_url_parameters = $extra_url_parameters;
103 41
	}
104
105
	/**
106
	 * @return bool
107
	 */
108
	public function isUrlSessionCheckDisabled(): bool
109
	{
110
		return $this->disable_url_session_check;
111
	}
112
113
	/**
114
	 * @param bool $disable_url_session_check
115
	 */
116 41
	public function setDisableUrlSessionCheck(bool $disable_url_session_check)
117
	{
118 41
		$this->disable_url_session_check = $disable_url_session_check;
119 41
	}
120
121
	/**
122
	 * @return string
123
	 */
124 41
	public function getBaseUrl(): string
125
	{
126 41
		return $this->base_url;
127
	}
128
129
	/**
130
	 * @param string $base_url
131
	 */
132 41
	public function setBaseUrl(string $base_url)
133
	{
134 41
		$this->base_url = $base_url;
135 41
	}
136
137
	/**
138
	 * @return string
139
	 */
140 41
	public function getMenuType(): string
141
	{
142 41
		return $this->menu_type;
143
	}
144
145
	/**
146
	 * @param string $menu_type
147
	 */
148 41
	public function setMenuType(string $menu_type)
149
	{
150 41
		$this->menu_type = $menu_type;
151 41
	}
152
153
	/**
154
	 * @return bool
155
	 */
156 41
	public function isDropDownToggleable(): bool
157
	{
158 41
		return $this->can_toggle_drop_down;
159
	}
160
161
	/**
162
	 * @param bool $can_toggle_drop_down
163
	 */
164 41
	public function setCanToggleDropDown(bool $can_toggle_drop_down)
165
	{
166 41
		$this->can_toggle_drop_down = $can_toggle_drop_down;
167 41
	}
168
169
	/**
170
	 * @return string
171
	 */
172 38
	public function getTemplateName(): string
173
	{
174 38
		return $this->template_name;
175
	}
176
177
	/**
178
	 * @param string $template_name
179
	 */
180 41
	public function setTemplateName(string $template_name)
181
	{
182 41
		$this->template_name = $template_name;
183 41
	}
184
185
	/**
186
	 * @return string
187
	 */
188 41
	public function getLayerName(): string
189
	{
190 41
		return $this->layer_name;
191
	}
192
193
	/**
194
	 * @param string $layer_name
195
	 */
196 41
	public function setLayerName(string $layer_name)
197
	{
198 41
		$this->layer_name = $layer_name;
199 41
	}
200
201
	/**
202
	 * @return array
203
	 */
204 38
	public function getCounters(): array
205
	{
206 38
		return $this->counters;
207
	}
208
209
	/**
210
	 * @param array $counters
211
	 */
212 41
	public function setCounters(array $counters)
213
	{
214 41
		$this->counters = $counters;
215 41
	}
216
217
	/**
218
	 * @param array $arr
219
	 *
220
	 * @return MenuOptions
221
	 */
222 41
	public static function buildFromArray(array $arr): MenuOptions
223
	{
224 41
		foreach (array_replace(
225 41
					$vars = get_object_vars($obj = new self),
226 41
					array_intersect_key($arr, $vars)
227
				) as $var => $val)
228
		{
229 41
			$obj->{'set' . str_replace('_', '', ucwords($var, '_'))}($val);
230
		}
231 41
		$obj->buildBaseUrl();
232 41
		$obj->buildTemplateVars();
233
234 41
		return $obj;
235
	}
236
237
	/**
238
	 * The theme needs some love, too.
239
	 */
240 41
	private function buildTemplateVars(): void
241
	{
242 41
		global $userInfo, $options;
243
244 41
		if (empty($this->getMenuType()))
245
		{
246 41
			$this->setMenuType(empty($options['use_sidebar_menu']) ? 'dropdown' : 'sidebar');
247
		}
248 41
		$this->setCanToggleDropDown(!$userInfo['is_guest'] && $this->isDropDownToggleable());
249
250 41
		$this->setLayerName($this->getLayerName() . '_' . $this->getMenuType());
251 41
	}
252
253
	/**
254
	 * Process the array of MenuOptions passed to the class
255
	 */
256 41
	protected function buildBaseUrl(): void
257
	{
258 41
		global $context, $scripturl;
259
260 41
		$this->setAction($this->getAction()?:$context['current_action']);
261
262 41
		$this->setBaseUrl($this->getBaseUrl()?:sprintf(
263 41
				'%s?action=%s',
264 41
				$scripturl,
265 41
				$this->getAction()
266
			));
267 41
	}
268
269
	/**
270
	 * Build the additional parameters for use in the url
271
	 */
272 39
	public function buildAdditionalParams(): string
273
	{
274 39
		global $context;
275
276 39
		$arr = $this->getExtraUrlParameters();
277
278
		// Only include the session ID in the URL if it's strictly necessary.
279 39
		if (empty($this->menuOptions['disable_url_session_check']))
0 ignored issues
show
Bug introduced by
The property menuOptions does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
280
		{
281 39
			$arr[$context['session_var']]=$context['session_id'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 0 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
282
		}
283
284 39
		$extra_url_parameters = '';
285 39
			foreach ($this->extra_url_parameters as $key => $value)
286
			{
287 38
				$extra_url_parameters .= sprintf(
288 38
				';%s=%s',
289 38
				$key,
290 38
				$value
291
			);
292
			}
293
294 39
		return $extra_url_parameters;
295
	}
296
}
297