-
Notifications
You must be signed in to change notification settings - Fork 4
/
vista_map.module
397 lines (355 loc) · 13.2 KB
/
vista_map.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* @file
* Code for the Map feature.
*/
include_once 'vista_map.features.inc';
// Default theme implementations.
require_once drupal_get_path('module', 'vista_map') . '/theme/vista_map.theme.inc';
/**
* Implements hook_form_FORM_ID_alter for field UI forms.
*/
function vista_map_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
// Add a granularity setting to geofield geocoder options.
if ($form['#field']['type'] == 'geofield') {
$widget_settings = $form['#instance']['widget']['settings'];
$granularity_options = array(
'exact' => t('Exact'),
'postal' => t('Postal code'),
);
$form['instance']['widget']['settings']['geocoder_granularity'] = array(
'#type' => 'select',
'#title' => t('Geocoder granularity'),
'#default_value' => isset($widget_settings['geocoder_granularity']) ? $widget_settings['geocoder_granularity'] : '',
'#options' => $granularity_options,
'#description' => t('Select the level of accuracy for geocoding.'),
'#required' => TRUE,
'#weight' => 0,
);
// Make the granularity setting flow in a UX-sensible place within the form.
$form['instance']['widget']['settings']['geocoder_field']['#weight'] = -1;
}
}
/**
* Implements hook_geocoder_geocode_values_alter.
*
* @param array $src_field_vals
* Address field sub-feild values, i.e. address components.
* @param array $field_info
* Standard field instance info.
* @param array $handler_settings
* Geocoder handling settings for the field instance.
* @param array $field_instance
* Field instance settings.
*/
function vista_map_geocoder_geocode_values_alter(&$src_field_vals, $field_info, $handler_settings, $field_instance) {
// If we only want postal-code granularity, unset the more-specific values.
if (isset($field_instance['widget']['settings']['geocoder_granularity'])) {
if ($field_instance['widget']['settings']['geocoder_granularity'] === 'postal') {
$postal_code_only_fields = array(
'dependent_locality',
'locality',
'sub_administrative_area',
'administrative_area',
'postal_code',
'country'
);
foreach ($src_field_vals as $idx => $address) {
foreach ($address as $key => $value) {
if (!in_array($key, $postal_code_only_fields)) {
unset($src_field_vals[$idx][$key]);
}
}
}
}
}
}
/**
* Implements hook_views_geojson_render_fields_alter().
*
* Geocluster doesn't discern between a cluster of nearby points and a
* cluster of exact points (monolithic cluster). Here we are adding a flag
* that can be used by the frontend to apply appropriate behavior to a
* monolithic cluster.
*
* @param (array) $feature
* A leaflet map feature, contains the geometry and properties.
* @param (Object) $view
* The view producing the feature.
* @param (Object) $row
* The row data from the view that produced the feature.
* @param (int) $index
* The cardinality of the feature, i.e. 1,2,3, etc.
*/
function vista_map_views_geojson_render_fields_alter(&$feature, &$view, &$row, &$index) {
// Get user ids the cluster.
// The uid is the GROUP_CONCAT field set up in the view.
$cluster_uids = explode(",", $row->uid);
// Only check for monolithis if there is more than one point in the feature.
if (count($cluster_uids) > 1) {
// Compute table and field names sourcing the feature.
$geofield_table_name = NULL;
$geofield_field_name = NULL;
foreach ($view->display as $display_name => $display) {
if ($view->current_display == $display_name) {
foreach ($display->display_options['fields'] as $field) {
$field_info = field_info_field($field['field']);
if ($field_info['type'] === 'geofield') {
$geofield_table_name[$display_name] = $field['table'];
$geofield_field_name[$display_name] = $field['field'];
break;
}
}
}
}
if (isset($geofield_table_name[$view->current_display]) && isset($geofield_field_name[$view->current_display])) {
// Set up a query to pull lat/lon data data from all entities
// in the cluster. We didn't load entities or use field API
// because we care about performance.
$entity_ids = explode(",", $row->geocluster_ids);
$geopoint_field_name = array(
'lat' => $geofield_field_name[$view->current_display] . '_lat',
'lon' => $geofield_field_name[$view->current_display] . '_lon',
);
$query = db_select($geofield_table_name[$view->current_display], 'gf')
->fields('gf', array(
$geopoint_field_name['lat'],
$geopoint_field_name['lon'],
))
->condition('entity_id', $entity_ids, 'IN');
// Figure out of the feature is monolithic or not.
// The idea here is that we set a reference lat/lon then iterate the
// results look for the first mismatch in lat or lon. Once we find
// a mismatch, the cluster is non-monolithic. Monolithic clusters are
// somewhat rare, so in most cases, we should break out of the loop
// pretty quickly. We do know that some of the largest monolithic clusters
// number in less than 100, so even in the worst case, the performance
// hit *should be* manageable.
$result = $query->execute();
$ref_point = $result->fetchAssoc();
$is_monolithic = TRUE;
while ($is_monolithic && ($point = $result->fetchAssoc())) {
if ($point[$geopoint_field_name['lat']] != $ref_point[$geopoint_field_name['lat']]) {
$is_monolithic = FALSE;
}
elseif ($point[$geopoint_field_name['lon']] != $ref_point[$geopoint_field_name['lon']]) {
$is_monolithic = FALSE;
}
}
// Add the flag and source display name for ajax rendering.
$feature['properties']['is_monolithic'] = $is_monolithic;
$feature['properties']['display_name'] = $view->current_display;
}
}
// Otherwise, this is a single-point feature. Just supply some
// info so the ajax loader knows what kind of popup to build.
else {
// Is this too slow?
$the_user = user_load($row->uid);
$feature['properties']['is_alumni']
= _vista_map_user_is_alumni($the_user);
$feature['properties']['is_active']
= _vista_map_user_is_active($the_user);
// Add the group of the user to the feature.
if (isset($the_user->og_user_node)) {
foreach ($the_user->og_user_node[LANGUAGE_NONE] as $group) {
$feature['properties']['group_id'][] = $group['target_id'];
}
}
// Also add the "other groups" id.
if (isset($the_user->og_user_node1)) {
foreach ($the_user->og_user_node1[LANGUAGE_NONE] as $group) {
$feature['properties']['group_id'][] = $group['target_id'];
}
}
}
}
/**
* Implements hook_leaflet_map_info_alter.
*
* We're adding the fullscreen control, so add it to the settings
* here regardless of what module (leaflet, leaflet_more_maps, etc)
* provides the default settings.
*/
function vista_map_leaflet_map_info_alter(&$map_info) {
foreach ($map_info as $map_key => $spec) {
$map_info[$map_key]['settings']['fullscreenControl'] = TRUE;
}
}
/**
* Implements hook_leaflet_map_prebuild_alter.
*/
function vista_map_leaflet_map_prebuild_alter(&$settings) {
// Add the fullscreen button needs from the libarary.
$fs_lib_path = libraries_get_path('leaflet.fullscreen');
drupal_add_js($fs_lib_path . '/dist/Leaflet.fullscreen.js');
drupal_add_css($fs_lib_path . '/dist/leaflet.fullscreen.css');
// Add sprite animation library.
$animate_lib_path = libraries_get_path('animate_sprite');
drupal_add_js($animate_lib_path . '/scripts/jquery.animateSprite.js');
// Add the awesome markers library.
$marker_lib = libraries_get_path('awesome_markers');
drupal_add_js($marker_lib . '/dist/leaflet.awesome-markers.js');
drupal_add_css($marker_lib . '/dist/leaflet.awesome-markers.css');
// Add our customized geocluster behaviors.
drupal_add_js(
drupal_get_path('module', 'vista_map') . '/vista_map.js', array(
// Add overrides after geocluster.leaflet.bbox.js.
'weight' => 110,
)
);
// Add micro css file to fix interference with layer control (not stylistic).
drupal_add_css(
drupal_get_path('module', 'vista_map') . '/vista_map.css'
);
}
/**
* Detect whether or not a user is a member of the alumni group.
*
* @param object $user
* The user account object for the user in question.
*
* @return bool
* Whether or not the user is an alum.
*/
function _vista_map_user_is_alumni($user) {
// Since the gid is set on site install, we are identifying groups by nid
// only. This obviously would break if a group were deleted and recreated.
$alumni_gid = 19;
$user_groups = og_get_groups_by_user($user);
// Freshly-migrated users may not yet be a member of a group yet,
// so only check for memberships when one or more are present.
if (isset($user_groups['node'])) {
$is_alumni = array_key_exists($alumni_gid, $user_groups['node']);
}
else {
$is_alumni = FALSE;
}
return $is_alumni;
}
/**
* Detect whether or not a user is a member of an active group.
*
* @param object $user
* The account object for the user in question.
*
* @return bool
* Whether or not the user is in one of the active groups.
*/
function _vista_map_user_is_active($user) {
$active_gids = array(
'vista' => 15,
'vista_leader' => 16,
'supervisor' => 17,
'state_offices' => 18,
);
$user_groups = og_get_groups_by_user($user);
$is_active = FALSE;
// Freshly-migrated users may not yet have a group,
// so only look for membership when one or more are present.
if (isset($user_groups['node'])) {
foreach ($active_gids as $gid) {
if ($is_active = array_key_exists($gid, $user_groups['node'])) {
$is_active = TRUE;
// We don't care which active group.
break;
}
}
}
return $is_active;
}
/**
* Implements hook_leaflet_geojson_map_pane_alter().
*
* Here we are altering the center and zoom point for the map based
* on user data. The user and associated profile has a personal
* address geocoded to zip-code granularity, and a program/site address
* geocoded to exact granularity. We choose an address and alter the point
* based on the following rules:
*
* 1. If the user is an alum, center and zoom on their personal zip code,
* regardless of any other group memberships.
* 2. If the user is active vista, leader, supervisor, or state office,
* center and zoom on program address.
* 3. If the user doesn't belong to any of these groups (or is anonymous)
* do nothing and leave the default center and zoom.
*
* @param array $map_base_info
* Array of information about the map base.
* @param array $feature_layers
* Array of feature (data) layers
*/
function vista_map_leaflet_geojson_map_pane_alter(&$map_base_info, &$feature_layers) {
if (!user_is_anonymous()) {
global $user;
// Detect alumni/active group memberships.
$is_alumni = _vista_map_user_is_alumni($user);
$is_active = _vista_map_user_is_active($user);
// Get the current user's profile.
$user_profile = profile2_load_by_user($user, 'main');
// Compute the new center and zoom based off the profile data.
$new_center = array();
$new_zoom = NULL;
if (!empty($user_profile)) {
if ($is_alumni) {
// An alumni member will recenter on their profile location.
$geofield_items = field_get_items('profile2', $user_profile, 'field_profile_location');
if ($geofield_items) {
$new_center['lat'] = $geofield_items[0]['lat'];
$new_center['lon'] = $geofield_items[0]['lon'];
$new_zoom = 8;
}
}
elseif ($is_active) {
// An active group member will recenter on program location.
$site_info_items = field_get_items('profile2', $user_profile, 'field_site_information');
if ($site_info_items) {
// The geofield is inside a field collection.
$fc_entity_id = $site_info_items[0]['value'];
$site_info = field_collection_item_load($fc_entity_id);
$geofield_items = field_get_items('field_collection_item', $site_info, 'field_organization_location');
if ($geofield_items) {
$new_center['lat'] = $geofield_items[0]['lat'];
$new_center['lon'] = $geofield_items[0]['lon'];
$new_zoom = 10;
}
}
}
}
// Adjust center if it's complete.
if (isset($new_center['lat']) && isset($new_center['lon'])) {
$map_base_info['center']['lat'] = $new_center['lat'];
$map_base_info['center']['lon'] = $new_center['lon'];
}
// Adjust zoom if it was changed.
if ($new_zoom) {
$map_base_info['settings']['zoom'] = $new_zoom;
}
}
}
/**
* Implements hook_block_info.
*/
function vista_map_block_info() {
$blocks = array();
// Map icon key as a block.
$blocks['vista_map_icon_key'] = array(
'info' => t('Vista map icon key'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_view.
*/
function vista_map_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'vista_map_icon_key':
$block['content'] = theme('vista_map_icon_key', array('key_data' => NULL));
break;
default:
break;
}
return $block;
}