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. 245
      CollectionClass.inc

245
CollectionClass.inc

@ -71,58 +71,79 @@ class CollectionClass {
}
/**
* gets objects related to this object. must include offset and limit
* calls getRelatedItems but enforces limit and offset
* @param type $pid
* @param type $limit
* 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
*/
static function getRelatedObjects($pid, $limit, $offset, $itqlquery=NULL) {
if (!isset($itqlquery)) {
$itqlquery = self::get_collection_query($pid);
}
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
* 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
* @param type $pid
* @param type $itqlquery
* @param int $limit
* 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::get_collection_query() 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 $itqlquery string
* An optional iTQL query.
* @param $limit int
* An optional integer to limit the number of results returned.
* @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', 'api/fedora_utils');
if (!isset($offset)) {
$offset = 0;
}
global $user;
if (!fedora_repository_access(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
if (!fedora_repository_access(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 ' ';
}
$objectHelper = new ObjectHelper();
$query_string = $itqlquery;
if (!isset($query_string)) {
if ($query_string === NULL) {
$query_string = self::get_collection_query($pid);
}
// Replace %parent_collection% with the actual collection PID
$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 .= "?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;
}
@ -501,9 +522,12 @@ class CollectionClass {
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
$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');
}
return $xslContent;
}
@ -530,7 +554,6 @@ class CollectionClass {
$results = $this->getRelatedItems($this->pid, $query);
$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;
$policy = CollectionPolicy::loadFromCollection($this->pid, TRUE);
@ -565,17 +588,6 @@ class CollectionClass {
'#tab_name' => 'add-tab',
);
}
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;
}
@ -608,6 +620,33 @@ class CollectionClass {
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
* @global type $base_url
@ -631,60 +670,106 @@ class CollectionClass {
$fedoraItem = NULL;
if (!$pageNumber) {
$pageNumber = 1;
}
if (empty($collectionName)) {
$collectionName = menu_get_active_title();
}
$xslContent = $this->getXslContent($pid, $path);
//get collection list and display using xslt-------------------------------------------
$objectList = '';
if (isset($content) && $content != FALSE) {
$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' => $collection_pid,
'collectionTitle' => $collectionName,
'baseUrl' => $base_url,
'path' => "$base_url/$path",
'hitPage' => $pageNumber,
if (!$xslContent) { //Didn't find an XSLT.
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$intermediate_results = ObjectHelper::parse_sparql_results($content);
unset($content);
$per_page = 20; //XXX: Make this configurable.
$pager_name = 0;
$total = count($intermediate_results);
$pager_page = self::_hack_pager($pager_name, $per_page, $total);
$results = array();
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 (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 50100) {
$proc->setParameter('', $options);
}
else {
foreach ($options as $name => $value) {
$proc->setParameter('', $name, $value);
}
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;
}
//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' => $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();
$xsl = new DomDocument();
$xsl->loadXML($xslContent);
// php xsl does not seem to work with namespaces so removing it below
// I may have just been being stupid here
// $content = str_ireplace('xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result"', '', $content);
$proc->registerPHPFunctions();
$xsl = new DomDocument();
$xsl->loadXML($xslContent);
// php xsl does not seem to work with namespaces so removing it below
// I may have just been being stupid here
// $content = str_ireplace('xmlns="http://www.w3.org/2001/sw/DataAccess/rf1/result"', '', $content);
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input);
$xsl = $proc->importStylesheet($xsl);
$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) {
throw new Exception("Invalid XML.");
if (!$objectList) {
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