Browse Source

Add the ability to render a collection view without an XSLT.

pull/99/head
Adam Vessey 13 years ago
parent
commit
4182bfac27
  1. 241
      CollectionClass.inc

241
CollectionClass.inc

@ -71,58 +71,79 @@ class CollectionClass {
} }
/** /**
* gets objects related to this object. must include offset and limit * Gets objects related to this object. Must include offset and limit!
* calls getRelatedItems but enforces limit and offset *
* @param type $pid * Calls self::getRelatedItems() but requires limit and offset.
* @param type $limit *
* @param $pid string
* A string containing a Fedora PID.
* @param $limit
* An integer
* @param type $offset * @param type $offset
* @param type $itqlquery * @param type $itqlquery
* @return type * @return type
*/ */
static function getRelatedObjects($pid, $limit, $offset, $itqlquery=NULL) { static function getRelatedObjects($pid, $limit, $offset, $itqlquery=NULL) {
if (!isset($itqlquery)) {
$itqlquery = self::get_collection_query($pid);
}
return self::getRelatedItems($pid, $itqlquery, $limit, $offset); return self::getRelatedItems($pid, $itqlquery, $limit, $offset);
} }
/** /**
* Gets objects related to this item. It will query the object for a Query stream and use that as a itql query * Gets objects related to this item.
* or if there is no query stream it will use the default. If you pass a query to this method it will use the passed in query no matter what *
* @global type $user * Query the resource index using the provided iTQL query. If no query is
* @param type $pid * provided, one should be obtained via self::get_collection_query() which
* @param type $itqlquery * grabs the child objects.
* @param int $limit *
* @param $pid string
* A string containing a PID which may be substituted into the query,
* in place of the %parent_collection% placeholder.
* @param $itqlquery string
* An optional iTQL query.
* @param $limit int
* An optional integer to limit the number of results returned.
* @param int $offset * @param int $offset
* @return type * 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.
*/ */
static function getRelatedItems($pid, $itqlquery = NULL, $limit = NULL, $offset = NULL) { static function getRelatedItems($pid, $query_string = NULL, $limit = NULL, $offset = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper'); module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils'); module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
if (!isset($offset)) {
$offset = 0; if (!fedora_repository_access(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid)) {
}
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'); drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or access to Fedora denied."), 'error');
return ' '; return ' ';
} }
$objectHelper = new ObjectHelper(); $objectHelper = new ObjectHelper();
$query_string = $itqlquery;
if (!isset($query_string)) { if ($query_string === NULL) {
$query_string = self::get_collection_query($pid); $query_string = self::get_collection_query($pid);
} }
// Replace %parent_collection% with the actual collection PID // Replace %parent_collection% with the actual collection PID
$query_string = preg_replace("/\%parent_collection\%/", "<info:fedora/$pid>", $query_string); $query_string = preg_replace("/\%parent_collection\%/", "<info:fedora/$pid>", $query_string);
$query_string = htmlentities(urlencode($query_string));
$content = '';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch'); $url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=Sparql&limit=$limit&offset=$offset&lang=itql&stream=on&query=" . $query_string;
$content .= do_curl($url);
$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; return $content;
} }
@ -501,9 +522,12 @@ class CollectionClass {
module_load_include('inc', 'fedora_repository', 'CollectionClass'); module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass(); $collectionClass = new CollectionClass();
$xslContent = $collectionClass->getCollectionViewStream($pid); $xslContent = $collectionClass->getCollectionViewStream($pid);
if (!$xslContent && $canUseDefault) { //no xslt so we will use the default sent with the module
//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'); $xslContent = file_get_contents($path . '/xsl/sparql_to_html.xsl');
} }
return $xslContent; return $xslContent;
} }
@ -530,7 +554,6 @@ class CollectionClass {
$results = $this->getRelatedItems($this->pid, $query); $results = $this->getRelatedItems($this->pid, $query);
$collection_items = $this->renderCollection($results, $this->pid, NULL, NULL, $page_number); $collection_items = $this->renderCollection($results, $this->pid, NULL, NULL, $page_number);
//$collection_item = new Fedora_Item($this->pid); //XXX: This didn't seem to be used...
$show_batch_tab = FALSE; $show_batch_tab = FALSE;
$policy = CollectionPolicy::loadFromCollection($this->pid, TRUE); $policy = CollectionPolicy::loadFromCollection($this->pid, TRUE);
@ -566,17 +589,6 @@ class CollectionClass {
); );
} }
if ($show_batch_tab && user_access('create batch process')) { //XXX: Is this not put in by the batch module?
$tabset['batch_ingest_tab'] = array(
// #type and #title are the minimum requirements.
'#type' => 'tabpage',
'#title' => t('Batch Ingest'),
// This will be the content of the tab.
'#content' => drupal_get_form('batch_creation_form', $this->pid, $content_models),
'#tab_name' => 'batch-ingest-tab',
);
}
return $tabset; return $tabset;
} }
@ -608,6 +620,33 @@ class CollectionClass {
return $ingestObject; return $ingestObject;
} }
/**
* Unfortunate function, I know...
*
* Does just what it says: Hacks the default Drupal pager such that it might
* be rendered.
*/
protected static function _hack_pager($pager_name, $per_page, $total_items) {
global $pager_total, $pager_page_array;
$pager_total[$pager_name] = ceil($total_items / $per_page);
$page_info = explode(',', isset($_GET['page']) ? $_GET['page'] : '');
$page = $page_info[$pager_name];
if ($page < 0) {
$page = 0;
}
if (!isset($pager_page_array)) {
$pager_page_array = pager_load_array($page, $pager_name, $page_info);
}
else {
$pager_page_array = pager_load_array($page, $pager_name, $pager_page_array);
}
$page = $pager_page_array[$pager_name];
return $page;
}
/** /**
* render collection * render collection
* @global type $base_url * @global type $base_url
@ -631,60 +670,106 @@ class CollectionClass {
$fedoraItem = NULL; $fedoraItem = NULL;
if (!$pageNumber) {
$pageNumber = 1;
}
if (empty($collectionName)) { if (empty($collectionName)) {
$collectionName = menu_get_active_title(); $collectionName = menu_get_active_title();
} }
$xslContent = $this->getXslContent($pid, $path); $xslContent = $this->getXslContent($pid, $path);
//get collection list and display using xslt-------------------------------------------
$objectList = ''; $objectList = '';
if (isset($content) && $content != FALSE) { if (isset($content) && $content != FALSE) {
$input = new DomDocument(); if (!$xslContent) { //Didn't find an XSLT.
$input->loadXML(trim($content)); module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$results = $input->getElementsByTagName('result'); $intermediate_results = ObjectHelper::parse_sparql_results($content);
if ($results->length > 0) { unset($content);
try {
$proc = new XsltProcessor(); $per_page = 20; //XXX: Make this configurable.
$options = array( //Could make this the return of a hook? $pager_name = 0;
'collectionPid' => $collection_pid, $total = count($intermediate_results);
'collectionTitle' => $collectionName, $pager_page = self::_hack_pager($pager_name, $per_page, $total);
'baseUrl' => $base_url,
'path' => "$base_url/$path", $results = array();
'hitPage' => $pageNumber, foreach (array_slice($intermediate_results, $per_page * $pager_page, $per_page) as $result) {
$title = $result['title'];
$obj_path = "fedora/repository/{$result['object']}";
$thumbnail = theme('image', "$obj_path/TN", $title, $title, array(), FALSE);
$results[] = array(
array(
'data' => l($thumbnail, $obj_path, array(
'html' => TRUE,
)),
),
array(
'data' => l($title, $obj_path),
),
); );
}
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;
$objectList = '<h3>' . t('Results @first to @last of @total', array(
'@first' => $first + 1,
'@last' => $last,
'@total' => $total,
)) . '</h3>';
$objectList .= theme('pager', array(), $per_page, $pager_name);
$objectList .= theme('table', NULL, $results);
$objectList .= theme('pager', array(), $per_page, $pager_name);
}
}
else {
if (!$pageNumber) {
$pageNumber = 1;
}
if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 50100) { //get collection list and display using xslt-------------------------------------------
$proc->setParameter('', $options); $input = new DomDocument();
} $input->loadXML(trim($content));
else { $results = $input->getElementsByTagName('result');
foreach ($options as $name => $value) { if ($results->length > 0) {
$proc->setParameter('', $name, $value); try {
$proc = new XsltProcessor();
$options = array( //Could make this the return of a hook?
'collectionPid' => $collection_pid,
'collectionTitle' => $collectionName,
'baseUrl' => $base_url,
'path' => "$base_url/$path",
'hitPage' => $pageNumber,
);
if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 50100) {
$proc->setParameter('', $options);
}
else {
foreach ($options as $name => $value) {
$proc->setParameter('', $name, $value);
}
} }
}
$proc->registerPHPFunctions(); $proc->registerPHPFunctions();
$xsl = new DomDocument(); $xsl = new DomDocument();
$xsl->loadXML($xslContent); $xsl->loadXML($xslContent);
// php xsl does not seem to work with namespaces so removing it below // php xsl does not seem to work with namespaces so removing it below
// I may have just been being stupid here // I may have just been being stupid here
// $content = str_ireplace('xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result"', '', $content); // $content = str_ireplace('xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result"', '', $content);
$xsl = $proc->importStylesheet($xsl); $xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input); $newdom = $proc->transformToDoc($input);
$objectList = $newdom->saveXML(); //is the xml transformed to html as defined in the xslt associated with the collection object $objectList = $newdom->saveHTML(); //is the xml transformed to html as defined in the xslt associated with the collection object
if (!$objectList) { if (!$objectList) {
throw new Exception("Invalid XML."); throw new Exception("Invalid XML.");
}
} catch (Exception $e) {
drupal_set_message(check_plain($e->getMessage()), 'error');
return '';
} }
} catch (Exception $e) {
drupal_set_message(check_plain($e->getMessage()), 'error');
return '';
} }
} }
} }

Loading…
Cancel
Save