-
Notifications
You must be signed in to change notification settings - Fork 19
/
GoogleMapsPlugin.php
173 lines (143 loc) · 6.15 KB
/
GoogleMapsPlugin.php
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
<?php
namespace Craft;
class GoogleMapsPlugin extends BasePlugin
{
public function getName()
{
return Craft::t('Google Maps');
}
public function getVersion()
{
return '0.8.4';
}
public function getDeveloper()
{
return 'Objective HTML';
}
public function getDeveloperUrl()
{
return 'https://objectivehtml.com';
}
protected function defineSettings()
{
return array(
'enableGeocoder' => array(AttributeType::Bool, 'label' => 'Enable Geocoder', 'default' => false),
'geocodeSections' => array(AttributeType::Mixed, 'label' => 'Geocode Sections', 'default' => array()),
'geocodeFields' => array(AttributeType::String, 'label' => 'Geocode Fields'),
'mapFields' => array(AttributeType::Mixed, 'label' => 'Google Maps Fields', 'default' => array()),
//'apiKey' => array(AttributeType::String, 'required' => true, 'label' => 'API Key'),
);
}
public function getSettingsHtml()
{
$sections = array();
foreach(craft()->sections->getAllSections() as $section)
{
$sections[] = array(
'label' => $section->name,
'value' => $section->id
);
}
$fields = array();
foreach(craft()->googleMaps_templates->getGoogleMapsFieldTypes() as $field)
{
$fields[] = array(
'label' => $field->name,
'value' => $field->id
);
}
return craft()->templates->render('googlemaps/settings', array(
'sections' => $sections,
'fields' => $fields,
'settings' => $this->getSettings()
));
}
public function init()
{
$settings = $this->getSettings();
/*
craft()->on('postmaster.init', function(Event $event) {
require_once 'GoogleMapsParcelType.php';
craft()->postmaster->registerParcelType('Craft\GoogleMapsParcelType');
});
*/
if($settings->enableGeocoder)
{
craft()->on('entries.saveEntry', function(Event $event) use ($settings)
{
$entry = $event->params['entry'];
$content = $entry->getContent();
// If section being saved matches a section set in the geocode settings
if(in_array($entry->sectionId, $settings->geocodeSections))
{
$address = array();
foreach(explode("\r\n", $settings->geocodeFields) as $field)
{
if($value = trim($content->getAttribute(trim($field))))
{
if(!empty($value))
{
$address[] = trim($value);
}
}
}
// If no address was entered, return script with no changes to db.
if(!count($address))
{
return;
}
$response = craft()->googleMaps_geocoder->geocode($address);
$mapFields = $settings->mapFields;
if($response->status == 'OK')
{
$geocodeResponse = $response->results[0];
if($mapFields == '*')
{
$fields = craft()->db->createCommand()
->select('f.*')
->from('fields f')
->where('f.type = :type', array(':type' => 'GoogleMaps_GoogleMap'))
->queryAll();
$mapFields = array();
foreach($fields as $field)
{
$mapFields[] = $field['id'];
}
}
foreach($mapFields as $mapFieldId)
{
$mapField = craft()->fields->getFieldById($mapFieldId);
$mapData = json_decode($content->{$mapField->handle});
$mapDataModel = new GoogleMaps_MapDataModel((array) $mapData);
// Update marker if exists, otherwise add a new marker
if($marker = $mapDataModel->getMarker(0))
{
$marker->lat = $geocodeResponse->geometry->location->lat;
$marker->lng = $geocodeResponse->geometry->location->lng;
$marker->address = $geocodeResponse->formatted_address;
$marker->addressComponents = $geocodeResponse->address_components;
if(!$marker->customContent)
{
$marker->customContent = GoogleMaps_MarkerModel::defaultContent($geocodeResponse->formatted_address);
}
}
else
{
$marker = GoogleMaps_MarkerModel::populateModel(array(
'lat' => $geocodeResponse->geometry->location->lat,
'lng' => $geocodeResponse->geometry->location->lng,
'address' => $geocodeResponse->formatted_address,
'addressComponents' => $geocodeResponse->address_components,
'content' => GoogleMaps_MarkerModel::defaultContent($geocodeResponse->formatted_address)
));
$mapDataModel->addMarker($marker);
}
$content->{$mapField->handle} = $mapDataModel->toJson();
craft()->content->saveContent($entry);
}
}
}
});
}
}
}