Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / breadcrumb / Breadcrumbs.class.php
CommitLineData
11ade432
AE
1<?php
2namespace wcf\system\breadcrumb;
ac4ff35d
AE
3use wcf\data\page\PageCache;
4use wcf\system\page\PageLocationManager;
11ade432 5use wcf\system\SingletonFactory;
94295844 6use wcf\system\WCF;
11ade432
AE
7
8/**
9 * Manages breadcrumbs.
10 *
11 * @author Alexander Ebert
c839bd49 12 * @copyright 2001-2018 WoltLab GmbH
11ade432 13 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 14 * @package WoltLabSuite\Core\System\Breadcrumb
11ade432 15 */
61714a53 16class Breadcrumbs extends SingletonFactory implements \Countable, \Iterator {
11ade432
AE
17 /**
18 * list of breadcrumbs
893aace3 19 * @var Breadcrumb[]
9f959ced 20 */
ac4ff35d 21 protected $items = null;
11ade432 22
61714a53
TD
23 /**
24 * Current iterator-index
25 */
26 protected $index = 0;
27
c8a4a1f5 28 /**
39abe192 29 * @inheritDoc
c8a4a1f5 30 */
7d29ab42 31 protected function init() {}
c8a4a1f5 32
11ade432 33 /**
b0eae87f 34 * Adds a breadcrumb (insertion order is crucial!).
11ade432 35 *
39abe192 36 * @param Breadcrumb $item
e71525e4 37 * @deprecated 3.0
9f959ced 38 */
11ade432 39 public function add(Breadcrumb $item) {
ac4ff35d 40 throw new \BadMethodCallException("Breadcrumbs::add() is no longer supported, please use " . PageLocationManager::class . " instead.");
11ade432
AE
41 }
42
43 /**
44 * Returns the list of breadcrumbs.
45 *
39abe192 46 * @return Breadcrumb[]
9f959ced 47 */
11ade432 48 public function get() {
ac4ff35d
AE
49 if ($this->items === null) {
50 $this->loadBreadcrumbs();
51 }
52
11ade432
AE
53 return $this->items;
54 }
55
56 /**
57 * Replaces a breadcrumb, returns true if replacement was successful.
58 *
39abe192
AE
59 * @param Breadcrumb $item
60 * @param integer $index
11ade432 61 * @return boolean
e71525e4 62 * @deprecated 3.0
11ade432 63 */
505b635d 64 public function replace(Breadcrumb $item, $index) {
ac4ff35d 65 throw new \BadMethodCallException("Breadcrumbs::replace() is no longer supported, please use " . PageLocationManager::class . " instead.");
11ade432
AE
66 }
67
68 /**
69 * Removes a breadcrumb, returns true if deletion was successful.
70 *
b0eae87f 71 * @param integer $index
11ade432 72 * @return boolean
e71525e4 73 * @deprecated 3.0
11ade432
AE
74 */
75 public function remove($index) {
ac4ff35d
AE
76 throw new \BadMethodCallException("Breadcrumbs::remove() is no longer supported, please use " . PageLocationManager::class . " instead.");
77 }
78
cddd373d
AE
79 /**
80 * Loads the list of breadcrumbs.
81 */
ac4ff35d
AE
82 protected function loadBreadcrumbs() {
83 $this->items = [];
84 $locations = PageLocationManager::getInstance()->getLocations();
85
86 // locations are provided starting with the current location followed
87 // by all relevant ancestors, but breadcrumbs appear in the reverse order
88 $locations = array_reverse($locations);
89
90 // add the landing page as first location, unless it is already included
91 $landingPage = PageCache::getInstance()->getLandingPage();
92 $addLandingPage = true;
93 for ($i = 0, $length = count($locations); $i < $length; $i++) {
94 if ($locations[$i]['pageID'] == $landingPage->pageID) {
95 $addLandingPage = false;
96 break;
97 }
98 }
99
100 if ($addLandingPage) {
101 array_unshift($locations, [
102 'link' => $landingPage->getLink(),
94295844 103 'title' => WCF::getLanguage()->get(PAGE_TITLE)
ac4ff35d 104 ]);
11ade432
AE
105 }
106
ac4ff35d
AE
107 // ignore the last location as it represents the current page
108 array_pop($locations);
109
110 for ($i = 0, $length = count($locations); $i < $length; $i++) {
111 $this->items[] = new Breadcrumb($locations[$i]['title'], $locations[$i]['link']);
112 }
11ade432 113 }
61714a53
TD
114
115 /**
39abe192 116 * @inheritDoc
61714a53
TD
117 */
118 public function count() {
ac4ff35d
AE
119 if ($this->items === null) {
120 $this->loadBreadcrumbs();
121 }
122
61714a53
TD
123 return count($this->items);
124 }
125
126 /**
39abe192 127 * @inheritDoc
61714a53
TD
128 */
129 public function current() {
130 return $this->items[$this->index];
131 }
132
133 /**
39abe192 134 * @inheritDoc
61714a53
TD
135 */
136 public function key() {
137 return $this->index;
138 }
139
140 /**
39abe192 141 * @inheritDoc
61714a53
TD
142 */
143 public function valid() {
144 return isset($this->items[$this->index]);
145 }
146
147 /**
39abe192 148 * @inheritDoc
61714a53
TD
149 */
150 public function rewind() {
151 $this->index = 0;
152 }
153
154 /**
39abe192 155 * @inheritDoc
61714a53
TD
156 */
157 public function next() {
158 $this->index++;
159 }
11ade432 160}