|
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=''; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
/** @var string $currentArea => overrides the current area */ |
|
31
|
|
|
private $currentArea=''; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** @var array $extraUrlParameters => an array or pairs or parameters to be added to the url */ |
|
34
|
|
|
private $extraUrlParameters=[]; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** @var boolean $disableUrlSessionCheck => (boolean) if true the session var/id are omitted from the url */ |
|
37
|
|
|
private $disableUrlSessionCheck=false; |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** @var string $baseUrl => an alternative base url */ |
|
40
|
|
|
private $baseUrl=''; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** @var string $menuType => alternative menu types? */ |
|
43
|
|
|
private $menuType=''; |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** @var boolean $canToggleDropDown => (boolean) if the menu can "toggle" */ |
|
46
|
|
|
private $canToggleDropDown=true; |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** @var string $templateName => an alternative template to load (instead of Generic) */ |
|
49
|
|
|
private $templateName='GenericMenu'; |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
/** @var string $layerName => alternative layer name for the menu */ |
|
52
|
|
|
private $layerName='generic_menu'; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** @var array $hook => hook name to call integrate_ . 'hook name' . '_areas' */ |
|
55
|
|
|
private $counters=[]; |
|
|
|
|
|
|
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->currentArea; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $currentArea |
|
83
|
|
|
*/ |
|
84
|
41 |
|
public function setCurrentArea(string $currentArea) |
|
85
|
|
|
{ |
|
86
|
41 |
|
$this->currentArea = $currentArea; |
|
87
|
41 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return array |
|
91
|
|
|
*/ |
|
92
|
39 |
|
public function getExtraUrlParameters(): array |
|
93
|
|
|
{ |
|
94
|
39 |
|
return $this->extraUrlParameters; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param array $extraUrlParameters |
|
99
|
|
|
*/ |
|
100
|
41 |
|
public function setExtraUrlParameters(array $extraUrlParameters) |
|
101
|
|
|
{ |
|
102
|
41 |
|
$this->extraUrlParameters = $extraUrlParameters; |
|
103
|
41 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return bool |
|
107
|
|
|
*/ |
|
108
|
|
|
public function isUrlSessionCheckDisabled(): bool |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->disableUrlSessionCheck; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param bool $disableUrlSessionCheck |
|
115
|
|
|
*/ |
|
116
|
41 |
|
public function setDisableUrlSessionCheck(bool $disableUrlSessionCheck) |
|
117
|
|
|
{ |
|
118
|
41 |
|
$this->disableUrlSessionCheck = $disableUrlSessionCheck; |
|
119
|
41 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
41 |
|
public function getBaseUrl(): string |
|
125
|
|
|
{ |
|
126
|
41 |
|
return $this->baseUrl; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $baseUrl |
|
131
|
|
|
*/ |
|
132
|
41 |
|
public function setBaseUrl(string $baseUrl) |
|
133
|
|
|
{ |
|
134
|
41 |
|
$this->baseUrl = $baseUrl; |
|
135
|
41 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
41 |
|
public function getMenuType(): string |
|
141
|
|
|
{ |
|
142
|
41 |
|
return $this->menuType; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param string $menuType |
|
147
|
|
|
*/ |
|
148
|
41 |
|
public function setMenuType(string $menuType) |
|
149
|
|
|
{ |
|
150
|
41 |
|
$this->menuType = $menuType; |
|
151
|
41 |
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return bool |
|
155
|
|
|
*/ |
|
156
|
41 |
|
public function isDropDownToggleable(): bool |
|
157
|
|
|
{ |
|
158
|
41 |
|
return $this->canToggleDropDown; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param bool $canToggleDropDown |
|
163
|
|
|
*/ |
|
164
|
41 |
|
public function setCanToggleDropDown(bool $canToggleDropDown) |
|
165
|
|
|
{ |
|
166
|
41 |
|
$this->canToggleDropDown = $canToggleDropDown; |
|
167
|
41 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
38 |
|
public function getTemplateName(): string |
|
173
|
|
|
{ |
|
174
|
38 |
|
return $this->templateName; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $templateName |
|
179
|
|
|
*/ |
|
180
|
41 |
|
public function setTemplateName(string $templateName) |
|
181
|
|
|
{ |
|
182
|
41 |
|
$this->templateName = $templateName; |
|
183
|
41 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return string |
|
187
|
|
|
*/ |
|
188
|
41 |
|
public function getLayerName(): string |
|
189
|
|
|
{ |
|
190
|
41 |
|
return $this->layerName; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param string $layerName |
|
195
|
|
|
*/ |
|
196
|
41 |
|
public function setLayerName(string $layerName) |
|
197
|
|
|
{ |
|
198
|
41 |
|
$this->layerName = $layerName; |
|
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'])) |
|
|
|
|
|
|
280
|
|
|
{ |
|
281
|
39 |
|
$arr[$context['session_var']]=$context['session_id']; |
|
|
|
|
|
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
39 |
|
$extraUrlParameters = ''; |
|
285
|
39 |
|
foreach ($arr as $key => $value) |
|
286
|
|
|
{ |
|
287
|
39 |
|
$extraUrlParameters .= sprintf( |
|
288
|
39 |
|
';%s=%s', |
|
289
|
39 |
|
$key, |
|
290
|
39 |
|
$value |
|
291
|
|
|
); |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
39 |
|
return $extraUrlParameters; |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.