-
Notifications
You must be signed in to change notification settings - Fork 12
/
ting.module
1061 lines (954 loc) · 29.6 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
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* Enables integration with Ting.
*/
// Define the different types of data that we cache in ting_set_cache and the
// default TTL.
define('TING_DEFAULT_CACHE_LIFETIME', 900);
define('TING_CACHE_TING_OBJECT', 'ting-object');
define('TING_CACHE_TING_OBJECT_FULLTEXT', 'ting-object-fulltext');
define('TING_CACHE_COLLECTION', 'ting-collection');
define('TING_CACHE_REPLY', 'ting-reply');
// Load Field module hooks.
module_load_include('inc', 'ting', 'ting.field');
/**
* Implements hook_ctools_plugin_api().
*/
function ting_ctools_plugin_api($module, $api) {
if ($module == 'page_manager' && $api == 'pages_default') {
return array('version' => 2);
}
}
/**
* Implements hook_ctools_plugin_directory().
*
* Lets the system know where our task and task_handler plugins are.
*/
function ting_ctools_plugin_directory($owner, $plugin_type) {
return 'plugins/' . $plugin_type;
}
/**
* Implements hook_ctools_plugin_type().
*
* Informs the plugin system that Page Manager owns task, task_handler, and
* page_wizard plugin types.
*
* All of these are empty because the defaults all work.
*/
function ting_ctools_plugin_type() {
return array(
'tasks' => array(),
'content_types' => array(),
'task_handlers' => array(),
'page_wizards' => array(),
'arguments' => array(),
'contexts' => array(),
);
}
/**
* Implements hook_menu().
*/
function ting_menu() {
$items['ting/object/%ting_object'] = array(
'title callback' => 'ting_page_title',
'title arguments' => array(2),
'page callback' => 'ting_object_page_view',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK | MENU_VISIBLE_IN_BREADCRUMB,
);
$items['ting/collection/%ting_collection'] = array(
'title callback' => 'ting_page_title',
'title arguments' => array(2),
'page callback' => 'ting_collection_page_view',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK | MENU_VISIBLE_IN_BREADCRUMB,
);
$items['admin/config/ting'] = array(
'title' => 'Ting',
'description' => 'Manage Ting integration settings.',
'position' => 'left',
'weight' => 20,
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/ting/settings'] = array(
'title' => 'Ting',
'description' => 'Manage Ting integration settings.',
'weight' => -20,
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_admin_ting_settings'),
'access arguments' => array('administer ting settings'),
'file' => 'ting.admin.inc',
);
$items['admin/config/ting/ranking'] = array(
'title' => 'Search result ranking',
'description' => 'Provides settings for how search results are ranked.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_admin_ranking_settings'),
'access arguments' => array('administer ting settings'),
'file' => 'ting.admin.inc',
);
$items['admin/config/ting/boost'] = array(
'title' => 'Search result boost',
'description' => 'Enable the user to boost specific values for specific fields in search results.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_admin_boost_settings'),
'access arguments' => array('administer ting settings'),
'file' => 'ting.admin.inc',
);
$items['admin/config/ting/online_types'] = array(
'title' => 'Online types and URL labels',
'description' => 'Define online resource types and their corresponding URL labels.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_admin_online_types_settings'),
'access arguments' => array('administer ting settings'),
'file' => 'ting.admin.inc',
);
$items['admin/config/ting/reservable'] = array(
'title' => 'Reservable Ting objects',
'description' => 'Configure on which Ting objects there will be a reservation button.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ting_admin_reservable_settings'),
'access arguments' => array('administer ting settings'),
'file' => 'ting.admin.inc',
);
return $items;
}
/**
* Implements hook_menu_alter().
*
* Adjusts the menu so that the field subtab becomes the default local task,
* to avoid having an useless placeholder page.
*/
function ting_menu_alter(&$items) {
if (module_exists('field_ui')) {
if (isset($items['admin/structure/ting_object/fields'])) {
// Make the fields task the default local task.
$items['admin/structure/ting_object'] = $items['admin/structure/ting_object/fields'];
$item = &$items['admin/structure/ting_object'];
$item['type'] = MENU_NORMAL_ITEM;
$item['title'] = 'Ting objects';
$item['description'] = 'Manage Ting object display.';
$items['admin/structure/ting_object/fields'] = array(
'title' => 'Manage fields',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
}
if (isset($items['admin/structure/ting_collection/fields'])) {
// Make the fields task the default local task.
$items['admin/structure/ting_collection'] = $items['admin/structure/ting_collection/fields'];
$item = &$items['admin/structure/ting_collection'];
$item['type'] = MENU_NORMAL_ITEM;
$item['title'] = 'Ting collection';
$item['description'] = 'Manage Ting collection display.';
$items['admin/structure/ting_collection/fields'] = array(
'title' => 'Manage fields',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
}
}
}
/**
* Implements hook_ding_install_tasks().
*/
function ting_ding_install_tasks() {
module_load_include('inc', 'ting', 'ting.admin');
return array(
'ting_admin_ting_settings' => array(
'display_name' => st('Ting service settings'),
'type' => 'form',
'file' => drupal_get_path('module', 'ting') . '/ting.admin.inc',
),
);
}
/**
* Implements hook_permission().
*/
function ting_permission() {
return array(
'administer ting settings' => array(
'title' => t('Administer ting settings'),
),
);
}
/**
* Implements hook_block_info().
*/
function ting_block_info() {
return array(
'ting_collection_types' => array(
'info' => t('Ting collection material types'),
'cache' => DRUPAL_CACHE_PER_PAGE,
),
'ting_object_types' => array(
'info' => t('Ting alternative material types'),
'cache' => DRUPAL_CACHE_PER_PAGE,
),
'ting_relation_anchors' => array(
'info' => t('Ting relations for material '),
'cache' => DRUPAL_CACHE_PER_PAGE,
),
);
}
/**
* Implements hook_cron().
*
* Ensures that all expired entries are delete form the cache on cron runes.
*/
function ting_cron() {
cache_clear_all(NULL, 'cache_ting');
}
/**
* Implements hook_flush_caches().
*/
function ting_flush_caches() {
return array('cache_ting');
}
/**
* Get relations for an ting_entity as an array ([$type] => array($relations)).
*/
function ting_get_relations($ting_entity) {
$relations = array();
foreach ($ting_entity->relations as $relation) {
if ($relation->type == 'dbcaddi:hasOpenUrl' || $relation->type == 'dbcaddi:hasOnlineAccess') {
continue;
}
$relations[$relation->type][] = $relation;
}
// Get references from ting_reference.
if (module_exists('ting_reference')) {
$refs = module_invoke('ting_reference', 'get_relations', 'ting_object', $ting_entity);
if (!empty($refs)) {
foreach ($refs as $ref) {
$relations[$ref->relation_type][] = $ref;
}
}
}
return $relations;
}
/**
* Implements hook_block_view().
*/
function ting_block_view($delta = '') {
$block = new stdClass();
switch ($delta) {
case 'ting_relation_anchors':
$items = array();
if ($object = menu_get_object('ting_object', 2)) {
$relations = ting_get_relations($object);
if (!empty($relations)) {
$relation_types = module_invoke_all('ding_anchor_info');
$block->subject = t('About the material');
foreach ($relations as $key => $relation) {
if (isset($relation_types[$key])) {
$items[] = l($relation_types[$key] . ' (' . count($relation) . ') ', '#' . $key, array('external' => TRUE));
}
}
$block->content = array(
'#theme' => 'item_list',
'#items' => $items,
);
}
}
break;
case 'ting_collection_types':
if ($collection = menu_get_object('ting_collection', 2)) {
$block->subject = t('Other materialtypes');
$items = array();
foreach ($collection->types as $type) {
$items[] = l($type . ' (' . $collection->types_count[$type] . ')', '#' . $type, array('external' => TRUE));
}
$block->content = array(
'#theme' => 'item_list',
'#items' => $items,
);
}
break;
case 'ting_object_types':
$object = menu_get_object('ting_object', 2);
if ($object && $collection = ting_collection_load($object->id)) {
$items = array();
foreach ($collection->types as $type) {
$uri = entity_uri('ting_collection', $collection);
$uri['options']['fragment'] = _ting_anchor_name($type);
$uri['options']['attributes'] = array('class' => array('js-search-overlay'));
$items[] = l($type . ' (' . $collection->types_count[$type] . ')', $uri['path'], $uri['options']);
}
// Only display block if there are more than on item.
if ($items > 1) {
$block->subject = t('Other materialtypes');
$block->content = array(
'#theme' => 'item_list',
'#items' => $items,
);
// Add search overlay trigger.
drupal_add_js(drupal_get_path('module', 'ting') . '/js/ting.js');
}
}
break;
}
return $block;
}
/**
* Implements hook_element_info().
*
* Define a new form element named ting_ranking_field.
*
* It calls the theme function theme_ting_ranking_field, if not defined
* no elements will be displayed.
*/
function ting_element_info() {
return array(
'ting_boost_field' => array(
'#input' => TRUE,
'#process' => array('ting_boost_field_element_process'),
),
'ting_ranking_field' => array(
'#input' => TRUE,
'#process' => array('ting_ranking_field_element_process'),
),
);
}
/**
* Implements hook_theme().
*/
function ting_theme() {
return array(
'ting_object' => array(
'template' => 'ting_object',
'render element' => 'elements',
'file' => 'ting.theme.inc',
),
'ting_ranking_field' => array(
'arguments' => array('element' => NULL),
),
);
}
/**
* Implements hook_ding_devel_timers().
*/
function ting_ding_devel_timers() {
return array(
'ting' => array(
'title' => 'Ting total request time was @time ms.',
),
'ting_net' => array(
'title' => 'Ting net time was @time ms.',
'include in total' => FALSE,
),
);
}
/**
* Implements hook_entity_info().
*/
function ting_entity_info() {
$return = array(
'ting_object' => array(
'label' => t('Ting object'),
'controller class' => 'TingObjectController',
'base table' => 'ting_object',
'revision table' => 'ting_object_revision',
'uri callback' => 'ting_object_uri',
'ding_entity_type' => 'ding_entity',
'ding_entity_menu' => 'ting/object/%ting_object',
'ding_entity_index' => 2,
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'tid',
'revision' => 'vid',
),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => TRUE,
),
),
'bundles' => array(
'ting_object' => array(
'label' => 'Ting object',
'admin' => array(
'path' => 'admin/structure/ting_object',
'access arguments' => array('administer content types'),
),
),
),
),
'ting_collection' => array(
'label' => t('Ting collection'),
'controller class' => 'TingCollectionController',
// Some modules assume that fieldable entities has a base
// table. Specify the object table as each collection is also an
// object.
'base table' => 'ting_object',
'uri callback' => 'ting_collection_uri',
'ding_entity_type' => 'ding_entity_collection',
'ding_entity_menu' => 'ting/collection/%ting_collection',
'ding_entity_index' => 2,
'fieldable' => TRUE,
'entity keys' => array(
// We don't really have an ID, but use the ding_entity_id,
// so we can use entity_uri.
'id' => 'ding_entity_id',
),
'view modes' => array(
'full' => array(
'label' => t('Full content'),
'custom settings' => FALSE,
),
'teaser' => array(
'label' => t('Teaser'),
'custom settings' => TRUE,
),
),
'bundles' => array(
'ting_collection' => array(
'label' => 'Ting collection',
'admin' => array(
'path' => 'admin/structure/ting_collection',
'access arguments' => array('administer content types'),
),
),
),
),
);
return $return;
}
/**
* Implements hook_registry_files_alter().
*/
function ting_registry_files_alter(&$files, $modules) {
// Add in ting client classes, so the autoloader knows where to find
// them. This is more handy than hardcoding them into the info file.
$def = array(
'module' => 'ting',
'weight' => 0,
);
// Add all of the Ting client library to the registry.
foreach (file_scan_directory(drupal_get_path('module', 'ting') . '/lib/ting-client/lib', '/\.php$/') as $filename => $dummy) {
$files[$filename] = $def;
}
// And the Ting client unit tests as well.
foreach (file_scan_directory(drupal_get_path('module', 'ting') . '/lib/ting-client/tests', '/\.test$/') as $filename => $dummy) {
$files[$filename] = $def;
}
}
/**
* Implements hook_ding_entity_is().
*/
function ting_ding_entity_is($object, $class) {
if ($class == 'reservable' || $class == 'cartable') {
$sources = variable_get('ting_reservable_sources', _ting_default_reservable_sources());
$types = variable_get('ting_reservable_types', _ting_default_reservable_types());
if (in_array(drupal_strtolower($object->ac_source), $sources) && in_array(drupal_strtolower($object->type), $types)) {
return TRUE;
}
else {
return FALSE;
}
}
elseif ($class == 'periodical') {
// @todo make this configurable.
// @todo also make sure that availability information isn't displayed
// on the object
return in_array(drupal_strtolower($object->type), array('tidsskrift', 'periodikum', 'årbog'));
}
}
/**
* Implements hook_ding_entity_buttons().
*/
function ting_ding_entity_buttons($type, $entity) {
if ($entity instanceof TingEntity && $entity->online_url) {
$settings = variable_get('ting_url_labels', _ting_default_url_labels());
$type = drupal_strtolower($entity->type);
$label = isset($settings[$type]) && $settings[$type] ? $settings[$type] : $settings['_default'];
return array(
// The link can't use l() as it will encode the url from ting_proxy. Use
// get online as the dc:identifier in $entity->online is not always the
// correct url.
array(
'#type' => 'markup',
'#markup' => '<a class="button-see-online" target="_new" href="' . $entity->getOnline_url() . '">' . $label . '</a>',
),
);
}
}
/**
* Implements hook_page_alter().
*
* Log, and display if the devel module is active and the user has
* permissions, a warning if the data well was queried more than once.
*/
function ting_page_alter(&$page) {
$calls = &drupal_static('ting_execute_cache');
if (count($calls) > 5) {
$calls_str = array();
foreach ($calls as $call) {
$calls_str[] = print_r($calls, TRUE);
}
watchdog('ting', 'Warning, ting_execute called @x times:<br />"@queries"', array('@x' => count($calls), '@queries' => implode('" "', $calls_str)), WATCHDOG_WARNING);
if (function_exists('dpm') && user_access('access devel information')) {
drupal_set_message(t('Warning, ting_execute called @x times.', array('@x' => count($calls))), 'error');
dpm($calls, 'ting_execute queries:');
}
}
}
/**
* Page callback: Display a ting object.
*/
function ting_object_page_view($object) {
return ting_object_view($object);
}
/**
* Page callback: Display a ting collection.
*/
function ting_collection_page_view($object) {
if (count($object->entities) < 2) {
drupal_goto('ting/object/' . $object->id);
}
return ting_collection_view($object);
}
/**
* Page title callback.
*
* Strips chars '<' and '>' in order to avoid HTML injections.
*/
function ting_page_title($object) {
return str_replace('&', '&', htmlspecialchars($object->title, ENT_NOQUOTES, 'UTF-8'));
}
/**
* Callback for entity_uri(). Returns the uri for the object.
*/
function ting_object_uri($collection) {
return array('path' => 'ting/object/' . $collection->id);
}
/**
* Callback for entity_uri(). Returns the uri for the collection.
*/
function ting_collection_uri($collection) {
return array('path' => 'ting/collection/' . $collection->id);
}
/**
* Add a provider_id to the objects.
*
* @to do
* Maybe this should be in a mapper module.
*/
function ting_ting_object_load($objects) {
foreach ($objects as $object) {
$object->provider_id = $object->localId;
}
}
/**
* Load a ting object.
*
* Don't use this, use ding_entity_load(). Default menu callback load of ting
* object.
*
* Handles redirect from data well version 2 to version 3.
*/
function ting_object_load($id) {
$agency = variable_get('ting_agency', FALSE);
if ($agency && preg_match('/^(\d{6}):(\w+)$/', $id, $matches)) {
// Matched to data well version 2, to redirect to version 3.
$new_id = ding_provider_build_entity_id($matches[2], $matches[1]);
if ($new_id === FALSE) {
$id = ting_lookup_and_translate($id);
}
else {
$id = $new_id;
}
drupal_goto('ting/object/' . $id, array(), 301);
}
return ding_entity_load($id, 'ting_object');
}
/**
* Try to lookup well3 pid via a search on the old pid.
* Search query is constructed from old pid eg. 870971:72966643 -> '870971 and 72966643'
*
* This is NOT foolproof - if more than one result is found the first is returned, and that might not
* be the correct one
*
* @param $old_pid ( eg 870971:72966643)
* @return bool|mixed; new pid (eg 870971-tsart:72966643 ) if found; else FALSE
*/
function ting_lookup_and_translate($old_pid) {
$query_elements = explode(':', $old_pid);
$query = implode(' and ', $query_elements);
module_load_include('client.inc', 'ting');
$result = ting_do_search($query, 1, 1, array('facets' => array()));
if (!empty($result->collections)) {
$id = key($result->collections);
return $id;
}
return FALSE;
}
/**
* Load multiple ting objects.
*
* Don't use this, use ding_entity_load_multiple().
*/
function ting_object_load_multiple($ids) {
return ding_entity_load_multiple($ids, 'ting_object');
}
/**
* Load a ting collection.
*
* Don't use this, use ding_collection_load(). Default menu callback load of
* ting collections.
*
* Handles redirect from data well version 2 to version 3.
*/
function ting_collection_load($id) {
$agency = variable_get('ting_agency', FALSE);
if ($agency && preg_match('/^(\d{6}):(\w+)$/', $id, $matches)) {
// Matched to data well version 2, to redirect to version 3.
$new_id = ding_provider_build_entity_id($matches[2], $matches[1]);
if ($new_id === FALSE) {
$id = ting_lookup_and_translate($id);
}
else {
$id = $new_id;
}
drupal_goto('ting/collection/' . $id, array(), 301);
}
return ding_entity_load($id, 'ting_collection');
}
/**
* Display a ting object.
*/
function ting_object_view($object, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
$object->content = array();
// _field_invoke_multiple gets pissy if the arrays aren't indexed by the
// local ID.
field_attach_prepare_view('ting_object', array($object->tid => $object), $view_mode, $langcode);
entity_prepare_view('ting_object', array($object->id => $object), $langcode);
$object->content += field_attach_view('ting_object', $object, $view_mode, $langcode);
// Allow modules to make their own additions to the object.
module_invoke_all('ding_entity_view', $object, $view_mode, $langcode);
module_invoke_all('entity_view', $object, 'ting_object', $view_mode, $langcode);
$build = $object->content;
unset($object->content);
$build += array(
'#theme' => 'ting_object',
'#object' => $object,
'#view_mode' => $view_mode,
);
drupal_alter('ting_view', $build);
return $build;
}
/**
* Display a ting collection.
*/
function ting_collection_view($object, $view_mode = 'full', $langcode = NULL) {
if (!isset($langcode)) {
$langcode = $GLOBALS['language_content']->language;
}
$object->content = array();
// _field_invoke_multiple gets pissy if the arrays aren't indexed by the
// local ID.
field_attach_prepare_view('ting_collection', array($object->ding_entity_id => $object), $view_mode, $langcode);
entity_prepare_view('ting_collection', array($object->id => $object), $langcode);
$object->content += field_attach_view('ting_collection', $object, $view_mode, $langcode);
// Allow modules to make their own additions to the collection.
module_invoke_all('ding_entity_collection_view', $object, $view_mode, $langcode);
module_invoke_all('entity_view', $object, 'ting_collection', $view_mode, $langcode);
$build = $object->content;
unset($object->content);
$build += array(
// TODO: Separate theming?
'#theme' => 'ting_object',
'#object' => $object,
'#view_mode' => $view_mode,
);
drupal_alter('ting_collection_view', $build);
return $build;
}
/**
* Sorts the objects according to type and language.
*
* But maintains the order of types and languages in the original array.
*/
function _ting_type_lang_sort($objects, &$return_types) {
$types = array();
$languages = array();
$sorted = array();
// Sort the objects into type buckets containing language buckets.
foreach ($objects as $object) {
// We're using keys so we don't have to uniq its afterwards.
$types[$object->type] = TRUE;
$languages[$object->language] = TRUE;
$buckets[$object->type][$object->language][] = $object;
}
$return_types = array_keys($types);
// Now flatten the buckets into a flat array.
foreach ($return_types as $type) {
foreach (array_keys($languages) as $language) {
if (isset($buckets[$type][$language])) {
$sorted = array_merge($sorted, $buckets[$type][$language]);
}
}
}
return $sorted;
}
/**
* Default url labels.
*
* Defined as a function as a define can only be scalars.
*/
function _ting_default_url_labels() {
return array(
'_default' => t('See online'),
'cd' => t('Hear online'),
'kassettelydbånd' => t('Hear online'),
'lydbog (cd)' => t('Hear online'),
'lydbog (cd-mp3)' => t('Hear online'),
'lydbog (online)' => t('Hear online'),
'grammofonplade' => t('Hear online'),
);
}
/**
* Processor for the ting_boost_field form element.
*/
function ting_boost_field_element_process($element, $form_state) {
$element['#tree'] = TRUE;
$element['#prefix'] = '<div class="ting-boost-field-element clearfix">';
$element['#suffix'] = '</div>';
$element['field_name'] = array(
'#title' => t('Field name'),
'#type' => 'select',
'#options' => array(
'' => '- ' . t('Choose') . ' -',
'term.acSource' => t('Source'),
'term.creator' => t('Author'),
'term.date' => t('Year of publish'),
'term.language' => t('Language'),
'term.type' => t('Material type'),
'term.identifier' => t('ISBN number'),
),
'#default_value' => (isset($element['#value']['field_name'])) ? $element['#value']['field_name'] : NULL,
'#attributes' => array('class' => array('field-name')),
);
$element['field_value'] = array(
'#title' => t('Value'),
'#type' => 'textfield',
'#default_value' => (isset($element['#value']['field_value'])) ? $element['#value']['field_value'] : NULL,
);
$element['weight'] = array(
'#title' => t('Weight'),
'#type' => 'textfield',
// '#type' => 'select',
// '#options' => drupal_map_assoc(range(1, 10)),
'#default_value' => (isset($element['#value']['weight'])) ? $element['#value']['weight'] : NULL,
'#element_validate' => array('boost_weight_validate'),
);
return $element;
}
/**
* Validate the boost form.
*/
function boost_weight_validate($element, &$form_state) {
if (!empty($element['#value']) && (is_int($element['#value']))) {
form_error($element, t('Boost weight is an integer.'));
}
}
/**
* Processor for the ting_ranking_field form element.
*/
function ting_ranking_field_element_process($element, $form_state) {
$element['#tree'] = TRUE;
$element['#prefix'] = '<div class="ting-ranking-field-element clearfix">';
$element['#suffix'] = '</div>';
$element['field_type'] = array(
'#title' => t('Type'),
'#type' => 'select',
'#options' => array(
'phrase' => t('Phrase'),
'word' => t('Word'),
),
'#default_value' => (isset($element['#value']['field_type'])) ? $element['#value']['field_type'] : NULL,
);
$element['field_name'] = array(
'#title' => t('Field name'),
'#type' => 'select',
'#options' => array(
'' => '- ' . t('Choose') . ' -',
'term.acSource' => t('Source'),
'term.title' => t('Title'),
'term.creator' => t('Author'),
'term.subject' => t('Subject'),
'term.date' => t('Year of publish'),
'term.type' => t('Material type'),
),
'#default_value' => (isset($element['#value']['field_name'])) ? $element['#value']['field_name'] : NULL,
'#attributes' => array('class' => array('field-name')),
);
$element['weight'] = array(
'#title' => t('Weight'),
'#type' => 'select',
'#options' => drupal_map_assoc(range(1, 10)),
'#default_value' => (isset($element['#value']['weight'])) ? $element['#value']['weight'] : NULL,
);
return $element;
}
/**
* Theme function to format the custom form element ting_ranking_field.
*/
function theme_ting_ranking_field($element) {
return theme('form_element', $element);
}
/**
* Default online types.
*
* Defined as a function as a define can only be scalars.
*/
function _ting_default_online_types() {
return array(
'musik (net)',
'ebog',
'netdokument',
'lydbog (net)',
'film (net)',
'tidsskrift (net)',
'periodikum (net)',
'pc-spil (net)',
'avis (net)',
'e-node',
);
}
/**
* Default reservable sources.
*
* Defined as a function as a define can only be scalars.
*/
function _ting_default_reservable_sources() {
return array(
'bibliotekskatalog',
);
}
/**
* Default reservable types.
*
* Defined as a function as a define can only be scalars.
*/
function _ting_default_reservable_types() {
return array(
'bog',
'node',
'dvd',
'billedbog',
'cd (musik)',
'lydbog (cd)',
'lydbog (bånd)',
'tegneserie',
'lydbog (cd-mp3)',
'sammensat materiale',
'cd',
'bog stor skrift',
'video',
'blu-ray',
'cd-rom',
'pc-spil',
'playstation 3',
'xbox 360',
'wii',
'playstation 2',
'playstation 4',
'graphic novel',
'nintendo ds',
'dvd-rom',
'kort',
'xbox',
'gameboy advance',
'wii u',
'grammofonplade',
'playstation',
'lydbog',
'spil',
'puslespil',
'diskette',
);
}
/**
* Fetch known types from the datawell.
*
* @return array
* Array with data well material types.
*/
function _ting_fetch_well_types() {
// Get a list of types by executing a null query and look at the facets
// result.
$options = array(
'facets' => array('facet.type'),
'numFacets' => 99,
'reply_only' => TRUE,
'sort' => 'random',
);
module_load_include('client.inc', 'ting');
$result = ting_do_search("*", 0, 0, $options);