Drupal.org explicitly licenses as GPLv2 and frowns upon including a LICENSE
[mkdru-moved-to-drupal.org.git] / mkdru.module
1 <?php
2 // $Id$
3
4
5
6 // Module metainfo
7 /**
8 * Implements hook_node_info()
9 */
10 function mkdru_node_info() {
11   return array(
12     'mkdru' => array(
13       'name' => t("Pazpar2 metasearch interface"),
14       'module' => 'mkdru',
15       'description' => t("Metasearch interface for Z39.50/SRU and other targets via a Pazpar2/Service Proxy backend"),
16     )
17   );
18 }
19
20 function mkdru_ting_search_tab($keys) {
21   error_log("TING SEARCH TAB invoked");
22    $path = drupal_get_path('module', 'mkdru');
23   // Include client library.
24   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') 
25     . '/pz2.js', 'module', 'footer');
26   drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer');
27   drupal_add_js($path . '/recipe.js', 'module', 'footer');
28   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer');
29   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer');
30   $html = theme('mkdru_results');
31   drupal_add_js(array('mkdru' => 
32     array(
33       'settings' => json_encode(variable_get('mkdru_ding', NULL)),
34       'pz2_path' => variable_get('pz2_path', '/pazpar2/search.pz2'),
35       'query' => $keys
36     )
37   ), 'setting');
38   return array("content" => $html, "title" => "Meta Search");
39 }
40
41 /**
42 * Implements hook_perm()
43 */
44 function mkdru_perm() {
45   return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface');
46 }
47
48 /**
49 * Implements hook_access()
50 */
51 function mkdru_access($op, $node, $account) {
52
53   if ($op == 'create') {
54     // Only users with permission to do so may create this node type.
55     return user_access('create metasearch interface', $account);
56   }
57
58   // Users who create a node may edit or delete it later, assuming they have the
59   // necessary permissions.
60   if ($op == 'update' || $op == 'delete') {
61     if (user_access('edit own metasearch interface', $account) && ($account->uid == $node->uid)) {
62       return TRUE;
63     }
64     elseif (user_access('edit any metasearch interface', $account)) {
65       return TRUE;
66     }
67   }
68 }
69
70 /**
71 * Implements hook_menu()
72 */
73 function mkdru_menu() {
74   // Can't get tabbed menus to work in D6. The children work and the tabs
75   // show up there but the top level's url results in it trying to find
76   // the system module's system.inc in mkdru's directory
77 //   $items['admin/settings/mkdru'] = array(
78 //     'title' => 'Pazpar2 Metasearch Settings',
79 //     'description' => 'Settings for mkdru.',
80 //     'access arguments' => array('administer site configuration'),
81 //   );
82 //   $items['admin/settings/mkdru/settings'] = array(
83 //     'title' => 'Settings',
84 //     'description' => 'Settings for mkdru.',
85 //     'page callback' => 'drupal_get_form',
86 //     'page arguments' => array('mkdru_admin_settings'),
87 //     'type' => MENU_DEFAULT_LOCAL_TASK,
88 //     'file' => 'mkdru.admin.inc',
89 //     'weight' => 0,
90 //   );
91 //   $items['admin/settings/mkdru/ding'] = array(
92 //     'title' => 'Ding Integration',
93 //     'description' => 'Search settings for mkdru instance integrated into Ding.',
94 //     'page callback' => 'drupal_get_form',
95 //     'page arguments' => array('mkdru_ding_settings'),
96 //     'type' => MENU_LOCAL_TASK,
97 //     'weight' => 1,
98 //   );
99   $items['admin/settings/mkdru'] = array(
100     'title' => 'Pazpar2 Metasearch Settings',
101     'description' => 'Settings for mkdru.',
102     'page callback' => 'drupal_get_form',
103     'page arguments' => array('mkdru_admin_settings'),
104     'access arguments' => array('administer site configuration'),
105     'type' => MENU_NORMAL_ITEM,
106     'file' => 'mkdru.admin.inc',
107   );
108   $items['admin/settings/mkdru-ding'] = array(
109     'title' => 'Pazpar2 Metasearch Ding Integration',
110     'description' => 'Search settings for mkdru instance integrated into Ding.',
111     'page callback' => 'drupal_get_form',
112     'page arguments' => array('mkdru_ding_settings'),
113     'access arguments' => array('administer site configuration'),
114     'type' => MENU_NORMAL_ITEM,
115   );
116   $items['ahah-mkdru-facet'] = array(
117     'page callback' => 'mkdru_add_facet_callback',
118     'access arguments' => array('create metasearch interface'),
119     'type' => MENU_CALLBACK,
120   );
121   return $items;
122 }
123
124 /**
125 * Implements hook_init()
126 */
127 function mkdru_init() {
128   // Applies our module specific CSS to all pages. This works best because
129   // all CSS is aggregated and cached so we reduce the number of HTTP 
130   // requests and the size is negligible.
131   drupal_add_css(drupal_get_path('module', 'mkdru') .'/mkdru.css');
132 }
133
134
135 // Config form common to node and settings
136 // function mkdru_settings_form($form, &$form_state) {
137 function mkdru_settings_form(&$form_state) {
138   if (isset($form_state['values']['settings'])) {
139     $settings = $form_state['values']['settings'];
140   }
141   elseif (isset($form_state['build_info']['args']['settings'])) {
142     $settings = $form_state['build_info']['args']['settings'];
143   }
144   else {
145     $settings = variable_get('mkdru_defaults', NULL);
146   }
147
148   $form['#cache'] = TRUE;
149
150   $form['settings'] = array(
151     '#tree' => TRUE,
152   );
153
154   $form['settings']['pz2_path'] = array(
155     '#type' => 'textfield',
156     '#title' => t('Pazpar2/Service Proxy path'),
157     '#description' => t('Path that takes Pazpar2 commands via HTTP'),
158     '#required' => TRUE,
159     '#default_value' => $settings['pz2_path'],
160   );
161   $form['settings']['use_sessions'] = array(
162     '#type' => 'checkbox',
163     '#title' => t('Session handling'),
164     '#description' => t('Disable for use with Service Proxy'),
165     '#default_value' => $settings['use_sessions'],
166   );
167
168   $form['settings']['sp']  = array(
169     '#type' => 'fieldset',
170     '#title' => t('Service Proxy specific settings'),
171     '#collapsible' => TRUE,
172     '#collapsed' => FALSE
173   );
174   $form['settings']['sp']['user'] = array(
175     '#type' => 'textfield',
176     '#title' => t('Service Proxy username'),
177     '#description' => t('Service Proxy username'),
178     '#required' => FALSE,
179     '#default_value' => $settings['sp']['user'],
180   );
181   $form['settings']['sp']['pass'] = array(
182     '#type' => 'password',
183     '#title' => t('Service Proxy password'),
184     '#description' => t('Service Proxy password'),
185     '#required' => FALSE,
186     '#default_value' => $settings['sp']['pass'],
187   );
188
189   $form['settings']['facets']  = array(
190     '#type' => 'fieldset',
191     '#title' => t('Facet settings'),
192     // Set up the wrapper so that AJAX will be able to replace the fieldset.
193     '#prefix' => '<div id="mkdru-facets-form-wrapper">', 
194     '#suffix' => '</div>',
195     '#collapsible' => TRUE,
196     '#collapsed' => FALSE
197   );
198
199   foreach (array_keys($settings['facets']) as $facet) {
200     $form['settings']['facets'][$facet]  = array(
201       '#type' => 'fieldset',
202       '#title' => $facet . ' ' . t('facet'),
203       '#collapsible' => TRUE,
204       '#collapsed' => FALSE
205     );
206     $form['settings']['facets'][$facet]['displayName'] = array(
207       '#type' => 'textfield',
208       '#title' => t('Facet name to display in UI'),
209       '#required' => TRUE,
210       '#default_value' => $settings['facets'][$facet]['displayName'],
211     );
212     $form['settings']['facets'][$facet]['pz2Name'] = array(
213       '#type' => 'textfield',
214       '#title' => t('Name of termlist in Pazpar2'),
215       '#required' => TRUE,
216       '#default_value' => $settings['facets'][$facet]['pz2Name'],
217     );
218     $form['settings']['facets'][$facet]['limiter'] = array(
219       '#type' => 'textfield',
220       '#title' => t('Query limiter string'),
221       '#default_value' => $settings['facets'][$facet]['limiter'],
222       '#size' => 5,
223     );
224     $form['settings']['facets'][$facet]['max'] = array(
225       '#type' => 'textfield',
226       '#title' => t('Number of terms to display'),
227       '#required' => TRUE,
228       '#default_value' => $settings['facets'][$facet]['max'],
229       '#size' => 3,
230       '#maxlength' => 3,
231     );
232     $form['settings']['facets'][$facet]['orderWeight'] = array(
233       '#type' => 'textfield',
234       '#title' => t('Facet weight'),
235       '#default_value' => $settings['facets'][$facet]['orderWeight'],
236       '#size' => 3,
237       '#maxlength' => 3,
238     );
239     $form['settings']['facets'][$facet]['remove'] = array(
240       '#type' => 'submit',
241       '#value' => t('Remove ') . $facet . t(' facet'),
242       '#mkdru facet' => $facet,
243       '#submit' => array('mkdru_remove_facet_submit'),
244       '#ahah' => array(
245         'path' => 'ahah-mkdru-facet',
246         'wrapper' => 'mkdru-facets-form-wrapper',
247       ),
248     );
249   }
250   $form['new_facet']  = array(
251     '#type' => 'fieldset',
252     '#title' => t('Add new facet...'),
253     '#tree' => TRUE,
254     '#collapsible' => TRUE,
255     '#collapsed' => FALSE
256   );
257   $form['new_facet']['canonical'] = array(
258     '#type' => 'textfield',
259     '#title' => t('Canonical name of new facet'),
260   );
261   $form['new_facet']['button'] = array(
262     '#type' => 'submit',
263     '#value' => t('Add facet'),
264     '#description' => t('Configure additional facets based on Pazpar2/SP termlists'),
265     '#submit' => array('mkdru_add_facet_submit'),
266     '#ahah' => array(
267       'path' => 'ahah-mkdru-facet',
268       'wrapper' => 'mkdru-facets-form-wrapper',
269     ),
270   );
271   return $form;
272 }
273
274 function mkdru_add_facet_submit($form, &$form_state) {
275   $newfacet = $form_state['values']['new_facet']['canonical'];
276   $form_state['values']['settings']['facets'][$newfacet] = NULL;
277   $form_state['rebuild'] = TRUE;
278   return $form_state['values'];
279 }
280
281 function mkdru_remove_facet_submit($form, &$form_state) {
282   $delfacet = $form_state['clicked_button']['#mkdru facet'];
283   if ($delfacet)
284     unset($form_state['values']['settings']['facets'][$delfacet]);
285 }
286
287 function mkdru_add_facet_callback() {
288   // Necessary to work with hook_form
289   module_load_include('inc', 'node', 'node.pages');
290
291   // Retrieve form from cache
292   $form_state = array('storage' => NULL, 'submitted' => FALSE);
293   $form_build_id = $_POST['form_build_id'];
294   $form = form_get_cache($form_build_id, $form_state);
295
296   // Run drupal_process_form to call submit handler and update $form_state
297   $args = $form['#parameters'];
298   $form_id = array_shift($args);
299   $form_state['post'] = $form['#post'] = $_POST;
300   $form['#programmed'] = $form['#redirect'] = FALSE;
301   drupal_process_form($form_id, $form, $form_state);
302
303   // Regenerate form so we can render the new facet part
304   $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
305
306   // Choose subset of form to redraw.
307   $facet_form = $form['settings']['facets'];
308   // Prevent duplicate wrappers.
309   unset($facet_form['#prefix'], $facet_form['#suffix']);
310   $output = theme('status_messages') . drupal_render($facet_form);
311
312   // Final rendering callback
313   drupal_json(array('status' => TRUE, 'data' => $output));
314 }
315
316
317
318 // Ding config
319 function mkdru_ding_settings(&$form_state) {
320   $form_state['build_info']['args']['settings'] = variable_get('mkdru_ding', NULL);
321   $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
322   $form['settings']['#title'] = t('Default search settings');
323   $form['submit'] = array(
324     '#type' => 'submit',
325     '#value' => 'Save configuration',
326   );
327   return $form;
328 }
329 function mkdru_ding_settings_submit($form, &$form_state) {
330   variable_set('mkdru_ding', $form_state['values']['settings']);
331   drupal_set_message(t('The configuration options have been saved.'));
332 }
333
334
335
336 // Node config
337 /**
338 * Implements hook_form()
339 */
340 function mkdru_form(&$node, &$form_state) {
341   if (isset($node->settings)) {
342     // Second decode parameter indicates associative array
343     $form_state['build_info']['args']['settings'] = json_decode($node->settings, TRUE);
344   }
345
346   $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
347   $type = node_get_types('type', $node);
348   $form['title'] = array(
349     '#type' => 'textfield',
350     '#title' => check_plain($type->title_label),
351     '#required' => FALSE,
352     '#default_value' => $node->title,
353     '#weight' => -5
354   );
355   return $form;
356 }
357
358
359 /**
360 * Implements hook_validate()
361 */
362 function mkdru_validate($node) {
363   // TODO: validation
364 }
365
366 /**
367 * Implements hook_insert().
368 */
369 function mkdru_insert($node) {
370   $record['nid'] =  $node->nid;
371   $record['vid'] =  $node->vid;
372   $record['settings'] =  json_encode($node->settings);
373   drupal_write_record('mkdru', &$record);
374 }
375
376 /**
377 * Implements hook_update().
378 */
379 function mkdru_update($node) {
380   if ($node->revision) {
381     // New revision; treat it as a new record.
382     mkdru_insert($node);
383   }
384   else {
385     $record['nid'] =  $node->nid;
386     $record['vid'] =  $node->vid;
387     $record['settings'] =  json_encode($node->settings);
388     drupal_write_record('mkdru', &$record, 'vid');
389   }
390 }
391
392 /**
393  * Implements hook_nodeapi().
394  *
395  * When a node revision is deleted, we need to remove the corresponding record
396  * from our table. The only way to handle revision deletion is by implementing
397  * hook_nodeapi().
398  */
399 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
400   switch ($op) {
401     case 'delete revision':
402       db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
403       break;
404   }
405 }
406
407 /**
408  * Implements hook_delete().
409  */
410 function mkdru_delete($node) {
411   // Deleting by nid covers all revisions.
412   db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
413 }
414
415
416
417 // Node rendering
418 /**
419 * Implements hook_load()
420 */
421 function mkdru_load($node) {
422   return db_fetch_object(db_query('SELECT * FROM {mkdru} WHERE vid = %d', $node->vid));
423 }
424
425 /**
426 * Implements hook_theme().
427 */
428 function mkdru_theme() {
429   return array(
430     'mkdru_form' => array(
431       'template' => 'mkdru-form',
432       'arguments' => array(),
433     ),
434     'mkdru_results' => array(
435       'template' => 'mkdru-results',
436       'arguments' => array(),
437     ),
438     'mkdru_js' => array(
439       'arguments' => array('node' => NULL),
440     ),
441     'mkdru_block_search' => array(
442       'template' => 'mkdru-block-search',
443       'arguments' => array('nid' => NULL, 'path' => NULL),
444     ),
445     'mkdru_block_facet' => array(
446       'template' => 'mkdru-block-facet',
447       'arguments' => array('class' => NULL)
448     )
449   );
450 }
451
452 /**
453 * Theme function to include Javascript search client and deps
454 */
455 function theme_mkdru_js($node) {
456   $path = drupal_get_path('module', 'mkdru');
457   // Pazpar2 client library.
458   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer', TRUE, TRUE, FALSE);
459   // jQuery plugin for query string/history manipulation.
460   drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer', TRUE, TRUE, FALSE);
461   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE);
462   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE);
463   drupal_add_js(array('mkdru' => $node->mkdru), 'setting');
464   drupal_add_js(array('mkdru' => 
465     array(
466       'settings' => $node->settings,
467     )
468   ), 'setting');
469 }
470
471 /** 
472 * Implements hook_view()
473 */
474 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
475   $node->content['mkdru_js'] = array(
476     '#value' => theme('mkdru_js', $node), 
477     '#weight' => 0,
478   );
479   $node->content['mkdru_form'] = array(
480     '#value' => theme('mkdru_form'), 
481     '#weight' => 1,
482   );
483   $node->content['mkdru_results'] = array(
484     '#value' => theme('mkdru_results'), 
485     '#weight' => 2,
486   );
487   return $node;
488 }
489
490 /** 
491 * Implements hook_block()
492 */
493 function mkdru_block($op='list', $delta='sources', $edit=array()) {
494   switch ($op) {
495     case 'list':
496       // facet blocks
497       // D6 has no setting for note type visibility, set
498       // the default to limit facet display to this type
499       $visPHP = '<?php
500   if (arg(0) == "node" && is_numeric(arg(1))) {
501     $node = node_load(array("nid" => arg(1)));
502     return $node->type == "mkdru";
503   }
504 ?>';
505
506       // NB: block caching is redundant for static content
507       $blocks['mkdru_sources']['info'] = t('mkdru - source facets');
508       $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
509       $blocks['mkdru_sources']['visibility'] = 2;
510       $blocks['mkdru_sources']['pages'] = $visPHP;
511       $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets');
512       $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE;
513       $blocks['mkdru_subjects']['visibility'] = 2;
514       $blocks['mkdru_subjects']['pages'] = $visPHP;
515       $blocks['mkdru_authors']['info'] = t('mkdru - author facets');
516       $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE;
517       $blocks['mkdru_authors']['visibility'] = 2;
518       $blocks['mkdru_authors']['pages'] = $visPHP;
519       // search blocks
520       $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';");
521       while ($node = db_fetch_object($result)) {
522         $blocks['mkdru_search_' . $node->nid]['info'] = 
523            t('mkdru - search box for "' . $node->title . '"');
524         $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
525       };
526       return $blocks;
527
528     case 'view':
529       switch ($delta) {
530         case 'mkdru_sources':
531           $block['subject'] = t('Source');
532           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-source');
533           return $block;
534         case 'mkdru_subjects':
535           $block['subject'] = t('Subject');
536           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-subject');
537           return $block;
538         case 'mkdru_authors':
539           $block['subject'] = t('Author');
540           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-author');
541           return $block;
542     }
543     if (substr($delta, 0, 13) == 'mkdru_search_') {
544       $nid = substr($delta, 13);
545       $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
546       return $block;
547     }
548   }
549 }