forked from ryancramerdesign/FieldtypeMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputfieldMapMarker.module
More file actions
298 lines (250 loc) · 9.33 KB
/
InputfieldMapMarker.module
File metadata and controls
298 lines (250 loc) · 9.33 KB
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
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* ProcessWire Map Marker Inputfield
*
* Provides the admin control panel inputs for FieldtypeMapMarker
*
* For documentation about the fields used in this class, please see:
* /wire/core/Fieldtype.php
*
* ProcessWire 2.x
* Copyright (C) 2013 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class InputfieldMapMarker extends Inputfield {
public static function getModuleInfo() {
return array(
'title' => 'Map Marker',
'version' => 207,
'summary' => "Provides input for the MapMarker Fieldtype",
'requires' => 'FieldtypeMapMarker',
'icon' => 'map-marker',
);
}
const defaultAddr = 'Castaway Cay';
/**
* Just in case this Inputfield is being used separately from FieldtypeMapmarker, we include the MapMarker class
*
*/
public function __construct() {
require_once(dirname(__FILE__) . '/MapMarker.php');
$this->set('defaultAddr', self::defaultAddr);
$this->set('defaultZoom', 12);
$this->set('defaultType', 'HYBRID');
$this->set('defaultLat', '');
$this->set('defaultLng', '');
$this->set('height', 300);
parent::__construct();
}
/**
* Initialize the MapMarker Inputfield
*
* We require Google Maps API for map display, so we add it the scripts that will be loaded in PW admin
*
*/
public function init() {
$this->config->scripts->add(($this->config->https ? 'https' : 'http') . '://maps.google.com/maps/api/js?sensor=false');
return parent::init();
}
/**
* Set an attribute to this Inputfield
*
* In this case, we just capture the 'value' attribute and make sure it's something valid
*
*/
public function setAttribute($key, $value) {
if($key == 'value' && !$value instanceof MapMarker && !is_null($value)) {
throw new WireException("This input only accepts a MapMarker for it's value");
}
return parent::setAttribute($key, $value);
}
public function isEmpty() {
return (!$this->value || ((float) $this->value->lat) === 0.0);
}
/**
* Render the markup needed to draw the Inputfield
*
*/
public function ___render() {
$name = $this->attr('name');
$id = $this->attr('id');
$marker = $this->attr('value');
if($marker->lat == 0.0) $marker->lat = $this->defaultLat;
if($marker->lng == 0.0) $marker->lng = $this->defaultLng;
if(!$marker->zoom) $marker->zoom = $this->defaultZoom;
$address = htmlentities($marker->address, ENT_QUOTES, "UTF-8");
$toggleChecked = $marker->status != MapMarker::statusNoGeocode ? " checked='checked'" : '';
$status = $marker->status == MapMarker::statusNoGeocode ? 0 : $marker->status;
$mapType = $this->defaultType;
$height = $this->height ? (int) $this->height : 300;
$labels = array(
'addr' => $this->_('Address'),
'lat' => $this->_('Latitude'),
'lng' => $this->_('Longitude'),
'geo' => $this->_('Geocode?'),
'zoom' => $this->_('Zoom')
);
$out = <<< _OUT
<span></span>
<p class='InputfieldMapMarkerAddress'>
<label>
<strong>$labels[addr]</strong>
<br />
<input type='text' id='{$id}' name='{$name}' value='{$address}' /><br />
</label>
<input type='hidden' id='_{$name}_js_geocode_address' name='_{$name}_js_geocode_address' value='' />
</p>
<p class='InputfieldMapMarkerToggle'>
<label>
<br />
<input title='Geocode ON/OFF' type='checkbox' name='_{$name}_status' id='_{$name}_toggle' value='$status'$toggleChecked />
<strong>$labels[geo]</strong>
</label>
</p>
<p class='InputfieldMapMarkerLat'>
<label>
<strong>$labels[lat]</strong><br />
<input type='text' id='_{$id}_lat' name='_{$name}_lat' value='{$marker->lat}' />
</label>
</p>
<p class='InputfieldMapMarkerLng'>
<label>
<strong>$labels[lng]</strong><br />
<input type='text' id='_{$id}_lng' name='_{$name}_lng' value='{$marker->lng}' />
</label>
</p>
<p class='InputfieldMapMarkerZoom'>
<label>
<strong>$labels[zoom]</strong><br />
<input type='number' min='0' id='_{$id}_zoom' name='_{$name}_zoom' value='{$marker->zoom}' />
</label>
</p>
_OUT;
$out .= "<div class='InputfieldMapMarkerMap' " .
"id='_{$id}_map' " .
"style='height: {$height}px' " .
"data-lat='$marker->lat' " .
"data-lng='$marker->lng' " .
"data-zoom='$marker->zoom' " .
"data-type='$mapType'>" .
"</div>";
$this->notes = $marker->statusString;
return $out;
}
/**
* Process the input after a form submission
*
*/
public function ___processInput(WireInputData $input) {
$name = $this->attr('name');
$marker = $this->attr('value');
if(!isset($input->$name)) return $this;
if($input->$name == $this->defaultAddr) {
$marker->set('address', '');
} else {
$marker->set('address', $input->$name);
}
$lat = $input["_{$name}_lat"];
$lng = $input["_{$name}_lng"];
$precision = 4;
if( ((string) round($lat, $precision)) != ((string) round($this->defaultLat, $precision)) ||
((string) round($lng, $precision)) != ((string) round($this->defaultLng, $precision))) {
$marker->set('lat', $lat);
$marker->set('lng', $lng);
} else {
// $this->message("Kept lat/lng at unset value", Notice::debug);
}
$zoom = $input["_{$name}_zoom"];
if($zoom > -1 && $zoom < 30) $marker->zoom = (int) $zoom;
$status = $input["_{$name}_status"];
if(is_null($status)) $marker->set('status', MapMarker::statusNoGeocode); // disable geocode
else $marker->set('status', (int) $status);
// if the address changed, then redo the geocoding.
// while we do this in the Fieldtype, we also do it here in case this Inputfield is used on it's own.
// the MapMarker class checks to make sure it doesn't do the same geocode twice.
if($marker->isChanged('address') && $marker->address && $marker->status != MapMarker::statusNoGeocode) {
// double check that the address wasn't already populated by the JS geocoder
// this prevents user-dragged markers that don't geocode to an exact location from getting
// unintentionally moved by the PHP-side geocoder
if($input["_{$name}_js_geocode_address"] == $marker->address) {
// prevent the geocoder from running in the fieldtype
$marker->skipGeocode = true;
$this->message('Skipping geocode (already done by JS geocoder)', Notice::debug);
} else {
$marker->geocode();
}
}
return $this;
}
public function ___getConfigInputfields() {
$inputfields = parent::___getConfigInputfields();
$field = $this->modules->get('InputfieldText');
$field->attr('name', 'defaultAddr');
$field->label = $this->_('Default Address');
$field->description = $this->_('This will be geocoded to become the starting point of the map.');
$field->attr('value', $this->defaultAddr);
$field->notes = $this->_('When modifying the default address, please make the Latitude and Longitude fields below blank, which will force the system to geocode your new address.');
$inputfields->add($field);
if(!$this->defaultLat && !$this->defaultLng) {
$m = new MapMarker();
$m->address = $this->defaultAddr;
$status = $m->geocode();
if($status > 0) {
$this->defaultLat = $m->lat;
$this->defaultLng = $m->lng;
$this->message($this->_('Geocoded your default address. Please hit save once again to commit the new default latitude and longitude.'));
}
}
$field = $this->modules->get('InputfieldText');
$field->attr('name', 'defaultLat');
$field->label = $this->_('Default Latitude');
$field->attr('value', $this->defaultLat);
$field->columnWidth = 50;
$inputfields->add($field);
$field = $this->modules->get('InputfieldText');
$field->attr('name', 'defaultLng');
$field->label = $this->_('Default Longitude');
$field->attr('value', $this->defaultLng);
$field->columnWidth = 50;
$inputfields->add($field);
$field = $this->modules->get('InputfieldRadios');
$field->attr('name', 'defaultType');
$field->label = $this->_('Default Map Type');
$field->addOption('HYBRID', $this->_('Hybrid'));
$field->addOption('ROADMAP', $this->_('Road Map'));
$field->addOption('SATELLITE', $this->_('Satellite'));
$field->attr('value', $this->defaultType);
$field->optionColumns = 1;
$field->columnWidth = 50;
$inputfields->add($field);
$field = $this->modules->get('InputfieldInteger');
$field->attr('name', 'height');
$field->label = $this->_('Map Height (in pixels)');
$field->attr('value', $this->height);
$field->attr('type', 'number');
$field->columnWidth = 50;
$inputfields->add($field);
$field = $this->modules->get('InputfieldInteger');
$field->attr('name', 'defaultZoom');
$field->label = $this->_('Default Zoom');
$field->description = $this->_('Enter a value between 1 and 23. The highest zoom level is typically somewhere between 19-23, depending on the location being zoomed and how much data Google has for the location.'); // Zoom level description
$field->attr('value', $this->defaultZoom);
$field->attr('type', 'number');
$inputfields->add($field);
$field = $this->modules->get('InputfieldMarkup');
$field->label = $this->_('API Notes');
$field->description = $this->_('You can access individual values from this field using the following from your template files:');
$field->value =
"<pre>" .
"\$page->{$this->name}->address\n" .
"\$page->{$this->name}->lat\n" .
"\$page->{$this->name}->lng\n" .
"\$page->{$this->name}->zoom" .
"</pre>";
$inputfields->add($field);
return $inputfields;
}
}