-
Notifications
You must be signed in to change notification settings - Fork 15
/
FieldtypeLeafletMapMarker.module
261 lines (224 loc) · 8.95 KB
/
FieldtypeLeafletMapMarker.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
<?php namespace ProcessWire;
/**
* ProcessWire Leaflet Map Marker Fieldtype
*
* Holds an address and geocodes it to latitude and longitude via Leaflet Maps
*
* For documentation about the fields used in this class, please see:
* /wire/core/Fieldtype.php
*
* ProcessWire 3.x
* Copyright (C) 2015 by Mats Neander
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
*
* @todo implement a getMatchQuery method and support LIKE with address.
*
*/
class FieldtypeLeafletMapMarker extends Fieldtype {
public static function getModuleInfo() {
return array(
'title' => 'Leaflet Map Marker',
'version' => '3.0.4',
'summary' => 'Field that stores an address with latitude and longitude coordinates and has built-in geocoding capability with Leaflet Maps API.',
'installs' => 'InputfieldLeafletMapMarker',
'icon' => 'map-marker',
'requires' => 'ProcessWire>=3.0.0',
);
}
/**
* Include our LeafletMapMarker class, which serves as the value for fields of type FieldtypeLeafletMapMarker
*
*/
public function __construct() {
require_once(dirname(__FILE__) . '/LeafletMapMarker.php');
}
/**
* Return the Inputfield required by this Fieldtype
*
*/
public function getInputfield(Page $page, Field $field) {
$inputfield = $this->modules->get('InputfieldLeafletMapMarker');
return $inputfield;
}
/**
* Return all compatible Fieldtypes
*
*/
public function ___getCompatibleFieldtypes(Field $field) {
// there are no other fieldtypes compatible with this one
return null;
}
/**
* Sanitize value for runtime
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
// if it's not a LeafletMapMarker, then just return a blank LeafletMapMarker
if(!$value instanceof LeafletMapMarker) $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
*
*/
public function getBlankValue(Page $page, Field $field) {
return new LeafletMapMarker();
}
/**
* 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 LeafletMapMarker 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->raw = $value['raw'];
// $marker->provider = $value['provider'];
$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
*
*/
public function ___sleepValue(Page $page, Field $field, $value) {
$marker = $value;
if(!$marker instanceof LeafletMapMarker)
throw new WireException("Expecting an instance of LeafletMapMarker");
// if the address was changed, then force it to geocode the new address
if($marker->isChanged('address') && $marker->address && $marker->status != LeafletMapMarker::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,
'raw' => $marker->raw/*,
'provider' => $marker->provider*/
);
return $sleepValue;
}
/**
* Return the database schema in specified format
*
*/
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['raw'] = "TEXT NOT NULL DEFAULT ''"; // raw google geocode data
// $schema['provider'] = "VARCHAR(255) NOT NULL DEFAULT ''";
$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
*
*/
protected function updateDatabaseSchema(Field $field, array $schema) {
// increment the requiredVersion number with each new database schema change
$requiredVersion = 2;
$schemaVersion = (int) $field->schemaVersion;
if($schemaVersion >= $requiredVersion) {
// already up-to-date
return;
}
//PDO update by Ryan
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());
}
}
}
//PDO update by Patman15
if($schemaVersion == 1) {
// update schema to v2: add 'raw' column
$schemaVersion = 2;
$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='raw'");
$query->execute();
if(!$query->rowCount()) try {
$database->exec("ALTER TABLE `$table` ADD raw $schema[raw] AFTER zoom");
$this->message("Added 'raw' column to '$field->table'");
} catch(Exception $e) {
$this->error($e->getMessage());
}
}
}
$field->set('schemaVersion', $schemaVersion);
$field->save();
}
/**
* Match values for PageFinder
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
if(!$subfield || $subfield == 'address') $subfield = 'data';
if($subfield != 'data' || 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;
}
/**
* Perform installation: check that this fieldtype can be used with geocoding and warn them if not.
*
*/
public function ___install() {
if(!ini_get('allow_url_fopen')) {
$this->error("Some parts of LeafletMapMarker geocoding will not work because 'allow_url_fopen' is denied in your PHP settings.");
}
}
}