Towards per-node config. If only I can coax it into making the table...
[mkdru-moved-to-drupal.org.git] / mkdru.module
1 <?php
2 // Module metainfo
3 /**
4 * Implementation of hook_node_info().
5 */
6 function mkdru_node_info() {
7   return array(
8     'mkdru' => array(
9       'name' => t("Z39.50/SRU metasearch interface"),
10       'module' => 'mkdru',
11       'description' => t("Metasearch interface for Z39.50/SRU and other targets via a Pazpar2/Service Proxy backend"),
12
13     )
14   );
15 }
16
17 /**
18 * Implementation of hook_perm().
19 */
20 function mkdru_perm() {
21   return array('create metasearch interface', 'edit any metasearch interface', 'edit own metasearch interface');
22 }
23
24 /**
25 * Implementation of hook_access().
26 */
27 function mkdru_access($op, $node, $account) {
28
29   if ($op == 'create') {
30     // Only users with permission to do so may create this node type.
31     return user_access('create metasearch interface', $account);
32   }
33
34   // Users who create a node may edit or delete it later, assuming they have the
35   // necessary permissions.
36   if ($op == 'update' || $op == 'delete') {
37     if (user_access('edit own metasearch interface', $account) && ($account->uid == $node->uid)) {
38       return TRUE;
39     }
40     elseif (user_access('edit any metasearch interface', $account)) {
41       return TRUE;
42     }
43   }
44 }
45
46
47
48 // Node config
49 /**
50 * Implementation of hook_form().
51 */
52 function mkdru_form(&$node, $form_state) {
53   $type = node_get_types('type', $node);
54
55   $form['title'] = array(
56     '#type' => 'textfield',
57     '#title' => check_plain($type->title_label),
58     '#required' => TRUE,
59     '#default_value' => $node->title,
60     '#weight' => -5
61   );
62   $form['pz2_path'] = array(
63     '#type' => 'textfield',
64     '#title' => t('Pazpar2/Service Proxy path'),
65     '#description' => t('Absolute URL path without leading slash'),
66     '#required' => TRUE,
67     '#default_value' => isset($node->pz2_path) ? $node->pz2_path : 'pazpar2',
68     '#weight' => 0
69   );
70   return $form;
71 }
72
73 /**
74 * Implementation of hook_insert().
75 */
76 function mkdru_insert($node) {
77   db_query("INSERT INTO {mkdru} (nid, vid, pz2_path) VALUES (%d, %d, '%s')",
78     $node->nid, $node->vid, $node->pz2_path);
79 }
80
81 /**
82 * Implementation of hook_update().
83 */
84 function mkdru_update($node) {
85   if ($node->revision) {
86     // New revision; treat it as a new record.
87     mkdru_insert($node);
88   }
89   else {
90     db_query("UPDATE {mkdru} SET pz2_path = '%s' WHERE vid = '%d'", $node->pz2_path, $node->vid);
91   }
92 }
93
94 /**
95  * Implementation of hook_nodeapi().
96  *
97  * When a node revision is deleted, we need to remove the corresponding record
98  * from our table. The only way to handle revision deletion is by implementing
99  * hook_nodeapi().
100  */
101 function node_example_nodeapi(&$node, $op, $teaser, $page) {
102   switch ($op) {
103     case 'delete revision':
104       // Notice that we're matching a single revision based on the node's vid.
105       db_query('DELETE FROM {mkdru} WHERE vid = %d', $node->vid);
106       break;
107   }
108 }
109
110 /**
111  * Implementation of hook_delete().
112  *
113  * When a node is deleted, we need to remove all related records from our table.
114  */
115 function node_example_delete($node) {
116   // Deleting by nid covers all revisions.
117   db_query('DELETE FROM {mkdru} WHERE nid = %d', $node->nid);
118 }
119
120
121
122 // Node rendering
123 /**
124 * Implementation of hook_load()
125 */
126 function mkdru_load($node) {
127   return db_fetch_object(db_query('SELECT pz2_path FROM {mkdru} WHERE vid = %d', $node->vid));
128 }
129
130 /**
131 * Implementation of hook_theme().
132 */
133 function mkdru_theme() {
134   return array(
135     'mkdru_page' => array(
136       'template' => 'mkdru-page',
137       'arguments' => array(),
138     ),
139     'mkdru_page_js' => array(
140       'arguments' => array(),
141     ),
142 //     'mkdru_block_facet' => array(
143 //       'template' => 'mkdru-block-facet',
144 //       'arguments' => array('divId' => NULL),
145 //     ),
146   );
147 }
148
149 /**
150 * Theme function to include Javascript search client and deps
151 */
152 function theme_mkdru_page_js() {
153   $path = drupal_get_path('module', 'mkdru');
154   drupal_add_js('pazpar2/js/pz2.js', 'module', 'footer');
155   drupal_add_js($path . '/mkdru.theme.js', 'module', 'footer');
156   drupal_add_js($path . '/mkdru.client.js', 'module', 'footer');
157 }
158
159 /** 
160 * Implementation of hook_view()
161 */
162 function mkdru_view($node, $teaser = FALSE, $page = FALSE) {
163   $node->content['mkdru_page_js'] = array(
164     '#value' => theme('mkdru_page_js'), 
165     '#weight' => 0,
166   );
167   $node->content['mkdru_page'] = array(
168     '#value' => theme('mkdru_page'), 
169     '#weight' => 1,
170   );
171   return $node;
172 }
173
174 /** 
175 * Implementation of hook_block()
176 */
177 function mkdru_block($op='list', $delta='sources', $edit=array()) {
178   switch ($op) {
179     case 'list':
180       $blocks['mkdru_sources']['info'] = t('mkdru - source facets');
181       $blocks['mkdru_sources']['cache'] = BLOCK_NO_CACHE;
182       $blocks['mkdru_subjects']['info'] = t('mkdru - subject facets');
183       $blocks['mkdru_subjects']['cache'] = BLOCK_NO_CACHE;
184       $blocks['mkdru_authors']['info'] = t('mkdru - author facets');
185       $blocks['mkdru_authors']['cache'] = BLOCK_NO_CACHE;
186       return $blocks;
187
188     case 'view':
189       switch ($delta) {
190         // TODO: make the facet themable, I have no clue why this won't work
191 //         case 'mkdru_sources':
192 //           $block['subject'] = t('Source');
193 //           $block['content'] = theme('mkdru_block_facet', 'mkdru-sources');
194 //           return $block;
195 //         case 'mkdru_subjects':
196 //           $block['subject'] = t('Subject');
197 //           $block['content'] = theme('mkdru_block_facet', 'mkdru-subjects');
198 //           return $block;
199 //         case 'mkdru_authors':
200 //           $block['subject'] = t('Author');
201 //           $block['content'] = theme('mkdru_block_facet', 'mkdru-authors');
202 //           return $block;
203         case 'mkdru_sources':
204           $block['subject'] = t('Source');
205           $block['content'] = '<div id="mkdru-sources"> </div>';
206           return $block;
207         case 'mkdru_subjects':
208           $block['subject'] = t('Subject');
209           $block['content'] = '<div id="mkdru-subjects"> </div>';
210           return $block;
211         case 'mkdru_authors':
212           $block['subject'] = t('Author');
213           $block['content'] = '<div id="mkdru-authors"> </div>';
214           return $block;
215     }
216   }
217 }