/
leaflet.php
87 lines (70 loc) · 2.26 KB
/
leaflet.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
<?php namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Data\Data;
use Grav\Common\Page\Page;
class LEafletPlugin extends Plugin
{
private static $instances = 0;
private $template_html = 'plugins/leaflet/leaflet.html.twig';
private $template_vars = [];
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin()) {
$this->active = false;
return;
}
$this->enable([
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]
]);
}
public function onTwigInitialized()
{
$this->grav['twig']->twig()->addFunction(
new \Twig_SimpleFunction('leaflet', [$this, 'leafletFunction'], ['is_safe' => ['html']])
);
}
public function onTwigTemplatePaths()
{
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}
public function leafletFunction($params = [])
{
$page = $this->grav['page'];
$this->mergePluginConfig($page, $params);
$this->template_vars = [
'id' => $this->config->get('id') . '-' . self::$instances,
'width' => $this->config->get('width'),
'height' => $this->config->get('height'),
'class' => $this->config->get('class'),
'lat' => $this->config->get('lat'),
'lng' => $this->config->get('lng'),
'lat_marker' => $this->config->get('lat_marker'),
'lng_marker' => $this->config->get('lng_marker'),
'zoom' => $this->config->get('zoom'),
'address' => $this->config->get('address'),
'instances' => self::$instances
];
$output = $this->grav['twig']->twig()->render($this->template_html, $this->template_vars);
self::$instances++;
return $output;
}
private function mergePluginConfig(Page $page, $params = [])
{
$this->config = new Data((array) $this->grav['config']->get('plugins.leaflet'));
if (isset($page->header()->leaflet)) {
if (is_array($page->header()->leaflet)) {
$this->config = new Data(array_replace_recursive($this->config->toArray(), $page->header()->leaflet));
} else {
$this->config->set('enabled', $page->header()->leaflet);
}
}
$this->config = new Data(array_replace_recursive($this->config->toArray(), $params));
}
}