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