This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimplemap.php
106 lines (84 loc) · 2.99 KB
/
simplemap.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
<?php
class SimplemapField extends InputField {
public $type = 'simplemap';
static public $assets = array(
'css' => array('map.css'),
'js' => array('map.js')
);
public function get_field_data() {
if (!empty($this->map)) {
$mapSettings = $this->map;
} else {
$mapSettings = array(
'zoom' => 2,
'center' => array(
'lat' => 0,
'lng' => 0
)
);
}
if (!empty($this->value)) {
$parsed = (array)yaml::decode($this->value); // get saved values
$mapSettings['zoom'] = $parsed['zoom'];
$mapSettings['center'] = array(
'lat' => $parsed['lat'],
'lng' => $parsed['lng']
);
}
$data = array(
'key' => c::get('google.maps.key'),
'lang' => panel()->translation()->code(),
'map' => $mapSettings
);
if (!empty($this->marker)) {
$data['marker'] = $this->marker;
}
if (!empty($this->style)) {
$styleJson = c::get('google.maps.styles.' . $this->style);
if ($styleJson) {
$data['style'] = $styleJson;
}
}
return $data;
}
public function content() {
$fieldData = $this->get_field_data();
$field = new Brick('div');
$field->addClass('field-content');
if (empty($fieldData['key'])) {
$field->append('Missing Google Maps API key config setting.');
return $field;
}
$field->data('simplemap', json_encode($fieldData, JSON_NUMERIC_CHECK));
$field->data('field', 'kirbySimplemap'); // jQuery plugin, defined in assets/js/map.js
$map = new Brick('div');
$map->addClass('simplemap-canvas input');
if ($this->readonly() || $this->disabled()) {
$map->addClass('is-disabled');
}
if (!empty($this->ratio)) {
$ratioParts = explode(':', $this->ratio);
if (count($ratioParts) == 2 && $ratioParts[0] > 0) {
$paddingValue = ($ratioParts[1] / $ratioParts[0]) * 100;
$map->attr('style', "padding-top: $paddingValue%;");
}
}
$field->append($map);
$field->append($this->create_input('lat', $fieldData['map']['center']['lat']));
$field->append($this->create_input('lng', $fieldData['map']['center']['lng']));
$field->append($this->create_input('zoom', $fieldData['map']['zoom']));
return $field;
}
public function create_input($key, $value) {
$input = new Brick('input');
$input->attr('type', 'hidden');
$input->attr('name', $this->name() . "[$key]");
$input->addClass("simplemap-$key");
$input->val($value);
return $input;
}
public function result() {
$input = parent::result();
return yaml::encode($input);
}
}