-
Notifications
You must be signed in to change notification settings - Fork 1
/
islandora_catalog_search.module
323 lines (278 loc) · 10.2 KB
/
islandora_catalog_search.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
<?php
/**
* @file
* Module to add catalog search feature to citations
*/
/**
* Page callback: Islandora catalog search settings
*
* @see islandora_catalog_search_menu()
*/
function islandora_catalog_search_form($form, &$form_state) {
// checkbox options
$form['islandora_catalog_search_options'] = array(
'#type' => 'checkboxes',
'#title' => t('Options'),
'#options' => array(
'new_window' => t('Open link in new window'),
'use_image' => t('Use image for link'),
),
'#default_value' => variable_get('islandora_catalog_search_options', ''),
);
// catalog url
$form['islandora_catalog_search_url'] = array(
'#type' => 'textfield',
'#title' => t('Catalog URL'),
'#default_value' => variable_get('islandora_catalog_search_url', ''),
'#size' => 100,
'#description' => t('The base URL to your catalog. Must start with "http://"'),
);
// http parameters
$form['islandora_catalog_search_url_title'] = array(
'#type' => 'textfield',
'#title' => t('HTTP title parameter'),
'#default_value' => variable_get('islandora_catalog_search_url_title', ''),
'#size' => 100,
'#description' => t('The HTTP parameter for article titles'),
);
$form['islandora_catalog_search_url_author'] = array(
'#type' => 'textfield',
'#title' => t('HTTP author parameter'),
'#default_value' => variable_get('islandora_catalog_search_url_author', ''),
'#size' => 100,
'#description' => t('The HTTP parameter for article authors'),
);
$form['islandora_catalog_search_url_issn'] = array(
'#type' => 'textfield',
'#title' => t('HTTP issn parameter'),
'#default_value' => variable_get('islandora_catalog_search_url_issn', ''),
'#size' => 100,
'#description' => t('The HTTP parameter for article ISSN'),
);
// extra parameters
$form['islandora_catalog_search_url_extra'] = array(
'#type' => 'textfield',
'#title' => t('HTTP extra parameters'),
'#default_value' => variable_get('islandora_catalog_search_url_extra', ''),
'#size' => 100,
'#maxlength' => 4096,
'#description' => t('Extra HTTP parameters. Can have multiple of the form key=value separated by spaces'),
);
// upload image
$form['islandora_catalog_search_link_image'] = array(
'#type' => 'managed_file',
'#name' => 'link_image',
'#title' => t('Link Image'),
'#default_value' => variable_get('islandora_catalog_search_link_image', ''),
'#description' => t("Use this image as the link to your catalog"),
'#upload_location' => 'public://catalog_link_image',
);
// image size
$form['islandora_catalog_search_link_image_width'] = array(
'#type' => 'textfield',
'#title' => t('Image Width'),
'#default_value' => variable_get('islandora_catalog_search_link_image_width', '150'),
'#size' => 100,
'#description' => t('The width of the link image'),
);
$form['islandora_catalog_search_link_image_height'] = array(
'#type' => 'textfield',
'#title' => t('Image Height'),
'#default_value' => variable_get('islandora_catalog_search_link_image_height', '50'),
'#size' => 100,
'#description' => t('The height of the link image'),
);
// link text
$form['islandora_catalog_search_link_text'] = array(
'#type' => 'textfield',
'#title' => t('Link Text'),
'#default_value' => variable_get('islandora_catalog_search_link_text', 'Check our catalog'),
'#size' => 100,
'#description' => t('The Text to display for the link to the catalog if not using an image'),
);
// submit handler to save link image
$form['#submit'][] = 'islandora_catalog_search_form_submit';
return system_settings_form($form);
}
/*
* Additional submit handler for making the link image file permanent
*/
function islandora_catalog_search_form_submit($form, &$form_state) {
$file = $form['islandora_catalog_search_link_image']['#file'];
if (is_object($file)) {
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
// prevent 'file not referenced' warning
file_usage_add($file, 'islandora_catalog_link', 'node', 1);
}
}
/**
* Validate the size of the image specified in the admin form
*/
function islandora_catalog_search_form_validate($form, &$form_state) {
$width = $form_state['values']['islandora_catalog_search_link_image_width'];
$height = $form_state['values']['islandora_catalog_search_link_image_height'];
if (!is_numeric($width)) {
form_set_error('islandora_catalog_search_link_image_width', t('You must enter an integer for the image width.'));
}
elseif (!is_numeric($height)) {
form_set_error('islandora_catalog_search_link_image_height', t('You must enter an integer for the image height.'));
}
elseif ($width <= 0) {
form_set_error('islandora_catalog_search_link_image_width', t('Image width must be positive.'));
}
elseif ($height <= 0) {
form_set_error('islandora_catalog_search_link_image_height', t('Image height must be positive.'));
}
}
/**
* Implements hook_menu().
*/
function islandora_catalog_search_menu() {
$items = array();
$items['admin/islandora/islandora_catalog_search'] = array(
'title' => 'Islandora Catalog Search',
'description' => 'Configuration for Islandora Catalog Search module',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_catalog_search_form'),
'access arguments' => array('manage islandora catalog search'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_block_view().
*/
function islandora_catalog_search_block_view($delta = '') {
switch ($delta) {
case 'catalog_link':
$block['subject'] = t('Link to a library catalog');
$block['content'] = get_catalog_link();
break;
}
return $block;
}
/**
* Implements hook_block_info().
*/
function islandora_catalog_search_block_info() {
// display a link to search the catalog
$blocks['catalog_link'] = array(
'info' => t('Link to library catalog'),
'visibility' => BLOCK_VISIBILITY_LISTED,
'pages' => 'islandora/object/citations*',
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Create the catalog link.
*/
function get_catalog_link() {
$options = variable_get('islandora_catalog_search_options', '');
$link_options = array('external' => TRUE, 'absolute' => TRUE);
if ($options['new_window'])
$link_options['attributes'] = array('target' => '_blank');
if ($options['use_image']) {
$image_fid = variable_get('islandora_catalog_search_link_image');
$image = file_load($image_fid);
$image_url = file_create_url($image->uri);
$width = variable_get('islandora_catalog_search_link_image_width', '150');
$height = variable_get('islandora_catalog_search_link_image_height', '50');
$title = variable_get('islandora_catalog_search_link_text', 'Check our catalog');
$link_options['html'] = TRUE;
$display = '<img title="' . $title . '" src="' . $image_url . '" style="height:' . $height . 'px; width:' . $width . 'px;"/>';
}
else {
$display = variable_get('islandora_catalog_search_link_text', 'Check our catalog');
}
// build the URL
$url = variable_get('islandora_catalog_search_url', '');
// add the HTTP parameters
add_params($link_options);
return l($display, $url, $link_options);
}
/*
* Add the HTTP parameters to the URL
* Code adapted from https://github.com/Islandora/islandora_scholar/blob/7.x/modules/islandora_google_scholar/islandora_google_scholar.module
*/
function add_params(&$link_options) {
// get citation object
$item = menu_get_item();
$object = $item['map'][2];
// get mods data if available
if (!isset($object['MODS']) || !islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $object['MODS']))
return '';
$mods = $object['MODS']->content;
$mods_xml = new SimpleXMLElement($mods);
$mods_xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
$link_options['query'] = array();
// get title
$title_key = variable_get('islandora_catalog_search_url_title', '');
$title_results = $mods_xml->xpath('//mods:mods[1]/mods:titleInfo/mods:title');
$link_options['query'][$title_key] = (string) reset($title_results);
// get first author
$author_key = variable_get('islandora_catalog_search_url_author', '');
$name_xml = $mods_xml->xpath('mods:name');
if ($name_xml != null) {
$name_xml = $name_xml[0];
$name_parts = array();
$name_xml->registerXPathNamespace('mods', 'http://www.loc.gov/mods/v3');
$roles = $name_xml->xpath('mods:role/mods:roleTerm');
$role = strtolower((string) reset($roles));
if ($role) {
if ($role == 'author') {
foreach ($name_xml->xpath('mods:namePart') as $name_part) {
if ((string) $name_part != '') {
// Strip periods off the end.
$np = (string) rtrim($name_part, '.');
if ($name_part['type'] == 'given') {
$name_parts['first_name'] = (string) $name_part;
}
if ($name_part['type'] == 'family') {
$name_parts['last_name'] = $np;
}
if (!isset($name_part['type'])) {
$name_parts['no_type'] = $np;
}
}
}
}
if (isset($name_parts['last_name']) && isset($name_parts['first_name'])) {
$link_options['query'][$author_key] = $name_parts['first_name'] . ' ' . $name_parts['last_name'];
}
elseif (isset($name_parts['no_type'])) {
$link_options['query'][$author_key] = $name_parts['no_type'];
}
}
}
// get ISSN
$issn_key = variable_get('islandora_catalog_search_url_issn', '');
$issn = $mods_xml->xpath('//mods:identifier[@type="issn"]');
if ($issn) {
$link_options['query'][$issn_key] = str_replace(
array("&", "=", ",", ";"),
array('', '', '', ''),
(string) reset($issn));
}
// add extras
$extras = variable_get('islandora_catalog_search_url_extra', '');
$arr = explode(" ", $extras);
foreach ($arr as $extra) {
$param = explode("=", $extra);
if (count($param) != 2)
continue;
$link_options['query'][$param[0]] = $param[1];
}
}
/**
* Implements hook_permission
*/
function islandora_catalog_search_permission() {
return array(
'manage islandora catalog search' => array(
'title' => t('Manage Islandora Catalog Search'),
'description' => t('Perform admin tasks for the Islandora Catalog Search Module'),
)
);
}