1 <?php
2
3 4 5 6 7
8 class rex_be_page
9 {
10 private $key;
11 private $fullKey;
12 private $title;
13
14 private = null;
15 private $href;
16 private $itemAttr = [];
17 private $linkAttr = [];
18 private $path;
19 private $subPath;
20
21
22 private $parent;
23
24
25 private $subpages = [];
26
27 private $isActive = null;
28 private $hidden = false;
29 private $hasLayout = true;
30 private $hasNavigation = true;
31 private $pjax;
32 private $icon;
33 private $requiredPermissions = [];
34
35 36 37 38 39 40 41 42
43 public function __construct($key, $title)
44 {
45 if (!is_string($key)) {
46 throw new InvalidArgumentException('Expecting $key to be a string, ' . gettype($key) . ' given!');
47 }
48 if (!is_string($title)) {
49 throw new InvalidArgumentException('Expecting $title to be a string, ' . gettype($title) . ' given!');
50 }
51
52 $this->key = $key;
53 $this->fullKey = $key;
54 $this->title = $title;
55 }
56
57 58 59 60 61
62 public function getKey()
63 {
64 return $this->key;
65 }
66
67 68 69 70 71
72 public function getFullKey()
73 {
74 return $this->fullKey;
75 }
76
77 78 79 80 81
82 public function getTitle()
83 {
84 return $this->title;
85 }
86
87 88 89 90 91 92 93 94 95 96
97 public function ($popup)
98 {
99 if ($popup) {
100 $this->popup = true;
101 $this->setHasNavigation(false);
102 $this->addItemClass('rex-popup');
103 $this->addLinkClass('rex-popup');
104 if (is_string($popup)) {
105 $this->setLinkAttr('onclick', $popup);
106 }
107 } else {
108 $this->popup = false;
109 $this->setHasNavigation(true);
110 $this->removeItemClass('rex-popup');
111 $this->removeLinkClass('rex-popup');
112 $this->removeLinkAttr('onclick');
113 }
114
115 return $this;
116 }
117
118 119 120 121 122
123 public function ()
124 {
125 if (null !== $this->popup) {
126 return $this->popup;
127 }
128
129 return $this->parent && $this->parent->isPopup();
130 }
131
132 133 134 135 136 137 138
139 public function setHref($href)
140 {
141 if (is_array($href)) {
142 $href = rex_url::backendController($href, false);
143 }
144 $this->href = $href;
145
146 return $this;
147 }
148
149 150 151 152 153
154 public function hasHref()
155 {
156 return (bool) $this->href;
157 }
158
159 160 161 162 163
164 public function getHref()
165 {
166 if ($this->href) {
167 return $this->href;
168 }
169 return rex_url::backendPage($this->getFirstSubpagesLeaf()->getFullKey(), [], false);
170 }
171
172 173 174 175 176 177 178 179 180 181
182 public function setItemAttr($name, $value)
183 {
184 if (!is_string($name)) {
185 throw new InvalidArgumentException('Expecting $name to be a string, ' . gettype($name) . 'given!');
186 }
187 if (!is_scalar($value)) {
188 throw new InvalidArgumentException('Expecting $value to be a scalar, ' . gettype($value) . 'given!');
189 }
190 $this->itemAttr[$name] = $value;
191
192 return $this;
193 }
194
195 196 197 198 199 200 201 202
203 public function getItemAttr($name, $default = '')
204 {
205
206 if ($name === null) {
207 return $this->itemAttr;
208 }
209
210 return isset($this->itemAttr[$name]) ? $this->itemAttr[$name] : $default;
211 }
212
213 214 215 216 217
218 public function removeItemAttr($name)
219 {
220 unset($this->itemAttr[$name]);
221 }
222
223 224 225 226 227 228 229 230 231
232 public function addItemClass($class)
233 {
234 if (!is_string($class)) {
235 throw new InvalidArgumentException('Expecting $class to be a string, ' . gettype($class) . 'given!');
236 }
237 $classAttr = $this->getItemAttr('class');
238 if (!preg_match('/\b' . preg_quote($class, '/') . '\b/', $classAttr)) {
239 $this->setItemAttr('class', ltrim($classAttr . ' ' . $class));
240 }
241
242 return $this;
243 }
244
245 246 247 248 249
250 public function removeItemClass($class)
251 {
252 $this->setItemAttr('class', preg_replace('/\b' . preg_quote($class, '/') . '\b/', '', $this->getItemAttr('class')));
253 }
254
255 256 257 258 259 260 261 262 263 264
265 public function setLinkAttr($name, $value)
266 {
267 if (!is_string($name)) {
268 throw new InvalidArgumentException('Expecting $name to be a string, ' . gettype($name) . 'given!');
269 }
270 if (!is_scalar($value)) {
271 throw new InvalidArgumentException('Expecting $value to be a scalar, ' . gettype($value) . 'given!');
272 }
273 $this->linkAttr[$name] = $value;
274
275 return $this;
276 }
277
278 279 280 281 282
283 public function removeLinkAttr($name)
284 {
285 unset($this->linkAttr[$name]);
286 }
287
288 289 290 291 292 293 294 295
296 public function getLinkAttr($name, $default = '')
297 {
298
299 if ($name === null) {
300 return $this->linkAttr;
301 }
302
303 return isset($this->linkAttr[$name]) ? $this->linkAttr[$name] : $default;
304 }
305
306 307 308 309 310 311 312 313 314
315 public function addLinkClass($class)
316 {
317 if (!is_string($class)) {
318 throw new InvalidArgumentException('Expecting $class to be a string, ' . gettype($class) . 'given!');
319 }
320 $classAttr = $this->getLinkAttr('class');
321 if (!preg_match('/\b' . preg_quote($class, '/') . '\b/', $classAttr)) {
322 $this->setLinkAttr('class', ltrim($classAttr . ' ' . $class));
323 }
324
325 return $this;
326 }
327
328 329 330 331 332
333 public function removeLinkClass($class)
334 {
335 $this->setLinkAttr('class', preg_replace('/\b' . preg_quote($class, '/') . '\b/', '', $this->getLinkAttr('class')));
336 }
337
338 339 340 341 342 343 344
345 public function setPath($path)
346 {
347 $this->path = $path;
348
349 return $this;
350 }
351
352 353 354 355 356
357 public function hasPath()
358 {
359 return !empty($this->path) || $this->parent && $this->parent->hasPath();
360 }
361
362 363 364 365 366
367 public function getPath()
368 {
369 if (!empty($this->path)) {
370 return $this->path;
371 }
372 return $this->parent ? $this->parent->getPath() : null;
373 }
374
375 376 377 378 379 380 381
382 public function setSubPath($subPath)
383 {
384 $this->subPath = $subPath;
385
386 return $this;
387 }
388
389 390 391 392 393
394 public function hasSubPath()
395 {
396 return !empty($this->subPath);
397 }
398
399 400 401 402 403
404 public function getSubPath()
405 {
406 return $this->subPath;
407 }
408
409 410 411 412 413 414 415
416 public function addSubpage(self $subpage)
417 {
418 $this->subpages[$subpage->getKey()] = $subpage;
419 $subpage->parent = $this;
420 $subpage->setParentKey($this->getFullKey());
421
422 return $this;
423 }
424
425 426 427
428 private function setParentKey($key)
429 {
430 $this->fullKey = $key . '/' . $this->key;
431 foreach ($this->subpages as $subpage) {
432 $subpage->setParentKey($this->fullKey);
433 }
434 }
435
436 437 438 439 440 441 442
443 public function setSubpages(array $subpages)
444 {
445 $this->subpages = [];
446 array_walk($subpages, [$this, 'addSubpage']);
447
448 return $this;
449 }
450
451 452 453 454 455 456 457
458 public function getSubpage($key)
459 {
460 return isset($this->subpages[$key]) ? $this->subpages[$key] : null;
461 }
462
463 464 465 466 467
468 public function getSubpages()
469 {
470 return $this->subpages;
471 }
472
473 474 475 476 477
478 public function getFirstSubpagesLeaf()
479 {
480 $page = $this;
481 while ($subpages = $page->getSubpages()) {
482 $page = reset($subpages);
483 }
484 return $page;
485 }
486
487 488 489 490 491 492 493
494 public function setIsActive($isActive = true)
495 {
496 $this->isActive = $isActive;
497
498 return $this;
499 }
500
501 502 503 504 505
506 public function isActive()
507 {
508 if ($this->isActive !== null) {
509 return $this->isActive;
510 }
511 $page = rex_be_controller::getCurrentPageObject();
512 do {
513 if ($page === $this) {
514 return true;
515 }
516 } while ($page = $page->getParent());
517 return false;
518 }
519
520 521 522 523 524
525 public function getParent()
526 {
527 return $this->parent;
528 }
529
530 531 532 533 534 535 536
537 public function setHidden($hidden = true)
538 {
539 $this->hidden = $hidden;
540
541 return $this;
542 }
543
544 545 546 547 548
549 public function isHidden()
550 {
551 return $this->hidden;
552 }
553
554 555 556 557 558 559 560
561 public function setHasLayout($hasLayout)
562 {
563 $this->hasLayout = $hasLayout;
564
565 return $this;
566 }
567
568 569 570 571 572
573 public function hasLayout()
574 {
575 return $this->hasLayout && (!$this->parent || $this->parent->hasLayout());
576 }
577
578 579 580 581 582 583 584
585 public function setHasNavigation($hasNavigation)
586 {
587 $this->hasNavigation = $hasNavigation;
588
589 return $this;
590 }
591
592 593 594 595 596
597 public function hasNavigation()
598 {
599 return $this->hasNavigation && (!$this->parent || $this->parent->hasNavigation());
600 }
601
602 603 604 605 606 607 608
609 public function setPjax($pjax = true)
610 {
611 $this->pjax = $pjax;
612
613 return $this;
614 }
615
616 617 618 619 620
621 public function allowsPjax()
622 {
623 if (null !== $this->pjax) {
624 return $this->pjax;
625 }
626 if ($this->parent) {
627 return $this->parent->allowsPjax();
628 }
629 return false;
630 }
631
632 633 634 635 636 637 638
639 public function setIcon($icon)
640 {
641 $this->icon = $icon;
642
643 return $this;
644 }
645
646 647 648 649 650
651 public function getIcon()
652 {
653 return $this->icon;
654 }
655
656 657 658 659 660
661 public function hasIcon()
662 {
663 return !empty($this->icon);
664 }
665
666 667 668 669 670 671 672
673 public function setRequiredPermissions($perm)
674 {
675 $this->requiredPermissions = (array) $perm;
676
677 return $this;
678 }
679
680 681 682 683 684
685 public function getRequiredPermissions()
686 {
687 return $this->requiredPermissions;
688 }
689
690 691 692 693 694 695 696
697 public function checkPermission(rex_user $rexUser)
698 {
699 foreach ($this->requiredPermissions as $perm) {
700 if (!$rexUser->hasPerm($perm)) {
701 return false;
702 }
703 }
704 if ($parent = $this->getParent()) {
705 return $parent->checkPermission($rexUser);
706 }
707 return true;
708 }
709 }
710