Skip to content

Instantly share code, notes, and snippets.

@bryjbrown
Last active April 15, 2016 10:33
Show Gist options
  • Save bryjbrown/0f457852b7e02e35068cb04aaf6533b0 to your computer and use it in GitHub Desktop.
Save bryjbrown/0f457852b7e02e35068cb04aaf6533b0 to your computer and use it in GitHub Desktop.
<?php
function getCollectionData($collection_pid) {
$user = user_load(1);
$repository = islandora_get_tuque_connection($user);
$query = <<<EOQ
SELECT ?pid ?label ?cmodel
FROM <#ri>
WHERE {
?pid <info:fedora/fedora-system:def/relations-external#isMemberOfCollection> <info:fedora/$collection_pid> ;
<fedora-model:label> ?label ;
<fedora-model:hasModel> ?cmodel
FILTER (?cmodel != <info:fedora/fedora-system:FedoraObject-3.0>)
}
EOQ;
$results = $repository->repository->ri->sparqlQuery($query);
$subCollections = array();
$recordCount = 0;
foreach($results as $result) {
if ($result['cmodel']['value'] != 'islandora:collectionCModel') {
$recordCount = $recordCount + 1;
}
else {
$subCollections[] = $result['pid']['value'];
}
}
$ret = array('records' => $recordCount, 'collections' => $subCollections);
$collection_title = islandora_object_load($collection_pid)->label;
$msg = "<a href=\"/islandora/object/$collection_pid\">$collection_title [$recordCount]</a>\n<ul>\n";
foreach ($subCollections as $subCollection) {
$submsg = getCollectionData($subCollection);
$msg .= "<li>$submsg</li>\n";
}
$msg .= "</ul>";
return $msg;
}
// Initiate function with target collection PID of your choice
echo getCollectionData('islandora:root');
?>
@djevans
Copy link

djevans commented Apr 12, 2016

This would work well inside a custom module (providing content for a block, for example).
Is the 'isMemberOfCollection' predicate transitive? If so you might be able to get all the subcollections using just one SPARQL query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment