forked from Islandora/islandora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionClass.inc
769 lines (701 loc) · 26.3 KB
/
CollectionClass.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
<?php
/**
* @file
*
* Collection Class Class
*/
/**
* This CLASS caches the streams so once you call a getstream once it will always return
* the same stream as long as you are using the instance of this class. Cached to
* prevent mutiple hits to fedora. maybe a bit confusing though if this class is used in
* a different context.
*/
class CollectionClass {
public static $COLLECTION_CLASS_COLLECTION_POLICY_STREAM = 'COLLECTION_POLICY';
public static $COLLECTION_CLASS_COLLECTION_VIEW_STREAM = 'COLLECTION_VIEW';
private $contentModelStream = NULL;
private $collectionPolicyStream = NULL;
private $collectionViewStream = NULL;
public $collectionObject = NULL;
/**
* Creates a collection object. Optionally can associate it with a single collection with parameter $pid.
*
* @param string $pid The pid of the collection to represent.
* @return CollectionClass
*/
function __construct($pid = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$this->collectionObject = new ObjectHelper();
$this->pid = $pid;
}
public static function getCollectionQuery($pid) {
$query = self::getCollectionQueryFromStream($pid);
if (!$query) {
$query = self::getDefaultCollectionQuery($pid);
}
drupal_alter("islandora_collection_query", $query, $pid);
return $query;
}
protected static function getCollectionQueryFromStream($pid) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if ($item->exists() && array_key_exists('QUERY', $item->datastreams)) {
return $item->get_datastream_dissemination('QUERY');
}
else {
return FALSE;
}
}
protected static function getDefaultCollectionQuery($pid) {
return 'select $object $title from <#ri>
where ($object <fedora-model:label> $title
and ($object <fedora-rels-ext:isMemberOfCollection> <info:fedora/' . $pid . '>
or $object <fedora-rels-ext:isMemberOf> <info:fedora/' . $pid . '>)
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>)
order by $title';
}
/**
* Gets objects related to this object. Must include offset and limit!
*
* Calls self::getRelatedItems() but requires limit and offset.
*
* @param $pid string
* A string containing a Fedora PID.
* @param $limit
* An integer
* @param type $offset
* @param type $itqlquery
* @return type
*/
function getRelatedObjects($pid, $limit, $offset, $itqlquery=NULL) {
return $this->getRelatedItems($pid, $itqlquery, $limit, $offset);
}
/**
* Gets objects related to this item.
*
* Query the resource index using the provided iTQL query. If no query is
* provided, one should be obtained via self::getCollectionQuery() which
* grabs the child objects.
*
* @param $pid string
* A string containing a PID which may be substituted into the query,
* in place of the %parent_collection% placeholder.
* @param $query_string string
* An optional iTQL query.
* @param $limit int
* An optional integer to limit the number of results returned.
* @param int $offset
* An optional integer used to offset the results returned. (Query should
* involve a sort to maintain consistency.
* @return string
* Sparql XML results from the resource index.
*/
function getRelatedItems($pid, $query_string = NULL, $limit = NULL, $offset = NULL) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if (!fedora_repository_check_perm(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or access to Fedora denied."), 'error');
return ' ';
}
if ($query_string === NULL) {
$query_string = self::getCollectionQuery($pid);
}
// Replace %parent_collection% with the actual collection PID
$query_string = preg_replace("/\%parent_collection\%/", "<info:fedora/$pid>", $query_string);
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$settings = array(
'type' => 'tuples',
'flush' => TRUE,
'format' => 'Sparql',
'lang' => 'itql',
'stream' => 'on',
'query' => $query_string
);
if ($limit > 0) {
$settings['limit'] = $limit;
}
if ($offset > 0) {
$settings['offset'] = $offset;
}
$url .= '?' . http_build_query($settings, NULL, '&');
$content = do_curl($url);
return $content;
}
/**
* get Collection Policy Stream ?
* @param type $collection_pid
* @return type
*/
function getCollectionPolicyStream($collection_pid) {
if ($this->collectionPolicyStream != NULL) {
return $this->collectionPolicyStream;
}
$this->collectionPolicyStream = $this->getStream($collection_pid, CollectionClass :: $COLLECTION_CLASS_COLLECTION_POLICY_STREAM, 0);
return $this->collectionPolicyStream;
}
/**
* get Relationship element ?
* @param type $collection_pid
* @return type
*/
function getRelationshipElement($collection_pid) {
$stream = $this->getCollectionPolicyStream($collection_pid);
try {
$xml = new SimpleXMLElement($stream);
} catch (Exception $e) {
drupal_set_message(t('Error getting relationship element from policy stream @e', array('@e' => check_plain($e->getMessage()))), 'error');
return;
}
$relationship = $xml->relationship[0];
return $relationship;
}
/**
* get Collection View Stream ?
* @param type $collection_pid
* @return type
*/
function getCollectionViewStream($collection_pid) {
$this->collectionViewStream = $this->getStream($collection_pid, CollectionClass :: $COLLECTION_CLASS_COLLECTION_VIEW_STREAM, 0);
return $this->collectionViewStream;
}
/**
* get Stream ?
* @param type $pid
* @param type $dsid
* @param type $showError
* @return type
*/
function getStream($pid, $dsid, $showError = 1) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new fedora_item($pid);
return isset($item->datastreams[$dsid]) ? $item->get_datastream_dissemination($dsid) : NULL;
}
/**
* get Pid name space ?
* @param type $pid
* @param type $dsid
* @return type
*/
function getPidNameSpace($pid, $dsid) {
$stream = $this->getCollectionPolicyStream($pid);
try {
$xml = new SimpleXMLElement($stream);
} catch (Exception $e) {
drupal_set_message(t('Error getting PID namespace @e', array('@e' => check_plain($e->getMessage()))), 'error');
return;
}
foreach ($xml->contentmodels->contentmodel as $contentModel) {
// $contentModelName=strip_tags($contentModel['name']);
$contentdsid = strip_tags($contentModel->dsid);
if (strtolower($contentdsid) == strtolower($dsid)) {
return strip_tags($contentModel->pid_namespace->asXML());
}
}
drupal_set_message(t('Error getting PID namespace! using collection pid of !pid and content model of !dsid', array('!pid' => $pid, '!dsid' => $dsid)), 'error');
return NULL;
}
/**
* gets a list of content models from a collection policy
* @param type $collection_pid
* @param type $showError
* @return ContentModel
*/
function getContentModels($collection_pid, $showError = TRUE) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
$collection_stream = $this->getCollectionPolicyStream($collection_pid);
try {
$xml = new SimpleXMLElement($collection_stream);
} catch (Exception $e) {
if ($showError) {
drupal_set_message(t('@e', array('@e' => check_plain($e->getMessage()))), 'error');
}
return NULL;
}
foreach ($xml->contentmodels->contentmodel as $content_model) {
$contentModel = new ContentModel();
$contentModel->contentModelDsid = $content_model->dsid;
$contentModel->contentModelPid = $content_model->pid;
$contentModel->pidNamespace = $content_model->pidNamespace;
$contentModel->contentModelName = $content_model['name'];
$models[] = $contentModel;
}
return $models;
}
/**
* using the collection policies pid namespace get a new pid by calling fedora' get next pid and appending it to the namespace
* $pid is the $pid of the content model
* $dsid is the datastream id of the content model.
*/
function getNextPid($pid, $dsid) {
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$pidNameSpace = $this->getPidNameSpace($pid, $dsid);
$pname = substr($pidNameSpace, 0, strpos($pidNameSpace, ":"));
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
return Fedora_Item::get_next_pid_in_namespace($pname);
}
/**
* gets the form handler file, class and method and returns them in an array
*
* @param string $pid The content model PID
* @param string $dsid The content model DSID
* @return array The file, class and method name to handle the ingest form.
*/
function getFormHandler($pid, $dsid) {
$stream = $this->getContentModelStream($pid, $dsid);
try {
$xml = new SimpleXMLElement($stream);
} catch (Exception $e) {
drupal_set_message(t('Error Getting FormHandler: @e', array('@e' => check_plain($e->getMessage()))), 'error');
return NULL;
}
$formHandler = $xml->ingest_form;
if ($formHandler != NULL) {
$handlerDsid = strip_tags($formHandler['dsid']);
$handlerMethod = strip_tags($formHandler->form_builder_method->form_handler->asXML());
$handlerFile = strip_tags($formHandler->form_builder_method->file->asXML());
$handlerClass = strip_tags($formHandler->form_builder_method->class_name->asXML());
$handlerModule = strip_tags($formHandler->form_builder_method->module->asXML());
$returnArray = array();
$returnArray['module'] = $handlerModule;
$returnArray['method'] = $handlerMethod;
$returnArray['class'] = $handlerClass;
$returnArray['file'] = $handlerFile;
return $returnArray;
}
drupal_set_message(t("Error getting form handler. No handler found for !pid - !dsid", array('!pid' => $pid, '!dsid' => $dsid)), 'error');
return NULL;
}
/**
* get Allowed Mime Types
* @param type $contentModelPid
* @param type $contentModel_dsid
* @return type
*/
function getAllowedMimeTypes($contentModelPid, $contentModel_dsid) {
$stream = $this->getContentModelStream($contentModelPid, $contentModel_dsid);
try {
$xml = new SimpleXMLElement($stream);
} catch (Exception $e) {
drupal_set_message(t('Error getting content model stream for mime types @e', array('@e' => check_plain($e->getMessage()))), 'error');
return;
}
foreach ($xml->mimetypes->type as $type) {
$types[] = $type;
}
return $types;
}
/**
* Grabs the rules from the content model stream
* file the file that has been uploaded
*
* @param type $file
* @param type $mimetype
* @param type $pid
* @param type $dsid
* @return type
*/
function getAndDoRules($file, $mimetype, $pid, $dsid) {
if (!fedora_repository_check_perm('ingest new fedora objects', $pid)) {
drupal_set_message(t('You do not have permission to ingest objects.'));
return FALSE;
}
$stream = $this->getContentModelStream($pid, $dsid);
try {
$xml = new SimpleXMLElement($stream);
} catch (Exception $e) {
drupal_set_message(t('Error getting content model stream @e', array('@e' => check_plain($e->getMessage()))), 'error');
return FALSE;
}
foreach ($xml->ingest_rules->rule as $rule) {
foreach ($rule->applies_to as $type) {
if (!strcmp(trim($type), trim($mimetype))) {
$methods = $rule->methods->method;
return $this->callMethods($file, $methods);
}
}
}
}
/**
* calls the methods defined in the content model rules .xml file stored in a Fedora object
*
* @param type $file
* @param type $methods
* @return type
*/
function callMethods($file, $methods) {
foreach ($methods as $method) {
$phpFile = strip_tags($method->file->asXML());
$phpClass = strip_tags($method->class_name->asXML());
$phpMethod = strip_tags($method->method_name->asXML());
$file_ext = strip_tags($method->modified_files_ext->asXML());
$parameters = $method->parameters->parameter;
$dsid = strip_tags($method->datastream_id);
$parametersArray = array();
if (isset($parameters)) {
foreach ($parameters as $parameter) {
$name = strip_tags($parameter['name']);
$value = strip_tags($parameter->asXML());
$parametersArray["$name"] = $value;
}
}
// module_load_include( $phpFile, 'fedora_repository', ' ');
require_once(drupal_get_path('module', 'fedora_repository') . '/' . $phpFile);
$thisClass = new $phpClass ();
$returnValue = $thisClass->$phpMethod($parametersArray, $dsid, $file, $file_ext);
if (!$returnValue) {
drupal_set_message(t('Error! Failed running content model method !m !rv', array('!m' => $phpMethod, '!rv' => $returnValue)));
return FALSE;
}
}
return TRUE;
}
/**
* grabs a xml form definition from a content model and builds
* the form using drupals forms api
*
* @param type $form
* @param type $form_state
* @param type $contentModelPid
* @param type $contentModelDsid
* @return type
*/
function build_ingest_form(&$form, &$form_state, $contentModelPid, $contentModelDsid) {
$stream = $this->getContentModelStream($contentModelPid, $contentModelDsid);
try {
$xml = new SimpleXMLElement($stream);
} catch (Exception $e) {
drupal_set_message(t('Error getting ingest form stream @e', array('@e' => check_plain($e->getMessage()))), 'error');
return FALSE;
}
$docRoot = $_SERVER['DOCUMENT_ROOT'];
$file = (isset($form_state['values']['ingest-file-location']) ? $form_state['values']['ingest-file-location'] : '');
// $fullPath = $docRoot . '' . $file;
$fullpath = $file;
// $form = array();
$form['step'] = array(
'#type' => 'hidden',
'#value' => (isset($form_state['values']['step']) ? $form_state['values']['step'] : 0) + 1,
);
$form['ingest-file-location'] = array(
'#type' => 'hidden',
'#value' => $file,
);
$form['content_model_name'] = array(
'#type' => 'hidden',
'#value' => $contentModelDsid
);
$form['models'] = array(//content models available
'#type' => 'hidden',
'#value' => $form_state['values']['models'],
);
$form['fullpath'] = array(
'#type' => 'hidden',
'#value' => $fullpath,
);
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['indicator']['ingest-file-location'] = array(
'#type' => 'file',
'#title' => t('Upload Document'),
'#size' => 48,
'#description' => t('Full text'),
);
if ($xml->ingest_form->hide_file_chooser == 'TRUE') {
$form['indicator']['ingest-file-location']['#type'] = 'hidden';
}
$ingest_form = $xml->ingest_form; //should only be one
$drupal_module = strip_tags($ingest_form->form_builder_method->module->asXML());
if (empty($drupal_module)) {
$drupal_module = 'fedora_repository';
}
$phpFile = strip_tags($ingest_form->form_builder_method->file->asXML());
$phpClass = strip_tags($ingest_form->form_builder_method->class_name->asXML());
$phpMethod = strip_tags($ingest_form->form_builder_method->method_name->asXML());
$dsid = strip_tags($ingest_form['dsid']);
// module_load_include('php', 'fedora_repository', $phpFile);
require_once(drupal_get_path('module', $drupal_module) . '/' . $phpFile);
$thisClass = new $phpClass ();
return $thisClass->$phpMethod($form, $ingest_form, $form_state['values'], $form_state);
}
/**
* this will also create a personal collection for an existing user if they don't have one
* not using this function currently
*
* @param type $user
* @return type
*/
function createUserCollection(& $user) {
if (isset($user->fedora_personal_pid)) {
return;
}
$username = array(
'name' => variable_get('fedora_admin_user', 'fedoraAdmin')
);
$admin_user = user_load($username);
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$connectionHelper = new ConnectionHelper();
try {
$soapClient = $connectionHelper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M'));
$pidNameSpace = variable_get('fedora_repository_pid', 'vre:');
$pidNameSpace = substr($pidNameSpace, 0, strpos($pidNameSpace, ":"));
$params = array(
'numPIDs' => '',
'pidNamespace' => $pidNameSpace
);
$object = $soapClient->__soapCall('getNextPID', array(
$params
));
} catch (exception $e) {
drupal_set_message(t('Error getting Next PID: @e', array('@e' => check_plain($e->getMessage()))), 'error');
return FALSE;
}
$pid = implode(get_object_vars($object));
$pidNumber = strstr($pid, ":");
$pid = $pidNameSpace . ':' . 'USER-' . $user->name . '-' . substr($pidNumber, 1);
$personal_collection_pid = array(
'fedora_personal_pid' => $pid
);
module_load_include('inc', 'fedora_repository', 'plugins/PersonalCollectionClass');
$personalCollectionClass = new PersonalCollectionClass();
if (!$personalCollectionClass->createCollection($user, $pid, $soapClient)) {
drupal_set_message(t("Did not create a personal collection object for !u", array('!u' => $user->name)));
return FALSE; //creation failed don't save the collection pid in drupal db
}
user_save($user, $personal_collection_pid);
return TRUE;
}
/**
* Queries a collection object for an xslt to format how the
* collection of objects is displayed.
*
* @param type $pid
* @param type $path
* @param type $canUseDefault
* @return type
*/
function getXslContent($pid, $path, $canUseDefault = TRUE) {
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
$xslContent = $collectionClass->getCollectionViewStream($pid);
//If there's no XSLT from the object, then check if the one which used to exist, does...
if (!$xslContent && $canUseDefault && file_exists($path . '/xsl/sparql_to_html.xsl')) {
$xslContent = file_get_contents($path . '/xsl/sparql_to_html.xsl');
}
return $xslContent;
}
/**
* show field sets ?
* @global type $base_url
* @global type $user
* @param type $page_number
* @return string
*/
function showFieldSets($page_number) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'CollectionPolicy');
global $base_url;
global $user;
$tabset = array();
$query = self::getCollectionQuery($this->pid);
$item = new Fedora_Item($this->pid);
$results = $this->getRelatedItems($this->pid, $query);
$collection_items = $this->renderCollection($results, $this->pid, NULL, NULL, $page_number);
$show_batch_tab = FALSE;
$policy = CollectionPolicy::loadFromCollection($this->pid, TRUE);
if (!empty($policy)) {
$content_models = $policy->getContentModels();
}
if (count($content_models) == 1 && $content_models[0]->pid == "islandora:collectionCModel") {
$show_batch_tab = FALSE;
}
// Check the form post to see if we are in the middle of an ingest operation.
$add_selected = ((!empty($_POST['form_id']) && $_POST['form_id'] == 'fedora_repository_ingest_form') || !$collection_items);
$view_selected = !$add_selected;
$tabset['view_tab'] = array(
'#type' => 'tabpage',
'#title' => t('View'),
'#selected' => $view_selected,
'#content' => $collection_items,
'#tab_name' => 'view-tab',
);
$add_to_collection = $this->getIngestInterface();
if (!empty($add_to_collection)) {
$tabset['add_tab'] = array(
'#type' => 'tabpage',
'#title' => t('Add'),
'#selected' => $add_selected,
// This will be the content of the tab.
'#content' => $add_to_collection,
'#tab_name' => 'add-tab',
);
}
return $tabset;
}
/**
* get Ingest Interface ??
* @global type $base_url
* @return string
*/
function getIngestInterface() {
module_load_include('inc', 'fedora_repository', 'CollectionPolicy');
$collectionPolicyExists = $this->collectionObject->getMimeType($this->pid, CollectionPolicy::getDefaultDSID());
if (fedora_repository_check_perm(ObjectHelper :: $INGEST_FEDORA_OBJECTS, $this->pid) && $collectionPolicyExists) {
if (!empty($collectionPolicyExists)) {
$allow = TRUE;
if (module_exists('fedora_fesl')) {
$allow = fedora_fesl_check_roles($this->pid, 'write');
}
if ($allow) {
$ingestObject = drupal_get_form('fedora_repository_ingest_form', $this->pid);
}
}
}
else {
$ingestObject = '';
}
return $ingestObject;
}
/**
* Assemble results in a somewhat more logical manner...
*
* ... Compared to generating a table in XSLT to contain a list (as
* renderCollection() used to do/does by default).
*
* @param $sparql_results array
* The array of results as yielded by ObjectHelper::parseSparqlResults()
* (and those associated functions which make use of it).
* Each result must contain:
* - 'object': The PID/URI of the child object.
* - 'title': A title for the child object.
* and may contain:
* - 'thumbnail': URI to a datastream. (will default to the 'TN' stream on the child)
* @return
* An array to be passed to drupal_render, containing a pager, an unordered
* list of items, and another pager.
*/
public static function assembleCollectionView($sparql_results) {
module_load_include('inc', 'fedora_repository', 'PagerSetup');
$per_page = 20; //XXX: Make this configurable.
$pager_name = 0;
$total = count($sparql_results);
$pager_page = fedora_repository_setup_pager($pager_name, $per_page, $total);
$max_title_length = 60;
$results = array();
foreach (array_slice($sparql_results, $per_page * $pager_page, $per_page) as $result) {
$title = $result['title'];
$truncated_title = truncate_utf8($title, $max_title_length, TRUE, TRUE, 5);
$obj_path = "fedora/repository/{$result['object']}";
//Get a thumbnail
$tn_path = ($result['thumbnail'] ?
"fedora/repository/{$result['thumbnail']}":
"$obj_path/TN");
$thumbnail = _fedora_repository_render_image($tn_path);
$results[] = array(
'data' => l($thumbnail, $obj_path, array(
'html' => TRUE,
'attributes' => array(
'class' => 'results-image',
),
)) . l($truncated_title, $obj_path, array('attributes' => array('class' => 'results-text'))),
);
}
if (!$results) {
drupal_set_message(t("No objects in this collection (or bad query)."));
}
else {
$first = $per_page * $pager_page;
$last = (($total - $first) > $per_page)?
($first + $per_page):
$total;
$results_range_text = t('Results @first to @last of @total', array(
'@first' => $first + 1,
'@last' => $last,
'@total' => $total,
));
return array(
array(
'#type' => 'markup',
'#value' => theme('pager', array(), $per_page, $pager_name),
),
array(
'#type' => 'markup',
'#value' => theme('item_list', $results, $result_range_text, 'ul', array(
'class' => 'islandora-collection-results-list',
))
),
array(
'#type' => 'markup',
'#value' => theme('pager', array(), $per_page, $pager_name)
),
);
}
}
/**
* render collection
* @global type $base_url
* @param type $content
* @param type $pid
* @param type $dsId
* @param type $collection
* @param int $pageNumber
* @return type
*/
function renderCollection($content, $pid, $dsId, $collectionName, $pageNumber = NULL) {
$path = drupal_get_path('module', 'fedora_repository');
global $base_url;
$contentModels = $this->collectionObject->get_content_models_list($pid);
if (empty($collectionName)) {
$collectionName = menu_get_active_title();
}
$xslContent = $this->getXslContent($pid, $path);
$objectList = '';
if (isset($content) && $content != FALSE) {
if (!$xslContent) { //Didn't find an XSLT.
return drupal_render(
self::assembleCollectionView(
$this->collectionObject->parseSparqlResults(
$content
)
)
);
}
else {
if (!$pageNumber) {
$pageNumber = 1;
}
//get collection list and display using xslt-------------------------------------------
$input = new DomDocument();
$input->loadXML(trim($content));
$results = $input->getElementsByTagName('result');
if ($results->length > 0) {
try {
$proc = new XsltProcessor();
$options = array( //Could make this the return of a hook?
'collectionPid' => $pid,
'collectionTitle' => $collectionName,
'baseUrl' => $base_url,
'path' => "$base_url/$path",
'hitPage' => $pageNumber,
);
$proc->setParameter('', $options);
$proc->registerPHPFunctions();
$xsl = new DomDocument();
$xsl->loadXML($xslContent);
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input);
$objectList = $newdom->saveHTML(); //is the xml transformed to html as defined in the xslt associated with the collection object
if (!$objectList) {
throw new Exception("Invalid XML.");
}
} catch (Exception $e) {
drupal_set_message(check_plain($e->getMessage()), 'error');
return '';
}
}
}
}
else {
drupal_set_message(t("No objects in this collection (or bad query)."));
}
return $objectList;
}
}