@ -377,37 +377,25 @@ function add_stream_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['rebuild'] = TRUE;
return;
return;
}
}
module_load_include('inc', 'fedora_repository', 'MimeClass');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pathToModule = drupal_get_path('module', 'fedora_repository');
$pathToModule = drupal_get_path('module', 'fedora_repository');
$file = $form_state['values']['add-stream-file-location'];
$pid = $form_state['values']['pid'];
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['stream_id'];
$dsid = $form_state['values']['stream_id'];
$dsLabel = $form_state['values']['stream_label'] . substr($file, strrpos($file, '.')); // Add the file extention to the end of the label.;
$dsLabel = $form_state['values']['stream_label'] . substr($file, strrpos($file, '.')); // Add the file extention to the end of the label.;
$file_basename = basename($file);
$file_directory = dirname($file);
$streamUrl = $base_url . '/' . $file_directory . '/' . drupal_urlencode($file_basename);
/* -----------------------------------------------------------------
* need a better way to get mimetypes
*/
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file);
$controlGroup = "M";
if ($dformat == 'text/xml') {
$controlGroup = 'X';
}
try {
try {
$item = new Fedora_Item($pid);
$item = new Fedora_Item($pid);
$item->add_datastream_from_url($streamUrl, $dsid, $dsLabel, $dformat, $controlGroup );
$item->add_datastream_from_url($form_state['storage']['stream_url'], $dsid, $dsLabel, $form_state['storage']['ds_mimetype'], $form_state['storage']['control_group']);
$object_helper = new ObjectHelper();
if ($file = $form_state['values']['add-stream-file-location']) {
$object_helper->get_and_do_datastream_rules($pid, $dsid, $file);
$object_helper = new ObjectHelper();
$object_helper->get_and_do_datastream_rules($pid, $dsid, $file);
file_delete($file);
file_delete($file);
}
} catch (exception $e) {
} catch (exception $e) {
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error');
return;
return;
@ -434,6 +422,7 @@ function add_stream_form(&$form_state, $pid) {
* @return type
* @return type
*/
*/
function add_stream_form_validate($form, &$form_state) {
function add_stream_form_validate($form, &$form_state) {
module_load_include('inc', 'fedora_repository', 'MimeClass');
if ($form_state['clicked_button']['#value'] == 'OK') {
if ($form_state['clicked_button']['#value'] == 'OK') {
$form_state['rebuild'] = TRUE;
$form_state['rebuild'] = TRUE;
return;
return;
@ -444,15 +433,15 @@ function add_stream_form_validate($form, &$form_state) {
form_set_error('', t('Data stream ID cannot be more than 64 characters.'));
form_set_error('', t('Data stream ID cannot be more than 64 characters.'));
return FALSE;
return FALSE;
}
}
if (!(preg_match("/^[a-zA-Z]/", $dsid))) {
else if (!(preg_match("/^[a-zA-Z]/", $dsid))) {
form_set_error('', t("Data stream ID (@dsid) has to start with a letter.", array('@dsid' => check_plain( $dsid) )));
form_set_error('', t("Data stream ID (@dsid) has to start with a letter.", array('@dsid' => $dsid)));
return FALSE;
return FALSE;
}
}
if (strlen($dsLabel) > 64) {
else if (strlen($dsLabel) > 64) {
form_set_error('', t('Data stream Label cannot be more than 64 characters.'));
form_set_error('', t('Data stream Label cannot be more than 64 characters.'));
return FALSE;
return FALSE;
}
}
if (strpos($dsLabel, '/')) {
else if (strpos($dsLabel, '/') !== FALSE ) {
form_set_error('', t('Data stream Label cannot contain a "/".'));
form_set_error('', t('Data stream Label cannot contain a "/".'));
return FALSE;
return FALSE;
}
}
@ -461,13 +450,40 @@ function add_stream_form_validate($form, &$form_state) {
// 'file_validate_image_resolution' => array('85x85'),
// 'file_validate_image_resolution' => array('85x85'),
// 'file_validate_size' => array(30 * 1024),
// 'file_validate_size' => array(30 * 1024),
);
);
$fileObject = file_save_upload('add-stream-file-location', $validators);
$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)) !== 0)) {
// Move the uploaded file to Drupal's files directory.
// Move the uploaded file to Drupal's files directory.
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
$form_state['values']['add-stream-file-location'] = $fileObject->filepath;
$form_state['values']['add-stream-file-location'] = $fileObject->filepath;
// TODO: Add error checking here.
$file_basename = basename($fileObject->filepath);
$file_directory = dirname($fileObject->filepath);
$form_state['storage']['stream_url'] = url($file_directory . '/' . drupal_urlencode($file_basename), array(
'absolute' => TRUE,
));
}
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;
$form_state['rebuild'] = FALSE;
}
}
@ -607,6 +623,42 @@ function fedora_repository_purge_stream_form_submit($form, &$form_state) {
$form_state['redirect'] = $base_url . "/fedora/repository/$pid";
$form_state['redirect'] = $base_url . "/fedora/repository/$pid";
}
}
function fedora_repository_download_datastream_form(&$form_state, $pid, $dsid, $label) {
$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
* fedora repository replace stream
* @param type $pid
* @param type $pid
@ -646,27 +698,31 @@ function fedora_repository_replace_stream_form(&$form_state, $pid, $dsId, $dsLab
* @return type
* @return type
*/
*/
function fedora_repository_replace_stream_form_validate($form, &$form_state) {
function fedora_repository_replace_stream_form_validate($form, &$form_state) {
// If a file was uploaded, process it.
// If a file was uploaded, process it.
if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name']['file'])) {
if (isset($_FILES['files']) && is_uploaded_file($_FILES['files']['tmp_name']['file'])) {
// attempt to save the uploaded file
// attempt to save the uploaded file
$file = file_save_upload('file', array(), file_directory_path());
$file = file_save_upload('file', array(), file_directory_path());
// set error is file was not uploaded
// set error is file was not uploaded
if (!$file) {
if (!$file) {
form_set_error('file', 'Error uploading file.');
form_set_error('file', 'Error uploading file.');
return;
return;
}
}
$doc = new DOMDocument();
/* -----------------------------------------------------------------
module_load_include('inc', 'Fedora_Repository', 'MimeClass');
* TODO: need a better way to get mimetypes
*/
module_load_include('inc', 'fedora_repository', 'MimeClass');
$mime = new MimeClass();
$mime = new MimeClass();
if ($mime->getType($file->filepath) == 'text/xml' && !$doc->load($file->filepath)) {
$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.');
form_set_error('file', 'Invalid XML format.');
return;
return;
}
}
// set files to form_state, to process when form is submitted
// set files to form_state, to process when form is submitted
$form_state['values']['file'] = $file;
$form_state['values']['file'] = $file;
}
}
}
}
@ -678,45 +734,28 @@ function fedora_repository_replace_stream_form_validate($form, &$form_state) {
* @param array $form_state
* @param array $form_state
*/
*/
function fedora_repository_replace_stream_form_submit($form, &$form_state) {
function fedora_repository_replace_stream_form_submit($form, &$form_state) {
global $base_url;
$file = $form_state['values']['file'];
$file = $form_state['values']['file'];
$pid = $form_state['values']['pid'];
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['dsId'];
$dsid = $form_state['values']['dsId'];
$dsLabel = $form_state['values']['dsLabel'];
$dsLabel = $form_state['values']['dsLabel'];
// Remove the original file extension from the label and add the new one
$indexOfDot = strrpos($dsLabel, '.'); //use strrpos to get the last dot
$streamUrl = ($file !== NULL) ?
if ($indexOfDot !== FALSE) {
$file->filepath:
$dsLabel = substr($dsLabel, 0, $indexOfDot);
url($form_state['values']['reference'], array('absolute' => TRUE));
$dsLabel .= substr($file->filename, strrpos($file->filename, '.')); // Add the file extention to the end of the label.;
// 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.
}
}
module_load_include('inc', 'Fedora_Repository', 'MimeClass');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$file_basename = basename($file->filepath);
$dformat = $form_state['storage']['mime_type'];
$file_directory = dirname($file->filepath);
$streamUrl = $base_url . '/' . $file_directory . '/' . urlencode($file_basename);
/* -----------------------------------------------------------------
* TODO: need a better way to get mimetypes
*/
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file->filepath);
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
$item = new Fedora_Item($pid);
$info = $item->get_datastream_info($dsid);
$item->modify_datastream($streamUrl, $dsid, $dsLabel, $dformat);
if($info->datastream->controlGroup == 'M') {
$item->modify_datastream_by_reference($streamUrl, $dsid, $dsLabel, $dformat);
} elseif ($info->datastream->controlGroup == 'X') {
if($dformat == 'text/xml') {
$item->modify_datastream_by_value(file_get_contents($file->filepath), $dsid, $dsLabel, $dformat);
}
else {
drupal_set_message('File must be of mimetype text/xml in order to replace inline XML datastream.', 'error');
}
} else {
drupal_set_message('Cannot replace Redirect or Managed Datastream.', 'error');
}
$form_state['redirect'] = 'fedora/repository/' . $pid;
$form_state['redirect'] = 'fedora/repository/' . $pid;
}
}
@ -912,17 +951,97 @@ function makeObject($pid, $dsID) {
}
}
/**
/**
* Sends an ITQL query to the Fedora Resource index (can only communicate with Kowari or mulgara)
* Implementation of hook_islandora_tabs().
* Reads the pid and datastream id as url parameters. Queries the collection object for the query
*
* if there is no query datastream falls back to the query shipped with the module.
* @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 type $user
* @global $user stdObject
* @param type $pid
* @param $pid string
* @param type $dsId
* An optional string containing the PID of an object. (defaults to islandora:root)
* @param type $collection
* @param $dsId string
* @param type $page_number
* An optional string containing the dsid of an object. ("-" will be ignored
* @param type $limit
* to allow later parameters without including one).
* @return type
* @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) {
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', 'ObjectHelper');
@ -944,7 +1063,7 @@ function fedora_repository_get_items($pid = NULL, $dsId = NULL, $collection = NU
$pid = variable_get('fedora_repository_pid', 'islandora:root');
$pid = variable_get('fedora_repository_pid', 'islandora:root');
}
}
$item = new fedora_i tem($pid);
$item = new Fedora_I tem($pid);
if (!$item->exists()) {
if (!$item->exists()) {
drupal_not_found();
drupal_not_found();
exit();
exit();
@ -983,57 +1102,30 @@ function fedora_repository_get_items($pid = NULL, $dsId = NULL, $collection = NU
return makeObject($pid, $dsId);
return makeObject($pid, $dsId);
}
}
$content = '<div id="content-fedora">';
module_load_include('inc', 'fedora_repository', 'CollectionClass');
$collectionClass = new CollectionClass();
module_load_include('inc', 'fedora_repository', 'ContentModel');
module_load_include('inc', 'fedora_repository', 'plugins/FedoraObjectDetailedContent');
$breadcrumbs = array();
$breadcrumbs = array();
$objectHelper->getBreadcrumbs($pid, $breadcrumbs);
$objectHelper->getBreadcrumbs($pid, $breadcrumbs);
drupal_set_breadcrumb(array_reverse($breadcrumbs));
drupal_set_breadcrumb(array_reverse($breadcrumbs));
$offset = $limit * $page_number;
$content_models = $objectHelper->get_content_models_list($pid);
$content_models = $objectHelper->get_content_models_list($pid);
// Each content model may return either a tabset array or plain HTML. If it's HTML, stick it in a tab.
$cmodels_tabs = array(
//Get the tabs from all modules...
'#type' => 'tabset',
$hook_tabs = module_invoke_all('islandora_tabs', $content_models, $pid, $page_number);
$cmodels_tabs = array(
'#type' => 'tabset',
);
);
foreach ($content_models as $content_model) {
$cmodels_tabs += $hook_tabs;
$content_model_fieldset = $content_model->displayExtraFieldset($pid, $page_number);
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.
//Assemble parameters, to pass during alter
$obj = new FedoraObjectDetailedContent($pid);
$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);
//can disable showing the object details tab in admin UI
if (variable_get('fedora_repository_show_object_details_tab', TRUE)) {
$object_details = $obj->showFieldSets();
if ($object_details['fedora_object_details']['#selected'] == TRUE) {
foreach ($cmodels_tabs as &$cmodel_tab) {
if (is_array($cmodel_tab)) {
$cmodel_tab['#selected'] = FALSE;
}
}
}
}
else {
$object_details = array();
}
$hook_tabs = module_invoke_all('islandora_tabs', $content_models, $pid);
$cmodels_tabs = array_merge($cmodels_tabs, $object_details, $hook_tabs);
return tabs_render($cmodels_tabs);
return tabs_render($cmodels_tabs);
}
}
@ -1059,7 +1151,6 @@ function fedora_repository_urlencode_string($str) {
* @return type
* @return type
*/
*/
function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
global $user;
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if ($pid == NULL || $dsId == NULL) {
if ($pid == NULL || $dsId == NULL) {
@ -1068,7 +1159,7 @@ function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
}
}
$objectHelper = new ObjectHelper();
$objectHelper = new ObjectHelper();
$objectHelper->makeObject($pid, $dsId, 1 , $label, FALSE, $version);
$objectHelper->makeObject($pid, $dsId, TRUE , $label, FALSE, $version);
}
}
/**
/**
@ -2273,9 +2364,6 @@ function theme_fedora_repository_solution_packs_list($solution_packs) {
$header = array();
$header = array();
$rows = array();
$rows = array();
drupal_add_css(drupal_get_path('module', 'update') . '/update.css');
drupal_add_css(drupal_get_path('module', 'update') . '/update.css');
return $output;
return $output;
}
}