X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=mkdru.module;h=40bc0c461abb7abb30e1aa5bb19ddd995c8c3711;hb=refs%2Fheads%2F7.x;hp=8158bb0604620da236dcd9ec4d2f0ae4ce349564;hpb=9bb075c111afad0a4dc12d6b9bee8b21b532500c;p=mkdru-moved-to-drupal.org.git diff --git a/mkdru.module b/mkdru.module index 8158bb0..40bc0c4 100644 --- a/mkdru.module +++ b/mkdru.module @@ -5,31 +5,87 @@ // Module metainfo /** -* Implementation of hook_node_info() +* Implements hook_node_info() */ function mkdru_node_info() { return array( 'mkdru' => array( - 'name' => t("Z39.50/SRU metasearch interface"), - 'module' => '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"), - ) ); } /** -* Implementation of hook_perm() -*/ -function mkdru_perm() { - return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface'); + * 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; } /** -* Implementation of hook_access() + * 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) { + theme('mkdru_js', array('setting' => array('mkdru' => array( + 'settings' => json_encode(variable_get('mkdru_ding', NULL)), + 'query' => $keys)))); + return array(); +} + +/** +* Implements hook_permission() */ -function mkdru_access($op, $node, $account) { +function mkdru_permission() { + return array( + 'administer metasearch interfaces' => array( + 'title' => t('Administer Pazpar2 metasearch integration'), + ), + 'create metasearch interface' => array( + 'title' => t('Create metasearch interface'), + ), + 'edit any metasearch interface' => array( + 'title' => t('Edit any metasearch interface'), + ), + 'edit own metasearch interface' => array( + 'title' => t('Edit own metasearch interface'), + ), + ); +} +/** +* Implements hook_node_access() +*/ +function mkdru_node_access($node, $op, $account) { if ($op == 'create') { // Only users with permission to do so may create this node type. return user_access('create metasearch interface', $account); @@ -48,11 +104,11 @@ function mkdru_access($op, $node, $account) { } /** -* Implementation of hook_menu() +* 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'), @@ -60,104 +116,259 @@ function mkdru_menu() { 'type' => MENU_NORMAL_ITEM, 'file' => 'mkdru.admin.inc', ); + $items['admin/config/search/mkdru_ding'] = array( + 'title' => 'Pazpar2 Metasearch Ding Integration', + 'description' => 'Search settings for mkdru instance integrated into Ding.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('mkdru_ding_settings'), + 'access arguments' => array('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + ); return $items; } - -// Node config /** -* Implementation of hook_form() +* Implementation of hook_init() */ -function mkdru_form(&$node, $form_state) { - $type = node_get_types('type', $node); +function mkdru_init() { + // Applies our module specific CSS to all pages. This works best because + // all CSS is aggregated and cached so we reduce the number of HTTP + // requests and the size is negligible. + drupal_add_css(drupal_get_path('module', 'mkdru') .'/mkdru.css'); +} - $form['title'] = array( - '#type' => 'textfield', - '#title' => check_plain($type->title_label), - '#required' => FALSE, - '#default_value' => $node->title, - '#weight' => -5 - ); - $form['search_settings'] = array( - '#type' => 'fieldset', - '#title' => t('Pazpar2/Service Proxy search settings'), - '#collapsible' => TRUE, - '#collapsed' => FALSE + +// Config form common to node and settings +function mkdru_settings_form($form, &$form_state) { + 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['settings'] = array( + '#tree' => TRUE, ); - $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['display_settings'] = array( + $form['settings']['sp'] = array( '#type' => 'fieldset', - '#title' => t('Display settings'), + '#title' => t('Service Proxy specific settings'), '#collapsible' => TRUE, - '#collapsed' => FALSE + '#collapsed' => TRUE ); - $form['display_settings']['source_max'] = array( + $form['settings']['sp']['user'] = 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, + '#title' => t('Service Proxy username'), + '#description' => t('Service Proxy username'), + '#required' => FALSE, + '#default_value' => $settings['sp']['user'], ); - $form['display_settings']['author_max'] = array( + $form['settings']['sp']['pass'] = 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('Service Proxy password'), + '#description' => t('Service Proxy password'), + '#required' => FALSE, + '#default_value' => $settings['sp']['pass'], + ); + + $form['settings']['facets'] = array( + '#type' => 'fieldset', + '#title' => t('Facet settings'), + // Set up the wrapper so that AJAX will be able to replace the fieldset. + '#prefix' => '
', + '#suffix' => '
', + '#collapsible' => TRUE, + '#collapsed' => FALSE + ); + + 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]['multiLimit'] = array( + '#type' => 'checkbox', + '#title' => t('Allow multiple limits?'), + '#default_value' => $settings['facets'][$facet]['multiLimit'], + ); + $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['settings']['facets'][$facet]['remove'] = array( + '#type' => 'submit', + '#value' => t('Remove ') . $facet . t(' facet'), + '#mkdru facet' => $facet, + '#submit' => array('mkdru_remove_facet'), + '#ajax' => array( + 'callback' => 'mkdru_ajax_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['display_settings']['subject_max'] = array( + $form['new_facet']['canonical'] = 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' => 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_ajax_facet_callback', + 'wrapper' => 'mkdru-facets-form-wrapper', + ), ); 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_remove_facet($form, &$form_state) { + $delfacet = $form_state['clicked_button']['#mkdru facet']; + if ($delfacet) + unset($form_state['values']['settings']['facets'][$delfacet]); + // Block table is not rebuilt like in D6 so we need to remove blocks explicitly + // This is a bit preemptive but the block still reappears in block_list if you + // decide not to save the facet deletion. + db_delete('block')->condition(db_and() + ->condition('module', 'mkdru') + ->condition('delta', 'mkdru_facet_' . $delfacet . '_' . $form_state['values']['nid']) + )->execute(); + $form_state['rebuild'] = TRUE; +} +function mkdru_ajax_facet_callback($form, &$form_state) { + return $form['settings']['facets']; +} + + + +// Ding config +function mkdru_ding_settings($form, &$form_state) { + $form_state['build_info']['args']['settings'] = variable_get('mkdru_ding', NULL); + $form = drupal_retrieve_form('mkdru_settings_form', $form_state); + $form['settings']['#title'] = t('Search settings for DING integration'); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Save configuration', + ); + return $form; +} + +function mkdru_ding_settings_submit($form, &$form_state) { + variable_set('mkdru_ding', $form_state['values']['settings']); + drupal_set_message(t('The configuration options have been saved.')); +} + + +// Node config /** -* Implementation of hook_validate() +* Implements hook_form() */ -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.')); +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' => check_plain($type->title_label), + '#required' => FALSE, + '#default_value' => $node->title, + '#weight' => -5 + ); + return $form; } /** -* Implementation of hook_insert(). +* Implements hook_validate() +*/ +function mkdru_validate($node) { +// 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) { - db_query("INSERT INTO {mkdru} (nid, vid, pz2_path, use_sessions, source_max, author_max, subject_max) VALUES (%d, %d, '%s', %d, %d, %d, %d)", - $node->nid, $node->vid, $node->pz2_path, $node->use_sessions, $node->source_max, $node->author_max, $node->subject_max); + $fields = array('nid' => $node->nid, 'vid' => $node->vid, + 'settings' => json_encode($node->settings)); + db_insert('mkdru')->fields($fields)->execute(); } /** -* Implementation of hook_update(). +* Implements hook_update(). */ function mkdru_update($node) { if ($node->revision) { @@ -165,133 +376,165 @@ function mkdru_update($node) { mkdru_insert($node); } else { - db_query("UPDATE {mkdru} SET pz2_path = '%s', use_sessions = %d, source_max = %d, author_max = %d, subject_max = %d WHERE vid = %d", $node->pz2_path, $node->use_sessions, $node->source_max, $node->author_max, $node->subject_max, $node->vid); + db_update('mkdru') + ->condition('vid', $node->vid) + ->fields(array('settings' => json_encode($node->settings))) + ->execute(); + block_flush_caches(); } } /** - * Implementation of hook_nodeapi(). - * - * When a node revision is deleted, we need to remove the corresponding record - * from our table. The only way to handle revision deletion is by implementing - * hook_nodeapi(). + * Implements hook_node_revision_delete() */ -function mkdru_nodeapi(&$node, $op, $teaser, $page) { - switch ($op) { - case 'delete revision': - db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid); - break; - } +function mkdru_node_revision_delete($node) { + db_delete('mkdru') + ->condition('vid', $node->vid) + ->execute(); } /** - * Implementation of hook_delete(). - * - * When a node is deleted, we need to remove all related records from our table. + * Implements hook_delete() */ function mkdru_delete($node) { // Deleting by nid covers all revisions. - db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid); + db_delete('mkdru') + ->condition('nid', $node->nid) + ->execute(); + // Block table is not rebuilt like in D6 so we need to remove blocks explicitly + db_delete('block')->condition(db_and() + ->condition('module', 'mkdru') + ->condition('delta', '%\_' . $node->nid, 'like') + )->execute(); } // Node rendering /** -* Implementation of hook_load() +* Implements hook_load() */ -function mkdru_load($node) { - return array('mkdru' => db_fetch_object(db_query( - 'SELECT * FROM {mkdru} WHERE vid = %d', $node->vid))); +function mkdru_load($nodes) { + $result = db_query('SELECT * FROM {mkdru} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes))); + foreach ($result as $record) { + $nodes[$record->nid]->mkdru = $record; + } } /** -* Implementation of hook_theme(). +* Implements hook_theme(). */ function mkdru_theme() { return array( - 'mkdru_page' => array( - 'template' => 'mkdru-page', - 'arguments' => array(), + 'mkdru_form' => array( + 'template' => 'mkdru-form', + 'variables' => array(), ), - 'mkdru_page_js' => array( - 'arguments' => array('node' => NULL), + 'mkdru_results' => array( + 'template' => 'mkdru-results', + 'variables' => array(), ), -// 'mkdru_block_facet' => array( -// 'template' => 'mkdru-block-facet', -// 'arguments' => array('divId' => NULL), -// ), + 'mkdru_js' => array( + 'variables' => array('setting' => NULL), + ), + 'mkdru_block_search' => array( + 'template' => 'mkdru-block-search', + 'variables' => array('nid' => NULL, 'path' => NULL), + ), + 'mkdru_block_facet' => array( + 'template' => 'mkdru-block-facet', + 'variables' => array('class' => NULL) + ) ); } /** * Theme function to include Javascript search client and deps */ -function theme_mkdru_page_js($node) { +function theme_mkdru_js(&$variables) { $path = drupal_get_path('module', 'mkdru'); - // Include client library. - drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer'); - drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer'); - drupal_add_js($path . '/mkdru.client.js', 'module', 'footer'); - drupal_add_js(array('mkdru' => $node->mkdru), 'setting'); + // Pazpar2 client library + drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', array( + 'type' => 'external', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE)); + // jQuery plugin for query string/history manipulation. + 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( + 'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE)); + drupal_add_js($variables['setting'], 'setting'); } /** -* Implementation of hook_view() +* Implements hook_view() */ -function mkdru_view($node, $teaser = FALSE, $page = FALSE) { - $node->content['mkdru_page_js'] = array( - '#value' => theme('mkdru_page_js', $node), - '#weight' => 0, - ); - $node->content['mkdru_page'] = array( - '#value' => theme('mkdru_page'), - '#weight' => 1, - ); +function mkdru_view($node, $view_mode) { + if ($view_mode == 'full') { + $node->content['mkdru_js'] = array( + '#markup' => theme('mkdru_js', array('setting' + => array('mkdru' => array('settings' => $node->mkdru->settings)))), + '#weight' => 0, + ); + $node->content['mkdru_form'] = array( + '#markup' => theme('mkdru_form'), + '#weight' => 1, + ); + $node->content['mkdru_results'] = array( + '#markup' => theme('mkdru_results'), + '#weight' => 2, + ); + } return $node; } + + +// Blocks /** -* Implementation of hook_block() +* Implements hook_block_info() */ -function mkdru_block($op='list', $delta='sources', $edit=array()) { - switch ($op) { - case 'list': - $blocks['mkdru_sources']['info'] = t('mkdru - source facets'); - $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE; - $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets'); - $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE; - $blocks['mkdru_authors']['info'] = t('mkdru - author facets'); - $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE; - return $blocks; - - case 'view': - switch ($delta) { - // TODO: make the facet themable, I have no clue why this won't work -// case 'mkdru_sources': -// $block['subject'] = t('Source'); -// $block['content'] = theme('mkdru_block_facet', 'mkdru-sources'); -// return $block; -// case 'mkdru_subjects': -// $block['subject'] = t('Subject'); -// $block['content'] = theme('mkdru_block_facet', 'mkdru-subjects'); -// return $block; -// case 'mkdru_authors': -// $block['subject'] = t('Author'); -// $block['content'] = theme('mkdru_block_facet', 'mkdru-authors'); -// return $block; - case 'mkdru_sources': - $block['subject'] = t('Source'); - $block['content'] = '
'; - return $block; - case 'mkdru_subjects': - $block['subject'] = t('Subject'); - $block['content'] = '
'; - return $block; - case 'mkdru_authors': - $block['subject'] = t('Author'); - $block['content'] = '
'; - return $block; +function mkdru_block_info() { + $blocks = array(); + $result = db_query("SELECT title, {mkdru}.nid as nid, settings FROM {node},{mkdru} WHERE {mkdru}.nid = {node}.nid;"); + foreach($result as $node) { + // search blocks + $blocks['mkdru_search_' . $node->nid]['info'] = + t('mkdru - search box for "' . $node->title . '"'); + $blocks['mkdru_search_' . $node->nid]['cache'] = DRUPAL_NO_CACHE; + // facet blocks + $settings = json_decode($node->settings, TRUE); + foreach ($settings['facets'] as $facet_name => $facet) { + $key = 'mkdru_facet_' . $facet_name . '_' . $node->nid; + $blocks[$key]['info'] = 'mkdru - ' . $facet_name + . t(' facet for "') . $node->title . '"'; + $blocks[$key]['visibility'] = 1; + $blocks[$key]['pages'] = 'node/' . $node->nid; + $blocks[$key]['cache'] = DRUPAL_CACHE_GLOBAL; + } + }; + return $blocks; +} + +/** +* Implements hook_block_view() +*/ +function mkdru_block_view($delta) { + if (substr($delta, 0, 13) == 'mkdru_search_') { + $nid = substr($delta, 13); + $block['content'] = theme('mkdru_block_search', + array('nid' => $nid, 'path' => '/node/' . $nid)); + return $block; + } + else if (preg_match('/^mkdru_facet_(.*)_(\d+)$/', $delta, $matches) > 0) { + $facet = $matches[1]; + $nid = $matches[2]; + $result = db_query("SELECT settings FROM {mkdru} WHERE nid = :nid;", array(':nid' => $nid)); + $settingsjson = $result->fetchObject()->settings; + $settings = json_decode($settingsjson, TRUE); + if (isset($settings['facets'][$facet]['displayName'])) { + $block['subject'] = $settings['facets'][$facet]['displayName']; } + $block['content'] = theme('mkdru_block_facet', array('class' => 'mkdru-facet-' . $facet)); + return $block; } }