/
googlemaps.php
91 lines (81 loc) · 2.46 KB
/
googlemaps.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
<?php
use Herbie\DI;
use Herbie\Hook;
class GooglemapsPlugin
{
/** @var \Herbie\Config */
private static $config;
/**
* @var int
*/
private static $instances = 0;
/**
* @return array
*/
public static function install()
{
self::$config = DI::get('Config');
if ((bool)self::$config->get('plugins.config.googlemaps.twig', false)) {
Hook::attach('twigInitialized', ['GooglemapsPlugin', 'addTwigFunction']);
}
if ((bool)self::$config->get('plugins.config.googlemaps.shortcode', true)) {
Hook::attach('shortcodeInitialized', ['GooglemapsPlugin', 'addShortcode']);
}
}
public static function addTwigFunction($twig)
{
$twig->addFunction(
new Twig_SimpleFunction('googlemaps', ['GooglemapsPlugin', 'googlemapsTwig'], ['is_safe' => ['html']])
);
}
public static function addShortcode($shortcode)
{
$shortcode->add('googlemaps', ['GooglemapsPlugin', 'googlemapsShortcode']);
}
/**
* @param string $id
* @param int $width
* @param int $height
* @param string $type
* @param string $class
* @param int $zoom
* @param string $address
* @return string
*/
public static function googlemapsTwig($id = 'gmap', $width = 600, $height = 450, $type = 'roadmap', $class = 'gmap', $zoom = 15, $address = '')
{
self::$instances++;
$template = self::$config->get(
'plugins.config.googlemaps.template',
'@plugin/googlemaps/templates/googlemaps.twig'
);
return DI::get('Twig')->render($template, [
'id' => $id . '-' . self::$instances,
'width' => $width,
'height' => $height,
'type' => $type,
'class' => $class,
'zoom' => $zoom,
'address' => $address,
'instances' => self::$instances
]);
}
/**
* @param array $options
* @return string
*/
public static function googlemapsShortcode($options)
{
$options = array_merge([
'id' => 'gmap',
'width' => 600,
'height' => 450,
'type' => 'roadmap',
'class' => 'gmap',
'zoom' => 15,
'address' => '',
], $options);
return call_user_func_array(['GooglemapsPlugin', 'googlemapsTwig'], $options);
}
}
GooglemapsPlugin::install();