-
Notifications
You must be signed in to change notification settings - Fork 1
/
ting_covers_plus.module
297 lines (246 loc) · 10.5 KB
/
ting_covers_plus.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
<?php
/**
* @file ting_covers_plus.module
*
* Provides:
* - plugins for retrieving covers for Ting objects and collections from additional cover providers
* - extra default covers and interface for administering them
*/
/**
* Implements hook_init().
*/
function ting_covers_plus_init() {
if (arg(0) == 'admin') {
$path = drupal_get_path('module', 'ting_covers_plus');
drupal_add_css($path . '/css/admin.css');
}
}
/**
* Implements hook_menu().
*/
function ting_covers_plus_menu() {
$items = array();
$items['ting/coversplus'] = array(
'title' => 'Retreives cover for Ting objects',
'page callback' => 'ting_covers_plus_objects',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
'file' => 'ting_covers_plus.pages.inc',
);
$items['admin/config/ting/covers/defaults'] = array(
'title' => 'Default covers',
'description' => 'Configure settings for default covers.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_covers_plus_admin_default_covers_form'),
'access arguments' => array('administer ting settings'),
'file' => 'ting_covers_plus.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
/**
* Implements hook_preprocess_ting_object_cover().
*/
function ting_covers_plus_preprocess_ting_object_cover(&$variables) {
$object = $variables['object'] = $variables['elements']['#object'];
$ac_source = !empty($object->reply->record['ac:source'][''][0]) ? $object->reply->record['ac:source'][''][0] : 'unknown'; //array_shift($object->reply->record['ac:source']);
$dc_type = !empty($object->reply->record['dc:type']['dkdcplus:BibDK-Type'][0]) ? $object->reply->record['dc:type']['dkdcplus:BibDK-Type'][0] : (!empty($object->reply->record['dc:type'][''][0]) ? $object->reply->record['dc:type'][''][0] : 'unknown');
// Set extra values required by the template.
// We change the strings to lowercase to make them match the values in the variable arrays ting_well_types and ting_well_sources.
$variables['classes'][] = 'ting-cover-mtype-' . mb_strtolower(drupal_clean_css_identifier($dc_type), 'utf-8');
$variables['classes'][] = 'ting-cover-source-' . mb_strtolower(drupal_clean_css_identifier($ac_source), 'utf-8');
}
/**
* Implements hook_field_formatter_info().
*/
function ting_covers_plus_field_formatter_info() {
return array(
'ting_cover_default' => array(
'label' => t('Default'),
'field types' => array('ting_cover'),
'settings' => array(
'image_style' => 'medium',
),
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function ting_covers_plus_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#theme' => 'ting_object_cover',
'#object' => $entity,
'#image_style' => $display['settings']['image_style'],
'#attached' => array(
'js' => array(drupal_get_path('module', 'ting_covers_plus') . '/js/ting-covers-plus.js'),
),
);
}
return $element;
}
/**
* Function to call all extra covers sources that have been defined.
*
* The array $missing_images_local_ids is modified to include
* additional image URLs to external sources.
*
* @param array $missing_images_local_ids (by reference)
* @param array $covers (by reference)
* @param array $image_styles;
*/
function ting_covers_plus_get_image_urls(&$missing_images_local_ids, &$covers, $image_styles) {
// Calls to external image sources for specific sources
foreach ($missing_images_local_ids as $local_id_key => $local_id) {
// Danske Billeder source
if ($local_id->sourceName == 'Danske-Billeder') {
$covers[] = array(
'local_id' => $local_id->localIdentifier,
'image_style' => $image_styles[$local_id->localIdentifier],
'url' => ting_covers_plus_cover_url_dkbilleder($local_id, $image_styles[$local_id->localIdentifier]),
);
// Remove from missing images array so ADDI service won't call it later
unset($missing_images_local_ids[$local_id_key]);
}
// Historisk Atlas source
if ($local_id->sourceName == 'Historisk-Atlas') {
$covers[] = array(
'local_id' => $local_id->localIdentifier,
'image_style' => $image_styles[$local_id->localIdentifier],
'url' => ting_covers_plus_cover_url_histatlas($local_id, $image_styles[$local_id->localIdentifier]),
);
// Remove from missing images array so ADDI service won't call it later
unset($missing_images_local_ids[$local_id_key]);
}
}
// Reset array index before traversing it again
reset($missing_images_local_ids);
}
/**
* Helper function returning current (alternative) search profile id
*
* This function supports ding-sites that employ multiple search
* modules/interfaces (see f.ex. https://github.com/vejlebib/ting_extrasearch).
* It looks for "/search/ting" in the HTTP_REFERER string as it
* it assumes that any other ting search modules will also be implemented
* starting with "ting" (such as the URL-path "/search/tingextra/").
* It then extracts the needed string from HTTP_REFERER, and (again) makes
* an assumption that a variable named according to the pattern
* "ting_{EXTRACTEDSTRING}search_profile" will exist.
*
* @return string
* ID of a well search profile, or NULL if the current search
* profile is the default.
*/
function _ting_covers_plus_alt_search_profile_ID() {
$profile_val = NULL;
$referer = $GLOBALS[_SERVER][HTTP_REFERER];
if (strpos($referer, '/search/ting') !== false) {
$referer_parts = explode('/', $referer);
array_pop($referer_parts); // Removes last part of the array
if(end($referer_parts)) {
$ting_search_part = str_replace('ting', '', end($referer_parts));
if ($ting_search_part != '') {
$profile_val = "ting_" . $ting_search_part . "search_profile";
}
}
}
return $profile_val;
}
/**
* Function to get cover URL from provider Historisk Atlas
*
* @param object $local_id: is a 2-part identifier containing $local_id->libraryCode and $local_id->localIdentifier
* @return string: url to image from Historisk Atlas
*/
function ting_covers_plus_cover_url_histatlas($local_id, $image_style) {
$histatlas_url = '';
// Call the well with agency (ex. 763000) and the 2-part identifier (owner_id:local_id, eks. 150043:3802)
require_once drupal_get_path('module', 'ting') . '/ting.client.inc';
$request = ting_get_request_factory()->getObjectRequest();
if ($agency = variable_get('ting_agency', FALSE)) {
$request->setAgency($agency);
}
$alt_search_profile_ID = _ting_covers_plus_alt_search_profile_ID();
if($alt_search_profile_ID) {
$profile = variable_get($alt_search_profile_ID, '');
}
else {
$profile = variable_get('ting_search_profile', '');
}
if (!empty($profile) && method_exists($request, 'setProfile')) {
$request->setProfile($profile);
}
// The identifier is libraryCode (aka owner_id) + local ID
$request->setObjectId($local_id->libraryCode . ':' . $local_id->localIdentifier);
$request->setFormat('opensearchobject');
$request->setObjectFormat('opensearchobject');
$request->setAllRelations('true');
$request->setRelationData('full');
// Set to PHP output instead of JSON to retrieve the relations - in JSON they are not present
$request->setOutputType('php');
$histatlas_result = ting_get_client()->execute($request);
// Get URL to image from Historisk Atlas
$histatlas_url = unserialize($histatlas_result)->searchResponse->_value->result->_value->searchResult[0]->_value->collection->_value->object[0]->_value->relations->_value->relation[1]->_value->relationObject->_value->object->_value->object->_value->image->_value;
if (!empty($histatlas_url)) {
$histatlas_url = $histatlas_url . '?width=200';
// Try to download the image locally.
if ($file = _ting_covers_pages_fetch_image(ting_covers_object_path($local_id->localIdentifier), $histatlas_url)) {
// Generate a path corresponding to the downloaded image, styled.
$histatlas_url = image_style_url($image_style, $file);
}
}
return $histatlas_url;
}
/**
* Function to get cover URL from provider Danske Billeder
*
* @param object $local_id: is a 2-part identifier containing $local_id->libraryCode and $local_id->localIdentifier
* @return string: url to image from Danske Billeder
*/
function ting_covers_plus_cover_url_dkbilleder($local_id, $image_style) {
$dkbilleder_url = '';
// Call the well with agency (ex. 763000) and the 2-part identifier (owner_id:local_id, eks. 150043:3802)
require_once drupal_get_path('module', 'ting') . '/ting.client.inc';
$request = ting_get_request_factory()->getObjectRequest();
if ($agency = variable_get('ting_agency', FALSE)) {
$request->setAgency($agency);
}
$alt_search_profile_ID = _ting_covers_plus_alt_search_profile_ID();
if($alt_search_profile_ID) {
$profile = variable_get($alt_search_profile_ID, '');
}
else {
$profile = variable_get('ting_search_profile', '');
}
if (!empty($profile) && method_exists($request, 'setProfile')) {
$request->setProfile($profile);
}
// The identifier is libraryCode (aka owner_id) + local ID
$request->setObjectId($local_id->libraryCode . ':' . $local_id->localIdentifier);
$request->setFormat('opensearchobject');
$request->setObjectFormat('opensearchobject');
$request->setAllRelations('true');
$request->setRelationData('full');
// Set to PHP output instead of JSON to retrieve the relations - in JSON they are not present
$request->setOutputType('php');
$dkbilleder_result = ting_get_client()->execute($request);
// Get URL to image from Danske Billeder
$dkbilleder_url = unserialize($dkbilleder_result)->searchResponse->_value->result->_value->searchResult[0]->_value->collection->_value->object[0]->_value->relations->_value->relation[1]->_value->relationObject->_value->object->_value->object->_value->image->_value;
if (!empty($dkbilleder_url)) {
// For some reason, it helps to replace the URL encodings for a certain danish character with the old writing style
$trans = array(
'%C5' => 'Aa',
'%E5' => 'aa',
);
$dkbilleder_url = strtr( $dkbilleder_url, $trans);
// Try to download the image locally.
if ($file = _ting_covers_pages_fetch_image(ting_covers_object_path($local_id->localIdentifier), $dkbilleder_url)) {
// Generate a path corresponding to the downloaded image, styled.
$dkbilleder_url = image_style_url($image_style, $file);
}
}
return $dkbilleder_url;
}