Checkbox to set multiLimit option for facets.
[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]['multiLimit'] = array(
225       '#type' => 'checkbox',
226       '#title' => t('Allow multiple limits?'),
227       '#default_value' => $settings['facets'][$facet]['multiLimit'],
228     );
229     $form['settings']['facets'][$facet]['max'] = array(
230       '#type' => 'textfield',
231       '#title' => t('Number of terms to display'),
232       '#required' => TRUE,
233       '#default_value' => $settings['facets'][$facet]['max'],
234       '#size' => 3,
235       '#maxlength' => 3,
236     );
237     $form['settings']['facets'][$facet]['orderWeight'] = array(
238       '#type' => 'textfield',
239       '#title' => t('Facet weight'),
240       '#default_value' => $settings['facets'][$facet]['orderWeight'],
241       '#size' => 3,
242       '#maxlength' => 3,
243     );
244     $form['settings']['facets'][$facet]['remove'] = array(
245       '#type' => 'submit',
246       '#value' => t('Remove ') . $facet . t(' facet'),
247       '#mkdru facet' => $facet,
248       '#submit' => array('mkdru_remove_facet_submit'),
249       '#ahah' => array(
250         'path' => 'ahah-mkdru-facet',
251         'wrapper' => 'mkdru-facets-form-wrapper',
252       ),
253     );
254   }
255   $form['new_facet']  = array(
256     '#type' => 'fieldset',
257     '#title' => t('Add new facet...'),
258     '#tree' => TRUE,
259     '#collapsible' => TRUE,
260     '#collapsed' => FALSE
261   );
262   $form['new_facet']['canonical'] = array(
263     '#type' => 'textfield',
264     '#title' => t('Canonical name of new facet'),
265   );
266   $form['new_facet']['button'] = array(
267     '#type' => 'submit',
268     '#value' => t('Add facet'),
269     '#description' => t('Configure additional facets based on Pazpar2/SP termlists'),
270     '#submit' => array('mkdru_add_facet_submit'),
271     '#ahah' => array(
272       'path' => 'ahah-mkdru-facet',
273       'wrapper' => 'mkdru-facets-form-wrapper',
274     ),
275   );
276   return $form;
277 }
278
279 function mkdru_add_facet_submit($form, &$form_state) {
280   $newfacet = $form_state['values']['new_facet']['canonical'];
281   $form_state['values']['settings']['facets'][$newfacet] = NULL;
282   $form_state['rebuild'] = TRUE;
283   return $form_state['values'];
284 }
285
286 function mkdru_remove_facet_submit($form, &$form_state) {
287   $delfacet = $form_state['clicked_button']['#mkdru facet'];
288   if ($delfacet)
289     unset($form_state['values']['settings']['facets'][$delfacet]);
290 }
291
292 function mkdru_add_facet_callback() {
293   // Necessary to work with hook_form
294   module_load_include('inc', 'node', 'node.pages');
295
296   // Retrieve form from cache
297   $form_state = array('storage' => NULL, 'submitted' => FALSE);
298   $form_build_id = $_POST['form_build_id'];
299   $form = form_get_cache($form_build_id, $form_state);
300
301   // Run drupal_process_form to call submit handler and update $form_state
302   $args = $form['#parameters'];
303   $form_id = array_shift($args);
304   $form_state['post'] = $form['#post'] = $_POST;
305   $form['#programmed'] = $form['#redirect'] = FALSE;
306   drupal_process_form($form_id, $form, $form_state);
307
308   // Regenerate form so we can render the new facet part
309   $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
310
311   // Choose subset of form to redraw.
312   $facet_form = $form['settings']['facets'];
313   // Prevent duplicate wrappers.
314   unset($facet_form['#prefix'], $facet_form['#suffix']);
315   $output = theme('status_messages') . drupal_render($facet_form);
316
317   // Final rendering callback
318   drupal_json(array('status' => TRUE, 'data' => $output));
319 }
320
321
322
323 // Ding config
324 function mkdru_ding_settings(&$form_state) {
325   $form_state['build_info']['args']['settings'] = variable_get('mkdru_ding', NULL);
326   $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
327   $form['settings']['#title'] = t('Default search settings');
328   $form['submit'] = array(
329     '#type' => 'submit',
330     '#value' => 'Save configuration',
331   );
332   return $form;
333 }
334 function mkdru_ding_settings_submit($form, &$form_state) {
335   variable_set('mkdru_ding', $form_state['values']['settings']);
336   drupal_set_message(t('The configuration options have been saved.'));
337 }
338
339
340
341 // Node config
342 /**
343 * Implements hook_form()
344 */
345 function mkdru_form(&$node, &$form_state) {
346   if (isset($node->settings)) {
347     // Second decode parameter indicates associative array
348     $form_state['build_info']['args']['settings'] = json_decode($node->settings, TRUE);
349   }
350
351   $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
352   $type = node_get_types('type', $node);
353   $form['title'] = array(
354     '#type' => 'textfield',
355     '#title' => check_plain($type->title_label),
356     '#required' => FALSE,
357     '#default_value' => $node->title,
358     '#weight' => -5
359   );
360   return $form;
361 }
362
363
364 /**
365 * Implements hook_validate()
366 */
367 function mkdru_validate($node) {
368   // TODO: validation
369 }
370
371 /**
372 * Implements hook_insert().
373 */
374 function mkdru_insert($node) {
375   $record['nid'] =  $node->nid;
376   $record['vid'] =  $node->vid;
377   $record['settings'] =  json_encode($node->settings);
378   drupal_write_record('mkdru', &$record);
379 }
380
381 /**
382 * Implements hook_update().
383 */
384 function mkdru_update($node) {
385   if ($node->revision) {
386     // New revision; treat it as a new record.
387     mkdru_insert($node);
388   }
389   else {
390     $record['nid'] =  $node->nid;
391     $record['vid'] =  $node->vid;
392     $record['settings'] =  json_encode($node->settings);
393     drupal_write_record('mkdru', &$record, 'vid');
394   }
395 }
396
397 /**
398  * Implements hook_nodeapi().
399  *
400  * When a node revision is deleted, we need to remove the corresponding record
401  * from our table. The only way to handle revision deletion is by implementing
402  * hook_nodeapi().
403  */
404 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
405   switch ($op) {
406     case 'delete revision':
407       db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
408       break;
409   }
410 }
411
412 /**
413  * Implements hook_delete().
414  */
415 function mkdru_delete($node) {
416   // Deleting by nid covers all revisions.
417   db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
418 }
419
420
421
422 // Node rendering
423 /**
424 * Implements hook_load()
425 */
426 function mkdru_load($node) {
427   return db_fetch_object(db_query('SELECT * FROM {mkdru} WHERE vid = %d', $node->vid));
428 }
429
430 /**
431 * Implements hook_theme().
432 */
433 function mkdru_theme() {
434   return array(
435     'mkdru_form' => array(
436       'template' => 'mkdru-form',
437       'arguments' => array(),
438     ),
439     'mkdru_results' => array(
440       'template' => 'mkdru-results',
441       'arguments' => array(),
442     ),
443     'mkdru_js' => array(
444       'arguments' => array('node' => NULL),
445     ),
446     'mkdru_block_search' => array(
447       'template' => 'mkdru-block-search',
448       'arguments' => array('nid' => NULL, 'path' => NULL),
449     ),
450     'mkdru_block_facet' => array(
451       'template' => 'mkdru-block-facet',
452       'arguments' => array('class' => NULL)
453     )
454   );
455 }
456
457 /**
458 * Theme function to include Javascript search client and deps
459 */
460 function theme_mkdru_js($node) {
461   $path = drupal_get_path('module', 'mkdru');
462   // Pazpar2 client library.
463   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer', TRUE, TRUE, FALSE);
464   // jQuery plugin for query string/history manipulation.
465   drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer', TRUE, TRUE, FALSE);
466   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE);
467   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE);
468   drupal_add_js(array('mkdru' => $node->mkdru), 'setting');
469   drupal_add_js(array('mkdru' => 
470     array(
471       'settings' => $node->settings,
472     )
473   ), 'setting');
474 }
475
476 /** 
477 * Implements hook_view()
478 */
479 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
480   $node->content['mkdru_js'] = array(
481     '#value' => theme('mkdru_js', $node), 
482     '#weight' => 0,
483   );
484   $node->content['mkdru_form'] = array(
485     '#value' => theme('mkdru_form'), 
486     '#weight' => 1,
487   );
488   $node->content['mkdru_results'] = array(
489     '#value' => theme('mkdru_results'), 
490     '#weight' => 2,
491   );
492   return $node;
493 }
494
495 /** 
496 * Implements hook_block()
497 */
498 function mkdru_block($op='list', $delta='sources', $edit=array()) {
499   switch ($op) {
500     case 'list':
501       // maybe if I label the tables or something?
502       $result = db_query("SELECT title, {mkdru}.nid as nid, settings FROM {node},{mkdru} WHERE {mkdru}.nid = {node}.nid;");
503       while ($node = db_fetch_object($result)) {
504         // search blocks
505         $blocks['mkdru_search_' . $node->nid]['info'] = 
506            t('mkdru - search box for "' . $node->title . '"');
507         // facet blocks
508         $settings = json_decode($node->settings, TRUE);
509         foreach ($settings['facets'] as $facet_name => $facet) {
510           $key = 'mkdru_facet_' . $facet_name . '_' . $node->nid;
511           $blocks[$key]['info'] = 'mkdru - ' . $facet_name
512             . t(' facet for "') . $node->title . '"';
513           $blocks[$key]['visibility'] = 1;
514           $blocks[$key]['pages'] = 'node/' . $node->nid;
515           $blocks[$key]['cache'] = BLOCK_CACHE_GLOBAL;
516         }
517       };
518       return $blocks;
519
520     case 'view':
521       if (preg_match('/^mkdru_facet_(.*)_(\d+)$/', $delta, $matches) > 0) {
522         $facet = $matches[1];
523         $nid = $matches[2];
524
525         // this is rather spendy just to get the title, should really
526         // be able to set a default title in op=list. will be cached though
527         // so I suppose it's okay?
528         $result = db_query("SELECT settings FROM {mkdru} WHERE nid = %d;", $nid);
529         $settingsjson = db_fetch_object($result)->settings;
530         $settings = json_decode($settingsjson, TRUE);
531         if (isset($settings['facets'][$facet]['displayName'])) {
532           $block['subject'] = $settings['facets'][$facet]['displayName'];
533         }
534         $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-' . $facet);
535       }
536       else if (substr($delta, 0, 13) == 'mkdru_search_') {
537         $nid = substr($delta, 13);
538         $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
539       }
540     return $block;
541   }
542 }