Newer
Older
<?php
namespace QUI\Menu\Independent;
use QUI;
use QUI\Menu\Independent\Items\AbstractMenuItem;
use function is_array;
use function is_numeric;
use function is_string;
use function json_decode;
use function json_encode;
/**
* Main Menu Class
*
* - Manage menu items
*/
class Menu
{
protected int $id;
protected ?array $title = null;
protected ?array $workingTitle = null;
protected array $data = [];
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @param int|array $menuId - menu id or menu data
*
* @throws QUI\Exception
* @throws QUI\Database\Exception
*/
public function __construct($menuId)
{
if (is_numeric($menuId)) {
$data = Handler::getMenuData($menuId);
} else {
$data = $menuId;
if (!isset($data['id'])
|| !isset($data['title'])
|| !isset($data['workingTitle'])
) {
throw new QUI\Exception(
'Menu not found',
404,
[
'menuData' => $data
]
);
}
}
$this->id = $data['id'];
if (is_string($data['title'])) {
$this->title = json_decode($data['title'], true);
} elseif (is_array($data['title'])) {
$this->title = $data['title'];
}
if (is_string($data['workingTitle'])) {
$this->workingTitle = json_decode($data['workingTitle'], true);
} elseif (is_array($data['workingTitle'])) {
$this->workingTitle = $data['workingTitle'];
}
if (is_string($data['data'])) {
$decode = json_decode($data['data'], true);
if ($decode) {
$this->data = json_decode($data['data'], true);
}
} elseif (is_array($data['data'])) {
$this->data = $data['data'];
}
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// build children
if (isset($this->data['children'])) {
$this->buildChildren($this, $this->data['children']);
}
}
//region children
/**
* @param AbstractMenuItem|Menu $Parent
* @param array $children
* @return void
*/
protected function buildChildren($Parent, array $children)
{
foreach ($children as $item) {
$type = $item['type'];
if (!class_exists($type)) {
continue;
}
if (isset($item['title'])) {
$item['title'] = json_decode($item['title'], true);
}
$Item = new $type($item);
if (!($Item instanceof AbstractMenuItem)) {
continue;
}
$Parent->appendChild($Item);
if (isset($item['children']) && is_array($item['children'])) {
$this->buildChildren($Item, $item['children']);
}
}
}
/**
* Add a child item
*
* @param AbstractMenuItem $Item
*/
public function appendChild(AbstractMenuItem $Item)
{
$this->children[] = $Item;
/**
* @return array
*/
public function getChildren(): array
{
return $this->children;
}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//region getter
/**
* @return array
*/
public function toArray(): array
{
return [
'id' => $this->getId(),
'title' => $this->getTitle(),
'workingTitle' => $this->getWorkingTitle(),
'data' => $this->data
];
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param QUI\Locale|null $Locale
* @return string
*/
public function getTitle(QUI\Locale $Locale = null): string
{
if ($this->title === null) {
return '';
}
if ($Locale === null) {
$Locale = QUI::getLocale();
}
$current = $Locale->getCurrent();
if (isset($this->title[$current])) {
return $this->title[$current];
}
return '';
}
/**
* @param QUI\Locale|null $Locale
* @return string
*/
public function getWorkingTitle(QUI\Locale $Locale = null): string
{
if ($this->workingTitle === null) {
return '';
}
if ($Locale === null) {
$Locale = QUI::getLocale();
}
$current = $Locale->getCurrent();
if (isset($this->workingTitle[$current])) {
return $this->workingTitle[$current];
}
return '';
}
//endregion
//region setter
/**
* @param null|QUI\Interfaces\Users\User $PermissionUser
*
* @throws QUI\Database\Exception
* @throws QUI\Permissions\Exception
*/
public function save(?QUI\Interfaces\Users\User $PermissionUser = null)
{
QUI\Permissions\Permission::checkPermission('quiqqer.menu.edit', $PermissionUser);
QUI::getDataBase()->update(Handler::table(), [
'title' => json_encode($this->title),
'workingTitle' => json_encode($this->workingTitle),
'data' => json_encode($this->data)
], [
'id' => $this->getId()
]);
}
/**
* set the titles in different languages
*
* @param array|null $title - ['de' => '', 'en' => '']
* @return void
*/
public function setTitle(?array $title)
{
if ($title === null) {
return;
}
if (!is_array($this->title)) {
$this->title = [];
}
$available = QUI::availableLanguages();
foreach ($available as $language) {
if (isset($title[$language]) && is_string($title[$language])) {
$this->title[$language] = $title[$language];
}
}
}
/**
* set the working titles in different languages
*
* @param array|null $title - ['de' => '', 'en' => '']
* @return void
*/
public function setWorkingTitle(?array $title)
{
if ($title === null) {
return;
}
if (!is_array($this->workingTitle)) {
$this->workingTitle = [];
}
$available = QUI::availableLanguages();
foreach ($available as $language) {
if (isset($title[$language]) && is_string($title[$language])) {
$this->workingTitle[$language] = $title[$language];
}
}
}
/**
* @param array|null $data
* @return void
*/
public function setData(?array $data)
{
if ($data === null) {
return;
}
if (isset($data['children'])) {
$data = $this->checkData($data);
if ($data) {
$this->data = $data;
}
}
}
/**
* Checks a data array and filters not allowed entries
*
* @param $data
* @return array|null
*/
protected function checkData($data): ?array
{
$result = [];
if (isset($data['children'])) {
$result['children'] = [];
foreach ($data['children'] as $item) {
$child = $this->checkMenuDataItem($item);
if ($child) {
$result['children'][] = $this->checkMenuDataItem($item);
}
}
}
if (empty($result)) {
return null;
}
return $result;
}
/**
* @param array $item
* @return array|null
*/
protected function checkMenuDataItem(array $item): ?array
{
$result = [];
if (isset($item['title'])) {
$result['title'] = $item['title'];
}
if (isset($item['data'])) {
$result['data'] = $item['data'];
}
// @todo check if fa icon or image
if (isset($item['icon'])) {
$result['icon'] = $item['icon'];
}
if (isset($item['type'])
&& class_exists($item['type'])
&& new $item['type']() instanceof AbstractMenuItem) {
$result['type'] = $item['type'];
}
if (isset($item['children'])) {
foreach ($item['children'] as $item) {
$child = $this->checkMenuDataItem($item);
if ($child) {
$result['children'][] = $this->checkMenuDataItem($item);
}
}
}
if (empty($result) || !isset($result['title']) || !isset($result['type'])) {
return null;
}
return $result;
}
//endregion
}