-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeocode.module
executable file
·308 lines (265 loc) · 9.29 KB
/
geocode.module
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/**
* The Geocode API call.
* @example:
* geocode('google','4925 Gair Ave, Terrace, BC');
* geocode('google','New York City','polygon');
*/
function geocode() {
ctools_include('plugins');
$args = func_get_args();
$processor_name = array_shift($args);
$processor = ctools_get_plugins('geocode', 'geocode_handler', $processor_name);
$geometry = call_user_func_array($processor['callback'],$args);
return $geometry;
}
/**
* Return a list of all handlers that might geocode something for you.
*/
function geocode_handler_info($field_type = NULL) {
ctools_include('plugins');
static $handlers;
if (!$handlers) {
$handlers = ctools_get_plugins('geocode', 'geocode_handler');
}
if ($field_type) {
$field_handlers = $handlers;
foreach($field_handlers as $i => $handler) {
if (!isset($handler['field_types']) || !in_array($field_type, $handler['field_types'])) {
unset($field_handlers[$i]);
}
}
return $field_handlers;
}
return $handlers;
}
function geocode_get_handler($handler_name) {
$handlers = geocode_handler_info();
return $handlers[$handler_name];
}
function geocode_supported_field_types() {
$supported = array();
$processors = geocode_handler_info();
foreach ($processors as $processor) {
if (isset($processor['field_types'])) {
foreach ($processor['field_types'] as $field_type) {
$supported[$field_type][] = $processor['name'];
}
}
}
return $supported;
}
function geocode_load_geophp() {
static $static_cache = FALSE;
if (!$static_cache) {
$path = libraries_get_path('geoPHP');
$file = $path.'/geoPHP.inc';
if (file_exists($file)) {
if (include_once($file)) {
$static_cache = $file;
}
}
}
return $static_cache;
}
/**
* Implementation of hook_ctools_plugin_api().
*/
function geocode_ctools_plugin_api() {
return array('version' => 1);
}
/**
* Implementation of hook_ctools_plugin_dierctory() to let the system know
* we implement plugins.
*/
function geocode_ctools_plugin_directory($module, $plugin) {
return 'plugins/' . $plugin;
}
/**
* Implements hook_ctools_plugin_type
*/
function geocode_ctools_plugin_type() {
return array(
'geocode_handler' => array(),
);
}
/**
* Implements hook_field_widget_info().
*/
function geocode_field_widget_info() {
return array(
'geocode' => array(
'label' => t('Geocode from another field'),
'field types' => array('geofield'),
),
);
}
/**
* Implements field_widget_settings_form().
*/
function geocode_field_widget_settings_form($this_field, $instance) {
$settings = $instance['widget']['settings'];
$entity_fields = field_info_instances($instance['entity_type'], $instance['bundle']);
$all_fields = field_info_fields();
$supported_field_types = geocode_supported_field_types();
$processors = geocode_handler_info();
$handlers_by_type = array();
$field_types = array();
$valid_fields = array();
$available_handlers = array();
// Add in the title/name
switch ($instance['entity_type']) {
case 'node':
$all_fields['title'] = array(
'field_name' => 'title',
'type' => 'text',
);
$entity_fields['title']['label'] = t('Title');
break;
case 'taxonomy_term':
$all_fields['name'] = array(
'field_name' => 'name',
'type' => 'text',
);
$entity_fields['name']['label'] = t('Name');
break;
}
// Get a list of all valid fields that we both support and are part of this entity
foreach ($all_fields as $field) {
if (array_key_exists($field['field_name'], $entity_fields)) {
if (in_array($field['type'], array_keys($supported_field_types)) && ($field['field_name'] != $this_field['field_name'])) {
$valid_fields[$field['field_name']] = $entity_fields[$field['field_name']]['label'];
foreach ($supported_field_types[$field['type']] as $handler) {
$available_handlers[$handler] = $processors[$handler]['title'];
$handlers_by_type[$field['type']][] = $handler;
$field_types[$field['field_name']] = $field['type'];
}
}
}
}
$form['geocode_field'] = array(
'#type' => 'select',
'#title' => t('Geocode from field'),
'#default_value' => isset($settings['geocode_field']) ? $settings['geocode_field']: '',
'#options' => $valid_fields,
'#description' => t('Select which field you would like to geocode from.'),
'#required' => TRUE,
'#attributes' => array('onchange' => 'geocode_admin_handler_filter();'),
);
$form['geocode_handler'] = array(
'#type' => 'select',
'#title' => t('Geocoder'),
'#prefix' => '<div id="geocode-handler-div">',
'#suffix' => '</div>',
'#default_value' => isset($settings['geocode_handler']) ? $settings['geocode_handler']: '',
'#options' => $available_handlers,
'#description' => t('Select which type of geocoding handler you would like to use'),
'#required' => TRUE,
);
drupal_add_js(array('geocode_widget_settings' => array('handlers' => $handlers_by_type, 'types' => $field_types)),'setting');
drupal_add_js(drupal_get_path('module','geocode').'/geocode.admin.js','file');
return $form;
}
function geocode_field_widget_settings_form_callback($form, $form_state) {
return $form['geocode_handler'];
}
/**
* Implements hook_field_widget_form().
*/
function geocode_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
$element = $base;
$element['wkt'] = array(
'#type' => 'hidden',
'#element_validate' => array('geocode_widget_validate'),
);
$element['geocode_target'] = array(
'#type' => 'hidden',
'#value' => $instance['field_name'],
);
$element['geocode_source'] = array(
'#type' => 'hidden',
'#value' => isset($instance['widget']['settings']['geocode_field']) ? $instance['widget']['settings']['geocode_field'] : '',
);
$element['geocode_handler'] = array(
'#type' => 'hidden',
'#value' => isset($instance['widget']['settings']['geocode_handler']) ? $instance['widget']['settings']['geocode_handler'] : '',
);
return $element;
}
function geocode_widget_validate($element, &$form_state) {
// To validate, we simply run through the form fields, looking for items with geocode_source and geocode_target properties
// Walk the form_state values, applying all instances of geocode source and target operations
foreach ($form_state['values'] as &$field) {
geocode_widget_validate_walkthrough($field, $form_state);
}
if (isset($form_state['field']['#parents'])) {
//Walk through the fields that are under parents and match up and sources and targets there
foreach ($form_state['field']['#parents'] as $parent => $parent_data) {
foreach ($form_state['values'][$parent] as &$field) {
geocode_widget_validate_walkthrough($field, $form_state, $parent);
}
}
}
}
function geocode_widget_validate_walkthrough(&$field, $form_state, $parent = NULL) {
if (is_array($field)) {
$lang = isset($form_state['values']['language']) ? $form_state['values']['language'] : LANGUAGE_NONE;
if ((!empty($field[$lang][0]['geocode_source']) && !empty($field[$lang][0]['geocode_target']) && !empty($field[$lang][0]['geocode_handler']))) {
// We found a source/target/handler set, let's do this.
$source = $field[$lang][0]['geocode_source'];
$handler = geocode_get_handler($field[$lang][0]['geocode_handler']);
$field[$lang] = array();
// If the sources is 'title' then pull from the title
if ($source == 'title' || 'name') {
if (isset($form_state['values'][$source])) {
$field_object = array('type' => 'text');
$item = array('value' => $form_state['values'][$source]);
try {
$geometry = call_user_func($handler['field_callback'],$field_object, $item);
}
catch(Exception $e) {
drupal_set_message($e->getMessage(),'error');
return;
}
if ($geometry) {
$field[$lang][0] = geofield_get_values_from_geometry($geometry);
}
}
}
else {
$field_object = field_info_field($field[$lang][0]['geocode_source']);
if (isset($parent)) {
if (isset($form_state['values'][$parent][$source][$lang])) {
foreach ($form_state['values'][$parent][$source][$lang] as $delta => $item) {
try {
$geometry = call_user_func($handler['field_callback'],$field_object, $item);
}
catch(Exception $e) {
drupal_set_message($e->getMessage(),'error');
return;
}
if ($geometry) {
$field[$lang][$delta] = geofield_get_values_from_geometry($geometry);
}
}
}
} else {
if (isset($form_state['values'][$source][$lang])) {
foreach ($form_state['values'][$source][$lang] as $delta => $item) {
try {
$geometry = call_user_func($handler['field_callback'],$field_object, $item);
}
catch(Exception $e) {
drupal_set_message($e->getMessage(),'error');
return;
}
if ($geometry) {
$field[$lang][$delta] = geofield_get_values_from_geometry($geometry);
}
}
}
}
}
}
}
}