Nits
[mkdru-moved-to-drupal.org.git] / mkdru.module
index 24e36c2..9610cd0 100644 (file)
@@ -71,6 +71,31 @@ function mkdru_access($op, $node, $account) {
 * Implements hook_menu()
 */
 function mkdru_menu() {
+  // Can't get tabbed menus to work in D6. The children work and the tabs
+  // show up there but the top level's url results in it trying to find
+  // the system module's system.inc in mkdru's directory
+//   $items['admin/settings/mkdru'] = array(
+//     'title' => 'Pazpar2 Metasearch Settings',
+//     'description' => 'Settings for mkdru.',
+//     'access arguments' => array('administer site configuration'),
+//   );
+//   $items['admin/settings/mkdru/settings'] = array(
+//     'title' => 'Settings',
+//     'description' => 'Settings for mkdru.',
+//     'page callback' => 'drupal_get_form',
+//     'page arguments' => array('mkdru_admin_settings'),
+//     'type' => MENU_DEFAULT_LOCAL_TASK,
+//     'file' => 'mkdru.admin.inc',
+//     'weight' => 0,
+//   );
+//   $items['admin/settings/mkdru/ding'] = array(
+//     'title' => 'Ding Integration',
+//     'description' => 'Search settings for mkdru instance integrated into Ding.',
+//     'page callback' => 'drupal_get_form',
+//     'page arguments' => array('mkdru_ding_settings'),
+//     'type' => MENU_LOCAL_TASK,
+//     'weight' => 1,
+//   );
   $items['admin/settings/mkdru'] = array(
     'title' => 'Pazpar2 Metasearch Settings',
     'description' => 'Settings for mkdru.',
@@ -88,6 +113,11 @@ function mkdru_menu() {
     'access arguments' => array('administer site configuration'),
     'type' => MENU_NORMAL_ITEM,
   );
+  $items['ahah-mkdru-facet'] = array(
+    'page callback' => 'mkdru_add_facet_callback',
+    'access arguments' => array('create metasearch interface'),
+    'type' => MENU_CALLBACK,
+  );
   return $items;
 }
 
@@ -106,15 +136,17 @@ function mkdru_init() {
 // function mkdru_settings_form($form, &$form_state) {
 function mkdru_settings_form(&$form_state) {
   if (isset($form_state['values']['settings'])) {
-    $settings = $form_state['build_info']['args']['settings'];
+    $settings = $form_state['values']['settings'];
   }
-  else if (isset($form_state['build_info']['args']['settings'])) {
+  elseif (isset($form_state['build_info']['args']['settings'])) {
     $settings = $form_state['build_info']['args']['settings'];
   }
   else {
     $settings = variable_get('mkdru_defaults', NULL);
   }
 
+  $form['#cache'] = TRUE;
+
   $form['settings'] = array(
     '#tree' => TRUE,
   );
@@ -163,10 +195,8 @@ function mkdru_settings_form(&$form_state) {
     '#collapsible' => TRUE,
     '#collapsed' => FALSE
   );
-  if (empty($form_state['facet_names'])) {
-    $form_state['facet_names'] = array('source', 'author', 'subject');
-  }
-  foreach ($form_state['facet_names'] as $facet) {
+
+  foreach (array_keys($settings['facets']) as $facet) {
     $form['settings']['facets'][$facet]  = array(
       '#type' => 'fieldset',
       '#title' => $facet . ' ' . t('facet'),
@@ -206,32 +236,84 @@ function mkdru_settings_form(&$form_state) {
       '#size' => 3,
       '#maxlength' => 3,
     );
+    $form['settings']['facets'][$facet]['remove'] = array(
+      '#type' => 'submit',
+      '#value' => t('Remove facet'),
+      '#mkdru facet' => $facet,
+      '#submit' => array('mkdru_remove_facet_submit'),
+      '#ahah' => array(
+        'path' => 'ahah-mkdru-facet',
+        'wrapper' => 'mkdru-facets-form-wrapper',
+      ),
+    );
   }
-//   $form['new_facet']  = array(
-//     '#type' => 'fieldset',
-//     '#title' => t('Add new facet...'),
-//     '#tree' => TRUE,
-//     '#collapsible' => TRUE,
-//     '#collapsed' => FALSE
-//   );
-//   $form['new_facet']['canonical'] = array(
-//     '#type' => 'textfield',
-//     '#title' => t('Canonical name of new facet'),
-//   );
-//   $form['new_facet']['button'] = array(
-//     '#type' => 'submit',
-//     '#value' => t('Add facet'),
-//     '#description' => t('Configure additional facets based on Pazpar2/SP termlists'),
-//     '#weight' => 1,
-//     '#submit' => array('mkdru_add_facet_form'),
-//     '#ajax' => array(
-//       'callback' => 'mkdru_add_facet_callback', 
-//       'wrapper' => 'mkdru-facets-form-wrapper',
-//     ),
-//   );
+  $form['new_facet']  = array(
+    '#type' => 'fieldset',
+    '#title' => t('Add new facet...'),
+    '#tree' => TRUE,
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE
+  );
+  $form['new_facet']['canonical'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Canonical name of new facet'),
+  );
+  $form['new_facet']['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Add facet'),
+    '#description' => t('Configure additional facets based on Pazpar2/SP termlists'),
+    '#submit' => array('mkdru_add_facet_submit'),
+    '#ahah' => array(
+      'path' => 'ahah-mkdru-facet',
+      'wrapper' => 'mkdru-facets-form-wrapper',
+    ),
+  );
   return $form;
 }
 
+function mkdru_add_facet_submit($form, &$form_state) {
+  $newfacet = $form_state['values']['new_facet']['canonical'];
+  $form_state['values']['settings']['facets'][$newfacet] = NULL;
+  $form_state['rebuild'] = TRUE;
+  return $form_state['values'];
+}
+
+function mkdru_remove_facet_submit($form, &$form_state) {
+  dpm($form_state);
+  $delfacet = $form_state['clicked_button']['#mkdru facet'];
+  if ($delfacet)
+    unset($form_state['values']['settings']['facets'][$delfacet]);
+}
+
+function mkdru_add_facet_callback() {
+  // Necessary to work with hook_form
+  module_load_include('inc', 'node', 'node.pages');
+
+  // Retrieve form from cache
+  $form_state = array('storage' => NULL, 'submitted' => FALSE);
+  $form_build_id = $_POST['form_build_id'];
+  $form = form_get_cache($form_build_id, $form_state);
+
+  // Run drupal_process_form to call submit handler and update $form_state
+  $args = $form['#parameters'];
+  $form_id = array_shift($args);
+  $form_state['post'] = $form['#post'] = $_POST;
+  $form['#programmed'] = $form['#redirect'] = FALSE;
+  drupal_process_form($form_id, $form, $form_state);
+
+  // Regenerate form so we can render the new facet part
+  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
+
+  // Choose subset of form to redraw.
+  $facet_form = $form['settings']['facets'];
+  // Prevent duplicate wrappers.
+  unset($facet_form['#prefix'], $facet_form['#suffix']);
+  $output = theme('status_messages') . drupal_render($facet_form);
+
+  // Final rendering callback
+  drupal_json(array('status' => TRUE, 'data' => $output));
+}
+
 
 
 // Ding config
@@ -246,7 +328,7 @@ function mkdru_ding_settings(&$form_state) {
   return $form;
 }
 function mkdru_ding_settings_submit($form, &$form_state) {
-  variable_set('mkdru_ding', $form_state[values][settings]);
+  variable_set('mkdru_ding', $form_state['values']['settings']);
   drupal_set_message(t('The configuration options have been saved.'));
 }
 
@@ -263,7 +345,6 @@ function mkdru_form(&$node, &$form_state) {
   }
 
   $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
-
   $type = node_get_types('type', $node);
   $form['title'] = array(
     '#type' => 'textfield',