From 7f65c61eb4ac230a607fa8125ca41a7e3dc539f8 Mon Sep 17 00:00:00 2001 From: Jason Skomorowski Date: Fri, 29 Apr 2011 22:41:08 +0000 Subject: [PATCH] (untested w/ding) Finish new architecture to bring mkdru7 in line with 6. --- mkdru.admin.inc | 44 +------------- mkdru.install | 6 +- mkdru.module | 173 ++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 112 insertions(+), 111 deletions(-) diff --git a/mkdru.admin.inc b/mkdru.admin.inc index 78ebf65..af8a7ce 100644 --- a/mkdru.admin.inc +++ b/mkdru.admin.inc @@ -3,49 +3,9 @@ function mkdru_admin_settings($form, &$form_state) { $form['pz2_js_path']=array( '#type' => 'textfield', '#title' => t('Path to Pazpar2 client library'), - '#description' => t('Absolute path to the directory containing pz2.js within the current domain. Do not include leading slash.'), - '#default_value' => variable_get('pz2_js_path', 'pazpar2/js'), + '#description' => t('Absolute path to the directory containing pz2.js within the current domain.'), + '#default_value' => variable_get('pz2_js_path', '/pazpar2/js'), '#required' => TRUE ); - $form['search_settings'] = array( - '#type' => 'fieldset', - '#title' => t('Pazpar2/Service Proxy search settings'), - '#collapsible' => TRUE, - '#collapsed' => FALSE - ); - $form['search_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' => variable_get('pz2_path','/pazpar2/search.pz2') - ); - $form['search_settings']['use_sessions'] = array( - '#type' => 'checkbox', '#title' => t('Session handling'), - '#description' => t('Disable for use with Service Proxy'), - '#default_value' => variable_get('use_sessions', '1') - ); - - $form['sp_settings'] = array( - '#type' => 'fieldset', - '#title' => t('Service Proxy specific settings'), - '#collapsible' => TRUE, - '#collapsed' => FALSE - ); - $form['sp_settings']['sp_user'] = array( - '#type' => 'textfield', - '#title' => t('Service Proxy username'), - '#description' => t('Service Proxy username'), - '#required' => FALSE, - '#default_value' => variable_get('sp_user','') - ); - $form['sp_settings']['sp_pass'] = array( - '#type' => 'textfield', - '#title' => t('Service Proxy password'), - '#description' => t('Service Proxy password'), - '#required' => FALSE, - '#default_value' => variable_get('sp_pass','') - ); - return system_settings_form($form); } diff --git a/mkdru.install b/mkdru.install index 4651a03..280e7c8 100644 --- a/mkdru.install +++ b/mkdru.install @@ -62,16 +62,17 @@ function mkdru_install() { $settings['facets']['subject']['displayName'] = "Subject"; $settings['facets']['subject']['pz2Name'] = "subject"; $settings['facets']['subject']['limiter'] = "su"; - $settings['facets']['subject']['multiLimit'] = TRUE; + $settings['facets']['subject']['multiLimit'] = 1; $settings['facets']['subject']['max'] = 10; $settings['facets']['subject']['orderWeight'] = 2; $settings['facets']['author']['displayName'] = "Author"; $settings['facets']['author']['pz2Name'] = "author"; $settings['facets']['author']['limiter'] = "au"; - $settings['facets']['author']['multiLimit'] = FALSE; + $settings['facets']['author']['multiLimit'] = 0; $settings['facets']['author']['max'] = 10; $settings['facets']['author']['orderWeight'] = 3; variable_set('mkdru_defaults', $settings); + variable_set('mkdru_ding', $settings); } /** @@ -81,6 +82,7 @@ function mkdru_uninstall() { // Delete variables variable_del('pz2_js_path'); variable_del('mkdru_defaults'); + variable_del('mkdru_ding'); // Clear block visibility db_delete('block_node_type')->condition('module', 'mkdru')->execute(); } diff --git a/mkdru.module b/mkdru.module index 72edab9..ddac028 100644 --- a/mkdru.module +++ b/mkdru.module @@ -56,27 +56,9 @@ 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')); - 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'); - + theme('mkdru_js', array('setting' => array('mkdru' => array( + 'settings' => json_encode(variable_get('mkdru_ding', NULL)), + 'query' => $keys)))); return array(); } @@ -134,14 +116,13 @@ 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.', + $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_admin_settings'), + 'page arguments' => array('mkdru_ding_settings'), 'access arguments' => array('administer site configuration'), - 'type' => MENU_LOCAL_TASK, - 'file' => 'mkdru.admin.inc', + 'type' => MENU_NORMAL_ITEM, ); return $items; } @@ -160,9 +141,6 @@ function mkdru_init() { // 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']; } @@ -247,6 +225,11 @@ function mkdru_settings_form($form, &$form_state) { '#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'), @@ -262,6 +245,16 @@ function mkdru_settings_form($form, &$form_state) { '#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( @@ -282,13 +275,57 @@ function mkdru_settings_form($form, &$form_state) { '#weight' => 1, '#submit' => array('mkdru_add_facet_form'), '#ajax' => array( - 'callback' => 'mkdru_add_facet_callback', + '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 /** @@ -311,17 +348,6 @@ function mkdru_form(&$node, &$form_state) { 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() */ @@ -354,6 +380,7 @@ function mkdru_update($node) { ->condition('vid', $node->vid) ->fields(array('settings' => json_encode($node->settings))) ->execute(); + block_flush_caches(); } } @@ -374,6 +401,11 @@ function mkdru_delete($node) { 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(); } @@ -403,7 +435,7 @@ function mkdru_theme() { 'variables' => array(), ), 'mkdru_js' => array( - 'variables' => array('node' => NULL), + 'variables' => array('setting' => NULL), ), 'mkdru_block_search' => array( 'template' => 'mkdru-block-search', @@ -423,14 +455,14 @@ function theme_mkdru_js(&$variables) { $path = drupal_get_path('module', 'mkdru'); // Pazpar2 client library drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', array( - 'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE)); + '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(array('mkdru' => $variables['node']->mkdru), 'setting'); + drupal_add_js($variables['setting'], 'setting'); } /** @@ -439,7 +471,8 @@ function theme_mkdru_js(&$variables) { function mkdru_view($node, $view_mode) { if ($view_mode == 'full') { $node->content['mkdru_js'] = array( - '#markup' => theme('mkdru_js', array('node' => $node)), + '#markup' => theme('mkdru_js', array('setting' + => array('mkdru' => array('settings' => $node->mkdru->settings)))), '#weight' => 0, ); $node->content['mkdru_form'] = array( @@ -461,23 +494,24 @@ function mkdru_view($node, $view_mode) { * Implements hook_block_info() */ function mkdru_block_info() { - // facet blocks $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'); - $blocks['mkdru_facet_' . $facet]['cache'] = DRUPAL_NO_CACHE; - } - - // search blocks - $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';"); + $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_sources']['cache'] = DRUPAL_NO_CACHE; + $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; } @@ -485,17 +519,22 @@ function mkdru_block_info() { * Implements hook_block_view() */ function mkdru_block_view($delta) { - if (substr($delta, 0, 12) == 'mkdru_facet_') { - $facet = substr($delta, 12); - $block['subject'] = t(ucwords($facet)); - $block['content'] = theme('mkdru_block_facet', - array('class' => 'mkdru-facet-' . $facet)); - return $block; - } - elseif (substr($delta, 0, 13) == 'mkdru_search_') { + if (substr($delta, 0, 13) == 'mkdru_search_') { $nid = substr($delta, 13); $block['content'] = theme('mkdru_block_search', - array('nid' => $nid, 'path' => '/node/' . $nid)); + 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; } } -- 1.7.10.4