Browse Source

Allow the addition of Redirect and External datastreams.

pull/101/head
Adam Vessey 13 years ago
parent
commit
01ac63a3d9
  1. 64
      fedora_repository.module
  2. 73
      formClass.inc

64
fedora_repository.module

@ -377,37 +377,25 @@ function add_stream_form_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
return;
}
module_load_include('inc', 'fedora_repository', 'MimeClass');
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$pathToModule = drupal_get_path('module', 'fedora_repository');
$file = $form_state['values']['add-stream-file-location'];
$pid = $form_state['values']['pid'];
$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.;
$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 {
$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['values']['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;
@ -434,6 +422,7 @@ function add_stream_form(&$form_state, $pid) {
* @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;
@ -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.'));
return FALSE;
}
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))));
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;
}
if (strlen($dsLabel) > 64) {
elseif (strlen($dsLabel) > 64) {
form_set_error('', t('Data stream Label cannot be more than 64 characters.'));
return FALSE;
}
if (strpos($dsLabel, '/')) {
elseif (strpos($dsLabel, '/') !== FALSE) {
form_set_error('', t('Data stream Label cannot contain a "/".'));
return FALSE;
}
@ -462,11 +451,29 @@ function add_stream_form_validate($form, &$form_state) {
// 'file_validate_size' => array(30 * 1024),
);
$fileObject = file_save_upload('add-stream-file-location', $validators);
$mimetype = new MimeClass();
$controlGroup = $form_state['values']['control_group'];
if (in_array($controlGroup, array('X', 'M')) && ($fileObject = file_save_upload('add-stream-file-location', $validators)) !== 0) {
// Move the uploaded file to Drupal's files directory.
file_move($fileObject->filepath, 0, 'FILE_EXISTS_RENAME');
$form_state['values']['add-stream-file-location'] = $fileObject->filepath;
$form_state['storage']['ds_mimetype'] = $mimetype->getType($fileObject->filepath);
$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 (in_array($controlGroup, array('M', 'R', 'E')) && ($ref = $form_state['values']['ds_reference'])) {
$form_state['storage']['ds_mimetype'] = $mimetype->getType($ref);
$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".'));
}
// TODO: Add error checking here.
$form_state['rebuild'] = FALSE;
}
@ -680,6 +687,11 @@ function fedora_repository_replace_stream_form_validate($form, &$form_state) {
function fedora_repository_replace_stream_form_submit($form, &$form_state) {
global $base_url;
$file = $form_state['values']['file'];
if ($file !== NULL) {
$file = $form_state['values']['reference'];
}
$pid = $form_state['values']['pid'];
$dsid = $form_state['values']['dsId'];
$dsLabel = $form_state['values']['dsLabel'];
@ -702,14 +714,9 @@ function fedora_repository_replace_stream_form_submit($form, &$form_state) {
$mimetype = new MimeClass();
$dformat = $mimetype->getType($file->filepath);
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
if(in_array($info->datastream->controlGroup, array('M', 'X'))) {
$item->modify_datastream($streamUrl, $dsid, $dsLabel, $dformat);
} else {
drupal_set_message(t('Cannot replace Redirect or Managed Datastream.'), 'error');
}
$form_state['redirect'] = 'fedora/repository/' . $pid;
}
@ -1052,7 +1059,6 @@ function fedora_repository_urlencode_string($str) {
* @return type
*/
function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
global $user;
module_load_include('inc', 'fedora_repository', 'ObjectHelper');
if ($pid == NULL || $dsId == NULL) {
@ -1061,7 +1067,7 @@ function fedora_object_as_attachment($pid, $dsId, $label=NULL, $version=NULL) {
}
$objectHelper = new ObjectHelper();
$objectHelper->makeObject($pid, $dsId, 1, $label, FALSE, $version);
$objectHelper->makeObject($pid, $dsId, TRUE, $label, FALSE, $version);
}
/**

73
formClass.inc

@ -656,17 +656,21 @@ class formClass {
}
}
$form['add_datastream_label'] = array(
'#value' => t('<br /><h3>Add Datastream:</h3>'),
'#weight' => -10,
$form['fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Add datastream'),
);
//$form['add_datastream_label'] = array(
// '#value' => t('<br /><h3>Add Datastream:</h3>'),
// '#weight' => -10,
//);
$form['pid'] = array(
$form['fieldset']['pid'] = array(
'#type' => 'hidden',
'#value' => "$pid"
);
$form['stream_label'] = array(
$form['fieldset']['stream_label'] = array(
'#title' => 'Datastream Label',
'#required' => 'TRUE',
'#description' => t('A Human readable label'),
@ -674,35 +678,37 @@ class formClass {
);
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['add-stream-file-location'] = array(
$form['fieldset']['add-stream-file-location'] = array(
'#type' => 'file',
'#title' => t('Upload Document'),
'#size' => 48,
// '#required'=>'TRUE',
'#description' => t('The file to upload.')
'#description' => t('The file to upload. (Only for Managed and Inline)')
);
$form['fieldset']['ds_reference'] = array(
'#type' => 'textfield',
'#title' => t('Datastream reference'),
'#size' => 48,
'#description' => t('A URL reference to resolve for the contents of the datastream. (Required for External and Redirect, but will still work for Managed and Inline.)'),
);
$form['#redirect'] = "fedora/repository/$pid/";
$form['submit'] = array(
$form['fieldset']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add Datastream')
);
if (!empty($unused_dsids)) {
$dsidsForForm = array();
foreach ($unused_dsids as $dsid) {
$dsidsForForm[$dsid] = $dsid;
}
$form['stream_id'] = array(
$form['fieldset']['stream_id'] = array(
'#type' => 'select',
'#title' => t('Datastream ID'),
'#default_value' => variable_get('feed_item_length', 'teaser'),
'#weight' => '-1',
'#weight' => -1,
'#description' => t('Datastream IDs defined by the content model.'),
'#options' => array_combine($unused_dsids, $unused_dsids),
);
$form['stream_id']['#options'] = array_combine($unused_dsids, $unused_dsids);
}
else {
$form['stream_id'] = array(
$form['fieldset']['stream_id'] = array(
'#title' => 'Datastream ID',
'#required' => 'TRUE',
'#description' => t('An ID for this stream that is unique to this object. Must start with a letter and contain only alphanumeric characters and dashes and underscores.'),
@ -710,6 +716,18 @@ class formClass {
'#weight' => -1,
);
}
$form['fieldset']['control_group'] = array(
'#type' => 'select',
'#title' => t('Control group'),
'#options' => array(
'X' => t('Inline XML'),
'M' => t('Managed datastream'),
'E' => t('Externally Referenced/managed datastream'),
'R' => t('Redirect datastream'),
),
'#description' => t('The manner in which the datastream will be stored. "Inline XML" must be XML and will be placed directly into the FOXML for the object. "Managed" datastreams are made to live on the filesystem as discrete files in the Fedora data directory. Both "Redirect" and "External" streams are URL references; the difference being the redirect stream instruct clients to perform an HTTP redirect, such that the data does not pass though Fedora (useful for streaming). External streams are mediated (by which I mean loaded and streamed from) the Fedora server.'),
'#weight' => 0,
);
return $form;
}
@ -836,11 +854,32 @@ class formClass {
$form = array();
$form['#attributes']['enctype'] = 'multipart/form-data';
module_load_include('inc', 'fedora_repository', 'api/fedora_item');
$item = new Fedora_Item($pid);
$info = $item->get_datastream_info($dsId);
$control_group = $info->datastream->controlGroup;
if (in_array($control_group, array('M', 'X'))) {
$form['file'] = array(
'#type' => 'file',
'#title' => t('Upload Document'),
'#description' => t('The file to upload.')
'#description' => t('A file with which to replace the contents of this datastream.'),
);
}
if ($control_group != 'X') {
$form['reference'] = array(
'#type' => 'textfield',
'#title' => t('Reference to object'),
'#description' => t('A URL the datastream will be updated to reference.'),
);
}
if ($control_group == 'M') {
$form['note'] = array(
'#type' => 'item',
'#title' => t('NOTE'),
'#value' => t('If both a file and a reference are given, the file will be given preference.'),
);
}
$form['pid'] = array(
'#type' => 'value',

Loading…
Cancel
Save