5dcef1a45e47c003973ace17b97683dc0969c558
[plewww.git] / modules / taxonomy_block / taxonomy_block.module
1 <?php
2 // $Id: taxonomy_block.module 144 2007-03-28 07:52:20Z thierry $
3
4 /**
5  * Implementation of hook_help.
6  */
7 function taxonomy_block_help($section) {
8   switch ($section) {
9     case 'admin/modules#description' :
10         return t('Makes blocks based on taxonomy vocabularies and terms.');
11       break;
12   }
13 }
14
15 /**
16  * Implementation of hook_perm.
17  */
18 function taxonomy_block_perm() {
19   return array('administer taxonomy blocks');
20 }
21
22 /**
23  * Implementation of hook_menu.
24  */
25 function taxonomy_block_menu($may_cache) {
26   $items = array();
27
28   if ($may_cache) {
29     $items[] = array('path' => 'admin/block/taxonomy_block', 'title' => t('taxonomy block'), 'callback' => 'taxonomy_block_admin', 'access' => user_access('administer taxonomy blocks'));
30     $items[] = array('path' => 'admin/block/taxonomy_block/edit', 'title' => t('edit taxonomy block'), 'callback' => 'taxonomy_block_form', 'access' => user_access('administer taxonomy blocks'), 'type' => MENU_CALLBACK);
31   }
32
33   return $items;
34 }
35
36 /**
37  * Implementation of hook_block.
38  */
39 function taxonomy_block_block($op = 'list', $delta = 0) {
40   switch ($op) {
41     case 'list':
42       return taxonomy_block_get_blocks();
43       break;
44     case 'view':
45       return taxonomy_block_get_block($delta);
46       break;
47   }
48 }
49
50 /**
51  * Displays the administrator page to this module.
52  */
53 function taxonomy_block_admin($op = NULL, $bid = NULL) {
54   switch ($op) {
55     case 'delete' :
56       $form['bid'] = array(
57         '#type' => 'hidden',
58         '#value' => $bid,
59         );
60       $output = confirm_form('taxonomy_block_delete_form', $form, t('Are you sure you want to delete the taxonomy block?'), 'admin/block/taxonomy_block');
61       break;
62     default:
63       $output = taxonomy_block_blocks();
64       $output .= taxonomy_block_form();
65       break;
66   }
67
68   drupal_set_title(t('Taxonomy Block Administration'));
69   return $output;
70 }
71
72 /**
73  * Inserts a block into the database.
74  */
75 function taxonomy_block_insert($edit) {
76   $sql = 'INSERT INTO {taxonomy_block} (tid, name, description, length, type, teaser) VALUES (%d, \'%s\', \'%s\', %d, \'%s\', %d)';
77   db_query($sql, $edit['tid'], $edit['name'], $edit['description'], $edit['length'], $edit['type'], $edit['teaser']);
78   drupal_set_message(t('Created taxonomy block'));
79 }
80
81 /**
82  * Updates a block in the database.
83  */
84 function taxonomy_block_update($edit) {
85   $sql = 'UPDATE {taxonomy_block} SET tid = %d, name = \'%s\', description = \'%s\', length = %d, type = \'%s\', teaser = %d WHERE bid = %d';
86   db_query($sql, $edit['tid'], $edit['name'], $edit['description'], $edit['length'], $edit['type'], $edit['teaser'], $edit['bid']);
87   drupal_set_message(t('Updated taxonomy block'));
88 }
89
90 /**
91  * Deletes a block from the database.
92  */
93 function taxonomy_block_delete($bid) {
94   db_query('DELETE FROM {taxonomy_block} WHERE bid = %d', $bid);
95 }
96
97 /**
98 * Displays the block creation form.
99 */
100 function taxonomy_block_form($bid = NULL) {
101   if ($bid) {
102     $block = db_fetch_object(db_query('SELECT * FROM {taxonomy_block} WHERE bid = %d', $bid));
103   }
104
105   $form['create_taxonomy_block'] = array(
106     '#type' => 'fieldset',
107     '#title' => t('Create Block'),
108     );
109   $form['create_taxonomy_block']['bid'] = array(
110     '#type' => 'hidden',
111     '#value' => $bid,
112   );
113   $form['create_taxonomy_block']['name'] = array(
114     '#type' => 'textfield',
115     '#title' => t('Block Name'),
116     '#default_value' => $block->name,
117     '#description' => t('This is the name of your block.'),
118     );
119   $form['create_taxonomy_block']['description'] = array(
120     '#type' => 'textfield',
121     '#title' => t('Block Description'),
122     '#default_value' => $block->description,
123     '#description' => t('This is the description of your block. It is not displayed to users.'),
124     '#required' => TRUE,
125     );
126   $form['create_taxonomy_block']['teaser'] = array(
127     '#type' => 'textfield',
128     '#title' => t('Teaser Length'),
129     '#default_value' => $block->teaser,
130     '#description' => t('This is the length of node body content to display under each title in characters. Leave blank for none.'),
131     );
132   $form['create_taxonomy_block']['length'] = array(
133     '#type' => 'textfield',
134     '#title' => t('Node Count'),
135     '#default_value' => $block->length,
136     '#description' => t('This is the number of nodes to display.'),
137     '#required' => TRUE,
138     );
139
140   $form['create_taxonomy_block']['tid'] = _taxonomy_block_get_taxonomy_dropdown($block->type == 'vocabulary' ? 'v'. $block->tid : $block->tid);
141
142   if($bid) {
143     $form['create_taxonomy_block'][] = array(
144       '#type' => 'submit',
145       '#value' => t('Edit Block'),
146       );
147     $form['create_taxonomy_block'][] = array(
148       '#type' => 'submit',
149       '#value' => t('Cancel'),
150       );
151   }
152   else {
153     $form['create_taxonomy_block'][] = array(
154       '#type' => 'submit',
155       '#value' => t('Create Block'),
156       );
157   }
158
159   return drupal_get_form('taxonomy_block_form', $form);
160 }
161
162 /**
163  * taxonomy_block_form form validate callback function.
164  */
165 function taxonomy_block_form_validate($form_id, $edit) {
166   if(!$edit['description']) {
167     form_set_error('description', t('Please provide a description for your block. It will be used in administration screens only.'));
168   }
169   if($edit['length'] <=0) {
170     form_set_error('length', t('Please provide a node count greater than 0.'));
171   }
172   if(!$edit['tid']) {
173     form_set_error('tid', t('Please select a category for your block to display.'));
174   }
175 }
176 /**
177  * taxonomy_block_form form execute callback function.
178  */
179 function taxonomy_block_form_submit($form_id, $edit) {
180   $op = $_POST['op'];
181
182   if(substr($edit['tid'], 0, 1)=='v') {
183     $edit['type'] = 'vocabulary';
184     $edit['tid'] = substr($edit['tid'], 1);
185   }
186   else {
187     $edit['type'] = 'term';
188   }
189
190   if($op == t('Create Block')) {
191     taxonomy_block_insert($edit);
192   }
193   elseif($op == t('Edit Block')) {
194     taxonomy_block_update($edit);
195   }
196   drupal_goto('admin/block/taxonomy_block');
197 }
198
199 function taxonomy_block_delete_form_submit($form_id, $edit) {
200   taxonomy_block_delete($edit['bid']);
201   drupal_set_message(t('The block has been deleted.'));
202   drupal_goto('admin/block/taxonomy_block');
203 }
204
205 /**
206 * Displays a list of the blocks.
207 */
208 function taxonomy_block_blocks() {
209   $result = db_query('SELECT * FROM {taxonomy_block}');
210   while ($block = db_fetch_object($result)) {
211     $links = array();
212     $links[] = l(t('edit'), 'admin/block/taxonomy_block/edit/'. $block->bid, array('title'=>t('edit block')));;
213     $links[] = l(t('delete'), 'admin/block/taxonomy_block/delete/'. $block->bid, array('title'=>t('delete block')));;
214     $blocks[] = $block->description .' - '. theme('links', $links);
215   }
216
217   $form['show_taxonomy_block'][] = array(
218     '#type' => 'fieldset',
219     '#title' => t('Current Blocks'),
220     '#value' => theme('item_list', $blocks),
221     );
222
223   return drupal_get_form('taxonomy_block_admin', $form);
224 }
225
226 /**
227 * Returns the requested block by $bid.
228 */
229 function taxonomy_block_get_block($bid) {
230   $tids = array();
231   $result = db_fetch_object(db_query('SELECT * FROM {taxonomy_block} WHERE bid = %d', $bid));
232   if ($result) {
233     if ($result->type == 'term') {
234       $tids = taxonomy_get_children($result->tid);
235       $tids[$result->tid] = $result->tid;
236     }
237     else {
238       $tids = taxonomy_get_children(0, $result->tid);
239     }
240
241     $nodes = db_query(db_rewrite_sql('SELECT DISTINCT(n.nid), n.title, r.body, n.sticky, n.created FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE t.tid IN (%s) AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC LIMIT %d'), implode(array_keys($tids), ','), $result->length);
242
243     $block['subject'] = $result->name;
244
245     while ($node = db_fetch_object($nodes)) {
246       if($result->teaser) {
247         $teaser = strip_tags(substr($node->body, 0, $result->teaser) . (strlen($node->body) > $result->teaser ? '...' : ''));
248       }
249       $items .= theme('taxonomy_block_list_item', $node, $teaser);
250     }
251     $content = theme('taxonomy_block_list', $items);
252
253     $content .= '<div class="more-link '. (($x % 2 == 1) ? 'even': 'odd') .'">';
254     $content .= l(t("more"), 'taxonomy/term/'. implode(array_keys($tids), '+'), array("title" => t("View all."))) .'</div>';
255     $block['content'] = $content;
256     return $block;
257   }
258   else {
259     return null;
260   }
261 }
262
263 /**
264 * Returns an array of block descriptions for the block config page.
265 */
266 function taxonomy_block_get_blocks() {
267   $results = db_query('SELECT * FROM {taxonomy_block}');
268   while ($block = db_fetch_object($results)) {
269     $blocks[$block->bid]['info'] = $block->description;
270   }
271   return $blocks;
272 }
273
274 /**
275 * Returns a dropdown event taxonomy term input control.
276 */
277 function _taxonomy_block_get_taxonomy_dropdown($tid = NULL) {
278   $vocabs = taxonomy_get_vocabularies();
279
280   $links[] = '';
281   foreach ($vocabs as $vocab) {
282     $links['v'.$vocab->vid] = $vocab->name;
283     $tree = taxonomy_get_tree($vocab->vid);
284     foreach ($tree as $term) {
285       $links[$term->tid] = $vocab->name .' - '. $term->name;
286     }
287   }
288
289   return array(
290     '#type' => 'select',
291     '#title' => t('Category'),
292     '#default_value' => $tid,
293     '#options' => $links,
294     '#description' => t('Select taxonomy type to display'),
295     '#required' => TRUE,
296     );
297 }
298
299 /**
300  * Format a single item for a list.
301  *
302  * @ingroup themeable
303  */
304 function theme_taxonomy_block_list_item($node, $teaser) {
305   $output = '<li>' . l($node->title, 'node/'. $node->nid,  array('title'=>t("view {$node->title} in full") ));
306   if ($teaser) {
307     $output .= '<br/>' . $teaser;
308   }
309   $output .= '</li>';
310
311   return $output;
312 }
313
314 /**
315  * Format the item list.
316  *
317  * @ingroup themeable
318  */
319 function theme_taxonomy_block_list($items) {
320   $output = '<div class="item-list"><ul>' . $items . '</ul></div>';
321
322   return $output;
323 }
324 ?>