e477eabe93cfaf3ce4fc5d75781a723a03e5bc29
[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       'base'        => 'mkdru',
15       'description' => t("Metasearch interface for Z39.50/SRU and other targets via a Pazpar2/Service Proxy backend"),
16     )
17   );
18 }
19
20 /**
21  * Implements hook_search_info()
22  */
23 function mkdru_search_info() {
24   return array(
25     'title'               => 'Meta search',
26     'path'                => 'meta',
27     'conditions_callback' => 'mkdru_search_conditions_callback',
28   );
29 }
30
31 /**
32  * Implements hook_search_page()
33  */
34 function mkdru_search_page($results) {
35   $output['prefix']['#markup'] = theme('mkdru_results');
36   $output['suffix']['#markup'] = '';
37   return $output;
38 }
39
40 /**
41  * Implements hook_ding_facetbrowser()
42  */
43 function mkdru_ding_facetbrowser() {
44         $results             = new stdClass();
45   $results->facets     = array();
46   $results->show_empty = TRUE; # Show an empty facetbrowser block, even if search didn't return any results
47   return $results;
48 }
49
50 /**
51  * Search callback function that is invoked by search_view()
52  */
53 function mkdru_search_conditions_callback($keys) {}
54
55 /**
56  * Implement hook_search_execute()
57  */
58 function mkdru_search_execute($keys = NULL, $conditions = NULL) {
59   $path = drupal_get_path('module', 'mkdru');
60   // Include client library.
61   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js',
62     array('type' => 'file', 'scope' => 'footer'));
63   drupal_add_library('overlay', 'jquery-bbq');
64   drupal_add_js($path . '/mkdru.theme.js',
65     array('type' => 'file', 'scope' => 'footer'));
66   drupal_add_js($path . '/mkdru.client.js',
67     array('type' => 'file', 'scope' => 'footer'));
68   drupal_add_js(array('mkdru' =>
69     array(
70       'use_sessions' => variable_get('use_sessions', '1'),
71       'pz2_path'     => variable_get('pz2_path', '/pazpar2/search.pz2'),
72       'sp_user'      => variable_get('sp_user', ''),
73       'sp_pass'      => variable_get('sp_pass', ''),
74       'query'        => $keys,
75     )
76   ), 'setting');
77
78   return array();
79 }
80
81 /**
82 * Implements hook_permission()
83 */
84 function mkdru_permission() {
85   return array(
86     'administer metasearch interfaces' => array(
87       'title' => t('Administer Pazpar2 metasearch integration'),
88     ),
89     'create metasearch interface' => array(
90       'title' => t('Create metasearch interface'),
91     ),
92     'edit any metasearch interface' => array(
93       'title' => t('Edit any metasearch interface'),
94     ),
95     'edit own metasearch interface' => array(
96       'title' => t('Edit own metasearch interface'),
97     ),
98   );
99 }
100
101 /**
102 * Implements hook_node_access()
103 */
104 function mkdru_node_access($node, $op, $account) {
105   if ($op == 'create') {
106     // Only users with permission to do so may create this node type.
107     return user_access('create metasearch interface', $account);
108   }
109
110   // Users who create a node may edit or delete it later, assuming they have the
111   // necessary permissions.
112   if ($op == 'update' || $op == 'delete') {
113     if (user_access('edit own metasearch interface', $account) && ($account->uid == $node->uid)) {
114       return TRUE;
115     }
116     elseif (user_access('edit any metasearch interface', $account)) {
117       return TRUE;
118     }
119   }
120 }
121
122 /**
123 * Implements hook_menu()
124 */
125 function mkdru_menu() {
126   $items['admin/settings/mkdru'] = array(
127     'title' => 'mkdru Settings',
128     'description' => 'Settings for mkdru.',
129     'page callback' => 'drupal_get_form',
130     'page arguments' => array('mkdru_admin_settings'),
131     'access arguments' => array('administer site configuration'),
132     'type' => MENU_NORMAL_ITEM,
133     'file' => 'mkdru.admin.inc',
134   );
135   return $items;
136 }
137
138 /**
139 * Implementation of hook_init()
140 */
141 function mkdru_init() {
142   // Applies our module specific CSS to all pages. This works best because
143   // all CSS is aggregated and cached so we reduce the number of HTTP 
144   // requests and the size is negligible.
145   drupal_add_css(drupal_get_path('module', 'mkdru') .'/mkdru.css');
146 }
147
148
149
150 // Node config
151 /**
152 * Implements hook_form()
153 */
154 function mkdru_form(&$node, $form_state) {
155   $type = node_type_get_type($node);
156
157   $form['title'] = array(
158     '#type' => 'textfield',
159     '#title' => check_plain($type->title_label),
160     '#required' => FALSE,
161     '#default_value' => $node->title,
162     '#weight' => -5
163   );
164
165   $form['search_settings']  = array(
166     '#type' => 'fieldset',
167     '#title' => t('Pazpar2/Service Proxy search settings'),
168     '#collapsible' => TRUE,
169     '#collapsed' => FALSE
170   );
171   $form['search_settings']['pz2_path'] = array(
172     '#type' => 'textfield',
173     '#title' => t('Pazpar2/Service Proxy path'),
174     '#description' => t('Path that takes Pazpar2 commands via HTTP'),
175     '#required' => TRUE,
176     '#default_value' => isset($node->mkdru->pz2_path) ? $node->mkdru->pz2_path : '/pazpar2/search.pz2',
177   );
178   $form['search_settings']['use_sessions'] = array(
179     '#type' => 'checkbox',
180     '#title' => t('Session handling'),
181     '#description' => t('Disable for use with Service Proxy'),
182     '#default_value' => isset($node->mkdru->use_sessions) ? $node->mkdru->use_sessions : 1,
183   );
184
185   $form['sp_settings']  = array(
186     '#type' => 'fieldset',
187     '#title' => t('Service Proxy specific settings'),
188     '#collapsible' => TRUE,
189     '#collapsed' => TRUE
190   );
191   $form['sp_settings']['sp_user'] = array(
192     '#type' => 'textfield',
193     '#title' => t('Service Proxy username'),
194     '#description' => t('Service Proxy username'),
195     '#required' => FALSE,
196     '#default_value' => isset($node->mkdru->sp_user) ? 
197       $node->mkdru->sp_user : '',
198   );
199   $form['sp_settings']['sp_pass'] = array(
200     '#type' => 'textfield',
201     '#title' => t('Service Proxy password'),
202     '#description' => t('Service Proxy password'),
203     '#required' => FALSE,
204     '#default_value' => isset($node->mkdru->sp_pass) ? 
205       $node->mkdru->sp_pass : '',
206   );
207
208   $form['display_settings']  = array(
209     '#type' => 'fieldset',
210     '#title' => t('Display settings'),
211     '#collapsible' => TRUE,
212     '#collapsed' => FALSE
213   );
214   $form['display_settings']['source_max'] = array(
215     '#type' => 'textfield',
216     '#title' => t('Number of sources to display'),
217     '#required' => TRUE,
218     '#default_value' => isset($node->mkdru->source_max) ? $node->mkdru->source_max : 10,
219     '#size' => 3,
220     '#maxlength' => 3,
221   );
222   $form['display_settings']['author_max'] = array(
223     '#type' => 'textfield',
224     '#title' => t('Number of authors to display'),
225     '#required' => TRUE,
226     '#default_value' => isset($node->mkdru->author_max) ? $node->mkdru->author_max : 10,
227     '#size' => 3,
228     '#maxlength' => 3,
229   );
230   $form['display_settings']['subject_max'] = array(
231     '#type' => 'textfield',
232     '#title' => t('Number of subjects to display'),
233     '#required' => TRUE,
234     '#default_value' => isset($node->mkdru->subject_max) ? $node->mkdru->subject_max : 10,
235     '#size' => 3,
236     '#maxlength' => 3,
237   );
238   return $form;
239 }
240
241
242 /**
243 * Implements hook_validate()
244 */
245 function mkdru_validate($node) {
246   if (!is_numeric($node->source_max)) {
247     form_set_error('source_max', t('Please enter a number.'));
248   }
249   if (!is_numeric($node->author_max)) {
250     form_set_error('author_max', t('Please enter a number.'));
251   }
252   if (!is_numeric($node->subject_max)) {
253     form_set_error('subject_max', t('Please enter a number.'));
254   }
255 }
256
257 /**
258 * Implements hook_insert().
259 */
260 function mkdru_insert($node) {
261   drupal_write_record('mkdru', $node);
262 }
263
264 /**
265 * Implements hook_update().
266 */
267 function mkdru_update($node) {
268   if ($node->revision) {
269     // New revision; treat it as a new record.
270     mkdru_insert($node);
271   }
272   else {
273     drupal_write_record('mkdru', $node, 'vid');
274   }
275 }
276
277 /**
278  * Implements hook_node_revision_delete()
279  */
280 function mkdru_node_revision_delete($node) {
281   db_delete('mkdru')
282     ->condition('vid', $node->vid)
283     ->execute();
284 }
285
286 /**
287  * Implements hook_delete()
288  */
289 function mkdru_delete($node) {
290   // Deleting by nid covers all revisions.
291   db_delete('mkdru')
292     ->condition('nid', $node->nid)
293     ->execute();
294 }
295
296
297
298 // Node rendering
299 /**
300 * Implements hook_load()
301 */
302 function mkdru_load($nodes) {
303   $result = db_query('SELECT * FROM {mkdru} WHERE nid IN (:nids)', array(':nids' => array_keys($nodes)));
304   foreach ($result as $record) {
305     $nodes[$record->nid]->mkdru = $record;
306   }
307 }
308
309 /**
310 * Implements hook_theme().
311 */
312 function mkdru_theme() {
313   return array(
314     'mkdru_form' => array(
315       'template' => 'mkdru-form',
316       'variables' => array(),
317     ),
318     'mkdru_results' => array(
319       'template' => 'mkdru-results',
320       'variables' => array(),
321     ),
322     'mkdru_js' => array(
323       'variables' => array('node' => NULL),
324     ),
325     'mkdru_block_search' => array(
326       'template' => 'mkdru-block-search',
327       'variables' => array('nid' => NULL, 'path' => NULL),
328     ),
329     'mkdru_block_facet' => array(
330       'template' => 'mkdru-block-facet',
331       'variables' => array('class' => NULL)
332     )
333   );
334 }
335
336 /**
337 * Theme function to include Javascript search client and deps
338 */
339 function theme_mkdru_js(&$variables) {
340   $path = drupal_get_path('module', 'mkdru');
341   // Pazpar2 client library
342   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', array(
343     'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
344   // jQuery plugin for query string/history manipulation.
345   drupal_add_library('system', 'jquery.bbq');
346   drupal_add_js($path . '/mkdru.theme.js', array(
347     'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
348   drupal_add_js($path . '/mkdru.client.js', array(
349     'type' => 'file', 'scope' => 'footer', 'defer' => TRUE, 'preprocess' => FALSE));
350   drupal_add_js(array('mkdru' => $variables['node']->mkdru), 'setting');
351 }
352
353 /** 
354 * Implements hook_view()
355 */
356 function mkdru_view($node, $view_mode) {
357   if ($view_mode == 'full') {
358     $node->content['mkdru_js'] = array(
359       '#markup' => theme('mkdru_js', array('node' => $node)), 
360       '#weight' => 0,
361     );
362     $node->content['mkdru_form'] = array(
363       '#markup' => theme('mkdru_form'), 
364       '#weight' => 1,
365     );
366     $node->content['mkdru_results'] = array(
367       '#markup' => theme('mkdru_results'), 
368       '#weight' => 2,
369     );
370   }
371   return $node;
372 }
373
374
375
376 // Blocks
377 /** 
378 * Implements hook_block_info()
379 */
380 function mkdru_block_info() {
381   // facet blocks
382   $facets = variable_get('mkdru_facets');
383   foreach ($facets as $facet) {
384     // NB: block caching is redundant for static content
385     $blocks['mkdru_facet_' . $facet]['info'] = "mkdru - $facet " . t('facet');
386     $blocks['mkdru_facet_' . $facet]['cache'] = DRUPAL_NO_CACHE;
387   }
388
389   // search blocks
390   $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';");
391   foreach($result as $node) {
392     $blocks['mkdru_search_' . $node->nid]['info'] = 
393         t('mkdru - search box for "' . $node->title . '"');
394     $blocks['mkdru_sources']['cache'] = DRUPAL_NO_CACHE;
395   };
396
397   return $blocks;
398 }
399
400 /** 
401 * Implements hook_block_view()
402 */
403 function mkdru_block_view($delta) {
404   if (substr($delta, 0, 12) == 'mkdru_facet_') {
405     $facet = substr($delta, 12);
406     $block['subject'] = t(ucwords($facet));
407     $block['content'] = theme('mkdru_block_facet',
408                                array('class' => 'mkdru-facet-' . $facet));
409     return $block;
410   }
411   elseif (substr($delta, 0, 13) == 'mkdru_search_') {
412     $nid = substr($delta, 13);
413     $block['content'] = theme('mkdru_block_search',
414         array('nid' => $nid, 'path' => '/node/' . $nid));
415     return $block;
416   }
417 }