forked from dingproject/ting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ting.module
476 lines (416 loc) · 13.3 KB
/
ting.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php // $Id$
/**
* @file ting.module
* Module to integrate the Ting search engine in our Drupal site.
*/
/**
* Implementation of hook_menu().
*/
function ting_menu() {
$path = drupal_get_path('module', 'ting') . '/includes';
$items = array();
$items['ting/object/redirect'] = array(
'title' => 'Redirect to object based on local ID (links from bibliotek.dk).',
'page callback' => 'ting_search_pages_object_redirect',
'access arguments' => array('search content'),
'type' => MENU_CALLBACK,
'file' => 'ting.pages.inc',
);
$items['admin/settings/ting'] = array(
'title' => 'Ting search',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_admin_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'ting.admin.inc',
'file path' => $path,
);
$items['admin/settings/ting/access'] = array(
'title' => 'Ting access',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
return $items;
}
/**
* Menu loader function.
*/
function ting_id_load($id) {
module_load_include('client.inc', 'ting');
return ting_get_object_by_id($id);
}
/**
* Menu loader function.
*/
function ting_local_id_load($id) {
module_load_include('client.inc', 'ting');
return ting_get_object_by_local_id($id);
}
/**
* Add additional information to a ting object.
*/
function ting_enrich_object(&$object) {
$object = array_shift(ting_add_additional_info(array($object)));
}
/**
* Implementation of hook_theme().
*/
function ting_theme() {
return array(
'ting_thing' => array(
'arguments' => array('type' => 'object', 'object' => NULL, 'query_args' => NULL),
),
'ting_list' => array(
'arguments' => array('ting_list' => NULL, 'sorted' => FALSE),
'template' => 'ting-list',
),
'ting_list_item' => array(
'arguments' => array('object' => NULL),
'template' => 'ting-list-item',
),
'ting_tabs' => array(
'arguments' => array('tabs' => NULL, 'wrapper_id' => NULL),
'template' => 'ting-tabs',
),
'ting_collection' => array(
'arguments' => array('collection' => NULL, 'query_args' => NULL),
'template' => 'ting_collection',
),
'ting_object' => array(
'arguments' => array('object' => NULL),
'template' => 'ting_object',
),
'ting_object_title' => array(
'arguments' => array('object' => NULL, 'display_image' => NULL),
'template' => 'ting-object-title',
),
);
}
/**
* Implementation of hook_ctools_plugin_api().
*/
function ting_ctools_plugin_api($module, $api) {
if ($module == 'page_manager' && $api == 'pages_default') {
return array('version' => 1);
}
}
/**
* Implementation of hook_ctools_plugin_directory().
*
* Tells CTools (and thus Panels) where to look for plugin code.
*/
function ting_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' || $module == 'panels') {
return 'plugins/' . $plugin;
}
}
/**
* Implementation of hook_imagecache_default_presets()
*/
function ting_imagecache_default_presets() {
$presets = array();
$presets['180_x'] = array (
'presetname' => '180_x',
'actions' =>
array (
0 =>
array (
'weight' => '0',
'module' => 'imagecache',
'action' => 'imagecache_scale',
'data' =>
array (
'width' => '180',
'height' => '',
'upscale' => 0,
),
),
),
);
$presets['80_x'] = array (
'presetname' => '80_x',
'actions' =>
array (
0 =>
array (
'weight' => '0',
'module' => 'imagecache',
'action' => 'imagecache_scale',
'data' =>
array (
'width' => '80',
'height' => '',
'upscale' => 1,
),
),
)
);
return $presets;
}
/**
* Implementation of hook_ting_add_js().
*/
function ting_ting_add_js($type) {
// Add button helper on collection and object views if the required jQuery UI modules are loaded
// These are not requirements to avoid unecessary dependencies in ting module
if (in_array($type, array('ting_collection_view', 'ting_object_view'))) {
if (module_exists('jquery_ui') && module_exists('jquery_ui_theme')) {
jquery_ui_add('ui.dialog');
jquery_ui_theme_load();
drupal_add_js(drupal_get_path('module', 'ting') . '/js/ting.buttons.js');
}
}
}
/**
* Implementation of hook_ting_object_buttons().
*/
function ting_ting_object_buttons($object) {
if (!empty($object->localId)) {
if (!empty($object->online_url) &&
ting_object_is($object, 'online')) {
if (in_array(drupal_strtolower($object->type), array('lydbog (online)', 'netmusik (album)'))) {
$text = t('Hear online');
}
else {
$text = t('See online');
}
return array(
array(
'data' => l($text, $object->online_url),
'class' => 'view-online',
)
);
}
}
}
/**
* Theme something from Ting.
*/
function theme_ting_thing($type, $object, $query_args = NULL) {
static $displayed_relation_types = array(
'isAuthorDescriptionOf',
'hasAuthorDescription',
'hasReview',
'isReviewOf',
'hasAnalysis',
'isAnalysisOf',
'isSubjectDescriptionOf',
'hasSubjectDescription',
'hasFulltext',
);
module_invoke_all('ting_add_js', "ting_${type}_view");
$tabs = array();
$tabs['main'] = array(
'title' => t('Materials'),
'content' => theme("ting_$type", $object, $query_args),
);
if (sizeof($object->relations)) {
$relations = array();
foreach ($object->relations as $relation) {
if (in_array($relation->relationType, $displayed_relation_types)) {
$relations[] = $relation;
}
}
if ($relations) {
$tabs['related'] = array(
'title' => t('Related'),
'content' => theme('ting_list', $object->relations),
);
}
}
drupal_alter('ting_tabs', $tabs, $type, $object);
return theme('ting_tabs', $tabs, $type == 'collection' ? 'ting-collection' : 'ting-object');
}
/**
* Template preprocessor for ting-list.
*/
function template_preprocess_ting_list(&$vars) {
if (!$vars['sorted']) {
$sorted = array();
foreach ($vars['ting_list'] as $object) {
$sorted[$object->type][] = $object;
}
ksort($sorted);
$vars['ting_list'] = $sorted;
}
foreach ($vars['ting_list'] as $type => $object_list) {
$vars['ting_list'][$type] = "";
foreach ($object_list as $object) {
$vars['ting_list'][$type] .= theme('ting_list_item', $object);
}
}
}
/**
* Template preprocessor for ting-list-item.
*/
function template_preprocess_ting_list_item(&$vars) {
$object = $vars['object'];
$vars['local_id'] = check_plain($object->localId);
$image_url = ting_covers_object_url($object, '80_x');
if ($image_url) {
$vars['image'] = theme('image', $image_url, '', '', null, false);
}
$vars['date'] = check_plain($object->record['dc:date'][''][0]);
$vars['title'] = l($object->title, $object->url, array('attributes' => array('class' => 'alternative')));
$creators = $object->creators;
$first_creator = array_shift($creators);
$vars['creator'] = l($first_creator, 'ting/search/'. $first_creator, array('attributes' => array('class' => 'author alternative')));
$vars['additional_creators'] = array_filter($creators);
$vars['language'] = check_plain($object->language);
$vars['more_link'] = l(t('More information'), $object->url, array('attributes' => array('class' => 'more-link')));
$vars['type'] = check_plain($object->type);
// Let other modules add buttons.
$vars['buttons'] = module_invoke_all('ting_object_buttons', $object);
}
/**
* Template preprocessor for ting tabs.
*/
function template_preprocess_ting_tabs(&$vars) {
// jQuery UI tabs
jquery_ui_add('ui.tabs');
jquery_ui_theme_load();
drupal_add_js(drupal_get_path('module', 'ting') . '/js/ting.js', 'module');
$tab_labels = array();
$tab_content = array();
foreach ($vars['tabs'] as $id => $tab) {
$id = form_clean_id($id);
// Cannot use l/url, they can't generate a link with only a fragment. Also
// we don't want any 'active' classes.
$tab_labels[] = array(
'data' => "<a href='#$id'>" . check_plain($tab['title']) . "</a>",
);
$tab_content[$id] = $tab['content'];
}
$vars['tabs_labels'] = theme('item_list', $tab_labels);
$vars['tabs_content'] = $tab_content;
}
/**
* Template preprocessor for collections of ting objects.
*/
function template_preprocess_ting_collection(&$variables) {
// Preprocess the collection object here!
$collection = $variables["collection"];
// Allow modules to add action buttons to collection objects
$variables['buttons'] = array();
foreach ($collection->objects as $object) {
$variables['buttons'][$object->id] = module_invoke_all('ting_object_buttons', $object);
}
$sorted = array();
$object_list = array();
// If we know what query lead to the collection then we can use this for sorting
if ($query_args = $variables['query_args']) {
if ($entry = $query_args['entry']) {
// Build the query for the collection in the search result
$query = '';
//Wrap base query
$query .= (isset($query_args['query'])) ? '('.$query_args['query'].')' : '';
//Extend query with selected facets
if (isset($query_args['facets'])) {
foreach ($query_args['facets'] as $name => $value) {
$facetArray[] = $name.'="'.$value.'"';
}
$query .= ' AND '.implode(' AND ', $facetArray);
}
// Get the corresponding entry in the search result
$result = ting_do_search($query, $entry, 1, array('facets' => array()));
//Determine the order of object types based on the order of objects
//in the search result
if ($search_collection = array_shift($result->collections)) {
foreach ($search_collection->objects as $object) {
$sorted[$object->type] = array();
}
}
//Put all objects from original collection into the ordered object type buckets.
//Types not referenced in the search result placed last
foreach ($collection->objects as $object) {
$sorted[$object->type][] = $object;
}
}
}
//Default to standard sorting
if (sizeof($sorted) == 0) {
foreach ($collection->objects as $object) {
$sorted[$object->type][] = $object;
}
// By coincidence, the default type, "Bog", appears alphabetically first.
ksort($sorted);
}
$variables["sorted_collection"] = $sorted;
$variables['ting_list'] = theme('ting_list', $sorted, TRUE);
}
/**
* Template preprocessor for ting objects.
*/
function template_preprocess_ting_object(&$variables) {
$object = $variables['object'];
// Allow modules to add action buttons to objects
$variables['buttons'] = module_invoke_all('ting_object_buttons', $object);
$image_url = ting_covers_object_url($object, '180_x');
if ($image_url) {
$variables['image'] = theme('image', $image_url, '', '', NULL, FALSE);
}
$variables['title'] = check_plain($object->record['dc:title'][''][0]);
$titles = $object->record['dc:title'];
unset($titles['']);
$titles = array_values(array_map('array_shift', $titles));
$variables['other_titles'] = check_plain(implode(', ', $titles));
$variables['alternative_titles'] = array();
if ($object->record['dcterms:alternative']['']) {
$titles = array_values($object->record['dcterms:alternative']['']);
$variables['alternative_titles'] = array_map('check_plain', $titles);
}
$creators = array();
foreach ($object->creators as $i => $creator) {
$creators[] = l($creator, 'ting/search/' . $creator, array('attributes' => array('class' => 'author')));
}
$variables['creators'] = implode(', ', $creators);
if (!empty($object->date)) {
$variables['date'] = check_plain($object->date);
}
$variables['abstract'] = check_plain($object->record['dcterms:abstract'][''][0]);
}
/**
* Parse a string containing search query arguments into an array
* @return array Array of search query arguments
*/
function _ting_search_parse_query_args($query_string) {
$query_args = array();
$query_elements = explode(';', $query_string);
foreach ($query_elements as &$e) {
$e = explode(':', $e, 2);
if (strpos($e[1], ':') !== FALSE) {
$query_args[$e[0]] = array();
$e_args = explode(':', $e[1], 2);
$query_args[$e[0]][$e_args[0]] = $e_args[1];
} else {
$query_args[$e[0]] = $e[1];
}
}
return $query_args;
}
/**
* Return whether an object is of a given pseudo-class.
*
* Modules can add their own pseudo-classes by implementing
* hook_ting_object_is(). This function will call all implementations and
* return the overall result.
*
* Modules may return TRUE, FALSE or NULL from the hook. If any modules
* returned FALSE, the result is FALSE, else the result is TRUE if anyone
* returned TRUE, FALSE otherwise.
*/
function ting_object_is($object, $class) {
$result = module_invoke_all('ting_object_is', $object, $class);
// Return true if anyone returned true, and nobody returned false.
if (!in_array(FALSE, $result) && in_array(TRUE, $result)) {
return TRUE;
}
return FALSE;
}
/**
* Implementation of hook_ting_object_is().
*/
function ting_ting_object_is($object, $class) {
// Known online types.
if ($class == 'online' and in_array(drupal_strtolower($object->type), array('lydbog (online)', 'netmusik (album)', 'netdokument'))) {
return TRUE;
}
}