Ignore bytarget
[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_show($params) {
21    $path = drupal_get_path('module', 'mkdru');
22   // Include client library.
23   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') 
24     . '/pz2.js', 'module', 'footer');
25   drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer');
26   drupal_add_js($path . '/recipe.js', 'module', 'footer');
27   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer');
28   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer');
29   $html = theme('mkdru_results'); 
30   drupal_add_js(array('mkdru' => 
31     array(
32       'use_sessions' => variable_get('use_sessions', '1'),
33       'pz2_path' => variable_get('pz2_path', '/pazpar2/search.pz2'),
34       'sp_user' => variable_get('sp_user', ''),
35       'sp_pass' => variable_get('sp_pass', ''),
36       'query' => $params['keys']
37     )
38   ), 'setting');
39   return array("content" => $html);
40 }
41
42 /**
43 * Implements hook_perm()
44 */
45 function mkdru_perm() {
46   return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface');
47 }
48
49 /**
50 * Implements hook_access()
51 */
52 function mkdru_access($op, $node, $account) {
53
54   if ($op == 'create') {
55     // Only users with permission to do so may create this node type.
56     return user_access('create metasearch interface', $account);
57   }
58
59   // Users who create a node may edit or delete it later, assuming they have the
60   // necessary permissions.
61   if ($op == 'update' || $op == 'delete') {
62     if (user_access('edit own metasearch interface', $account) && ($account->uid == $node->uid)) {
63       return TRUE;
64     }
65     elseif (user_access('edit any metasearch interface', $account)) {
66       return TRUE;
67     }
68   }
69 }
70
71 /**
72 * Implements hook_menu()
73 */
74 function mkdru_menu() {
75   $items['admin/settings/mkdru'] = array(
76     'title' => 'mkdru Settings',
77     'description' => 'Settings for mkdru.',
78     'page callback' => 'drupal_get_form',
79     'page arguments' => array('mkdru_admin_settings'),
80     'access arguments' => array('administer site configuration'),
81     'type' => MENU_NORMAL_ITEM,
82     'file' => 'mkdru.admin.inc',
83   );
84   return $items;
85 }
86
87 /**
88 * Implements hook_init()
89 */
90 function mkdru_init() {
91   // Applies our module specific CSS to all pages. This works best because
92   // all CSS is aggregated and cached so we reduce the number of HTTP 
93   // requests and the size is negligible.
94   drupal_add_css(drupal_get_path('module', 'mkdru') .'/mkdru.css');
95 }
96
97
98
99 // Node config
100 /**
101 * Implements hook_form()
102 */
103 function mkdru_form(&$node, $form_state) {
104   $type = node_get_types('type', $node);
105
106   $form['title'] = array(
107     '#type' => 'textfield',
108     '#title' => check_plain($type->title_label),
109     '#required' => FALSE,
110     '#default_value' => $node->title,
111     '#weight' => -5
112   );
113
114   $form['search_settings']  = array(
115     '#type' => 'fieldset',
116     '#title' => t('Pazpar2/Service Proxy search settings'),
117     '#collapsible' => TRUE,
118     '#collapsed' => FALSE
119   );
120   $form['search_settings']['pz2_path'] = array(
121     '#type' => 'textfield',
122     '#title' => t('Pazpar2/Service Proxy path'),
123     '#description' => t('Path that takes Pazpar2 commands via HTTP'),
124     '#required' => TRUE,
125     '#default_value' => isset($node->mkdru->pz2_path) ? $node->mkdru->pz2_path : '/pazpar2/search.pz2',
126   );
127   $form['search_settings']['use_sessions'] = array(
128     '#type' => 'checkbox',
129     '#title' => t('Session handling'),
130     '#description' => t('Disable for use with Service Proxy'),
131     '#default_value' => isset($node->mkdru->use_sessions) ? $node->mkdru->use_sessions : 1,
132   );
133
134   $form['sp_settings']  = array(
135     '#type' => 'fieldset',
136     '#title' => t('Service Proxy specific settings'),
137     '#collapsible' => TRUE,
138     '#collapsed' => FALSE
139   );
140   $form['sp_settings']['sp_user'] = array(
141     '#type' => 'textfield',
142     '#title' => t('Service Proxy username'),
143     '#description' => t('Service Proxy username'),
144     '#required' => FALSE,
145     '#default_value' => isset($node->mkdru->sp_user) ? 
146       $node->mkdru->sp_user : '',
147   );
148   $form['sp_settings']['sp_pass'] = array(
149     '#type' => 'textfield',
150     '#title' => t('Service Proxy password'),
151     '#description' => t('Service Proxy password'),
152     '#required' => FALSE,
153     '#default_value' => isset($node->mkdru->sp_pass) ? 
154       $node->mkdru->sp_pass : '',
155   );
156
157   $form['display_settings']  = array(
158     '#type' => 'fieldset',
159     '#title' => t('Display settings'),
160     '#collapsible' => TRUE,
161     '#collapsed' => FALSE
162   );
163   $form['display_settings']['source_max'] = array(
164     '#type' => 'textfield',
165     '#title' => t('Number of sources to display'),
166     '#required' => TRUE,
167     '#default_value' => isset($node->mkdru->source_max) ? $node->mkdru->source_max : 10,
168     '#size' => 3,
169     '#maxlength' => 3,
170   );
171   $form['display_settings']['author_max'] = array(
172     '#type' => 'textfield',
173     '#title' => t('Number of authors to display'),
174     '#required' => TRUE,
175     '#default_value' => isset($node->mkdru->author_max) ? $node->mkdru->author_max : 10,
176     '#size' => 3,
177     '#maxlength' => 3,
178   );
179   $form['display_settings']['subject_max'] = array(
180     '#type' => 'textfield',
181     '#title' => t('Number of subjects to display'),
182     '#required' => TRUE,
183     '#default_value' => isset($node->mkdru->subject_max) ? $node->mkdru->subject_max : 10,
184     '#size' => 3,
185     '#maxlength' => 3,
186   );
187   return $form;
188 }
189
190
191 /**
192 * Implements hook_validate()
193 */
194 function mkdru_validate($node) {
195   if (!is_numeric($node->source_max)) {
196     form_set_error('source_max', t('Please enter a number.'));
197   }
198   if (!is_numeric($node->author_max)) {
199     form_set_error('author_max', t('Please enter a number.'));
200   }
201   if (!is_numeric($node->subject_max)) {
202     form_set_error('subject_max', t('Please enter a number.'));
203   }
204 }
205
206 /**
207 * Implements hook_insert().
208 */
209 function mkdru_insert($node) {
210   drupal_write_record('mkdru', $node);
211 }
212
213 /**
214 * Implements hook_update().
215 */
216 function mkdru_update($node) {
217   if ($node->revision) {
218     // New revision; treat it as a new record.
219     mkdru_insert($node);
220   }
221   else {
222     drupal_write_record('mkdru', $node, 'vid');
223   }
224 }
225
226 /**
227  * Implements hook_nodeapi().
228  *
229  * When a node revision is deleted, we need to remove the corresponding record
230  * from our table. The only way to handle revision deletion is by implementing
231  * hook_nodeapi().
232  */
233 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
234   switch ($op) {
235     case 'delete revision':
236       db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
237       break;
238   }
239 }
240
241 /**
242  * Implements hook_delete().
243  */
244 function mkdru_delete($node) {
245   // Deleting by nid covers all revisions.
246   db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
247 }
248
249
250
251 // Node rendering
252 /**
253 * Implements hook_load()
254 */
255 function mkdru_load($node) {
256   return array('mkdru' => db_fetch_object(db_query(
257     'SELECT * FROM {mkdru} WHERE vid = %d', $node->vid)));
258 }
259
260 /**
261 * Implements hook_theme().
262 */
263 function mkdru_theme() {
264   return array(
265     'mkdru_form' => array(
266       'template' => 'mkdru-form',
267       'arguments' => array(),
268     ),
269     'mkdru_results' => array(
270       'template' => 'mkdru-results',
271       'arguments' => array(),
272     ),
273     'mkdru_js' => array(
274       'arguments' => array('node' => NULL),
275     ),
276     'mkdru_block_search' => array(
277       'template' => 'mkdru-block-search',
278       'arguments' => array('nid' => NULL, 'path' => NULL),
279     ),
280     'mkdru_block_facet' => array(
281       'template' => 'mkdru-block-facet',
282       'arguments' => array('class' => NULL)
283     )
284   );
285 }
286
287 /**
288 * Theme function to include Javascript search client and deps
289 */
290 function theme_mkdru_js($node) {
291   $path = drupal_get_path('module', 'mkdru');
292   // Pazpar2 client library.
293   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer', TRUE, TRUE, FALSE);
294   // jQuery plugin for query string/history manipulation.
295   drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer', TRUE, TRUE, FALSE);
296   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE);
297   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE);
298   drupal_add_js(array('mkdru' => $node->mkdru), 'setting');
299 }
300
301 /** 
302 * Implements hook_view()
303 */
304 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
305   $node->content['mkdru_js'] = array(
306     '#value' => theme('mkdru_js', $node), 
307     '#weight' => 0,
308   );
309   $node->content['mkdru_form'] = array(
310     '#value' => theme('mkdru_form'), 
311     '#weight' => 1,
312   );
313   $node->content['mkdru_results'] = array(
314     '#value' => theme('mkdru_results'), 
315     '#weight' => 2,
316   );
317   return $node;
318 }
319
320 /** 
321 * Implements hook_block()
322 */
323 function mkdru_block($op='list', $delta='sources', $edit=array()) {
324   switch ($op) {
325     case 'list':
326       // facet blocks
327       // D6 has no setting for note type visibility, set
328       // the default to limit facet display to this type
329       $visPHP = '<?php
330   if (arg(0) == "node" && is_numeric(arg(1))) {
331     $node = node_load(array("nid" => arg(1)));
332     return $node->type == "mkdru";
333   }
334 ?>';
335
336       // NB: block caching is redundant for static content
337       $blocks['mkdru_sources']['info'] = t('mkdru - source facets');
338       $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
339       $blocks['mkdru_sources']['visibility'] = 2;
340       $blocks['mkdru_sources']['pages'] = $visPHP;
341       $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets');
342       $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE;
343       $blocks['mkdru_subjects']['visibility'] = 2;
344       $blocks['mkdru_subjects']['pages'] = $visPHP;
345       $blocks['mkdru_authors']['info'] = t('mkdru - author facets');
346       $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE;
347       $blocks['mkdru_authors']['visibility'] = 2;
348       $blocks['mkdru_authors']['pages'] = $visPHP;
349       // search blocks
350       $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';");
351       while ($node = db_fetch_object($result)) {
352         $blocks['mkdru_search_' . $node->nid]['info'] = 
353            t('mkdru - search box for "' . $node->title . '"');
354         $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
355       };
356       return $blocks;
357
358     case 'view':
359       switch ($delta) {
360         case 'mkdru_sources':
361           $block['subject'] = t('Source');
362           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-source');
363           return $block;
364         case 'mkdru_subjects':
365           $block['subject'] = t('Subject');
366           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-subject');
367           return $block;
368         case 'mkdru_authors':
369           $block['subject'] = t('Author');
370           $block['content'] = theme('mkdru_block_facet', 'mkdru-facet-author');
371           return $block;
372     }
373     if (substr($delta, 0, 13) == 'mkdru_search_') {
374       $nid = substr($delta, 13);
375       $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
376       return $block;
377     }
378   }
379 }