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