0; } /** * 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. * @param array $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: * - id: The PID with which the object should be created. * - namespace: The PID namespace in which the object should be created. * (id is used first, if it is given). * - label: The initial label for the object. Defaults to "New Object". * - collections: An array of collection PIDs, to which the new object should * be related. * - models: An array of content model PIDs, to which the new object might * subscribe * - parent: The parent of the child to be ingested. This is needed for XACML * to correctly apply the parent's POLICY to children. * * @return array * The form definition of the current step. */ function islandora_ingest_form(array $form, array &$form_state, array $configuration) { try { islandora_ingest_form_init_form_state_storage($form_state, $configuration); return islandora_ingest_form_execute_step($form, $form_state); } catch (Exception $e) { watchdog( 'islandora', 'Exception during ingest form processing with Message: "@exception", and Trace: @trace', array('@exception' => $e->getMessage(), '@trace' => $e->getTraceAsString()), WATCHDOG_ERROR ); drupal_set_message($e->getMessage(), 'error'); return array(array( '#markup' => l(t('Back'), 'javascript:window.history.back();', array('external' => TRUE)))); } } /** * Initializes the form_state storage for use in the ingest multi-page forms. * * @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. */ function islandora_ingest_form_init_form_state_storage(array &$form_state, array $configuration) { if (empty($form_state['islandora'])) { $configuration['models'] = isset($configuration['models']) ? $configuration['models'] : array(); // Make sure the models actually exist. foreach ($configuration['models'] as $key => $model) { if (!islandora_object_load($model)) { unset($configuration['models'][$key]); } } // Required for step hooks. $form_state['islandora'] = array( 'step_id' => NULL, 'objects' => $configuration['objects'], 'shared_storage' => $configuration, 'step_storage' => array(), ); // No need to persist the 'objects' within the configuration. unset($configuration['objects']); // Must be called after $form_state['islandora'] is initialized, otherwise, // the values in 'islandora' would not be availible to the step hooks. $form_state['islandora']['step_id'] = islandora_ingest_form_get_first_step_id($form_state); } } /** * Get the first step ID. * * @param array $form_state * The Drupal form state. * * @return string * The 'id' of the very first step. */ function islandora_ingest_form_get_first_step_id(array &$form_state) { $steps = islandora_ingest_form_get_steps($form_state); $keys = array_keys($steps); return array_shift($keys); } /** * Get the last step ID. * * @param array $form_state * The Drupal form state. * * @return string * The 'id' of the very last step. */ function islandora_ingest_form_get_last_step_id(array &$form_state) { $steps = islandora_ingest_form_get_steps($form_state); $keys = array_keys($steps); return array_pop($keys); } /** * Gets the given/current step. * * If the current step is not defined it's assumed that all steps have executed. * * @param array $form_state * The Drupal form state. * @param string $step_id * The ID of the step. The current step is returned if no step ID is given. * * @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; } /** * Gets the next step. * * If the current step is not defined it's assumed that all steps have executed. * * @param array $form_state * The Drupal form state. * @param array $step * The step relative to the result, if not provided the current step is used. * * @return array|null * The next step if found, NULL otherwise. */ function islandora_ingest_form_get_next_step(array &$form_state, array $step = NULL) { $step = isset($step) ? $step : islandora_ingest_form_get_step($form_state); $next_step_id = islandora_ingest_form_get_next_step_id($form_state, $step['id']); return isset($next_step_id) ? islandora_ingest_form_get_step($form_state, $next_step_id) : NULL; } /** * Gets the previous step. * * If the current step is not defined it's assumed that all steps have executed. * * @param array $form_state * The Drupal form state. * @param array $step * The step relative to the result, if not provided the current step is used. * * @return array|null * The next step if found, NULL otherwise. */ function islandora_ingest_form_get_previous_step(array &$form_state, array $step = NULL) { $step = isset($step) ? $step : islandora_ingest_form_get_step($form_state); $previous_step_id = islandora_ingest_form_get_previous_step_id($form_state, $step['id']); return isset($previous_step_id) ? islandora_ingest_form_get_step($form_state, $previous_step_id) : NULL; } /** * Gets the ID of the current step. * * If the current step is not defined it's assumed that all steps have executed. * * @param array $form_state * The Drupal form state. * * @return string * The step ID. */ function islandora_ingest_form_get_current_step_id(array &$form_state) { return $form_state['islandora']['step_id']; } /** * Gets the ID of the next step. * * If the current step is not defined it's assumed that all steps have executed. * * @param array $form_state * The Drupal form state. * @param string $step_id * The ID of the step relative to the result, if not provided the current * step_id is used. * * @return string * The next step ID if found, NULL otherwise. */ function islandora_ingest_form_get_next_step_id(array &$form_state, $step_id = NULL) { $step_id = isset($step_id) ? $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 the current step is not defined it's assumed that all steps have executed. * In such cases the last step will be returned. * * @param array $form_state * The Drupal form state. * @param string $step_id * The ID of the step relative to the result, if not provided the current * step_id is used. * * @return string * The previous step ID if found, NULL otherwise. */ function islandora_ingest_form_get_previous_step_id(array &$form_state, $step_id = NULL) { $step_id = isset($step_id) ? $step_id : islandora_ingest_form_get_current_step_id($form_state); // If the current step is not defined it's assumed that all steps have // executed. So return the very last step. if ($step_id == NULL) { return islandora_ingest_form_get_last_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); 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); // Don't decrement passed the first step. 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); } } /** * Executes the current step. * * Builds the form definition and appends on any additonal elements required * for the step to function. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. * @param string $step_id * The ID of the step relative to the result, if not provided the current * step_id is used. * * @return array * The form definition of the current step. */ function islandora_ingest_form_execute_step(array $form, array &$form_state, $step_id = NULL) { // Load any required files for the current step. islandora_ingest_form_load_include($form_state); $step = isset($step_id) ? islandora_ingest_form_get_step($form_state) : islandora_ingest_form_get_step($form_state, $step_id); switch ($step['type']) { case 'callback': // Execute all the consecutive callbacks, and move then attempt to process // the next step. islandora_ingest_form_execute_consecutive_callback_steps($form, $form_state, $step); return islandora_ingest_form_execute_step($form, $form_state); case 'form': return islandora_ingest_form_execute_form_step($form, $form_state, $step); case 'batch': // @todo Implement if possible. break; } return array(); } /** * Execute the given 'form' step. * * Assumes the given step is a 'form' step. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. * @param array $step * The step we are executing. * * @return array * The form definition of the given step. */ function islandora_ingest_form_execute_form_step(array $form, array &$form_state, array $step) { $args = array($form, &$form_state); $args = isset($step['args']) ? array_merge($args, $step['args']) : $args; $shared_storage = islandora_ingest_form_get_shared_storage($form_state); // Build the form step. $form = call_user_func_array($step['form_id'], $args); // Since the list of steps depends on the shared storage we will rebuild the // list of steps if the shared storage has changed. This must be done before // stepifying, so the prev/next buttons get updated. if ($shared_storage != islandora_ingest_form_get_shared_storage($form_state)) { drupal_static_reset('islandora_ingest_form_get_steps'); } return islandora_ingest_form_stepify($form, $form_state, $step); } /** * Execute the given 'callback' step and any consecutive 'callback' steps. * * Assumes the given step is a 'callback' step. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. * @param array $step * The step that execution begins from. */ function islandora_ingest_form_execute_consecutive_callback_steps(array $form, array &$form_state, array $step) { do { islandora_ingest_form_execute_callback_step($form, $form_state, $step); islandora_ingest_form_increment_step($form_state); $step = islandora_ingest_form_get_step($form_state); } while (isset($step) && $step['type'] == 'callback'); } /** * Execute the given 'callback' step. * * Assumes the given step is a 'callback' step. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. * @param array $step * The step currently being executed. */ function islandora_ingest_form_execute_callback_step(array $form, array &$form_state, array $step) { $args = array(&$form_state); $args = isset($step['do_function']['args']) ? array_merge($args, $step['do_function']['args']) : $args; if (isset($step['do_function']['file'])) { require_once drupal_get_path('module', $step['module']) . "/" . $step['do_function']['file']; } call_user_func_array($step['do_function']['function'], $args); } /** * Undo the given 'callback' step and any consecutive 'callback' steps. * * Assumes the given $step is a 'callback' step. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. * @param array $step * The step that execution begins from. */ function islandora_ingest_form_undo_consecutive_callback_steps(array $form, array &$form_state, array $step) { do { islandora_ingest_form_undo_callback_step($form, $form_state, $step); islandora_ingest_form_decrement_step($form_state); $step = islandora_ingest_form_get_step($form_state); } while (isset($step) && $step['type'] == 'callback'); } /** * Undo the given 'callback' step. * * Assumes the given $step is a 'callback' step. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. * @param array $step * The step which the undo callback is being called on. */ function islandora_ingest_form_undo_callback_step(array $form, array &$form_state, array $step) { if (isset($step['undo_function'])) { $args = array(&$form_state); $args = isset($step['undo_function']['args']) ? array_merge($args, $step['undo_function']['args']) : $args; if (isset($step['undo_function']['file'])) { require_once drupal_get_path('module', $step['module']) . "/" . $step['undo_function']['file']; } call_user_func_array($step['undo_function']['function'], $args); } } /** * Append Prev/Next buttons submit/validation handlers etc. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. * @param array $step * An array defining a ingest step. * * @return array * The stepified Drupal form definition for the given step. */ function islandora_ingest_form_stepify(array $form, array &$form_state, array $step) { $first_form_step = islandora_ingest_form_on_first_form_step($form_state); $last_form_step = islandora_ingest_form_on_last_form_step($form_state); $form['form_step_id'] = array( '#type' => 'hidden', '#value' => islandora_ingest_form_get_current_step_id($form_state), ); $form['prev'] = $first_form_step ? NULL : islandora_ingest_form_previous_button($form_state); $form['next'] = $last_form_step ? islandora_ingest_form_ingest_button($form_state) : islandora_ingest_form_next_button($form_state); // Duplicate next button and hide it at the top of the form so that the form // always triggers "next" if the user submits the form with the enter key. if (!is_null($form['prev'])) { $form['hidden_next'] = $form['next']; $form['hidden_next']['#weight'] = -99; $form['hidden_next']['#prefix'] = '
'; } if (variable_get('islandora_render_context_ingeststep', FALSE)) { // Add active CModel header to the form. islandora_ingest_form_add_step_context($form, $form_state); } // Allow for a hook_form_FORM_ID_alter(). drupal_alter(array('form_' . $step['form_id'], 'form'), $form, $form_state, $step['form_id']); return $form; } /** * Append CModel label(s) to the top of the step's form. * * @param array $form * The Drupal form. * @param array $form_state * The Drupal form state. */ function islandora_ingest_form_add_step_context(array &$form, array $form_state) { $items = array(); $get_label = function(AbstractObject $collection = NULL, AbstractObject $fedora_cmodel) { if (isset($collection['COLLECTION_POLICY'])) { $policy = new CollectionPolicy($collection['COLLECTION_POLICY']->content); $policy_content_models = $policy->getContentModels(); } return isset($policy_content_models[$fedora_cmodel->id]) ? $policy_content_models[$fedora_cmodel->id]['name'] : $fedora_cmodel->label; }; $shared_storage = islandora_ingest_form_get_shared_storage($form_state); // Ingest steps also work for newspaper children and // pages of books which don't pass a collection object // and of course don't use collection policies. if (!isset($shared_storage['collection'])) { $shared_storage['collection'] = NULL; } foreach ($shared_storage['models'] as $key => $value) { $fedora_cmodel = islandora_object_load($value); if ($fedora_cmodel) { if (is_array($shared_storage['collection'])) { // Form state config allows for multiple collection objects. foreach ($shared_storage['collection'] as $collection) { $items[$value] = $get_label($collection, $fedora_cmodel); } } else { $items[$value] = $get_label($shared_storage['collection'], $fedora_cmodel); } } } switch (count($items)) { case 0: // No-op; no element. break; case 1: $label = reset($items); $form["islandora-ingest-form-step-context"] = array( '#markup' => "