forked from Islandora/islandora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fedora_repository.module
1716 lines (1475 loc) · 63.6 KB
/
fedora_repository.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
// $Id$
/*
* Created on Aug 10, 2007
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
/**
* Drupal hook for admin form
* fedora_repository_name is the name of the top level collection this module will query
* fedora_repository_pid is the name of the top level pid.
* Stores this info in the drupal variables table.
* the name and pid can also be passed as url parameters
*/
function fedora_repository_admin() {
module_load_include('inc', 'fedora_repository', 'formClass');
$adminForm = new formClass();
return $adminForm->createAdminForm();
}
function fedora_repository_admin_settings_submit($form, $form_values) {
drupal_set_message("Custom form handler.");
}
/**
* drupal hook
* calls the fedora_repositorys_admin form
*/
function fedora_repository_menu() {
module_load_include('inc', 'fedora_repository', 'formClass');
$adminMenu = new formClass();
return $adminMenu->createMenu();
}
/**
* drupal hook to show help
*/
function fedora_repository_help($path, $arg) {
switch ($path) {
case 'admin/modules#description' :
return t('Grabs a list of items from a collection in Drupal that are presented on the home page.');
case 'node/add#fedora_repository' :
return t('Use this page to grab a list of items from a Fedora collection.');
}
}
function fedora_repository_purge_object($pid = NULL, $name = NULL) {
if (!user_access('purge objects and datastreams')) {
drupal_set_message(t('You do not have access to add a datastream to this object.'), 'error');
return '';
}
if ($pid == NULL) {
drupal_set_message(t('You must specify an object pid to purge an object.'), 'error');
return '';
}
$output = t('Are you sure you wish to purge object %name %pid!<br /><b>This cannot be undone</b><br />',
array(
'%name' => $name,
'%pid' => $pid)
);
$output .= drupal_get_form('fedora_repository_purge_object_form', $pid);
return $output;
}
function fedora_repository_collection_view($pid = NULL, $collection = NULL, $pageNumber = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
global $user;
if (!fedora_repository_access(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or access to Fedora denied."), 'error');
return ' ';
}
$objectHelper = new ObjectHelper();
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:top');
}
$content = '';
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
$results = $collectionClass->getRelatedItems($pid, NULL);
$content .= $objectHelper->parseContent($results, $pid, $dsId, $collection, $pageNumber);
return $content;
}
function fedora_repository_ingest_object($collection_pid=NULL, $collection_label = NULL, $content_model = NULL) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
if (!user_access('ingest new fedora objects')) {
drupal_set_message(t('You do not have permission to ingest.'), 'error');
return '';
}
if (!validPid($collection_pid)) {
if (validPid(urldecode($collection_pid))) {
$collection_pid = urldecode($collection_pid);
} else {
drupal_set_message(t("This collection PID $collection_pid is not valid"), 'error');
return ' ';
}
}
if ($collection_pid == NULL) {
drupal_set_message(t('You must specify a collection object pid to ingest an object.'), 'error');
return '';
}
$output = drupal_get_form('fedora_repository_ingest_form', $collection_pid, $collection_label, $content_model);
return $output;
}
function fedora_repository_ingest_form_submit($form, &$form_state) {
//only validate the form if the submit button was pressed (other buttons may be used for AHAH
if ($form_state['clicked_button']['#id'] == 'edit-submit') {
global $base_url;
module_load_include('inc', 'fedora_repository', 'CollectionClass');
module_load_include('inc', 'fedora_repository', 'CollectionPolicy');
module_load_include('inc', 'fedora_repository', 'ContentModel');
$contentModelPid = ContentModel::getPidFromIdentifier($form_state['values']['models']);
$contentModelDsid = ContentModel::getDSIDFromIdentifier($form_state['values']['models']);
$err = TRUE;
$redirect = TRUE;
if (($cp = CollectionPolicy::loadFromCollection($form_state['values']['collection_pid'])) !== FALSE) {
$relationship = $cp->getRelationship();
if (($cm = ContentModel::loadFromModel($contentModelPid, $contentModelDsid)) !== FALSE) {
$pid = $cp->getNextPid($contentModelDsid);
global $user;
$form_state['values']['user_id'] = $user->name;
$form_state['values']['pid'] = $pid;
$form_state['values']['content_model_pid'] = $contentModelPid;
$form_state['values']['relationship'] = $relationship;
$err = (!$cm->execFormHandler($form_state['values']));
$_SESSION['fedora_ingest_files'] = ''; //empty this variable
$attr = $cm->getIngestFormAttributes();
$redirect = $attr['redirect'];
if ($redirect) {
$form_state['storage'] = NULL;
}
}
}
if ($redirect) {
$form_state['redirect'] = ($err) ? ' ' : $base_url . "/fedora/repository/{$form_state['values']['collection_pid']}";
}
}
}
function fedora_repository_ingest_form_validate($form, &$form_state) {
//only validate the form if the submit button was pressed (other buttons may be used for AHAH
if ($form_state['clicked_button']['#id'] == 'edit-submit') {
switch ($form_state['storage']['step']) {
case 1:
$form_state['storage']['step']++;
$form_state['rebuild'] = TRUE;
break;
case 2:
// Get the uploaded file.
$validators = array();
if (!empty($_FILES['files']['name']['ingest-file-location'])) {
$fileObject = file_save_upload('ingest-file-location', $validators);
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
$form_state['values']['ingest-file-location'] = $fileObject->filepath;
}
if (file_exists($form_state['values']['ingest-file-location'])) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
module_load_include('inc', 'fedora_repository', 'MimeClass');
$file = $form_state['values']['ingest-file-location'];
$contentModelPid = ContentModel::getPidFromIdentifier($form_state['values']['models']);
$contentModelDsid = ContentModel::getDSIDFromIdentifier($form_state['values']['models']);
if (($cm = ContentModel::loadFromModel($contentModelPid, $contentModelDsid)) !== FALSE) {
$allowedMimeTypes = $cm->getMimetypes();
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file);
if (!empty($file)) {
if (!in_array($dformat, $allowedMimeTypes)) {
form_set_error('ingest-file-location', t('The uploaded file\'s mimetype (' . $dformat . ') is not associated with this Content Model. The allowed types are ' .
implode(' ', $allowedMimeTypes)));
file_delete($file);
return;
} elseif (!$cm->execIngestRules($file, $dformat)) {
drupal_set_message(t('Error following Content Model Rules'), 'error');
foreach (ContentModel::$errors as $err) {
drupal_set_message($err, 'error');
}
}
}
}
}
$form_state['rebuild'] = FALSE;
break;
}
}
}
function fedora_repository_ingest_form(&$form_state, $collection_pid, $collection_label = NULL, $content_model = NULL) {
module_load_include('inc', 'fedora_repository', 'formClass');
//$client = getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
// For the sake of easily maintaining the module in different core versions create our own form_values variable.
if (empty($form_state['storage']['step'])) {
$form_state['storage']['step'] = 1;
}
$ingestForm = new formClass();
$form_state['storage']['content_model'] = $content_model;
$form_state['storage']['collection_pid'] = $collection_pid;
return $ingestForm->createIngestForm($collection_pid, $collection_label, $form_state);
}
function fedora_repository_purge_object_form(&$form_state, $pid, $collectionPid = NULL) {
$form['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Purge')
);
if ($collectionPid == NULL) {
$collectionPid = $_SESSION['fedora_collection'];
}
// $form['#redirect'] = "fedora/repository/$collectionPid/";
return $form;
}
function add_stream($collection_pid=NULL, $collectionName=NULL) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
if (!validPid($collection_pid)) {
drupal_set_message(t("This PID is not valid!"), 'error');
return ' ';
}
if (!user_access('ingest new fedora objects')) {
drupal_set_message(t('You do not have permission to ingest.'), 'error');
return '';
}
if ($collection_pid == NULL) {
drupal_set_message(t('You must specify an collection object pid to ingest an object.'), 'error');
return '';
}
$output .= drupal_get_form('fedora_repository_add_stream_form', $pid);
return $output;
}
function add_stream_form_submit($form, &$form_state) {
global $base_url;
if (!empty($form_state['submit']) && $form_state['submit'] == 'OK') {
$form_state['rebuild'] = TRUE;
return;
}
module_load_include('inc', 'fedora_repository', 'MimeClass');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pathToModule = drupal_get_path('module', 'fedora_repository');
$file = $form_state['values']['add-stream-file-location'];
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['stream_id'];
$dsLabel = $form_state['values']['stream_label'] . substr($file, strrpos($file, '.')); // Add the file extention to the end of the label.;
$streamUrl = $base_url . '/' . drupal_urlencode($file);
/* -----------------------------------------------------------------
* need a better way to get mimetypes
*/
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file);
$controlGroup = "M";
if ($dformat == 'text/xml') {
$controlGroup = 'X';
}
try {
$item = new Fedora_Item($pid);
$item->add_datastream_from_url($streamUrl, $dsid, $dsLabel, $dformat, $controlGroup);
$object_helper = new ObjectHelper();
$object_helper->get_and_do_datastream_rules($pid, $dsid, $file);
file_delete($file);
} catch (exception $e) {
drupal_set_message(t($e->getMessage()), 'error');
return;
}
$form_state['rebuild'] = TRUE;
//$form_state['redirect'] = $base_url."/fedora/repository/$pid";
// drupal_goto("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
}
function add_stream_form(&$form_state, $pid) {
//module_load_module_load_include('hp', ''Fedora_Repository'', 'config', 'fedora_repository', '');
module_load_include('inc', 'fedora_repository', 'formClass');
//$client = getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
$addDataStreamForm = new formClass();
return $addDataStreamForm->createAddDataStreamForm($pid, $form_state);
}
function add_stream_form_validate($form, &$form_state) {
if ($form_state['clicked_button']['#value'] == 'OK') {
$form_state['rebuild'] = TRUE;
return;
}
$dsid = $form_state['values']['stream_id'];
$dsLabel = $form_state['values']['stream_label'];
if (strlen($dsid) > 64) {
form_set_error('', t('Data stream ID cannot be more than 64 characters.'));
return FALSE;
}
if (!(preg_match("/^[a-zA-Z]/", $dsid))) {
form_set_error('', t('Data stream ID has to start with a letter.'));
return FALSE;
}
if (strlen($dsLabel) > 64) {
form_set_error('', t('Data stream Label cannot be more than 64 characters.'));
return FALSE;
}
if (strpos($dsLabel, '/')) {
form_set_error('', t('Data stream Label cannot contain a "/".'));
return FALSE;
}
$validators = array(
// 'file_validate_is_image' => array(),
// 'file_validate_image_resolution' => array('85x85'),
// 'file_validate_size' => array(30 * 1024),
);
$fileObject = file_save_upload('add-stream-file-location', $validators);
// Move the uploaded file to Drupal's files directory.
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
$form_state['values']['add-stream-file-location'] = $fileObject->filepath;
// TODO: Add error checking here.
$form_state['rebuild'] = FALSE;
}
function fedora_repository_purge_stream($pid = NULL, $dsId = NULL, $name = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
global $user;
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t('You must specify an object pid and DataStream ID to purge a datastream'), 'error');
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER :: $PURGE_FEDORA_OBJECTSANDSTREAMS, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or you do not have permission to purge objects."), 'error');
return ' ';
}
$output = t('Are you sure you wish to purge this datastream %name<br />',
array(
'%name' => $name)
);
$output .= drupal_get_form('fedora_repository_purge_stream_form', $pid, $dsId);
return $output;
}
function fedora_repository_purge_object_form_submit($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
//$client = getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
$pid = $form_state['values']['pid'];
$params = array(
"pid" => $pid,
"logMessage" => "Purged",
"force" => ""
);
try {
$soapHelper = new ConnectionHelper();
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
$object = $client->__soapCall('purgeObject', array(
$params
)
);
} catch (exception $e) {
if (preg_match('/org\.fcrepo\.server\.security\.xacml\.pep\.AuthzDeniedException/',$e->getMessage())) {
drupal_set_message(t('Error: Insufficient permissions to purge object.'), 'error');
} else {
drupal_set_message(t($e->getMessage()), 'error');
}
return;
}
if (empty($collectionPid)) {
$collectionPid = $_SESSION['fedora_collection'];
}
$form_state['redirect'] = "fedora/repository/$collectionPid/";
}
function fedora_repository_purge_stream_form(&$form_state, $pid, $dsId) {
$form['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
);
$form['dsid'] = array(
'#type' => 'hidden',
'#value' => "$dsId"
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Purge')
);
return $form;
}
function fedora_repository_purge_stream_form_submit($form, &$form_state) {
global $base_url;
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
//$client = getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
$pid = $form_state['values']['pid'];
$item = new Fedora_Item($pid);
$dsid = $form_state['values']['dsid'];
try {
$item->purge_datastream($dsid);
} catch (exception $e) {
drupal_set_message(t($e->getMessage()), 'error');
}
$form_state['redirect'] = $base_url . "/fedora/repository/$pid";
}
function fedora_repository_replace_stream($pid, $dsId, $dsLabel, $collectionName = NULL) {
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t('You must specify an pid and dsId to replace.'), 'error');
return '';
}
$output .= drupal_get_form('fedora_repository_replace_stream_form', $pid, $dsId, $dsLabel);
return $output;
}
function fedora_repository_replace_stream_form(&$form_state, $pid, $dsId, $dsLabel) {
//module_load_module_load_include('hp', ''Fedora_Repository'', 'config', 'fedora_repository', '');
module_load_include('inc', 'Fedora_Repository', 'formClass');
//$client = getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
$replaceDataStreamForm = new formClass();
return $replaceDataStreamForm->createReplaceDataStreamForm($pid, $dsId, $dsLabel, $form_state);
}
function fedora_repository_replace_stream_form_validate($form, &$form_state) {
// If a file was uploaded, process it.
if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name']['file'])) {
// attempt to save the uploaded file
$file = file_save_upload('file', array(), file_directory_path());
// set error is file was not uploaded
if (!$file) {
form_set_error('file', 'Error uploading file.');
return;
}
$doc = new DOMDocument();
module_load_include('inc', 'Fedora_Repository', 'MimeClass');
$mime = new MimeClass();
if ($mime->getType($file->filepath) == 'text/xml' && !$doc->load($file->filepath)) {
form_set_error('file', 'Invalid XML format.');
return;
}
// set files to form_state, to process when form is submitted
$form_state['values']['file'] = $file;
}
}
function fedora_repository_replace_stream_form_submit($form, &$form_state) {
$file = $form_state['values']['file'];
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['dsId'];
// Remove the original file extension from the label and add the new one
$dsLabel = substr($form_state['values']['dsLabel'], 0, strrpos($form_state['values']['dsLabel'], '.'));
$dsLabel .= substr($file->filename, strrpos($file->filename, '.')); // Add the file extention to the end of the label.;
module_load_include('inc', 'Fedora_Repository', 'MimeClass');
module_load_include('inc', 'Fedora_Repository', 'ObjectHelper');
module_load_include('inc', 'Fedora_Repository', 'ConnectionHelper');
$streamUrl = file_create_url($file->filepath);
/* -----------------------------------------------------------------
* need a better way to get mimetypes
*/
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file->filepath);
$controlGroup = "M";
$function = 'modifyDatastreamByReference';
$params = array(
'pid' => $pid,
'dsID' => $dsid,
'altIDs' => "",
'dsLabel' => $dsLabel,
'MIMEType' => $dformat,
'formatURI' => "URL",
'dsLocation' => $streamUrl,
'controlGroup' => "$controlGroup",
'dsState' => "A",
'checksumType' => "DISABLED",
'checksum' => "none",
'logMessage' => "datastream replaced",
'force' => FALSE,
);
if ($dformat == 'text/xml') {
$doc = new DOMDocument();
$doc->load($file->filepath);
$controlGroup = 'X';
$function = 'modifyDatastreamByValue';
$params['dsContent'] = $doc->saveXML();
unset($params['dsLocation']);
}
try {
$soapHelper = new ConnectionHelper();
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
if ($client == NULL) {
drupal_set_message(t('Error Getting Soap Client.'), 'error');
return;
}
$object = $client->__soapCall($function, array('parameters' => $params));
// Apply the add datastream rules.
$object_helper = new ObjectHelper();
$object_helper->get_and_do_datastream_rules($pid, $dsid, $file->filepath);
file_delete($file->filepath);
} catch (exception $e) {
drupal_set_message(t($e->getMessage()), 'error');
return;
}
drupal_goto('fedora/repository/' . $pid . '/-/' . $dsId);
}
function fedora_repository_edit_qdc_page($pid = NULL, $dsId = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
global $user;
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t('You must specify an object pid and a Dublin Core DataStream ID to edit metadata'), 'error');
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER :: $EDIT_FEDORA_METADATA, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or you do not have permission to edit meta data for this object."), 'error');
return ' ';
}
$output = drupal_get_form('fedora_repository_edit_qdc_form', $pid, $dsId);
return $output;
}
function fedora_repository_edit_qdc_form(&$form_state, $pid, $dsId = NULL) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if ($pid == NULL) {
drupal_set_message(t('You must specify an object pid!'), 'error');
}
global $user;
if (!fedora_repository_access(OBJECTHELPER :: $EDIT_FEDORA_METADATA, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or you do not have permission to edit meta data for this object."), 'error');
return ' ';
}
module_load_include('inc', 'fedora_repository', 'formClass');
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$soapHelper = new ConnectionHelper();
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_url', 'http://localhost:8080/fedora/services/access?wsdl'));
// Check if there is a custom edit metadata function defined in the content model.
$output = '';
if (($cm = ContentModel::loadFromObject($pid)) !== FALSE) {
$output = $cm->buildEditMetadataForm($pid, $dsId);
}
if (empty($output)) {
// There is no custom function, so just load the standard QDC form.
$metaDataForm = new formClass();
//currently we only edit the dc metadata. If you defined a custom form with a custom handler you are sol for now.
return $metaDataForm->createMetaDataForm($pid, $dsId, $client);
}
return $output;
}
function fedora_repository_edit_qdc_form_validate($form, &$form_state) {
}
/**
* Check if there is a custom edit metadata function defined in the content model. If so,
* call it, if not do the submit action for the standard QDC metadata. Custom forms will
* need to implement their own equivalent to the FormClass->updateMetaData function
*
* @param array $form
* @param array $form_state
* @return
*/
function fedora_repository_edit_qdc_form_submit($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
global $base_url;
if (strstr($form_state['clicked_button']['#id'], 'edit-submit')) {
//$client = getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
$soap_helper = new ConnectionHelper();
$client = $soap_helper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/services/management?wsdl'));
// Check the content model for a custom edit metadata form submit function.
if (isset($form_state['values']['pid'])) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
if (($cm = ContentModel::loadFromObject($form_state['values']['pid'])) !== FALSE) {
return $cm->handleEditMetadataForm($form_state['values']['form_id'], $form_state, $client);
}
}
module_load_include('inc', 'fedora_repository', 'formClass');
$metaDataForm = new formClass();
$return_value = $metaDataForm->updateMetaData($form_state['values']['form_id'], $form_state['values'], $client);
$form_state['redirect'] = $base_url . '/fedora/repository/' . $form_state['values']['pid'];
return $return_value;
}
}
/**
* drupal hook
* creates a new permission than can be assigned to roles
*/
function fedora_repository_perm() {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
return array(
OBJECTHELPER::$OBJECT_HELPER_VIEW_FEDORA,
OBJECTHELPER::$EDIT_FEDORA_METADATA,
OBJECTHELPER::$PURGE_FEDORA_OBJECTSANDSTREAMS,
OBJECTHELPER::$ADD_FEDORA_STREAMS,
OBJECTHELPER::$INGEST_FEDORA_OBJECTS,
OBJECTHELPER::$EDIT_TAGS_DATASTREAM,
OBJECTHELPER::$VIEW_DETAILED_CONTENT_LIST,
);
}
/**
* drupal hook
* determines if a user has access to what they are asking for
*
*
*/
function fedora_repository_access($op, $node, $account) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$objectHelper = new ObjectHelper();
return $objectHelper->fedora_repository_access($op, $node, $account);
}
/**
* Grabs a stream from fedora sets the mimetype and returns it. $dsID is the
* datastream id.
* @param $pid String
* @param $dsID String
*/
function makeObject($pid, $dsID) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
if (!validPid($pid)) {
drupal_set_message(t("Invalid PID!"), 'error');
return ' ';
}
if (!validDsid($dsID)) {
drupal_set_message(t("Invalid dsID!"), 'error');
return ' ';
}
if ($pid == NULL || $dsID == NULL) {
drupal_set_message(t("No pid or dsid given to create an object with."));
return ' ';
}
global $user;
if (!fedora_repository_access(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_access_denied();
return;
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace."), 'error');
return ' ';
}
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$objectHelper = new ObjectHelper();
$objectHelper->makeObject($pid, $dsID);
}
/**
* Sends an ITQL query to the Fedora Resource index (can only communicate with Kowari or mulgara)
* Reads the pid and datastream id as url parameters. Queries the collection object for the query
* if there is no query datastream falls back to the query shipped with the module.
* @return String
*/
function fedora_repository_get_items($pid = NULL, $dsId = NULL, $collection = NULL, $pageNumber = NULL, $limit = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
global $user;
if ($pid &!validPid($pid)) {
drupal_set_message(t("Invalid PID!"), 'error');
return ' ';
}
if ($dsId &!validDsid($dsId)) {
drupal_set_message(t("Invalid dsID!"), 'error');
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER::$OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or access to Fedora denied!"), 'error');
return ' ';
}
$objectHelper = new ObjectHelper();
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:top');
}
$headers = module_invoke_all('file_download', "/fedora/repository/$pid");
if (in_array(-1, $headers)) {
drupal_access_denied();
exit;
return ' ';
}
if ($dsId != NULL && $dsId != '-') { //if we have a dsID return the stream otherwise query for a collection of objects
//probably should check pid as well here.
return makeObject($pid, $dsId);
}
$content = '<div id="content-fedora">';
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
//if(!isset($pageNumber)){
// $pageNumber=0;
//}
//if(!isset($limit)){
// $limit=20;
//}
$breadcrumbs = array();
$objectHelper->getBreadcrumbs($pid, $breadcrumbs);
drupal_set_breadcrumb(array_reverse($breadcrumbs));
$offset = $limit * $pageNumber;
$results = $collectionClass->getRelatedObjects($pid, $limit, $offset, NULL); //updated so we can do paging in query not in xslt
//$results = $collectionClass->getRelatedItems($pid, NULL);
$content .= $objectHelper->parseContent($results, $pid, $dsId, $collection, $pageNumber);
//the below is for islandlives we should be able to do this in the xslt though
//$css=$path.'/stylesheets/container-large.css';
//drupal_add_css($css);
return $content . '</div>';
}
function fedora_repository_urlencode_string($str) {
return htmlentities($str);
}
/**
* Uses makeobject to get a stream. Sets the Content Disposition in the header so it suggests a filename
* and sends it as an attachment. This should prompt for a download of the object.
*
*/
function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
global $user;
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t("no pid or dsid given to create an object with!"));
return ' ';
}
$objectHelper = new ObjectHelper();
$objectHelper->makeObject($pid, $dsId, 1, $label, FALSE, $version);
}
function repository_page($pid = NULL, $dsId = NULL, $collection = NULL, $pageNumber = NULL) {
//do security check at fedora_repository_get_items function as it has to be called there in case
//someone trys to come in a back door.
return fedora_repository_get_items($pid, $dsId, $collection, $pageNumber);
}
function repository_service($pid = NULL, $servicePid = NULL, $serviceMethod = NULL) {
module_load_include('inc','fedora_repository', 'api/fedora_item');
module_load_include('inc','fedora_repository', 'ObjectHelper');
global $user;
if (!fedora_repository_access(OBJECTHELPER::$OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or access to Fedora denied!"), 'error');
return ' ';
}
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:top');
}
$headers = module_invoke_all('file_download', "/fedora/repository/$pid");
if (in_array(-1, $headers)) {
drupal_access_denied();
exit;
return ' ';
}
$item = new Fedora_Item($pid);
if ($item !== false)
{
echo $item->get_dissemination($servicePid,$serviceMethod);
}
exit();
}
//Search Stuff ********************************************************************
/**
* Implementation of hook_search().
* sends a search query to fedora fgsearch which is backed by Lucene
* In our implementation of Fedora we have api-a and api-m locked down
* to authorized users but at the object level. We can query Lucene and the
* RI index to get a list of results without authorization but to view any
* datastreams users must be authorized.
*
*/
function fedora_repository_search($op = 'search', $keys = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
switch ($op) {
case 'name':
if (user_access('view fedora collection')) {
return t('Digital Repository', array('-9'));
}
case 'search':
if (user_access('view fedora collection')) {
//demo search string ?operation=gfindObjects&indexName=DemoOnLucene&query=fgs.DS.first.text%3Achristmas&hitPageStart=11&hitPageSize=10
$resultData = NULL;
$numberOfHitsPerPage = NULL;
$index = strpos($keys, '.');
$test = substr($keys, 0, $index + 1);
$type = NULL;
if ($index > 0) {
$index = strpos($keys, ':');
$type = substr($keys, 0, $index);
$keys = substr($keys, $index + 1);
}
$index = strpos($keys, ':');
$startPage = substr($keys, 0, $index);
if ($index > 1) {
$keys = substr($keys, $index + 1);
}
if (!$startPage) {
$startPage = 1;
}
$xmlDoc = NULL;
$path = drupal_get_path('module', 'fedora_repository');
$xmlDoc = new DomDocument();
$xmlDoc->load($path . '/searchTerms.xml');
$nodeList = $xmlDoc->getElementsByTagName('default');
if (!$type) {
//$type = 'dc.description';
$type = $nodeList->item(0)->nodeValue;
}
$nodeList = $xmlDoc->getElementsByTagName('number_of_results');
$numberOfHitsPerPage = $nodeList->item(0)->nodeValue;
$indexName = variable_get('fedora_index_name', 'DemoOnLucene');
$keys = htmlentities(urlencode($keys));
$searchQuery = NULL;
if (isset($type) && strcmp($type, ':')) {
$searchQuery = $type . ':' . $keys;
} else {
$searchQuery = $keys;
}
//$searchQuery.=" AND (PID:vre OR PID:vre:ref OR PID:demo OR PID:changeme)";
$searchUrl = variable_get('fedora_fgsearch_url', 'http://localhost:8080/fedoragsearch/rest');
$searchString = '?operation=gfindObjects&indexName=' . $indexName . '&restXslt=copyXml&query=' . $searchQuery;
$searchString .= '&hitPageSize=' . $numberOfHitsPerPage . '&hitPageStart=' . $startPage;
//$searchString = htmlentities(urlencode($searchString));
$searchUrl .= $searchString;
$objectHelper = new ObjectHelper();
$resultData = do_curl($searchUrl);
$results[] = array(
array(
'data' => $resultData,
'colspan' => 2
)
);
return $results;
}
} // switch ($op)
}
/**
* Implementation of hook_search_page().
* Display the search results
*/
function fedora_repository_search_page($resultData) {
$path = drupal_get_path('module', 'fedora_repository');
$proc = NULL;
if (!$resultData[0][0]['data']) {
return ''; //no results
}
$text = utf8_encode($resultData[0][0]['data']);
try {
$proc = new XsltProcessor();
} catch (Exception $e) {
$out[] = array(
array(
'data' => $e->getMessage(),
'colspan' => 2
)
);
return $out;
}
//inject into xsl stylesheet
$proc->setParameter('', 'searchToken', drupal_get_token('search_form')); //token generated by Drupal, keeps tack of what tab etc we are on
$proc->setParameter('', 'searchUrl', url('search') . '/fedora_repository'); //needed in our xsl
$proc->setParameter('', 'objectsPage', base_path());
$proc->setParameter('', 'allowedPidNameSpaces', variable_get('fedora_pids_allowed', 'default: demo: changeme: Islandora: ilives: '));
$proc->registerPHPFunctions();
$xsl = new DomDocument();
$xsl->load($path . '/xsl/results.xsl');
$quimby = $xsl;
$input = new DomDocument();
$didLoadOk = $input->loadXML(utf8_encode($resultData[0][0]['data']));
if (!$didLoadOk) {
$results[] = array(
array(
'data' => 'Error parsing results',
'colspan' => 2
)
);
} else {
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input);
$results[] = array(
array(
'data' => $newdom->saveHTML(),
'colspan' => 2
)
);
}
$header = array(
array(
'data' => t('Collection results'),
NULL,
),
(NULL)
);
$output .= theme('table', $header, $results);
return $output;
}
/**
* Implementation of hook_form_alter().
* allows the advanced search form in drupal
*/
function fedora_repository_form_alter(&$form, &$form_state, $form_id) {
// Advanced node search form
module_load_include('inc', 'fedora_repository', 'SearchClass');
$path = drupal_get_path('module', 'fedora_repository');
if ($form_id == 'search_form' && arg(1) == 'fedora_repository' && user_access('use advanced search')) {
$default_value = $form['basic']['inline']['keys']['#default_value'];
$index = strpos($default_value, '.');
$test = substr($default_value, 0, $index + 1);
$type = NULL;
if ($index > 0) {
$index = strpos($default_value, ':');
$type = substr($default_value, 0, $index);
$default_value = substr($default_value, $index + 1);
}