1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: namespace Pancake;
17:
18: 19: 20: 21: 22:
23: class Navigation {
24:
25: 26: 27: 28: 29: 30:
31: const TYPE_LINK = 1;
32:
33: 34: 35: 36: 37: 38:
39: const TYPE_LABEL = 2;
40:
41: 42: 43: 44: 45: 46:
47: const TYPE_DIVIDER = 4;
48:
49: 50: 51: 52:
53: protected static $navbar;
54:
55: 56: 57: 58:
59: protected static $quicklinks;
60:
61: 62: 63: 64: 65: 66: 67: 68: 69: 70:
71: public static function registerNavbarLink($url, $title, $parent_url = null) {
72: self::registerNavbarItem($url, $title, $parent_url, self::TYPE_LINK);
73: }
74:
75: 76: 77: 78: 79: 80: 81: 82: 83: 84:
85: public static function registerNavbarLabel($title, $parent_url) {
86: self::registerNavbarItem($title, $title, $parent_url, self::TYPE_LABEL);
87: }
88:
89: 90: 91: 92: 93: 94: 95: 96: 97: 98:
99: public static function registerDivider($parent_url) {
100: $title = "divider-" . uniqid();
101: self::registerNavbarItem($title, $title, $parent_url, self::TYPE_DIVIDER);
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111: 112:
113: public static function setBadge($url, $badge) {
114: if (!isset(self::$navbar[$url])) {
115: throw new NavigationException("You cannot set a badge for the link '$url', because it is not a navbar link.");
116: }
117:
118: self::$navbar[$url]['badge'] = $badge;
119: }
120:
121: 122: 123: 124: 125: 126: 127:
128: public static function setClass($url, $class) {
129: if (!isset(self::$navbar[$url])) {
130: throw new NavigationException("You cannot set a class for the link '$url', because it is not a navbar link.");
131: }
132:
133: self::$navbar[$url]['class'] = $class;
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public static function setContainerClass($url, $container_class) {
144: if (!isset(self::$navbar[$url])) {
145: throw new NavigationException("You cannot set a class for the container of the link '$url', because it is not a navbar link.");
146: }
147:
148: self::$navbar[$url]['container_class'] = $container_class;
149: }
150:
151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163:
164: public static function setContainerDataAttributes($url, $data_attributes) {
165: if (!isset(self::$navbar[$url])) {
166: throw new NavigationException("You cannot set data attributes for the container of the link '$url', because it is not a navbar link.");
167: }
168:
169: if (is_array($data_attributes) or $data_attributes instanceof ArrayAccess) {
170: self::$navbar[$url]['container_data_attributes'] = $data_attributes;
171: } else {
172: throw new NavigationException("Unexpected \$data_attributes type: " . gettype($data_attributes) . " (Expected: array or instanceof ArrayAccess)");
173: }
174: }
175:
176: 177: 178: 179: 180: 181:
182: public static function registerQuickLinkOwner($url) {
183: self::$quicklinks[$url] = array();
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193:
194: public static function registerQuickLink($owner, $url, $title) {
195: if (!isset(self::$quicklinks[$url])) {
196: throw new NavigationException("The page '$owner' does not use Quick Links, and thus no Quick Links can be registered for it.");
197: }
198:
199: self::$quicklinks[$url] = $title;
200: }
201:
202: 203: 204: 205: 206: 207:
208: public static function getNavbarLinks() {
209: $navbar = array();
210: $self_navbar = self::$navbar;
211:
212: $process_children = function($details) use (&$process_children, $self_navbar) {
213: if (count($details['children']) > 0) {
214: $children = array();
215: foreach ($details['children'] as $child) {
216: $child_details = $self_navbar[$child];
217: $child_details['children'] = $process_children($child_details);
218: $children[$child] = $child_details;
219: }
220: return $children;
221: }
222: };
223:
224: foreach (self::$navbar as $url => $details) {
225: if ($details['parent_url'] === null) {
226: $details['children'] = $process_children($details);
227: $navbar[$url] = $details;
228: }
229: }
230:
231: return $navbar;
232: }
233:
234: 235: 236: 237: 238: 239: 240: 241:
242: public static function getQuickLinks($owner) {
243: if (!isset(self::$quicklinks[$owner])) {
244: throw new NavigationException("The page '$owner' does not use Quick Links, and thus it is impossible to get Quick Links for it.");
245: }
246:
247: return self::$quicklinks[$owner];
248: }
249:
250: 251: 252: 253: 254: 255: 256: 257: 258: 259:
260: protected static function registerNavbarItem($url, $title, $parent_url, $type) {
261: if ($parent_url !== null and ! isset(self::$navbar[$parent_url])) {
262: throw new NavigationException("The link '$parent_url' cannot be set as the parent of '$url' because it is not a navbar link.");
263: }
264:
265: self::$navbar[$url] = array(
266: 'title' => $title,
267: 'children' => array(),
268: 'parent_url' => $parent_url,
269: 'badge' => null,
270: 'class' => null,
271: 'container_class' => null,
272: 'type' => $type,
273: 'container_data_attributes' => array(),
274: );
275:
276: if ($parent_url !== null) {
277: self::$navbar[$parent_url]['children'][] = $url;
278: }
279: }
280:
281: }
282: