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