forked from ryancramerdesign/FieldtypeMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldtypeMapMarker.module
276 lines (239 loc) · 7.65 KB
/
FieldtypeMapMarker.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
<?php
/**
* ProcessWire Map Marker Fieldtype
*
* Holds an address and geocodes it to latitude and longitude via Google Maps
*
* For documentation about the fields used in this class, please see:
* /wire/core/Fieldtype.php
*
* ProcessWire 2.x
* Copyright (C) 2016 by Ryan Cramer
* Licensed under MPL 2.0
*
* https://processwire.com
*
* @todo implement a getMatchQuery method and support LIKE with address.
*
* @property string $googleApiKey
*
*/
class FieldtypeMapMarker extends Fieldtype implements ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Map Marker',
'version' => 209,
'summary' => 'Field that stores an address with latitude and longitude coordinates and has built-in geocoding capability with Google Maps API.',
'installs' => 'InputfieldMapMarker',
'icon' => 'map-marker',
);
}
/**
* Include our MapMarker class, which serves as the value for fields of type FieldtypeMapMarker
*
*/
public function __construct() {
require_once(dirname(__FILE__) . '/MapMarker.php');
}
/**
* Return the Inputfield required by this Fieldtype
*
* @param Page $page
* @param Field $field
* @return InputfieldMapMarker
*
*/
public function getInputfield(Page $page, Field $field) {
/** @var InputfieldMapMarker $inputfield */
$inputfield = $this->modules->get('InputfieldMapMarker');
$inputfield->set('googleApiKey', $this->get('googleApiKey'));
return $inputfield;
}
/**
* Return all compatible Fieldtypes
*
* @param Field $field
* @return null
*
*/
public function ___getCompatibleFieldtypes(Field $field) {
// there are no other fieldtypes compatible with this one
return null;
}
/**
* Sanitize value for runtime
*
* @param Page $page
* @param Field $field
* @param MapMarker $value
* @return MapMarker
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
// if it's not a MapMarker, then just return a blank MapMarker
if(!$value instanceof MapMarker) $value = $this->getBlankValue($page, $field);
// if the address changed, tell the $page that this field changed
if($value->isChanged('address')) $page->trackChange($field->name);
return $value;
}
/**
* Get a blank value used by this fieldtype
*
* @param Page $page
* @param Field $field
* @return MapMarker
*
*/
public function getBlankValue(Page $page, Field $field) {
return new MapMarker();
}
/**
* Given a raw value (value as stored in DB), return the value as it would appear in a Page object
*
* @param Page $page
* @param Field $field
* @param string|int|array $value
* @return string|int|array|object $value
*
*/
public function ___wakeupValue(Page $page, Field $field, $value) {
// get a blank MapMarker instance
$marker = $this->getBlankValue($page, $field);
if("$value[lat]" === "0") $value['lat'] = '';
if("$value[lng]" === "0") $value['lng'] = '';
// populate the marker
$marker->address = $value['data'];
$marker->lat = $value['lat'];
$marker->lng = $value['lng'];
$marker->status = $value['status'];
$marker->zoom = $value['zoom'];
$marker->setTrackChanges(true);
return $marker;
}
/**
* Given an 'awake' value, as set by wakeupValue, convert the value back to a basic type for storage in DB.
*
* @param Page $page
* @param Field $field
* @param string|int|array|object $value
* @return string|int
* @throws WireException
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
$marker = $value;
if(!$marker instanceof MapMarker)
throw new WireException("Expecting an instance of MapMarker");
// if the address was changed, then force it to geocode the new address
if($marker->isChanged('address') && $marker->address && $marker->status != MapMarker::statusNoGeocode) $marker->geocode();
$sleepValue = array(
'data' => $marker->address,
'lat' => strlen($marker->lat) ? $marker->lat : 0,
'lng' => strlen($marker->lng) ? $marker->lng : 0,
'status' => $marker->status,
'zoom' => $marker->zoom
);
return $sleepValue;
}
/**
* Return the database schema in specified format
*
* @param Field $field
* @return array
*
*/
public function getDatabaseSchema(Field $field) {
// get the default schema
$schema = parent::getDatabaseSchema($field);
$schema['data'] = "VARCHAR(255) NOT NULL DEFAULT ''"; // address (reusing the 'data' field from default schema)
$schema['lat'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // latitude
$schema['lng'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // longitude
$schema['status'] = "TINYINT NOT NULL DEFAULT 0"; // geocode status
$schema['zoom'] = "TINYINT NOT NULL DEFAULT 0"; // zoom level (schema v1)
$schema['keys']['latlng'] = "KEY latlng (lat, lng)"; // keep an index of lat/lng
$schema['keys']['data'] = 'FULLTEXT KEY `data` (`data`)';
$schema['keys']['zoom'] = "KEY zoom (zoom)";
if($field->id) $this->updateDatabaseSchema($field, $schema);
return $schema;
}
/**
* Update the DB schema, if necessary
*
* @param Field $field
* @param array $schema
*
*/
protected function updateDatabaseSchema(Field $field, array $schema) {
$requiredVersion = 1;
$schemaVersion = (int) $field->get('schemaVersion');
if($schemaVersion >= $requiredVersion) {
// already up-to-date
return;
}
if($schemaVersion == 0) {
// update schema to v1: add 'zoom' column
$schemaVersion = 1;
$database = $this->wire('database');
$table = $database->escapeTable($field->getTable());
$query = $database->prepare("SHOW TABLES LIKE '$table'");
$query->execute();
$row = $query->fetch(\PDO::FETCH_NUM);
$query->closeCursor();
if(!empty($row)) {
$query = $database->prepare("SHOW COLUMNS FROM `$table` WHERE field='zoom'");
$query->execute();
if(!$query->rowCount()) try {
$database->exec("ALTER TABLE `$table` ADD zoom $schema[zoom] AFTER status");
$this->message("Added 'zoom' column to '$field->table'");
} catch(Exception $e) {
$this->error($e->getMessage());
}
}
}
$field->set('schemaVersion', $schemaVersion);
$field->save();
}
/**
* Match values for PageFinder
*
* @param DatabaseQuerySelect $query
* @param string $table
* @param string $subfield
* @param string $operator
* @param string $value
* @return DatabaseQuerySelect
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
if(!$subfield || $subfield == 'address') $subfield = 'data';
if($subfield != 'data' || $this->wire('database')->isOperator($operator)) {
// if dealing with something other than address, or operator is native to SQL,
// then let Fieldtype::getMatchQuery handle it instead
return parent::getMatchQuery($query, $table, $subfield, $operator, $value);
}
// if we get here, then we're performing either %= (LIKE and variations) or *= (FULLTEXT and variations)
$ft = new DatabaseQuerySelectFulltext($query);
$ft->match($table, $subfield, $operator, $value);
return $query;
}
/**
* Module configuration
*
* @param array $data
* @return InputfieldWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
$inputfields = new InputfieldWrapper();
/** @var InputfieldText $f */
$f = wire('modules')->get('InputfieldText');
$f->attr('name', 'googleApiKey');
$f->label = __('Google Maps API Key');
$f->icon = 'map';
$f->description = sprintf(__('[Click here](%s) for instructions from Google on how to obtain an API key.'),
'https://developers.google.com/maps/documentation/javascript/get-api-key');
$f->attr('value', isset($data['googleApiKey']) ? $data['googleApiKey'] : '');
$inputfields->add($f);
return $inputfields;
}
}