|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* turns a field into a geo-coding field. |
|
4
|
|
|
* |
|
5
|
|
|
* @authors: Nicolaas [at] Sunny Side Up .co.nz |
|
6
|
|
|
* @package: forms |
|
7
|
|
|
* @sub-package: geocoding |
|
8
|
|
|
* @inspiration: http://gmaps-samples-v3.googlecode.com/svn/trunk/places/autocomplete-addressform.html |
|
9
|
|
|
**/ |
|
10
|
|
|
class GoogleAddressField extends TextField |
|
11
|
|
|
{ |
|
12
|
|
|
private static $google_map_api_location = '//maps.googleapis.com/maps/api/js'; |
|
13
|
|
|
|
|
14
|
|
|
private static $field_js_location = 'google_address_field/javascript/GoogleAddressField.js'; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
//when autocomplete returns a place we check if the type is an allowed type and if not |
|
18
|
|
|
//provide the user an alert to let them know their address may not have been correctly autocompleted |
|
19
|
|
|
private static $allowed_types = ['street_address']; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $api_key = ""; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* return a list of requirements |
|
28
|
|
|
* @return [type] [description] |
|
|
|
|
|
|
29
|
|
|
*/ |
|
30
|
|
|
public static function js_requirements() |
|
31
|
|
|
{ |
|
32
|
|
|
$array = []; |
|
33
|
|
|
$api = Config::inst()->get('GoogleAddressField', 'google_map_api_location'); |
|
34
|
|
|
$js = Config::inst()->get('GoogleAddressField', 'field_js_location'); |
|
35
|
|
|
if ($api) { |
|
36
|
|
|
$array[] = $api |
|
37
|
|
|
.'?' |
|
38
|
|
|
.'&libraries=places' |
|
39
|
|
|
.'&key='.Config::inst()->get('GoogleAddressField', 'api_key'); |
|
40
|
|
|
} |
|
41
|
|
|
if ($js) { |
|
42
|
|
|
$array[] = $js; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $array; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* |
|
50
|
|
|
* @var bool |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $useSensor = false; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Do you want this annoying ... |
|
56
|
|
|
* this website wants to know exactly where you are |
|
57
|
|
|
* and what you are wearing thing ... |
|
58
|
|
|
* then this is your VAR. |
|
59
|
|
|
* |
|
60
|
|
|
* @param bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setUseSensor($b) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->useSensor = $b; |
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected $alwaysShowFields = false; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param bool |
|
72
|
|
|
*/ |
|
73
|
|
|
public function setAlwaysShowFields($b) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->alwaysShowFields = $b; |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Link to the static map. Set to an empty string to have no static image appear. |
|
81
|
|
|
* Use the [ADDRESS] tag to insert the address... |
|
82
|
|
|
* user the [MAXWIDTH] tag to set it automatically to the width of the container. |
|
83
|
|
|
* |
|
84
|
|
|
* @var string |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $googleStaticMapLink = '//maps.googleapis.com/maps/api/staticmap?center=[ADDRESS]&zoom=17&scale=false&size=[MAXWIDTH]x[MAXHEIGHT]&maptype=roadmap&format=png&visual_refresh=true&markers=size:mid%7Ccolor:red%7Clabel:%7C[ADDRESS]'; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* set to empty string to NOT show a static map. |
|
90
|
|
|
* |
|
91
|
|
|
* @param string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setGoogleStaticMapLink($s) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->googleStaticMapLink = $s; |
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* get to empty string to NOT show a static map. |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getGoogleStaticMapLink() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->googleStaticMapLink . '&key='.Config::inst()->get('GoogleAddressField', "api_key"); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* CSS file used in this field (can be themed!). |
|
111
|
|
|
* |
|
112
|
|
|
* @var string |
|
113
|
|
|
*/ |
|
114
|
|
|
protected $cssLocation = 'GoogleAddressField'; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setCssLocation($s) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->cssLocation = $s; |
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* list of links between |
|
127
|
|
|
* form fields in the current field (e.g. TextField with name City) |
|
128
|
|
|
* and the result XML. |
|
129
|
|
|
* When the results are returned this field will fill the form |
|
130
|
|
|
* fields with XML data from the results using this array |
|
131
|
|
|
* Format is: |
|
132
|
|
|
* [formFieldName] => array( |
|
133
|
|
|
* resultType1 => 'long_name', |
|
134
|
|
|
* resultType2 => 'long_name', |
|
135
|
|
|
* resultType2 => 'short_name', |
|
136
|
|
|
* etc... |
|
137
|
|
|
* ) |
|
138
|
|
|
* e.g. |
|
139
|
|
|
* <code php> |
|
140
|
|
|
* "BillingRegion" => array("administrative_area_level_1" => "long_name", "country" => "short_name") |
|
141
|
|
|
* </code>. |
|
142
|
|
|
* |
|
143
|
|
|
* @var array |
|
144
|
|
|
*/ |
|
145
|
|
|
protected $fieldMap = array(); |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param array |
|
149
|
|
|
*/ |
|
150
|
|
|
public function setFieldMap($a) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->fieldMap = $a; |
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param string $formField |
|
158
|
|
|
* @param array $arrayOfGeoData |
|
159
|
|
|
*/ |
|
160
|
|
|
public function addFieldMapEntry($formField, $arrayOfGeoData) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->fieldMap[$formField] = $arrayOfGeoData; |
|
163
|
|
|
return $this; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $formField |
|
168
|
|
|
*/ |
|
169
|
|
|
public function removeFieldMapEntry($formField) |
|
170
|
|
|
{ |
|
171
|
|
|
unset($this->fieldMap[$formField]); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getFieldMap() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->fieldMap; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
|
|
183
|
|
|
protected $typeToBeReturned = 'address'; |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param string $code - e.g. address |
|
|
|
|
|
|
187
|
|
|
*/ |
|
188
|
|
|
public function setTypeToBeReturned($ype) |
|
|
|
|
|
|
189
|
|
|
{ |
|
190
|
|
|
$this->typeToBeReturned = $type; |
|
|
|
|
|
|
191
|
|
|
|
|
192
|
|
|
return $this; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
|
|
196
|
|
|
protected $restrictToCountryCode = ''; |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $code - e.g. NZ |
|
200
|
|
|
*/ |
|
201
|
|
|
public function setRestrictToCountryCode($code) |
|
202
|
|
|
{ |
|
203
|
|
|
$this->restrictToCountryCode = $code; |
|
204
|
|
|
|
|
205
|
|
|
return $this; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @return string |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getRestrictToCountryCode() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->restrictToCountryCode; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @return bool |
|
220
|
|
|
*/ |
|
221
|
|
|
public function hasData() |
|
222
|
|
|
{ |
|
223
|
|
|
return false; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @return string |
|
228
|
|
|
*/ |
|
229
|
|
|
public function Field($properties = array()) |
|
230
|
|
|
{ |
|
231
|
|
|
$this->addExtraClass('text'); |
|
232
|
|
|
foreach (self::js_requirements() as $jsFile) { |
|
233
|
|
|
Requirements::javascript($jsFile); |
|
234
|
|
|
} |
|
235
|
|
|
Requirements::customScript( |
|
236
|
|
|
$this->getJavascript(), |
|
237
|
|
|
'GoogleAddressField'.$this->id() |
|
238
|
|
|
); |
|
239
|
|
|
|
|
240
|
|
|
if ($this->cssLocation) { |
|
241
|
|
|
Requirements::themedCSS($this->cssLocation, 'google_address_field'); |
|
242
|
|
|
} |
|
243
|
|
|
$this->setAttribute('autocomplete', 'false'); |
|
244
|
|
|
$this->setAttribute('autofill', 'false'); |
|
245
|
|
|
$this->setAttribute('data-selectedOptionNotAllowed', Convert::raw2att(_t('GoogleAddressField.SELECTED_OPTION_NOT_ALLOWED', 'ERROR: You have selected an invalid'))); |
|
|
|
|
|
|
246
|
|
|
$this->setAttribute('data-errorMessageMoreSpecific', Convert::raw2att(_t('GoogleAddressField.ERROR_MESSAGE_MORE_SPECIFIC', 'Error: please enter a more specific location.'))); |
|
|
|
|
|
|
247
|
|
|
$this->setAttribute('data-errorMessageAddressNotFound', Convert::raw2att(_t('GoogleAddressField.ERROR_MESSAGE_ADDRESS_NOT_FOUND', 'Error: sorry, address could not be found.'))); |
|
|
|
|
|
|
248
|
|
|
$this->setAttribute('data-findNewAddressText', Convert::raw2att(_t('GoogleAddressField.FIND_NEW_ADDRESS', 'Find Different Address'))); |
|
|
|
|
|
|
249
|
|
|
$this->setAttribute('data-relatedFields', Convert::raw2att(Convert::raw2json($this->getFieldMap()))); |
|
|
|
|
|
|
250
|
|
|
$this->setAttribute('data-alwaysShowFields', ($this->alwaysShowFields ? 'true' : 'false')); |
|
251
|
|
|
$this->setAttribute('data-useSensor', ($this->useSensor ? 'true' : 'false')); |
|
252
|
|
|
$this->setAttribute('data-googleStaticMapLink', $this->getGoogleStaticMapLink()); |
|
253
|
|
|
$this->setAttribute('data-typeToBeReturned', $this->typeToBeReturned); |
|
254
|
|
|
if ($code = $this->getRestrictToCountryCode()) { |
|
255
|
|
|
$this->setAttribute('data-restrictToCountryCode', $code); |
|
256
|
|
|
} |
|
257
|
|
|
$this->setAttribute('data-linkLabelToViewMap', Convert::raw2att(_t('GoogleAddressField.LINK_LABEL_TO_VIEW_MAP', 'view map'))); |
|
|
|
|
|
|
258
|
|
|
$this->setAttribute('data-defaultAddress', Convert::raw2att(str_replace("'", '', $this->Value()))); |
|
|
|
|
|
|
259
|
|
|
//right title |
|
260
|
|
|
$this->RightTitle(); |
|
261
|
|
|
|
|
262
|
|
|
return parent::Field($properties); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* retuns the customised Javascript for the form field. |
|
267
|
|
|
* |
|
268
|
|
|
* @return string |
|
269
|
|
|
*/ |
|
270
|
|
|
protected function getJavascript() |
|
271
|
|
|
{ |
|
272
|
|
|
$allowed_types = Config::inst()->get('GoogleAddressField', 'allowed_types'); |
|
273
|
|
|
|
|
274
|
|
|
if ($allowed_types) { |
|
275
|
|
|
return ' |
|
276
|
|
|
if(typeof GoogleAddressFieldStatics === "undefined") { |
|
277
|
|
|
var GoogleAddressFieldStatics = {}; |
|
278
|
|
|
} |
|
279
|
|
|
GoogleAddressFieldStatics.allowedTypes = '.json_encode($allowed_types).'; |
|
280
|
|
|
'; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return ''; |
|
284
|
|
|
} |
|
285
|
|
|
/** |
|
286
|
|
|
* @return string |
|
|
|
|
|
|
287
|
|
|
*/ |
|
288
|
|
|
public function RightTitle() |
|
289
|
|
|
{ |
|
290
|
|
|
$rightTitle = $this->renderWith('GoogleAddressFieldRightTitle'); |
|
291
|
|
|
if (strlen(trim($rightTitle))) { |
|
292
|
|
|
return $rightTitle; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.