diff --git a/includes/ingest.form.inc b/includes/ingest.form.inc index c285c5a1..e2e97d5f 100644 --- a/includes/ingest.form.inc +++ b/includes/ingest.form.inc @@ -31,8 +31,15 @@ * The form definition of the current step. */ function islandora_ingest_form(array $form, array &$form_state, array $configuration) { - islandora_ingest_form_init_form_state_storage($form_state, $configuration); - return islandora_ingest_form_execute_step($form, $form_state); + try { + islandora_ingest_form_init_form_state_storage($form_state, $configuration); + return islandora_ingest_form_execute_step($form, $form_state); + } + catch(Exception $e) { + drupal_set_message($e->getMessage(), 'error'); + return array(array( + '#markup' => l(t('Back'), 'javascript:window.history.back();', array('external' => TRUE)))); + } } /** @@ -42,21 +49,16 @@ function islandora_ingest_form(array $form, array &$form_state, array $configura * * @todo Add hook for manipulating/validating the configuration. * + * @see islandora_ingest_form() + * * @throws InvalidArgumentException * * @param array $configuration * 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_validiate_configuration(array $configuration) { +function islandora_ingest_form_validate_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); + throw new InvalidArgumentException('Ingest configuration not vaild, no models were given'); } } @@ -71,8 +73,8 @@ function islandora_ingest_form_validiate_configuration(array $configuration) { */ function islandora_ingest_form_init_form_state_storage(array &$form_state, array $configuration) { if (empty($form_state['islandora'])) { - // Validate the configuration before using it. - islandora_ingest_form_validiate_configuration($configuration); + // Validate the configuration before we use it. + islandora_ingest_form_validate_configuration($configuration); $object = islandora_ingest_form_prepare_new_object($configuration); $form_state['islandora'] = array( 'step_id' => NULL, @@ -233,12 +235,19 @@ function islandora_ingest_form_decrement_step(array &$form_state) { * The list of key/value pairs of configuration. */ function islandora_ingest_get_approximate_steps(array $configuration) { - $fake_form_state = array( + try { + islandora_ingest_form_validate_configuration($configuration); + } + catch(InvalidArgumentException $e) { + // Don't log or display exception. + return array(); + } + $stubbed_form_state = array( 'islandora' => array( 'shared_storage' => $configuration, ), ); - $steps = islandora_ingest_form_get_steps($fake_form_state); + $steps = islandora_ingest_form_get_steps($stubbed_form_state); drupal_static_reset('islandora_ingest_form_get_steps'); return $steps; } diff --git a/includes/utilities.inc b/includes/utilities.inc index 3efb6f28..f061f72c 100644 --- a/includes/utilities.inc +++ b/includes/utilities.inc @@ -79,6 +79,19 @@ function islandora_is_valid_pid($pid) { return drupal_strlen(trim($pid)) <= 64 && preg_match('/^([A-Za-z0-9]|-|\.)+:(([A-Za-z0-9])|-|\.|~|_|(%[0-9A-F]{2}))+$/', trim($pid)); } +/** + * Checks if the given namespace is valid. + * + * @param string $namespace + * The namespace to check without the ":" character. + * + * @return bool + * TRUE if valid, FALSE otherwise. + */ +function islandora_is_valid_namespace($namespace) { + return drupal_strlen(trim($namespace)) <= 64 && preg_match('/^([A-Za-z0-9]|-|\.)+$/', trim($namespace)); +} + /** * Checks if the given datastream id is valid. * @@ -673,3 +686,55 @@ function islandora_user_access_any(array $perms, $account = NULL) { } return FALSE; } + +/** + * Gets the list of allowed namespaces as defined by 'islandora_pids_allowed'. + * + * @return array + * The list of namespaces striped of trailing ":" characters. + */ +function islandora_get_allowed_namespaces() { + $matches = array(); + $allowed_namespaces = variable_get('islandora_pids_allowed', 'default: demo: changeme: islandora:'); + preg_match_all('/([A-Za-z0-9-\.]+):/', $allowed_namespaces, $matches); + return $matches[1]; +} + +/** + * Gets a list of all existing content models. + * + * If 'islandora_namespace_restriction_enforced' is set to true only return + * content models in the allowed namespace. + * + * @param bool $ignore_system_namespace + * Ignore content models in the 'fedora-system' namespace. + * + * @return array + * An associative array of all existing content models. + * - pid: The PID of the content model object. + * - pid: The PID of the content model object. + * - label: The label of the content model object. + */ +function islandora_get_content_models($ignore_system_namespace = TRUE) { + module_load_include('inc', 'islandora', 'includes/utilities'); + $tuque = islandora_get_tuque_connection(); + $query = 'select $object $label from <#ri> + where ($object $label + and ($object + or $object ) + and $object ) + order by $label'; + $content_models = array(); + $results = $tuque->repository->ri->itqlQuery($query, 'unlimited'); + foreach ($results as $result) { + $content_model = $result['object']['value']; + $label = $result['label']['value']; + $namespace = islandora_get_namespace($content_model); + $ignore = $ignore_system_namespace && $namespace == 'fedora-system'; + $ignore |= !islandora_namespace_accessible($namespace); + if (!$ignore) { + $content_models[$content_model] = array('pid' => $content_model, 'label' => $label); + } + } + return $content_models; +} diff --git a/islandora.module b/islandora.module index df04fe7e..94aedbe0 100644 --- a/islandora.module +++ b/islandora.module @@ -229,6 +229,15 @@ function islandora_menu() { 'access arguments' => array(FEDORA_PURGE, 2, 4), 'load arguments' => array(2), ); + $items['islandora/object/%islandora_object/print'] = array( + 'title' => 'Print Object', + 'page callback' => 'islandora_print_object', + 'page arguments' => array(2), + 'type' => MENU_CALLBACK, + 'access callback' => 'islandora_object_access_callback', + 'access arguments' => array(FEDORA_VIEW_OBJECTS, 2), + 'load arguments' => array(2), + ); $items['islandora/ingest'] = array( 'title' => 'Add an Object', 'page callback' => 'islandora_ingest_callback', @@ -272,6 +281,11 @@ function islandora_theme() { 'file' => 'includes/solution_packs.inc', 'render element' => 'form', ), + // Print object view. + 'islandora_object_print' => array( + 'file' => 'theme/theme.inc', + 'variables' => array('object' => NULL, 'content' => array()), + ), ); } @@ -837,10 +851,18 @@ function islandora_add_object(NewFedoraObject &$object) { * An object to delete. * * @return bool - * The ingested FedoraObject, after running the pre/post ingest hooks. + * TRUE if successful, FALSE otherwise. */ function islandora_delete_object(FedoraObject &$object) { - return $object->repository->purgeObject($object->id); + try { + $object->repository->purgeObject($object->id); + $object = NULL; + return TRUE; + } + catch(Exception $e) { + // Exception message gets logged in Tuque Wrapper. + return FALSE; + } } /** @@ -929,3 +951,20 @@ function islandora_entity_property_info() { return $info; } + +/** + * Renders the print page for the given object. + * + * Modules can either implement preprocess functions to append content onto the + * 'content' variable, or override the display by providing a theme suggestion. + * + * @param FedoraObject $object + * The object. + * + * @return array + * A renderable array. + */ +function islandora_print_object(FedoraObject $object) { + drupal_set_title($object->label); + return theme('islandora_object_print', array('object' => $object)); +} diff --git a/theme/theme.inc b/theme/theme.inc index 1622d810..a37cda74 100644 --- a/theme/theme.inc +++ b/theme/theme.inc @@ -123,3 +123,41 @@ function islandora_preprocess_islandora_default(&$variables) { $variables['islandora_thumbnail_url'] = $base_url . '/islandora/object/' . $islandora_object->id . '/datastream/TN/view'; } } + +/** + * Implements hook_preprocess_theme(). + */ +function islandora_preprocess_islandora_object_print(array &$variables) { + // Apply the print CSS in non print context. + $only_print_media = function($o) { + return $o['media'] == 'print'; + }; + $css = array_filter(drupal_add_css(), $only_print_media); + foreach ($css as $data => $options) { + $options['media'] = 'all'; + drupal_add_css($data, $options); + } + // Allow modules to define their own theme suggestions for the given object. + // Suggestions are defined as islandora_object_print__CMODEL__PID. For + // example for the image object islandora:1234. + // islandora_object_print__islandora_imagecmodel + // islandora_object_print__islandora_imagecmodel__islandora_1234 + // would be valid theme suggestions. This step up does not support objects + // with multiple content models in which each content model provides a theme + // suggestion. + $object = $variables['object']; + $pid = strtolower(preg_replace('/[^a-zA-Z0-9_]/', '_', $object->id)); + $models = array_diff($object->models, array('fedora-system:FedoraObject-3.0')); + foreach ($models as $model) { + $model = strtolower(preg_replace('/[^a-zA-Z0-9_]/', '_', $model)); + $suggestions = theme_get_suggestions(array($model, $pid), 'islandora_object_print'); + $variables['theme_hook_suggestions'] = array_merge($variables['theme_hook_suggestions'], $suggestions); + } +} + +/** + * Implements theme_hook(). + */ +function theme_islandora_object_print(array &$variables) { + return drupal_render($variables['content']); +}