-
Notifications
You must be signed in to change notification settings - Fork 46
/
islandora_basic_collection.module
1278 lines (1197 loc) · 46.5 KB
/
islandora_basic_collection.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
* Provides functionality for creating/managing/deleting/viewing collections.
*/
// Constants.
define('ISLANDORA_BASIC_COLLECTION_CREATE_CHILD_COLLECTION', 'create child collection');
define('ISLANDORA_BASIC_COLLECTION_MANAGE_COLLECTION_POLICY', 'manage collection policy');
define('ISLANDORA_BASIC_COLLECTION_MIGRATE_COLLECTION_MEMBERS', 'migrate collection members');
const ISLANDORA_BASIC_COLLECTION_LEGACY_BACKEND = 'islandora_basic_collection_legacy_sparql';
/**
* Implements hook_menu().
*/
function islandora_basic_collection_menu() {
$ingest_object_menu_item = array(
'title' => 'Add an object to this Collection',
'page callback' => 'islandora_basic_collection_ingest_action',
'page arguments' => array(2),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/ingest.form.inc',
'access callback' => 'islandora_basic_collection_ingest_access',
'access arguments' => array(2),
'weight' => -1,
);
return array(
'admin/islandora/solution_pack_config/basic_collection' => array(
'title' => 'Collection Solution Pack',
'description' => 'Configure the core Islandora collection functionality.',
'page callback' => 'drupal_get_form',
'access arguments' => array('administer site configuration'),
'page arguments' => array('islandora_basic_collection_admin'),
'file' => 'includes/admin.form.inc',
'type' => MENU_NORMAL_ITEM,
),
'islandora/object/%islandora_object/manage/collection' => array(
'title' => 'Collection',
'page callback' => 'islandora_basic_collection_manage_object',
'page arguments' => array(2),
'type' => MENU_LOCAL_TASK,
'file' => 'includes/manage_collection.inc',
'access callback' => 'islandora_basic_collection_manage_access',
'access arguments' => array(2),
),
'islandora/basic_collection/find_collections' => array(
'title' => 'Autocomplete Collection Search',
'page callback' => 'islandora_basic_collection_get_collections_filtered',
'type' => MENU_CALLBACK,
'file' => 'includes/utilities.inc',
'access arguments' => array(ISLANDORA_BASIC_COLLECTION_MIGRATE_COLLECTION_MEMBERS),
),
'islandora/object/%islandora_object/manage/object/share' => array(
'title' => 'Share this Object with another collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_basic_collection_share_item_form', 2),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/share.form.inc',
'access callback' => 'islandora_basic_collection_share_migrate_access',
'access arguments' => array(2),
),
'islandora/object/%islandora_object/manage/object/migrate' => array(
'title' => 'Migrate this Object to another collection',
'page callback' => 'drupal_get_form',
'page arguments' => array('islandora_basic_collection_migrate_item_form', 2),
'type' => MENU_LOCAL_ACTION,
'file' => 'includes/migrate.form.inc',
'access callback' => 'islandora_basic_collection_share_migrate_access',
'access arguments' => array(2),
),
'islandora/object/%islandora_object/manage/collection/ingest' => $ingest_object_menu_item,
'islandora/object/%islandora_object/manage/overview/ingest' => $ingest_object_menu_item,
'islandora/collection/count_ajax' => array(
'title' => 'AJAX callback to get count',
'page callback' => 'islandora_basic_collection_object_count_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
'file' => 'includes/blocks.inc',
),
);
}
/**
* Implements hook_menu_local_tasks_alter().
*
* Moves 'Add an Object' link to top using 'weight'.
*/
function islandora_basic_collection_menu_local_tasks_alter(&$data, $router_item, $root_path) {
if ($root_path == 'islandora/object/%/manage' && isset($data['actions']['output'])) {
foreach ($data['actions']['output'] as &$action) {
if (isset($action['#link']['weight'])) {
$action['#weight'] = $action['#link']['weight'];
}
}
}
}
/**
* Access callback for ingest.
*
* @param AbstractObject $object
* The object to test if we're allowed to ingest... Check that it actually
* is a collection and we have sufficient info to show the form.
*
* @return bool
* TRUE if $object represents a collection, we can show the ingest form and
* we have permission to ingest; otherwise, FALSE.
*/
function islandora_basic_collection_ingest_access(AbstractObject $object) {
$collection_models = islandora_basic_collection_get_collection_content_models();
$is_a_collection = (
(count(array_intersect($collection_models, $object->models)) > 0) &&
isset($object['COLLECTION_POLICY'])
);
if (!$is_a_collection) {
return FALSE;
}
module_load_include('inc', 'islandora', 'includes/ingest.form');
module_load_include('inc', 'islandora_basic_collection', 'includes/ingest.form');
$configuration = islandora_basic_collection_get_ingest_configuration($object);
$has_ingest_steps = islandora_ingest_can_display_ingest_form($configuration);
return $has_ingest_steps && islandora_object_access(ISLANDORA_INGEST, $object);
}
/**
* Access callback for share/migrate.
*
* @param AbstractObject $object
* The object to test if we're allowed to ingest... Check that it actually
* is a collection and we have sufficient info to show the form.
*
* @return bool
* TRUE if $object represents a collection, we can show the form and
* we have permission to manage object; otherwise, FALSE.
*/
function islandora_basic_collection_share_migrate_access(AbstractObject $object) {
$collection_models = islandora_basic_collection_get_collection_content_models();
$is_a_collection = (
(count(array_intersect($collection_models, $object->models)) > 0) &&
isset($object['COLLECTION_POLICY'])
);
if ($is_a_collection) {
if ($object->id == "islandora:root") {
return FALSE;
}
}
return islandora_object_access(ISLANDORA_MANAGE_PROPERTIES, $object);
}
/**
* Implements hook_menu_alter().
*/
function islandora_basic_collection_menu_alter(array &$items) {
// We want to add more permissions to the access arguments for the manage tab.
$current_access_arguments = $items['islandora/object/%islandora_object/manage']['access arguments'][0];
$new_access_arguments = array(
ISLANDORA_BASIC_COLLECTION_MANAGE_COLLECTION_POLICY,
ISLANDORA_BASIC_COLLECTION_MIGRATE_COLLECTION_MEMBERS,
);
$new_access_arguments = array_merge($current_access_arguments, $new_access_arguments);
$items['islandora/object/%islandora_object/manage']['access arguments'] = array($new_access_arguments, 2);
}
/**
* Implements hook_islandora_required_objects().
*/
function islandora_basic_collection_islandora_required_objects(IslandoraTuque $connection) {
$module_path = drupal_get_path('module', 'islandora_basic_collection');
// Collection Content Model.
$collection_content_model = $connection->repository->constructObject('islandora:collectionCModel');
$collection_content_model->owner = 'fedoraAdmin';
$collection_content_model->label = 'Islandora Collection Content Model';
$collection_content_model->models = 'fedora-system:ContentModel-3.0';
// DS-COMPOSITE-MODEL Datastream.
$datastream = $collection_content_model->constructDatastream('DS-COMPOSITE-MODEL', 'X');
$datastream->label = 'DS-COMPOSITE-MODEL';
$datastream->mimetype = 'application/xml';
$datastream->setContentFromFile("$module_path/xml/islandora_basic_collection_ds_composite_model.xml", FALSE);
$collection_content_model->ingestDatastream($datastream);
return array(
'islandora_basic_collection' => array(
'title' => 'Islandora basic collection',
'objects' => array($collection_content_model),
),
);
}
/**
* Determine whether or not to show this modules manage tab.
*
* @param AbstractObject $object
* The object being managed.
*
* @return bool
* TRUE if it should be shown, and FALSE if it should not be shown.
*/
function islandora_basic_collection_manage_access($object = NULL) {
$collection_models = islandora_basic_collection_get_collection_content_models();
$is_a_collection = count(array_intersect($collection_models, $object->models)) > 0;
return $is_a_collection && (
islandora_object_access(ISLANDORA_BASIC_COLLECTION_MANAGE_COLLECTION_POLICY, $object) ||
islandora_object_access(ISLANDORA_BASIC_COLLECTION_MIGRATE_COLLECTION_MEMBERS, $object) ||
islandora_object_access(ISLANDORA_INGEST, $object) ||
islandora_object_access(ISLANDORA_PURGE, $object)
);
}
/**
* Implements hook_theme().
*/
function islandora_basic_collection_theme($existing, $type, $theme, $path) {
return array(
'islandora_basic_collection' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/islandora-basic-collection',
'pattern' => 'islandora_basic_collection__',
'variables' => array('islandora_object' => NULL),
),
'islandora_basic_collection_grid' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/islandora-basic-collection-grid',
'pattern' => 'islandora_basic_collection_grid__',
'variables' => array('islandora_object' => NULL, 'collection_results' => NULL),
),
'islandora_basic_collection_wrapper' => array(
'file' => 'theme/theme.inc',
'template' => 'theme/islandora-basic-collection-wrapper',
'variables' => array('islandora_object' => NULL),
),
'islandora_basic_collection_policy_management_table' => array(
'file' => 'theme/theme.inc',
'render element' => 'table',
),
'islandora_basic_collection_metadata_table_drag_components' => array(
'render element' => 'element',
'file' => 'theme/theme.inc',
),
);
}
/**
* Implements hook_CMODEL_PID_islandora_view_object().
*/
function islandora_basic_collection_islandora_collectionCModel_islandora_view_object(AbstractObject $object) {
/* completely disable view of object */
$disable = variable_get('islandora_basic_collection_disable_display_generation', FALSE);
if ($disable) {
$to_return = array('Collection View' => '');
}
else {
$backend = variable_get('islandora_basic_collection_display_backend', ISLANDORA_BASIC_COLLECTION_LEGACY_BACKEND);
$backends = module_invoke_all('islandora_basic_collection_query_backends');
if ($backend === ISLANDORA_BASIC_COLLECTION_LEGACY_BACKEND || !isset($backends[$backend])) {
$output = theme('islandora_basic_collection_wrapper', array('islandora_object' => $object));
return array('Collection View' => $output);
}
else {
$limit = ((empty($_GET['pagesize'])) ?
variable_get('islandora_basic_collection_page_size', '12') :
$_GET['pagesize']);
$pager_element = 0;
$passed_page = pager_find_page($pager_element);
if (isset($backends[$backend]['file'])) {
require_once $backends[$backend]['file'];
}
list($total, $pids) = call_user_func($backends[$backend]['callable'], $object, $passed_page, $limit);
$page = pager_default_initialize($total, $limit, $pager_element);
if ($page < $passed_page) {
// If we were somehow on a page past the end, let us go to the last page
// of results.
list($total, $pids) = call_user_func($backends[$backend]['callable'], $object, $page, $limit);
}
$to_return = array(
'islandora_basic_collection_display' => array(
'#type' => 'markup',
'#theme' => 'islandora_objects_subset',
'#objects' => $pids,
'#total' => $total,
'#limit' => $limit,
'#pager_element' => $pager_element,
'#display' => variable_get('islandora_basic_collection_default_view', 'grid'),
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array(
'islandora-solr-collection-display',
),
),
),
);
}
}
// Add collection description metadata.
if (variable_get('islandora_collection_metadata_display', FALSE)) {
module_load_include('inc', 'islandora', 'includes/metadata');
$to_return['description'] = array(
'#type' => 'markup',
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array(
'islandora-collection-metadata',
'islandora-collection-metadata-description',
),
),
'#markup' => islandora_retrieve_description_markup($object),
);
// Add parent collections.
$parent_collections = islandora_get_parents_from_rels_ext($object);
if (!empty($parent_collections)) {
$to_return['collections'] = array(
'#type' => 'markup',
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array(
'islandora-collection-metadata-in-collections',
'islandora-collection-metadata',
),
),
);
$to_return['collections']['heading'] = array(
'#markup' => t('<h2>In collections</h2>'),
);
$to_return['collections']['list'] = array(
'#prefix' => '<ul>',
'#suffix' => '</ul>',
);
foreach ($parent_collections as $parent) {
$to_return['collections']['list'][] = array(
'#markup' => l($parent->label, "islandora/object/{$parent->id}"),
'#prefix' => '<li>',
'#suffix' => '</li>',
);
}
}
// Preserve the 'wrapper' wrapper for legacy support.
$to_return['wrapper'] = array(
'#type' => 'item',
'#theme_wrappers' => array('container'),
'#attributes' => array(
'class' => array(
'islandora-collection-metadata',
'islandora-collection-metadata-markup',
),
),
);
// Add the metadata fieldset markup.
$to_return['wrapper']['metadata'] = array(
'#type' => 'markup',
'#attached' => array(
'js' => array(
'misc/form.js',
'misc/collapse.js',
),
),
'#markup' => islandora_retrieve_metadata_markup($object),
);
}
foreach (variable_get('islandora_basic_collection_metadata_info_table_drag_attributes', array()) as $key => $config) {
if ($config['omit']) {
unset($to_return[$key]);
}
if (!isset($to_return[$key])) {
continue;
}
$to_return[$key]['#weight'] = $config['weight'];
}
return $to_return;
}
/**
* Implements hook_islandora_basic_collection_query_backends().
*/
function islandora_basic_collection_islandora_basic_collection_query_backends() {
$module_path = drupal_get_path('module', 'islandora_basic_collection');
return array(
ISLANDORA_BASIC_COLLECTION_LEGACY_BACKEND => array(
// XXX: "callable" not required, since this one is the original/legacy
// case which gets handled as it used to (inline in our implementation of
// hook_islandora_collectionCModel_islandora_view_object()).
'title' => t('SPARQL (Legacy)'),
),
'islandora_basic_collection_sparql_query_backend' => array(
'title' => t('SPARQL'),
'callable' => 'islandora_basic_collection_display_query_sparql',
'file' => "$module_path/includes/utilities.inc",
),
);
}
/**
* Implements hook_xml_form_builder_forms().
*/
function islandora_basic_collection_xml_form_builder_forms() {
$module_path = drupal_get_path('module', 'islandora_basic_collection');
return array(
'Collection MODS form' => array(
'form_file' => "$module_path/xml/islandora_basic_collection_form_mods.xml",
),
);
}
/**
* Implements hook_xml_form_builder_form_associations().
*/
function islandora_basic_collection_xml_form_builder_form_associations() {
return array(
'islandora_basic_collection_mods_form' => array(
'content_model' => 'islandora:collectionCModel',
'form_name' => 'Collection MODS form',
'dsid' => 'MODS',
'title_field' => array(
'titleInfo', 'title',
),
'transform' => 'mods_to_dc.xsl',
'self_transform' => 'islandora_cleanup_mods_extended.xsl',
'template' => FALSE,
),
);
}
/**
* Implements hook_CMODEL_PID_islandora_view_object().
*/
function islandora_basic_collection_islandora_collectionCModel_islandora_overview_object(AbstractObject $object) {
$rows = array();
$model_query = islandora_basic_collection_get_query_info(array(
'object' => $object,
'page_size' => -1,
'page_number' => 0,
'vars' => '?model',
'order_by' => FALSE,
));
$models = (array) $object->repository->ri->query($model_query['query'], $model_query['type']);
$ignore_models = array(
'fedora-system:FedoraObject-3.0',
);
$disable_count = variable_get('islandora_basic_collection_disable_count_object', FALSE);
foreach ($models as $model) {
$model_pid = $model['model']['value'];
if (in_array($model_pid, $ignore_models)) {
continue;
}
$model_count_query = islandora_basic_collection_get_query_info(array(
'object' => $object,
'page_size' => -1,
'page_number' => 0,
'order_by' => FALSE,
'model' => "<info:fedora/$model_pid>",
));
$model_object = islandora_object_load($model_pid);
$rows[$model_pid] = array(
$model_object ? l($model_object->label, "islandora/object/{$model_object->id}") : $model_pid,
);
if (!$disable_count) {
$rows[$model_pid][] = $object->repository->ri->countQuery($model_count_query['query'], $model_count_query['type']);
}
}
$content = array(
'table' => array(
'#theme' => 'table',
'#header' => array(
'type' => array('data' => t('Type')),
),
'#rows' => $rows,
'#empty' => t('Collection is empty.')),
);
if (!$disable_count) {
list($total_count, $all_results) = islandora_basic_collection_get_member_objects($object, 0, 0);
$content['total'] = array('#markup' => t('Total members: %total', array('%total' => $total_count)));
$content['table']['#header']['count'] = array('data' => t('Count'));
}
return array(
'collection' => drupal_render($content),
);
}
/**
* Implements hook_islandora_view_object().
*/
function islandora_basic_collection_islandora_overview_object(AbstractObject $object) {
module_load_include('inc', 'islandora_basic_collection', 'includes/utilities');
if (!in_array('islandora:collectionCModel', $object->models)) {
$map_to_row = function($o) {
$o = islandora_object_load($o);
return ($o ?
array(l($o->label, "islandora/object/{$o->id}")) :
FALSE);
};
$pids = islandora_basic_collection_get_parent_pids($object);
$rows = array_map($map_to_row, $pids);
$rows = array_filter($rows);
$table = theme('table', array(
'header' => array(t('Parent Collections')),
'rows' => $rows,
'empty' => t('No parent collections')));
return array('collection' => $table);
}
}
/**
* Get the query to get child objects.
*
* @param array $params
* An array containing all the parameters, at least:
* - object: An AbstractObject we're querying for.
* but may also contain:
* - page_size: The size of the page/number of results to return. Defaults to
* 10.
* - page_number: An integer representing the offset as a multiple of
* page_size. Defaults to 0.
* - vars: A string containing the list of variables to select. Defaults to
* "?object ?title".
* - order_by: A string indicating which variable by which to sort. Defaults
* to "?title". May be set to FALSE to avoid sorting.
* - model: A string representing a URI. Defaults to "?model". Could be
* provided as "<info:fedora/cmodel:pid>" if the type of object to query
* should be filtered.
* @param string $type
* Either 'view' or 'manage', its meant to repersent how the objects will be
* used.
*
* @return array
* An array containing a number of keys used to describe the query,
* including all the values from $params (which haven't been unset in the
* alter), in addition to:
* - query: The actual query string.
* - type: A string containing the type of query, likely one of:
* - itql; or,
* - sparql.
* - pid: The pid of the collection being queried.
*/
function islandora_basic_collection_get_query_info(array $params, $type = 'view') {
if (!isset($params['object'])) {
throw new Exception(t('!function requires "!object_parameter" to be given in the array of parameters.', array(
'!function' => __FUNCTION__,
'!object_parameter' => 'object',
)));
}
// Add in defaults.
$params += array(
'page_number' => 0,
'page_size' => 10,
'vars' => '?object ?title ?owner ?date_modified',
'order_by' => '?title',
'model' => '?model',
'context' => $type,
);
$object = $params['object'];
$query = <<<EOQ
SELECT DISTINCT !vars
FROM <#ri>
WHERE {
!statements .
!optionals
!filters
}
EOQ;
$filters = array();
$query_filters = module_invoke_all('islandora_basic_collection_get_query_filters', $type);
$query_optionals = module_invoke_all('islandora_basic_collection_get_query_optionals', $type);
$query_statements = module_invoke_all('islandora_basic_collection_get_query_statements', $type);
drupal_alter('islandora_basic_collection_query_param', $query_filters, $query_statements, $params, $query_optionals);
foreach ($query_filters as $filter) {
$filters[] = "FILTER($filter)";
}
$query = format_string($query, array(
'!statements' => implode(' . ', $query_statements),
'!optionals' => (!empty($query_optionals) ? ('OPTIONAL {{' . implode('} UNION {', $query_optionals) . '}}') : ''),
'!filters' => implode(' ', $filters),
'!vars' => $params['vars'],
));
$query = format_string($query, array(
'!pid' => $object->id,
'!model' => $params['model'],
));
if ($params['order_by']) {
$query .= <<<EOQO
ORDER BY {$params['order_by']}
EOQO;
}
$query_array = $params + array(
'query' => $query,
'type' => 'sparql',
'pid' => $object->id,
);
drupal_alter('islandora_basic_collection_query', $query_array);
return $query_array;
}
/**
* Implements hook_islandora_basic_collection_get_query_statements().
*/
function islandora_basic_collection_islandora_basic_collection_get_query_statements($type = 'view') {
$to_return = array();
$to_return[] = <<<EOQ
?object ?collection_predicate <info:fedora/!pid> ;
<fedora-model:label> ?title ;
<fedora-model:ownerId> ?owner ;
<fedora-view:lastModifiedDate> ?date_modified ;
<fedora-model:hasModel> !model
EOQ;
if ($type == 'view') {
$to_return[] = '?object <fedora-model:state> <fedora-model:Active>';
}
return $to_return;
}
/**
* Implements hook_islandora_basic_collection_get_query_filters().
*/
function islandora_basic_collection_islandora_basic_collection_get_query_filters($type) {
return 'sameTerm(?collection_predicate, <fedora-rels-ext:isMemberOfCollection>) || sameTerm(?collection_predicate, <fedora-rels-ext:isMemberOf>)';
}
/**
* Get objects associated with this object.
*
* Currently, we are only concerned with the with isMemberOf and
* isMemberOfCollection relationships.
*
* @param AbstractObject $object
* The collection object whose members will be fetched.
* @param int $page_number
* The page number in the query for members.
* @param int $page_size
* The number of results per page page from the query for members.
* @param string $type
* Either 'view' or 'manage', its meant to repersent how the objects will be
* used.
* @param string $cmodel
* The content model in which to explicitly select.
*
* @return array|bool
* An array containing two values:
* - An integer representing the total number of tuples which can be
* selected with the given parameters.
* - The tuples in the slice according to $page_number and $page_size.
* or boolean FALSE if the query fails.
*/
function islandora_basic_collection_get_member_objects(AbstractObject $object, $page_number = 0, $page_size = 20, $type = 'view', $cmodel = NULL) {
$params = array(
'object' => $object,
'page_number' => $page_number,
'page_size' => $page_size,
);
if (isset($cmodel)) {
$params['model'] = "<info:fedora/$cmodel>";
}
$query_array = islandora_basic_collection_get_query_info($params, $type);
try {
$count = $object->repository->ri->countQuery($query_array['query'], $query_array['type']);
$is_itql = strcasecmp('itql', $query_array['type']) === 0;
if ($is_itql && ($page_number > 0 || $page_size >= 0)) {
// Strip the final semi-colon(s) of any itql query, where they exist.
$query = trim($query_array['query']);
while (strpos($query, -1) == ';') {
$query = substr($query, 0, -1);
}
$query_array['query'] = $query;
}
if ($page_number > 0 && $page_size > 0) {
// Add in the offset somehow.
$offset = $page_number * $page_size;
$query_array['query'] .= " offset $offset";
}
if ($page_size >= 0) {
// Add in the limit somehow.
$query_array['query'] .= " limit $page_size";
}
if ($is_itql) {
// Add in the final semi-colon.
$query_array['query'] .= ';';
}
$results = $object->repository->ri->query($query_array['query'], $query_array['type']);
}
catch (Exception $e) {
$variables = array(
'@message' => $e->getMessage(),
'@stack' => $e->getTraceAsString(),
);
watchdog('islandora_basic_collection',
'Islandora basic collection failed to retrieve associated objects.<br/>Error:<br/>@message<br/>Stack: <br/>@stack',
$variables,
WATCHDOG_ERROR);
return FALSE;
}
return array($count, $results);
}
/**
* Get all existing collections.
*
* @return array
* An associative array containing all the known collections:
* - pid: The PID of the collection.
* - pid: The PID of the collection.
* - label: The label of the collection.
*/
function islandora_basic_collection_get_collections() {
module_load_include('inc', 'islandora', 'includes/utilities');
$tuque = islandora_get_tuque_connection();
$query = <<<EOQ
SELECT ?object ?label
FROM <#ri>
WHERE {
?object <fedora-model:hasModel> <info:fedora/islandora:collectionCModel> ;
<fedora-model:label> ?label ;
<fedora-model:state> <fedora-model:Active> .
}
ORDER BY ?label
EOQ;
$results = $tuque->repository->ri->sparqlQuery($query, 'unlimited');
$collections = array();
foreach ($results as $result) {
$pid = $result['object']['value'];
if (islandora_namespace_accessible($pid)) {
$collections[$pid] = array(
'pid' => $pid,
'label' => $result['label']['value'] . " (" . $pid . ")",
);
}
}
return $collections;
}
/**
* Implements hook_islandora_undeletable_datastreams().
*/
function islandora_basic_collection_islandora_undeletable_datastreams($models) {
if (in_array('islandora:collectionCModel', $models)) {
if (variable_get('islandora_basic_collection_disable_collection_policy_delete', TRUE)) {
return array('COLLECTION_POLICY');
}
}
}
/**
* Implements hook_permission().
*/
function islandora_basic_collection_permission() {
return array(
ISLANDORA_BASIC_COLLECTION_CREATE_CHILD_COLLECTION => array(
'title' => t('Create child collections'),
'description' => t('Create new collections within an existing collection.'),
),
ISLANDORA_BASIC_COLLECTION_MANAGE_COLLECTION_POLICY => array(
'title' => t('Manage collection policies'),
'description' => t('Define which content models are available for each collection.'),
),
ISLANDORA_BASIC_COLLECTION_MIGRATE_COLLECTION_MEMBERS => array(
'title' => t('Migrate collection members'),
'description' => t('Move objects from one collection to another.'),
),
);
}
/**
* Implements hook_form_islandora_object_properties_form_alter().
*/
function islandora_basic_collection_form_islandora_object_properties_form_alter(array &$form, array &$form_state) {
$object = $form_state['object'];
if (in_array('islandora:collectionCModel', $object->models)) {
$form['delete']['#value'] = t('Delete Collection');
}
}
/**
* Implements hook_form_islandora_object_properties_form_alter().
*/
function islandora_basic_collection_form_islandora_delete_object_form_alter(array &$form, array &$form_state) {
$object = $form_state['object'];
if (in_array('islandora:collectionCModel', $object->models)) {
$form['description']['#markup'] = t('This will remove the collection object and all its child objects. If child objects are collection objects, their children will not be deleted, and will become orphaned. Child objects currently shared with other collections will not be deleted. This action cannot be undone.');
$form['#submit'] = array('islandora_basic_collection_islandora_delete_object_form_delete_children_submit');
}
}
/**
* Delete all the child objects related to the collection object being deleted.
*
* @param array $form
* The Drupal form.
* @param array $form_state
* The Drupal form state.
*/
function islandora_basic_collection_islandora_delete_object_form_delete_children_submit(array $form, array &$form_state) {
module_load_include('inc', 'islandora_basic_collection', 'includes/batch');
// @todo This should be recursive.
batch_set(islandora_basic_collection_delete_children_batch($form_state['object']));
// Called from within this submit handler rather than from the Drupal Form API
// as we need the object to exist to generate the pages and if we run this.
// batch operation from a submit handler any submit handlers to be called
// afterwards will not get called, which is a bug/feature of the Form API.
islandora_delete_object_form_submit($form, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters islandora_repository_admin form.
* Adds in a setting for site-wide UUID generation on ingest.
*/
function islandora_basic_collection_form_islandora_repository_admin_alter(&$form, &$form_state) {
$form['islandora_tabs']['islandora_general']['islandora_basic_collection_generate_uuid'] = array(
'#type' => 'checkbox',
'#title' => t('UUID PID Generation'),
'#default_value' => variable_get('islandora_basic_collection_generate_uuid', FALSE),
'#description' => t('Generate Fedora object PIDs with v4 UUIDs.'),
);
}
/**
* Implements hook_islandora_ingest_steps().
*/
function islandora_basic_collection_islandora_ingest_steps(array &$form_state) {
// Assumes that only a single object will get created.
$shared_storage = islandora_ingest_form_get_shared_storage($form_state);
// Not ingesting into a collection.
if (empty($shared_storage['collection'])) {
return;
}
// Configure the step storage.
$step_storage = &islandora_ingest_form_get_step_storage($form_state, 'islandora_basic_collection_select_content_model');
$step_storage['models'] = isset($step_storage['models']) ? $step_storage['models'] : $shared_storage['models'];
if (count($step_storage['models']) > 0) {
return array(
'islandora_basic_collection_select_content_model' => array(
'type' => 'form',
'form_id' => 'islandora_basic_collection_select_content_model_form',
'args' => array($step_storage['models']),
'weight' => -50,
'module' => 'islandora_basic_collection',
'file' => 'includes/ingest.form.inc',
),
);
}
}
/**
* Implements hook_islandora_ingest_steps_alter().
*/
function islandora_basic_collection_islandora_ingest_steps_alter(array &$steps, array $form_state) {
$step_storage = &islandora_ingest_form_get_step_storage($form_state, 'islandora_basic_collection_select_content_model');
if (!empty($step_storage)) {
// Convert the select content model form step to a callback when only one
// content model is available and when there are more than one form steps,
// as we can't have a form with no steps.
$select_model_form_step = !empty($steps['islandora_basic_collection_select_content_model']);
$no_model_selection = count($step_storage['models']) == 1;
$form_steps = islandora_ingest_form_get_form_steps($form_state);
if ($select_model_form_step && $no_model_selection && count($form_steps) > 1) {
$steps['islandora_basic_collection_select_content_model'] = array(
'type' => 'callback',
'do_function' => array(
'function' => 'islandora_basic_collection_set_content_model_callback',
'args' => array(
reset($step_storage['models']),
),
),
'undo_function' => array('function' => 'islandora_basic_collection_set_content_model_undo_callback'),
'weight' => -50,
'module' => 'islandora_basic_collection',
'file' => 'includes/ingest.form.inc',
);
}
}
}
/**
* Get the content models which can should be treated as collections.
*
* @return array
* An array of strings, each of which represents the PID of a content model.
*/
function islandora_basic_collection_get_collection_content_models() {
return array(
'islandora:collectionCModel',
);
}
/**
* Implements hook_islandora_object_access().
*
* Maps our three permissions onto those in the Islandora core.
*/
function islandora_basic_collection_islandora_object_access($op, $object, $user) {
$result = NULL;
$collection_models = islandora_basic_collection_get_collection_content_models();
$is_a_collection = count(array_intersect($collection_models, $object->models)) > 0;
if (in_array($op, array_keys(islandora_basic_collection_permission()))) {
if ($is_a_collection) {
if ($op == ISLANDORA_BASIC_COLLECTION_CREATE_CHILD_COLLECTION && isset($object['COLLECTION_POLICY'])) {
$result = islandora_object_access(ISLANDORA_INGEST, $object, $user) && islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $object['COLLECTION_POLICY'], $user);
if ($result) {
$policy = new CollectionPolicy($object['COLLECTION_POLICY']->content);
$policy_content_models = $policy->getContentModels();
$result = count(array_intersect($collection_models, array_keys($policy_content_models))) > 0;
}
}
elseif ($op == ISLANDORA_BASIC_COLLECTION_MANAGE_COLLECTION_POLICY) {
if (isset($object['COLLECTION_POLICY'])) {
$result = islandora_datastream_access(ISLANDORA_METADATA_EDIT, $object['COLLECTION_POLICY'], $user);
}
else {
$result = islandora_object_access(ISLANDORA_ADD_DS, $object, $user);
}
}
elseif ($op == ISLANDORA_BASIC_COLLECTION_MIGRATE_COLLECTION_MEMBERS) {
// Not sure how much sense this makes... Check that we can modify the
// RELS-EXT of the current object, assuming that we'll be able to modify
// the children as well...
$result = islandora_datastream_access(ISLANDORA_METADATA_EDIT, $object['RELS-EXT'], $user);
}
}
else {
$result = FALSE;
}
}
return $result;
}
/**
* Implements hook_islandora_ingest_steps().
*/
function islandora_basic_collection_islandora_collectioncmodel_islandora_ingest_steps() {
return array(
'islandora_basic_collection' => array(
'weight' => -11,
'type' => 'form',
'form_id' => 'islandora_basic_collection_create_child_collection_form',
'module' => 'islandora_basic_collection',
'file' => 'includes/manage_collection.inc',
),
);
}
/**
* Implements hook_CMODEL_PID_islandora_object_ingested().
*/
function islandora_basic_collection_islandora_collectioncmodel_islandora_object_ingested(AbstractObject $fedora_object) {
if (!isset($fedora_object['TN'])) {
// Add TN datastream.
$thumbnail_datastream = $fedora_object->constructDatastream('TN');
$thumbnail_datastream->setContentFromFile(drupal_get_path('module', 'islandora_basic_collection') . '/images/folder.png', FALSE);
$thumbnail_datastream->label = 'Thumbnail';
$thumbnail_datastream->mimetype = 'image/png';
$fedora_object->ingestDatastream($thumbnail_datastream);
}
}
/**
* Implements hook_block_info().
*/
function islandora_basic_collection_block_info() {
$blocks['collection_object_count'] = array(
'info' => t('Islandora Collection Object Count Listing'),
'cache' => DRUPAL_CACHE_PER_USER,
);
$blocks['collection_listing'] = array(
'info' => t('Islandora Collection Listing'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_configure().
*/
function islandora_basic_collection_block_configure($delta = '') {