From 682254727e82f75234b504935404377f0676c03c Mon Sep 17 00:00:00 2001 From: Alan Stanley Date: Tue, 22 Jan 2013 13:15:25 -0400 Subject: [PATCH] added ajax callback to admin page, guaranteed collection object --- fedora_repository.api.php | 2 +- fedora_repository.module | 119 ++++++++++++++++++++++++++++++++------ formClass.inc | 105 +++++++++++++++++++-------------- 3 files changed, 164 insertions(+), 62 deletions(-) diff --git a/fedora_repository.api.php b/fedora_repository.api.php index 190c9ec1..52552ab1 100644 --- a/fedora_repository.api.php +++ b/fedora_repository.api.php @@ -26,7 +26,7 @@ function hook_islandora_tabs($content_models, $pid, $page_number) { $tabset['A TAB'] = array( '#type' => 'tabpage', '#title' => t('A TITLE'), - '#content' => 'content') + '#content' => 'content' ); return $tabset; diff --git a/fedora_repository.module b/fedora_repository.module index 13038770..b1d5d688 100644 --- a/fedora_repository.module +++ b/fedora_repository.module @@ -1,4 +1,5 @@ createAdminForm(); + return $admin_form->createAdminForm(&$form_state); } /** @@ -325,7 +326,7 @@ function fedora_repository_purge_object_form(&$form_state, $pid, $referrer = NUL /* ALSO do $form definition here. Your final submit handler * (after user clicks Yes, I Confirm) * will only see $form_state info defined here. - * Form you create here passed as param1 to confirm_form*/ + * Form you create here passed as param1 to confirm_form */ // Had better luck leaving off last param 'name'. return confirm_form($form, 'Confirm Purge Object', $referrer, 'Are you sure you want to delete this object? This action cannot be undone.', 'Delete', 'Cancel'); @@ -531,7 +532,7 @@ function fedora_repository_purge_object_form_validate($form, &$form_state) { foreach ($content_models as $content_model) { if ($content_model->pid == 'islandora:collectionCModel') { $member_pids = get_related_items_as_array($pid, 'isMemberOfCollection', 10000, 0, FALSE); - if (is_array($member_pids) && ! empty($member_pids)) { + if (is_array($member_pids) && !empty($member_pids)) { form_set_error('new_collection_pid', t("Please purge all members of this collection before deleting the collection itself.")); return; } @@ -556,14 +557,14 @@ function fedora_repository_purge_object_form_submit($form, &$form_state) { if (!isset($form_state['storage']['confirm'])) { /* This will cause the form to be rebuilt, - * entering the confirm part of the form.*/ + * entering the confirm part of the form. */ $form_state['storage']['confirm'] = TRUE; // Along with this. $form_state['rebuild'] = TRUE; } else { /* This is where you do your processing after - * they have pressed the confirm button.*/ + * they have pressed the confirm button. */ $params = array( "pid" => $pid, "logMessage" => "Purged", @@ -771,14 +772,14 @@ function fedora_repository_replace_stream_form_submit($form, &$form_state) { $dsLabel = $form_state['values']['dsLabel']; $streamUrl = ($file !== NULL) ? - file_create_url($file->filepath): - url($form_state['values']['reference'], array('absolute' => TRUE)); + file_create_url($file->filepath) : + url($form_state['values']['reference'], array('absolute' => TRUE)); // Remove the original file extension from the label and add the new one // use strrpos to get the last dot if (($indexOfDot = strrpos($dsLabel, '.')) !== FALSE) { $dsLabel = substr($dsLabel, 0, $indexOfDot) . - substr($streamUrl, strrpos($streamUrl, '.')); // Add the file extention to the end of the label. + substr($streamUrl, strrpos($streamUrl, '.')); // Add the file extention to the end of the label. } $dformat = $form_state['storage']['mime_type']; @@ -901,7 +902,7 @@ function fedora_repository_edit_qdc_form_submit($form, &$form_state) { $client = $soap_helper->getSoapClient(variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M')); /* Check the content model for a custom - * edit metadata form submit function.*/ + * edit metadata form submit function. */ if (isset($form_state['values']['pid'])) { module_load_include('inc', 'fedora_repository', 'ContentModel'); if (($cm = ContentModel::loadFromObject($form_state['values']['pid'])) !== FALSE) { @@ -1003,9 +1004,9 @@ function fedora_repository_islandora_tabs($content_models, $pid, $page_number) { } else { $cmodels_tabs[$content_model->pid] = array( - '#type' => 'tabpage', - '#title' => $content_model->name, - '#content' => $content_model_fieldset, + '#type' => 'tabpage', + '#title' => $content_model->name, + '#content' => $content_model_fieldset, ); } } @@ -1621,10 +1622,10 @@ function fedora_repository_imagecache_default_presets() { function _fedora_repository_render_image($tn_path, $imagecache_preset = 'fedora_repository_collection_thumbnail', $truncated_title = '', $title = '') { $thumbnail = NULL; if ($thumbnail === NULL && - module_exists('imagecache_external') && - is_callable('theme_imagecache_external_image') && - variable_get('fedora_repository_use_imagecache_external_in_collection_view', FALSE) && - imagecache_external_can_fetch($tn_path, TRUE)) { + module_exists('imagecache_external') && + is_callable('theme_imagecache_external_image') && + variable_get('fedora_repository_use_imagecache_external_in_collection_view', FALSE) && + imagecache_external_can_fetch($tn_path, TRUE)) { $thumbnail = theme('imagecache_external_image', $imagecache_preset, $tn_path, $truncated_title, $title); } if ($thumbnail === NULL) { @@ -1755,3 +1756,87 @@ function fedora_repository_fedora_repository_check_perm($op, $pid, $user) { return NULL; } } + +function update_fedora_url_div() { + $form = islandora_callback_prep(); + $changed_elements = $form['fedora_ahah_wrapper']; + unset($changed_elements['#prefix'], $changed_elements['#suffix']); + $output = theme('status_messages') . drupal_render($changed_elements); + drupal_json(array( + 'status' => TRUE, + 'data' => $output, + )); +} + +function islandora_callback_prep() { + $form_state = array('storage' => NULL, 'submitted' => FALSE, 'rebuild' => TRUE); + $form_build_id = $_POST['form_build_id']; + $form = form_get_cache($form_build_id, $form_state); + $args = $form['#parameters']; + $form_id = array_shift($args); + $form_state['post'] = $form['#post'] = $_POST; + // Enable the submit/validate handlers to determine whether AHAH-submittted. + $form_state['ahah_submission'] = TRUE; + $form['#programmed'] = $form['#redirect'] = FALSE; + drupal_process_form($form_id, $form, $form_state); + $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id); + return $form; +} + +function islandora_fedora_settings_form_submit($form, &$form_state) { + + $op = isset($form_state['values']['op']) ? $form_state['values']['op'] : ''; + if ($form_state['ahah_submission'] == TRUE) { + $form_state['rebuild'] = TRUE; + return; + } + if ($form_state['clicked_button']['#id'] != 'edit-submit' && $op != t('Reset to defaults')) { + $form_state['rebuild'] = TRUE; + return; + } + + + // Exclude unnecessary elements. + unset($form_state['values']['submit'], $form_state['values']['reset'], $form_state['values']['form_id'], $form_state['values']['op'], $form_state['values']['form_token'], $form_state['values']['form_build_id']); + + foreach ($form_state['values'] as $key => $value) { + if ($op == t('Reset to defaults')) { + variable_del($key); + } + else { + if (is_array($value) && isset($form_state['values']['array_filter'])) { + $value = array_keys(array_filter($value)); + } + variable_set($key, $value); + } + } + + //set elements previously defined in config code. + //the elements are left in to allow for third party modules. + if ($op != t('Reset to defaults')) { + $fedora_base = variable_get('fedora_base_url', 'http://localhost:8080/fedora'); + variable_set('fedora_soap_url', $fedora_base . '/wsdl?api=API-A'); + variable_set('fedora_soap_manage_url', $fedora_base . '/wsdl?api=API-M'); + } + if ($op == t('Reset to defaults')) { + drupal_set_message(t('The configuration options have been reset to their default values.')); + } + else { + drupal_set_message(t('The Islandora configuration options have been saved.')); + } + + $root = variable_get('fedora_repository_pid', 'islandora:root'); + islandora_guarantee_root_collection($root); + cache_clear_all(); + drupal_rebuild_theme_registry(); +} + +function islandora_guarantee_root_collection($pid){ + module_load_include('inc', 'fedora_repository', 'api/fedora_item'); + $item = new Fedora_Item($pid); + if($item->exists()){ + return; + } + $item = Fedora_item::ingest_new_item($pid, 'A', 'Islandora Root Collection', 'admin'); + $item->add_relationship('hasModel', 'islandora:collectionCModel', FEDORA_MODEL_URI); +} \ No newline at end of file diff --git a/formClass.inc b/formClass.inc index 35878b11..5cfe17ef 100644 --- a/formClass.inc +++ b/formClass.inc @@ -158,6 +158,13 @@ class formClass { 'access arguments' => array('administer site configuration'), ); + $items['islandora/fedora/url'] = array( + 'page callback' => 'update_fedora_url_div', + 'access arguments' => array('view fedora collection'), + 'file' => 'fedora_repository.module', + 'type' => MENU_CALLBACK, + ); + return $items; } @@ -165,14 +172,25 @@ class formClass { * Create admin form * @return array */ - function createAdminForm() { + function createAdminForm(&$form_state) { if (!user_access('administer site configuration')) { drupal_set_message(t('You must be a site administrator to edit the Fedora collections list.'), 'error'); return; } module_load_include('inc', 'fedora_repository', 'api/fedora_utils'); module_load_include('inc', 'fedora_repository', 'ObjectHelper'); + $fedora_path = isset($form_state['values']['fedora_base_url']) ? $form_state['values']['fedora_base_url'] : variable_get('fedora_base_url', 'http://localhost:8080/fedora'); + $triplepath = "$fedora_path/risearch"; + $response = do_curl_ext($fedora_path . '/describe'); + $available = ($response != NULL) ? ($response[1] == 0) : FALSE; + + $confirmation_message = ($available ? '' + . t('Successfully connected to Fedora server at @fedora_soap_url', array('@fedora_soap_url' => $fedora_path)) : ' ' + . t('Unable to connect to Fedora server at @fedora_soap_url', array('@fedora_soap_url' => $fedora_path))); + $form = array(); + $form['fedora_repository_pid'] = array( '#type' => 'textfield', '#title' => t('Root Collection PID'), @@ -182,45 +200,41 @@ class formClass { '#weight' => -18 ); - $form['fedora_base_url'] = array( - '#type' => 'textfield', - '#title' => t('Fedora base URL'), - '#default_value' => variable_get('fedora_base_url', 'http://localhost:8080/fedora'), - '#description' => t('The URL to use for REST-type connections'), - '#required' => TRUE, - '#weight' => -16, - ); - $form['fedora_repository_url'] = array( - '#type' => 'textfield', - '#title' => t('Fedora RISearch URL'), - '#default_value' => variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch'), - '#description' => t('The url of the Fedora server'), '#required' => TRUE, - '#weight' => -14 +// new ahah stuff + + $form['fedora_ahah_wrapper'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#type' => 'markup', ); - $form['fedora_soap_url'] = array( + $form['fedora_ahah_wrapper']['fedora_base_url'] = array( '#type' => 'textfield', - '#title' => t('Fedora SOAP Url'), - '#default_value' => variable_get('fedora_soap_url', 'http://localhost:8080/fedora/wsdl?api=API-A'), - '#description' => t('The URL to use for SOAP connections'), - '#required' => TRUE, - '#weight' => -12, - '#suffix' => '

' . ( - fedora_available() ? - theme('image', 'misc/watchdog-ok.png') . t('Successfully connected to Fedora server at @fedora_soap_url', array('@fedora_soap_url' => variable_get('fedora_soap_url', ''))) : - theme('image', 'misc/watchdog-error.png') . t('Unable to connect to Fedora server at @fedora_soap_url', array('@fedora_soap_url' => variable_get('fedora_soap_url', '')))) . '

', + '#title' => t('Fedora base URL'), + '#default_value' => $fedora_path, + '#description' => t('The URL to use for REST-type connections'), + '#ahah' => array( + 'path' => 'islandora/fedora/url', + 'wrapper' => 'islandora-fedora-url', + 'effect' => 'fade', + 'event' => 'change'), ); - - $form['fedora_soap_manage_url'] = array( + $form['fedora_ahah_wrapper']['infobox'] = array( + '#type' => 'item', + '#value' => $confirmation_message, + ); + $form['fedora_ahah_wrapper']['fedora_repository_url'] = array( '#type' => 'textfield', - '#title' => t('Fedora SOAP management URL'), - '#default_value' => variable_get('fedora_soap_manage_url', 'http://localhost:8080/fedora/wsdl?api=API-M'), '#description' => t('The URL to use for SOAP API-M connections'), + '#title' => t('Fedora RISearch URL'), + '#default_value' => $triplepath, + '#description' => t('The url of the Fedora server\'s Triplestore. Change this only if you have set up an external triplestore'), '#required' => TRUE, - '#weight' => -10 + ); + // will allow admin user to remove namepsace restrictions if not explicitly disallowed in settings.php if (variable_get('allow_open_namespace', TRUE)) { $form['fedora_namespace'] = array( @@ -288,7 +302,7 @@ class formClass { if (array_key_exists('config', $profile)) { $config_path = $profile['config']; if (isset($config_path) && $config_path != "") { - $display_options[$machine_name] .= " (". l("config", $config_path, array()) .")"; + $display_options[$machine_name] .= " (" . l("config", $config_path, array()) . ")"; } } } @@ -322,14 +336,14 @@ class formClass { '#title' => t('Allow imagecache_external use for thumbnails in collection view'), '#default_value' => variable_get('fedora_repository_use_imagecache_external_in_collection_view', FALSE), '#description' => t('If enabled, the default collection list view (or ' . - 'anywhere the function "@function" is used) will try to use ' . - 'imagecache_external, defaulting to the "@preset" preset. XSLTs may ' . - 'be updated to use this function.', - array( - '@function' => '_fedora_repository_render_image()', - '@preset' => 'fedora_repository_collection_thumbnail', - '@xsl' => 'sparql_to_html.xsl', - )), + 'anywhere the function "@function" is used) will try to use ' . + 'imagecache_external, defaulting to the "@preset" preset. XSLTs may ' . + 'be updated to use this function.', + array( + '@function' => '_fedora_repository_render_image()', + '@preset' => 'fedora_repository_collection_thumbnail', + '@xsl' => 'sparql_to_html.xsl', + )), ); $form['advanced']['fedora_control_group_control_during_ingest'] = array( @@ -352,7 +366,7 @@ class formClass { '#type' => 'textfield', '#title' => t('Path to FITS executable'), '#description' => t("Path to FITS program on your server. It must be accessible by Drupal (i.e. apache user)."), - '#default_value' => variable_get('fits_path','/usr/local/bin/fits.sh'), + '#default_value' => variable_get('fits_path', '/usr/local/bin/fits.sh'), ); //Export functionality @@ -365,9 +379,13 @@ class formClass { ); $form['#attributes'] = array('enctype' => "multipart/form-data"); + $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save Configuration')); + $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults')); + $form['#submit'][] = 'islandora_fedora_settings_form_submit'; + $form['#theme'] = 'system_settings_form'; + return ($form); - - return system_settings_form($form); + // return system_settings_form($form); } /** @@ -422,8 +440,7 @@ class formClass { } // Save the title - if ( $label != null ) - { + if ($label != null) { $object = new Fedora_Item($form_values['pid']); $object->modify_object($label); }