X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=mkdru.module;h=e8631b8acb53365ca3193291493c6f86752a0ce6;hb=f38d18598fa78f8d3e2ac33f476bda64c1afaabd;hp=d057df2a3dbee3aaeb8367be124a191405a4575d;hpb=895c5ddc7ab1754bc7a97c5af9132e22557f49ca;p=mkdru-moved-to-drupal.org.git diff --git a/mkdru.module b/mkdru.module index d057df2..e8631b8 100644 --- a/mkdru.module +++ b/mkdru.module @@ -17,25 +17,25 @@ function mkdru_node_info() { ); } -function mkdru_ting_search_show($params) { +function mkdru_ting_search_tab($keys) { + error_log("TING SEARCH TAB invoked"); $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 . '/jquery.ba-bbq.js', 'module', 'footer'); + drupal_add_js($path . '/recipe.js', 'module', 'footer'); drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer'); drupal_add_js($path . '/mkdru.client.js', 'module', 'footer'); - $html = theme('mkdru_results'); + $html = theme('mkdru_results'); drupal_add_js(array('mkdru' => array( - 'use_sessions' => variable_get('use_sessions', '1'), + 'settings' => json_encode(variable_get('mkdru_ding', NULL)), 'pz2_path' => variable_get('pz2_path', '/pazpar2/search.pz2'), - 'sp_user' => variable_get('sp_user', ''), - 'sp_pass' => variable_get('sp_pass', ''), - 'query' => $params['keys'] + 'query' => $keys ) ), 'setting'); - return array("content" => $html); + return array("content" => $html, "title" => "Meta Search"); } /** @@ -71,8 +71,33 @@ 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' => 'mkdru Settings', + 'title' => 'Pazpar2 Metasearch Settings', 'description' => 'Settings for mkdru.', 'page callback' => 'drupal_get_form', 'page arguments' => array('mkdru_admin_settings'), @@ -80,6 +105,19 @@ function mkdru_menu() { 'type' => MENU_NORMAL_ITEM, 'file' => 'mkdru.admin.inc', ); + $items['admin/settings/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, + ); + $items['ahah-mkdru-facet'] = array( + 'page callback' => 'mkdru_add_facet_callback', + 'access arguments' => array('create metasearch interface'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -94,94 +132,230 @@ function mkdru_init() { } +// Config form common to node and settings +// function mkdru_settings_form($form, &$form_state) { +function mkdru_settings_form(&$form_state) { + if (isset($form_state['values']['settings'])) { + $settings = $form_state['values']['settings']; + } + elseif (isset($form_state['build_info']['args']['settings'])) { + $settings = $form_state['build_info']['args']['settings']; + } + else { + $settings = variable_get('mkdru_defaults', NULL); + } -// Node config -/** -* Implements hook_form() -*/ -function mkdru_form(&$node, $form_state) { - $type = node_get_types('type', $node); + $form['#cache'] = TRUE; - $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' => FALSE ); - $form['sp_settings']['sp_user'] = array( + $form['settings']['sp']['user'] = array( '#type' => 'textfield', '#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( - '#type' => 'textfield', + $form['settings']['sp']['pass'] = array( + '#type' => 'password', '#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' => '
', + '#suffix' => '
', '#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]['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_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['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['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) { + $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 +function mkdru_ding_settings(&$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('Default search settings'); + $form['submit'] = array( + '#type' => 'submit', + '#value' => 'Save configuration', ); - $form['display_settings']['subject_max'] = array( + 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 +/** +* Implements hook_form() +*/ +function mkdru_form(&$node, &$form_state) { + if (isset($node->settings)) { + // Second decode parameter indicates associative array + $form_state['build_info']['args']['settings'] = json_decode($node->settings, TRUE); + } + + $form = drupal_retrieve_form('mkdru_settings_form', &$form_state); + $type = node_get_types('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; } @@ -191,22 +365,17 @@ function mkdru_form(&$node, $form_state) { * 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 } /** * Implements hook_insert(). */ function mkdru_insert($node) { - drupal_write_record('mkdru', $node); + $record['nid'] = $node->nid; + $record['vid'] = $node->vid; + $record['settings'] = json_encode($node->settings); + drupal_write_record('mkdru', &$record); } /** @@ -218,7 +387,10 @@ function mkdru_update($node) { mkdru_insert($node); } else { - drupal_write_record('mkdru', $node, 'vid'); + $record['nid'] = $node->nid; + $record['vid'] = $node->vid; + $record['settings'] = json_encode($node->settings); + drupal_write_record('mkdru', &$record, 'vid'); } } @@ -252,8 +424,7 @@ function mkdru_delete($node) { * Implements hook_load() */ function mkdru_load($node) { - return array('mkdru' => db_fetch_object(db_query( - 'SELECT * FROM {mkdru} WHERE vid = %d', $node->vid))); + return db_fetch_object(db_query('SELECT * FROM {mkdru} WHERE vid = %d', $node->vid)); } /** @@ -295,6 +466,11 @@ function theme_mkdru_js($node) { drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE); drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE); drupal_add_js(array('mkdru' => $node->mkdru), 'setting'); + drupal_add_js(array('mkdru' => + array( + 'settings' => $node->settings, + ) + ), 'setting'); } /** @@ -322,57 +498,45 @@ function mkdru_view($node, $teaser = FALSE, $page = FALSE) { function mkdru_block($op='list', $delta='sources', $edit=array()) { switch ($op) { case 'list': - // facet blocks - // D6 has no setting for note type visibility, set - // the default to limit facet display to this type - $visPHP = ' arg(1))); - return $node->type == "mkdru"; - } -?>'; - - // NB: block caching is redundant for static content - $blocks['mkdru_sources']['info'] = t('mkdru - source facets'); - $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE; - $blocks['mkdru_sources']['visibility'] = 2; - $blocks['mkdru_sources']['pages'] = $visPHP; - $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets'); - $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE; - $blocks['mkdru_subjects']['visibility'] = 2; - $blocks['mkdru_subjects']['pages'] = $visPHP; - $blocks['mkdru_authors']['info'] = t('mkdru - author facets'); - $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE; - $blocks['mkdru_authors']['visibility'] = 2; - $blocks['mkdru_authors']['pages'] = $visPHP; - // search blocks - $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';"); + // maybe if I label the tables or something? + $result = db_query("SELECT title, {mkdru}.nid as nid, settings FROM {node},{mkdru} WHERE {mkdru}.nid = {node}.nid;"); while ($node = db_fetch_object($result)) { + // search blocks $blocks['mkdru_search_' . $node->nid]['info'] = t('mkdru - search box for "' . $node->title . '"'); - $blocks['mkdru_sources']['cache'] = BLOCK_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'] = BLOCK_CACHE_GLOBAL; + } }; return $blocks; case 'view': - switch ($delta) { - case 'mkdru_sources': - $block['subject'] = t('Source'); - $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-source'); - return $block; - case 'mkdru_subjects': - $block['subject'] = t('Subject'); - $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-subject'); - return $block; - case 'mkdru_authors': - $block['subject'] = t('Author'); - $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-author'); - return $block; - } - if (substr($delta, 0, 13) == 'mkdru_search_') { - $nid = substr($delta, 13); - $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid); - return $block; - } + if (preg_match('/^mkdru_facet_(.*)_(\d+)$/', $delta, $matches) > 0) { + $facet = $matches[1]; + $nid = $matches[2]; + + // this is rather spendy just to get the title, should really + // be able to set a default title in op=list. will be cached though + // so I suppose it's okay? + $result = db_query("SELECT settings FROM {mkdru} WHERE nid = %d;", $nid); + $settingsjson = db_fetch_object($result)->settings; + $settings = json_decode($settingsjson, TRUE); + if (isset($settings['facets'][$facet]['displayName'])) { + $block['subject'] = $settings['facets'][$facet]['displayName']; + } + $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-' . $facet); + } + else if (substr($delta, 0, 13) == 'mkdru_search_') { + $nid = substr($delta, 13); + $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid); + } + return $block; } }