2 // $Id: taxonomy_block.module 144 2007-03-28 07:52:20Z thierry $
5 * Implementation of hook_help.
7 function taxonomy_block_help($section) {
9 case 'admin/modules#description' :
10 return t('Makes blocks based on taxonomy vocabularies and terms.');
16 * Implementation of hook_perm.
18 function taxonomy_block_perm() {
19 return array('administer taxonomy blocks');
23 * Implementation of hook_menu.
25 function taxonomy_block_menu($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);
37 * Implementation of hook_block.
39 function taxonomy_block_block($op = 'list', $delta = 0) {
42 return taxonomy_block_get_blocks();
45 return taxonomy_block_get_block($delta);
51 * Displays the administrator page to this module.
53 function taxonomy_block_admin($op = NULL, $bid = NULL) {
60 $output = confirm_form('taxonomy_block_delete_form', $form, t('Are you sure you want to delete the taxonomy block?'), 'admin/block/taxonomy_block');
63 $output = taxonomy_block_blocks();
64 $output .= taxonomy_block_form();
68 drupal_set_title(t('Taxonomy Block Administration'));
73 * Inserts a block into the database.
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'));
82 * Updates a block in the database.
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'));
91 * Deletes a block from the database.
93 function taxonomy_block_delete($bid) {
94 db_query('DELETE FROM {taxonomy_block} WHERE bid = %d', $bid);
98 * Displays the block creation form.
100 function taxonomy_block_form($bid = NULL) {
102 $block = db_fetch_object(db_query('SELECT * FROM {taxonomy_block} WHERE bid = %d', $bid));
105 $form['create_taxonomy_block'] = array(
106 '#type' => 'fieldset',
107 '#title' => t('Create Block'),
109 $form['create_taxonomy_block']['bid'] = array(
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.'),
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.'),
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.'),
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.'),
140 $form['create_taxonomy_block']['tid'] = _taxonomy_block_get_taxonomy_dropdown($block->type == 'vocabulary' ? 'v'. $block->tid : $block->tid);
143 $form['create_taxonomy_block'][] = array(
145 '#value' => t('Edit Block'),
147 $form['create_taxonomy_block'][] = array(
149 '#value' => t('Cancel'),
153 $form['create_taxonomy_block'][] = array(
155 '#value' => t('Create Block'),
159 return drupal_get_form('taxonomy_block_form', $form);
163 * taxonomy_block_form form validate callback function.
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.'));
169 if($edit['length'] <=0) {
170 form_set_error('length', t('Please provide a node count greater than 0.'));
173 form_set_error('tid', t('Please select a category for your block to display.'));
177 * taxonomy_block_form form execute callback function.
179 function taxonomy_block_form_submit($form_id, $edit) {
182 if(substr($edit['tid'], 0, 1)=='v') {
183 $edit['type'] = 'vocabulary';
184 $edit['tid'] = substr($edit['tid'], 1);
187 $edit['type'] = 'term';
190 if($op == t('Create Block')) {
191 taxonomy_block_insert($edit);
193 elseif($op == t('Edit Block')) {
194 taxonomy_block_update($edit);
196 drupal_goto('admin/block/taxonomy_block');
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');
206 * Displays a list of the blocks.
208 function taxonomy_block_blocks() {
209 $result = db_query('SELECT * FROM {taxonomy_block}');
210 while ($block = db_fetch_object($result)) {
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);
217 $form['show_taxonomy_block'][] = array(
218 '#type' => 'fieldset',
219 '#title' => t('Current Blocks'),
220 '#value' => theme('item_list', $blocks),
223 return drupal_get_form('taxonomy_block_admin', $form);
227 * Returns the requested block by $bid.
229 function taxonomy_block_get_block($bid) {
231 $result = db_fetch_object(db_query('SELECT * FROM {taxonomy_block} WHERE bid = %d', $bid));
233 if ($result->type == 'term') {
234 $tids = taxonomy_get_children($result->tid);
235 $tids[$result->tid] = $result->tid;
238 $tids = taxonomy_get_children(0, $result->tid);
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);
243 $block['subject'] = $result->name;
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 ? '...' : ''));
249 $items .= theme('taxonomy_block_list_item', $node, $teaser);
251 $content = theme('taxonomy_block_list', $items);
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;
264 * Returns an array of block descriptions for the block config page.
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;
275 * Returns a dropdown event taxonomy term input control.
277 function _taxonomy_block_get_taxonomy_dropdown($tid = NULL) {
278 $vocabs = taxonomy_get_vocabularies();
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;
291 '#title' => t('Category'),
292 '#default_value' => $tid,
293 '#options' => $links,
294 '#description' => t('Select taxonomy type to display'),
300 * Format a single item for a list.
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") ));
307 $output .= '<br/>' . $teaser;
315 * Format the item list.
319 function theme_taxonomy_block_list($items) {
320 $output = '<div class="item-list"><ul>' . $items . '</ul></div>';