From 36aec2a94c6127b67c587f55f170994006d54b1a Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Mon, 11 Feb 2013 22:42:38 +0100 Subject: [PATCH 1/5] Added function to simplify system settings forms that utilize AJAX. --- includes/utilities.inc | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/includes/utilities.inc b/includes/utilities.inc index b210d35c..aa266d35 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -537,3 +537,24 @@ function islandora_directory_exists_message($path) { } return $image . $message; } + +/** + * Gets the default value for the given system_settings_form() element. + * + * Checks the $form_state for the default value if not it checks variable_get(). + * Assumes #tree is FALSE. This is only really required for elements that utilize + * AJAX, as their value can change before the submit actually takes place. + * + * @param string $name + * The name of the system settings form element. + * @param mixed $default_value + * The default value for the system settings form element. + * @param array $form_state + * The Drupal form state. + * + * @return mixed + * The default value for use in a the system_settings_form(). + */ +function islandora_system_settings_form_default_value($name, $default_value, array &$form_state) { + return isset($form_state['values'][$name]) ? $form_state['values'][$name] : variable_get($name, $default_value); +} From e18a7498a6a08ce68b5f2e115989ab371e54e076 Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Tue, 12 Feb 2013 09:39:51 +0100 Subject: [PATCH 2/5] Filename conventions. --- .../{islandora_authtokens.inc => authtokens.inc} | 0 ...y_controller.inc => object.entity_controller.inc} | 0 includes/{islandora_tuque.inc => tuque.inc} | 0 ...islandora_tuque_wrapper.inc => tuque_wrapper.inc} | 0 islandora.info | 12 ++++++------ islandora.module | 8 ++++---- tests/{islandora_authtokens.test => authtokens.test} | 6 +++--- tests/{islandora_hooks.test => hooks.test} | 0 ...islandora_hooks_test.info => islandora_test.info} | 3 +-- ...ndora_hooks_test.module => islandora_test.module} | 0 ...islandora_web_test_case.inc => web_test_case.inc} | 0 theme/{islandora.theme.inc => theme.inc} | 0 12 files changed, 14 insertions(+), 15 deletions(-) rename includes/{islandora_authtokens.inc => authtokens.inc} (100%) rename includes/{islandora_object.entity_controller.inc => object.entity_controller.inc} (100%) rename includes/{islandora_tuque.inc => tuque.inc} (100%) rename includes/{islandora_tuque_wrapper.inc => tuque_wrapper.inc} (100%) rename tests/{islandora_authtokens.test => authtokens.test} (94%) rename tests/{islandora_hooks.test => hooks.test} (100%) rename tests/{islandora_hooks_test.info => islandora_test.info} (62%) rename tests/{islandora_hooks_test.module => islandora_test.module} (100%) rename tests/{islandora_web_test_case.inc => web_test_case.inc} (100%) rename theme/{islandora.theme.inc => theme.inc} (100%) diff --git a/includes/islandora_authtokens.inc b/includes/authtokens.inc similarity index 100% rename from includes/islandora_authtokens.inc rename to includes/authtokens.inc diff --git a/includes/islandora_object.entity_controller.inc b/includes/object.entity_controller.inc similarity index 100% rename from includes/islandora_object.entity_controller.inc rename to includes/object.entity_controller.inc diff --git a/includes/islandora_tuque.inc b/includes/tuque.inc similarity index 100% rename from includes/islandora_tuque.inc rename to includes/tuque.inc diff --git a/includes/islandora_tuque_wrapper.inc b/includes/tuque_wrapper.inc similarity index 100% rename from includes/islandora_tuque_wrapper.inc rename to includes/tuque_wrapper.inc diff --git a/islandora.info b/islandora.info index c17ba661..9763819b 100644 --- a/islandora.info +++ b/islandora.info @@ -8,10 +8,10 @@ stylesheets[all][] = css/islandora.base.css stylesheets[all][] = css/islandora.theme.css files[] = includes/mime_detect.inc files[] = includes/dublin_core.inc -files[] = includes/islandora_tuque.inc -files[] = includes/islandora_tuque_wrapper.inc -files[] = includes/islandora_object.entity_controller.inc -files[] = tests/islandora_web_test_case.inc -files[] = tests/islandora_authtokens.test -files[] = tests/islandora_hooks.test +files[] = includes/tuque.inc +files[] = includes/tuque_wrapper.inc +files[] = includes/object.entity_controller.inc +files[] = tests/web_test_case.inc +files[] = tests/authtokens.test +files[] = tests/hooks.test php = 5.3 diff --git a/islandora.module b/islandora.module index bddb2967..e85a4156 100644 --- a/islandora.module +++ b/islandora.module @@ -227,13 +227,13 @@ function islandora_theme() { return array( // default object template 'islandora_default' => array( - 'file' => 'theme/islandora.theme.inc', + 'file' => 'theme/theme.inc', 'template' => 'theme/islandora-object', 'variables' => array('islandora_object' => NULL), ), // default edit page 'islandora_default_edit' => array( - 'file' => 'theme/islandora.theme.inc', + 'file' => 'theme/theme.inc', 'template' => 'theme/islandora-object-edit', 'variables' => array('islandora_object' => NULL), ), @@ -615,7 +615,7 @@ function islandora_tokened_object_load($object_id, $map) { if (array_key_exists('token', $_GET)) { $token = filter_input(INPUT_GET, 'token', FILTER_SANITIZE_STRING); if ($token) { - module_load_include('inc', 'islandora', 'includes/islandora_authtokens'); + module_load_include('inc', 'islandora', 'includes/authtokens'); $token_user = islandora_validate_object_token($object_id, $map[4], $token); $token_user = $token_user ? user_load($token_user->uid) : NULL; islandora_get_tuque_connection($token_user); @@ -794,7 +794,7 @@ function islandora_delete_datastream(FedoraDatastream &$datastream) { * Removes expired authentication tokens. */ function islandora_cron() { - module_load_include('inc', 'islandora', 'includes/islandora_authtokens'); + module_load_include('inc', 'islandora', 'includes/authtokens'); islandora_remove_expired_tokens(); } diff --git a/tests/islandora_authtokens.test b/tests/authtokens.test similarity index 94% rename from tests/islandora_authtokens.test rename to tests/authtokens.test index 326225f2..ccd3088a 100644 --- a/tests/islandora_authtokens.test +++ b/tests/authtokens.test @@ -29,7 +29,7 @@ class IslandoraAuthtokensTestCase extends IslandoraWebTestCase { * Test redeeming invalid tokens. */ public function testRedeemInvalidToken() { - module_load_include('inc', 'islandora', 'includes/islandora_authtokens'); + module_load_include('inc', 'islandora', 'includes/authtokens'); $token = islandora_get_object_token('test:pid', 'woot', 1); $this->assertTrue($token, 'Token was generated correctly.', 'Unit Tests'); // Redeem a token that doesn't exist with real pid and dsid. @@ -41,7 +41,7 @@ class IslandoraAuthtokensTestCase extends IslandoraWebTestCase { * Test redeeming valid tokens. */ public function testRedeemValidToken() { - module_load_include('inc', 'islandora', 'includes/islandora_authtokens'); + module_load_include('inc', 'islandora', 'includes/authtokens'); // Change the current user. global $user; $user_backup = $user; @@ -82,7 +82,7 @@ class IslandoraAuthtokensTestCase extends IslandoraWebTestCase { // Do some voodoo to get a token as the user we are connecting as // to do this we need to change the user we are logged in as. - module_load_include('inc', 'islandora', 'includes/islandora_authtokens'); + module_load_include('inc', 'islandora', 'includes/authtokens'); global $user; $backup = $user; $user = $account; diff --git a/tests/islandora_hooks.test b/tests/hooks.test similarity index 100% rename from tests/islandora_hooks.test rename to tests/hooks.test diff --git a/tests/islandora_hooks_test.info b/tests/islandora_test.info similarity index 62% rename from tests/islandora_hooks_test.info rename to tests/islandora_test.info index 9d211978..c6cb57ad 100644 --- a/tests/islandora_hooks_test.info +++ b/tests/islandora_test.info @@ -1,7 +1,6 @@ -name = Islandora Hook testing +name = Islandora Test Module description = Tests Hooks. Do not enable. core = 7.x package = Testing hidden = TRUE dependencies[] = islandora -files[] = islandora_hooks_test.module diff --git a/tests/islandora_hooks_test.module b/tests/islandora_test.module similarity index 100% rename from tests/islandora_hooks_test.module rename to tests/islandora_test.module diff --git a/tests/islandora_web_test_case.inc b/tests/web_test_case.inc similarity index 100% rename from tests/islandora_web_test_case.inc rename to tests/web_test_case.inc diff --git a/theme/islandora.theme.inc b/theme/theme.inc similarity index 100% rename from theme/islandora.theme.inc rename to theme/theme.inc From 1e594543c9a845eefc2fc863de7ddccad0c3fbba Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Thu, 14 Feb 2013 02:37:46 +0100 Subject: [PATCH 3/5] Drupal coding standards. --- includes/add_datastream.form.inc | 111 ++++++++-------- includes/authtokens.inc | 22 ++-- includes/breadcrumb.inc | 14 +- includes/datastream.inc | 12 +- includes/delete_datastream.form.inc | 35 ++--- includes/delete_object.form.inc | 6 +- includes/dublin_core.inc | 64 +++++---- includes/ingest.form.inc | 76 ++++++----- includes/ingest.menu.inc | 3 +- includes/mime_detect.inc | 61 +++++---- includes/object.entity_controller.inc | 30 ++++- includes/object_properties.form.inc | 28 ++-- includes/solution_packs.inc | 178 ++++++++++++-------------- includes/tuque.inc | 28 +++- includes/utilities.inc | 75 +++++++---- islandora.install | 6 +- islandora.module | 135 +++++++++---------- islandora.rules.inc | 9 +- theme/islandora-object-edit.tpl.php | 5 +- theme/islandora-object.tpl.php | 2 +- theme/theme.inc | 72 +++++++---- 21 files changed, 539 insertions(+), 433 deletions(-) diff --git a/includes/add_datastream.form.inc b/includes/add_datastream.form.inc index f4506424..1a577fb8 100644 --- a/includes/add_datastream.form.inc +++ b/includes/add_datastream.form.inc @@ -2,7 +2,6 @@ /** * @file - * * Functions for generating/validating/submitting the add datastream form. */ @@ -26,13 +25,12 @@ function islandora_add_datastream_form(array $form, array &$form_state, FedoraOb $form_state['object'] = $object; $form_state['datastream_requirements'] = islandora_get_missing_datastreams_requirements($object); $unused_datastreams = array_keys($form_state['datastream_requirements']); - $unused_datastreams = array_map(function($o) { return "'$o'"; }, $unused_datastreams); - $unused_datastreams = implode(', ', $unused_datastreams); - $upload_size = min((int)ini_get('post_max_size'), (int)ini_get('upload_max_filesize')); + $unused_datastreams = "'" . implode("', '", $unused_datastreams) . "'"; + $upload_size = min((int) ini_get('post_max_size'), (int) ini_get('upload_max_filesize')); return array( '#redirect' => "islandora/object/{$object->id}", '#attributes' => array( - 'enctype' => 'multipart/form-data' + 'enctype' => 'multipart/form-data', ), 'fieldset' => array( '#type' => 'fieldset', @@ -41,52 +39,46 @@ function islandora_add_datastream_form(array $form, array &$form_state, FedoraOb '#collapsed' => FALSE, 'dsid' => array( '#title' => 'Datastream ID', - '#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. Datastreams that ' . - 'are defined by the content model don\'t currently exist: ' . - '@unused_dsids.', - array('@unused_dsids' => $unused_datastreams) - ), - '#type' => 'textfield', - '#size' => 64, - '#maxlength' => 64, - '#required' => TRUE, - '#element_validate' => array( - 'islandora_add_datastream_form_field_is_not_an_existing_datastream_id', - 'islandora_add_datastream_form_field_starts_with_a_letter', - 'islandora_add_datastream_form_field_is_valid_dsid', - ), - '#autocomplete_path' => "islandora/object/{$object->id}/manage/datastreams/add/autocomplete" - ), - 'label' => array( - '#title' => 'Datastream Label', - '#required' => TRUE, - '#size' => 64, - '#maxlength' => 64, - '#description' => t('A Human readable label'), - '#type' => 'textfield', - '#element_validate' => array('islandora_add_datastream_form_field_does_not_contain_a_forward_slash') + '#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. Datastreams that are defined by the content model don't currently exist: @unused_dsids.", array('@unused_dsids' => $unused_datastreams)), ), - 'file' => array( - '#type' => 'managed_file', - '#required' => TRUE, - '#title' => t('Upload Document'), - '#size' => 48, - '#description' => t('Select a file to upload.
Files must be less than @size MB.', array('@size' => $upload_size)), - '#default_value' => isset($form_state['values']['files']) ? $form_state['values']['files'] : NULL, - '#upload_location' => 'temporary://', - '#upload_validators' => array( - 'file_validate_extensions' => array(NULL), - // Assume its specified in MB - 'file_validate_size' => array($upload_size * 1024 * 1024), - ), + '#type' => 'textfield', + '#size' => 64, + '#maxlength' => 64, + '#required' => TRUE, + '#element_validate' => array( + 'islandora_add_datastream_form_field_is_not_an_existing_datastream_id', + 'islandora_add_datastream_form_field_starts_with_a_letter', + 'islandora_add_datastream_form_field_is_valid_dsid', ), - 'submit' => array( - '#type' => 'submit', - '#value' => t('Add Datastream') + '#autocomplete_path' => "islandora/object/{$object->id}/manage/datastreams/add/autocomplete", + ), + 'label' => array( + '#title' => 'Datastream Label', + '#required' => TRUE, + '#size' => 64, + '#maxlength' => 64, + '#description' => t('A Human readable label'), + '#type' => 'textfield', + '#element_validate' => array('islandora_add_datastream_form_field_does_not_contain_a_forward_slash'), + ), + 'file' => array( + '#type' => 'managed_file', + '#required' => TRUE, + '#title' => t('Upload Document'), + '#size' => 48, + '#description' => t('Select a file to upload.
Files must be less than @size MB.', array('@size' => $upload_size)), + '#default_value' => isset($form_state['values']['files']) ? $form_state['values']['files'] : NULL, + '#upload_location' => 'temporary://', + '#upload_validators' => array( + 'file_validate_extensions' => array(NULL), + // Assume its specified in MB. + 'file_validate_size' => array($upload_size * 1024 * 1024), ), - ) + ), + 'submit' => array( + '#type' => 'submit', + '#value' => t('Add Datastream'), + ), ); } @@ -123,8 +115,7 @@ function islandora_add_datastream_form_field_starts_with_a_letter(array $element } /** - * Checks if the given form field contains only valid characters for a - * datastream id. + * Checks if the given form field contains a valid datastream ID. * * @param array $element * The form field to check. @@ -157,8 +148,7 @@ function islandora_add_datastream_form_field_does_not_contain_a_forward_slash(ar } /** - * Checks if the given datastream id requires that the upload file be of a - * certian MIME type. + * Checks if the given datastream requires the upload to be a certian MIME type. * * @param array $form * The Drupal form. @@ -180,8 +170,9 @@ function islandora_add_datastream_form_validate(array $form, array &$form_state) } /** - * Adds the new datastream based on the submitted values, only creates managed - * datastreams at the moment. + * Adds the new datastream based on the submitted values. + * + * Only creates managed datastreams at the moment. * * @param array $form * The Drupal form. @@ -200,9 +191,11 @@ function islandora_add_datastream_form_submit(array $form, array &$form_state) { $ds->setContentFromFile($path); $object->ingestDatastream($ds); file_delete($file); - } catch (exception $e) { + } + catch (exception $e) { drupal_set_message(t('@message', array('@message' => check_plain($e->getMessage()))), 'error'); - file_delete($file); // Make sure to delete anyways. + // Make sure to delete anyways. + file_delete($file); return; } drupal_set_message(t("Successfully Added Datastream!")); @@ -210,6 +203,7 @@ function islandora_add_datastream_form_submit(array $form, array &$form_state) { /** * Callback for an autocomplete field in the admin add datastream form. + * * It lists the missing required (may be optional) datastreams. * * @param FedoraObject $object @@ -225,7 +219,10 @@ function islandora_add_datastream_form_autocomplete_callback(FedoraObject $objec $dsids = array_combine($dsids, $dsids); $query = trim($query); if (!empty($query)) { - $dsids = array_filter($dsids, function($id) use($query) { return stripos($id, $query) !== FALSE; }); + $filter = function($id) use($query) { + return stripos($id, $query) !== FALSE; + }; + $dsids = array_filter($dsids, $filter); } drupal_json_output($dsids); } diff --git a/includes/authtokens.inc b/includes/authtokens.inc index 8efc4d11..9d5783a9 100644 --- a/includes/authtokens.inc +++ b/includes/authtokens.inc @@ -1,9 +1,11 @@ id}/manage/datastreams"); break; + case 1: - // One registry implementation, go there + // One registry implementation, go there. drupal_goto($edit_registry[0]['url']); break; + default: - // Multiple edit routes registered + // Multiple edit routes registered. return islandora_edit_datastream_registry_render($edit_registry); } } diff --git a/includes/delete_datastream.form.inc b/includes/delete_datastream.form.inc index 9c3cd368..faf92a18 100644 --- a/includes/delete_datastream.form.inc +++ b/includes/delete_datastream.form.inc @@ -2,9 +2,7 @@ /** * @file - * - * This file contains the admin (confirmation) form and callback functions to - * delete/purge a datastream. + * The admin/confirmation form to delete/purge a datastream. */ /** @@ -32,19 +30,18 @@ function islandora_delete_datastream_form(array $form, array &$form_state, Fedor } /** - * Submit handler for the delete datastream form. Purges/Delete's the given - * FedoraDatastream if possible. + * Submit handler for the delete datastream form. + * + * Purges/Delete's the given FedoraDatastream if possible. * * The ISLANDORA_PRE_PURGE_DATASTREAM_HOOK will query other modules as to * whether the given FedoraDatastream * should be: blocked from purging; state set to 'Deleted'; or purged. * - * @see islandora_delete_datastream(). - * - * @param string $object_id - * ID of the object - * @param string $datastream_id - * ID of the datastream + * @param array $form + * The Drupal form. + * @param array $form_state + * The Drupal form state. */ function islandora_delete_datastream_form_submit(array $form, array &$form_state) { $datastream = $form_state['datastream']; @@ -53,14 +50,22 @@ function islandora_delete_datastream_form_submit(array $form, array &$form_state $deleted = FALSE; try { $deleted = islandora_delete_datastream($datastream); - } catch (Exception $e) { - drupal_set_message(t('Error deleting %s datastream from object %o %e', array('%s' => $datastream_id, '%o' => $object->label, '%e' => $e->getMessage())), 'error'); + } + catch (Exception $e) { + drupal_set_message(t('Error deleting %s datastream from object %o %e', array( + '%s' => $datastream_id, + '%o' => $object->label, + '%e' => $e->getMessage())), 'error'); } if ($deleted) { - drupal_set_message(t('%d datastream sucessfully deleted from Islandora object %o', array('%d' => $datastream_id, '%o' => $object->label))); + drupal_set_message(t('%d datastream sucessfully deleted from Islandora object %o', array( + '%d' => $datastream_id, + '%o' => $object->label))); } else { - drupal_set_message(t('Error deleting %s datastream from object %o', array('%s' => $datastream_id, '%o' => $object->label)), 'error'); + drupal_set_message(t('Error deleting %s datastream from object %o', array( + '%s' => $datastream_id, + '%o' => $object->label)), 'error'); } $form_state['redirect'] = "islandora/object/{$object->id}"; } diff --git a/includes/delete_object.form.inc b/includes/delete_object.form.inc index 07cc2c21..46210e3c 100644 --- a/includes/delete_object.form.inc +++ b/includes/delete_object.form.inc @@ -2,9 +2,7 @@ /** * @file - * - * This file contains the admin (confirmation) form and callback functions to - * purge an object. + * The admin/confirmation form to purge an object. */ /** @@ -32,7 +30,7 @@ function islandora_delete_object_form(array $form, array &$form_state, FedoraObj } /** - * Delete's the object in questionGives deloption of deleting or purging and object. + * Delete's the given object. * * @param array $form * The Drupal form. diff --git a/includes/dublin_core.inc b/includes/dublin_core.inc index 144bd17d..53879623 100644 --- a/includes/dublin_core.inc +++ b/includes/dublin_core.inc @@ -2,9 +2,9 @@ /** * @file + * Implements a simple class for working with Dublin Core data. * - * Implements a simple class for working with Dublin Core data and exporting it - * back to XML. Inspiration and design shamelessly stolen from the pyfedora + * Inspiration and design shamelessly stolen from the pyfedora * project at http://pypi.python.org/pypi/pyfedora/0.1.0 */ @@ -33,34 +33,40 @@ class DublinCore { public $owner; /** - * Constructs a DublinCore object from a Fedora_Item object and populates the - * $dc array. + * Constructor. * * @param string $dc_xml + * The Dublin Core XML. */ - function DublinCore($dc_xml = NULL) { + public function __construct($dc_xml = NULL) { if (!empty($dc_xml)) { - $this->dc = self::import_from_xml_string($dc_xml); + $this->dc = self::importFromXMLString($dc_xml); } } /** - * Add Elements - * @param $element_name - * @param $value + * Add an element. + * + * @param string $element_name + * The name of the element to add. + * @param string $value + * The value of the element to add. */ - function add_element($element_name, $value) { + public function addElement($element_name, $value) { if (is_string($value) && is_array($this->dc[$element_name])) { $this->dc[$element_name][] = $value; } } /** - * Replace the given DC element with the values in $values - * @param string $elemnt_name - * @param array $values + * Replace the given DC element with the given values. + * + * @param string $element_name + * The name of the elements to set. + * @param mixed $values + * The values of the set the elements too. */ - function set_element($element_name, $values) { + public function setElement($element_name, $values) { if (is_array($values)) { $this->dc[$element_name] = $values; } @@ -70,11 +76,12 @@ class DublinCore { } /** - * Serialize this object to XML and return it. - * @param type $with_preamble - * @return type + * Serialize this object as an XML string. + * + * @return string + * The serialized XML. */ - function as_xml($with_preamble = FALSE) { + public function asXML() { $dc_xml = new DomDocument(); $oai_dc = $dc_xml->createElementNS('http://www.openarchives.org/OAI/2.0/oai_dc/', 'oai_dc:dc'); $oai_dc->setAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/'); @@ -95,18 +102,17 @@ class DublinCore { } /** - * Create dc from dict ( does nothing ) + * Serializes this object as an array. + * + * @return array + * The serialized object. */ - static function create_dc_from_dict() { - - } - - function as_formatted_array() { + public function asArray() { $dc_array = array(); foreach ($this as $element) { if (!empty($element)) { foreach ($element as $field => $values) { - // split value if the result value is an array + // Split value if the result value is an array. if (is_array($values)) { $value = ''; $i = 0; @@ -135,17 +141,19 @@ class DublinCore { /** - * Creates a new instance of the class by parsing dc_xml + * Creates a new instance of the class by parsing dc_xml. * * @param string $dc_xml + * Dublin Core XML. + * * @return DublinCore + * The instantiated object. */ - static function import_from_xml_string($dc_xml) { + public static function importFromXMLString($dc_xml) { $dc_doc = new DomDocument(); if ($dc_doc->loadXML($dc_xml)) { $oai_dc = $dc_doc->getElementsByTagNameNS('http://purl.org/dc/elements/1.1/', '*'); $new_dc = new DublinCore(); - foreach ($oai_dc as $child) { if (isset($new_dc->dc[$child->nodeName])) { array_push($new_dc->dc[$child->nodeName], $child->nodeValue); diff --git a/includes/ingest.form.inc b/includes/ingest.form.inc index 5d340257..420ebc87 100644 --- a/includes/ingest.form.inc +++ b/includes/ingest.form.inc @@ -2,14 +2,14 @@ /** * @file - * - * Defines the multi-page ingest form and any relevant hooks and functions for - * defining the multi-page ingest forms. + * Defines the multi-page ingest form and any relevant hooks and functions. */ /** - * Ingest form build function. Initializes the form state, and builds the - * initial list of steps, excutes the current step. + * Ingest form build function. + * + * Initializes the form state, and builds the initial list of steps, excutes + * the current step. * * @param array $form * The drupal form. @@ -32,9 +32,7 @@ */ function islandora_ingest_form(array $form, array &$form_state, array $configuration) { islandora_ingest_form_init_form_state($form_state, $configuration); - $form = islandora_ingest_form_execute_step($form, $form_state); - return $form; } @@ -71,7 +69,9 @@ function islandora_ingest_form_init_form_state_storage(array &$form_state, array // Use namespace if ID not given. $namespace = isset($configuration['namespace']) && !isset($id) ? $configuration['namespace'] : $id; $label = isset($configuration['label']) ? $configuration['label'] : 'New Object'; - $relationship_map = function($o) { return array('relationship' => 'isMemberOfCollection', 'pid' => $o); }; + $relationship_map = function($o) { + return array('relationship' => 'isMemberOfCollection', 'pid' => $o); + }; $relationships = empty($configuration['collections']) ? array() : array_map($relationship_map, $configuration['collections']); $object = islandora_prepare_new_object($namespace, $label, array(), array(), $relationships); $form_state['islandora'] = array( @@ -115,8 +115,10 @@ function islandora_ingest_get_approximate_steps(array $configuration) { } /** - * Executes the current step, building the form definition and appending on any - * additonal elements required for the step to function. + * Executes the current step. + * + * Builds the form definition and appends any additonal elements required for + * the step to function. * * @param array $form * The drupal form. @@ -138,6 +140,7 @@ function islandora_ingest_form_execute_step(array $form, array &$form_state) { $args = isset($step_info['args']) ? array_merge($args, $step_info['args']) : $args; $form = call_user_func_array($step_info['form_id'], $args); return islandora_ingest_form_stepify($form, $form_state, $step_info); + case 'batch': // @todo Implement if possible. break; @@ -160,7 +163,7 @@ function islandora_ingest_form_execute_step(array $form, array &$form_state) { */ function islandora_ingest_form_stepify(array $form, array &$form_state, array $step_info) { $step = islandora_ingest_form_get_step_number($form_state); - + $first_step = $step > 0; $last_step = $step == (islandora_ingest_form_get_step_count($form_state) - 1); $form['prev'] = $first_step ? islandora_ingest_form_previous_button() : NULL; @@ -187,11 +190,19 @@ function islandora_ingest_form_previous_button() { // convert all required fields to use #element_validation functions, and // Remove the need for #limit_validation_errors. Or maybe there is some // other solution, regardless of what it is, it won't be standard. - '#limit_validation_errors' => array() + '#limit_validation_errors' => array(), ); } -function islandora_ingest_form_get_step_number($form_state, $step_id = NULL) { +/** + * Get the current step. + * + * @param array $form_state + * The Drupal form state. + * @param string $step_id + * The ID of the step to get. + */ +function islandora_ingest_form_get_step_number(array $form_state, $step_id = NULL) { if ($step_id === NULL) { $step_id = islandora_ingest_form_get_step_id($form_state); } @@ -215,7 +226,7 @@ function islandora_ingest_form_get_step_number($form_state, $step_id = NULL) { function islandora_ingest_form_previous_submit(array $form, array &$form_state) { $step = islandora_ingest_form_get_step_number($form_state); islandora_ingest_form_stash_info($form_state); - $step = max(array($step - 1, 0)) ; + $step = max(array($step - 1, 0)); $form_state['islandora']['step_id'] = islandora_ingest_form_get_step_id($form_state, $step); islandora_ingest_form_grab_info($form_state); $form_state['rebuild'] = TRUE; @@ -240,7 +251,7 @@ function islandora_ingest_form_next_button(array $step_info) { '#value' => t('Next'), '#name' => 'next', '#validate' => $validate, - '#submit' => $submit + '#submit' => $submit, ); } @@ -260,7 +271,7 @@ function islandora_ingest_form_next_submit(array $form, array &$form_state) { $step = islandora_ingest_form_get_step_number($form_state); drupal_static_reset('islandora_ingest_form_get_steps'); islandora_ingest_form_stash_info($form_state); - $step = min(array($step + 1, islandora_ingest_form_get_step_count($form_state) - 1)) ; + $step = min(array($step + 1, islandora_ingest_form_get_step_count($form_state) - 1)); $form_state['islandora']['step_id'] = islandora_ingest_form_get_step_id($form_state, $step); islandora_ingest_form_grab_info($form_state); $form_state['rebuild'] = TRUE; @@ -272,7 +283,7 @@ function islandora_ingest_form_next_submit(array $form, array &$form_state) { function islandora_ingest_form_stash_info(array &$form_state) { $storage = &islandora_ingest_form_get_step_storage($form_state); $storage['values'] = $form_state['values']; - unset($form_state['values']); + unset($form_state['values']); } /** @@ -280,7 +291,7 @@ function islandora_ingest_form_stash_info(array &$form_state) { */ function islandora_ingest_form_grab_info(array &$form_state) { $storage = islandora_ingest_form_get_step_storage($form_state); - $form_state['values'] = isset($storage['values'])?$storage['values']:array(); + $form_state['values'] = isset($storage['values']) ? $storage['values'] : array(); if (isset($storage['configuration'])) { $form_state['islandora']['configuration'] = $storage['configuration']; } @@ -305,7 +316,7 @@ function islandora_ingest_form_ingest_button(array $step_info) { '#name' => 'ingest', '#value' => t('Ingest'), '#validate' => $validate, - '#submit' => $submit + '#submit' => $submit, ); } @@ -324,8 +335,10 @@ function islandora_ingest_form_submit(array $form, array &$form_state) { try { islandora_add_object($object); $form_state['redirect'] = "islandora/object/{$object->id}"; - } catch (Exception $e) { - // If post hooks throws it may already exist at this point but may be invalid, so don't say failed :P + } + catch (Exception $e) { + // If post hooks throws it may already exist at this point but may be + // invalid, so don't say failed. watchdog('islandora', $e->getMessage(), NULL, WATCHDOG_ERROR); drupal_set_message(t('A problem occured while ingesting "@label" (ID: @pid), please notifiy the administrator.', array('@label' => $object->label, '@pid' => $object->id)), 'error'); } @@ -346,8 +359,7 @@ function &islandora_ingest_form_get_configuration(array &$form_state) { } /** - * Gets a reference to the stored NewFedoraObject's which are to be ingested - * when the final step submits. + * Gets a reference to the stored NewFedoraObject's. * * @param array $form_state * The drupal form state. @@ -401,15 +413,14 @@ function islandora_ingest_form_get_step_id(array &$form_state, $step_number = NU } /** - * Get a reference to the step info of the given step or the current step if - * none is given. + * Get the step info of the given step or the current step if none is given. * * @param array $form_state * The drupal form state. * @param int $step * The index of the step to get. * - * @return integer + * @return int * The step info of the requested step if found, NULL otherwise. */ function islandora_ingest_form_get_step_info(array &$form_state, $step = NULL) { @@ -423,7 +434,12 @@ function islandora_ingest_form_get_step_info(array &$form_state, $step = NULL) { } /** - * Get general storage for the given step. (or the current step) + * Get general storage for the given/current step. + * + * @param array $form_state + * The drupal form state. + * @param string $step_id + * The ID of the step. */ function &islandora_ingest_form_get_step_storage(array &$form_state, $step_id = NULL) { if ($step_id === NULL) { @@ -459,7 +475,7 @@ function islandora_ingest_form_get_steps(array &$form_state) { function islandora_ingest_form_step_form_load_include(array &$form_state) { form_load_include($form_state, 'inc', 'islandora', 'includes/ingest.form'); $step_info = islandora_ingest_form_get_step_info($form_state); - // Load include files + // Load include files. if (isset($step_info['file']) && isset($step_info['module'])) { $matches = array(); preg_match('/^(.*)\.(.*)$/', $step_info['file'], $matches); @@ -490,7 +506,7 @@ function islandora_ingest_form_get_step_count(array $form_state) { * The sort order is undefined for steps which have the same weight. * * @param array $form_state - * All available form_state options used to build the multi-paged ingest process. + * The Drupal form state. * * @return array * The list of sorted ingest steps as defined by all implementers @@ -515,7 +531,7 @@ function &islandora_ingest_get_steps(array &$form_state) { // Need to pass by ref... But don't really want an alter. foreach (module_implements($hook) as $module) { $function = $module . '_' . $hook; - $module_steps = (array)$function($form_state); + $module_steps = (array) $function($form_state); $steps = array_merge($steps, $module_steps); } } diff --git a/includes/ingest.menu.inc b/includes/ingest.menu.inc index ac551014..ca4f6156 100644 --- a/includes/ingest.menu.inc +++ b/includes/ingest.menu.inc @@ -2,7 +2,6 @@ /** * @file - * * Ingest Menu callback hooks. */ @@ -57,7 +56,7 @@ function islandora_ingest_get_configuration() { * @param array $configuration * The key value pairs that are used to build the multi-paged ingest process. * - * @return boolean + * @return bool * TRUE if the configuration is valid, FALSE otherwise. */ function islandora_valid_ingest_configuration(array $configuration) { diff --git a/includes/mime_detect.inc b/includes/mime_detect.inc index 3807c50e..7e1fb7f7 100644 --- a/includes/mime_detect.inc +++ b/includes/mime_detect.inc @@ -1,9 +1,9 @@ extension + * Gets an associative array of MIME type and extension associations. + * + * Users the system mime.types file, or a local mime.types if one is found + * @see MIMEDetect::__constuctor() + * + * @return array + * An associative array where the keys are MIME types and the values + * extensions. */ protected function systemMimetypeExtensions() { $out = array(); @@ -328,11 +332,14 @@ class MimeDetect { } /** - * function: systemMimetypeExtensions - * description: populates an internal array of mimetype/extension associations - * from the system mime.types file, or a local mime.types if one is found (see - * __constuctor). - * return: array of extension => mimetype + * Gets a associative array of extensions and MIME types. + * + * Users the system mime.types file, or a local mime.types if one is found + * @see MIMEDetect::__constuctor() + * + * @return array + * An associative array where the keys are extensions and the values + * MIME types. */ protected function systemExtensionMimetypes() { $out = array(); @@ -340,15 +347,18 @@ class MimeDetect { $file = fopen($this->etcMimeTypes, 'r'); while (($line = fgets($file)) !== FALSE) { $line = trim(preg_replace('/#.*/', '', $line)); - if (!$line) + if (!$line) { continue; + } $parts = preg_split('/\s+/', $line); - if (count($parts) == 1) + if (count($parts) == 1) { continue; + } // A single part means a mimetype without extensions, which we ignore. $type = array_shift($parts); - foreach ($parts as $part) + foreach ($parts as $part) { $out[$part] = $type; + } } fclose($file); } @@ -356,4 +366,3 @@ class MimeDetect { } } - diff --git a/includes/object.entity_controller.inc b/includes/object.entity_controller.inc index 6f58e723..d3fd6986 100644 --- a/includes/object.entity_controller.inc +++ b/includes/object.entity_controller.inc @@ -1,13 +1,30 @@ array( '#type' => 'hidden', - '#value' => $object->id + '#value' => $object->id, ), 'object_label' => array( '#title' => t('Item Label'), '#default_value' => $object->label, '#required' => 'TRUE', '#description' => t('A Human readable label'), - // Double the normal length + // Double the normal length. '#size' => 120, - // Max length for a Fedora Label + // Max length for a Fedora Label. '#maxlength' => 255, - '#type' => 'textfield' + '#type' => 'textfield', ), // @todo Make this into an autocomplete field that list the users in the // system as well. @@ -45,7 +44,7 @@ function islandora_object_properties_form(array $form, array &$form_state, Fedor '#default_value' => $object->owner, '#required' => FALSE, '#description' => t('The owner id'), - '#type' => 'textfield' + '#type' => 'textfield', ), 'object_state' => array( '#title' => t('State'), @@ -53,18 +52,18 @@ function islandora_object_properties_form(array $form, array &$form_state, Fedor '#required' => TRUE, '#description' => t('The items state one of active, inactive or deleted'), '#type' => 'select', - '#options' => array('A' => 'Active', 'I' => 'Inactive', 'D' => 'Deleted') + '#options' => array('A' => 'Active', 'I' => 'Inactive', 'D' => 'Deleted'), ), 'submit' => array( '#type' => 'submit', - '#value' => 'Update Properties' + '#value' => 'Update Properties', ), 'delete' => array( '#type' => 'submit', '#value' => t('Delete'), '#submit' => array('islandora_object_properties_form_delete'), - '#limit_validation_errors' => array(array('pid')) - ) + '#limit_validation_errors' => array(array('pid')), + ), ); } @@ -85,7 +84,8 @@ function islandora_object_properties_form_submit(array $form, array &$form_state try { $object->owner = $owner; drupal_set_message(t('Successfully updated owner %s', array('%s' => $owner))); - } catch (Exception $e) { + } + catch (Exception $e) { form_set_error('object_owner', t('Error updating owner %s', array('%s' => $e->getMessage()))); } } @@ -93,7 +93,8 @@ function islandora_object_properties_form_submit(array $form, array &$form_state try { $object->state = $state; drupal_set_message(t('Successfully updated state %s', array('%s' => $state))); - } catch (Exception $e) { + } + catch (Exception $e) { form_set_error('object_state', t('Error updating state %s', array('%s' => $e->getMessage()))); } } @@ -101,7 +102,8 @@ function islandora_object_properties_form_submit(array $form, array &$form_state try { $object->label = $label; drupal_set_message(t('Successfully updated label %s', array('%s' => check_plain($label)))); - } catch (Exception $e) { + } + catch (Exception $e) { form_set_error(t('Error updating label %s', array('%s' => $e->getMessage()))); } } diff --git a/includes/solution_packs.inc b/includes/solution_packs.inc index f3e0f0e8..02952233 100644 --- a/includes/solution_packs.inc +++ b/includes/solution_packs.inc @@ -2,9 +2,7 @@ /** * @file - * - * This file contains all admin and callback functions for solution pack - * management. + * Admin and callback functions for solution pack management. */ /** @@ -36,8 +34,10 @@ function islandora_solution_packs_admin() { } /** - * A solution pack form for the given module, it lists all the given objects and - * their status, allowing the user to re-ingest them. + * A solution pack form for the given module. + * + * It lists all the given objects and their status, allowing the user to + * re-ingest them. * * @param array $form * The Drupal form definition. @@ -54,7 +54,7 @@ function islandora_solution_packs_admin() { * @return array * The Drupal form definition. */ -function islandora_solution_pack_form(array $form, array &$form_state, $solution_pack_module, $solution_pack_name, $objects = array()) { +function islandora_solution_pack_form(array $form, array &$form_state, $solution_pack_module, $solution_pack_name, $objects = array()) { // The order is important in terms of severity of the status, where higher // index indicates the status is more serious, this will be used to determine // what messages get displayed to the user. @@ -69,22 +69,22 @@ function islandora_solution_pack_form(array $form, array &$form_state, $solutio 'modified_datastream' => array( 'solution_pack' => t('Some objects must be reinstalled. See objects list for details.'), 'image' => $warning_image, - 'button' => t("Reinstall objects") + 'button' => t("Reinstall objects"), ), 'out_of_date' => array( 'solution_pack' => t('Some objects must be reinstalled. See objects list for details.'), 'image' => $warning_image, - 'button' => t("Reinstall objects") + 'button' => t("Reinstall objects"), ), 'missing_datastream' => array( 'solution_pack' => t('Some objects must be reinstalled. See objects list for details.'), 'image' => $warning_image, - 'button' => t("Reinstall objects") + 'button' => t("Reinstall objects"), ), 'missing' => array( - 'solution_pack' => t( 'Some objects are missing and must be installed. See objects list for details.'), + 'solution_pack' => t('Some objects are missing and must be installed. See objects list for details.'), 'image' => $warning_image, - 'button' => t("Install objects") + 'button' => t("Install objects"), ), ); $status_severities = array_keys($status_info); @@ -94,7 +94,8 @@ function islandora_solution_pack_form(array $form, array &$form_state, $solutio $object_status = islandora_check_object_status($object); $object_status_info = $status_info[$object_status['status']]; $object_status_severity = array_search($object_status['status'], $status_severities); - // The solution pack status severity will be the highest severity of the objects. + // The solution pack status severity will be the highest severity of + // the objects. $solution_pack_status_severity = max($solution_pack_status_severity, $object_status_severity); $exists = $object_status['status'] != 'missing'; $label = $exists ? l($object->label, "islandora/object/{$object->id}") : $object->label; @@ -129,22 +130,24 @@ function islandora_solution_pack_form(array $form, array &$form_state, $solutio 'install_status' => array( '#markup' => t('Object status: !image !status', array( '!image' => $solution_pack_status_info['image'], - '!status' => $solution_pack_status_info['solution_pack'] + '!status' => $solution_pack_status_info['solution_pack'], )), '#prefix' => '
', '#suffix' => '
', ), 'table' => array( '#type' => 'item', - '#markup' => theme('table', array('header' => array(t('Label'), t('PID'), t('Status')), 'rows' => $table_rows)) + '#markup' => theme('table', array( + 'header' => array(t('Label'), t('PID'), t('Status')), + 'rows' => $table_rows)), ), 'submit' => array( '#type' => 'submit', '#name' => $solution_pack_module, '#value' => $solution_pack_status_info['button'], '#attributes' => array('class' => array('islandora-solution-pack-submit')), - ) - ) + ), + ), ); } @@ -175,8 +178,7 @@ function islandora_solution_pack_form_submit(array $form, array &$form_state) { } /** - * Batch operation used by the solution pack forms to ingest/reingest required - * object(s) + * Batch operation to ingest/reingest required object(s). * * @param NewFedoraObject $object * The object to ingest/reingest. @@ -209,16 +211,20 @@ function islandora_solution_pack_batch_operation_reingest_object(NewFedoraObject drupal_set_message(t($msg, array( '@pid' => $pid, '@label' => $label, - '!object_link' => $object_link + '!object_link' => $object_link, )), $status); } /** - * This is to be called from the solution pack's hook_install() - * and hook_uninstall() functions. It provides a convient way to have a - * solution pack's required objects ingested at install time. + * Install the given solution pack. * - * @param string $module_name + * This is to be called from the solution pack's hook_install() and + * hook_uninstall() functions. + * + * It provides a convient way to have a solution pack's required objects + * ingested at install time. + * + * @param string $module * The name of the module that is calling this function in its * install/unistall hooks. * @param string $op @@ -248,7 +254,7 @@ function islandora_install_solution_pack($module, $op = 'install') { drupal_set_message(st($msg, array( '@module' => $module_name, '!config_link' => $config_link, - '@admin_link' => $admin_link + '@admin_link' => $admin_link, )), 'error'); return; } @@ -264,7 +270,7 @@ function islandora_install_solution_pack($module, $op = 'install') { foreach ($objects as $object) { $query = $connection->api->a->findObjects('query', 'pid=' . $object->id); $already_exists = !empty($query['results']); - $label = $object->label; + $label = $object->label; $object_link = l($label, "islandora/object/{$object->id}"); if ($already_exists) { $object_status = islandora_check_object_status($object); @@ -313,28 +319,28 @@ function islandora_uninstall_solution_pack($module) { if (!islandora_describe_repository()) { $msg = '@module: Did not uninstall any objects. Could not connect to the '; $msg .= 'repository. Please check the settings on the !config_link page '; - $msg .= 'and uninstall the required objects manually if necessary.'; + $msg .= 'and uninstall the required objects manually if necessary.'; drupal_set_message(st($msg, array( '@module' => $module_name, - '!config_link' => $config_link + '!config_link' => $config_link, )), 'error'); return; } $connection = islandora_get_tuque_connection(); $required_objects = module_invoke($module, 'islandora_required_objects', $connection); $objects = $required_objects[$module]['objects']; - $existing_objects = array_filter($objects, function($o) use($connection) { - $param = "pid={$o->id}"; - $query = $connection->api->a->findObjects('query', $param); - return !empty($query['results']); - } - ); + $filter = function($o) use($connection) { + $param = "pid={$o->id}"; + $query = $connection->api->a->findObjects('query', $param); + return !empty($query['results']); + }; + $existing_objects = array_filter($objects, $filter); foreach ($existing_objects as $object) { $msg = '@module: Did not remove !object_link. It may be used by other sites.'; $object_link = l($object->label, "islandora/object/{$object->id}"); drupal_set_message(st($msg, array( '!object_link' => $object_link, - '@module' => $module_name + '@module' => $module_name, )), 'warning'); } } @@ -364,10 +370,16 @@ function islandora_check_object_status(NewFedoraObject $object_definition) { $datastream_diff = array_diff($expected_datastreams, $existing_datastreams); if (!empty($datastream_diff)) { $status_friendly = format_plural(count($datastream_diff), 'Missing Datastream: %dsids.', 'Missing Datastreams: %dsids.', array('%dsids' => implode(', ', $datastream_diff))); - return array('status' => 'missing_datastream', 'status_friendly' => $status_friendly, 'data' => $datastream_diff); + return array( + 'status' => 'missing_datastream', + 'status_friendly' => $status_friendly, + 'data' => $datastream_diff, + ); } - $is_xml_datastream = function($ds) { return $ds->mimetype == 'text/xml'; }; + $is_xml_datastream = function($ds) { + return $ds->mimetype == 'text/xml'; + }; $xml_datastreams = array_filter(iterator_to_array($object_definition), $is_xml_datastream); $out_of_date_datastreams = array(); foreach ($xml_datastreams as $ds) { @@ -380,7 +392,11 @@ function islandora_check_object_status(NewFedoraObject $object_definition) { if (count($out_of_date_datastreams)) { $status_friendly = format_plural(count($out_of_date_datastreams), 'Datastream out of date: %dsids.', 'Datastreams out of date: %dsids.', array('%dsids' => implode(', ', $out_of_date_datastreams))); - return array('status' => 'out_of_date', 'status_friendly' => $status_friendly, 'data' => $out_of_date_datastreams); + return array( + 'status' => 'out_of_date', + 'status_friendly' => $status_friendly, + 'data' => $out_of_date_datastreams, + ); } // This is a pretty heavy function, but I'm not sure a better way. If we have @@ -418,10 +434,13 @@ function islandora_check_object_status(NewFedoraObject $object_definition) { } if (count($modified_datastreams)) { $status_friendly = format_plural(count($modified_datastreams), 'Modified Datastream: %dsids.', 'Modified Datastreams: %dsids.', array('%dsids' => implode(', ', $modified_datastreams))); - return array('status' => 'modified_datastream', 'data' => $modified_datastreams, 'status_friendly' => $status_friendly); + return array( + 'status' => 'modified_datastream', + 'data' => $modified_datastreams, + 'status_friendly' => $status_friendly, + ); } - // If not anything else we can assume its up to date. return array('status' => 'up_to_date', 'status_friendly' => t('Up-to-date')); } @@ -448,26 +467,21 @@ function islandora_check_object_status(NewFedoraObject $object_definition) { * @param string $model * The table will be populated with viewers supporting this content model * - * @return + * @return array * A form api array which defines a themed table to select a viewer. */ function islandora_viewers_form($variable_id = NULL, $mimetype = NULL, $model = NULL) { $form = array(); - // get viewers $viewers = islandora_get_viewers($mimetype, $model); if (!empty($viewers)) { - // add option for no viewer $no_viewer = array(); $no_viewer['none'] = array( 'label' => t('None'), - 'description' => t('Don\'t use a viewer for this solution pack.'), + 'description' => t("Don't use a viewer for this solution pack."), ); - // merge to viewers array $viewers = array_merge_recursive($no_viewer, $viewers); } - // get viewer settings $viewers_config = variable_get($variable_id, array()); - // viewer $form['viewers'] = array( '#type' => 'fieldset', '#title' => t('Viewers'), @@ -476,43 +490,33 @@ function islandora_viewers_form($variable_id = NULL, $mimetype = NULL, $model = ); if (!empty($viewers)) { - // viewers table $form['viewers'][$variable_id] = array( '#type' => 'item', '#title' => t('Select a viewer'), '#description' => t('Preferred viewer for your solution pack. These may be provided by third-party modules.'), - // This attribute is important to return the submitted values in a deeper - // nested arrays in '#tree' => TRUE, '#theme' => 'islandora_viewers_table', ); - // table loop foreach ($viewers as $name => $profile) { - $options[$name] = ''; - // machine name $form['viewers'][$variable_id]['name'][$name] = array( '#type' => 'hidden', '#value' => $name, ); - // label $form['viewers'][$variable_id]['label'][$name] = array( '#type' => 'item', '#markup' => $profile['label'], ); - // description $form['viewers'][$variable_id]['description'][$name] = array( '#type' => 'item', '#markup' => $profile['description'], ); - // configuration url $form['viewers'][$variable_id]['configuration'][$name] = array( '#type' => 'item', '#markup' => (isset($profile['configuration']) AND $profile['configuration'] != '') ? l(t('configure'), $profile['configuration']) : '', ); } - // radios $form['viewers'][$variable_id]['default'] = array( '#type' => 'radios', '#options' => isset($options) ? $options : array(), @@ -520,11 +524,9 @@ function islandora_viewers_form($variable_id = NULL, $mimetype = NULL, $model = ); } else { - // No viewers found $form['viewers'][$variable_id . '_no_viewers'] = array( '#markup' => t('No viewers detected.'), ); - // remove viewers variable variable_del($variable_id); } return $form; @@ -541,20 +543,19 @@ function islandora_viewers_form($variable_id = NULL, $mimetype = NULL, $model = * be listed. * * @param string $mimetype - * Specify a mimetype to return only viewers that support this certain - * mimetype. + * Specify a mimetype to return only viewers that support this certain + * mimetype. * @param string $content_model - * Specify a content model to return only viewers that support the content - * model. + * Specify a content model to return only viewers that support the content + * model. * * @return array - * Viewer definitions, or FALSE if none are found. + * Viewer definitions, or FALSE if none are found. */ function islandora_get_viewers($mimetype = NULL, $content_model = NULL) { $viewers = array(); - // get all viewers $defined_viewers = module_invoke_all('islandora_viewer_info'); - // filter viewers by mimetype + // Filter viewers by MIME type. foreach ($defined_viewers as $key => $value) { $value['mimetype'] = isset($value['mimetype']) ? $value['mimetype'] : array(); $value['model'] = isset($value['model']) ? $value['model'] : array(); @@ -569,54 +570,39 @@ function islandora_get_viewers($mimetype = NULL, $content_model = NULL) { } /** - * Theme function for the admin primary display table - * - * @param type $variables - * render element: $form - * Contains the form array - * @return - * rendered form element - * - * @see islandora_large_image_admin() + * Implements theme_hook(). */ function theme_islandora_viewers_table($variables) { - // set form $form = $variables['form']; $rows = array(); - foreach ($form['name'] as $key => $element) { - // Do not take form control structures. if (is_array($element) && element_child($key)) { - // set rows $row = array(); $row[] = array('data' => drupal_render($form['default'][$key])); $row[] = array('data' => drupal_render($form['label'][$key])); $row[] = array('data' => drupal_render($form['description'][$key])); $row[] = array('data' => drupal_render($form['configuration'][$key])); - - // add to rows $rows[] = array('data' => $row); } } - - // Individual table headers. - // default | label | description | configuration $header = array(); $header[] = array('data' => t('Default')); $header[] = array('data' => t('Label')); $header[] = array('data' => t('Description')); $header[] = array('data' => t('Configuration')); - // render form $output = ''; - $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'islandora-viewers-table'))); + $output .= theme('table', array( + 'header' => $header, + 'rows' => $rows, + 'attributes' => array('id' => 'islandora-viewers-table'), + )); $output .= drupal_render_children($form); - return $output; } /** - * Gather information and return a rendered viewer + * Gather information and return a rendered viewer. * * @param array/string $params * Array or string with data the module needs in order to render a full viewer @@ -624,21 +610,17 @@ function theme_islandora_viewers_table($variables) { * The id of the Drupal variable the viewer settings are saved in * @param FedoraObject $fedora_object * The tuque object representing the fedora object being displayed - * @return + * + * @return string * The callback to the viewer module. Returns a rendered viewer. Returns FALSE * if no viewer is set. */ - function islandora_get_viewer($params = NULL, $variable_id = NULL, $fedora_object = NULL) { - // get viewer from settings $settings = variable_get($variable_id, array()); - // make sure a viewer is set if (!empty($settings) AND $settings['default'] !== 'none') { - // get callback function $viewer_id = islandora_get_viewer_id($variable_id); if ($viewer_id AND $params !== NULL) { $callback = islandora_get_viewer_callback($viewer_id); - // call callback function return $callback($params, $fedora_object); } } @@ -649,8 +631,9 @@ function islandora_get_viewer($params = NULL, $variable_id = NULL, $fedora_objec * Get id of the enabled viewer. * * @param string $variable_id - * The id of the Drupal variable the viewer settings are saved in - * @return + * The ID of the Drupal variable the viewer settings are saved in + * + * @return string * The enabled viewer id. Returns FALSE if no viewer config is set. */ function islandora_get_viewer_id($variable_id) { @@ -665,8 +648,9 @@ function islandora_get_viewer_id($variable_id) { * Get callback function for a viewer. * * @param string $viewer_id - * The ID of a viewer - * @return + * The ID of a viewer. + * + * @return string * The callback function as a string as defined by the viewer. */ function islandora_get_viewer_callback($viewer_id = NULL) { diff --git a/includes/tuque.inc b/includes/tuque.inc index 9da9da14..ec278192 100644 --- a/includes/tuque.inc +++ b/includes/tuque.inc @@ -2,13 +2,12 @@ /** * @file - * * This file contains a class to include the Tuque php library. */ $islandora_module_path = drupal_get_path('module', 'islandora'); -//do this until we expost these in a module or library +// Do this until we expose these in a module or library. @include_once 'sites/all/libraries/tuque/Datastream.php'; @include_once 'sites/all/libraries/tuque/FedoraApi.php'; @include_once 'sites/all/libraries/tuque/FedoraApiSerializer.php'; @@ -59,7 +58,15 @@ class IslandoraTuque { */ public $repository = NULL; - function __construct($user = NULL, $url = NULL) { + /** + * Constructor. + * + * @param array $user + * A Drupal user. + * @param string $url + * The url to the fedora instance. + */ + public function __construct($user = NULL, $url = NULL) { if (!isset($user)) { global $user; } @@ -86,14 +93,23 @@ class IslandoraTuque { } } - static function exists() { + /** + * Checks if the Connection class exists. + * + * @returns bool + * TRUE if the Connection class exists FALSE otherwise. + */ + public static function exists() { return class_exists('RepositoryConnection'); } - static function getError() { + /** + * Displays and error for use when Tuque is not installed. + */ + public static function getError() { $islandora_doc_link = l(t('Islandora documentation'), 'https://wiki.duraspace.org/display/ISLANDORA/Islandora'); $tuque_link = l(t('Tuque Fedora API'), 'http://github.com/islandora/tuque'); - $message = t('Islandora requires the !tuque_url. Please install in /sites/all/libraries/tuque before continuing. See the !islandora_url.', array( '!tuque_url' => $tuque_link, '!islandora_url' => $islandora_doc_link)); + $message = t('Islandora requires the !tuque_url. Please install in /sites/all/libraries/tuque before continuing. See the !islandora_url.', array('!tuque_url' => $tuque_link, '!islandora_url' => $islandora_doc_link)); drupal_set_message(filter_xss($message), 'error', FALSE); } } diff --git a/includes/utilities.inc b/includes/utilities.inc index aa266d35..3679acf2 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -2,18 +2,21 @@ /** * @file - * Contains islandora utility functions + * Utility functions. * * @todo this file should be broken out into other files. */ /** - * Convert bytes to human readable format + * Convert bytes to human readable format. * * XXX: Shouldn't Drupal's format_size() be preferred? * - * @param integer bytes Size in bytes to convert + * @param int $bytes + * Size in bytes to convert + * * @return string + * Human readable size. */ function islandora_convert_bytes_to_human_readable($bytes, $precision = 2) { $kilobyte = 1024; @@ -48,12 +51,16 @@ function islandora_control_group_to_human_readable($control_group) { switch ($control_group) { case 'M': return 'Managed'; + case 'X': return 'Inline XML'; + case 'R': return 'Redirect'; + case 'E': return 'Externally Referenced'; + default: return $control_group; } @@ -65,7 +72,7 @@ function islandora_control_group_to_human_readable($control_group) { * @param string $pid * The object id to check. * - * @return boolean + * @return bool * TRUE if valid, FALSE otherwise. */ function islandora_is_valid_pid($pid) { @@ -78,7 +85,7 @@ function islandora_is_valid_pid($pid) { * @param string $dsid * The datastream id to check. * - * @return boolean + * @return bool * TRUE if valid, FALSE otherwise. */ function islandora_is_valid_dsid($dsid) { @@ -92,7 +99,8 @@ function islandora_is_valid_dsid($dsid) { * * @param string $url * A url to a Fedora repository, if NULL the default is used. - * @return + * + * @return array * Returns an array describing the repository. Returns FALSE if Fedora is down * or if the url is incorrect. */ @@ -119,7 +127,6 @@ function islandora_describe_repository($url = NULL) { * will be passed as arguments to module_invoke_all(). * * @see islandora_build_hook_list() - * To see how the hook list is generated. * * @param string $hook * A hook to call. @@ -182,7 +189,7 @@ function islandora_build_hook_list($hook, $refinements = array()) { */ function islandora_escape_pid_for_function($pid) { // Apparently, case doesn't matter for function calls in PHP, so let's not - // really worry about changing the case. + // really worry about changing the case. return str_replace( // Any PID characters which are not valid in the name of a PHP function. array( @@ -217,10 +224,11 @@ function islandora_get_namespace($id) { } /** - * Checks the given namespace or PID is/has an accessible namespace as defined - * by the "islandora_pids_allowed" variable. + * Checks the given namespace/PID is/has an accessible namespace. * - * @param string $namespace + * Accessible is defined by the "islandora_pids_allowed" variable. + * + * @param string $id * Either a PID or namespace to check for accessibility. Any string like those * below are fine. * @@ -230,7 +238,7 @@ function islandora_get_namespace($id) { * 'islandora:1234', * @endcode * - * @return boolean + * @return bool * TRUE if accessible, FALSE otherwise. */ function islandora_namespace_accessible($id) { @@ -243,8 +251,9 @@ function islandora_namespace_accessible($id) { } /** - * Gets any objects that the given object has a - * (isMemberOf, isMemberOfCollection) relationship with. + * Gets any objects that the given object has a parent relationship with. + * + * Parent relationships are defined as (isMemberOf, isMemberOfCollection). * * This function gets its info from the RELS-EXT directly rather than through an * risearch. @@ -266,13 +275,15 @@ function islandora_get_parents_from_rels_ext(FedoraObject $object) { // @todo some logging would be nice, not sure what this throws. return array(); } - $collections = array_map(function($o) { return islandora_object_load($o['object']['value']); }, $collections); + $map = function($o) { + return islandora_object_load($o['object']['value']); + }; + $collections = array_map($map, $collections); return array_filter($collections); } /** - * Checks what datastreams the object already has against its required - * datastreams as defined by its content models, and returns their intersection. + * Gets the datastreams requirments that are missing. * * @param FedoraObject $object * The object which models will be used to determine what datastreams it @@ -294,21 +305,23 @@ function islandora_get_missing_datastreams_requirements(FedoraObject $object) { } /** + * Gets the required datastreams for the given object. + * * Checks the object's content model's for which datastream are expected to be * used with this object, as defined by the DS-COMPOSITE-MODEL datastreams. * * For duplicate datastreams in the models, the first model defines the * datastreams attributes regardless of what other models define. * This should be undefined behavior according to the documentation. - * @see https://wiki.duraspace.org/display/FEDORA34/Fedora+Digital+Object+Model#FedoraDigitalObjectModel-ContentModelObjectCMODEL + * + * @link https://wiki.duraspace.org/display/FEDORA34/Fedora+Digital+Object+Model#FedoraDigitalObjectModel-ContentModelObjectCMODEL @endlink + * + * @see islandora_get_required_datastreams_from_content_model() * * @param FedoraObject $object * The object which models will be used to determine what datastreams it * should have. * - * @see islandora_get_required_datastreams_from_content_model() from more - * details on the return value. - * * @return array * The DS-COMPOSITE-MODEL defined datastreams that are required for the given * object. @@ -336,14 +349,16 @@ function islandora_get_datastreams_requirements_from_models(array $models) { $dsids += islandora_get_datastreams_requirements_from_content_model($model); } } - // The AUDIT Datastream can not really be added, so it can't really be missing. + // The AUDIT Datastream can not really be added, so it can't really be + // missing. unset($dsids['AUDIT']); return $dsids; } /** - * Checks the given content model for which datastreams are required for - * subscribing objects, as defined by it's DS-COMPOSITE-MODEL datastream. + * Checks the given content model for which datastreams are required. + * + * As defined by it's DS-COMPOSITE-MODEL datastream. * * @todo Add support for fetching the schema information. * @@ -380,7 +395,7 @@ function islandora_get_datastreams_requirements_from_content_model(FedoraObject $dsids[$dsid] = array( 'id' => $dsid, 'mime' => $mime, - 'optional' => ($optional == 'true') ? TRUE : FALSE + 'optional' => ($optional == 'true') ? TRUE : FALSE, ); } return $dsids; @@ -432,8 +447,11 @@ function islandora_prepare_new_object($namespace = NULL, $label = NULL, $datastr $label = isset($ds['label']) ? $ds['label'] : ''; $mimetype = isset($ds['mimetype']) ? $ds['mimetype'] : 'text/xml'; // Default 'Managed' - $control_group = (isset($ds['control_group']) && in_array($ds['control_group'], array('X', 'M', 'R', 'E'))) ? $ds['control_group'] : 'M'; - + $control_group = 'M'; + $groups = array('X', 'M', 'R', 'E'); + if (isset($ds['control_group']) && in_array($ds['control_group'], $groups)) { + $control_group = $ds['control_group']; + } $ds_uri = FALSE; if (file_valid_uri($ds['datastream_file'])) { $datastream_file = $ds['datastream_file']; @@ -454,6 +472,7 @@ function islandora_prepare_new_object($namespace = NULL, $label = NULL, $datastr $datastream->setContentFromUrl($datastream_file); } break; + case 'X': $datastream->setContentFromString(file_get_contents($datastream_file)); break; @@ -464,7 +483,7 @@ function islandora_prepare_new_object($namespace = NULL, $label = NULL, $datastr } /** - * Displays the repository is inaccessible message + * Displays the repository is inaccessible message. * * Use anywhere we want to ensure a consitent error message when the repository * is not accessible. diff --git a/islandora.install b/islandora.install index 549f7e14..321f6f46 100644 --- a/islandora.install +++ b/islandora.install @@ -1,7 +1,8 @@ . */ -// Common datastreams +// Common datastreams. define('DS_COMP_STREAM', 'DS-COMPOSITE-MODEL'); -// Permissions +// Permissions. define('FEDORA_VIEW_OBJECTS', 'view fedora repository objects'); define('FEDORA_MANAGE_DATASTREAMS', 'view fedora repository datastreams'); define('FEDORA_METADATA_EDIT', 'edit fedora metadata'); @@ -34,7 +35,7 @@ define('FEDORA_INGEST', 'ingest fedora objects'); define('FEDORA_PURGE', 'delete fedora objects and datastreams'); define('FEDORA_MANAGE_PROPERTIES', 'manage object properties'); -// Hooks +// Hooks. define('ISLANDORA_VIEW_HOOK', 'islandora_view_object'); define('ISLANDORA_EDIT_HOOK', 'islandora_edit_object'); // @todo Add Documentation. @@ -108,7 +109,8 @@ function islandora_menu() { 'page arguments' => array(2), 'type' => MENU_LOCAL_TASK, 'access callback' => 'islandora_object_manage_access_callback', - 'access arguments' => array(array(FEDORA_MANAGE_DATASTREAMS, FEDORA_MANAGE_PROPERTIES, FEDORA_ADD_DS), 2), + 'access arguments' => array( + array(FEDORA_MANAGE_DATASTREAMS, FEDORA_MANAGE_PROPERTIES, FEDORA_ADD_DS), 2), ); $items['islandora/object/%islandora_object/manage/datastreams'] = array( 'title' => 'Datastreams', @@ -141,7 +143,7 @@ function islandora_menu() { 'page arguments' => array('islandora_add_datastream_form', 2), 'type' => MENU_LOCAL_ACTION, 'access callback' => 'islandora_object_access_callback', - 'access arguments' => array(FEDORA_ADD_DS, 2) + 'access arguments' => array(FEDORA_ADD_DS, 2), ); $items['islandora/object/%islandora_object/manage/datastreams/add/autocomplete'] = array( 'file' => 'includes/add_datastream.form.inc', @@ -149,7 +151,7 @@ function islandora_menu() { 'page arguments' => array(2), 'type' => MENU_CALLBACK, 'access callback' => 'islandora_object_access_callback', - 'access arguments' => array(FEDORA_ADD_DS, 2) + 'access arguments' => array(FEDORA_ADD_DS, 2), ); $items['islandora/object/%islandora_object/datastream/%islandora_datastream'] = array( 'title' => 'View datastream', @@ -225,19 +227,19 @@ function islandora_admin_paths() { */ function islandora_theme() { return array( - // default object template + // Default object template. 'islandora_default' => array( 'file' => 'theme/theme.inc', 'template' => 'theme/islandora-object', 'variables' => array('islandora_object' => NULL), ), - // default edit page + // Default edit page. 'islandora_default_edit' => array( 'file' => 'theme/theme.inc', 'template' => 'theme/islandora-object-edit', 'variables' => array('islandora_object' => NULL), ), - // admin table for solution pack viewer selection + // Admin table for solution pack viewer selection. 'islandora_viewers_table' => array( 'file' => 'includes/solution_packs.inc', 'render element' => 'form', @@ -252,31 +254,31 @@ function islandora_permission() { return array( FEDORA_VIEW_OBJECTS => array( 'title' => t('View repository objects'), - 'description' => t('View objects in the repository. Note: Fedora XACML security policies may override this permission.') + 'description' => t('View objects in the repository. Note: Fedora XACML security policies may override this permission.'), ), FEDORA_MANAGE_DATASTREAMS => array( 'title' => t('Manage repository object datastreams'), - 'description' => t('Manage datastreams of objects in the repository. Note: Fedora XACML security policies may override this permission.') + 'description' => t('Manage datastreams of objects in the repository. Note: Fedora XACML security policies may override this permission.'), ), FEDORA_ADD_DS => array( 'title' => t('Add datastreams to repository objects'), - 'description' => t('Add datastreams to objects in the repository. Note: Fedora XACML security policies may override this position.') + 'description' => t('Add datastreams to objects in the repository. Note: Fedora XACML security policies may override this position.'), ), FEDORA_METADATA_EDIT => array( 'title' => t('Edit metadata'), - 'description' => t('Edit metadata for objects in the repository.') + 'description' => t('Edit metadata for objects in the repository.'), ), FEDORA_INGEST => array( 'title' => t('Create new repository objects'), - 'description' => t('Create new objects in the repository.') + 'description' => t('Create new objects in the repository.'), ), FEDORA_PURGE => array( 'title' => t('Permanently remove objects from the repository'), - 'description' => t('Permanently remove objects from the repository.') + 'description' => t('Permanently remove objects from the repository.'), ), FEDORA_MANAGE_PROPERTIES => array( 'title' => t('Manage object properties'), - 'description' => t('Modify object labels, owner IDs, and states.') + 'description' => t('Modify object labels, owner IDs, and states.'), ), ); } @@ -295,22 +297,18 @@ function islandora_forms($form_id) { } /** - * Checks whether the user can access the given object with the given - * permission. + * Checks whether the user can access the given object. * * Checks for object existance, accessiblitly, namespace permissions, * and user permissions * - * @see islandora_object_load() To find potential solutions to enable - * page not found errors. - * * @param string $perm * User permission to test for. * @param FedoraObject $object * The object to test, if NULL given the object doesn't exist or is * inaccessible. * - * @return boolean + * @return bool * TRUE if the user is allowed to access this object, FALSE otherwise. */ function islandora_object_access_callback($perm, $object = NULL) { @@ -325,15 +323,11 @@ function islandora_object_access_callback($perm, $object = NULL) { } /** - * Checks whether the user can access the given object and datastream with - * the given permission. + * Checks whether the user can access the given object and datastream. * * Checks for object existance, accessiblitly, namespace permissions, * and user permissions * - * @see islandora_object_load() To find potential solutions to enable page - * not found errors. - * * @param string $perm * The user permission to test for. * @param FedoraObject $object @@ -345,7 +339,7 @@ function islandora_object_access_callback($perm, $object = NULL) { * @param StdObject $account * The account to test permissions as or NULL for current user. * - * @return boolean + * @return bool * TRUE if the user is allowed to access this object, FALSE otherwise. */ function islandora_object_datastream_access_callback($perm, $object = NULL, $datastream = NULL, $account = NULL) { @@ -354,9 +348,9 @@ function islandora_object_datastream_access_callback($perm, $object = NULL, $dat } /** - * Checks whether the user can access the given object and datastream with - * the given permission. This function will validate and use a token if one - * is provided in the _GET array. + * Checks whether the user can access the given object and datastream. + * + * This function will validate and use a token if present in the GET parameters. * * Checks for object existance, accessiblitly, namespace permissions, * and user permissions @@ -380,22 +374,18 @@ function islandora_object_datastream_tokened_access_callback($perm, $object = NU } /** - * Checks whether the user can access the given object's manage tab - * with the given array of permissions. + * Checks whether the user can access the given object's manage tab. * * Checks for object existance, accessiblitly, namespace permissions, * and user permissions * - * @see islandora_object_load() To find potential solutions to enable - * page not found errors. - * * @param array $perms * Array of user permission to test for. * @param FedoraObject $object * The object to test, if NULL given the object doesn't exist or is * inaccessible. * - * @return boolean + * @return bool * TRUE if the user is allowed to access this object, FALSE otherwise. */ function islandora_object_manage_access_callback($perms, $object = NULL) { @@ -496,7 +486,7 @@ function islandora_view_object(FedoraObject $object) { module_load_include('inc', 'islandora', 'includes/utilities'); drupal_set_title($object->label); drupal_set_breadcrumb(islandora_get_breadcrumbs($object)); - // Optional pager parameters + // Optional pager parameters. $page_number = (empty($_GET['page'])) ? '1' : $_GET['page']; $page_size = (empty($_GET['pagesize'])) ? '10' : $_GET['pagesize']; $output = array(); @@ -532,13 +522,13 @@ function islandora_default_islandora_view_object($object) { } /** - * Just a wrapper around fetchings the IslandoraTuque object, with some very - * basic error logging. + * Just a wrapper around fetchings the IslandoraTuque object. * - * @param $user - * The user to connect as. + * Includes some very basic error logging. * - * @param $url + * @param object $user + * The user to connect as. + * @param string $url * The URL to connect to. * * @return IslandoraTuque @@ -550,7 +540,8 @@ function islandora_get_tuque_connection($user = NULL, $url = NULL) { if (IslandoraTuque::exists()) { try { $tuque = new IslandoraTuque($user, $url); - } catch (Exception $e) { + } + catch (Exception $e) { drupal_set_message(t('Unable to connect to the repository %e', array('%e' => $e)), 'error'); } } @@ -562,8 +553,10 @@ function islandora_get_tuque_connection($user = NULL, $url = NULL) { } /** - * A helper function to get a connection and return an object for objects - * specified in the menu path as '%islandora_object'. + * Loads the object from the given ID if possible. + * + * Often used to get a connection and return an object for the one specified in + * the menu path as '%islandora_object'. * * @param string $object_id * The pid of an object in the menu path identified by '%islandora_object'. @@ -582,7 +575,8 @@ function islandora_object_load($object_id) { if ($tuque) { try { return $tuque->repository->getObject(urldecode($object_id)); - } catch (Exception $e) { + } + catch (Exception $e) { if ($e->getCode() == '404') { return FALSE; } @@ -599,6 +593,8 @@ function islandora_object_load($object_id) { } /** + * Load the the object using a token if passed as a GET parameter. + * * A helper function to get a connection and return an object using a token * for authentication. * @@ -625,6 +621,8 @@ function islandora_tokened_object_load($object_id, $map) { } /** + * Fetches a datastream object. + * * This datastream load must take in arguments in a different * order than the usual islandora_datastream_load. This is because * the function islandora_tokened_object_load needs DSID. It uses @@ -642,13 +640,16 @@ function islandora_tokened_object_load($object_id, $map) { * * @return FedoraDatastream * A datastream from Fedora. - * @see islandora_datastream_load + * + * @see islandora_datastream_load */ function islandora_tokened_datastream_load($datastream_id, $map) { return islandora_datastream_load($datastream_id, $map[2]); } /** + * Fetches a datastream object. + * * A helper function to get an datastream specified as '%islandora_datastream' * for the object specified in the menu path as '%islandora_object'. * @@ -677,28 +678,34 @@ function islandora_datastream_load($datastream_id, $object_id) { } /** + * Fetches the given datastream version from its datastream. + * * Content model, collection view and collection policy datastreams may now * optionally define a version number in their top-level XML element as an * attribute, as in: * content); + // @TODO, need better check for $object + if ($object && $object[$dsid]) { + $doc = simplexml_load_string($object[$dsid]->content); } elseif (!empty($datastream_file)) { $doc = simplexml_load_file($datastream_file); } - if (!empty($doc) && $version = (int)$doc->attributes()->version) { + if (!empty($doc) && $version = (int) $doc->attributes()->version) { $return = $version; } @@ -710,18 +717,18 @@ function islandora_get_islandora_datastream_version($item = NULL, $dsid = NULL, */ function islandora_islandora_required_objects(IslandoraTuque $connection) { $module_path = drupal_get_path('module', 'islandora'); - // Root Collection + // Root Collection. $root_collection = $connection->repository->constructObject('islandora:root'); $root_collection->owner = 'fedoraAdmin'; $root_collection->label = 'Top-level Collection'; $root_collection->models = 'islandora:collectionCModel'; - // Collection Policy Datastream + // Collection Policy Datastream. $datastream = $root_collection->constructDatastream('COLLECTION_POLICY', 'X'); $datastream->label = 'Collection policy'; $datastream->mimetype = 'text/xml'; $datastream->setContentFromFile("$module_path/xml/islandora_collection_policy.xml", FALSE); $root_collection->ingestDatastream($datastream); - // TN Datastream + // TN Datastream. $datastream = $root_collection->constructDatastream('TN', 'M'); $datastream->label = 'Thumbnail'; $datastream->mimetype = 'image/png'; @@ -731,9 +738,9 @@ function islandora_islandora_required_objects(IslandoraTuque $connection) { 'islandora' => array( 'title' => 'Islandora', 'objects' => array( - $root_collection - ) - ) + $root_collection, + ), + ), ); } @@ -789,9 +796,7 @@ function islandora_delete_datastream(FedoraDatastream &$datastream) { } /** - * Implements hook_cron() - * - * Removes expired authentication tokens. + * Implements hook_cron(). */ function islandora_cron() { module_load_include('inc', 'islandora', 'includes/authtokens'); diff --git a/islandora.rules.inc b/islandora.rules.inc index bda999b3..7a97e44a 100644 --- a/islandora.rules.inc +++ b/islandora.rules.inc @@ -1,5 +1,10 @@ array( 'type' => 'islandora_object', 'label' => t('Subject'), - 'description' => t('An object of which we should check the ' . - 'relationships (The "subject" of the relationship).'), + 'description' => t('An object of which we should check the relationships (The "subject" of the relationship).'), ), 'pred_uri' => array( 'type' => 'text', @@ -112,4 +116,3 @@ function islandora_object_remove_relationship($sub, $pred_uri, $pred, $object, $ function islandora_object_add_relationship($sub, $pred_uri, $pred, $object, $type) { $sub->relationships->add($pred_uri, $pred, $object, $type); } - diff --git a/theme/islandora-object-edit.tpl.php b/theme/islandora-object-edit.tpl.php index 266a1259..ea9cec64 100644 --- a/theme/islandora-object-edit.tpl.php +++ b/theme/islandora-object-edit.tpl.php @@ -2,9 +2,7 @@ /** * @file - * - * islandora-object-edit.tpl.php: This is a template for objects that do not - * have a module to registered to build their display. + * The default manage datastreams view for objects. * * islandora_object is a fedora tuque Object * $object->label @@ -17,7 +15,6 @@ * and each element has an array of values. * dc.title can have none, one or many titles * this is the case for all dc elements. - * */ ?> diff --git a/theme/islandora-object.tpl.php b/theme/islandora-object.tpl.php index 753232c5..13b40ba0 100644 --- a/theme/islandora-object.tpl.php +++ b/theme/islandora-object.tpl.php @@ -2,6 +2,7 @@ /** * @file + * The default object view. * * This is a template for objects that do not have a module to registered to * build their display. @@ -55,7 +56,6 @@ * foreach ($object as $ds) { * do something here * } - * */ ?>
diff --git a/theme/theme.inc b/theme/theme.inc index 7cac4d1a..1622d810 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -2,18 +2,13 @@ /** * @file - * * This file contains all theme and preprocess functions. */ /** - * Preprocess the edit template - * - * @global string $base_url - * @param array $variables - * theme variables for the edit template + * Implements hook_preprocess_theme(). */ -function islandora_preprocess_islandora_default_edit(&$variables) { +function islandora_preprocess_islandora_default_edit(array &$variables) { global $base_url; $islandora_object = $variables['islandora_object']; $datastreams = array(); @@ -32,27 +27,55 @@ function islandora_preprocess_islandora_default_edit(&$variables) { $rows = array(); foreach ($islandora_object as $ds) { $rows[] = array( - array('class' => 'datastream-id', 'data' => l($ds->id, islandora_datastream_get_url($ds, 'view'))), - array('class' => 'datastream-label', 'data' => $ds->label), - array('class' => 'datastream-control', 'data' => islandora_control_group_to_human_readable($ds->controlGroup)), - array('class' => 'datastream-mime', 'data' => $ds->mimeType), - array('class' => 'datastream-size', 'data' => islandora_datastream_get_human_readable_size($ds)), - array('class' => 'datastream-download', 'data' => l(t('download'), islandora_datastream_get_url($ds, 'download'))), - array('class' => 'datstream-edit', 'data' => islandora_datastream_edit_get_link($ds)), - array('class' => 'datastream-delete', 'data' => islandora_datastream_get_delete_link($ds)), + array( + 'class' => 'datastream-id', + 'data' => l($ds->id, islandora_datastream_get_url($ds, 'view')), + ), + array( + 'class' => 'datastream-label', + 'data' => $ds->label, + ), + array( + 'class' => 'datastream-control', + 'data' => islandora_control_group_to_human_readable($ds->controlGroup), + ), + array( + 'class' => 'datastream-mime', + 'data' => $ds->mimeType, + ), + array( + 'class' => 'datastream-size', + 'data' => islandora_datastream_get_human_readable_size($ds), + ), + array( + 'class' => 'datastream-download', + 'data' => l(t('download'), islandora_datastream_get_url($ds, 'download')), + ), + array( + 'class' => 'datstream-edit', + 'data' => islandora_datastream_edit_get_link($ds), + ), + array( + 'class' => 'datastream-delete', + 'data' => islandora_datastream_get_delete_link($ds), + ), ); } $caption = $islandora_object->label . ' - ' . $islandora_object->id; - $table = array('colgroups' => NULL, 'sticky' => TRUE, 'empty' => 'Error loading datastreams', 'caption' => $caption, 'header' => $header, 'rows' => $rows, 'attributes' => $table_attributes); + $table = array( + 'colgroups' => NULL, + 'sticky' => TRUE, + 'empty' => 'Error loading datastreams', + 'caption' => $caption, + 'header' => $header, + 'rows' => $rows, + 'attributes' => $table_attributes, + ); $variables['datastream_table'] = $table; } - /** - * Preprocess for the default view template - * - * @global string $base_url - * @param array $variables + * Implements hook_preprocess_theme(). */ function islandora_preprocess_islandora_default(&$variables) { drupal_add_js('misc/form.js'); @@ -86,9 +109,10 @@ function islandora_preprocess_islandora_default(&$variables) { $variables['datastreams'] = $datastreams; try { $dc = $islandora_object['DC']->content; - $dc_object = DublinCore::import_from_xml_string($dc); - $dc_array = $dc_object->as_formatted_array(); - } catch (Exception $e) { + $dc_object = DublinCore::importFromXMLString($dc); + $dc_array = $dc_object->asArray(); + } + catch (Exception $e) { drupal_set_message(t('Error retrieving object %s %t', array('%s' => $islandora_object->id, '%t' => $e->getMessage())), 'error', FALSE); } $variables['dc_array'] = isset($dc_array) ? $dc_array : array(); From 902742f10af7ef034257b41fdad98e1fefc1525d Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Thu, 14 Feb 2013 16:23:47 -0400 Subject: [PATCH 4/5] Added method to retreive basic ds_comp info --- includes/utilities.inc | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/includes/utilities.inc b/includes/utilities.inc index 3679acf2..118cf961 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -577,3 +577,58 @@ function islandora_directory_exists_message($path) { function islandora_system_settings_form_default_value($name, $default_value, array &$form_state) { return isset($form_state['values'][$name]) ? $form_state['values'][$name] : variable_get($name, $default_value); } + +/** + * Returns basic information about DS-COMPOSITE-MODEL + * + * @param string $pid + * pid of content model containing DS_COMP stream + * @return array + * array of values in the following form + * + * [DC] => Array + * ( + * [mimetype] => text/xml + * ) + * + * [MODS] => Array + * ( + * [optional] => true + * [mimetype] => text/xml + * ) + * + * [RELS-EXT] => Array + * ( + * [mimetype] => application/rdf+xml + * ) + * + * [RELS-INT] => Array + * ( + * [optional] => true + * [mimetype] => application/rdf+xml + * ) + * + */ + +function islandora_get_comp_ds_mappings($pid) { + $cm_object = islandora_object_load($pid); + if (!isset($cm_object) || !isset($cm_object['DS-COMPOSITE-MODEL'])) { + return FALSE; + } + $datastream = $cm_object['DS-COMPOSITE-MODEL']; + $ds_comp_stream = $datastream->content; + $sxml = new SimpleXMLElement($ds_comp_stream); + $mappings = array(); + foreach ($sxml->dsTypeModel as $ds) { + $dsid = (string) $ds['ID']; + $optional = (string) $ds['optional']; + foreach ($ds->form as $form) { + $mime = (string) $form['MIME']; + if ($optional) { + $mappings[$dsid]['optional'] = $optional; + } + $mappings[$dsid]['mimetype'] = $mime; + } + } + return $mappings; +} \ No newline at end of file From 355367ae158fd8883f636bad009fab5d7ad3595b Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Thu, 14 Feb 2013 09:05:51 +0100 Subject: [PATCH 5/5] Issue: 1013 MARCXml module ingest hook conflicts with Newspaper/Book page ingest Clean up the ingest form code, and make it easier to interact with. --- includes/ingest.form.inc | 528 ++++++++++++++++++++++----------------- includes/ingest.menu.inc | 32 +-- 2 files changed, 308 insertions(+), 252 deletions(-) diff --git a/includes/ingest.form.inc b/includes/ingest.form.inc index 420ebc87..c285c5a1 100644 --- a/includes/ingest.form.inc +++ b/includes/ingest.form.inc @@ -12,9 +12,9 @@ * the current step. * * @param array $form - * The drupal form. + * The Drupal form. * @param array $form_state - * The drupal form state. + * The Drupal form state. * @param array $configuration * An associative array of configuration values that are used to build the * list of steps to be executed, including: @@ -31,67 +31,196 @@ * The form definition of the current step. */ function islandora_ingest_form(array $form, array &$form_state, array $configuration) { - islandora_ingest_form_init_form_state($form_state, $configuration); - $form = islandora_ingest_form_execute_step($form, $form_state); - return $form; + islandora_ingest_form_init_form_state_storage($form_state, $configuration); + return islandora_ingest_form_execute_step($form, $form_state); } /** - * Prepares/Initializes the form state for use. + * Validates the given ingest configuration. * - * Also cleans up or loads any data required. + * At the moment it only requires that models are present. + * + * @todo Add hook for manipulating/validating the configuration. + * + * @throws InvalidArgumentException * - * @param array $form_state - * The drupal form state. * @param array $configuration - * A list of key value pairs that are used to build the list of steps to be - * executed. + * The key value pairs that are used to build the multi-paged ingest process. + * + * @see islandora_ingest_form() + * + * @return bool + * TRUE if the configuration is valid, FALSE otherwise. */ -function islandora_ingest_form_init_form_state(array &$form_state, array $configuration) { - // First time initialization of storage. - islandora_ingest_form_init_form_state_storage($form_state, $configuration); +function islandora_ingest_form_validiate_configuration(array $configuration) { + if (empty($configuration['models'])) { + $message = t('Ingest configuration not vaild, no models were given'); + drupal_set_message($message, 'error'); + throw new InvalidArgumentException($message); + } } /** * Initializes the form_state storage for use in the ingest multi-page forms. * * @param array $form_state - * The drupal form state. + * The Drupal form state. * @param array $configuration * A list of key value pairs that are used to build the list of steps to be * executed. */ function islandora_ingest_form_init_form_state_storage(array &$form_state, array $configuration) { if (empty($form_state['islandora'])) { - module_load_include('inc', 'islandora', 'includes/utilities'); - // Use ID if given. - $id = isset($configuration['id']) ? $configuration['id'] : NULL; - // Use namespace if ID not given. - $namespace = isset($configuration['namespace']) && !isset($id) ? $configuration['namespace'] : $id; - $label = isset($configuration['label']) ? $configuration['label'] : 'New Object'; - $relationship_map = function($o) { - return array('relationship' => 'isMemberOfCollection', 'pid' => $o); - }; - $relationships = empty($configuration['collections']) ? array() : array_map($relationship_map, $configuration['collections']); - $object = islandora_prepare_new_object($namespace, $label, array(), array(), $relationships); + // Validate the configuration before using it. + islandora_ingest_form_validiate_configuration($configuration); + $object = islandora_ingest_form_prepare_new_object($configuration); $form_state['islandora'] = array( 'step_id' => NULL, 'objects' => array($object), - 'configuration' => $configuration, + 'shared_storage' => $configuration, 'step_storage' => array(), ); } +} + +/** + * Prepares a new object based on the given configuration. + * + * @param array $configuration + * The list of key/value pairs of configuration. + * + * @return NewFedoraObject + * The new object. + */ +function islandora_ingest_form_prepare_new_object(array $configuration) { + module_load_include('inc', 'islandora', 'includes/utilities'); + // ID is more specific than namespace so it will take precedence. + $id = isset($configuration['namespace']) ? $configuration['namespace'] : 'islandora'; + $id = isset($configuration['id']) ? $configuration['id'] : $id; + $label = isset($configuration['label']) ? $configuration['label'] : 'New Object'; + $relationship_map = function($o) { + return array('relationship' => 'isMemberOfCollection', 'pid' => $o); + }; + $relationships = empty($configuration['collections']) ? array() : array_map($relationship_map, $configuration['collections']); + return islandora_prepare_new_object($id, $label, array(), array(), $relationships); +} + +/** + * Gets the given/current step. + * + * The current step is returned if no step ID is given. If the current step is + * not defined it's assume to be the first step. + * + * @param array $form_state + * The Drupal form state. + * @param string $step_id + * The ID of the step. + * + * @return array + * The given/current step if found, NULL otherwise. + */ +function islandora_ingest_form_get_step(array &$form_state, $step_id = NULL) { + $step_id = isset($step_id) ? $step_id : islandora_ingest_form_get_current_step_id($form_state); + $steps = islandora_ingest_form_get_steps($form_state); + if (isset($step_id) && isset($steps[$step_id])) { + return $steps[$step_id]; + } + return NULL; +} - $steps = islandora_ingest_get_steps($form_state); - if ($form_state['islandora']['step_id'] === NULL || !array_key_exists( - $form_state['islandora']['step_id'], $steps)) { - reset($steps); - $form_state['islandora']['step_id'] = key($steps); +/** + * Gets the ID of the current step. + * + * If a current step is not defined, its assumed to be the first step. + * + * @param array $form_state + * The Drupal form state. + * + * @return string + * The step ID. + */ +function islandora_ingest_form_get_current_step_id(array &$form_state) { + if (empty($form_state['islandora']['step_id'])) { + $steps = islandora_ingest_form_get_steps($form_state); + return array_shift(array_keys($steps)); } + return $form_state['islandora']['step_id']; +} - // Stash the current configuration in the step. - $storage = &islandora_ingest_form_get_step_storage($form_state); - $storage['configuration'] = $form_state['islandora']['configuration']; +/** + * Gets the ID of the next step. + * + * If a current step is not defined, its assumed to be the first step. + * + * @param array $form_state + * The Drupal form state. + * + * @return string + * The next step ID if found, NULL otherwise. + */ +function islandora_ingest_form_get_next_step_id(array &$form_state) { + $step_id = islandora_ingest_form_get_current_step_id($form_state); + $step_ids = array_keys(islandora_ingest_form_get_steps($form_state)); + $index = array_search($step_id, $step_ids); + $count = count($step_ids); + if ($index !== FALSE && ++$index < $count) { + return $step_ids[$index]; + } + return NULL; +} + +/** + * Gets the ID of the previous step. + * + * If a current step is not defined, its assumed to be the first step. + * + * @param array $form_state + * The Drupal form state. + * + * @return string + * The previous step ID if found, NULL otherwise. + */ +function islandora_ingest_form_get_previous_step_id(array &$form_state) { + $step_id = islandora_ingest_form_get_current_step_id($form_state); + $step_ids = array_keys(islandora_ingest_form_get_steps($form_state)); + $index = array_search($step_id, $step_ids); + if ($index !== FALSE && --$index >= 0) { + return $step_ids[$index]; + } + return NULL; +} + +/** + * Increments the current step if possible. + * + * @param array $form_state + * The Drupal form state. + */ +function islandora_ingest_form_increment_step(array &$form_state) { + // When going to the next step rebuild the list of steps as the submit + // of the current step could have added/removed a step. + drupal_static_reset('islandora_ingest_form_get_steps'); + $next_step_id = islandora_ingest_form_get_next_step_id($form_state); + if (isset($next_step_id)) { + islandora_ingest_form_stash_info($form_state); + $form_state['islandora']['step_id'] = $next_step_id; + islandora_ingest_form_grab_info($form_state); + } +} + +/** + * Decrement the current step if possible. + * + * @param array $form_state + * The Drupal form state. + */ +function islandora_ingest_form_decrement_step(array &$form_state) { + $previous_step_id = islandora_ingest_form_get_previous_step_id($form_state); + if (isset($previous_step_id)) { + islandora_ingest_form_stash_info($form_state); + $form_state['islandora']['step_id'] = $previous_step_id; + islandora_ingest_form_grab_info($form_state); + } } /** @@ -106,40 +235,38 @@ function islandora_ingest_form_init_form_state_storage(array &$form_state, array function islandora_ingest_get_approximate_steps(array $configuration) { $fake_form_state = array( 'islandora' => array( - 'configuration' => $configuration, + 'shared_storage' => $configuration, ), ); - $steps = islandora_ingest_get_steps($fake_form_state); - drupal_static_reset('islandora_ingest_get_steps'); + $steps = islandora_ingest_form_get_steps($fake_form_state); + drupal_static_reset('islandora_ingest_form_get_steps'); return $steps; } /** * Executes the current step. * - * Builds the form definition and appends any additonal elements required for - * the step to function. + * Builds the form definition and appends on any additonal elements required + * for the step to function. * * @param array $form - * The drupal form. + * The Drupal form. * @param array $form_state - * The drupal form state. + * The Drupal form state. * * @return array * The form definition of the current step. */ function islandora_ingest_form_execute_step(array $form, array &$form_state) { // Load any required files for the current step. - islandora_ingest_form_step_form_load_include($form_state); - - $step_info = islandora_ingest_form_get_step_info($form_state); - - switch ($step_info['type']) { + islandora_ingest_form_load_include($form_state); + $step = islandora_ingest_form_get_step($form_state); + switch ($step['type']) { case 'form': $args = array($form, &$form_state); - $args = isset($step_info['args']) ? array_merge($args, $step_info['args']) : $args; - $form = call_user_func_array($step_info['form_id'], $args); - return islandora_ingest_form_stepify($form, $form_state, $step_info); + $args = isset($step['args']) ? array_merge($args, $step['args']) : $args; + $form = call_user_func_array($step['form_id'], $args); + return islandora_ingest_form_stepify($form, $form_state, $step); case 'batch': // @todo Implement if possible. @@ -152,37 +279,76 @@ function islandora_ingest_form_execute_step(array $form, array &$form_state) { * Append Prev/Next buttons submit/validation handlers etc. * * @param array $form - * The drupal form. + * The Drupal form. * @param array $form_state - * The drupal form state. - * @param array $step_info - * The info for building the current form step. + * The Drupal form state. * * @return array * The stepified drupal form definition for the given step. */ -function islandora_ingest_form_stepify(array $form, array &$form_state, array $step_info) { - $step = islandora_ingest_form_get_step_number($form_state); - - $first_step = $step > 0; - $last_step = $step == (islandora_ingest_form_get_step_count($form_state) - 1); - $form['prev'] = $first_step ? islandora_ingest_form_previous_button() : NULL; - $form['next'] = $last_step ? islandora_ingest_form_ingest_button($step_info) : islandora_ingest_form_next_button($step_info); +function islandora_ingest_form_stepify(array $form, array &$form_state) { + $first_step = islandora_ingest_form_on_first_step($form_state); + $last_step = islandora_ingest_form_on_last_step($form_state); + $form['prev'] = $first_step ? NULL : islandora_ingest_form_previous_button($form_state); + $form['next'] = $last_step ? islandora_ingest_form_ingest_button($form_state) : islandora_ingest_form_next_button($form_state); return $form; } +/** + * Checks if we are on the first step. + * + * @param array $form_state + * The Drupal form state. + * + * @return bool + * TRUE if we are currently on the first step, FALSE otherwise. + */ +function islandora_ingest_form_on_first_step(array &$form_state) { + $step_id = islandora_ingest_form_get_current_step_id($form_state); + $step_ids = array_keys(islandora_ingest_form_get_steps($form_state)); + return array_search($step_id, $step_ids) == 0; +} + +/** + * Checks if we are on the last step. + * + * @param array $form_state + * The Drupal form state. + * + * @return bool + * TRUE if we are currently on the last step, FALSE otherwise. + */ +function islandora_ingest_form_on_last_step(array &$form_state) { + $step_id = islandora_ingest_form_get_current_step_id($form_state); + $step_ids = array_keys(islandora_ingest_form_get_steps($form_state)); + $count = count($step_ids); + return array_search($step_id, $step_ids) == --$count; +} + /** * Defines the previous button for the ingest form. * + * Adds submit handlers for the form step if they exist. + * + * @param array $form_state + * The Drupal form state. + * * @return array * The previous button for the ingest form. */ -function islandora_ingest_form_previous_button() { +function islandora_ingest_form_previous_button(array &$form_state) { + // Before we move back to the previous step we should tell the previous step + // to undo whatever its submit handler did. + $prev_step_id = islandora_ingest_form_get_previous_step_id($form_state); + $prev_step = islandora_ingest_form_get_step($form_state, $prev_step_id); + $form_id = $prev_step['form_id']; + $submit_callback = $form_id . '_undo_submit'; + $submit = function_exists($submit_callback) ? array($submit_callback, 'islandora_ingest_form_previous_submit') : array('islandora_ingest_form_undo_submit'); return array( '#type' => 'submit', '#value' => t('Previous'), '#name' => 'prev', - '#submit' => array('islandora_ingest_form_previous_submit'), + '#submit' => $submit, // #limit_validation_errors, is why when the previous button is pressed no // values persisted in the form_state, but its also what allows us to go // back when validation errors occur. To have a better solution going @@ -194,23 +360,6 @@ function islandora_ingest_form_previous_button() { ); } -/** - * Get the current step. - * - * @param array $form_state - * The Drupal form state. - * @param string $step_id - * The ID of the step to get. - */ -function islandora_ingest_form_get_step_number(array $form_state, $step_id = NULL) { - if ($step_id === NULL) { - $step_id = islandora_ingest_form_get_step_id($form_state); - } - - $steps = array_flip(array_keys(islandora_ingest_form_get_steps($form_state))); - return $steps[$step_id]; -} - /** * The submit handler for the ingest form previous button. * @@ -219,16 +368,12 @@ function islandora_ingest_form_get_step_number(array $form_state, $step_id = NUL * Restores the form values for the previous step. * * @param array $form - * The drupal form. + * The Drupal form. * @param array $form_state - * The drupal form state. + * The Drupal form state. */ function islandora_ingest_form_previous_submit(array $form, array &$form_state) { - $step = islandora_ingest_form_get_step_number($form_state); - islandora_ingest_form_stash_info($form_state); - $step = max(array($step - 1, 0)); - $form_state['islandora']['step_id'] = islandora_ingest_form_get_step_id($form_state, $step); - islandora_ingest_form_grab_info($form_state); + islandora_ingest_form_decrement_step($form_state); $form_state['rebuild'] = TRUE; } @@ -237,11 +382,15 @@ function islandora_ingest_form_previous_submit(array $form, array &$form_state) * * Adds submit/validate handlers for the form step if they exist. * + * @param array $form_state + * The Drupal form state. + * * @return array * The next button for the ingest form. */ -function islandora_ingest_form_next_button(array $step_info) { - $form_id = $step_info['form_id']; +function islandora_ingest_form_next_button(array &$form_state) { + $step = islandora_ingest_form_get_step($form_state); + $form_id = $step['form_id']; $validate_callback = $form_id . '_validate'; $validate = function_exists($validate_callback) ? array($validate_callback) : NULL; $submit_callback = $form_id . '_submit'; @@ -263,22 +412,20 @@ function islandora_ingest_form_next_button(array $step_info) { * Restores the form values for the next step if present. * * @param array $form - * The drupal form. + * The Drupal form. * @param array $form_state - * The drupal form state. + * The Drupal form state. */ function islandora_ingest_form_next_submit(array $form, array &$form_state) { - $step = islandora_ingest_form_get_step_number($form_state); - drupal_static_reset('islandora_ingest_form_get_steps'); - islandora_ingest_form_stash_info($form_state); - $step = min(array($step + 1, islandora_ingest_form_get_step_count($form_state) - 1)); - $form_state['islandora']['step_id'] = islandora_ingest_form_get_step_id($form_state, $step); - islandora_ingest_form_grab_info($form_state); + islandora_ingest_form_increment_step($form_state); $form_state['rebuild'] = TRUE; } /** * Push current info into the current step's storage. + * + * @param array $form_state + * The Drupal form state. */ function islandora_ingest_form_stash_info(array &$form_state) { $storage = &islandora_ingest_form_get_step_storage($form_state); @@ -288,13 +435,13 @@ function islandora_ingest_form_stash_info(array &$form_state) { /** * Pops the info for the given step from storage into the form_state. + * + * @param array $form_state + * The Drupal form state. */ function islandora_ingest_form_grab_info(array &$form_state) { $storage = islandora_ingest_form_get_step_storage($form_state); $form_state['values'] = isset($storage['values']) ? $storage['values'] : array(); - if (isset($storage['configuration'])) { - $form_state['islandora']['configuration'] = $storage['configuration']; - } } /** @@ -302,11 +449,15 @@ function islandora_ingest_form_grab_info(array &$form_state) { * * This button is only shown on the last page of the multi-page ingest form. * + * @param array $form_state + * The Drupal form state. + * * @return array * The ingest button for the ingest form. */ -function islandora_ingest_form_ingest_button(array $step_info) { - $form_id = $step_info['form_id']; +function islandora_ingest_form_ingest_button(array &$form_state) { + $step = islandora_ingest_form_get_step($form_state); + $form_id = $step['form_id']; $validate_callback = $form_id . '_validate'; $validate = function_exists($validate_callback) ? array($validate_callback) : NULL; $submit_callback = $form_id . '_submit'; @@ -326,9 +477,9 @@ function islandora_ingest_form_ingest_button(array $step_info) { * Attempts to ingest every object built by the previous steps. * * @param array $form - * The drupal form. + * The Drupal form. * @param array $form_state - * The drupal form state. + * The Drupal form state. */ function islandora_ingest_form_submit(array $form, array &$form_state) { foreach ($form_state['islandora']['objects'] as $object) { @@ -345,159 +496,94 @@ function islandora_ingest_form_submit(array $form, array &$form_state) { } } -/** - * Gets the configuration used to create the multi-page ingest form. - * - * @param array $form_state - * The drupal form state. - * - * @return array - * The configuration used to generate the multi-page ingest forms. - */ -function &islandora_ingest_form_get_configuration(array &$form_state) { - return $form_state['islandora']['configuration']; -} - /** * Gets a reference to the stored NewFedoraObject's. * * @param array $form_state - * The drupal form state. + * The Drupal form state. * * @return array * A reference to the stored NewFedoraObjects to be ingested when the final * step submits. */ -function &islandora_ingest_form_get_objects(array $form_state) { +function &islandora_ingest_form_get_objects(array &$form_state) { return $form_state['islandora']['objects']; } /** * Gets a single object from the stored NewFedoraObject's. * - * @note - In our current use case we are only dealing with a single object + * @note In our current use case we are only dealing with a single object * ingest, this makes it convenient to access it. Ideally the steps * implementations will be abstracted to be indifferent to what object it's * currently working on. This will act as a placeholder for such * functionality. * * @param array $form_state - * The drupal form state. + * The Drupal form state. * * @return NewFedoraObject * Returns the 'current' object in the array of NewFedoraObjects, generally * this is only used when there is one object in the list of objects. */ -function islandora_ingest_form_get_object(array $form_state) { +function islandora_ingest_form_get_object(array &$form_state) { $objects = &islandora_ingest_form_get_objects($form_state); return current($objects); } -/** - * Get a reference to the current step index. - * - * @param array $form_state - * The drupal form state. - * - * @return string - * The current step index. - */ -function islandora_ingest_form_get_step_id(array &$form_state, $step_number = NULL) { - if ($step_number === NULL) { - return $form_state['islandora']['step_id']; - } - else { - $steps = array_keys(islandora_ingest_form_get_steps($form_state)); - return $steps[$step_number]; - } -} - -/** - * Get the step info of the given step or the current step if none is given. - * - * @param array $form_state - * The drupal form state. - * @param int $step - * The index of the step to get. - * - * @return int - * The step info of the requested step if found, NULL otherwise. - */ -function islandora_ingest_form_get_step_info(array &$form_state, $step = NULL) { - $step = isset($step) ? $step : islandora_ingest_form_get_step_id($form_state); - $steps = islandora_ingest_form_get_steps($form_state); - if (!empty($steps[$step])) { - return $steps[$step]; - } - // @todo deal with unknown case. - return NULL; -} - /** * Get general storage for the given/current step. * * @param array $form_state - * The drupal form state. - * @param string $step_id + * The Drupal form state. + * @param array $step_id * The ID of the step. + * + * @return array + * The given/current step storage if found, NULL otherwise. */ function &islandora_ingest_form_get_step_storage(array &$form_state, $step_id = NULL) { - if ($step_id === NULL) { - $step_id = islandora_ingest_form_get_step_id($form_state); - } - - if (!isset($form_state['islandora']['step_storage'][$step_id])) { - $form_state['islandora']['step_storage'][$step_id] = array(); + $step_id = isset($step_id) ? $step_id : islandora_ingest_form_get_current_step_id($form_state); + if (isset($step_id)) { + if (!isset($form_state['islandora']['step_storage'][$step_id])) { + $form_state['islandora']['step_storage'][$step_id] = array(); + } + return $form_state['islandora']['step_storage'][$step_id]; } - - return $form_state['islandora']['step_storage'][$step_id]; + return NULL; } /** - * Get a reference to the steps stored in the form state. + * Gets the configuration used to create the multi-page ingest form. * * @param array $form_state - * The drupal form state. + * The Drupal form state. * * @return array - * All the steps to be used in the ingest process. + * The configuration used to generate the multi-page ingest forms. */ -function islandora_ingest_form_get_steps(array &$form_state) { - return islandora_ingest_get_steps($form_state); +function &islandora_ingest_form_get_shared_storage(array &$form_state) { + return $form_state['islandora']['shared_storage']; } /** * Call form_load_include, for the current step if it defines a required file. * * @param array $form_state - * The drupal form state. + * The Drupal form state. */ -function islandora_ingest_form_step_form_load_include(array &$form_state) { +function islandora_ingest_form_load_include(array &$form_state) { form_load_include($form_state, 'inc', 'islandora', 'includes/ingest.form'); - $step_info = islandora_ingest_form_get_step_info($form_state); + $step = islandora_ingest_form_get_step($form_state); // Load include files. - if (isset($step_info['file']) && isset($step_info['module'])) { + if (isset($step['file']) && isset($step['module'])) { $matches = array(); - preg_match('/^(.*)\.(.*)$/', $step_info['file'], $matches); + preg_match('/^(.*)\.(.*)$/', $step['file'], $matches); list($file, $name, $type) = $matches; - form_load_include($form_state, $type, $step_info['module'], $name); + form_load_include($form_state, $type, $step['module'], $name); } } -/** - * Get the number of steps. - * - * @param array $form_state - * The drupal form state. - * - * @return int - * The number of steps. - */ -function islandora_ingest_form_get_step_count(array $form_state) { - $steps = islandora_ingest_form_get_steps($form_state); - return count($steps); -} - /** * Buildes the initial list of ingest steps. * @@ -512,40 +598,26 @@ function islandora_ingest_form_get_step_count(array $form_state) { * The list of sorted ingest steps as defined by all implementers * of ISLANDORA_INGEST_STEP_HOOK. */ -function &islandora_ingest_get_steps(array &$form_state) { +function islandora_ingest_form_get_steps(array &$form_state) { module_load_include('inc', 'islandora', 'includes/utilities'); $steps = &drupal_static(__FUNCTION__); - - if (!isset($steps)) { - $steps = array(); - $configuration = islandora_ingest_form_get_configuration($form_state); - $models = array(); - if (isset($configuration['selected_models'])) { - $models = $configuration['selected_models']; - } - elseif (isset($configuration['models'])) { - $models = $configuration['models']; - } - - foreach (islandora_build_hook_list(ISLANDORA_INGEST_STEP_HOOK, $models) as $hook) { - // Need to pass by ref... But don't really want an alter. - foreach (module_implements($hook) as $module) { - $function = $module . '_' . $hook; - $module_steps = (array) $function($form_state); - $steps = array_merge($steps, $module_steps); - } - } - - // Remove NULL values. - $steps = array_filter($steps); - uasort($steps, 'drupal_sort_weight'); - foreach (islandora_build_hook_list(ISLANDORA_INGEST_STEP_HOOK, $models) as $hook) { - $form_state_copy = $form_state; - drupal_alter($hook, $steps, $form_state_copy); + if (isset($steps)) { + return $steps; + } + $steps = array(); + $shared_storage = &islandora_ingest_form_get_shared_storage($form_state); + foreach (islandora_build_hook_list(ISLANDORA_INGEST_STEP_HOOK, $shared_storage['models']) as $hook) { + // Required for pass by reference. + foreach (module_implements($hook) as $module) { + $function = $module . '_' . $hook; + $module_steps = (array) $function($form_state); + $steps = array_merge($steps, $module_steps); } - - uasort($steps, 'drupal_sort_weight'); } - + $steps = array_filter($steps); + foreach (islandora_build_hook_list(ISLANDORA_INGEST_STEP_HOOK, $shared_storage['models']) as $hook) { + drupal_alter($hook, $steps, $form_state); + } + uasort($steps, 'drupal_sort_weight'); return $steps; } diff --git a/includes/ingest.menu.inc b/includes/ingest.menu.inc index ca4f6156..a6f79891 100644 --- a/includes/ingest.menu.inc +++ b/includes/ingest.menu.inc @@ -12,19 +12,20 @@ * HTML representing the mult-page ingest form. */ function islandora_ingest_callback() { - $configuration = islandora_ingest_get_configuration(); - if ($configuration === FALSE) { - drupal_set_message(t('Invalid ingest configuration.'), 'error'); + module_load_include('inc', 'islandora', 'includes/ingest.form'); + try { + $configuration = islandora_ingest_get_configuration(); + return drupal_get_form('islandora_ingest_form', $configuration); + } + catch(Exception $e) { $redirect = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; // Redirect back to referer or top level collection. drupal_goto($redirect); } - module_load_include('inc', 'islandora', 'includes/ingest.form'); - return drupal_get_form('islandora_ingest_form', $configuration); } /** - * Fetches/validates the ingest configuration from the $_GET parameters. + * Fetches the ingest configuration from the $_GET parameters. * * Generic parameters as accepted by all ingest processes, other modules may * add to this list. @@ -44,22 +45,5 @@ function islandora_ingest_get_configuration() { foreach ($convert_to_array_keys as $key) { $configuration[$key] = explode(',', $configuration[$key]); } - // @todo add hook for manipulating/validating the configuration. - return islandora_valid_ingest_configuration($configuration) ? $configuration : FALSE; -} - -/** - * Validates the given ingest configuration. - * - * At the moment it requires that models and collections are present. - * - * @param array $configuration - * The key value pairs that are used to build the multi-paged ingest process. - * - * @return bool - * TRUE if the configuration is valid, FALSE otherwise. - */ -function islandora_valid_ingest_configuration(array $configuration) { - // @todo Add more robust validation, add watchdog logs, etc. - return isset($configuration['models']) && isset($configuration['collections']); + return $configuration; }