Add jquery-bbq js in the ting hook
[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']['use_sessions'] = array(
121     '#type' => 'checkbox',
122     '#title' => t('Session handling'),
123     '#description' => t('Disable for use with Service Proxy'),
124     '#default_value' => isset($node->mkdru->use_sessions) ? $node->mkdru->use_sessions : 1,
125   );
126
127   $form['display_settings']  = array(
128     '#type' => 'fieldset',
129     '#title' => t('Display settings'),
130     '#collapsible' => TRUE,
131     '#collapsed' => FALSE
132   );
133   $form['display_settings']['source_max'] = array(
134     '#type' => 'textfield',
135     '#title' => t('Number of sources to display'),
136     '#required' => TRUE,
137     '#default_value' => isset($node->mkdru->source_max) ? $node->mkdru->source_max : 10,
138     '#size' => 3,
139     '#maxlength' => 3,
140   );
141   $form['display_settings']['author_max'] = array(
142     '#type' => 'textfield',
143     '#title' => t('Number of authors to display'),
144     '#required' => TRUE,
145     '#default_value' => isset($node->mkdru->author_max) ? $node->mkdru->author_max : 10,
146     '#size' => 3,
147     '#maxlength' => 3,
148   );
149   $form['display_settings']['subject_max'] = array(
150     '#type' => 'textfield',
151     '#title' => t('Number of subjects to display'),
152     '#required' => TRUE,
153     '#default_value' => isset($node->mkdru->subject_max) ? $node->mkdru->subject_max : 10,
154     '#size' => 3,
155     '#maxlength' => 3,
156   );
157   return $form;
158 }
159
160
161 /**
162 * Implementation of hook_validate()
163 */
164 function mkdru_validate($node) {
165   if (!is_numeric($node->source_max)) {
166     form_set_error('source_max', t('Please enter a number.'));
167   }
168   if (!is_numeric($node->author_max)) {
169     form_set_error('author_max', t('Please enter a number.'));
170   }
171   if (!is_numeric($node->subject_max)) {
172     form_set_error('subject_max', t('Please enter a number.'));
173   }
174 }
175
176 /**
177 * Implementation of hook_insert().
178 */
179 function mkdru_insert($node) {
180   db_query("INSERT INTO {mkdru} (nid, vid, pz2_path, use_sessions, source_max, author_max, subject_max) VALUES (%d, %d, '%s', %d, %d, %d, %d)",
181     $node->nid, $node->vid, $node->pz2_path, $node->use_sessions, $node->source_max, $node->author_max, $node->subject_max);
182 }
183
184 /**
185 * Implementation of hook_update().
186 */
187 function mkdru_update($node) {
188   if ($node->revision) {
189     // New revision; treat it as a new record.
190     mkdru_insert($node);
191   }
192   else {
193     db_query("UPDATE {mkdru} SET pz2_path = '%s', use_sessions = %d, source_max = %d, author_max = %d, subject_max = %d WHERE vid = %d", $node->pz2_path, $node->use_sessions, $node->source_max, $node->author_max, $node->subject_max, $node->vid);
194   }
195 }
196
197 /**
198  * Implementation of hook_nodeapi().
199  *
200  * When a node revision is deleted, we need to remove the corresponding record
201  * from our table. The only way to handle revision deletion is by implementing
202  * hook_nodeapi().
203  */
204 function mkdru_nodeapi(&$node, $op, $teaser, $page) {
205   switch ($op) {
206     case 'delete revision':
207       db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
208       break;
209   }
210 }
211
212 /**
213  * Implementation of hook_delete().
214  */
215 function mkdru_delete($node) {
216   // Deleting by nid covers all revisions.
217   db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
218 }
219
220
221
222 // Node rendering
223 /**
224 * Implementation of hook_load()
225 */
226 function mkdru_load($node) {
227   return array('mkdru' => db_fetch_object(db_query(
228     'SELECT * FROM {mkdru} WHERE vid = %d', $node->vid)));
229 }
230
231 /**
232 * Implementation of hook_theme().
233 */
234 function mkdru_theme() {
235   return array(
236     'mkdru_form' => array(
237       'template' => 'mkdru-form',
238       'arguments' => array(),
239     ),
240     'mkdru_results' => array(
241       'template' => 'mkdru-results',
242       'arguments' => array(),
243     ),
244     'mkdru_js' => array(
245       'arguments' => array('node' => NULL),
246     ),
247     'mkdru_block_search' => array(
248       'template' => 'mkdru-block-search',
249       'arguments' => array('nid' => null, 'path' => NULL),
250     ),
251 //     'mkdru_block_facet' => array(
252 //       'template' => 'mkdru-block-facet',
253 //       'arguments' => array('divId' => NULL),
254 //     ),
255   );
256 }
257
258 /**
259 * Theme function to include Javascript search client and deps
260 */
261 function theme_mkdru_js($node) {
262   $path = drupal_get_path('module', 'mkdru');
263   // Pazpar2 client library.
264   drupal_add_js(variable_get('pz2_js_path', 'pazpar2/js') . '/pz2.js', 'module', 'footer', TRUE, TRUE, FALSE);
265   // jQuery plugin for query string/history manipulation.
266   drupal_add_js($path . '/jquery.ba-bbq.js', 'module', 'footer', TRUE, TRUE, FALSE);
267   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer', TRUE, TRUE, FALSE);
268   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer', TRUE, TRUE, FALSE);
269   drupal_add_js(array('mkdru' => $node->mkdru), 'setting');
270 }
271
272 /** 
273 * Implementation of hook_view()
274 */
275 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
276   $node->content['mkdru_js'] = array(
277     '#value' => theme('mkdru_js', $node), 
278     '#weight' => 0,
279   );
280   $node->content['mkdru_form'] = array(
281     '#value' => theme('mkdru_form'), 
282     '#weight' => 1,
283   );
284   $node->content['mkdru_results'] = array(
285     '#value' => theme('mkdru_results'), 
286     '#weight' => 2,
287   );
288   return $node;
289 }
290
291 /** 
292 * Implementation of hook_block()
293 */
294 function mkdru_block($op='list', $delta='sources', $edit=array()) {
295   switch ($op) {
296     case 'list':
297       // facet blocks
298       // NB: block caching is redundant for static content
299       $blocks['mkdru_sources']['info'] = t('mkdru - source facets');
300       $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
301       $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets');
302       $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE;
303       $blocks['mkdru_authors']['info'] = t('mkdru - author facets');
304       $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE;
305       // search blocks
306       $result = db_query("SELECT title, nid FROM {node} WHERE type = 'mkdru';");
307       while ($node = db_fetch_object($result)) {
308         $blocks['mkdru_search_' . $node->nid]['info'] = 
309            t('mkdru - search box for "' . $node->title . '"');
310         $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
311       };
312       return $blocks;
313
314     case 'view':
315       switch ($delta) {
316         // TODO: make the facet themable, I have no clue why this won't work
317 //         case 'mkdru_sources':
318 //           $block['subject'] = t('Source');
319 //           $block['content'] = theme('mkdru_block_facet', 'mkdru-sources');
320 //           return $block;
321 //         case 'mkdru_subjects':
322 //           $block['subject'] = t('Subject');
323 //           $block['content'] = theme('mkdru_block_facet', 'mkdru-subjects');
324 //           return $block;
325 //         case 'mkdru_authors':
326 //           $block['subject'] = t('Author');
327 //           $block['content'] = theme('mkdru_block_facet', 'mkdru-authors');
328 //           return $block;
329         case 'mkdru_sources':
330           $block['subject'] = t('Source');
331           $block['content'] = '<div class="mkdru-facet mkdru-facet-sources"> </div>';
332           return $block;
333         case 'mkdru_subjects':
334           $block['subject'] = t('Subject');
335           $block['content'] = '<div class="mkdru-facet mkdru-facet-subjects"> </div>';
336           return $block;
337         case 'mkdru_authors':
338           $block['subject'] = t('Author');
339           $block['content'] = '<div class="mkdru-facet mkdru-facet-authors"> </div>';
340           return $block;
341     }
342     if (substr($delta, 0, 13) == 'mkdru_search_') {
343       $nid = substr($delta, 13);
344       $block['content'] = theme('mkdru_block_search', $nid, '/node/' . $nid);
345       return $block;
346     }
347   }
348 }