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