Skip to content

Commit de51667

Browse files
authored
Merge pull request #18 from patman15/PW3
Raw Data Storage
2 parents c6bda94 + 2f75da4 commit de51667

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

FieldtypeLeafletMapMarker.module

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class FieldtypeLeafletMapMarker extends Fieldtype {
2323
public static function getModuleInfo() {
2424
return array(
2525
'title' => 'Leaflet Map Marker',
26-
'version' => '3.0.2',
26+
'version' => '3.0.3',
2727
'summary' => 'Field that stores an address with latitude and longitude coordinates and has built-in geocoding capability with Leaflet Maps API.',
2828
'installs' => 'InputfieldLeafletMapMarker',
2929
'icon' => 'map-marker',
@@ -103,6 +103,7 @@ class FieldtypeLeafletMapMarker extends Fieldtype {
103103
$marker->lng = $value['lng'];
104104
$marker->status = $value['status'];
105105
$marker->zoom = $value['zoom'];
106+
$marker->raw = $value['raw'];
106107
// $marker->provider = $value['provider'];
107108
$marker->setTrackChanges(true);
108109

@@ -133,7 +134,8 @@ class FieldtypeLeafletMapMarker extends Fieldtype {
133134
'lat' => strlen($marker->lat) ? $marker->lat : 0,
134135
'lng' => strlen($marker->lng) ? $marker->lng : 0,
135136
'status' => $marker->status,
136-
'zoom' => $marker->zoom/*,
137+
'zoom' => $marker->zoom,
138+
'raw' => $marker->raw/*,
137139
'provider' => $marker->provider*/
138140
);
139141

@@ -155,6 +157,7 @@ class FieldtypeLeafletMapMarker extends Fieldtype {
155157
$schema['lng'] = "FLOAT(10,6) NOT NULL DEFAULT 0"; // longitude
156158
$schema['status'] = "TINYINT NOT NULL DEFAULT 0"; // geocode status
157159
$schema['zoom'] = "TINYINT NOT NULL DEFAULT 0"; // zoom level (schema v1)
160+
$schema['raw'] = "TEXT NOT NULL DEFAULT ''"; // raw google geocode data
158161
// $schema['provider'] = "VARCHAR(255) NOT NULL DEFAULT ''";
159162

160163
$schema['keys']['latlng'] = "KEY latlng (lat, lng)"; // keep an index of lat/lng
@@ -201,6 +204,28 @@ class FieldtypeLeafletMapMarker extends Fieldtype {
201204
}
202205
}
203206
}
207+
208+
//PDO update by Patman15
209+
if($schemaVersion == 1) {
210+
// update schema to v2: add 'raw' column
211+
$schemaVersion = 2;
212+
$database = $this->wire('database');
213+
$table = $database->escapeTable($field->getTable());
214+
$query = $database->prepare("SHOW TABLES LIKE '$table'");
215+
$query->execute();
216+
$row = $query->fetch(\PDO::FETCH_NUM);
217+
$query->closeCursor();
218+
if(!empty($row)) {
219+
$query = $database->prepare("SHOW COLUMNS FROM `$table` WHERE field='raw'");
220+
$query->execute();
221+
if(!$query->rowCount()) try {
222+
$database->exec("ALTER TABLE `$table` ADD raw $schema[raw] AFTER zoom");
223+
$this->message("Added 'raw' column to '$field->table'");
224+
} catch(Exception $e) {
225+
$this->error($e->getMessage());
226+
}
227+
}
228+
}
204229

205230
$field->set('schemaVersion', $schemaVersion);
206231
$field->save();

InputfieldLeafletMapMarker.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ var InputfieldLeafletMapMarker = {
3939
var $lng = $map.siblings(".InputfieldLeafletMapMarkerLng").find("input[type=text]");
4040
var $addr = $map.siblings(".InputfieldLeafletMapMarkerAddress").find("input[type=text]");
4141
var $addrJS = $map.siblings(".InputfieldLeafletMapMarkerAddress").find("input[type=hidden]");
42+
var $raw = $map.siblings(".InputfieldLeafletMapMarkerAddress").find("input[name$=_raw]");
4243
var $toggle = $map.siblings(".InputfieldLeafletMapMarkerToggle").find("input[type=checkbox]");
4344
var $zoom = $map.siblings(".InputfieldLeafletMapMarkerZoom").find("input[type=number]");
4445
var $notes = $map.siblings(".notes");
@@ -66,6 +67,7 @@ var InputfieldLeafletMapMarker = {
6667
var r = results[0];
6768
if (r) {
6869
$addr.val(r.name);
70+
$raw.val(JSON.stringify(r.properties));
6971
}
7072
})
7173
});
@@ -78,6 +80,7 @@ var InputfieldLeafletMapMarker = {
7880
var r = results[0];
7981
if (r) {
8082
$addr.val(r.name);
83+
$raw.val(JSON.stringify(r.properties));
8184
}
8285
})
8386
});
@@ -89,6 +92,7 @@ var InputfieldLeafletMapMarker = {
8992
$lat.val(result.center.lat);
9093
$lng.val(result.center.lng);
9194
$addr.val(result.name);
95+
$raw.val(JSON.stringify(result.properties));
9296
};
9397

9498

@@ -102,6 +106,7 @@ var InputfieldLeafletMapMarker = {
102106
var r = results[0];
103107
if (r) {
104108
$addr.val(r.name);
109+
$raw.val(JSON.stringify(r.properties));
105110
}
106111
})
107112
});

InputfieldLeafletMapMarker.module

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class InputfieldLeafletMapMarker extends Inputfield {
2424
public static function getModuleInfo() {
2525
return array(
2626
'title' => 'Leaflet Map Marker',
27-
'version' => '3.0.2',
27+
'version' => '3.0.3',
2828
'summary' => "Provides input for the LeafletMapMarker Fieldtype",
2929
'requires' => 'ProcessWire>=3.0.0, FieldtypeLeafletMapMarker',
3030
'icon' => 'map-marker',
@@ -127,6 +127,7 @@ class InputfieldLeafletMapMarker extends Inputfield {
127127
<input readonly type='text' id='{$id}' name='{$name}' value='{$address}' /><br />
128128
</label>
129129
<input type='hidden' id='_{$name}_js_geocode_address' name='_{$name}_js_geocode_address' value='' />
130+
<input type='hidden' id='_{$id}_raw' name='_{$name}_raw' value='{$marker->raw}' />
130131
</p>
131132

132133

@@ -205,6 +206,8 @@ _OUT;
205206
if(is_null($status)) $marker->set('status', LeafletMapMarker::statusNoGeocode); // disable geocode
206207
else $marker->set('status', (int) $status);
207208

209+
$raw = $input["_{$name}_raw"];
210+
$marker->set('raw', $raw);
208211
// $provider = $input["_{$name}_provider"];
209212
// $marker->set('provider', $provider);
210213

@@ -307,7 +310,8 @@ _OUT;
307310
"\$page->{$this->name}->address\n" .
308311
"\$page->{$this->name}->lat\n" .
309312
"\$page->{$this->name}->lng\n" .
310-
"\$page->{$this->name}->zoom" .
313+
"\$page->{$this->name}->zoom\n" .
314+
"\$page->{$this->name}->raw" .
311315
"</pre>";
312316

313317
$inputfields->add($field);

LeafletMapMarker.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function __construct() {
3636
$this->set('status', 0);
3737
$this->set('zoom', 0);
3838
$this->set('provider', '');
39+
$this->set('raw', '');
3940
// temporary runtime property to indicate the geocode should be skipped
4041
$this->set('skipGeocode', false);
4142
}
@@ -79,7 +80,7 @@ public function geocode() {
7980
return 0;
8081
}
8182

82-
$url = "http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . urlencode($this->address);
83+
$url = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=" . urlencode($this->address);
8384
$json = file_get_contents($url);
8485
$json = json_decode($json, true);
8586

@@ -89,6 +90,7 @@ public function geocode() {
8990
else $this->status = -1;
9091
$this->lat = 0;
9192
$this->lng = 0;
93+
$this->raw = '';
9294
return $this->status;
9395
}
9496

@@ -98,6 +100,7 @@ public function geocode() {
98100

99101
$this->lat = $location['lat'];
100102
$this->lng = $location['lng'];
103+
$this->raw = json_encode($json['results'][0]);
101104

102105
$statusString = $json['status'] . '_' . $locationType;
103106
$status = array_search($statusString, $this->geocodeStatuses);

0 commit comments

Comments
 (0)