-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathting.theme.inc
962 lines (877 loc) · 31.4 KB
/
ting.theme.inc
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
<?php
/**
* @file
* Theme function and template preprocessors for the Ting module.
*/
/**
* Theme something from Ting.
*/
function theme_ting_thing($type, $object, $query_args = NULL) {
static $displayed_relation_types = array(
'dbcaddi:hasCreatorDescription',
'dbcaddi:hasReview',
'dbcaddi:isReviewOf',
'dbcaddi:hasAnalysis',
'dbcaddi:isAnalysisOf',
'dbcaddi:hasSubjectDescription',
'dbcaddi: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 collections of ting objects.
*/
function template_preprocess_ting_collection(&$vars) {
// Preprocess the collection object here!
$collection = $vars["collection"];
if (!empty($collection->objects)) {
// We use the first object in the collection for determining object
// information. Extract information about this object into variable
// scope.
$vars = array_merge($vars, ting_collection_preprocess($collection));
}
// Build collection details
// Save preprocessing for ting_preprocess_ting_collection() to allow other
// modules to alter this array before rendering it.
$vars['ting_details'] = array(
t('Series') => 'ting_series_links',
t('Classiciation') => 'ting_classification_links',
t('Listing') => 'ting_listing_links',
t('Subjects') => 'ting_subjects_links',
);
// Allow modules to add action buttons to collection objects.
$vars['buttons'] = array();
foreach ($collection->objects as $object) {
$vars['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 = $vars['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) {
$facet_array[] = $name . '="' . $value . '"';
}
$query .= ' AND ' . implode(' AND ', $facet_array);
}
// 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);
}
$vars["sorted_collection"] = $sorted;
$vars['ting_list'] = theme('ting_list', $sorted, TRUE);
}
/**
* Second preprocessing of Ting collections.
*/
function ting_preprocess_ting_collection($vars) {
// Loop through all levels of ting details to create the item lists
// We do this here instead of in template_preprocess_ting_object() to
// let other modules preprocess the ting_details structure before it
// is rendered.
foreach ($vars['ting_details'] as $title => &$var_name) {
if (!empty($vars[$var_name])) {
$var = $vars[$var_name];
if (!is_array($var)) {
$var = array($var);
}
$var_name = array(
'class' => _ting_property_classes($var_name),
'data' => theme('item_list', $var, $title, 'span'),
);
}
else {
unset($vars['ting_details'][$title]);
}
}
$vars['ting_details'] = theme('item_list', $vars['ting_details'], NULL, 'div', array('class' => 'ting-properties'));
}
/**
* Template preprocessor for ting objects.
*/
function template_preprocess_ting_object(&$vars) {
$object = $vars['object'];
// Extract information about the object into variable scope.
$vars = array_merge($vars, ting_object_preprocess($vars['object']));
// Allow modules to add action buttons to objects.
$vars['buttons'] = module_invoke_all('ting_object_buttons', $object);
// Determine the cover url.
$image_url = ting_covers_object_url($object, '180_x');
if ($image_url) {
$vars['image'] = theme('image', $image_url, '', '', NULL, FALSE);
}
// Build a data structure for creating lists of object details.
// The structure has the following levels:
// 1. Detail groups: Currently details, additional details and description
// 2. Object properties: Title, author etc.
// 3. Property values: These are variable names as we only want to include
// them if they are set.
// Each level is passed though theme('item_list')
$details = array(array(
'title' => NULL,
'class' => 'details clearfix',
'data' => array(
t('Classiciation') => 'ting_classification_links',
t('Listing') => 'ting_listing_links',
t('Subjects') => 'ting_subjects_links',
),
),
array(
'title' => NULL,
'class' => 'additional-details clearfix',
'data' => array(
t('Original title') => 'ting_source_title',
t('Contributors') => 'ting_contributors',
t('Coauthors') => 'ting_coauthor',
t('Writer') => 'ting_writer',
t('Translators') => 'ting_translators',
t('Photographers') => 'ting_photographers',
t('Musicians') => 'ting_musicians',
t('Readers') => 'ting_readers',
t('Illustrators') => 'ting_illustrators',
t('Scriptwriters') => 'ting_scriptwriters',
t('Actors') => 'ting_actors',
t('Editors') => 'ting_editors',
t('Coeditors') => 'ting_coeditors',
t('Type') => 'ting_type',
t('Language') => 'ting_language',
t('Edition') => 'ting_edition',
t('Extent') => 'ting_extent',
t('Audience') => 'ting_audience_age',
t('Media council') => 'ting_audience_media_council',
t('PEGI') => 'ting_pegi_rating',
t('Related') => 'ting_related',
t('Subtitles') => 'ting_subtitles',
t('Publisher') => 'ting_publisher',
t('ISBN') => 'ting_isbn',
t('ISSN') => 'ting_isssn',
t('URI') => 'ting_uri',
t('ISMN') => 'ting_ismn',
t('Record label') => 'ting_record_label',
),
),
array(
'title' => NULL,
'class' => 'descrition clearfix',
'data' => array(
t('Description') => 'ting_description',
),
),
);
// If there is more than one author add it to the details.
if (sizeof($vars['ting_creators_links']) > 1) {
$details[1]['data'] = array_merge(array(t('Creators') => 'ting_creators_links'), $details[1]['data']);
}
$vars['ting_details'] = $details;
}
/**
* Second level preprocessor for ting objects.
*/
function ting_preprocess_ting_object(&$vars) {
// Preprocess ting_details for the ting_details theming function.
// Loop through all levels of ting details to collect the items that should be
// rendered.
// We do this here instead of in template_preprocess_ting_object() to let
// other modules preprocess the ting_details structure before it is rendered.
$processed = array();
foreach ($vars['ting_details'] as $group) {
foreach ($group['data'] as $title => $var_name) {
if (!empty($vars[$var_name])) {
$var = $vars[$var_name];
if (!is_array($var)) {
$var = array($var);
}
$group['data'][$title] = array(
'class' => _ting_property_classes($var_name),
'data' => $var,
);
}
else {
unset($group['data'][$title]);
}
}
$processed[] = $group;
}
// If there are any hasDescriptionFromPublisher relations, load tthem
// as docbook and display it as additional content.
// normal abstract with it.
if (sizeof($vars['object']->relations)) {
foreach ($vars['object']->relations as $relation) {
if (strpos($relation->relationType, 'hasDescriptionFromPublisher') !== FALSE && !empty($relation->abstract)) {
$vars['ting_abstract'] = $relation->abstract;
// Load full text version of description if available.
if (in_array('docbook', $relation->formatsAvailable) && function_exists('ting_fulltext_object_load')) {
$docxml = ting_fulltext_object_load($relation->id, FALSE, 'docbook');
if (!empty($docxml)) {
module_load_include('parse.inc', 'ting_fulltext');
$vars['additional_main_content'][]['#children'] = theme('ting_fulltext', ting_fulltext_parse($docxml));
}
}
}
}
}
// If there are any hasDescriptionFromPublisher relations, replace the
// normal abstract with it.
if (sizeof($vars['object']->relations)) {
foreach ($vars['object']->relations as $relation) {
if (strpos($relation->relationType, 'hasDescriptionFromPublisher') !== FALSE && !empty($relation->abstract)) {
$vars['ting_abstract'] = $relation->abstract;
}
}
}
$vars['ting_details'] = theme('ting_details', $processed);
// Generate a list of links to materials of other types in the same //
// collection.
$collection = ting_get_collection_by_id($vars['object']->id);
$material_links = array();
$material_types = array();
if ($collection instanceof TingClientObjectCollection && is_array($collection->types)) {
// Do we have more than only this one type?
if (count($collection->types) > 1) {
foreach ($collection->types as $type) {
$material_types[] = $type;
if ($type != $vars['object']->type) {
// Clean up the type name, so we can use it as a CSS class.
$type_id = preg_replace('/[^a-z]/', '-', drupal_strtolower($type));
$type_id = preg_replace('/--+/', '-', $type_id);
$type_id = trim($type_id, '-');
$material_links['category material-type-' . $type_id] = array(
'title' => $type,
'href' => $collection->url,
);
}
}
}
}
if (!empty($material_links)) {
$vars['alternate_material_types'] = theme('links', $material_links, array(
'class' => 'material-type-list',
));
}
// Expose the available material types as a JavaScript variable, so we
// can use it in scripts.
drupal_add_js(array(
'ting' => array('materialTypes' => array($vars['object']->localId => $material_types)),
), 'setting');
}
/**
* Theme ting_details.
*/
function theme_ting_details($details) {
$groups = array();
foreach ($details as $group) {
$groups[] = array(
'class' => $group['class'],
'data' => theme('ting_details_group', $group['title'], $group['data']),
);
}
return theme('item_list', $groups, NULL, 'ul');
}
/**
* Theme ting_details group.
*/
function theme_ting_details_group($title, $data) {
$items = array();
foreach ($data as $item_title => $item) {
$items[] = array(
'class' => $item['class'],
'data' => theme('item_list', $item['data'], $item_title, 'ul'),
);
}
return theme('item_list', $items, $title, 'ul', array('class' => 'ting-properties'));
}
/**
* Template preprocessor for ting-list.
*/
function template_preprocess_ting_list(&$vars) {
if (!$vars['sorted']) {
$sorted = array();
foreach ($vars['ting_list'] as $object) {
$object_vars = ting_object_preprocess($object);
$sorted[$object_vars['ting_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'];
// Extract information about the object into variable scope.
$vars = array_merge($vars, ting_object_preprocess($object));
$image_url = ting_covers_object_url($object, '80_x');
if ($image_url) {
$vars['image'] = theme('image', $image_url, '', '', NULL, FALSE);
}
$vars['more_link'] = l(t('More information'), $object->url, array('attributes' => array('class' => 'more-link')));
// 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;
}
/**
* Core function for extracting information from a Ting object.
*
* @param TingClientObject $object
* The ting object to process.
*/
function ting_object_preprocess(TingClientObject $object) {
// Key values.
$vars = array(
'ting_local_id' => $object->localId,
'ting_id' => $object->id,
'ting_url' => url('ting/object/' . $object->id, array('absolute' => TRUE)),
);
// Definitions of all the record properties we want to extract
// Keys are the programatic names of properties. This is used to determined
// variable names when extracting the values.
// Each definition consists of:
// - entry: The position in the object record where the value is located
// - namespaces: An array of the allowed namespaces from which to extract
// values. If this is omitted then all namespaces are used.
// - !namespaces: An array of namespaces to ignore. If this is omitted then
// all namespaces are allowed.
// - multiple: If set to true the extracted value is an array of all
// extracted values. Otherwise only the first value from
// a valid namespace is used.
// - links: If set to true an additional variable is set containing link(s)
// to search results for each value.
// - link_facet: If set this facet will be used when creating links instead of
// broad searches
// - html: Whether to allow escape any HTML content contained within the field
// Each property may contain multiple definitions. Handling depends on the
// multiple value.
$properties = array(
'title' => array(
'entry' => 'dc:title',
'namespaces' => array(''),
),
'title_full' => array(
'entry' => 'dc:title',
'namespaces' => array('dkdcplus:full'),
),
'source_title' => array(
'entry' => 'dc:source',
),
'volumne_title' => array(
'entry' => 'dc:title',
'namespaces' => array('oss:full'),
),
'creators' => array(
'entry' => 'dc:creator',
'!namespaces' => array('oss:sort'),
'multiple' => TRUE,
'links' => TRUE,
'link_facet' => 'facet.creator',
),
'publication_date' => array(
'entry' => 'dc:date',
'namespaces' => array(''),
),
'abstract' => array(
'entry' => 'dcterms:abstract',
'namespaces' => array(''),
'html' => TRUE,
),
'description' => array(
'entry' => 'dc:description',
'multiple' => TRUE,
'namespaces' => array(''),
),
'subjects' => array(
'entry' => 'dc:subject',
'namespaces' => array('dkdcplus:genre', 'dkdcplus:DBCF', 'dkdcplus:DBCS', 'dkdcplus:DBCM', 'dkdcplus:DBCO', 'dkdcplus:DBCN', 'dcterms:spatial', 'dcterms:temporal',
/* Included for backwards compatability with DKABM 1.x' */
'oss:genre'),
'multiple' => TRUE,
'links' => TRUE,
),
'classification' => array(
'entry' => 'dc:subject',
'namespaces' => array('dkdcplus:DK5'),
'multiple' => TRUE,
'links' => TRUE,
),
'listing' => array(
'entry' => 'dc:subject',
'namespaces' => array('dkdcplus:DK5-Text'),
'multiple' => TRUE,
'links' => TRUE,
),
'series' => array(
array(
'entry' => 'dc:title',
'namespaces' => array('dkdcplus:series'),
'multiple' => TRUE,
),
array(
'entry' => 'dc:description',
'namespaces' => array('dkdcplus:series'),
'multiple' => TRUE,
),
),
'source' => array(
'entry' => 'ac:source',
),
'type' => array(
'entry' => 'dc:type',
),
'language' => array(
'entry' => 'dc:language',
'namespaces' => array(''),
),
'language_iso639_2' => array(
'entry' => 'dc:language',
'namespaces' => array('dcterms:ISO639-2'),
),
'edition' => array(
'entry' => 'dkdcplus:version',
),
'extent' => array(
'entry' => 'dcterms:extent',
),
'record_label' => array(
'entry' => 'dc:identifier',
'namespaces' => array(''),
),
'audience_age' => array(
'entry' => 'dcterms:audience',
'namespaces' => array('dkdcplus:age'),
'multiple' => TRUE,
),
'audience_media_council' => array(
'entry' => 'dcterms:audience',
'namespaces' => array('dkdcplus:medieraad'),
'multiple' => TRUE,
),
'pegi_rating' => array(
'entry' => 'dcterms:audience',
'namespaces' => array('dkdcplus:pegi'),
),
'related' => array(
array(
'entry' => 'dcterms:hasPart',
'namespaces' => array(''),
'multiple' => TRUE,
'links' => TRUE,
),
array(
'entry' => 'dcterms:hasPart',
'namespaces' => array('', 'dkdcplus:track'),
'multiple' => TRUE,
// Track names for streaming music may be wrapped in direct
// links. Do no escape these.
'html' => TRUE,
'links' => TRUE,
),
array(
'entry' => 'dcterms:isPartOf',
'namespaces' => array(
'', 'dkdcplus:albumId', 'dkdcplus:albumTitle',
'dkdcplus:ISBN', 'dkdcplus:ISSN',
),
'multiple' => TRUE,
'links' => TRUE,
),
array(
'entry' => 'dcterms:isReplacedBy',
'namespaces' => array(''),
'multiple' => TRUE,
'links' => TRUE,
),
array(
'entry' => 'dcterms:replaces',
'namespaces' => array(''),
'multiple' => TRUE,
'links' => TRUE,
),
array(
'entry' => 'dcterms:references',
'namespaces' => array('dkdcplus:ISBN'),
'multiple' => TRUE,
'links' => TRUE,
),
),
'subtitles' => array(
'entry' => 'dc:language',
'namespaces' => array(
'dkdcplus:subtitles',
/* Included for backwards compatability with DKABM 1.x' */
'oss:subtitles',
),
'multiple' => TRUE,
),
'publisher' => array(
'entry' => 'dc:publisher',
),
'isbn' => array(
'entry' => 'dc:identifier',
'namespaces' => array('dkdcplus:ISBN'),
),
'issn' => array(
'entry' => 'dc:identifier',
'namespaces' => array('dkdcplus:ISSN'),
),
'uri' => array(
'entry' => 'dc:identifier',
'namespaces' => array('dkdcplus:URI'),
),
'ismn' => array(
'entry' => 'dc:identifier',
'namespaces' => array('dkdcplus:ISMN'),
),
);
// Add contributors.
// They only vary by name and namespace so they get special handling.
$contrib_properties = array(
'actors' => array('dkdcplus:act'),
'coauthor' => array('dkdcplus:dkmed'),
'coeditors' => array('dkdcplus:dkmdt'),
'conductors' => array('oss:cnd'),
'draftsmen' => array('dkdcplus:drm'),
'editors' => array('dkdcplus:edt'),
'illustrators' => array('dkdcplus:ill'),
'interviewee' => array('dkdcplus:ive'),
'musicians' => array('dkdcplus:mus'),
'orchestras' => array('oss:mus'),
'photographers' => array('dkdcplus:pht'),
'readers' => array('dkdcplus:dkind'),
'scriptwriters' => array('dkdcplus:aus'),
'translators' => array('dkdcplus:trl'),
'writers' => array('dkdcplus:dktek'),
// All contributors. Any contributor with a recognized function will
// be removed later on.
'contributors' => NULL,
);
foreach ($contrib_properties as $name => $namespaces) {
$properties[$name] = array(
'entry' => 'dc:contributor',
'namespaces' => $namespaces,
'multiple' => TRUE,
'links' => TRUE,
);
}
// Extract all the properties into variables.
foreach ($properties as $name => $definitions) {
// Generate names for the $vars array
// Names for the foo property will be ting_creators and ting_creators_links.
$var_name = 'ting_' . $name;
$var_name_links = $var_name . '_' . ((!empty($definitions['multiple'])) ? 'links' : 'link');
// Convert single definitions to an array of definitions.
if (!isset($definitions[0])) {
$definitions = array($definitions);
}
// Loop through all definitions.
foreach ($definitions as $definition) {
if (!empty($object->record[$definition['entry']])) {
// Values are grouped by namespace. Loop through all of them.
foreach ($object->record[$definition['entry']] as $namespace => $values) {
// Only look for values in the specified namespaces. If no
// namespaces have been defined the go through all of them.
if ((!isset($definition['namespaces']) || in_array($namespace, $definition['namespaces'])) &&
(!isset($definition['!namespaces']) || !in_array($namespace, $definition['!namespaces']))) {
// If the property is multivalue...
if (!empty($definition['multiple'])) {
// Create arrays to hold values and links if they have not
// been defined.
if (!isset($vars[$var_name])) {
$vars[$var_name] = array();
}
if (!empty($definition['links']) && !isset($vars[$var_name])) {
$vars[$var_name_links] = array();
}
// Add the values.
foreach ($values as $value) {
$vars[$var_name][] = (!empty($definition['html'])) ? $value : check_plain($value);
if (!empty($definition['links'])) {
$facet = (!empty($definition['link_facet'])) ? $definition['link_facet'] : NULL;
// Add a link.
$vars[$var_name_links][] = _ting_property_value_link($value, $name, $namespace, $facet);
}
}
}
else {
// We have a single value so get the first entry.
$value = array_shift($values);
$vars[$var_name] = (!empty($definition['html'])) ? $value : check_plain($value);
if (!empty($definition['links'])) {
$facet = (!empty($definition['link_facet'])) ? $definition['link_facet'] : NULL;
// Add a link.
$vars[$var_name_links] = _ting_property_value_link($value, $name, $namespace, $facet);
}
// We have our single value so no need to look further.
break 2;
}
}
}
}
}
// Remove any duplicate values.
foreach (array($var_name, $var_name_links) as $var) {
if (isset($vars[$var]) && is_array($vars[$var])) {
$vars[$var] = array_unique($vars[$var]);
}
}
}
// Property post processing.
// Remove full title if same as normal title.
if ($vars['ting_title'] == $vars['ting_title_full']) {
unset($vars['ting_title_full']);
}
// Filter contributors listed in other functions.
$contrib_var_names = array('ting_contributors', 'ting_contributors_links');
// Loop through all contributor type properties...
foreach ($contrib_properties as $name => $namespace) {
// ... and all permutations of the individual property.
$var_names = array('ting_' . $name, 'ting_' . $name . '_links');
foreach ($var_names as $var_name) {
// If the property is set and not a contributor variable name in it self.
if (isset($vars[$var_name]) && !in_array($var_name, $contrib_var_names)) {
// ... loop through all valus.
foreach ($vars[$var_name] as $value) {
// If the value is also contained within the general
// contributor values.
foreach ($contrib_var_names as $contrib_var_name) {
$key = array_search($value, $vars[$contrib_var_name]);
if ($key !== FALSE) {
// ... then remove it.
unset($vars[$contrib_var_name][$key]);
}
}
}
}
}
}
// Process series information
// This could be handled more elegantly if we had better structured data.
// For now we have to work with what we got to convert titles to links
// Series information appear in the following formats:
// - Samhørende: [title 1] ; [title 2] ; [title 3]
// - [volumne number]. del af: [title]
// - [title] : [volumne number]
if (isset($vars['ting_series'])) {
$vars['ting_series_links'] = array();
foreach ($vars['ting_series'] as $series) {
// Any text appearing before series titles.
$prefix = '';
// Series titles.
// This will already have been passed through check_plain so we decode
// it to avoid double encoding.
$titles = htmlspecialchars_decode($series, ENT_QUOTES);
// Check for prefixes in the series information.
$prefixes = array('Samhørende:', 'del af:');
foreach ($prefixes as $p) {
// If prefix is found then split titles and prefix.
if (($pos = stripos($titles, $p)) !== FALSE) {
$split = drupal_strlen($p) + $pos;
$prefix = drupal_substr($titles, 0, $split);
$titles = drupal_substr($titles, $split + 1);
break;
}
}
// Multiple titles are separated by ' ; '. Explode to iterate over them.
$titles = explode(' ; ', $titles);
foreach ($titles as &$title) {
$title = trim($title);
// Some title elements are actually volumne numbers. Do not
// convert these to links.
if (!preg_match('/(nr.)? \d+/i', $title)) {
$title = _ting_property_value_link($title, 'dc:title', 'dkdcplus:series');
}
}
// Reassemple titles.
$titles = implode(' ; ', $titles);
// Create a link string.
$vars['ting_series_links'][] = (!empty($prefix) ? check_plain($prefix) . ' ' : '') . $titles;
}
}
return $vars;
}
/**
* Core function for extracting information from a Ting collection.
*
* @param TingClientObjectCollection $collection
* Collection preprocessor.
*/
function ting_collection_preprocess(TingClientObjectCollection $collection) {
// The first object in the collection will be the one to determine
// most of the variable values.
$vars = ting_object_preprocess($collection->objects[0]);
// Override url if collection contains multiple objects.
if (sizeof($collection->objects) > 1) {
$vars['ting_url'] = url('ting/collection/' . $collection->objects[0]->id, array('absolute' => TRUE));
}
// Define aggregate values.
$aggregate_properties = array('subjects', 'type');
// Extract values for all objects.
$object_vars = array();
foreach ($collection->objects as $object) {
$object_vars[] = ting_object_preprocess($object);
}
// Loop through all aggregate properties.
foreach ($aggregate_properties as $property) {
// Generate names for the $vars array
// Names for the foo property will be ting_creators and ting_creators_links
$var_name = 'ting_' . $property;
$var_name_link = $var_name . '_link';
$var_name_links = $var_name . '_links';
// Loop through all possible variable names.
foreach (array($var_name, $var_name_link, $var_name_links) as $var_name) {
$aggregate = array();
// Loop through all object variables.
foreach ($object_vars as $object_var) {
// If the object variables contain the variable name then we
// have something to add to our aggregates.
if (isset($object_var[$var_name])) {
// Values may be single values or arrays. Convert the singles
// to arrays to make them easier to work with.
if (!is_array($object_var[$var_name])) {
$object_var[$var_name] = array($object_var[$var_name]);
}
foreach ($object_var[$var_name] as $var_value) {
// New variable. Add value as key and set count to 0.
if (empty($aggregate[$var_value])) {
$aggregate[$var_value] = 0;
}
// Add count to value.
$aggregate[$var_value]++;
}
}
}
if (!empty($aggregate)) {
// Sort by count and return keys.
arsort($aggregate);
$vars[$var_name] = array_keys($aggregate);
}
}
}
return $vars;
}
/**
* Utility function to generate a link for a Ting object property.
*
* @param string $value
* The property valye
* @param string $name
* The name of the property
* @param string $namespace
* The namespace of the property
* @param string $facet
* A facet to use when creating the link
*/
function _ting_property_value_link($value, $name, $namespace = NULL, $facet = NULL) {
// TODO: This creates a hard dependency to ting_search.
// Could this be decoupled using a hook or the like?
$path = 'ting/search/' . (($facet) ? $facet . '=' : '') . '"' . $value . '"';
$options = array(
'attributes' => array(
'class' => _ting_property_classes($name, $namespace, 'ting'),
),
);
return l($value, $path, $options);
}
/**
* Generate a string of CSS classes for a Ting object property.
*
* @param string $name
* The name of the property
* @param string $namespace
* The namespace of the property
* @param string $prefix
* A prefix to preprend to all classes
*/
function _ting_property_classes($name, $namespace = NULL, $prefix = NULL) {
$classes = array();
// Properties may have link(s) appended - remove this.
if ($pos = strrpos($name, '_link', -6)) {
$name = substr($name, 0, $pos);
}
// First generate a class for the property with optional prefix
// in the format prefix-property
// All characters are set in lowercase and everything execept letters
// and numbers are replaced with -.
$classes[] = (!empty($prefix) ? $prefix . '-' : '') . preg_replace('/[^a-z1-9-]/', '-', strtolower($name));
if (!empty($namespace)) {
// Next add a class for the property with prefix prepended and
// namespace appended.
// Use self to reuse character replacement.
$classes[] = _ting_property_classes($classes[0] . '-' . $namespace);
}
return implode(' ', $classes);
}