-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.php
223 lines (192 loc) · 5.95 KB
/
view.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use \Grav\Common\Grav;
use \Grav\Common\Page;
use Grav\Common\Twig\Twig;
use RocketTheme\Toolbox\Event\Event;
use \Symfony\Component\Yaml\Yaml as YamlParser;
class ViewPlugin extends Plugin
{
/**
* @var Page\Page
*/
private $reference;
/**
* Implements 'getSubscribedEvents' event.
*
* - Assigns plugin listeners.
*
* @return array
*/
public static function getSubscribedEvents()
{
// Assign listeners.
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onGetPageTemplates' => ['onGetPageTemplates', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigPageVariables' => ['onTwigPageVariables', 0],
'onPageProcessed' => ['onPageProcessed', 0]
];
}
/**
* Implements 'onPluginsInitialized' event.
*
* - Set plugin as active.
*/
public function onPluginsInitialized()
{
// Plugin always active.
$this->active = true;
return;
}
/**
* Implements 'onTwigTemplatePaths' event.
*
* - Add twig paths to instance.
*/
public function onTwigTemplatePaths()
{
// Add current directory to twig lookup paths.
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
/**
* Implements 'onGetPageTemplates' event.
*
* - Add blueprints & templates to instance.
*
* @param $event
*/
public function onGetPageTemplates(Event $event)
{
/* @var Page\Types $types */
$types = $event->types;
$locator = Grav::instance()['locator'];
// Set blueprints & templates.
$types->scanBlueprints($locator->findResource('plugin://' . $this->name . '/blueprints'));
$types->scanTemplates($locator->findResource('plugin://' . $this->name . '/templates'));
}
/**
* Implements 'onTwigPageVariables' event.
*
* - Set view vars to page header.
*
* @param $event
*/
public function onTwigPageVariables(Event $event)
{
/** @var Page\Page $page */
$page = $event['page'];
/** @var Twig $twig */
$twig = $this->grav['twig'];
// Exit if no view in page header.
if (!isset($page->header()->view)) {
return;
}
// Merge config.
$config = $this->mergeConfig($page);
// Parse and set params to page header.
$page->header()->view['params'] = $this->getParams($page);
// Set twig vars.
$twig->twig_vars['view']['collection'] = $this->getCollection($page);
$twig->twig_vars['view']['template'] = $config->get('template');
}
/**
* Get and parse params from page header.
*
* @param $page
*
* @return array|string
*/
private function getParams(Page\Page $page)
{
$params = array();
// Check for params in page header.
if (isset($page->header()->view['params'])) {
// Try to convert Yaml.
try {
$params = (array) YamlParser::parse($page->header()->view['params']);
}
// Else throw warning.
catch(\Exception $e) {
$this->grav['messages']->add('Caught exception: ' . $e->getMessage() . "\n");
}
}
// Items are needed. Get page children by default.
if (!isset($params['items'])) {
$params['items'] = '@self.children';
}
return $params;
}
/**
* Get and parse view collection from page header.
*
* @param $page
*
* @return mixed
*/
private function getCollection(Page\Page $page)
{
// Get vars.
$reference = isset($page->header()->view['reference']) ? $page->header()->view['reference'] : '/';
$params = isset($page->header()->view['params']) ? $page->header()->view['params'] : 'content';
$filter = isset($page->header()->view['filter']) ? $page->header()->view['filter'] : false;
$pagination = is_array($params) && !empty($params['pagination']) ? $params['pagination'] : false;
// Check if reference root.
if ($reference !== '/') {
// Set the reference page, used for filtering.
$this->reference = $page->find($reference);
/* @var Page\Collection $collection */
$collection = $this->reference->collection($params, $pagination);
} else {
/* @var Page\Collection $collection */
$collection = $page->collection($params, $pagination);
}
// Remove parent pages from page collection.
if ($collection && $filter) {
/* @var Page\Collection $collection */
$collection = $collection->filter(array($this, 'filter'));
}
return $collection;
}
/**
* Implements 'onPageProcessed' event.
*
* - Sets parent page header pagination to true, enabling the pagination
* plugin to run for this page.
*
* @param $event
*/
public function onPageProcessed(Event $event)
{
/* @var Page\Page $page */
$page = $event['page'];
// If page is a view.
if ('modular/view' == $page->value('name')) {
$params = $this->getParams($page);
if (isset($params['pagination']) && $params['pagination']) {
$page->parent()->modifyHeader('pagination', true);
}
}
}
/**
* Filter view collection result.
*
* @param $value
* @param $key
*
* @return bool
*/
public function filter($value, $key)
{
/* @var Page\Collection $children */
$children = $this->reference->children();
// If key is not in reference page collection, filter it from results.
if ($children->offsetGet($key)) {
return true;
} else {
return false;
}
}
}