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