This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MenuTags.php
executable file
·183 lines (143 loc) · 4.13 KB
/
MenuTags.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
namespace Statamic\Addons\Menu;
use Statamic\API\Content;
use Statamic\API\File;
use Statamic\API\Parse;
use Statamic\Extend\Tags;
class MenuTags extends Tags {
private $templateMenu;
private $templateItem;
public function __construct() {
parent::__construct();
$this->templateMenu = $this->getTemplateFile('menu');
$this->templateItem = $this->getTemplateFile('item');
}
/**
* The {{ menu }} tag
*
* @return string|array
*/
public function index() {
$menu = $this->storage->getYAML($this->getParam('src'));
if (count($menu['items']) > 0) {
return $this->buildTree($menu['items'], $menu['locale']);
}
}
/**
* Build menu tree
*
* @param array $items
* @param string $locale
* @return string
*/
protected function buildTree($items, $locale) {
$tree = [];
// set type of list: ol, ul
$type = $this->getParam('type') ? $this->getParam('type') : 'ul';
// set HTML attributes of tree
$attributes = $this->treeAttributes();
foreach ($items as $item) {
$tree[] = $this->buildItem($item, $locale);
}
return Parse::template($this->templateMenu, compact('tree', 'type', 'attributes'));
}
/**
* @param array $item
* @param string $locale
* @return string
*/
protected function buildItem($item, $locale) {
$element = [];
$currentPath = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (isset($item['id'])) {
//load page
$page = Content::find($item['id']);
$page->locale($locale);
$element['link'] = $page->absoluteUrl();
} else {
// add url
$element['link'] = $item['url'];
}
// set active class
if ($element['link'] == $currentPath) {
$item['attributes']['class'] .= $this->getParam('active_class') ? ' ' . $this->getParam('active_class') : ' is-active';
}
// title link
$element['title'] = $item['title'];
// tag attributes
$element['attributes'] = $this->linkAttributes($item['attributes']);
$element['items'] = [];
// sub items
if (count($item['items']) > 0) {
$element['items'] = $this->buildTree($item['items'], $locale);
}
// set list element attributes
$attributes = $this->elementAttributes();
return Parse::template($this->templateItem, ['item' => $element, 'attributes' => $attributes]);
}
/**
* Return a template file from this addon.
*
* @param string $name The name of the html view file
*
* @return string
*/
private function getTemplateFile($name) {
return File::get($this->getDirectory() . "/resources/views/tags/{$name}.html");
}
/**
* Build attributes on tree
*
* @return string
*/
private function treeAttributes() {
$attributes = '';
if ($this->getParam('id')) {
$attributes .= ' id="' . $this->getParam('id') . '"';
}
if ($this->getParam('class')) {
$attributes .= ' class="' . $this->getParam('class') . '"';
}
return $attributes;
}
/**
* Build attributes on list element
*
* @return string
*/
private function elementAttributes() {
$attributes = '';
if ($this->getParam('element_class')) {
$attributes .= ' class="' . $this->getParam('element_class') . '"';
}
return $attributes;
}
/**
* Build attributes on link item
*
* @param array $values
* @return string
*/
private function linkAttributes(array $values) {
$attributes = '';
if ($values['id']) {
$attributes .= ' id="' . $values['id'] . '"';
}
$linkClass = $this->getParam('link_class') ? $this->getParam('link_class') . ' ' : '';
if ($values['class']) {
$attributes .= ' class="' . $linkClass . $values['class'] . '"';
} else {
$attributes .= ' class="' . $linkClass . '"';
}
if ($values['title']) {
$attributes .= ' title="' . $values['title'] . '"';
}
if ($values['target'] != '_self') {
$attributes .= ' target="' . $values['target'] . '"';
}
if ($values['rel']) {
$attributes .= ' rel="' . $values['rel'] . '"';
}
return $attributes;
}
}