2204b01a147696071e1a49df29144c0031ce7367
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / breadcrumb / Breadcrumbs.class.php
1 <?php
2
3 namespace wcf\system\breadcrumb;
4
5 use wcf\data\page\PageCache;
6 use wcf\system\page\PageLocationManager;
7 use wcf\system\SingletonFactory;
8 use wcf\system\WCF;
9
10 /**
11 * Manages breadcrumbs.
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>
16 */
17 final class Breadcrumbs extends SingletonFactory implements \Countable, \Iterator
18 {
19 /**
20 * list of breadcrumbs
21 * @var Breadcrumb[]
22 */
23 protected $items;
24
25 /**
26 * Current iterator-index
27 */
28 protected int $index = 0;
29
30 /**
31 * Returns the list of breadcrumbs.
32 *
33 * @return Breadcrumb[]
34 */
35 public function get(): array
36 {
37 if ($this->items === null) {
38 $this->loadBreadcrumbs();
39 }
40
41 return $this->items;
42 }
43
44 /**
45 * Loads the list of breadcrumbs.
46 */
47 protected function loadBreadcrumbs(): void
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 */
84 public function count(): int
85 {
86 if ($this->items === null) {
87 $this->loadBreadcrumbs();
88 }
89
90 return \count($this->items);
91 }
92
93 /**
94 * @inheritDoc
95 */
96 public function current(): Breadcrumb
97 {
98 return $this->items[$this->index];
99 }
100
101 /**
102 * @inheritDoc
103 */
104 public function key(): int
105 {
106 return $this->index;
107 }
108
109 /**
110 * @inheritDoc
111 */
112 public function valid(): bool
113 {
114 return isset($this->items[$this->index]);
115 }
116
117 /**
118 * @inheritDoc
119 */
120 public function rewind(): void
121 {
122 $this->index = 0;
123 }
124
125 /**
126 * @inheritDoc
127 */
128 public function next(): void
129 {
130 $this->index++;
131 }
132 }