forked from ryancramerdesign/FieldtypeMapMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypeMapMarker.module
More file actions
161 lines (133 loc) · 4.12 KB
/
FieldtypeMapMarker.module
File metadata and controls
161 lines (133 loc) · 4.12 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
<?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
*
*/
class FieldtypeMapMarker extends Fieldtype {
public static function getModuleInfo() {
return array(
'title' => 'Map Marker',
'version' => 100,
'summary' => 'Field that stores an address with latitude and longitude coordinates and has built-in geocoding capability with Google Maps API.',
);
}
/**
* 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);
// populate the marker
$marker->address = $value['data'];
$marker->lat = $value['lat'];
$marker->lng = $value['lng'];
$marker->status = $value['status'];
$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->geocode();
$sleepValue = array(
'data' => $marker->address,
'lat' => $marker->lat,
'lng' => $marker->lng,
'status' => $marker->status,
);
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['keys']['latlng'] = "KEY latlng (lat, lng)"; // keep an index of lat/lng
return $schema;
}
/**
* 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("MapMarker Geocoding will not work because 'allow_url_fopen' is denied in your PHP settings.");
}
}
}