forked from Islandora-Labs/islandora_simple_map
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathislandora_simple_map.module
353 lines (334 loc) · 12.3 KB
/
islandora_simple_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
<?php
/**
* @file
* The main Islandora Simple Map module file.
*/
define('ISLANDORA_SIMPLE_MAP_XPATHS', "//mods:subject/mods:cartographics/mods:coordinates\n//mods:subject/mods:geographic");
define('ISLANDORA_SIMPLE_MAP_COORDINATE_HOOK', 'islandora_simple_map_get_coordinates');
define('ISLANDORA_SIMPLE_MAP_PARSE_HOOK', 'islandora_simple_map_parse_coordinates_callback');
define('ISLANDORA_SIMPLE_MAP_CACHE_PREFIX', 'islandora_simple_map_collection_map_');
define('ISLANDORA_SIMPLE_MAP_CACHE_BIN', 'cache');
// Includes blocks.
require_once dirname(__FILE__) . '/includes/blocks.inc';
// Include utilities.
require_once dirname(__FILE__) . '/includes/utilities.inc';
/**
* Implements hook_menu().
*/
function islandora_simple_map_menu() {
$items = array();
$items['admin/islandora/tools/islandora_simple_map'] = array(
'title' => 'Islandora Simple Map',
'description' => 'Configure the Islandora Simple Map module.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('islandora_simple_map_admin_settings'),
'type' => MENU_NORMAL_ITEM,
'file' => 'includes/admin_form.inc',
);
$items['admin/islandora/tools/islandora_simple_map/settings'] = array(
'title' => 'Settings',
'description' => 'Configure the Islandora Simple Map module.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('islandora_simple_map_admin_settings'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'includes/admin_form.inc',
'weight' => -9,
);
$items['admin/islandora/tools/islandora_simple_map/collections'] = array(
'title' => 'Collection Maps',
'description' => 'Collections displaying a map of all member points',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_simple_map_admin_collection_display'),
'access callback' => 'islandora_simple_map_collection_admin_access',
'type' => MENU_LOCAL_TASK,
'file' => 'includes/admin_form.inc',
);
$items['islandora/object/%islandora_object/maps'] = array(
'title' => 'Collection Map',
'description' => 'All points from the collection in one place.',
'page callback' => 'islandora_simple_map_collection_map_view',
'page arguments' => array(2),
'access callback' => 'islandora_simple_map_collection_tab_access',
'access arguments' => array(2),
'type' => MENU_LOCAL_TASK,
);
$items['islandora_simple_map/marker/%islandora_object'] = array(
'title' => 'Map marker',
'description' => 'Ajax call to get object information for the map marker',
'page callback' => '_islandora_simple_map_marker_info',
'page arguments' => array(2),
'access callback' => 'islandora_object_access',
'access arguments' => array(ISLANDORA_VIEW_OBJECTS, 2),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_theme().
*/
function islandora_simple_map_theme() {
return array(
'islandora_simple_map' => array(
'variables' => array(
'coords' => NULL,
'iframe_width' => NULL,
'iframe_height' => NULL,
'zoom' => NULL,
'collapsed' => NULL,
),
'path' => drupal_get_path('module', 'islandora_simple_map') . '/theme',
'template' => 'islandora_simple_map',
),
);
}
/**
* Access check for admin page of collections displaying maps.
*
* @return bool
* Does user have privilege and are collections enabled.
*/
function islandora_simple_map_collection_admin_access() {
return (user_access('administer site configuration') &&
_islandora_simple_map_collection_map_enabled());
}
/**
* Access check for collection map tab.
*
* @param \AbstractObject $object
* The object being viewed.
*
* @return bool
* Does user have privilege to see/access the tab.
*/
function islandora_simple_map_collection_tab_access(AbstractObject $object) {
return (islandora_user_access($object, array(ISLANDORA_VIEW_OBJECTS)) &&
_islandora_simple_map_collection_map_enabled() &&
_islandora_simple_map_display_collection_map($object->id));
}
/**
* Implements hook_islandora_view_object_alter().
*/
function islandora_simple_map_islandora_view_object_alter(&$object, &$rendered) {
if (in_array('islandora:compoundCModel', $object->models) &&
variable_get('islandora_simple_map_omit_compound_display', TRUE)) {
return array();
}
$found_coords = islandora_simple_map_process_coordinates($object);
if (isset($found_coords['coordinates']) && count($found_coords['coordinates']) > 0) {
$width = (int) variable_get('islandora_simple_map_iframe_width', '600');
$height = (int) variable_get('islandora_simple_map_iframe_height', '600');
$zoom = (int) variable_get('islandora_simple_map_zoom', '10');
$collapsed = variable_get('islandora_simple_map_collapsed', 'collapsed');
if (variable_get('islandora_simple_map_use_gmaps_api', FALSE)) {
if (variable_get('islandora_simple_map_disable_page_display', FALSE)) {
return array();
}
$map_div_id = drupal_html_id('islandora_simple_map_page');
$settings['islandora_simple_map'][$map_div_id] = array(
'map_markers' => $found_coords,
'map_div_id' => $map_div_id,
'map_zoom_level' => $zoom,
'disable_scroll_zoom' => (bool) variable_get('islandora_simple_map_disable_scroll', FALSE),
);
$content = array(
'islandora_simple_map' => array(
'#type' => 'fieldset',
'#title' => t('Map'),
'#weight' => 100,
'#attributes' => array(
'class' => array('collapsible', ($collapsed ? 'collapsed' : '')),
),
'islandora_simple_map_div' => array(
'#type' => 'container',
'#attributes' => array(
'id' => $map_div_id,
'class' => array('islandora-simple-map-holder', 'islandora-simple-map-page'),
),
),
'#attached' => _islandora_simple_map_generate_js_code($settings),
),
);
$content['islandora_simple_map']['#attached']['css'][] = array(
'type' => 'inline',
'data' => "#{$map_div_id} {\nheight: {$height}px;\nwidth: {$width}px;\n}",
);
}
else {
$width = variable_get('islandora_simple_map_iframe_width', '600');
$height = variable_get('islandora_simple_map_iframe_height', '600');
$zoom = variable_get('islandora_simple_map_zoom', '10');
$collapsed = variable_get('islandora_simple_map_collapsed', 'collapsed');
$markup = theme('islandora_simple_map', array(
// We use the first set of coords if our XPaths found more than one.
'coords' => urlencode($found_coords['coordinates'][0]),
'iframe_width' => $width,
'iframe_height' => $height,
'zoom' => $zoom,
'collapsed' => $collapsed,
));
}
// Some Islandora render arrays have a single member, with a key of NULL.
if (isset($rendered[NULL])) {
if (isset($rendered[NULL]['#markup'])) {
if (isset($content) && !isset($markup)) {
// Don't render unless/until we need to.
$markup = render($content);
}
$rendered[NULL]['#markup'] = $rendered[NULL]['#markup'] . $markup;
}
elseif (is_array($rendered[NULL])) {
$rendered[NULL]['islandora_simple_map'] = $content['islandora_simple_map'];
}
}
else {
// Others have several members, depending on the content model.
$member = islandora_simple_map_get_rendered_member($object->models);
if (isset($rendered[$member]['#markup'])) {
if (isset($content) && !isset($markup)) {
// Don't render unless/until we need to.
$markup = render($content);
}
$rendered[$member]['#markup'] = $rendered[$member]['#markup'] . $markup;
}
elseif (isset($rendered[$member]) && is_array($rendered[$member])) {
// These are render arrays.
if (isset($content)) {
// We have a render array to add it to the bottom.
$rendered[$member]['islandora_simple_map'] = $content['islandora_simple_map'];
}
else {
// Add JS needed and make a markup render array element.
drupal_add_js('misc/form.js');
drupal_add_js('misc/collapse.js');
$rendered[$member]['islandora_simple_map'] = array(
'#type' => 'markup',
'#markup' => $markup,
'#weight' => 100,
);
}
}
}
}
}
/**
* Determines which member of the object's render array to append map to.
*
* @param array $models
* The object's content models.
*
* @return string
* The key identifiying the member in the render array.
*/
function islandora_simple_map_get_rendered_member(array $models) {
// Use the first content model. We don't need to worry about basic and
// large images, PDFs, and several other content types. We only need to
// identify content models that do not have a render array that uses a
// NULL key for the markup.
$model = $models[0];
switch ($model) {
case 'islandora:bookCModel':
$member = 'islandora_book';
break;
case 'islandora:newspaperCModel':
$member = 'islandora_newspaper';
break;
case 'islandora:collectionCModel':
$member = 'collection_view';
break;
case 'ir:thesisCModel':
case 'ir:citationCModel':
$member = 'citation.tab';
break;
default:
$member = '';
}
return $member;
}
/**
* Implements hook_islandora_basic_collection_build_manage_object().
*
* Display a form on the Collection tab of Manage object of collections.
*/
function islandora_simple_map_islandora_basic_collection_build_manage_object(array $form_state, AbstractObject $object) {
module_load_include('inc', 'islandora_simple_map', 'includes/manage_collection_form');
$form_state['manage_collection_object']['manage_islandora_simple_maps'] = array(
'#id' => 'manage-islandora-simple-maps',
'#group' => 'manage_collection_object',
'#access' => (islandora_user_access($object, array(ISLANDORA_METADATA_EDIT))
&& _islandora_simple_map_collection_map_enabled()),
'#type' => 'fieldset',
'#title' => t('Manage Islandora Simple Maps'),
'form' => drupal_get_form('islandora_simple_map_collection_manage_form', $object),
);
return $form_state;
}
/**
* Generate a map of points from the members of this collection.
*
* @param \AbstractObject $object
* The collection object.
*
* @return array
* Render array to display.
*/
function islandora_simple_map_collection_map_view(AbstractObject $object) {
$coordinates = _islandora_simple_map_get_collection_points($object);
if (count($coordinates) > 0) {
$zoom = (int) variable_get('islandora_simple_map_zoom', '10');
$map_div_id = drupal_html_id('islandora_simple_map_collection_page');
$settings['islandora_simple_map'][$map_div_id] = array(
'map_markers' => $coordinates,
'map_div_id' => $map_div_id,
'map_zoom_level' => $zoom,
'disable_scroll_zoom' => FALSE,
);
$content = array(
'islandora_simple_map' => array(
'islandora_simple_map_div' => array(
'#type' => 'container',
'#attributes' => array(
'id' => $map_div_id,
'class' => array('islandora-simple-map-holder', 'islandora-simple-map-collection-page'),
),
'#attached' => _islandora_simple_map_generate_js_code($settings),
),
),
);
}
else {
$content = array(
'islandora_simple_map' => array(
'islandora_simple_map_div' => array(
'#type' => 'markup',
'#markup' => 'Collection has no points to display',
'#prefix' => '<div>',
'#suffix' => '</div>',
),
),
);
}
return $content;
}
/**
* Return information for marker infowindow in json.
*
* @param \AbstractObject $object
* The object being asked about.
*
* @return string
* Json encoded information.
*/
function _islandora_simple_map_marker_info(AbstractObject $object) {
$pid = $object->id;
$output = array(
'pid' => check_plain($object->id),
'label' => check_plain($object->label),
'uri' => url("islandora/object/{$pid}"),
);
if (isset($object['TN']) && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $object['TN'])) {
$output['thumbnail_uri'] = url("islandora/object/{$pid}/datastream/TN/view");
}
drupal_json_output($output);
}