Facet removal
[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   else if (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'),
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   dpm($form_state);
283   $delfacet = $form_state['clicked_button']['#mkdru facet'];
284   if ($delfacet)
285     unset($form_state['values']['settings']['facets'][$delfacet]);
286 }
287
288 function mkdru_add_facet_callback() {
289   // Necessary to work with hook_form
290   module_load_include('inc', 'node', 'node.pages');
291
292   // Retrieve form from cache
293   $form_state = array('storage' => NULL, 'submitted' => FALSE);
294   $form_build_id = $_POST['form_build_id'];
295   $form = form_get_cache($form_build_id, $form_state);
296
297   // Run drupal_process_form to call submit handler and update $form_state
298   $args = $form['#parameters'];
299   $form_id = array_shift($args);
300   $form_state['post'] = $form['#post'] = $_POST;
301   $form['#programmed'] = $form['#redirect'] = FALSE;
302   drupal_process_form($form_id, $form, $form_state);
303
304   // Regenerate form so we can render the new facet part
305   $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
306
307   // Choose subset of form to redraw.
308   $facet_form = $form['settings']['facets'];
309   // Prevent duplicate wrappers.
310   unset($facet_form['#prefix'], $facet_form['#suffix']);
311   $output = theme('status_messages') . drupal_render($facet_form);
312
313   // Final rendering callback
314   drupal_json(array('status' => TRUE, 'data' => $output));
315 }
316
317
318
319 // Ding config
320 function mkdru_ding_settings(&$form_state) {
321   $form_state['build_info']['args']['settings'] = variable_get('mkdru_ding', NULL);
322   $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
323   $form['settings']['#title'] = t('Default search settings');
324   $form['submit'] = array(
325     '#type' => 'submit',
326     '#value' => 'Save configuration',
327   );
328   return $form;
329 }
330 function mkdru_ding_settings_submit($form, &$form_state) {
331   variable_set('mkdru_ding', $form_state[values][settings]);
332   drupal_set_message(t('The configuration options have been saved.'));
333 }
334
335
336
337 // Node config
338 /**
339 * Implements hook_form()
340 */
341 function mkdru_form(&$node, &$form_state) {
342   if (isset($node->settings)) {
343     // Second decode parameter indicates associative array
344     $form_state['build_info']['args']['settings'] = json_decode($node->settings, TRUE);
345   }
346
347   $form = drupal_retrieve_form('mkdru_settings_form', &$form_state);
348   $type = node_get_types('type', $node);
349   $form['title'] = array(
350     '#type' => 'textfield',
351     '#title' => check_plain($type->title_label),
352     '#required' => FALSE,
353     '#default_value' => $node->title,
354     '#weight' => -5
355   );
356   return $form;
357 }
358
359
360 /**
361 * Implements hook_validate()
362 */
363 function mkdru_validate($node) {
364   // TODO: validation
365 }
366
367 /**
368 * Implements hook_insert().
369 */
370 function mkdru_insert($node) {
371   $record['nid'] =  $node->nid;
372   $record['vid'] =  $node->vid;
373   $record['settings'] =  json_encode($node->settings);
374   drupal_write_record('mkdru', &$record);
375 }
376
377 /**
378 * Implements hook_update().
379 */
380 function mkdru_update($node) {
381   if ($node->revision) {
382     // New revision; treat it as a new record.
383     mkdru_insert($node);
384   }
385   else {
386     $record['nid'] =  $node->nid;
387     $record['vid'] =  $node->vid;
388     $record['settings'] =  json_encode($node->settings);
389     drupal_write_record('mkdru', &$record, 'vid');
390   }
391 }
392
393 /**
394  * Implements hook_nodeapi().
395  *
396  * When a node revision is deleted, we need to remove the corresponding record
397  * from our table. The only way to handle revision deletion is by implementing
398  * hook_nodeapi().
399  */
400 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
401   switch ($op) {
402     case 'delete revision':
403       db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
404       break;
405   }
406 }
407
408 /**
409  * Implements hook_delete().
410  */
411 function mkdru_delete($node) {
412   // Deleting by nid covers all revisions.
413   db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
414 }
415
416
417
418 // Node rendering
419 /**
420 * Implements hook_load()
421 */
422 function mkdru_load($node) {
423   return db_fetch_object(db_query('SELECT * FROM {mkdru} WHERE vid = %d', $node->vid));
424 }
425
426 /**
427 * Implements hook_theme().
428 */
429 function mkdru_theme() {
430   return array(
431     'mkdru_form' => array(
432       'template' => 'mkdru-form',
433       'arguments' => array(),
434     ),
435     'mkdru_results' => array(
436       'template' => 'mkdru-results',
437       'arguments' => array(),
438     ),
439     'mkdru_js' => array(
440       'arguments' => array('node' => NULL),
441     ),
442     'mkdru_block_search' => array(
443       'template' => 'mkdru-block-search',
444       'arguments' => array('nid' => NULL, 'path' => NULL),
445     ),
446     'mkdru_block_facet' => array(
447       'template' => 'mkdru-block-facet',
448       'arguments' => array('class' => NULL)
449     )
450   );
451 }
452
453 /**
454 * Theme function to include Javascript search client and deps
455 */
456 function theme_mkdru_js($node) {
457   $path = drupal_get_path('module', 'mkdru');
458   // Pazpar2 client library.
459   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer', TRUE, TRUE, FALSE);
460   // jQuery plugin for query string/history manipulation.
461   drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer', TRUE, TRUE, FALSE);
462   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE);
463   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE);
464   drupal_add_js(array('mkdru' => $node->mkdru), 'setting');
465   drupal_add_js(array('mkdru' => 
466     array(
467       'settings' => $node->settings,
468     )
469   ), 'setting');
470 }
471
472 /** 
473 * Implements hook_view()
474 */
475 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
476   $node->content['mkdru_js'] = array(
477     '#value' => theme('mkdru_js', $node), 
478     '#weight' => 0,
479   );
480   $node->content['mkdru_form'] = array(
481     '#value' => theme('mkdru_form'), 
482     '#weight' => 1,
483   );
484   $node->content['mkdru_results'] = array(
485     '#value' => theme('mkdru_results'), 
486     '#weight' => 2,
487   );
488   return $node;
489 }
490
491 /** 
492 * Implements hook_block()
493 */
494 function mkdru_block($op='list', $delta='sources', $edit=array()) {
495   switch ($op) {
496     case 'list':
497       // facet blocks
498       // D6 has no setting for note type visibility, set
499       // the default to limit facet display to this type
500       $visPHP = '<?php
501   if (arg(0) == "node" && is_numeric(arg(1))) {
502     $node = node_load(array("nid" => arg(1)));
503     return $node->type == "mkdru";
504   }
505 ?>';
506
507       // NB: block caching is redundant for static content
508       $blocks['mkdru_sources']['info'] = t('mkdru - source facets');
509       $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
510       $blocks['mkdru_sources']['visibility'] = 2;
511       $blocks['mkdru_sources']['pages'] = $visPHP;
512       $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets');
513       $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE;
514       $blocks['mkdru_subjects']['visibility'] = 2;
515       $blocks['mkdru_subjects']['pages'] = $visPHP;
516       $blocks['mkdru_authors']['info'] = t('mkdru - author facets');
517       $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE;
518       $blocks['mkdru_authors']['visibility'] = 2;
519       $blocks['mkdru_authors']['pages'] = $visPHP;
520       // search blocks
521       $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';");
522       while ($node = db_fetch_object($result)) {
523         $blocks['mkdru_search_' . $node->nid]['info'] = 
524            t('mkdru - search box for "' . $node->title . '"');
525         $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
526       };
527       return $blocks;
528
529     case 'view':
530       switch ($delta) {
531         case 'mkdru_sources':
532           $block['subject'] = t('Source');
533           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-source');
534           return $block;
535         case 'mkdru_subjects':
536           $block['subject'] = t('Subject');
537           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-subject');
538           return $block;
539         case 'mkdru_authors':
540           $block['subject'] = t('Author');
541           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-author');
542           return $block;
543     }
544     if (substr($delta, 0, 13) == 'mkdru_search_') {
545       $nid = substr($delta, 13);
546       $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
547       return $block;
548     }
549   }
550 }