createAdminForm();
}
/**
* drupal hook
* calls the fedora_repositorys_admin form
*/
function fedora_repository_menu() {
module_load_include('inc', 'fedora_repository', 'formClass');
$adminMenu = new formClass();
return $adminMenu->createMenu();
}
/**
* drupal hook to show help
*
* @param type $path
* @param type $arg
* @return type
*/
function fedora_repository_help($path, $arg) {
switch ($path) {
case 'admin/modules#description' :
return t('Grabs a list of items from a collection in Drupal that are presented on the home page.');
case 'node/add#fedora_repository' :
return t('Use this page to grab a list of items from a Fedora collection.');
}
}
/**
* fedora repository purge object
* @param type $pid
* @param type $name
* @return type
*/
function fedora_repository_purge_object($pid = NULL, $name = NULL) {
if (!user_access('purge objects and datastreams')) {
drupal_set_message(t('You do not have access to add a datastream to this object.'), 'error');
return '';
}
if ($pid == NULL) {
drupal_set_message(t('You must specify an object pid to purge an object.'), 'error');
return '';
}
$output = t('Are you sure you wish to purge object %name %pid!
This cannot be undone
', array(
'%name' => $name,
'%pid' => $pid)
);
$output .= drupal_get_form('fedora_repository_purge_object_form', $pid);
return $output;
}
/**
* fedora repository collection view
* @global type $user
* @param type $pid
* @param type $collection
* @param type $pageNumber
* @return type
*/
function fedora_repository_collection_view($pid = NULL, $collection = NULL, $pageNumber = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
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');
return ' ';
}
$objectHelper = new ObjectHelper();
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:root');
}
$content = '';
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
$results = $collectionClass->getRelatedItems($pid, NULL);
$content .= $objectHelper->parseContent($results, $pid, $dsId, $collection, $pageNumber);
return $content;
}
/**
* fedora repository ingest object
* @param type $collection_pid
* @param type $collection_label
* @param type $content_model
* @return type
*/
function fedora_repository_ingest_object($collection_pid=NULL, $collection_label = NULL, $content_model = NULL) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if (!user_access('ingest new fedora objects')) {
drupal_set_message(t('You do not have permission to ingest.'), 'error');
return '';
}
if (!valid_pid($collection_pid)) {
if (valid_pid(urldecode($collection_pid))) {
$collection_pid = urldecode($collection_pid);
}
else {
drupal_set_message(t("This collection PID @collection_pid is not valid", check_plain($collection_pid)), 'error');
return ' ';
}
}
if ($collection_pid == NULL) {
drupal_set_message(t('You must specify a collection object pid to ingest an object.'), 'error');
return '';
}
$output = drupal_get_form('fedora_repository_ingest_form', $collection_pid, $collection_label, $content_model);
$breadcrumbs = array();
$objectHelper = new ObjectHelper();
$objectHelper->getBreadcrumbs($collection_pid, $breadcrumbs);
drupal_set_breadcrumb(array_reverse($breadcrumbs));
return $output;
}
/**
* fedora repository ingest form submit
* @global type $base_url
* @global type $user
* @param array $form
* @param array $form_state
*/
function fedora_repository_ingest_form_submit(array $form, array &$form_state) {
//only validate the form if the submit button was pressed (other buttons may be used for AHAH
if ($form_state['ahah_submission']) {
$form_state['submitted'] = FALSE;
return;
}
//ddebug_backtrace();
if ($form_state['storage']['xml'] && module_exists('islandora_content_model_forms')) {
module_load_include('inc', 'islandora_content_model_forms', 'IngestObjectMetadataForm');
$xml_form = new IngestObjectMetadataForm();
$xml_form->submit($form, $form_state);
}
elseif (strpos($form_state['clicked_button']['#id'], 'edit-submit') === 0) {
global $base_url;
module_load_include('inc', 'fedora_repository', 'CollectionClass');
module_load_include('inc', 'fedora_repository', 'CollectionPolicy');
module_load_include('inc', 'fedora_repository', 'ContentModel');
$contentModelPid = ContentModel::getPidFromIdentifier($form_state['values']['models']);
$contentModelDsid = ContentModel::getDSIDFromIdentifier($form_state['values']['models']);
$err = TRUE;
$redirect = TRUE;
if (($cp = CollectionPolicy::loadFromCollection($form_state['values']['collection_pid'])) !== FALSE) {
$relationship = $cp->getRelationship();
if (($cm = ContentModel::loadFromModel($contentModelPid, $contentModelDsid)) !== FALSE) {
$pid = $cp->getNextPid($contentModelDsid);
global $user;
$form_state['values']['user_id'] = $user->name;
$form_state['values']['pid'] = $pid;
$form_state['values']['content_model_pid'] = $contentModelPid;
$form_state['values']['relationship'] = $relationship;
$err = (!$cm->execFormHandler($form_state['values'], $form_state));
$_SESSION['fedora_ingest_files'] = ''; //empty this variable
$attr = $cm->getIngestFormAttributes();
$redirect = $attr['redirect'];
if ($redirect) {
$form_state['storage'] = NULL;
}
}
}
if ($redirect) {
$form_state['redirect'] = ($err) ? ' ' : url("fedora/repository/{$form_state['values']['collection_pid']}");
}
}
}
/**
* fedora repository ingest form validate
* @param type $form
* @param type $form_state
* @return type
*/
function fedora_repository_ingest_form_validate($form, &$form_state) {
//only validate the form if the submit button was pressed (other buttons may be used for AHAH
if ($form_state['ahah_submission']) {
$form_state['submitted'] = FALSE;
return;
}
if (strpos($form_state['clicked_button']['#id'], 'edit-submit') === 0 && $form_state['ahah_submission'] != 1) {
switch ($form_state['storage']['step']) {
case 1:
$form_state['storage']['step']++;
$form_state['rebuild'] = TRUE;
break;
case 2:
// XML based form.
if ($form_state['storage']['xml']) {
module_load_include('inc', 'xml_form_api', 'XMLForm');
$xml_form = new XMLForm($form_state);
$xml_form->validate($form, $form_state);
}
// Get the uploaded file.
$validators = array();
if (!empty($_FILES['files']['name']['ingest-file-location'])) {
$fileObject = file_save_upload('ingest-file-location', $validators);
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
$form_state['values']['ingest-file-location'] = $fileObject->filepath;
}
if (isset($form_state['values']['ingest-file-location']) && file_exists($form_state['values']['ingest-file-location'])) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
module_load_include('inc', 'fedora_repository', 'MimeClass');
$file = $form_state['values']['ingest-file-location'];
$contentModelPid = ContentModel::getPidFromIdentifier($form_state['values']['models']);
$contentModelDsid = ContentModel::getDSIDFromIdentifier($form_state['values']['models']);
if (($cm = ContentModel::loadFromModel($contentModelPid, $contentModelDsid)) !== FALSE) {
$allowedMimeTypes = $cm->getMimetypes();
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file);
if (!empty($file)) {
if (!in_array($dformat, $allowedMimeTypes)) {
form_set_error('ingest-file-location',
t('The uploaded file\'s mimetype') .
' (' . $dformat . ') ' .
t('is not associated with this Content Model. The allowed types are') .
' ' . implode(' ', $allowedMimeTypes));
file_delete($file);
return;
}
elseif (!$cm->execIngestRules($file, $dformat)) {
drupal_set_message(t('Error following Content Model Rules'), 'error');
foreach (ContentModel::$errors as $err) {
drupal_set_message(check_plain($err), 'error');
}
}
}
}
}
$form_state['rebuild'] = FALSE;
break;
}
}
}
/**
* fedora repository ingest form
* @param type $form_state
* @param type $collection_pid
* @param type $collection_label
* @param type $content_model
* @return type
*/
function fedora_repository_ingest_form(&$form_state, $collection_pid, $collection_label = NULL, $content_model = NULL) {
module_load_include('inc', 'fedora_repository', 'formClass');
// For the sake of easily maintaining the module in different core versions create our own form_values variable.
if (empty($form_state['storage']['step'])) {
$form_state['storage']['step'] = 1;
}
$ingestForm = new formClass();
$form_state['storage']['content_model'] = $content_model;
$form_state['storage']['collection_pid'] = $collection_pid;
$form = $ingestForm->createIngestForm($collection_pid, $collection_label, $form_state);
return $form;
}
/**
* fedora repository purge object form
* @global type $base_url
* @param type $form_state
* @param type $pid
* @param type $referrer
* @return type
*/
function fedora_repository_purge_object_form(&$form_state, $pid, $referrer = NULL) {
global $base_url;
if (!user_access('purge objects and datastreams')) {
return NULL;
}
if ($pid == NULL) {
return NULL;
}
$form['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
);
if (!strstr(drupal_get_destination(), urlencode('fedora/repository'))) {
$form['referrer'] = array(
'#type' => 'hidden',
'#value' => $referrer,
);
}
if (!isset($form_state['storage']['confirm'])) {
// do your normal $form definition here
$form['submit'] = array(
'#type' => 'image_button',
'#src' => drupal_get_path('module', 'fedora_repository') . '/images/purge_big.png',
'#value' => t('Purge'),
'#suffix' => 'Purge this object',
);
if (!empty($collectionPid)) {
$collectionPid = $_SESSION['fedora_collection'];
}
//$form['#rebuild'] = $false;
return $form;
}
else {
// ALSO do $form definition here. Your final submit handler (after user clicks Yes, I Confirm) will only see $form_state info defined here. Form you create here passed as param1 to confirm_form
return confirm_form($form, 'Confirm Purge Object', $referrer, 'Are you sure you want to delete this object? This action cannot be undone.', 'Delete', 'Cancel'); //Had better luck leaving off last param 'name'
}
return $form;
}
/**
* add stream
* @param type $collection_pid
* @param type $collectionName
* @return type
*/
function add_stream($collection_pid=NULL, $collectionName=NULL) {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
if (!valid_pid($collection_pid)) {
drupal_set_message(t("This PID is not valid!"), 'error');
return ' ';
}
if (!user_access('ingest new fedora objects')) {
drupal_set_message(t('You do not have permission to ingest.'), 'error');
return '';
}
if ($collection_pid == NULL) {
drupal_set_message(t('You must specify an collection object pid to ingest an object.'), 'error');
return '';
}
$output .= drupal_get_form('fedora_repository_add_stream_form', $pid);
return $output;
}
/**
* add stream form submit
* @global type $base_url
* @param type $form
* @param type $form_state
* @return type
*/
function add_stream_form_submit($form, &$form_state) {
global $base_url;
if (!empty($form_state['submit']) && $form_state['submit'] == 'OK') {
$form_state['rebuild'] = TRUE;
return;
}
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pathToModule = drupal_get_path('module', 'fedora_repository');
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['stream_id'];
$dsLabel = $form_state['values']['stream_label'] . substr($form_state['storage']['stream_url'], strrpos($form_state['storage']['stream_url'], '.')); // Add the file extention to the end of the label.;
try {
$item = new Fedora_Item($pid);
$item->add_datastream_from_url($form_state['storage']['stream_url'], $dsid, $dsLabel, $form_state['storage']['ds_mimetype'], $form_state['storage']['control_group']);
if ($file = $form_state['values']['add-stream-file-location']) {
$object_helper = new ObjectHelper();
$object_helper->get_and_do_datastream_rules($pid, $dsid, $file);
file_delete($file);
}
} catch (exception $e) {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
return;
}
unset($form_state['storage']); //Using storage; need to unset it for forms to work properly...
$form_state['rebuild'] = TRUE;
}
/**
* add stream form
* @param type $form_state
* @param type $pid
* @return type
*/
function add_stream_form(&$form_state, $pid) {
module_load_include('inc', 'fedora_repository', 'formClass');
$addDataStreamForm = new formClass();
return $addDataStreamForm->createAddDataStreamForm($pid, $form_state);
}
/**
* add stream form validate
* @param type $form
* @param type $form_state
* @return type
*/
function add_stream_form_validate($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'MimeClass');
if ($form_state['clicked_button']['#value'] == 'OK') {
$form_state['rebuild'] = TRUE;
return;
}
$dsid = $form_state['values']['stream_id'];
$dsLabel = $form_state['values']['stream_label'];
if (strlen($dsid) > 64) {
form_set_error('', t('Data stream ID cannot be more than 64 characters.'));
return FALSE;
}
elseif (!(preg_match("/^[a-zA-Z]/", $dsid))) {
form_set_error('', t("Data stream ID (@dsid) has to start with a letter.", array('@dsid' => $dsid)));
return FALSE;
}
elseif (strlen($dsLabel) > 64) {
form_set_error('', t('Data stream Label cannot be more than 64 characters.'));
return FALSE;
}
elseif (strpos($dsLabel, '/') !== FALSE) {
form_set_error('', t('Data stream Label cannot contain a "/".'));
return FALSE;
}
$validators = array(
// 'file_validate_is_image' => array(),
// 'file_validate_image_resolution' => array('85x85'),
// 'file_validate_size' => array(30 * 1024),
);
$controlGroup = $form_state['storage']['control_group'] = $form_state['values']['control_group'];
if ((($controlGroup && in_array($controlGroup, array('X', 'M'))) || !$controlGroup) && (($fileObject = file_save_upload('add-stream-file-location', $validators)))) {
// Move the uploaded file to Drupal's files directory.
$file_path = $fileObject->filepath;
file_move($file_path, 0, FILE_EXISTS_RENAME);
$form_state['values']['add-stream-file-location'] = $file_path;
$form_state['storage']['stream_url'] = file_create_url($file_path);
}
elseif ($controlGroup && in_array($controlGroup, array('M', 'R', 'E')) && ($ref = $form_state['values']['ds_reference'])) {
$form_state['storage']['stream_url'] = $form_state['values']['ds_reference'];
}
else {
form_set_error('', t('No file given when "X" or "M", or no reference given when "M", "R" or "E".'));
}
$mimeClass = new MimeClass();
$mimetype = $form_state['storage']['ds_mimetype'] = $mimeClass->getType($form_state['storage']['stream_url']);
if (!$controlGroup) {
if ($mimetype == 'text/xml') {
$form_state['storage']['control_group'] = 'X';
}
else {
$form_state['storage']['control_group'] = 'M';
}
}
// TODO: Add error checking here.
$form_state['rebuild'] = FALSE;
}
/**
* fedora repository purge stream
* @global type $user
* @param type $pid
* @param type $dsId
* @param type $name
* @return type
*/
function fedora_repository_purge_stream($pid = NULL, $dsId = NULL, $name = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
global $user;
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t('You must specify an object pid and DataStream ID to purge a datastream'), 'error');
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER :: $PURGE_FEDORA_OBJECTSANDSTREAMS, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or you do not have permission to purge objects."), 'error');
return ' ';
}
$output = t('Are you sure you wish to purge this datastream %name
', array(
'%name' => $name)
);
$output .= drupal_get_form('fedora_repository_purge_stream_form', $pid, $dsId);
return $output;
}
function fedora_repository_purge_object_form_validate($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'api/fedora_collection');
$pid = $form_state['values']['pid'];
$objectHelper = new ObjectHelper();
$contentModels = $objectHelper->get_content_models_list($pid);
foreach ($contentModels as $contentModel) {
if ($contentModel->pid == 'islandora:collectionCModel') {
$member_pids = get_related_items_as_array($pid, 'isMemberOfCollection');
if (is_array($member_pids) && ! empty($member_pids)) {
form_set_error('new_collection_pid', t("Please purge all members of this collection before deleting the collection itself."));
return;
}
}
}
}
/**
* fedora repository purge object form submit
* @param type $form
* @param type $form_state
* @return type
*/
function fedora_repository_purge_object_form_submit($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$pid = $form_state['values']['pid'];
if (!isset($form_state['storage']['confirm'])) {
$form_state['storage']['confirm'] = TRUE; // this will cause the form to be rebuilt, entering the confirm part of the form
$form_state['rebuild'] = TRUE; // along with this
}
else {
// this is where you do your processing after they have pressed the confirm button
$params = array(
"pid" => $pid,
"logMessage" => "Purged",
"force" => ""
);
try {
$soapHelper = new ConnectionHelper();
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M'));
$object = $client->__soapCall('purgeObject', array($params));
unset($form_state['storage']['confirm']);
} catch (exception $e) {
if (preg_match('/org\.fcrepo\.server\.security\.xacml\.pep\.AuthzDeniedException/', $e->getMessage())) {
drupal_set_message(t('Error: Insufficient permissions to purge object.'), 'error');
}
else {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
}
return;
}
if (!empty($form_state['values']['referrer'])) {
$form_state['redirect'] = $form_state['values']['referrer'];
}
elseif (empty($collectionPid) && !empty($_SESSION['fedora_collection']) && $_SESSION['fedora_collection'] != $pid) {
$collectionPid = $_SESSION['fedora_collection'];
$form_state['redirect'] = "fedora/repository/$collectionPid/";
}
else {
$form_state['redirect'] = 'fedora/repository/';
}
}
}
/**
* fedora repository purge stream form
* @param type $form_state
* @param type $pid
* @param type $dsId
* @return type
*/
function fedora_repository_purge_stream_form(&$form_state, $pid, $dsId) {
$form['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
);
$form['dsid'] = array(
'#type' => 'hidden',
'#value' => "$dsId"
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Purge')
);
return $form;
}
/**
* fedora repository purge stream form submit
* @global type $base_url
* @param type $form
* @param array $form_state
*/
function fedora_repository_purge_stream_form_submit($form, &$form_state) {
global $base_url;
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pid = $form_state['values']['pid'];
$item = new Fedora_Item($pid);
$dsid = $form_state['values']['dsid'];
try {
$item->purge_datastream($dsid);
} catch (exception $e) {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
}
$form_state['redirect'] = "fedora/repository/$pid";
}
function fedora_repository_download_datastream_form(&$form_state, $pid, $dsid, $label) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$form = array(
'#action' => url("fedora/repository/object_download/$pid/$dsid/$label"),
'submit' => array(
'#type' => 'submit',
'#value' => t('Download'),
),
);
if (user_access(ObjectHelper::$EDIT_FEDORA_METADATA)) {
$item = new Fedora_Item($pid);
$versions = $item->get_datastream_history($dsid);
$version_array = array();
if (is_array($versions)) {
foreach ($versions as $version) {
$version_array[] = $version->createDate;
}
}
else {
$version_array[] = $versions->createDate;
}
if (count($version_array) > 1) {
$form['#attributes'] = array(
'onsubmit' => 'this.action="' . $form['#action'] . '/" + this.version.value;'
);
$form['version'] = array(
'#type' => 'select',
'#options' => array_combine($version_array, $version_array),
);
}
}
return $form;
}
/**
* fedora repository replace stream
* @param type $pid
* @param type $dsId
* @param type $dsLabel
* @param type $collectionName
* @return type
*/
function fedora_repository_replace_stream($pid, $dsId, $dsLabel = '', $collectionName = NULL) {
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t('You must specify an pid and dsId to replace.'), 'error');
return '';
}
$output = drupal_get_form('fedora_repository_replace_stream_form', $pid, $dsId, $dsLabel);
return $output;
}
/**
* fedora repository replace stream form
* @param type $form_state
* @param type $pid
* @param type $dsId
* @param type $dsLabel
* @return type
*/
function fedora_repository_replace_stream_form(&$form_state, $pid, $dsId, $dsLabel) {
module_load_include('inc', 'Fedora_Repository', 'formClass');
$replaceDataStreamForm = new formClass();
return $replaceDataStreamForm->createReplaceDataStreamForm($pid, $dsId, $dsLabel, $form_state);
}
/**
* fedora repository replace stream form validate
* @param type $form
* @param type $form_state
* @return type
*/
function fedora_repository_replace_stream_form_validate($form, &$form_state) {
// If a file was uploaded, process it.
if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name']['file'])) {
// attempt to save the uploaded file
$file = file_save_upload('file', array(), file_directory_path());
// set error is file was not uploaded
if (!$file) {
form_set_error('file', 'Error uploading file.');
return;
}
/* -----------------------------------------------------------------
* TODO: need a better way to get mimetypes
*/
module_load_include('inc', 'fedora_repository', 'MimeClass');
$mime = new MimeClass();
$mimetype = $form_state['storage']['mime_type'] = $mime->getType($file->filepath);
if ($mimetype == 'text/xml' && !DOMDocument::load($file->filepath)) {
form_set_error('file', 'Invalid XML format.');
return;
}
// set files to form_state, to process when form is submitted
$form_state['values']['file'] = $file;
}
elseif (!$form_state['values']['ds_reference']) {
form_set_error('', 'Need either a file or a reference!');
}
}
/**
* fedora repository replace stream form submit
* @global type $base_url
* @param type $form
* @param array $form_state
*/
function fedora_repository_replace_stream_form_submit($form, &$form_state) {
$file = $form_state['values']['file'];
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['dsId'];
$dsLabel = $form_state['values']['dsLabel'];
$streamUrl = ($file !== NULL) ?
file_create_url($file->filepath):
url($form_state['values']['reference'], array('absolute' => TRUE));
// Remove the original file extension from the label and add the new one
// use strrpos to get the last dot
if (($indexOfDot = strrpos($dsLabel, '.')) !== FALSE) {
$dsLabel = substr($dsLabel, 0, $indexOfDot) .
substr($streamUrl, strrpos($streamUrl, '.')); // Add the file extention to the end of the label.
}
$dformat = $form_state['storage']['mime_type'];
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
$item->modify_datastream($streamUrl, $dsid, $dsLabel, $dformat);
unset($form_state['storage']);
$form_state['redirect'] = 'fedora/repository/' . $pid;
}
/**
* fedora repository edit qdc page
* @global type $user
* @param type $pid
* @param type $dsId
* @return type
*/
function fedora_repository_edit_qdc_page($pid = NULL, $dsId = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
global $user;
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t('You must specify an object pid and a Dublin Core DataStream ID to edit metadata'), 'error');
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER :: $EDIT_FEDORA_METADATA, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or you do not have permission to edit meta data for this object."), 'error');
return ' ';
}
$output = drupal_get_form('fedora_repository_edit_qdc_form', $pid, $dsId);
return $output;
}
/**
* fedora repository edit qdc form
* @global type $user
* @param type $form_state
* @param type $pid
* @param type $dsId
* @return type
*/
function fedora_repository_edit_qdc_form(&$form_state, $pid, $dsId = NULL) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if ($pid == NULL) {
drupal_set_message(t('You must specify an object pid!'), 'error');
}
global $user;
if (!fedora_repository_access(OBJECTHELPER :: $EDIT_FEDORA_METADATA, $pid, $user)) {
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace or you do not have permission to edit meta data for this object."), 'error');
return ' ';
}
module_load_include('inc', 'fedora_repository', 'formClass');
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
$soapHelper = new ConnectionHelper();
$client = $soapHelper->getSoapClient(variable_get('fedora_soap_url', 'http://localhost:8080/fedora/services/access?wsdl'));
// Check if there is a custom edit metadata function defined in the content model.
$output = '';
if (($cm = ContentModel::loadFromObject($pid)) !== FALSE) {
$output = $cm->buildEditMetadataForm($pid, $dsId);
}
if (empty($output)) {
// There is no custom function, so just load the standard QDC form.
$metaDataForm = new formClass();
//currently we only edit the dc metadata. If you defined a custom form with a custom handler you are sol for now.
return $metaDataForm->createMetaDataForm($pid, $dsId, $client, $form_state);
}
return $output;
}
/**
* fedora repository edit qdc form validate
* @param type $form
* @param boolean $form_state
*/
function fedora_repository_edit_qdc_form_validate($form, &$form_state) {
if ($form_state['storage']['xml']) {
if ($form_state['storage']['step'] == 1) {
$form_state['storage']['step']++;
$form_state['rebuild'] = TRUE;
}
module_load_include('inc', 'xml_form_api', 'XMLForm');
$xml_form = new XMLForm($form_state);
$xml_form->validate($form, $form_state);
}
}
/**
* Check if there is a custom edit metadata function defined in the content model. If so,
* call it, if not do the submit action for the standard QDC metadata. Custom forms will
* need to implement their own equivalent to the FormClass->updateMetaData function
*
* @param array $form
* @param array $form_state
* @return
*/
function fedora_repository_edit_qdc_form_submit($form, &$form_state) {
if ($form_state['storage']['xml']) {
module_load_include('inc', 'islandora_content_model_forms', 'EditObjectMetadataForm');
$xml_form = new EditObjectMetadataForm();
$xml_form->submit($form, $form_state);
}
else {
module_load_include('inc', 'fedora_repository', 'ConnectionHelper');
global $base_url;
if (strstr($form_state['clicked_button']['#id'], 'edit-submit')) {
$soap_helper = new ConnectionHelper();
$client = $soap_helper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M'));
// Check the content model for a custom edit metadata form submit function.
if (isset($form_state['values']['pid'])) {
module_load_include('inc', 'fedora_repository', 'ContentModel');
if (($cm = ContentModel::loadFromObject($form_state['values']['pid'])) !== FALSE) {
return $cm->handleEditMetadataForm($form_state['values']['form_id'], $form_state, $client);
}
}
module_load_include('inc', 'fedora_repository', 'formClass');
$metaDataForm = new formClass();
$return_value = $metaDataForm->updateMetaData($form_state['values']['form_id'], $form_state['values'], $client);
$form_state['storage'] = NULL;
$form_state['redirect'] = $base_url . '/fedora/repository/' . $form_state['values']['pid'];
return $return_value;
}
}
}
/**
* drupal hook
* creates a new permission than can be assigned to roles
*/
function fedora_repository_perm() {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
return array(
OBJECTHELPER::$OBJECT_HELPER_VIEW_FEDORA,
OBJECTHELPER::$EDIT_FEDORA_METADATA,
OBJECTHELPER::$PURGE_FEDORA_OBJECTSANDSTREAMS,
OBJECTHELPER::$ADD_FEDORA_STREAMS,
OBJECTHELPER::$INGEST_FEDORA_OBJECTS,
OBJECTHELPER::$EDIT_TAGS_DATASTREAM,
OBJECTHELPER::$VIEW_DETAILED_CONTENT_LIST,
);
}
/**
* drupal hook
* determines if a user has access to what they are asking for
*
* @param type $op
* @param type $node
* @param type $account
* @return type
*/
function fedora_repository_access($op, $node = NULL, $account = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$objectHelper = new ObjectHelper();
return $objectHelper->fedora_repository_access($op, $node, $account);
}
/**
* Grabs a stream from fedora sets the mimetype and returns it. $dsID is the
* datastream id.
* @param $pid String
* @param $dsID String
*/
function makeObject($pid, $dsID) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
if (!valid_pid($pid)) {
drupal_set_message(t("Invalid PID!"), 'error');
return ' ';
}
if (!valid_dsid($dsID)) {
drupal_set_message(t("Invalid dsID!"), 'error');
return ' ';
}
if ($pid == NULL || $dsID == NULL) {
drupal_set_message(t("No pid or dsid given to create an object with."));
return ' ';
}
global $user;
if (!fedora_repository_access(OBJECTHELPER :: $OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
drupal_access_denied();
return;
drupal_set_message(t("You do not have access to Fedora objects within the attempted namespace."), 'error');
return ' ';
}
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
$objectHelper = new ObjectHelper();
$objectHelper->makeObject($pid, $dsID);
}
/**
* Implementation of hook_islandora_tabs().
*
* @param $content_models array
* An array of ContentModel objects to which the current Fedora Object
* subscribes.
* @param $pid string
* A string containing the Fedora PID of the current Fedora Object.
* @param $page_number integer
* An integer for which page we should start on in the loaded object.
* @return array
* An array containing a tabset (an array of tabpages), renderable with
* drupal_render().
*/
function fedora_repository_islandora_tabs($content_models, $pid, $page_number) {
$cmodels_tabs = array();
foreach ($content_models as $content_model) {
$content_model_fieldset = $content_model->displayExtraFieldset($pid, $page_number);
// Each content model may return either a tabpage array or plain HTML. If
// it is HTML, stick it in a tabpage.
if (is_array($content_model_fieldset)) {
$cmodels_tabs = array_merge($cmodels_tabs, $content_model_fieldset);
}
else {
$cmodels_tabs[$content_model->pid] = array(
'#type' => 'tabpage',
'#title' => $content_model->name,
'#content' => $content_model_fieldset,
);
}
}
// Add a 'manage object' tab for all objects, where detailed list of content is shown.
// XXX: Perhaps this should be extracted into its own object?
module_load_include('inc', 'fedora_repository', 'plugins/FedoraObjectDetailedContent');
$obj = new FedoraObjectDetailedContent($pid);
//can disable showing the object details tab in admin UI
if (variable_get('fedora_repository_show_object_details_tab', TRUE)) {
$object_details = $obj->showFieldSets();
$cmodel_tabs = array_merge($cmodels_tabs, $object_details);
}
return array_merge($cmodels_tabs, $object_details);
}
/**
* Implementation of hook_islandora_tabs_alter().
*
* @param &$tabs array
* The array of tabs/tabset to alter.
* @param $params array
* An associative array containing the parameters with which the original
* hook was called.
* @see fedora_repository_get_items()
*/
function fedora_repository_islandora_tabs_alter(&$tabs, $params) {
//Deselect all other tabs if the fedora_object_details tab is supposed
// to be selected.
$object_details_key = 'fedora_object_details';
if ($tabs[$object_details_key]['#selected']) {
foreach (element_children($tabs) as $key) {
if ($key != $object_details_key) {
$tabs[$key]['#selected'] = FALSE;
}
}
}
}
/**
* Menu callback for "fedora/repository".
*
* If user is allow, and we are given a PID and a sensical DSID, return the
* datastream via the makeObject() function; otherwise, call out to the PIDs'
* ContentModels and all Drupal modules for Islandora tabs.
*
* @global $user stdObject
* @param $pid string
* An optional string containing the PID of an object. (defaults to islandora:root)
* @param $dsId string
* An optional string containing the dsid of an object. ("-" will be ignored
* to allow later parameters without including one).
* @param $collection string
* The collection name... Deprecated.
* @param $page_number string/integer(?)
* A page number to start on... Seems to be going towards deprecation?
* @param $limit string/integer(?)
* Used to limit the number of results returned? Deprecated?
* @return string
* A string containing markup for the rendered tabs.
*/
function fedora_repository_get_items($pid = NULL, $dsId = NULL, $collection = NULL, $page_number = NULL, $limit = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
global $user;
if (!fedora_available()) {
drupal_set_message(t('The Fedora repository server is currently unavailable. Please contact the site administrator.'), 'warning', FALSE);
return '';
}
if (!risearch_available()) {
drupal_set_message(t('The Fedora resource index search is currently unavailable. Please contact the site administrator.'), 'warning', FALSE);
return '';
}
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:root');
}
$item = new Fedora_Item($pid);
if (!$item->exists()) {
drupal_not_found();
exit();
}
if ($pid & !valid_pid($pid)) {
drupal_set_message(t("Invalid PID!"), 'error');
return ' ';
}
if ($dsId & !valid_dsid($dsId)) {
drupal_set_message(t("Invalid dsID!"), 'error');
return ' ';
}
if (!fedora_repository_access(OBJECTHELPER::$OBJECT_HELPER_VIEW_FEDORA, $pid, $user)) {
if (user_access('access administration pages')) {
drupal_set_message(t("PIDs may be added to allowed namespaces, or all namespace restrictions removed @here", array('@here' => l('here', 'admin/settings/fedora_repository'))), 'warning');
}
drupal_access_denied();
exit;
return ' ';
}
$objectHelper = new ObjectHelper();
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:root');
}
$headers = module_invoke_all('file_download', "/fedora/repository/$pid");
if (in_array(-1, $headers)) {
drupal_access_denied();
exit;
return ' ';
}
if ($dsId != NULL && $dsId != '-') { //if we have a dsID return the stream otherwise query for a collection of objects
//probably should check pid as well here.
return makeObject($pid, $dsId);
}
$breadcrumbs = array();
$objectHelper->getBreadcrumbs($pid, $breadcrumbs);
drupal_set_breadcrumb(array_reverse($breadcrumbs));
$content_models = $objectHelper->get_content_models_list($pid);
//Get the tabs from all modules...
$hook_tabs = module_invoke_all('islandora_tabs', $content_models, $pid, $page_number);
$cmodels_tabs = array(
'#type' => 'tabset',
);
$cmodels_tabs += $hook_tabs;
//Assemble parameters, to pass during alter
$params = array(
'content_models' => $content_models,
'pid' => $pid,
'page' => $page_number,
);
//Allow returned tabs to be altered, before return.
drupal_alter('islandora_tabs', $cmodels_tabs, $params);
return tabs_render($cmodels_tabs);
}
/**
* fedora repository urlencode string
* FIXME: URL-encoding is not the same as HTML/XML encoding...
* @param type $str
* @return type
*/
function fedora_repository_urlencode_string($str) {
return htmlentities($str);
}
/**
* Uses makeobject to get a stream. Sets the Content Disposition in the header so it suggests a filename
* and sends it as an attachment. This should prompt for a download of the object.
*
* @global type $user
* @param type $pid
* @param type $dsId
* @param type $label
* @param type $version
* @return type
*/
function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if ($pid == NULL || $dsId == NULL) {
drupal_set_message(t("no pid or dsid given to create an object with!"));
return ' ';
}
$objectHelper = new ObjectHelper();
$objectHelper->makeObject($pid, $dsId, TRUE, $label, FALSE, $version);
}
/**
* repository page
* @param type $pid
* @param type $dsId
* @param type $collection
* @param type $pageNumber
* @return type
*/
function repository_page($pid = NULL, $dsId = NULL, $collection = NULL, $pageNumber = NULL) {
//do security check at fedora_repository_get_items function as it has to be called there in case
//someone trys to come in a back door.
return fedora_repository_get_items($pid, $dsId, $collection, $pageNumber);
}
/**
* repository service
* @global type $user
* @param type $pid
* @param type $servicePid
* @param type $serviceMethod
* @return type
*/
function repository_service($pid = NULL, $servicePid = NULL, $serviceMethod = NULL) {
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
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_access_denied();
if (user_access('access administration pages')) {
drupal_set_message(t("PIDs may be added to allowed namespaces, or all namespace restrictions removed @here", array('@here' => l('here', 'admin/settings/fedora_repository'))), 'error');
}
return ' ';
}
if ($pid == NULL) {
$pid = variable_get('fedora_repository_pid', 'islandora:root');
}
$headers = module_invoke_all('file_download', "/fedora/repository/$pid");
if (in_array(-1, $headers)) {
drupal_access_denied();
exit;
return ' ';
}
$item = new Fedora_Item($pid);
if ($item !== FALSE) {
echo $item->get_dissemination($servicePid, $serviceMethod);
}
exit();
}
//Search Stuff ********************************************************************
/**
* Implementation of hook_search().
* sends a search query to fedora fgsearch which is backed by Lucene
* In our implementation of Fedora we have api-a and api-m locked down
* to authorized users but at the object level. We can query Lucene and the
* RI index to get a list of results without authorization but to view any
* datastreams users must be authorized.
*
* @param type $op
* @param type $keys
* @return array
*/
function fedora_repository_search($op = 'search', $keys = NULL) {
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
switch ($op) {
case 'name':
if (user_access('view fedora collection')) {
return t('Digital Repository', array('-9'));
}
case 'search':
if (user_access('view fedora collection')) {
//demo search string ?operation=gfindObjects&indexName=DemoOnLucene&query=fgs.DS.first.text%3Achristmas&hitPageStart=11&hitPageSize=10
$resultData = NULL;
$numberOfHitsPerPage = NULL;
$index = strpos($keys, '.');
$test = substr($keys, 0, $index + 1);
$type = NULL;
if ($index > 0) {
$index = strpos($keys, ':');
$type = substr($keys, 0, $index);
$keys = substr($keys, $index + 1);
}
$index = strpos($keys, ':');
$startPage = substr($keys, 0, $index);
if ($index > 1) {
$keys = substr($keys, $index + 1);
}
if (!$startPage) {
$startPage = 1;
}
$xmlDoc = NULL;
$path = drupal_get_path('module', 'fedora_repository');
$xmlDoc = new DomDocument();
$xmlDoc->load($path . '/searchTerms.xml');
$nodeList = $xmlDoc->getElementsByTagName('default');
if (!$type) {
//$type = 'dc.description';
$type = $nodeList->item(0)->nodeValue;
}
$nodeList = $xmlDoc->getElementsByTagName('number_of_results');
$numberOfHitsPerPage = $nodeList->item(0)->nodeValue;
$indexName = variable_get('fedora_index_name', 'DemoOnLucene');
$keys = htmlentities(urlencode($keys));
$searchQuery = NULL;
if (isset($type) && strcmp($type, ':')) {
$searchQuery = $type . ':' . $keys;
}
else {
$searchQuery = $keys;
}
//$searchQuery.=" AND (PID:vre OR PID:vre:ref OR PID:demo OR PID:changeme)";
$searchUrl = variable_get('fedora_fgsearch_url', 'http://localhost:8080/fedoragsearch/rest');
$searchString = '?operation=gfindObjects&indexName=' . $indexName . '&restXslt=copyXml&query=' . $searchQuery;
$searchString .= '&hitPageSize=' . $numberOfHitsPerPage . '&hitPageStart=' . $startPage;
//$searchString = htmlentities(urlencode($searchString));
$searchUrl .= $searchString;
$objectHelper = new ObjectHelper();
$resultData = do_curl($searchUrl);
$results[] = array(
array(
'data' => $resultData,
'colspan' => 2
)
);
return $results;
}
} // switch ($op)
}
/**
* Implementation of hook_search_page().
* Display the search results
*
* @param type $resultData
* @return array
*/
function fedora_repository_search_page($resultData) {
$path = drupal_get_path('module', 'fedora_repository');
$isRestricted = variable_get('fedora_namespace_restriction_enforced', TRUE);
$proc = NULL;
if (!$resultData[0][0]['data']) {
return ''; //no results
}
$text = utf8_encode($resultData[0][0]['data']);
try {
$proc = new XsltProcessor();
} catch (Exception $e) {
$out[] = array(
array(
'data' => $e->getMessage(),
'colspan' => 2
)
);
return $out;
}
//inject into xsl stylesheet
$proc->setParameter('', 'searchToken', drupal_get_token('search_form')); //token generated by Drupal, keeps tack of what tab etc we are on
$proc->setParameter('', 'searchUrl', url('search') . '/fedora_repository'); //needed in our xsl
$proc->setParameter('', 'objectsPage', base_path());
$proc->setParameter('', 'allowedPidNameSpaces', variable_get('fedora_pids_allowed', 'default: demo: changeme: islandora: ilives: islandora-book: books: newspapers: '));
$proc->registerPHPFunctions();
$xsl = new DomDocument();
if ($isRestricted) {
$xsl->load($path . '/xsl/results.xsl');
}
else {
$xsl->load($path . '/xsl/unfilteredresults.xsl');
}
$input = new DomDocument();
$didLoadOk = $input->loadXML(utf8_encode($resultData[0][0]['data']));
if (!$didLoadOk) {
$results[] = array(
array(
'data' => 'Error parsing results',
'colspan' => 2
)
);
}
else {
$xsl = $proc->importStylesheet($xsl);
$newdom = $proc->transformToDoc($input);
$results[] = array(
array(
'data' => $newdom->saveHTML(),
'colspan' => 2
)
);
}
$header = array(
array(
'data' => t('Collection results'),
NULL,
),
(NULL)
);
$output .= theme('table', $header, $results);
return $output;
}
/**
* Implementation of hook_form_alter().
* allows the advanced search form in drupal
*
* @param type $form
* @param type $form_state
* @param type $form_id
*/
function fedora_repository_form_alter(&$form, &$form_state, $form_id) {
// Advanced node search form
module_load_include('inc', 'fedora_repository', 'SearchClass');
$path = drupal_get_path('module', 'fedora_repository');
if ($form_id == 'search_form' && arg(1) == 'fedora_repository' && user_access('use advanced search')) {
$default_value = $form['basic']['inline']['keys']['#default_value'];
$index = strpos($default_value, '.');
$test = substr($default_value, 0, $index + 1);
$type = NULL;
if ($index > 0) {
$index = strpos($default_value, ':');
$type = substr($default_value, 0, $index);
$default_value = substr($default_value, $index + 1);
}
$form['basic']['inline']['keys']['#default_value'] = $default_value;
// Keyword boxes:
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced search'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array(
'class' => 'search-advanced'
),
);
$form['advanced']['keywords'] = array(
'#prefix' => '
If you install the ' . l('fedora demo objects', 'https://wiki.duraspace.org/display/FCR30/Demonstration+Objects') . ' Islandora can display them as a collection.
' ); } $form['install_demos']['ingest'] = array( '#type' => 'submit', '#name' => 'install_demos', '#value' => 'Install Selected Demos', '#disabled' => (empty($form['install_demos']['demo_collections']['#options'])) ? TRUE : FALSE, ); $form['existing_demos'] = array( '#prefix' => 'Demo collections already installed in this repository: