Surround constants in quotes. Patch from Hasse.
[mkdru-moved-to-drupal.org.git] / mkdru.module
index b21b38b..72edab9 100644 (file)
 function mkdru_node_info() {
   return array(
     'mkdru' => array(
-      'name' => t("Pazpar2 metasearch interface"),
-      'base' => 'mkdru',
+      'name'        => t("Pazpar2 metasearch interface"),
+      'base'        => 'mkdru',
       'description' => t("Metasearch interface for Z39.50/SRU and other targets via a Pazpar2/Service Proxy backend"),
     )
   );
 }
 
-function mkdru_ting_search_show($params) {
-   $path = drupal_get_path('module', 'mkdru');
+/**
+ * Implements hook_search_info()
+ */
+function mkdru_search_info() {
+  return array(
+    'title'               => 'Meta search',
+    'path'                => 'meta',
+    'conditions_callback' => 'mkdru_search_conditions_callback',
+  );
+}
+
+/**
+ * Implements hook_search_page()
+ */
+function mkdru_search_page($results) {
+  $output['prefix']['#markup'] = theme('mkdru_results');
+  $output['suffix']['#markup'] = '';
+  return $output;
+}
+
+/**
+ * Implements hook_ding_facetbrowser()
+ */
+function mkdru_ding_facetbrowser() {
+       $results             = new stdClass();
+  $results->facets     = array();
+  $results->show_empty = TRUE; # Show an empty facetbrowser block, even if search didn't return any results
+  return $results;
+}
+
+/**
+ * Search callback function that is invoked by search_view()
+ */
+function mkdru_search_conditions_callback($keys) {}
+
+/**
+ * Implement hook_search_execute()
+ */
+function mkdru_search_execute($keys = NULL, $conditions = NULL) {
+  $path = drupal_get_path('module', 'mkdru');
   // Include client library.
   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js',
     array('type' => 'file', 'scope' => 'footer'));
   drupal_add_library('overlay', 'jquery-bbq');
+  drupal_add_js($path . '/recipe.js',
+    array('type' => 'file', 'scope' => 'footer'));
   drupal_add_js($path . '/mkdru.theme.js',
     array('type' => 'file', 'scope' => 'footer'));
   drupal_add_js($path . '/mkdru.client.js',
     array('type' => 'file', 'scope' => 'footer'));
-  $html = theme('mkdru_results');
-  drupal_add_js(array('mkdru' => 
-    array('use_sessions' => '1', 'query' => $params['keys']
-    )), 'setting');
-  return array("content" => $html);
+  drupal_add_js(array('mkdru' =>
+    array(
+      'use_sessions' => variable_get('use_sessions', '1'),
+      'pz2_path'     => variable_get('pz2_path', '/pazpar2/search.pz2'),
+      'sp_user'      => variable_get('sp_user', ''),
+      'sp_pass'      => variable_get('sp_pass', ''),
+      'query'        => $keys,
+    )
+  ), 'setting');
+
+  return array();
 }
 
 /**
@@ -79,8 +125,8 @@ function mkdru_node_access($node, $op, $account) {
 * Implements hook_menu()
 */
 function mkdru_menu() {
-  $items['admin/settings/mkdru'] = array(
-    'title' => 'mkdru Settings',
+  $items['admin/config/search/mkdru'] = array(
+    'title' => 'Configure Pazpar2 metasearch integration',
     'description' => 'Settings for mkdru.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('mkdru_admin_settings'),
@@ -88,6 +134,15 @@ function mkdru_menu() {
     'type' => MENU_NORMAL_ITEM,
     'file' => 'mkdru.admin.inc',
   );
+  $items['admin/config/search/mkdru/defaults'] = array(
+    'title' => 'Default settings for Pazpar2 metasearch integration',
+    'description' => 'Settings for mkdru.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('mkdru_admin_settings'),
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'mkdru.admin.inc',
+  );
   return $items;
 }
 
@@ -103,118 +158,187 @@ function mkdru_init() {
 
 
 
-// Node config
-/**
-* Implements hook_form()
-*/
-function mkdru_form(&$node, $form_state) {
-  $type = node_type_get_type($node);
+// Config form common to node and settings
+function mkdru_settings_form($form, &$form_state) {
+//   dpm($form);
+  dpm($form_state);
+//   dpm($settings);
+  if (isset($form_state['values']['settings'])) {
+    $settings = $form_state['values']['settings'];
+  }
+  else if (isset($form_state['build_info']['args']['settings'])) {
+    $settings = $form_state['build_info']['args']['settings'];
+  }
+  else {
+    $settings = variable_get('mkdru_defaults');
+  }
 
-  $form['title'] = array(
-    '#type' => 'textfield',
-    '#title' => check_plain($type->title_label),
-    '#required' => FALSE,
-    '#default_value' => $node->title,
-    '#weight' => -5
+  $form['settings'] = array(
+    '#tree' => TRUE,
   );
 
-  $form['search_settings']  = array(
-    '#type' => 'fieldset',
-    '#title' => t('Pazpar2/Service Proxy search settings'),
-    '#collapsible' => TRUE,
-    '#collapsed' => FALSE
-  );
-  $form['search_settings']['pz2_path'] = array(
+  $form['settings']['pz2_path'] = array(
     '#type' => 'textfield',
     '#title' => t('Pazpar2/Service Proxy path'),
     '#description' => t('Path that takes Pazpar2 commands via HTTP'),
     '#required' => TRUE,
-    '#default_value' => isset($node->mkdru->pz2_path) ? $node->mkdru->pz2_path : '/pazpar2/search.pz2',
+    '#default_value' => $settings['pz2_path'],
   );
-  $form['search_settings']['use_sessions'] = array(
+  $form['settings']['use_sessions'] = array(
     '#type' => 'checkbox',
     '#title' => t('Session handling'),
     '#description' => t('Disable for use with Service Proxy'),
-    '#default_value' => isset($node->mkdru->use_sessions) ? $node->mkdru->use_sessions : 1,
+    '#default_value' => $settings['use_sessions'],
   );
 
-  $form['sp_settings']  = array(
+  $form['settings']['sp']  = array(
     '#type' => 'fieldset',
     '#title' => t('Service Proxy specific settings'),
     '#collapsible' => TRUE,
     '#collapsed' => TRUE
   );
-  $form['sp_settings']['sp_user'] = array(
-    '#type' => 'textfield',
+  $form['settings']['sp']['user'] = array(
+    '#type' => 'password',
     '#title' => t('Service Proxy username'),
     '#description' => t('Service Proxy username'),
     '#required' => FALSE,
-    '#default_value' => isset($node->mkdru->sp_user) ? 
-      $node->mkdru->sp_user : '',
+    '#default_value' => $settings['sp']['user'],
   );
-  $form['sp_settings']['sp_pass'] = array(
+  $form['settings']['sp']['pass'] = array(
     '#type' => 'textfield',
     '#title' => t('Service Proxy password'),
     '#description' => t('Service Proxy password'),
     '#required' => FALSE,
-    '#default_value' => isset($node->mkdru->sp_pass) ? 
-      $node->mkdru->sp_pass : '',
+    '#default_value' => $settings['sp']['pass'],
   );
 
-  $form['display_settings']  = array(
+  $form['settings']['facets']  = array(
     '#type' => 'fieldset',
-    '#title' => t('Display settings'),
+    '#title' => t('Facet settings'),
+    // Set up the wrapper so that AJAX will be able to replace the fieldset.
+    '#prefix' => '<div id="mkdru-facets-form-wrapper">', 
+    '#suffix' => '</div>',
     '#collapsible' => TRUE,
     '#collapsed' => FALSE
   );
-  $form['display_settings']['source_max'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Number of sources to display'),
-    '#required' => TRUE,
-    '#default_value' => isset($node->mkdru->source_max) ? $node->mkdru->source_max : 10,
-    '#size' => 3,
-    '#maxlength' => 3,
+
+  foreach (array_keys($settings['facets']) as $facet) {
+    $form['settings']['facets'][$facet]  = array(
+      '#type' => 'fieldset',
+      '#title' => $facet . ' ' . t('facet'),
+      '#collapsible' => TRUE,
+      '#collapsed' => FALSE
+    );
+    $form['settings']['facets'][$facet]['displayName'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Facet name to display in UI'),
+      '#required' => TRUE,
+      '#default_value' => $settings['facets'][$facet]['displayName'],
+    );
+    $form['settings']['facets'][$facet]['pz2Name'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Name of termlist in Pazpar2'),
+      '#required' => TRUE,
+      '#default_value' => $settings['facets'][$facet]['pz2Name'],
+    );
+    $form['settings']['facets'][$facet]['limiter'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Query limiter string'),
+      '#default_value' => $settings['facets'][$facet]['limiter'],
+      '#size' => 5,
+    );
+    $form['settings']['facets'][$facet]['max'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Number of terms to display'),
+      '#required' => TRUE,
+      '#default_value' => $settings['facets'][$facet]['max'],
+      '#size' => 3,
+      '#maxlength' => 3,
+    );
+    $form['settings']['facets'][$facet]['orderWeight'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Facet weight'),
+      '#default_value' => $settings['facets'][$facet]['orderWeight'],
+      '#size' => 3,
+      '#maxlength' => 3,
+    );
+  }
+
+  $form['new_facet']  = array(
+    '#type' => 'fieldset',
+    '#title' => t('Add new facet...'),
+    '#tree' => TRUE,
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE
   );
-  $form['display_settings']['author_max'] = array(
+  $form['new_facet']['canonical'] = array(
     '#type' => 'textfield',
-    '#title' => t('Number of authors to display'),
-    '#required' => TRUE,
-    '#default_value' => isset($node->mkdru->author_max) ? $node->mkdru->author_max : 10,
-    '#size' => 3,
-    '#maxlength' => 3,
+    '#title' => t('Canonical name of new facet'),
   );
-  $form['display_settings']['subject_max'] = array(
+  $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',
+    ),
+  );
+  return $form;
+}
+
+
+// Node config
+/**
+* Implements hook_form()
+*/
+function mkdru_form(&$node, &$form_state) {
+  if (isset($node->mkdru->settings)) {
+    // Second decode parameter indicates associative array
+    $form_state['build_info']['args']['settings'] = json_decode($node->mkdru->settings, TRUE);
+  }
+  $form = drupal_retrieve_form('mkdru_settings_form', $form_state);
+  $type = node_type_get_type($node);
+  $form['title'] = array(
     '#type' => 'textfield',
-    '#title' => t('Number of subjects to display'),
-    '#required' => TRUE,
-    '#default_value' => isset($node->mkdru->subject_max) ? $node->mkdru->subject_max : 10,
-    '#size' => 3,
-    '#maxlength' => 3,
+    '#title' => check_plain($type->title_label),
+    '#required' => FALSE,
+    '#default_value' => $node->title,
+    '#weight' => -5
   );
   return $form;
 }
 
+function mkdru_add_facet_form($form, &$form_state) {
+  // TODO: validation
+  $newfacet = $form_state['values']['new_facet']['canonical'];
+  $form_state['values']['settings']['facets'][$newfacet] = NULL;
+  $form_state['rebuild'] = TRUE;
+}
+
+function mkdru_add_facet_callback($form, $form_state) {
+  return $form['settings']['facets'];
+}
 
 /**
 * Implements hook_validate()
 */
 function mkdru_validate($node) {
-  if (!is_numeric($node->source_max)) {
-    form_set_error('source_max', t('Please enter a number.'));
-  }
-  if (!is_numeric($node->author_max)) {
-    form_set_error('author_max', t('Please enter a number.'));
-  }
-  if (!is_numeric($node->subject_max)) {
-    form_set_error('subject_max', t('Please enter a number.'));
-  }
+// TODO: validation
+//   if (!is_numeric($node->source_max)) {
+//     form_set_error('source_max', t('Please enter a number.'));
+//   }
 }
 
 /**
 * Implements hook_insert().
 */
 function mkdru_insert($node) {
-  drupal_write_record('mkdru', $node);
+  $fields = array('nid' => $node->nid, 'vid' => $node->vid,
+            'settings' => json_encode($node->settings));
+  db_insert('mkdru')->fields($fields)->execute();
 }
 
 /**
@@ -226,7 +350,10 @@ function mkdru_update($node) {
     mkdru_insert($node);
   }
   else {
-    drupal_write_record('mkdru', $node, 'vid');
+    db_update('mkdru')
+      ->condition('vid', $node->vid)
+      ->fields(array('settings' => json_encode($node->settings)))
+      ->execute();
   }
 }
 
@@ -298,7 +425,7 @@ function theme_mkdru_js(&$variables) {
   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', array(
     'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
   // jQuery plugin for query string/history manipulation.
-  drupal_add_library('overlay', 'jquery-bbq');
+  drupal_add_library('system', 'jquery.bbq');
   drupal_add_js($path . '/mkdru.theme.js', array(
     'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
   drupal_add_js($path . '/mkdru.client.js', array(
@@ -335,7 +462,8 @@ function mkdru_view($node, $view_mode) {
 */
 function mkdru_block_info() {
   // facet blocks
-  $facets = variable_get('mkdru_facets');
+  $blocks = array();
+  $facets = variable_get('mkdru_facets', array());
   foreach ($facets as $facet) {
     // NB: block caching is redundant for static content
     $blocks['mkdru_facet_' . $facet]['info'] = "mkdru - $facet " . t('facet');