8 * Implements hook_node_info()
10 function mkdru_node_info() {
13 'name' => t("Pazpar2 metasearch interface"),
15 'description' => t("Metasearch interface for Z39.50/SRU and other targets via a Pazpar2/Service Proxy backend"),
20 function mkdru_ting_search_tab($keys) {
21 error_log("TING SEARCH TAB invoked");
22 $path = drupal_get_path('module', 'mkdru');
23 // D6 is not to be able to handle external scripts and always
24 // prepends '/' (so we cannot keep pz2.js on remote host)
25 // we will try to be even smarter :)
26 $pz2_js_path = variable_get('pz2_js_path', 'pazpar2/js');
27 if (preg_match('/^\//', $pz2_js_path)) {
28 $pz2_js_path = trim($pz2_js_path, '/');
29 drupal_add_js($pz2_js_path.'/pz2.js', 'module', 'footer',
31 error_log("Pz2.js added as alias script '".$pz2_js_path);
33 drupal_add_js($path.'/'.$pz2_js_path.'/pz2.js', 'module', 'footer');
35 drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer');
36 drupal_add_js($path . '/recipe.js', 'module', 'footer');
37 drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer');
38 drupal_add_js($path . '/mkdru.client.js', 'module', 'footer');
39 $html = theme('mkdru_results');
40 drupal_add_js(array('mkdru' =>
42 'settings' => json_encode(variable_get('mkdru_ding', NULL)),
43 'pz2_path' => variable_get('pz2_path', '/pazpar2/search.pz2'),
47 return array("content" => $html, "title" => "Meta Search");
51 * Implements hook_perm()
53 function mkdru_perm() {
54 return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface');
58 * Implements hook_access()
60 function mkdru_access($op, $node, $account) {
62 if ($op == 'create') {
63 // Only users with permission to do so may create this node type.
64 return user_access('create metasearch interface', $account);
67 // Users who create a node may edit or delete it later, assuming they have the
68 // necessary permissions.
69 if ($op == 'update' || $op == 'delete') {
70 if (user_access('edit own metasearch interface', $account) && ($account->uid == $node->uid)) {
73 elseif (user_access('edit any metasearch interface', $account)) {
80 * Implements hook_menu()
82 function mkdru_menu() {
83 // Can't get tabbed menus to work in D6. The children work and the tabs
84 // show up there but the top level's url results in it trying to find
85 // the system module's system.inc in mkdru's directory
86 // $items['admin/settings/mkdru'] = array(
87 // 'title' => 'Pazpar2 Metasearch Settings',
88 // 'description' => 'Settings for mkdru.',
89 // 'access arguments' => array('administer site configuration'),
91 // $items['admin/settings/mkdru/settings'] = array(
92 // 'title' => 'Settings',
93 // 'description' => 'Settings for mkdru.',
94 // 'page callback' => 'drupal_get_form',
95 // 'page arguments' => array('mkdru_admin_settings'),
96 // 'type' => MENU_DEFAULT_LOCAL_TASK,
97 // 'file' => 'mkdru.admin.inc',
100 // $items['admin/settings/mkdru/ding'] = array(
101 // 'title' => 'Ding Integration',
102 // 'description' => 'Search settings for mkdru instance integrated into Ding.',
103 // 'page callback' => 'drupal_get_form',
104 // 'page arguments' => array('mkdru_ding_settings'),
105 // 'type' => MENU_LOCAL_TASK,
108 $items['admin/settings/mkdru'] = array(
109 'title' => 'Pazpar2 Metasearch Settings',
110 'description' => 'Settings for mkdru.',
111 'page callback' => 'drupal_get_form',
112 'page arguments' => array('mkdru_admin_settings'),
113 'access arguments' => array('administer site configuration'),
114 'type' => MENU_NORMAL_ITEM,
115 'file' => 'mkdru.admin.inc',
117 $items['admin/settings/mkdru-ding'] = array(
118 'title' => 'Pazpar2 Metasearch Ding Integration',
119 'description' => 'Search settings for mkdru instance integrated into Ding.',
120 'page callback' => 'drupal_get_form',
121 'page arguments' => array('mkdru_ding_settings'),
122 'access arguments' => array('administer site configuration'),
123 'type' => MENU_NORMAL_ITEM,
125 $items['ahah-mkdru-facet'] = array(
126 'page callback' => 'mkdru_add_facet_callback',
127 'access arguments' => array('create metasearch interface'),
128 'type' => MENU_CALLBACK,
134 * Implements hook_init()
136 function mkdru_init() {
137 // Applies our module specific CSS to all pages. This works best because
138 // all CSS is aggregated and cached so we reduce the number of HTTP
139 // requests and the size is negligible.
140 drupal_add_css(drupal_get_path('module', 'mkdru') .'/mkdru.css');
144 // Config form common to node and settings
145 // function mkdru_settings_form($form, &$form_state) {
146 function mkdru_settings_form(&$form_state) {
147 if (isset($form_state['values']['settings'])) {
148 $settings = $form_state['values']['settings'];
150 elseif (isset($form_state['build_info']['args']['settings'])) {
151 $settings = $form_state['build_info']['args']['settings'];
154 $settings = variable_get('mkdru_defaults', NULL);
157 $form['#cache'] = TRUE;
159 $form['settings'] = array(
163 $form['settings']['pz2_path'] = array(
164 '#type' => 'textfield',
165 '#title' => t('Pazpar2/Service Proxy path'),
166 '#description' => t('Path that takes Pazpar2 commands via HTTP'),
168 '#default_value' => $settings['pz2_path'],
170 $form['settings']['use_sessions'] = array(
171 '#type' => 'checkbox',
172 '#title' => t('Session handling'),
173 '#description' => t('Disable for use with Service Proxy'),
174 '#default_value' => $settings['use_sessions'],
177 $form['settings']['sp'] = array(
178 '#type' => 'fieldset',
179 '#title' => t('Service Proxy specific settings'),
180 '#collapsible' => TRUE,
181 '#collapsed' => FALSE
183 $form['settings']['sp']['user'] = array(
184 '#type' => 'textfield',
185 '#title' => t('Service Proxy username'),
186 '#description' => t('Service Proxy username'),
187 '#required' => FALSE,
188 '#default_value' => $settings['sp']['user'],
190 $form['settings']['sp']['pass'] = array(
191 '#type' => 'textfield',
192 '#title' => t('Service Proxy password'),
193 '#description' => t('Service Proxy password'),
194 '#required' => FALSE,
195 '#default_value' => $settings['sp']['pass'],
198 $form['settings']['facets'] = array(
199 '#type' => 'fieldset',
200 '#title' => t('Facet settings'),
201 // Set up the wrapper so that AJAX will be able to replace the fieldset.
202 '#prefix' => '<div id="mkdru-facets-form-wrapper">',
203 '#suffix' => '</div>',
204 '#collapsible' => TRUE,
205 '#collapsed' => FALSE
208 foreach (array_keys($settings['facets']) as $facet) {
209 $form['settings']['facets'][$facet] = array(
210 '#type' => 'fieldset',
211 '#title' => $facet . ' ' . t('facet'),
212 '#collapsible' => TRUE,
213 '#collapsed' => FALSE
215 $form['settings']['facets'][$facet]['displayName'] = array(
216 '#type' => 'textfield',
217 '#title' => t('Facet name to display in UI'),
219 '#default_value' => $settings['facets'][$facet]['displayName'],
221 $form['settings']['facets'][$facet]['pz2Name'] = array(
222 '#type' => 'textfield',
223 '#title' => t('Name of termlist in Pazpar2'),
225 '#default_value' => $settings['facets'][$facet]['pz2Name'],
227 $form['settings']['facets'][$facet]['limiter'] = array(
228 '#type' => 'textfield',
229 '#title' => t('Query limiter string'),
230 '#default_value' => $settings['facets'][$facet]['limiter'],
233 $form['settings']['facets'][$facet]['multiLimit'] = array(
234 '#type' => 'checkbox',
235 '#title' => t('Allow multiple limits?'),
236 '#default_value' => $settings['facets'][$facet]['multiLimit'],
238 $form['settings']['facets'][$facet]['max'] = array(
239 '#type' => 'textfield',
240 '#title' => t('Number of terms to display'),
242 '#default_value' => $settings['facets'][$facet]['max'],
246 $form['settings']['facets'][$facet]['orderWeight'] = array(
247 '#type' => 'textfield',
248 '#title' => t('Facet weight'),
249 '#default_value' => $settings['facets'][$facet]['orderWeight'],
253 $form['settings']['facets'][$facet]['remove'] = array(
255 '#value' => t('Remove ') . $facet . t(' facet'),
256 '#mkdru facet' => $facet,
257 '#submit' => array('mkdru_remove_facet_submit'),
259 'path' => 'ahah-mkdru-facet',
260 'wrapper' => 'mkdru-facets-form-wrapper',
264 $form['new_facet'] = array(
265 '#type' => 'fieldset',
266 '#title' => t('Add new facet...'),
268 '#collapsible' => TRUE,
269 '#collapsed' => FALSE
271 $form['new_facet']['canonical'] = array(
272 '#type' => 'textfield',
273 '#title' => t('Canonical name of new facet'),
275 $form['new_facet']['button'] = array(
277 '#value' => t('Add facet'),
278 '#description' => t('Configure additional facets based on Pazpar2/SP termlists'),
279 '#submit' => array('mkdru_add_facet_submit'),
281 'path' => 'ahah-mkdru-facet',
282 'wrapper' => 'mkdru-facets-form-wrapper',
288 function mkdru_add_facet_submit($form, &$form_state) {
289 $newfacet = $form_state['values']['new_facet']['canonical'];
290 $form_state['values']['settings']['facets'][$newfacet] = NULL;
291 $form_state['rebuild'] = TRUE;
292 return $form_state['values'];
295 function mkdru_remove_facet_submit($form, &$form_state) {
296 $delfacet = $form_state['clicked_button']['#mkdru facet'];
298 unset($form_state['values']['settings']['facets'][$delfacet]);
301 function mkdru_add_facet_callback() {
302 // Necessary to work with hook_form
303 module_load_include('inc', 'node', 'node.pages');
305 // Retrieve form from cache
306 $form_state = array('storage' => NULL, 'submitted' => FALSE);
307 $form_build_id = $_POST['form_build_id'];
308 $form = form_get_cache($form_build_id, $form_state);
310 // Run drupal_process_form to call submit handler and update $form_state
311 $args = $form['#parameters'];
312 $form_id = array_shift($args);
313 $form_state['post'] = $form['#post'] = $_POST;
314 $form['#programmed'] = $form['#redirect'] = FALSE;
315 drupal_process_form($form_id, $form, $form_state);
317 // Regenerate form so we can render the new facet part
318 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
320 // Choose subset of form to redraw.
321 $facet_form = $form['settings']['facets'];
322 // Prevent duplicate wrappers.
323 unset($facet_form['#prefix'], $facet_form['#suffix']);
324 $output = theme('status_messages') . drupal_render($facet_form);
326 // Final rendering callback
327 drupal_json(array('status' => TRUE, 'data' => $output));
333 function mkdru_ding_settings(&$form_state) {
334 $form_state['build_info']['args']['settings'] = variable_get('mkdru_ding', NULL);
335 $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
336 $form['settings']['#title'] = t('Default search settings');
337 $form['submit'] = array(
339 '#value' => 'Save configuration',
343 function mkdru_ding_settings_submit($form, &$form_state) {
344 variable_set('mkdru_ding', $form_state['values']['settings']);
345 drupal_set_message(t('The configuration options have been saved.'));
352 * Implements hook_form()
354 function mkdru_form(&$node, &$form_state) {
355 if (isset($node->settings)) {
356 // Second decode parameter indicates associative array
357 $form_state['build_info']['args']['settings'] = json_decode($node->settings, TRUE);
360 $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
361 $type = node_get_types('type', $node);
362 $form['title'] = array(
363 '#type' => 'textfield',
364 '#title' => check_plain($type->title_label),
365 '#required' => FALSE,
366 '#default_value' => $node->title,
374 * Implements hook_validate()
376 function mkdru_validate($node) {
381 * Implements hook_insert().
383 function mkdru_insert($node) {
384 $record['nid'] = $node->nid;
385 $record['vid'] = $node->vid;
386 $record['settings'] = json_encode($node->settings);
387 drupal_write_record('mkdru', &$record);
391 * Implements hook_update().
393 function mkdru_update($node) {
394 if ($node->revision) {
395 // New revision; treat it as a new record.
399 $record['nid'] = $node->nid;
400 $record['vid'] = $node->vid;
401 $record['settings'] = json_encode($node->settings);
402 drupal_write_record('mkdru', &$record, 'vid');
407 * Implements hook_nodeapi().
409 * When a node revision is deleted, we need to remove the corresponding record
410 * from our table. The only way to handle revision deletion is by implementing
413 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
415 case 'delete revision':
416 db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
422 * Implements hook_delete().
424 function mkdru_delete($node) {
425 // Deleting by nid covers all revisions.
426 db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
433 * Implements hook_load()
435 function mkdru_load($node) {
436 return db_fetch_object(db_query('SELECT * FROM {mkdru} WHERE vid = %d', $node->vid));
440 * Implements hook_theme().
442 function mkdru_theme() {
444 'mkdru_form' => array(
445 'template' => 'mkdru-form',
446 'arguments' => array(),
448 'mkdru_results' => array(
449 'template' => 'mkdru-results',
450 'arguments' => array(),
453 'arguments' => array('node' => NULL),
455 'mkdru_block_search' => array(
456 'template' => 'mkdru-block-search',
457 'arguments' => array('nid' => NULL, 'path' => NULL),
459 'mkdru_block_facet' => array(
460 'template' => 'mkdru-block-facet',
461 'arguments' => array('class' => NULL)
467 * Theme function to include Javascript search client and deps
469 function theme_mkdru_js($node) {
470 $path = drupal_get_path('module', 'mkdru');
471 // Pazpar2 client library.
472 drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer', TRUE, TRUE, FALSE);
473 // jQuery plugin for query string/history manipulation.
474 drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer', TRUE, TRUE, FALSE);
475 drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE);
476 drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE);
477 drupal_add_js(array('mkdru' => $node->mkdru), 'setting');
478 drupal_add_js(array('mkdru' =>
480 'settings' => $node->settings,
486 * Implements hook_view()
488 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
489 $node->content['mkdru_js'] = array(
490 '#value' => theme('mkdru_js', $node),
493 $node->content['mkdru_form'] = array(
494 '#value' => theme('mkdru_form'),
497 $node->content['mkdru_results'] = array(
498 '#value' => theme('mkdru_results'),
505 * Implements hook_block()
507 function mkdru_block($op='list', $delta='sources', $edit=array()) {
510 // maybe if I label the tables or something?
511 $result = db_query("SELECT title, {mkdru}.nid as nid, settings FROM {node},{mkdru} WHERE {mkdru}.nid = {node}.nid;");
512 while ($node = db_fetch_object($result)) {
514 $blocks['mkdru_search_' . $node->nid]['info'] =
515 t('mkdru - search box for "' . $node->title . '"');
517 $settings = json_decode($node->settings, TRUE);
518 foreach ($settings['facets'] as $facet_name => $facet) {
519 $key = 'mkdru_facet_' . $facet_name . '_' . $node->nid;
520 $blocks[$key]['info'] = 'mkdru - ' . $facet_name
521 . t(' facet for "') . $node->title . '"';
522 $blocks[$key]['visibility'] = 1;
523 $blocks[$key]['pages'] = 'node/' . $node->nid;
524 $blocks[$key]['cache'] = BLOCK_CACHE_GLOBAL;
530 if (preg_match('/^mkdru_facet_(.*)_(\d+)$/', $delta, $matches) > 0) {
531 $facet = $matches[1];
534 // this is rather spendy just to get the title, should really
535 // be able to set a default title in op=list. will be cached though
536 // so I suppose it's okay?
537 $result = db_query("SELECT settings FROM {mkdru} WHERE nid = %d;", $nid);
538 $settingsjson = db_fetch_object($result)->settings;
539 $settings = json_decode($settingsjson, TRUE);
540 if (isset($settings['facets'][$facet]['displayName'])) {
541 $block['subject'] = $settings['facets'][$facet]['displayName'];
543 $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-' . $facet);
545 else if (substr($delta, 0, 13) == 'mkdru_search_') {
546 $nid = substr($delta, 13);
547 $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);