1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* Map Location Object |
6
|
|
|
* onBeforeWrite, it automagically adds all the details. |
7
|
|
|
* |
8
|
|
|
* To create a new GoogleMapLocationsObject |
9
|
|
|
* set the Address field and write. All other fields |
10
|
|
|
* are completed automatically... |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
class GoogleMapLocationsObject extends DataObject |
15
|
|
|
{ |
16
|
|
|
private static $parent_point_counts = array(); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* e.g. Page / home Page / Product Page / My Page |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private static $singular_name = 'Location'; |
|
|
|
|
23
|
|
|
public function i18n_singular_name() |
24
|
|
|
{ |
25
|
|
|
return self::$singular_name; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* e.g. Pages / home Pages / Product Pages / My Pages |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private static $plural_name = 'Locations'; |
|
|
|
|
34
|
|
|
public function i18n_plural_name() |
35
|
|
|
{ |
36
|
|
|
return self::$plural_name; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private static $db = array( |
|
|
|
|
40
|
|
|
'PointType' =>'Enum("none, point, polyline, polygon", "point")', |
41
|
|
|
'Accuracy' => 'Varchar(100)', |
42
|
|
|
'Longitude' => 'Double(12,7)', |
43
|
|
|
'Latitude' => 'Double(12,7)', |
44
|
|
|
'PointString' => 'Text', |
45
|
|
|
'Address' => 'Text', |
46
|
|
|
'FullAddress' => 'Text', |
47
|
|
|
'CountryNameCode' => 'Varchar(3)', |
48
|
|
|
'AdministrativeAreaName' => 'Varchar(255)', |
49
|
|
|
'SubAdministrativeAreaName' => 'Varchar(255)', |
50
|
|
|
'LocalityName' => 'Varchar(255)', |
51
|
|
|
'PostalCodeNumber' => 'Varchar(30)', |
52
|
|
|
'Manual' => 'Boolean', |
53
|
|
|
'CustomPopUpWindowTitle' => "Varchar(50)", |
54
|
|
|
'CustomPopUpWindowInfo' => "HTMLText(255)" |
55
|
|
|
//'GeoPointField' => 'GeoPoint', |
56
|
|
|
//'GeoPolygonField' => 'GeoPolygon', |
57
|
|
|
//'GeoLineString' => 'GeoLineString' |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
private static $summary_fields = array( |
|
|
|
|
61
|
|
|
'FullAddress' => "FullAddress", |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
private static $has_one = array( |
|
|
|
|
65
|
|
|
'Parent' => 'SiteTree' |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
private static $indexes = array( |
|
|
|
|
69
|
|
|
"Latitude" => true, |
70
|
|
|
"Longitude" => true |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
private static $field_labels = array( |
|
|
|
|
74
|
|
|
'PointType' =>'Marker Type', |
75
|
|
|
'Accuracy' => 'Accuracy', |
76
|
|
|
'Longitude' => 'Longitude', |
77
|
|
|
'Latitude' => 'Latitude', |
78
|
|
|
'PointString' => 'PointString', |
79
|
|
|
'Address' => 'Searched For Address', |
80
|
|
|
'FullAddress' => 'Found Address', |
81
|
|
|
'CountryNameCode' => 'Country Code', |
82
|
|
|
'AdministrativeAreaName' => 'Main Area', |
83
|
|
|
'SubAdministrativeAreaName' => 'Sub Area', |
84
|
|
|
'LocalityName' => 'Locality', |
85
|
|
|
'PostalCodeNumber' => 'Postal Code', |
86
|
|
|
'Manual' => 'Set Details Manually', |
87
|
|
|
'CustomPopUpWindowTitle' => "Marker Title", |
88
|
|
|
'CustomPopUpWindowInfo' => "Marker Description" |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
private static $casting = array( |
|
|
|
|
92
|
|
|
"ParentData" => "SiteTree", |
93
|
|
|
"AjaxInfoWindowLink" => "HTMLText", |
94
|
|
|
"ParentClassName" => "Varchar", |
95
|
|
|
"Link" => "Varchar" |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Provides MySQL snippet to work out distance between GoogleMapLocationsObject and location |
100
|
|
|
* The method returns a string for use in queries |
101
|
|
|
* The query snippet returns the distance between the GoogleMapLocationsObject and the latitude and longitude provided |
102
|
|
|
* NOTE: 6378.137 is the radius of the earth in kilometers |
103
|
|
|
* @param Double $lng - longitude of location |
104
|
|
|
* @param Double $lat - latitude of location |
105
|
|
|
* |
106
|
|
|
* @return String |
107
|
|
|
*/ |
108
|
|
|
public static function radius_definition($lng, $lat) |
109
|
|
|
{ |
110
|
|
|
return "( 6378.137 * ACOS( COS( RADIANS(".$lat.") ) * COS( RADIANS( \"GoogleMapLocationsObject\".\"Latitude\" ) ) * COS( RADIANS( \"GoogleMapLocationsObject\".\"Longitude\" ) - RADIANS(".$lng.") ) + SIN( RADIANS(".$lat.") ) * SIN( RADIANS( \"GoogleMapLocationsObject\".\"Latitude\" ) ) ) )"; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function radius_definition_other_table($lng, $lat, $table, $latitudeField, $longitudeField) |
114
|
|
|
{ |
115
|
|
|
$str = self::radius_definition($lng, $lat); |
116
|
|
|
$str = str_replace("\"GoogleMapLocationsObject\"", "\"$table\"", $str); |
117
|
|
|
$str = str_replace("\"Latitude\"", "\"$latitudeField\"", $str); |
118
|
|
|
$str = str_replace("\"Longitude\"", "\"$longitudeField\"", $str); |
119
|
|
|
return $str; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Int $lng |
124
|
|
|
* @param Int $lat |
125
|
|
|
* |
126
|
|
|
* return GoogleMapLocationsObject | Null |
127
|
|
|
*/ |
128
|
|
|
public static function point_exists($lng, $lat) |
129
|
|
|
{ |
130
|
|
|
return DataObject::get_one( |
131
|
|
|
'GoogleMapLocationsObject', |
132
|
|
|
array( |
133
|
|
|
"Longitude" => floatval($lng), |
134
|
|
|
"Latitude" => floatval($lat) |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function CMSEditLink() |
|
|
|
|
140
|
|
|
{ |
141
|
|
|
return singleton('GoogleMapModelAdmin')->Link( |
142
|
|
|
$this->ClassName.'/EditForm/field/'.$this->ClassName.'/item/'.$this->ID.'/edit' |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function getCMSFields() |
147
|
|
|
{ |
148
|
|
|
$fields = parent::getCMSFields(); |
149
|
|
|
$labels = $this->FieldLabels(); |
150
|
|
|
$addTitleAndContent = true; |
151
|
|
|
$parentPageID = $this->ParentID; |
|
|
|
|
152
|
|
|
if ($parentPageID) { |
153
|
|
|
$parent = SiteTree::get()->byID($parentPageID); |
154
|
|
|
if ($parent) { |
155
|
|
|
if ($parent->hasMethod("CustomAjaxInfoWindow")) { |
156
|
|
|
$addTitleAndContent = false; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
$fields->addFieldToTab("Root.Main", $addressField = new TextField('Address', $labels["Address"])); |
161
|
|
|
$addressField->setRightTitle( |
162
|
|
|
_t( |
163
|
|
|
"GoogleMap.CMS_ADDRESS_EXPLANATION", |
164
|
|
|
"(e.g. 123 Main Street, 90210, Newtown, Wellington, New Zealand ) - all other fields will be auto-completed" |
165
|
|
|
) |
166
|
|
|
); |
167
|
|
|
if ($this->Manual) { |
|
|
|
|
168
|
|
|
$fields->addFieldToTab("Root.Details", new TextField('Latitude', $labels["Latitude"])); |
169
|
|
|
$fields->addFieldToTab("Root.Details", new TextField('Longitude', $labels["Longitude"])); |
170
|
|
|
} else { |
171
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('Latitude', $labels["Latitude"])); |
172
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('Longitude', $labels["Longitude"])); |
173
|
|
|
} |
174
|
|
|
$fields->addFieldToTab("Root.Main", $manualField = new CheckboxField('Manual', $labels["Manual"])); |
175
|
|
|
$manualField->setDescription( |
176
|
|
|
_t("GoogleMap.MANUAL_DESCRIPTION", 'Edit address manually (e.g. enter Longitude and Latitude - check box, save and reload to edit...)') |
177
|
|
|
); |
178
|
|
|
$fields->addFieldToTab("Root.Main", new ReadonlyField('FullAddress', $labels["FullAddress"])); |
179
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('CountryNameCode', $labels["CountryNameCode"])); |
180
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('AdministrativeAreaName', $labels["AdministrativeAreaName"])); |
181
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('SubAdministrativeAreaName', $labels["SubAdministrativeAreaName"])); |
182
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('LocalityName', $labels["LocalityName"])); |
183
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('PostalCodeNumber', $labels["PostalCodeNumber"])); |
184
|
|
|
$fields->addFieldToTab("Root.Details", new ReadonlyField('Accuracy', $labels["Accuracy"])); |
185
|
|
|
$fields->addFieldToTab("Root.Type", $fields->dataFieldByName("PointType")); |
186
|
|
|
if ($this->PointType != "point" && $this->PointType != "none") { |
|
|
|
|
187
|
|
|
$fields->addFieldToTab("Root.Type", new TextField('PointString', $labels["PointString"])); |
188
|
|
|
} else { |
189
|
|
|
$fields->removeByName("PointString"); |
190
|
|
|
} |
191
|
|
|
if ($addTitleAndContent) { |
192
|
|
|
$fields->addFieldToTab("Root.Popup", $customPopUpWindowTitleField = new TextField('CustomPopUpWindowTitle', $labels["CustomPopUpWindowTitle"])); |
193
|
|
|
$customPopUpWindowTitleField->setRightTitle( |
194
|
|
|
_t("GoogleMap.CUSTOM_POP_UP_WINDOW_TITLE", 'Leave Blank to auto-complete the pop-up information on the map.') |
195
|
|
|
); |
196
|
|
|
$fields->addFieldToTab("Root.Popup", $customPopUpWindowInfoField = new HTMLEditorField('CustomPopUpWindowInfo', $labels["CustomPopUpWindowInfo"])); |
197
|
|
|
$customPopUpWindowInfoField->setRightTitle( |
198
|
|
|
_t("GoogleMap.CUSTOM_POP_UP_WINDOW_INFO", 'Leave Blank to auto-complete the pop-up information on the map.') |
199
|
|
|
); |
200
|
|
|
} else { |
201
|
|
|
$fields->removeByName("CustomPopUpWindowTitle"); |
202
|
|
|
$fields->removeByName("CustomPopUpWindowInfo"); |
203
|
|
|
} |
204
|
|
|
$fields->makeFieldReadonly("ParentID"); |
205
|
|
|
return $fields; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @casted variable |
210
|
|
|
* @return SiteTree |
211
|
|
|
*/ |
212
|
|
|
public function getParentData() |
213
|
|
|
{ |
214
|
|
|
return $this->Parent(); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @casted variable |
219
|
|
|
* @return String (HTML) |
220
|
|
|
*/ |
221
|
|
|
public function getAjaxInfoWindowLink() |
222
|
|
|
{ |
223
|
|
|
if (strlen($this->CustomPopUpWindowInfo) > 10) { |
|
|
|
|
224
|
|
|
return $this->CustomPopUpWindowInfo; |
|
|
|
|
225
|
|
|
} elseif ($parent = $this->getParentData()) { |
226
|
|
|
$html = $parent->AjaxInfoWindowLink(); |
227
|
|
|
} |
228
|
|
|
if (!$html) { |
|
|
|
|
229
|
|
|
$html = $this->FullAddress; |
|
|
|
|
230
|
|
|
} |
231
|
|
|
return $html; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @casted variable |
236
|
|
|
* @return String | Null |
|
|
|
|
237
|
|
|
*/ |
238
|
|
|
public function getParentClassName() |
239
|
|
|
{ |
240
|
|
|
if ($parent = $this->getParentData()) { |
241
|
|
|
return $parent->ClassName; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @casted variable |
247
|
|
|
* @return String | Null |
|
|
|
|
248
|
|
|
*/ |
249
|
|
|
public function getLink() |
250
|
|
|
{ |
251
|
|
|
if ($parent = $this->getParentData()) { |
252
|
|
|
return $parent->Link(); |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* add data from Parent to the object |
258
|
|
|
*/ |
259
|
|
|
public function addParentData() |
260
|
|
|
{ |
261
|
|
|
$parentData = $this->getParentData(); |
|
|
|
|
262
|
|
|
if (!isset(self::$parent_point_counts[$this->ParentID + 0]) && $this->getParentData()) { |
|
|
|
|
263
|
|
|
$count = GoogleMapLocationsObject::get()->filter(array("ParentID" => $this->ParentID))->count(); |
|
|
|
|
264
|
|
|
self::$parent_point_counts[$this->ParentID] = $count; |
|
|
|
|
265
|
|
|
} |
266
|
|
|
if (isset(self::$parent_point_counts[$this->ParentID + 0]) && self::$parent_point_counts[$this->ParentID + 0] == 1 && $this->getParentData()) { |
|
|
|
|
267
|
|
|
$this->Title = $this->getParentData()->Title; |
|
|
|
|
268
|
|
|
$this->Name = $this->getParentData()->Title; |
|
|
|
|
269
|
|
|
} else { |
270
|
|
|
$this->Title = $this->Address; |
|
|
|
|
271
|
|
|
$this->Name = $this->Address; |
|
|
|
|
272
|
|
|
} |
273
|
|
|
if ($this->CustomPopUpWindowTitle) { |
|
|
|
|
274
|
|
|
$this->Title = $this->CustomPopUpWindowTitle; |
|
|
|
|
275
|
|
|
$this->Name = $this->CustomPopUpWindowTitle; |
|
|
|
|
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function onBeforeWrite() |
280
|
|
|
{ |
281
|
|
|
parent::onBeforeWrite(); |
282
|
|
|
/* |
283
|
|
|
$this->GeoPointField->setX($this->Latitude); |
284
|
|
|
$this->GeoPointField->setX($this->Longitude); |
285
|
|
|
parent::onBeforeWrite(); |
286
|
|
|
*/ |
287
|
|
|
if ($this->PointType == "none") { |
|
|
|
|
288
|
|
|
$this->PointType = "point"; |
|
|
|
|
289
|
|
|
} |
290
|
|
|
$this->findGooglePoints($doNotWrite = true); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* complete points data |
295
|
|
|
* |
296
|
|
|
*/ |
297
|
|
|
protected function completePoints() |
298
|
|
|
{ |
299
|
|
|
$uncompletedPoints = GoogleMapLocationsObject::get()->where(" |
300
|
|
|
( |
301
|
|
|
(\"GoogleMapLocationsObject\".\"Address\" <> \"GoogleMapLocationsObject\".\"FullAddress\") |
302
|
|
|
OR ( |
303
|
|
|
\"GoogleMapLocationsObject\".\"Address\" = IsNull |
304
|
|
|
OR \"GoogleMapLocationsObject\".\"Address\" = '' |
305
|
|
|
) |
306
|
|
|
) |
307
|
|
|
AND |
308
|
|
|
\"GoogleMapLocationsObject\".\"Manual\" <> 1 |
309
|
|
|
AND \"GoogleMapLocationsObject\".\"Address\" <> IsNull |
310
|
|
|
AND ((\"GoogleMapLocationsObject\".\"Address\") <> '' OR (\"GoogleMapLocationsObject\".\"Longitude\"<> 0 |
311
|
|
|
AND \"GoogleMapLocationsObject\".\"Latitude\" <> 0 |
312
|
|
|
AND ( |
313
|
|
|
\"GoogleMapLocationsObject\".\"Address\" = '' |
314
|
|
|
OR \"GoogleMapLocationsObject\".\"Address\" = IsNull |
315
|
|
|
) |
316
|
|
|
)"); |
317
|
|
|
if ($uncompletedPoints->count()) { |
318
|
|
|
foreach ($uncompletedPoints as $point) { |
319
|
|
|
$point->findGooglePoints(false); |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* test to see if address is found. If address if found then |
326
|
|
|
* it will write the object, otherwise it returns null. |
327
|
|
|
* @params array $array (optional) |
328
|
|
|
* @return this|null |
|
|
|
|
329
|
|
|
*/ |
330
|
|
|
public function findGooglePointsAndWriteIfFound($params = []) |
331
|
|
|
{ |
332
|
|
|
$this->findGooglePoints(true, $params); |
333
|
|
|
if ($this->FullAddress && $this->Longitude && $this->Latitude) { |
|
|
|
|
334
|
|
|
$this->write(); |
335
|
|
|
return $this; |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param array $params params for the Google Server |
341
|
|
|
* @param bool $doNotWrite - do not write to Database |
342
|
|
|
*/ |
343
|
|
|
protected function findGooglePoints($doNotWrite, $params = []) |
344
|
|
|
{ |
345
|
|
|
if ($this->Address && !$this->Manual) { |
|
|
|
|
346
|
|
|
$newData = GetLatLngFromGoogleUsingAddress::get_placemark_as_array($this->Address, false, $params); |
|
|
|
|
347
|
|
|
} elseif ($this->Latitude && $this->Longitude && $this->Manual) { |
|
|
|
|
348
|
|
|
$newData = GetLatLngFromGoogleUsingAddress::get_placemark_as_array($this->Latitude.",".$this->Longitude, false, $params); |
|
|
|
|
349
|
|
|
} |
350
|
|
|
if (isset($newData) && is_array($newData)) { |
351
|
|
|
$this->addDataFromArray($newData, $doNotWrite); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* |
357
|
|
|
* @param Array $newData |
358
|
|
|
* @param Boolean $doNotWrite - do not write object to database |
359
|
|
|
*/ |
360
|
|
|
protected function addDataFromArray($newData, $doNotWrite = false) |
361
|
|
|
{ |
362
|
|
|
foreach ($newData as $field => $value) { |
363
|
|
|
$this->$field = $value; |
364
|
|
|
} |
365
|
|
|
if (!$doNotWrite) { |
366
|
|
|
/* AS THIS IS A onBeforeWrite there is NO POINT in writing!!!!! */ |
367
|
|
|
$this->write(); |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* provides a links to Google Maps to search for directions |
373
|
|
|
* @return String |
374
|
|
|
*/ |
375
|
|
|
public function DirectionsLink() |
376
|
|
|
{ |
377
|
|
|
return "https://www.google.com/maps/dir//".urlencode($this->Address); |
|
|
|
|
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|