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