Skip to content

Commit

Permalink
ISLANDORA-2175: Use appropriate namespace in CSV importer (#125)
Browse files Browse the repository at this point in the history
* ISLANDORA-2175: Use appropriate namespace in CSV importer

- Get the namespace for the new objects from the collection policy rather than
  imposing 'islandora'
- Default to ingesting into islandora:root if the user-provided collection PID
  is invalid

* Implement changes suggested by @willtp87 and @DiegoPino

- Remove default behaviour of ingesting into `islandora:root`
  and just warn instead
- Implement a validator for the form element to prevent the
  issue from arising in the first place

* Implement changes requested by @DiegoPino

- Perform all collection related checks in submit handler
- Add `$namespace` as an optional parameter to
  `function islandora_entities_import_csv_batch()` and
  `islandora_entities_build_scholar_object()`
- Treat whitespace only object label as empty
- Set current user as owner of created objects

* Improve form validation

- Define a function that retrieves the collection info
- Validate the 'collection' form element separately
- Retrieve namespace in submit handler and re-check
  collection info
  • Loading branch information
d-r-p authored and DiegoPino committed Apr 4, 2018
1 parent 0ff2387 commit 6bb64ab
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function islandora_entities_import_form(array $form, array &$form_state) {
'#description' => t('Enter collection name or PID'),
'#autocomplete_path' => 'import_entity_csv/autocomplete',
'#required' => TRUE,
'#element_validate' => array('islandora_entities_csv_import_validate_collection'),
);
$form['submit'] = array(
'#type' => 'submit',
Expand All @@ -55,5 +56,54 @@ function islandora_entities_import_form_submit(array $form, array &$form_state)
module_load_include('inc', 'islandora_entities_csv_import', 'includes/utilities');
$file = file_load($form_state['values']['file']);
$collection_pid = $form_state['values']['collection'];
islandora_entities_import_csv_batch(drupal_realpath($file->uri), $collection_pid);
// CSV importer is limited to Islandora Person Content Model.
$cmodel = 'islandora:personCModel';
$namespace = 'islandora';
$collection_info = islandora_entities_import_get_collection_info($collection_pid);
if ($collection_info && $collection_info['pid'] && $collection_info['cmodels'] && isset($collection_info['cmodels'][$cmodel])) {
$namespace = $collection_info['cmodels'][$cmodel]['namespace'];
islandora_entities_import_csv_batch(drupal_realpath($file->uri), $collection_pid, $namespace);
}
else {
form_set_error('collection', t('Something is wrong with the specified collection.'));
$form_state['rebuild'] = TRUE;
}
}

/**
* Validates the collection Drupal Form Element.
*
* Ensures that the object exists, is a collection and has the correct policy.
*
* @param array $element
* The collection Drupal Form Element.
* @param array $form_state
* The Drupal Form State.
*/
function islandora_entities_csv_import_validate_collection(array $element, array &$form_state) {
module_load_include('inc', 'islandora_entities_csv_import', 'includes/utilities');
$collection_pid = $form_state['values']['collection'];
$collection_info = islandora_entities_import_get_collection_info($collection_pid);
if ($collection_info) {
if (!$collection_info['pid']) {
form_set_error('collection', t('"@collection" is not a collection object.', array('@collection' => $collection_pid)));
}
elseif ($collection_info['cmodels']) {
// CSV importer is limited to Islandora Person Content Model.
$cmodel = 'islandora:personCModel';
if (!isset($collection_info['cmodels'][$cmodel])) {
form_set_error('collection', t('"@collection" is not set up to hold "@cmodel" objects.', array(
'@collection' => $collection_pid,
'@cmodel' => $cmodel,
)));
}
}
else {
// This should never happen.
form_set_error('collection', t('"@collection" does not have a COLLECTION_POLICY.', array('@collection' => $collection_pid)));
}
}
else {
form_set_error('collection', t('The collection "@collection" does not exist.', array('@collection' => $collection_pid)));
}
}
63 changes: 56 additions & 7 deletions modules/islandora_entities_csv_import/includes/utilities.inc
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,60 @@
* Creates Islandora Entity Objects from CSV.
*/

/**
* Retrieves collection info.
*
* @param string $collection_pid
* A PID.
*
* @return array|bool
* An associative array containing the following info:
* - pid: The collection PID, or FALSE if not a collection object.
* - cmodels: The content models as returned by
* `CollectionPolicy::getContentModels()`, or FALSE if no
* `COLLECTION_POLICY` datastream is present.
* or FALSE if the object with given PID does not exist.
*/
function islandora_entities_import_get_collection_info($collection_pid) {
$ret = array('pid' => $collection_pid, 'cmodels' => FALSE);
$collection_object = islandora_object_load($collection_pid);
if ($collection_object) {
if (!in_array('islandora:collectionCModel', $collection_object->models)) {
// Not a collection object.
$ret['pid'] = FALSE;
return $ret;
}
elseif (isset($collection_object['COLLECTION_POLICY'])) {
$collection_policy = new CollectionPolicy($collection_object['COLLECTION_POLICY']->content);
// Get the associative array of content models, mapping each CModel PID
// to an associative array containing:
// - pid: The CModel PID.
// - name: The CModel name.
// - namespace: The namespace to use for this CModel in this collection.
$ret['cmodels'] = $collection_policy->getContentModels();
return $ret;
}
else {
// No `COLLECTION_POLICY`; this should never happen.
return $ret;
}
}
// Object does not exist.
return FALSE;
}

/**
* Created Entities from supplied csv file.
*
* @param url $path
* Path to file
* @param string $collection_pid
* PID of collection where created objects are to be stored.
* @param string $namespace
* The namespace the newly created objects should obtain. Defaults to
* 'islandora'.
*/
function islandora_entities_import_csv_batch($path, $collection_pid) {
function islandora_entities_import_csv_batch($path, $collection_pid, $namespace = 'islandora') {
$scholars = islandora_entities_read_csv($path);
$batch = array(
'title' => t("Creating Scholar Objects"),
Expand All @@ -24,7 +69,7 @@ function islandora_entities_import_csv_batch($path, $collection_pid) {
foreach ($scholars as $scholar) {
$batch['operations'][] = array(
'islandora_entities_build_scholar_object',
array($scholar, $collection_pid),
array($scholar, $collection_pid, $namespace),
);
}
batch_set($batch);
Expand Down Expand Up @@ -65,15 +110,19 @@ function islandora_entities_read_csv($path) {
* Header => value pairs representing date to be converted to MADS record
* @param string $collection_pid
* PID of collection to hold new objects.
* @param string $namespace
* The namespace the object to be created should obtain. Defaults to
* 'islandora'.
*/
function islandora_entities_build_scholar_object($scholar, $collection_pid) {

function islandora_entities_build_scholar_object($scholar, $collection_pid, $namespace = 'islandora') {
global $user;
$mads = islandora_entities_build_mads($scholar);
$tuque = new IslandoraTuque();
$repository = $tuque->repository;
$fedora_object = $repository->constructObject('islandora');
$fedora_object->label = isset($scholar['DISPLAY_NAME']) ? $scholar['DISPLAY_NAME'] : 'Member';
$fedora_object->owner = 'admin';
$fedora_object = $repository->constructObject($namespace);
$label = isset($scholar['DISPLAY_NAME']) ? trim($scholar['DISPLAY_NAME']) : '';
$fedora_object->label = !empty($label) ? $label : 'Member';
$fedora_object->owner = isset($user->name) ? $user->name : $fedora_object->owner;
$datastream = $fedora_object->constructDatastream('MADS', 'M');
$datastream->label = 'MADS';
$datastream->mimetype = 'application/xml';
Expand Down

0 comments on commit 6bb64ab

Please sign in to comment.