-
Notifications
You must be signed in to change notification settings - Fork 0
/
taxonomy-filter.php
183 lines (155 loc) · 5.21 KB
/
taxonomy-filter.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 Grav\Plugin;
use Composer\Autoload\ClassLoader;
use DateTime;
use Grav\Common\Assets;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Page;
use Grav\Common\Plugin;
use Grav\Common\Uri;
use RocketTheme\Toolbox\Event\Event;
/**
* Class TaxonomyFilterPlugin
* @package Grav\Plugin
*/
class TaxonomyFilterPlugin extends Plugin
{
protected array $routes;
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents(): array
{
return [
'onPluginsInitialized' => [
// Uncomment following line when plugin requires Grav < 1.7
// ['autoload', 100000],
['onPluginsInitialized', 0]
]
];
}
/**
* Composer autoload
*
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized(): void
{
// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
return;
}
// Enable the main events we are interested in
if ($this->isOnRoute()) {
$this->enable([
// Put your main events here
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onAssetsInitialized' => ['onAssetsInitialized', 0],
'onCollectionProcessed' => ['onCollectionProcessed', 0],
]);
}
}
/**
* Determine if plugin should be enabled on current route.
*
* @return bool
*/
public function isOnRoute(): bool
{
$lang = $this->grav['language']->getActive();
$path = $this->grav['uri']->rootUrl() ?: '/';
$routes = $this->config->get('plugins.' . $this->name . '.routes');
foreach ($routes as $route) {
['blog' => $blog, 'items' => $items] = $route;
if ($path === $blog || str_starts_with($path, $items)) {
if ($lang) {
$route['blog'] = '/' . $lang . $route['blog'];
$route['items'] = '/' . $lang . $route['items'];
}
$this->routes = $route;
return true;
}
}
return false;
}
/**
* Add current directory to twig lookup paths.
*
* @return void
*/
public function onTwigTemplatePaths(): void
{
$this->grav['twig']->twig_paths[] = __DIR__ . "/templates";
}
/**
* Add Javascript and Css to page
*
* @return void
*/
public function onAssetsInitialized(): void
{
$isProduction = $this->config->get("plugins.$this->name.isProduction", true);
$minified = $isProduction ? '.min' : '';
/** @var Assets */
$assets = $this->grav['assets'];
$assets->addCss("plugin://{$this->name}/css/style$minified.css");
$assets->addJs(
"plugin://{$this->name}/js/taxonomy-filter$minified.js",
['group' => 'head', 'position' => 'after', 'loading' => 'defer']
);
$assets->addInlineJs(
"const taxonomyFilters = " . json_encode($this->routes),
['group' => 'head', 'position' => 'after']
);
}
/**
* Remove items from collection when selected dates do not fall between dates of seminar (if set in page header).
*
* @param Event $event Contains 'collection' and 'context'
*/
public function onCollectionProcessed(Event $event)
{
/** @var Collection */
$collection = $event['collection'];
/** @var Uri */
$uri = $this->grav['uri'];
$paramStart = $uri->param('starts-after');
$paramEnd = $uri->param('ends-before');
if ($paramStart !== false || $paramEnd !== false) {
/** @var Page */
foreach ($collection as $page) {
$header = $page->header();
if ($paramStart && isset($header->daterange['start'])) {
$headerStart = $header->daterange['start'];
// Remove item when selected start date is after start date seminar
$startDate = (new DateTime($paramStart))->getTimestamp();
if ($startDate > $headerStart) {
$collection->remove($page->path());
}
}
if ($paramEnd && isset($header->daterange['end'])) {
$headerEnd = $header->daterange['end'];
$endDate = (new DateTime($paramEnd))->getTimestamp();
// Remove item when selected end date is before end date seminar
if ($endDate < $headerEnd) {
$collection->remove($page->path());
}
}
}
}
}
}