forked from FriendsOfProcessWire/FieldtypeLeafletMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkupLeafletMap.module
287 lines (254 loc) · 11.5 KB
/
MarkupLeafletMap.module
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php namespace ProcessWire;
/**
* ProcessWire Leaflet Map Markup
* Port of Google Map Markup by Ryan Cramer
* Renders maps for the FieldtypeMapMarker module
*
* ProcessWire 3.x
* Copyright (C) 2013 by Ryan Cramer
* Copyright (C) 2015 by Mats Neander
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
* USAGE:
* ======
*
* Add this somewhere before your closing </head> tag:
*
* <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
* <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
*
* In the location where you want to output your map, do the following in your template file:
*
* $map = $modules->get('MarkupLeafletMap');
* echo $map->render($page, 'map'); // replace 'map' with the name of your LeafletFieldtypeMap field
*
* To render a map with multiple markers on it, specify a PageArray rather than a single $page:
*
* $items = $pages->find("template=something, map!='', sort=title");
* $map = $modules->get('MarkupLeafletMap');
* echo $map->render($items, 'map');
*
* To specify options, provide a 3rd argument with an options array:
*
* $map = $modules->get('MarkupLeafletMap');
* echo $map->render($items, 'map', array('height' => '500px'));
*
*
* OPTIONS
* =======
* Here is a list of all possible options (with defaults shown):
*
* // default width of the map
* 'width' => '100%'
*
* // default height of the map
* 'height' => '300px'
*
* // zoom level
* 'zoom' => 12 (or $field->defaultZoom)
*
* // set to true to automatically adjust maximum zoom level to default zoom
* 'maxZoom' => true
*
* // set to false to disable the scroll wheel zoom
* 'scrollWheelZoom' => true
*
* // map ID attribute
* 'id' => "mgmap"
*
* // map class attribute
* 'class' => "MarkupGoogleMap"
*
* // center latitude
* 'lat' => $field->defaultLat
*
* // center longitude
* 'lng' => $field->defaultLng
*
* // set to false only if you will style the map <div> yourself
* 'useStyles' => true
*
* // field to use for the marker link, or blank to not link
* 'markerLinkField' => 'url'
*
* // field to use for the marker title
* 'markerTitleField' => 'title'
*
* // map will automatically adjust to fit to the given markers (when multiple markers)
* 'fitToMarkers' => true
*
*
* // Any extra javascript initialization code you want to occur before the map itself is drawn
* 'init' => '',
*
*/
class MarkupLeafletMap extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'LeafletMap Markup',
'version' => '3.0.2',
'summary' => 'Renders Leaflet Maps for the LealetMapMarker Fieldtype',
'requires' => 'ProcessWire>=3.0.0, FieldtypeLeafletMapMarker',
'installs' => 'MarkupAddInlineScript',
);
}
/**
* Include our MapMarker class, which serves as the value for fields of type FieldtypeMapMarker
*
*/
public function init() {
$class = $this->classname();
$assetPath = $this->config->urls->$class;
$this->config->styles->add('https://unpkg.com/[email protected]/dist/leaflet.css');
$this->config->styles->add($assetPath . "assets/leaflet-markercluster/MarkerCluster.css");
$this->config->styles->add($assetPath . "assets/leaflet-markercluster/MarkerCluster.Default.css");
$this->config->scripts->add('https://unpkg.com/[email protected]/dist/leaflet.js');
$this->config->scripts->add($assetPath . 'assets/leaflet-markercluster/leaflet.markercluster.js');
$this->config->scripts->add($assetPath . 'assets/leaflet-providers/leaflet-providers.js');
$this->config->scripts->add($assetPath . 'MarkupLeafletMap.js');
require_once(dirname(__FILE__) . '/LeafletMapMarker.php');
}
public function getOptions($fieldName) {
static $n = 0;
$field = $this->fields->get($fieldName);
if(!$field) throw new WireException("Unknown field: $fieldName");
return array(
'useStyles' => true,
'fitToMarkers' => true,
'useMarkerSettings' => true,
'useHoverBox' => false,
'hoverBoxMarkup' => "<div data-top='-10' data-left='15' style='background: #000; color: #fff; padding: 0.25em 0.5em; border-radius: 3px;'></div>",
'markerLinkField' => 'url',
'markerTitleField' => 'title',
'width' => '100%',
'height' => $field->height,
'zoom' => $field->defaultZoom ? (int) $field->defaultZoom : 12,
'type' => $field->defaultType ? $field->defaultType : 'HYBRID',
'id' => "mleafletmap" . (++$n),
'class' => "MarkupLeafletMap",
'lat' => $field->defaultLat,
'lng' => $field->defaultLng,
'provider' => $field->defaultProvider,
'maxZoom' => false,
'scrollWheelZoom' => true,
'icon' => '', // url to icon (blank=use default)
'iconHover' => '', // url to icon when hovered (default=none)
'shadow' => '', // url to icon shadow (blank=use default)
'init' => '', // additional javascript code to insert in map initialization
'n' => $n,
'popupFormatter' => '',
// See https://github.com/lvoogdt/Leaflet.awesome-markers#properties
// for documentation on what values can go in the two lines above (look at the icon and markerColor setting in the docs)
'markerIcon' => 'home',
'markerColour' => 'darkblue',
'markerIconColour' => 'white',
'markerFormatter' => '',
);
}
/**
* Call this method when constucting the HTML header, to inject all the script and style files needed for Leaflet maps.
*/
public function getLeafletMapHeaderLines($fontawesome = true) {
$url = $this->config->urls->siteModules. 'FieldtypeLeafletMapMarker/';
$lines = '';
if ($fontawesome) {
$lines .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$url}assets/font-awesome-4.6.3/css/font-awesome.min.css\">";
}
$lines .= <<<LINES
<!-- Styles supporting the use of Leaflet.js -->
<link rel="stylesheet" type="text/css" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
<link rel="stylesheet" type="text/css" href="{$url}assets/leaflet-markercluster/MarkerCluster.css" />
<link rel="stylesheet" type="text/css" href="{$url}assets/leaflet-markercluster/MarkerCluster.Default.css" />
<!-- Scripts supporting the use of Leaflet.js -->
<script type="text/javascript" src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script type="text/javascript" src="{$url}assets/leaflet-markercluster/leaflet.markercluster.js"></script>
<script type="text/javascript" src="{$url}assets/leaflet-providers/leaflet-providers.js"></script>
<script type="text/javascript" src="{$url}MarkupLeafletMap.js"></script>
<!-- Extend Leaflet with Awesome.Markers -->
<link rel="stylesheet" type="text/css" href="{$url}assets/leaflet-awesome-markers/leaflet.awesome-markers.css" />
<script type="text/javascript" src="{$url}assets/leaflet-awesome-markers/leaflet.awesome-markers.min.js"></script>
LINES;
return $lines;
}
public function render($pageArray, $fieldName, array $options = array()) {
static $n = 0;
$n++;
$defaultOptions = $this->getOptions($fieldName);
$options = array_merge($defaultOptions, $options);
if($pageArray instanceof Page) {
$page = $pageArray;
$pageArray = new PageArray();
$pageArray->add($page);
}
$height = $options['height'];
$width = $options['width'];
if(empty($height)) $height = 300;
if(ctype_digit("$height")) $height .= "px";
if(ctype_digit("$width")) $width .= "px";
$style = '';
if($options['useStyles'] && !empty($height) && !empty($width)) {
$style = " style='width: $width; height: $height;'";
}
$lat = $options['lat'];
$lng = $options['lng'];
$zoom = $options['zoom'] > 0 ? (int) $options['zoom'] : $defaultOptions['zoom'];
//$type = in_array($options['type'], array('ROADMAP', 'SATELLITE', 'HYBRID')) ? $options['type'] : 'HYBRID';
$provider = ($options['provider'] == "") ? 'OpenStreetMap.Mapnik' : $options['provider'];
if($options['useMarkerSettings'] && (count($pageArray) == 1 || !$lat)) {
// single marker overrides lat, lng and zoom settings
$marker = $pageArray->first()->get($fieldName);
$lat = $marker->lat;
$lng = $marker->lng;
if($marker->zoom > 0) $zoom = (int) $marker->zoom;
}
$id = $options['id'];
$out = '';
$out .= "<div id='$id' class='$options[class]'$style></div>";
$inlineScript = "<script type='text/javascript'>\n$(function() {\n" .
"var $id = new jsMarkupLeafletMap();\n" .
"$id.setOption('zoom', $zoom);\n" .
"$id.setOption('scrollWheelZoom', {$options['scrollWheelZoom']});\n" .
$options['init'] . "\n" .
"$id.init('$id', $lat, $lng, '$provider');\n" .
"var default_marker_icon = L.AwesomeMarkers.icon({ icon: '{$options['markerIcon']}', iconColor: '{$options['markerIconColour']}', prefix: 'fa', markerColor: '{$options['markerColour']}' });\n";
foreach($pageArray as $page) {
$marker = $page->get($fieldName);
if(!$marker instanceof LeafletMapMarker) continue;
if(!$marker->lat) continue;
$url = $options['markerLinkField'] ? $page->get($options['markerLinkField']) : '';
$title = $options['markerTitleField'] ? $page->get($options['markerTitleField']) : '';
$popup = '';
if (isset($options['popupFormatter']) && is_callable($options['popupFormatter'])) {
// Need to escape returned single quotes and turn newlines into HTML breaks as this is going into a JS string
$popup = str_replace("'", "\\'", call_user_func($options['popupFormatter'], $page));
$popup = str_replace("\n", '<br/>', $popup);
}
$marker_icon = 'default_marker_icon';
if (isset($options['markerFormatter']) && is_callable($options['markerFormatter'])) {
$marker_options = array(
'prefix' => 'fa',
'icon' => $options['markerIcon'],
'markerColor' => $options['markerColour'],
'iconColor' => $options['markerIconColour'],
);
$marker_options = call_user_func($options['markerFormatter'], $page, $marker_options);
$obj = array();
foreach ($marker_options as $k => $v) {
$obj[] = "$k: '$v'";
}
$marker_options = implode(', ', $obj);
$marker_icon = 'L.AwesomeMarkers.icon({'.$marker_options.'})';
}
$inlineScript .= "$id.addMarkerIcon($marker_icon, $marker->lat, $marker->lng, '$url', '$title', '$popup');\n";
}
if(count($pageArray) > 1) {
if ($options['fitToMarkers']) $inlineScript .= "$id.fitToMarkers();\n";
if ($options['maxZoom']) $inlineScript .= "$id.setMaxZoom();\n";
}
$inlineScript .= "});\n</script>";
$this->page->inlineScript = $inlineScript;
return $out;
}
}