forked from ryancramerdesign/FieldtypeMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldtypeMapMarker.module
228 lines (190 loc) · 6.39 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
<?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) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
* @todo implement a getMatchQuery method and support LIKE with address.
*
*/
class FieldtypeMapMarker extends Fieldtype {
public static function getModuleInfo() {
return array(
'title' => 'Map Marker',
'version' => 206,
'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
*
*/
public function getInputfield(Page $page, Field $field) {
$inputfield = $this->modules->get('InputfieldMapMarker');
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 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
*
*/
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
*
*/
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
*
*/
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
*
*/
protected function updateDatabaseSchema(Field $field, array $schema) {
$requiredVersion = 1;
$schemaVersion = (int) $field->schemaVersion;
if($schemaVersion >= $requiredVersion) {
// already up-to-date
return;
}
if($schemaVersion == 0) {
// update schema to v1: add 'zoom' column
$schemaVersion = 1;
$result = $this->db->query("SHOW TABLES LIKE '$field->table'");
$row = $result->fetch_row();
if(!empty($row)) {
$result = $this->db->query("SHOW COLUMNS FROM `$field->table` WHERE field='zoom'");
if(!$result->num_rows) try {
$result = $this->db->query("ALTER TABLE `$field->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
*
*/
public function getMatchQuery($query, $table, $subfield, $operator, $value) {
if(!$subfield || $subfield == 'address') $subfield = 'data';
if($subfield != 'data' || wire('db')->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 MapMarker geocoding will not work because 'allow_url_fopen' is denied in your PHP settings.");
}
}
}