'admin/block/taxonomy_block', 'title' => t('taxonomy block'), 'callback' => 'taxonomy_block_admin', 'access' => user_access('administer taxonomy blocks')); $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); } return $items; } /** * Implementation of hook_block. */ function taxonomy_block_block($op = 'list', $delta = 0) { switch ($op) { case 'list': return taxonomy_block_get_blocks(); break; case 'view': return taxonomy_block_get_block($delta); break; } } /** * Displays the administrator page to this module. */ function taxonomy_block_admin($op = NULL, $bid = NULL) { switch ($op) { case 'delete' : $form['bid'] = array( '#type' => 'hidden', '#value' => $bid, ); $output = confirm_form('taxonomy_block_delete_form', $form, t('Are you sure you want to delete the taxonomy block?'), 'admin/block/taxonomy_block'); break; default: $output = taxonomy_block_blocks(); $output .= taxonomy_block_form(); break; } drupal_set_title(t('Taxonomy Block Administration')); return $output; } /** * Inserts a block into the database. */ function taxonomy_block_insert($edit) { $sql = 'INSERT INTO {taxonomy_block} (tid, name, description, length, type, teaser) VALUES (%d, \'%s\', \'%s\', %d, \'%s\', %d)'; db_query($sql, $edit['tid'], $edit['name'], $edit['description'], $edit['length'], $edit['type'], $edit['teaser']); drupal_set_message(t('Created taxonomy block')); } /** * Updates a block in the database. */ function taxonomy_block_update($edit) { $sql = 'UPDATE {taxonomy_block} SET tid = %d, name = \'%s\', description = \'%s\', length = %d, type = \'%s\', teaser = %d WHERE bid = %d'; db_query($sql, $edit['tid'], $edit['name'], $edit['description'], $edit['length'], $edit['type'], $edit['teaser'], $edit['bid']); drupal_set_message(t('Updated taxonomy block')); } /** * Deletes a block from the database. */ function taxonomy_block_delete($bid) { db_query('DELETE FROM {taxonomy_block} WHERE bid = %d', $bid); } /** * Displays the block creation form. */ function taxonomy_block_form($bid = NULL) { if ($bid) { $block = db_fetch_object(db_query('SELECT * FROM {taxonomy_block} WHERE bid = %d', $bid)); } $form['create_taxonomy_block'] = array( '#type' => 'fieldset', '#title' => t('Create Block'), ); $form['create_taxonomy_block']['bid'] = array( '#type' => 'hidden', '#value' => $bid, ); $form['create_taxonomy_block']['name'] = array( '#type' => 'textfield', '#title' => t('Block Name'), '#default_value' => $block->name, '#description' => t('This is the name of your block.'), ); $form['create_taxonomy_block']['description'] = array( '#type' => 'textfield', '#title' => t('Block Description'), '#default_value' => $block->description, '#description' => t('This is the description of your block. It is not displayed to users.'), '#required' => TRUE, ); $form['create_taxonomy_block']['teaser'] = array( '#type' => 'textfield', '#title' => t('Teaser Length'), '#default_value' => $block->teaser, '#description' => t('This is the length of node body content to display under each title in characters. Leave blank for none.'), ); $form['create_taxonomy_block']['length'] = array( '#type' => 'textfield', '#title' => t('Node Count'), '#default_value' => $block->length, '#description' => t('This is the number of nodes to display.'), '#required' => TRUE, ); $form['create_taxonomy_block']['tid'] = _taxonomy_block_get_taxonomy_dropdown($block->type == 'vocabulary' ? 'v'. $block->tid : $block->tid); if($bid) { $form['create_taxonomy_block'][] = array( '#type' => 'submit', '#value' => t('Edit Block'), ); $form['create_taxonomy_block'][] = array( '#type' => 'submit', '#value' => t('Cancel'), ); } else { $form['create_taxonomy_block'][] = array( '#type' => 'submit', '#value' => t('Create Block'), ); } return drupal_get_form('taxonomy_block_form', $form); } /** * taxonomy_block_form form validate callback function. */ function taxonomy_block_form_validate($form_id, $edit) { if(!$edit['description']) { form_set_error('description', t('Please provide a description for your block. It will be used in administration screens only.')); } if($edit['length'] <=0) { form_set_error('length', t('Please provide a node count greater than 0.')); } if(!$edit['tid']) { form_set_error('tid', t('Please select a category for your block to display.')); } } /** * taxonomy_block_form form execute callback function. */ function taxonomy_block_form_submit($form_id, $edit) { $op = $_POST['op']; if(substr($edit['tid'], 0, 1)=='v') { $edit['type'] = 'vocabulary'; $edit['tid'] = substr($edit['tid'], 1); } else { $edit['type'] = 'term'; } if($op == t('Create Block')) { taxonomy_block_insert($edit); } elseif($op == t('Edit Block')) { taxonomy_block_update($edit); } drupal_goto('admin/block/taxonomy_block'); } function taxonomy_block_delete_form_submit($form_id, $edit) { taxonomy_block_delete($edit['bid']); drupal_set_message(t('The block has been deleted.')); drupal_goto('admin/block/taxonomy_block'); } /** * Displays a list of the blocks. */ function taxonomy_block_blocks() { $result = db_query('SELECT * FROM {taxonomy_block}'); while ($block = db_fetch_object($result)) { $links = array(); $links[] = l(t('edit'), 'admin/block/taxonomy_block/edit/'. $block->bid, array('title'=>t('edit block')));; $links[] = l(t('delete'), 'admin/block/taxonomy_block/delete/'. $block->bid, array('title'=>t('delete block')));; $blocks[] = $block->description .' - '. theme('links', $links); } $form['show_taxonomy_block'][] = array( '#type' => 'fieldset', '#title' => t('Current Blocks'), '#value' => theme('item_list', $blocks), ); return drupal_get_form('taxonomy_block_admin', $form); } /** * Returns the requested block by $bid. */ function taxonomy_block_get_block($bid) { $tids = array(); $result = db_fetch_object(db_query('SELECT * FROM {taxonomy_block} WHERE bid = %d', $bid)); if ($result) { if ($result->type == 'term') { $tids = taxonomy_get_children($result->tid); $tids[$result->tid] = $result->tid; } else { $tids = taxonomy_get_children(0, $result->tid); } $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); $block['subject'] = $result->name; while ($node = db_fetch_object($nodes)) { if($result->teaser) { $teaser = strip_tags(substr($node->body, 0, $result->teaser) . (strlen($node->body) > $result->teaser ? '...' : '')); } $items .= theme('taxonomy_block_list_item', $node, $teaser); } $content = theme('taxonomy_block_list', $items); $content .= ''; $block['content'] = $content; return $block; } else { return null; } } /** * Returns an array of block descriptions for the block config page. */ function taxonomy_block_get_blocks() { $results = db_query('SELECT * FROM {taxonomy_block}'); while ($block = db_fetch_object($results)) { $blocks[$block->bid]['info'] = $block->description; } return $blocks; } /** * Returns a dropdown event taxonomy term input control. */ function _taxonomy_block_get_taxonomy_dropdown($tid = NULL) { $vocabs = taxonomy_get_vocabularies(); $links[] = ''; foreach ($vocabs as $vocab) { $links['v'.$vocab->vid] = $vocab->name; $tree = taxonomy_get_tree($vocab->vid); foreach ($tree as $term) { $links[$term->tid] = $vocab->name .' - '. $term->name; } } return array( '#type' => 'select', '#title' => t('Category'), '#default_value' => $tid, '#options' => $links, '#description' => t('Select taxonomy type to display'), '#required' => TRUE, ); } /** * Format a single item for a list. * * @ingroup themeable */ function theme_taxonomy_block_list_item($node, $teaser) { $output = '
  • ' . l($node->title, 'node/'. $node->nid, array('title'=>t("view {$node->title} in full") )); if ($teaser) { $output .= '
    ' . $teaser; } $output .= '
  • '; return $output; } /** * Format the item list. * * @ingroup themeable */ function theme_taxonomy_block_list($items) { $output = '
    '; return $output; } ?>