Browse Source

Allow breadcrumbs to reliably be generated multiple times.

pull/306/head
Adam Vessey 12 years ago
parent
commit
13e8839d50
  1. 88
      includes/breadcrumb.inc

88
includes/breadcrumb.inc

@ -25,13 +25,8 @@
* drupal_set_breadcrumb(). * drupal_set_breadcrumb().
*/ */
function islandora_get_breadcrumbs($object) { function islandora_get_breadcrumbs($object) {
$breadcrumbs = array(); $breadcrumbs = islandora_get_breadcrumbs_recursive($object->id, $object->repository);
islandora_get_breadcrumbs_recursive($object->id, $breadcrumbs, $object->repository); array_pop($breadcrumbs);
if (isset($breadcrumbs[0])) {
// Remove the actual object.
unset($breadcrumbs[0]);
}
$breadcrumbs = array_reverse($breadcrumbs);
return $breadcrumbs; return $breadcrumbs;
} }
@ -40,9 +35,6 @@ function islandora_get_breadcrumbs($object) {
* *
* @todo Make fully recursive... * @todo Make fully recursive...
* *
* @todo Could use some clean up, can't be called multiple times safely due to
* the use of static variables.
*
* @param string $pid * @param string $pid
* THe object id whose parent will be fetched for the next link. * THe object id whose parent will be fetched for the next link.
* @param array $breadcrumbs * @param array $breadcrumbs
@ -50,39 +42,44 @@ function islandora_get_breadcrumbs($object) {
* @param FedoraRepository $repository * @param FedoraRepository $repository
* The fedora repository. * The fedora repository.
*/ */
function islandora_get_breadcrumbs_recursive($pid, array &$breadcrumbs, FedoraRepository $repository) { function islandora_get_breadcrumbs_recursive($pid, FedoraRepository $repository, array &$context = NULL) {
// Before executing the query, we have a base case of accessing the top-level // Before executing the query, we have a base case of accessing the top-level
// collection. // collection.
static $max_level = 10; if ($context === NULL) {
static $level = -1; $context['level'] = 10;
if (count($breadcrumbs) === 0) {
$level = $max_level;
} }
$root = variable_get('islandora_repository_pid', 'islandora:root'); $root = variable_get('islandora_repository_pid', 'islandora:root');
if ($pid == $root) { if ($pid == $root) {
$breadcrumbs[] = l(menu_get_active_title(), 'islandora'); return array(
$breadcrumbs[] = l(t('Home'), '<front>'); l(t('Home'), '<front>'),
l(menu_get_active_title(), 'islandora'),
);
} }
else { else {
$query_string = 'select $parentObject $title $content from <#ri> $query_string = <<<EOQ
where ( SELECT ?parentObject ?title
<info:fedora/' . $pid . '> <fedora-model:label> $title FROM <#ri>
and $parentObject <fedora-model:hasModel> $content WHERE {
and ( ?object <fedora-model:label> ?title ;
<info:fedora/' . $pid . '> <fedora-rels-ext:isMemberOfCollection> $parentObject {
or <info:fedora/' . $pid . '> <fedora-rels-ext:isMemberOf> $parentObject ?object <fedora-rels-ext:isMemberOfCollection> ?parentObject .
or <info:fedora/' . $pid . '> <fedora-rels-ext:isPartOf> $parentObject }
) UNION {
and $parentObject <fedora-model:state> <info:fedora/fedora-system:def/model#Active> ?object <fedora-rels-ext:isMemberOf> ?parentObject .
) }
minus $content <mulgara:is> <info:fedora/fedora-system:FedoraObject-3.0> UNION {
minus $parentObject <mulgara:is> <info:fedora/' . $pid . '> ?object <fedora-rels-ext:isPartOf> ?parentObject .
order by $title desc'; }
$results = $repository->ri->itqlQuery($query_string); ?parentObject <fedora-model:state> <fedora-model:Active> .
FILTER (sameTerm(?object, <info:fedora/$pid>))
FILTER (!sameTerm(?object, ?parentObject))
}
ORDER BY ?title
EOQ;
$results = $repository->ri->sparqlQuery($query_string);
if (count($results) > 0 && $level > 0) { if (count($results) > 0 && $context['level'] > 0) {
$parent = $results[0]['parentObject']['value']; $parent = $results[0]['parentObject']['value'];
$this_title = $results[0]['title']['value']; $this_title = $results[0]['title']['value'];
@ -90,16 +87,23 @@ function islandora_get_breadcrumbs_recursive($pid, array &$breadcrumbs, FedoraRe
$this_title = t('-'); $this_title = t('-');
} }
$breadcrumbs[] = l($this_title, "islandora/object/$pid"); $context['level']--;
return array_merge(
$level--; islandora_get_breadcrumbs_recursive($parent, $repository, $context),
islandora_get_breadcrumbs_recursive($parent, $breadcrumbs, $repository); array(
l($this_title, "islandora/object/$pid"),
)
);
} }
else { else {
// Add an non-link, as we don't know how to get back to the root. // Add an non-link, as we don't know how to get back to the root, and
$breadcrumbs[] = '...'; // render the last two links and break (on the next pass).
// And render the last two links and break (on the next pass). return array_merge(
islandora_get_breadcrumbs_recursive($root, $breadcrumbs, $repository); islandora_get_breadcrumbs_recursive($root, $repository),
array(
'...'
)
);
} }
} }
} }

Loading…
Cancel
Save