-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroupwidget.php
156 lines (124 loc) · 4.6 KB
/
groupwidget.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
<?php
/* Copyright 2015 Francis Meyvis*/
/**
* GroupWidget is a Grav plugin
*
* This plugin facilitates navigation among categorized taxonomy
*
* Licensed under MIT, see LICENSE.
*
* @package GroupWidget
* @version 0.1.0
* @link <https://github.com/aptly-io/grav-plugin-groupwidget>
* @author Francis Meyvis <https://aptly.io/contact>
* @copyright 2015, Francis Meyvis
* @license MIT <http://opensource.org/licenses/MIT>
*/
namespace Grav\Plugin; // use this namespace to avoids bin/gpm fails
use Grav\Common\Grav;
use Grav\Plugin\Event;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Page\Collection;
class GroupWidgetPlugin extends Plugin
{
/** Enable all controls by default*/
const CONTROLS_YAML = 'controls';
const DEFAULT_CONTROLS = ['start', 'prev', 'next', 'last', 'menu', 'label'];
/** Return a list of subscribed events*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/** Initialize the plug-in*/
public function onPluginsInitialized()
{
/* Sommerregen explains this checks if the admin user is active.
* If so, this plug-in disables itself.
* rhukster mentions this is for speedup purposes related to the admin plugin
*/
if ($this->isAdmin()) {
$this->active = false;
} else {
if ($this->config->get('plugins.groupwidget.enabled')) {
// if the plugin is activated, then subscribe to these additional events
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
]);
}
}
}
/** Register the enabled plugin's template PATH*/
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/** Build a collection of pages that belong to the same group*/
private function buildGroupCollection($config)
{
// use taxonomy to build the collection
$taxonomy = $this->grav['taxonomy'];
$filters = (array)$config->get('filters');
$operator = $config->get('filter_combinator', 'or');
$collection = new Collection();
$collection->append($taxonomy->findTaxonomy(
$filters, $operator)->toArray());
// use a configured sorting order
$collection = $collection->order(
$config->get('order.by'),
$config->get('order.dir')
);
return $collection;
}
/** Find index to current page (to get previous and next)*/
private function findCurrentPage($collection, $current)
{
$cnt = 0;
foreach ($collection as $page) {
if ($current->url() == $page->url()) {
break;
}
$cnt++;
}
return $cnt;
}
/** Setup the necessary assets and variables to build the widget*/
public function onTwigSiteVariables()
{
$current_page = $this->grav['page'];
if (isset($current_page->header()->{$this->name})) {
$config = $this->mergeConfig($current_page);
if ($config->get('enabled', false)) {
if ($config->get('built_in_css', false)) {
$this->grav['assets']->addCss(
'plugin://groupwidget/assets/css/groupwidget.css');
}
$vars = array();
$vars['current'] = $current_page;
$vars['controls'] = $config->get(
GroupWidgetPlugin::CONTROLS_YAML, GroupWidgetPlugin::DEFAULT_CONTROLS);
$collection = $this->buildGroupCollection($config);
$idx = $this->findCurrentPage($collection, $current_page);
if (0 < $collection->count()) {
if (0 < $idx) {
$vars['first'] = $collection->first();
}
if (0 < $idx) {
$vars['prev'] = $collection->nth($idx - 1);
}
if ($idx < $collection->count() - 1) {
$vars['next'] = $collection->nth($idx + 1);
}
if ($idx < $collection->count() - 1) {
$vars['last'] = $collection->last();
}
}
$vars['group'] = $collection;
$this->grav['twig']->twig_vars['groupwidget'] = $vars;
}
}
}
}