Merge pull request #5989 from WoltLab/wsc-rpc-api-const
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / breadcrumb / Breadcrumbs.class.php
CommitLineData
11ade432 1<?php
a9229942 2
11ade432 3namespace wcf\system\breadcrumb;
a9229942 4
ac4ff35d
AE
5use wcf\data\page\PageCache;
6use wcf\system\page\PageLocationManager;
11ade432 7use wcf\system\SingletonFactory;
94295844 8use wcf\system\WCF;
11ade432
AE
9
10/**
11 * Manages breadcrumbs.
a9229942
TD
12 *
13 * @author Alexander Ebert
14 * @copyright 2001-2019 WoltLab GmbH
15 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11ade432 16 */
354b72a6 17final class Breadcrumbs extends SingletonFactory implements \Countable, \Iterator
a9229942
TD
18{
19 /**
20 * list of breadcrumbs
21 * @var Breadcrumb[]
22 */
23 protected $items;
24
25 /**
26 * Current iterator-index
27 */
3d574b8d 28 protected int $index = 0;
a9229942 29
a9229942
TD
30 /**
31 * Returns the list of breadcrumbs.
32 *
33 * @return Breadcrumb[]
34 */
3d574b8d 35 public function get(): array
a9229942
TD
36 {
37 if ($this->items === null) {
38 $this->loadBreadcrumbs();
39 }
40
41 return $this->items;
42 }
43
a9229942
TD
44 /**
45 * Loads the list of breadcrumbs.
46 */
3d574b8d 47 protected function loadBreadcrumbs(): void
a9229942
TD
48 {
49 $this->items = [];
50 $locations = PageLocationManager::getInstance()->getLocations();
51
52 // locations are provided starting with the current location followed
53 // by all relevant ancestors, but breadcrumbs appear in the reverse order
54 $locations = \array_reverse($locations);
55
56 // add the landing page as first location, unless it is already included
57 $landingPage = PageCache::getInstance()->getLandingPage();
58 $addLandingPage = true;
59 for ($i = 0, $length = \count($locations); $i < $length; $i++) {
60 if ($locations[$i]['pageID'] == $landingPage->pageID) {
61 $addLandingPage = false;
62 break;
63 }
64 }
65
66 if ($addLandingPage) {
67 \array_unshift($locations, [
68 'link' => $landingPage->getLink(),
69 'title' => BREADCRUMBS_HOME_USE_PAGE_TITLE ? WCF::getLanguage()->get(PAGE_TITLE) : $landingPage->getTitle(),
70 ]);
71 }
72
73 // ignore the last location as it represents the current page
74 \array_pop($locations);
75
76 for ($i = 0, $length = \count($locations); $i < $length; $i++) {
77 $this->items[] = new Breadcrumb($locations[$i]['title'], $locations[$i]['link']);
78 }
79 }
80
81 /**
82 * @inheritDoc
83 */
3c3f931d 84 public function count(): int
a9229942
TD
85 {
86 if ($this->items === null) {
87 $this->loadBreadcrumbs();
88 }
89
90 return \count($this->items);
91 }
92
93 /**
94 * @inheritDoc
95 */
3d574b8d 96 public function current(): Breadcrumb
a9229942
TD
97 {
98 return $this->items[$this->index];
99 }
100
101 /**
102 * @inheritDoc
103 */
3d574b8d 104 public function key(): int
a9229942
TD
105 {
106 return $this->index;
107 }
108
109 /**
110 * @inheritDoc
111 */
3d574b8d 112 public function valid(): bool
a9229942
TD
113 {
114 return isset($this->items[$this->index]);
115 }
116
117 /**
118 * @inheritDoc
119 */
3d574b8d 120 public function rewind(): void
a9229942
TD
121 {
122 $this->index = 0;
123 }
124
125 /**
126 * @inheritDoc
127 */
3d574b8d 128 public function next(): void
a9229942
TD
129 {
130 $this->index++;
131 }
11ade432 132}