remove the class from the parent
[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   // 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',
30        TRUE, TRUE, FALSE);
31      error_log("Pz2.js added as alias script '".$pz2_js_path);  
32   } else {
33     drupal_add_js($path.'/'.$pz2_js_path.'/pz2.js', 'module', 'footer');
34   }
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' => 
41     array(
42       'settings' => json_encode(variable_get('mkdru_ding', NULL)),
43       'pz2_path' => variable_get('pz2_path', '/pazpar2/search.pz2'),
44       'query' => $keys
45     )
46   ), 'setting');
47   return array("content" => $html, "title" => "Meta Search");
48 }
49
50 /**
51 * Implements hook_perm()
52 */
53 function mkdru_perm() {
54   return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface');
55 }
56
57 /**
58 * Implements hook_access()
59 */
60 function mkdru_access($op, $node, $account) {
61
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);
65   }
66
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)) {
71       return TRUE;
72     }
73     elseif (user_access('edit any metasearch interface', $account)) {
74       return TRUE;
75     }
76   }
77 }
78
79 /**
80 * Implements hook_menu()
81 */
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'),
90 //   );
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',
98 //     'weight' => 0,
99 //   );
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,
106 //     'weight' => 1,
107 //   );
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',
116   );
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,
124   );
125   $items['ahah-mkdru-facet'] = array(
126     'page callback' => 'mkdru_add_facet_callback',
127     'access arguments' => array('create metasearch interface'),
128     'type' => MENU_CALLBACK,
129   );
130   return $items;
131 }
132
133 /**
134 * Implements hook_init()
135 */
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');
141 }
142
143
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'];
149   }
150   elseif (isset($form_state['build_info']['args']['settings'])) {
151     $settings = $form_state['build_info']['args']['settings'];
152   }
153   else {
154     $settings = variable_get('mkdru_defaults', NULL);
155   }
156
157   $form['#cache'] = TRUE;
158
159   $form['settings'] = array(
160     '#tree' => TRUE,
161   );
162
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'),
167     '#required' => TRUE,
168     '#default_value' => $settings['pz2_path'],
169   );
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'],
175   );
176
177   $form['settings']['sp']  = array(
178     '#type' => 'fieldset',
179     '#title' => t('Service Proxy specific settings'),
180     '#collapsible' => TRUE,
181     '#collapsed' => FALSE
182   );
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'],
189   );
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'],
196   );
197
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
206   );
207
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
214     );
215     $form['settings']['facets'][$facet]['displayName'] = array(
216       '#type' => 'textfield',
217       '#title' => t('Facet name to display in UI'),
218       '#required' => TRUE,
219       '#default_value' => $settings['facets'][$facet]['displayName'],
220     );
221     $form['settings']['facets'][$facet]['pz2Name'] = array(
222       '#type' => 'textfield',
223       '#title' => t('Name of termlist in Pazpar2'),
224       '#required' => TRUE,
225       '#default_value' => $settings['facets'][$facet]['pz2Name'],
226     );
227     $form['settings']['facets'][$facet]['limiter'] = array(
228       '#type' => 'textfield',
229       '#title' => t('Query limiter string'),
230       '#default_value' => $settings['facets'][$facet]['limiter'],
231       '#size' => 5,
232     );
233     $form['settings']['facets'][$facet]['multiLimit'] = array(
234       '#type' => 'checkbox',
235       '#title' => t('Allow multiple limits?'),
236       '#default_value' => $settings['facets'][$facet]['multiLimit'],
237     );
238     $form['settings']['facets'][$facet]['max'] = array(
239       '#type' => 'textfield',
240       '#title' => t('Number of terms to display'),
241       '#required' => TRUE,
242       '#default_value' => $settings['facets'][$facet]['max'],
243       '#size' => 3,
244       '#maxlength' => 3,
245     );
246     $form['settings']['facets'][$facet]['orderWeight'] = array(
247       '#type' => 'textfield',
248       '#title' => t('Facet weight'),
249       '#default_value' => $settings['facets'][$facet]['orderWeight'],
250       '#size' => 3,
251       '#maxlength' => 3,
252     );
253     $form['settings']['facets'][$facet]['remove'] = array(
254       '#type' => 'submit',
255       '#value' => t('Remove ') . $facet . t(' facet'),
256       '#mkdru facet' => $facet,
257       '#submit' => array('mkdru_remove_facet_submit'),
258       '#ahah' => array(
259         'path' => 'ahah-mkdru-facet',
260         'wrapper' => 'mkdru-facets-form-wrapper',
261       ),
262     );
263   }
264   $form['new_facet']  = array(
265     '#type' => 'fieldset',
266     '#title' => t('Add new facet...'),
267     '#tree' => TRUE,
268     '#collapsible' => TRUE,
269     '#collapsed' => FALSE
270   );
271   $form['new_facet']['canonical'] = array(
272     '#type' => 'textfield',
273     '#title' => t('Canonical name of new facet'),
274   );
275   $form['new_facet']['button'] = array(
276     '#type' => 'submit',
277     '#value' => t('Add facet'),
278     '#description' => t('Configure additional facets based on Pazpar2/SP termlists'),
279     '#submit' => array('mkdru_add_facet_submit'),
280     '#ahah' => array(
281       'path' => 'ahah-mkdru-facet',
282       'wrapper' => 'mkdru-facets-form-wrapper',
283     ),
284   );
285   return $form;
286 }
287
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'];
293 }
294
295 function mkdru_remove_facet_submit($form, &$form_state) {
296   $delfacet = $form_state['clicked_button']['#mkdru facet'];
297   if ($delfacet)
298     unset($form_state['values']['settings']['facets'][$delfacet]);
299 }
300
301 function mkdru_add_facet_callback() {
302   // Necessary to work with hook_form
303   module_load_include('inc', 'node', 'node.pages');
304
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);
309
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);
316
317   // Regenerate form so we can render the new facet part
318   $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
319
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);
325
326   // Final rendering callback
327   drupal_json(array('status' => TRUE, 'data' => $output));
328 }
329
330
331
332 // Ding config
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(
338     '#type' => 'submit',
339     '#value' => 'Save configuration',
340   );
341   return $form;
342 }
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.'));
346 }
347
348
349
350 // Node config
351 /**
352 * Implements hook_form()
353 */
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);
358   }
359
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,
367     '#weight' => -5
368   );
369   return $form;
370 }
371
372
373 /**
374 * Implements hook_validate()
375 */
376 function mkdru_validate($node) {
377   // TODO: validation
378 }
379
380 /**
381 * Implements hook_insert().
382 */
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);
388 }
389
390 /**
391 * Implements hook_update().
392 */
393 function mkdru_update($node) {
394   if ($node->revision) {
395     // New revision; treat it as a new record.
396     mkdru_insert($node);
397   }
398   else {
399     $record['nid'] =  $node->nid;
400     $record['vid'] =  $node->vid;
401     $record['settings'] =  json_encode($node->settings);
402     drupal_write_record('mkdru', &$record, 'vid');
403   }
404 }
405
406 /**
407  * Implements hook_nodeapi().
408  *
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
411  * hook_nodeapi().
412  */
413 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
414   switch ($op) {
415     case 'delete revision':
416       db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
417       break;
418   }
419 }
420
421 /**
422  * Implements hook_delete().
423  */
424 function mkdru_delete($node) {
425   // Deleting by nid covers all revisions.
426   db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
427 }
428
429
430
431 // Node rendering
432 /**
433 * Implements hook_load()
434 */
435 function mkdru_load($node) {
436   return db_fetch_object(db_query('SELECT * FROM {mkdru} WHERE vid = %d', $node->vid));
437 }
438
439 /**
440 * Implements hook_theme().
441 */
442 function mkdru_theme() {
443   return array(
444     'mkdru_form' => array(
445       'template' => 'mkdru-form',
446       'arguments' => array(),
447     ),
448     'mkdru_results' => array(
449       'template' => 'mkdru-results',
450       'arguments' => array(),
451     ),
452     'mkdru_js' => array(
453       'arguments' => array('node' => NULL),
454     ),
455     'mkdru_block_search' => array(
456       'template' => 'mkdru-block-search',
457       'arguments' => array('nid' => NULL, 'path' => NULL),
458     ),
459     'mkdru_block_facet' => array(
460       'template' => 'mkdru-block-facet',
461       'arguments' => array('class' => NULL)
462     )
463   );
464 }
465
466 /**
467 * Theme function to include Javascript search client and deps
468 */
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' => 
479     array(
480       'settings' => $node->settings,
481     )
482   ), 'setting');
483 }
484
485 /** 
486 * Implements hook_view()
487 */
488 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
489   $node->content['mkdru_js'] = array(
490     '#value' => theme('mkdru_js', $node), 
491     '#weight' => 0,
492   );
493   $node->content['mkdru_form'] = array(
494     '#value' => theme('mkdru_form'), 
495     '#weight' => 1,
496   );
497   $node->content['mkdru_results'] = array(
498     '#value' => theme('mkdru_results'), 
499     '#weight' => 2,
500   );
501   return $node;
502 }
503
504 /** 
505 * Implements hook_block()
506 */
507 function mkdru_block($op='list', $delta='sources', $edit=array()) {
508   switch ($op) {
509     case 'list':
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)) {
513         // search blocks
514         $blocks['mkdru_search_' . $node->nid]['info'] = 
515            t('mkdru - search box for "' . $node->title . '"');
516         // facet blocks
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;
525         }
526       };
527       return $blocks;
528
529     case 'view':
530       if (preg_match('/^mkdru_facet_(.*)_(\d+)$/', $delta, $matches) > 0) {
531         $facet = $matches[1];
532         $nid = $matches[2];
533
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'];
542         }
543         $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-' . $facet);
544       }
545       else if (substr($delta, 0, 13) == 'mkdru_search_') {
546         $nid = substr($delta, 13);
547         $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
548       }
549     return $block;
550   }
551 }