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 // 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' =>
33 'settings' => json_encode(variable_get('mkdru_ding', NULL)),
34 'pz2_path' => variable_get('pz2_path', '/pazpar2/search.pz2'),
38 return array("content" => $html, "title" => "Meta Search");
42 * Implements hook_perm()
44 function mkdru_perm() {
45 return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface');
49 * Implements hook_access()
51 function mkdru_access($op, $node, $account) {
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);
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)) {
64 elseif (user_access('edit any metasearch interface', $account)) {
71 * Implements hook_menu()
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'),
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',
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,
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',
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,
116 $items['ahah-mkdru-facet'] = array(
117 'page callback' => 'mkdru_add_facet_callback',
118 'access arguments' => array('create metasearch interface'),
119 'type' => MENU_CALLBACK,
125 * Implements hook_init()
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');
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'];
141 elseif (isset($form_state['build_info']['args']['settings'])) {
142 $settings = $form_state['build_info']['args']['settings'];
145 $settings = variable_get('mkdru_defaults', NULL);
148 $form['#cache'] = TRUE;
150 $form['settings'] = array(
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'),
159 '#default_value' => $settings['pz2_path'],
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'],
168 $form['settings']['sp'] = array(
169 '#type' => 'fieldset',
170 '#title' => t('Service Proxy specific settings'),
171 '#collapsible' => TRUE,
172 '#collapsed' => FALSE
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'],
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'],
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
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
206 $form['settings']['facets'][$facet]['displayName'] = array(
207 '#type' => 'textfield',
208 '#title' => t('Facet name to display in UI'),
210 '#default_value' => $settings['facets'][$facet]['displayName'],
212 $form['settings']['facets'][$facet]['pz2Name'] = array(
213 '#type' => 'textfield',
214 '#title' => t('Name of termlist in Pazpar2'),
216 '#default_value' => $settings['facets'][$facet]['pz2Name'],
218 $form['settings']['facets'][$facet]['limiter'] = array(
219 '#type' => 'textfield',
220 '#title' => t('Query limiter string'),
221 '#default_value' => $settings['facets'][$facet]['limiter'],
224 $form['settings']['facets'][$facet]['max'] = array(
225 '#type' => 'textfield',
226 '#title' => t('Number of terms to display'),
228 '#default_value' => $settings['facets'][$facet]['max'],
232 $form['settings']['facets'][$facet]['orderWeight'] = array(
233 '#type' => 'textfield',
234 '#title' => t('Facet weight'),
235 '#default_value' => $settings['facets'][$facet]['orderWeight'],
239 $form['settings']['facets'][$facet]['remove'] = array(
241 '#value' => t('Remove ') . $facet . t(' facet'),
242 '#mkdru facet' => $facet,
243 '#submit' => array('mkdru_remove_facet_submit'),
245 'path' => 'ahah-mkdru-facet',
246 'wrapper' => 'mkdru-facets-form-wrapper',
250 $form['new_facet'] = array(
251 '#type' => 'fieldset',
252 '#title' => t('Add new facet...'),
254 '#collapsible' => TRUE,
255 '#collapsed' => FALSE
257 $form['new_facet']['canonical'] = array(
258 '#type' => 'textfield',
259 '#title' => t('Canonical name of new facet'),
261 $form['new_facet']['button'] = array(
263 '#value' => t('Add facet'),
264 '#description' => t('Configure additional facets based on Pazpar2/SP termlists'),
265 '#submit' => array('mkdru_add_facet_submit'),
267 'path' => 'ahah-mkdru-facet',
268 'wrapper' => 'mkdru-facets-form-wrapper',
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'];
281 function mkdru_remove_facet_submit($form, &$form_state) {
282 $delfacet = $form_state['clicked_button']['#mkdru facet'];
284 unset($form_state['values']['settings']['facets'][$delfacet]);
287 function mkdru_add_facet_callback() {
288 // Necessary to work with hook_form
289 module_load_include('inc', 'node', 'node.pages');
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);
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);
303 // Regenerate form so we can render the new facet part
304 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
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);
312 // Final rendering callback
313 drupal_json(array('status' => TRUE, 'data' => $output));
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(
325 '#value' => 'Save configuration',
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.'));
338 * Implements hook_form()
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);
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,
360 * Implements hook_validate()
362 function mkdru_validate($node) {
367 * Implements hook_insert().
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);
377 * Implements hook_update().
379 function mkdru_update($node) {
380 if ($node->revision) {
381 // New revision; treat it as a new record.
385 $record['nid'] = $node->nid;
386 $record['vid'] = $node->vid;
387 $record['settings'] = json_encode($node->settings);
388 drupal_write_record('mkdru', &$record, 'vid');
393 * Implements hook_nodeapi().
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
399 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
401 case 'delete revision':
402 db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
408 * Implements hook_delete().
410 function mkdru_delete($node) {
411 // Deleting by nid covers all revisions.
412 db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
419 * Implements hook_load()
421 function mkdru_load($node) {
422 return db_fetch_object(db_query('SELECT * FROM {mkdru} WHERE vid = %d', $node->vid));
426 * Implements hook_theme().
428 function mkdru_theme() {
430 'mkdru_form' => array(
431 'template' => 'mkdru-form',
432 'arguments' => array(),
434 'mkdru_results' => array(
435 'template' => 'mkdru-results',
436 'arguments' => array(),
439 'arguments' => array('node' => NULL),
441 'mkdru_block_search' => array(
442 'template' => 'mkdru-block-search',
443 'arguments' => array('nid' => NULL, 'path' => NULL),
445 'mkdru_block_facet' => array(
446 'template' => 'mkdru-block-facet',
447 'arguments' => array('class' => NULL)
453 * Theme function to include Javascript search client and deps
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' =>
466 'settings' => $node->settings,
472 * Implements hook_view()
474 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
475 $node->content['mkdru_js'] = array(
476 '#value' => theme('mkdru_js', $node),
479 $node->content['mkdru_form'] = array(
480 '#value' => theme('mkdru_form'),
483 $node->content['mkdru_results'] = array(
484 '#value' => theme('mkdru_results'),
491 * Implements hook_block()
493 function mkdru_block($op='list', $delta='sources', $edit=array()) {
496 // maybe if I label the tables or something?
497 $result = db_query("SELECT title, {mkdru}.nid as nid, settings FROM {node},{mkdru} WHERE {mkdru}.nid = {node}.nid;");
498 while ($node = db_fetch_object($result)) {
500 $blocks['mkdru_search_' . $node->nid]['info'] =
501 t('mkdru - search box for "' . $node->title . '"');
503 $settings = json_decode($node->settings, TRUE);
504 foreach ($settings['facets'] as $facet_name => $facet) {
505 $key = 'mkdru_facet_' . $facet_name . '_' . $node->nid;
506 $blocks[$key]['info'] = 'mkdru - ' . $facet_name
507 . t(' facet for "') . $node->title . '"';
508 $blocks[$key]['visibility'] = 1;
509 $blocks[$key]['pages'] = 'node/' . $node->nid;
510 $blocks[$key]['cache'] = BLOCK_CACHE_GLOBAL;
516 if (preg_match('/^mkdru_facet_(.*)_(\d+)$/', $delta, $matches) > 0) {
517 $facet = $matches[1];
520 // this is rather spendy just to get the title, should really
521 // be able to set a default title in op=list. will be cached though
522 // so I suppose it's okay?
523 $result = db_query("SELECT settings FROM {mkdru} WHERE nid = %d;", $nid);
524 $settingsjson = db_fetch_object($result)->settings;
525 $settings = json_decode($settingsjson, TRUE);
526 if (isset($settings['facets'][$facet]['displayName'])) {
527 $block['subject'] = $settings['facets'][$facet]['displayName'];
529 $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-' . $facet);
531 else if (substr($delta, 0, 13) == 'mkdru_search_') {
532 $nid = substr($delta, 13);
533 $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);