-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproject-space.php
201 lines (189 loc) · 5.42 KB
/
project-space.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
namespace Grav\Theme;
use Grav\Common\Grav;
use Grav\Common\Theme;
use Grav\Plugin\Taxonomylist;
use Grav\Theme\ProjectSpace\InAnyArrayTwigExtension;
use Grav\Theme\ProjectSpace\TruncateTwigExtension;
use Grav\Theme\ProjectSpace\UrlDecoderTwigExtension;
/**
* Project Space Theme
*
* Class ProjectSpace
*
* @category Extensions
* @package Grav\Theme
* @author Ole Vik <[email protected]>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/OleVik/grav-theme-project-space
*/
class ProjectSpace extends Theme
{
/**
* Page parameter-keys
*
* @var array
*/
public $params = [
'display',
'start',
'end',
'sort-by',
'sort-dir',
'limit',
'category',
'color',
'tag'
];
/**
* Initialize plugin and subsequent events
*
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onThemeInitialized' => ['onThemeInitialized', 0],
'onTwigExtensions' => ['onTwigExtensions', 0]
];
}
/**
* Get list of allowed categories from site.yaml
*
* @return array
*/
public static function categories()
{
$options = Grav::instance()['config']->get('themes.project-space.categories');
$categories = array();
if (!empty($options)) {
foreach ($options as $category) {
$categories[$category] = $category;
}
}
return $categories;
}
/**
* Get list of tags
*
* @return array
*/
public static function tags()
{
$taxonomylist = new Taxonomylist();
$tags = array();
if (!empty($taxonomylist->get()['tag'])) {
foreach (array_keys($taxonomylist->get()['tag']) as $tag) {
array_push($tags, array('text' => $tag, 'value' => $tag));
}
}
return $tags;
}
/**
* Get list of allowed categories from site.yaml
*
* @return array
*/
public static function colors()
{
$options = Grav::instance()['config']->get('themes.project-space.colors');
$colors = array();
foreach ($options as $color) {
$colors[$color['name']] = $color['name'];
}
return $colors;
}
/**
* Register events and route with Grav
*
* @return void
*/
public function onThemeInitialized()
{
/* Check if Admin-interface */
if ($this->isAdmin()) {
$this->enable(
[
'onPageInitialized' => ['onAdminPageInitialized', 0]
]
);
} else {
$this->enable(
[
'onPageInitialized' => ['onPageInitialized', 0]
]
);
}
}
/**
* Push styles to Admin-plugin via Assets Manager
*
* @return void
*/
public function onAdminPageInitialized()
{
$this->grav['assets']->addCss('theme://src/admin.css', 1);
$colors = '';
foreach (Grav::instance()['config']->get('themes.project-space.colors') as $color) {
if (!array_key_exists('name', $color) || !array_key_exists('value', $color)) {
continue;
}
$colors .= 'select[name="data[header][color]"] + div.selectize-control.single .option[data-value="' . $color['name'] . '"]:after,';
$colors .= 'select[name="data[header][color]"] + div.selectize-control.single .item[data-value="' . $color['name'] . '"]:after {';
$colors .= 'color:' . $color['value'] . '!important;';
$colors .= '}';
}
$this->grav['assets']->addInlineCss($colors);
}
/**
* Logic for handling tools-menu
*
* @return void
*/
public function onPageInitialized()
{
$output = [];
$sep = $this->grav['config']->get('system.param_sep');
foreach ($this->params as $param) {
if (!empty($_GET[$param])) {
if (in_array($param, array_keys($_GET))) {
if (is_array($_GET[$param])) {
$items = [];
foreach ($_GET[$param] as $item) {
$items[] = urlencode($item);
}
$output[] = $param . $sep . implode(',', array_unique($items));
} else {
$output[] = $param . $sep . $_GET[$param];
}
}
}
}
$params = '/' . implode('/', $output);
$location = rtrim($this->grav['page']->url(true), '/') . $params;
if (isset($_GET['redirect']) && !empty($location)) {
header('Location: ' . $location, true, 307);
exit();
}
}
/**
* Add Twig Extension
*
* @return void
*/
public function onTwigExtensions()
{
$this->grav['twig']->twig->addExtension(new UrlDecoderTwigExtension());
$this->grav['twig']->twig->addExtension(new TruncateTwigExtension());
$this->grav['twig']->twig->addExtension(new InAnyArrayTwigExtension());
}
/**
* Composer autoload.
*
* @return \Composer\Autoload\ClassLoader
*/
public function autoload(): \Composer\Autoload\ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
}