'admin/settings/taxonomy_context', 'title' => t('Taxonomy Context'), 'description' => t('Set display options.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('taxonomy_context_admin_settings'), 'access' => user_access('administer site configuration'), ); $items[] = array( 'path' => 'taxonomy/vocabulary', 'title' => t('Categories'), 'callback' => 'taxonomy_context_vocabulary_page', 'access' => user_access('access content'), 'type' => MENU_SUGGESTED_ITEM, ); $items[] = array( 'path' => 'activemenu/tc', 'title' => t('activemenu tc'), 'access' => user_access('access content'), 'type' => MENU_CALLBACK, 'callback' => 'taxonomy_context_js', ); } else { static $taxonomy_context_css_inserted = FALSE; if (variable_get('taxonomy_context_use_style', 1) && !$taxonomy_context_css_inserted) { drupal_add_css(drupal_get_path('module', 'taxonomy_context') . '/taxonomy_context.css'); $taxonomy_context_css_inserted = TRUE; } } return $items; } /** * Implementation of hook_taxonomy(). */ function taxonomy_context_taxonomy($op, $type, $object = NULL) { $keys = array('term' => 'tid', 'vocabulary' => 'vid'); switch ($op) { case 'update': db_query('DELETE FROM {taxonomy_context_'. $type .'} WHERE '. $keys[$type] .' = %d', $object[$keys[$type]]); // Fall through. case 'insert': db_query('INSERT INTO {taxonomy_context_'. $type .'} ('. $keys[$type] .', format) VALUES (%d, %d)', $object[$keys[$type]], $object['format']); drupal_set_message(t('Saved format data for !type.', array('!type' => t($type)))); break; case 'delete': db_query('DELETE FROM {taxonomy_context_'. $type .'} WHERE '. $keys[$type] .' = %d', $object[$keys[$type]]); drupal_set_message(t('Deleted format data for !type.', array('!type' => t($type)))); break; } } /** * Implementation of hook_block(). */ function taxonomy_context_block($op = 'list', $delta = 0, $edit = array()) { static $context; if (!isset($context)) { $context = taxonomy_context_get_context(); } switch ($op) { case 'configure': $form['taxonomy_context_block_display_'. $delta] = array( '#type' => 'radios', '#title' => t('Display mode'), '#description' => t('Select "Contextual" if you wish to display this block only on pages (vocabulary, terms, content pages) within its vocabulary. Select "Normal" to have this block display on all pages.'), '#default_value' => variable_get('taxonomy_context_block_display_'. $delta, 1), '#options' => array(t('Contextual'), t('Normal')) ); return $form; case 'save': variable_set('taxonomy_context_block_display_'. $delta, $edit['taxonomy_context_block_display_'. $delta]); break; case 'list': $vocabularies = taxonomy_get_vocabularies(); foreach ($vocabularies as $vid => $vocabulary) { if ($vocabulary->module == 'taxonomy' || !module_hook($vocabulary->module, 'term_path')) { $blocks[$vid]['info'] = t('Context for !vocabulary', array('!vocabulary' => check_plain($vocabulary->name))); } } return $blocks; case 'view': if (variable_get('taxonomy_context_block_display_'. $delta, 1) == 0 && $delta != $context->vid) { return; } $vocabulary = taxonomy_get_vocabulary($delta); $block['subject'] = check_plain($vocabulary->name); $block['content'] = taxonomy_context_menu_tree($delta, NULL, TRUE) . "\n"; return $block; } } /** * Implementation of hook_help(). */ function taxonomy_context_help($section) { // If we're on a 2nd or subsequent page, don't show taxonomy info. if (arg(0) == 'taxonomy' && empty($_GET['from'])) { $context = taxonomy_context_get_context(); switch (arg(1)) { case 'term': if (variable_get('taxonomy_context_show_term', TRUE) && $context->tid) { $output .= taxonomy_context_show_term($context->tid); } if (variable_get('taxonomy_context_show_subterms', TRUE)) { $output .= taxonomy_context_show_subterms($context->tid, $context->vid); } drupal_set_breadcrumb(taxonomy_context_get_breadcrumb($context)); break; } return $output; } switch ($section) { case 'admin/help#taxonomy_context': $output .= t('
This module can be used to output title and description of the current taxonomy term plus child nodes of that term.
'); break; } return $output; } /** * Implementation of hook_link(). */ function taxonomy_context_link($type, $object = NULL, $teaser = FALSE) { $links = array(); $keys = array('term' => 'tid', 'vocabulary' => 'vid'); if (in_array($type, array('term', 'vocabulary')) && user_access('administer taxonomy')) { $links['administer_'. $type] = array( 'title' => t('edit'), 'href' => 'admin/content/taxonomy/edit/'. $type .'/'. $object->$keys[$type], 'attributes' => array('title' => t('Edit this !type.', array('!type' => t($type)))) ); } return $links; } /** * Add links to a term or vocabulary. */ function taxonomy_context_get_links($type, &$object, $teaser = FALSE) { $object->links = module_invoke_all('link', $type, $object, $teaser); } /** * Implementation of hook_form_alter(). */ function taxonomy_context_form_alter($form_id, &$form) { if ($form_id == 'node_type_form' && isset($form['identity']['type'])) { $options = array(t('Disabled'), t('Enabled')); $form['taxonomy_context'] = array('#type' => 'fieldset', '#title' => t('Taxonomy context'), '#weight' => 0); $form['taxonomy_context']['taxonomy_context_inline'] = array( '#type' => 'radios', '#title' => t('Display taxonomy inline'), '#default_value' => variable_get('taxonomy_context_inline_'. $form['#node_type']->type, 0), '#options' => $options, '#description' => t('Display taxonomy information in node body.'), ); $form['taxonomy_context']['taxonomy_context_breadcrumb'] = array( '#type' => 'radios', '#title' => t('Show taxonomy breadcrumb'), '#default_value' => variable_get('taxonomy_context_breadcrumb_'. $form['#node_type']->type, 0), '#options' => $options, '#description' => t('Display a breadcrumb with full taxonomy context.'), ); } if (in_array($form_id, array('taxonomy_form_term', 'taxonomy_form_vocabulary'))) { $type = substr($form_id, 14, strlen($form_id)); $keys = array('term' => 'tid', 'vocabulary' => 'vid'); if ($form[$keys[$type]]) { $object = new StdClass(); $object->$keys[$type] = $form[$keys[$type]]['#value']; taxonomy_context_load($type, $object); } $form['description'] = array('description' => $form['description']); $form['description']['format'] = filter_form($object->format); } } /** * Load format data for a vocabulary or term. */ function taxonomy_context_load($type, &$object) { $keys = array('term' => 'tid', 'vocabulary' => 'vid'); $object->format = db_result(db_query('SELECT format FROM {taxonomy_context_'. $type .'} WHERE '. $keys[$type] .' = %d', $object->$keys[$type])); // Get links. taxonomy_context_get_links($type, $object); } /** * Implementation of hook_nodeapi() */ function taxonomy_context_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { switch($op) { case 'view': if ((arg(0) == 'node') && variable_get("taxonomy_context_inline_$node->type", 0)) { $node->content['taxonomy_context_taxonomy'] = array( '#value' => '', '#weight' => 10, ); $vocabularies = taxonomy_get_vocabularies($node->type); foreach ($vocabularies as $vocabulary) { if ($vocabulary->terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vocabulary->vid)) { $node->content['taxonomy_context_taxonomy']['#value'] .= theme('taxonomy_context_vocabulary_list', $vocabulary); } } $node->taxonomy = array(); } if (variable_get("taxonomy_context_breadcrumb_$node->type", 0)) { drupal_set_breadcrumb(taxonomy_context_get_breadcrumb(taxonomy_context_get_context())); } break; } } /** * Return context of the requested page */ function taxonomy_context_get_context() { static $context; if (!isset($context)) { $context = array('tid' => NULL, 'root_tid' => NULL, 'nid' => NULL, "vid" => NULL); $mode = arg(0); switch ($mode) { case 'taxonomy': switch (arg(1)) { case 'vocabulary': $context['tid'] = 0; $context['root_tid'] = 0; $context['vid'] = ((int)arg(2)) ? arg(2) : 1; break; case 'term': $tidcurrs = preg_split('/[+ ,]/', arg(2)); $context['tid'] = $tidcurrs[0]; list($top_parent) = taxonomy_get_parents_all($context['tid']); $context['root_tid'] = $top_parent->tid; $term = taxonomy_get_term($context['tid']); $context['vid'] = $term->vid; break; } break; case 'node': if (arg(2) == null) { $context['nid'] = arg(1); $tidscurr = taxonomy_node_get_terms(arg(1)); foreach ($tidscurr as $tidcurr) { $context['tid'] = $tidcurr->tid; break; } list($top_parent) = taxonomy_get_parents_all($context['tid']); $context['root_tid'] = $top_parent->tid; $term = taxonomy_get_term($context['tid']); $context['vid'] = $term->vid; } break; default: break; } $context = (object) $context; } return $context; } /** * Return the breadcrumb of taxonomy terms ending with $tid. * Statically caches results to reduce processing in the case of identical calls. */ function taxonomy_context_get_breadcrumb($context){ static $breadcrumb; if (! isset($breadcrumb)) $breadcrumb = array(); // create unique index based on $context attributes $cache_index = md5($context->vid . $context->tid . $context->nid); if (! isset($breadcrumb[$cache_index])) { $breadcrumb[$cache_index][] = l(t('Home'), ''); $vocabulary = taxonomy_get_vocabulary($context->vid); $breadcrumb[$cache_index][] = l($vocabulary->name, 'taxonomy/vocabulary/' . $context->vid); $parents = taxonomy_get_parents_all($context->tid); if ($parents) { $parents = array_reverse($parents); foreach ($parents as $parent) { if ($context->nid || $parent->tid != $context->tid) { $breadcrumb[$cache_index][] = l($parent->name, taxonomy_term_path($parent)); } } } } return $breadcrumb[$cache_index]; } /** * Menu callback: display a vocabulary's page. */ function taxonomy_context_vocabulary_page() { $vid = arg(2); if (empty($vid)) { $vocabularies = taxonomy_get_vocabularies(); if (!count($vocabularies)) { return t('No vocabularies have yet been created.'); } } else { $vocabularies = array(); $vocabularies[] = taxonomy_get_vocabulary($vid); } foreach ($vocabularies as $vocabulary) { taxonomy_context_load('vocabulary', $vocabulary); $vocabulary = taxonomy_context_prepare($vocabulary, FALSE); $vocabulary->links = module_invoke_all('link', 'vocabulary', $vocabulary); if (module_exists('i18n') && module_exists('translation')) { $result = db_query("SELECT * FROM {term_data} WHERE vid = %d AND language = '%s'", $vocabulary->vid, _i18n_get_lang()); $vocabulary->terms = array(); while ($term = db_fetch_object($result)) { $vocabulary->terms[] = $term; } } else { $vocabulary->terms = taxonomy_get_tree($vocabulary->vid, 0, -1, 1); } if (count($vocabulary->terms)) { foreach(array_keys($vocabulary->terms) as $key){ if (variable_get('taxonomy_context_show_empty', 1) || taxonomy_term_count_nodes($vocabulary->terms[$key]->tid)) { $vocabulary->terms[$key]->description = node_teaser($vocabulary->terms[$key]->description); // Load the term to ensure it gets a format assigned. taxonomy_context_load('term', $vocabulary->terms[$key]); $vocabulary->terms[$key] = taxonomy_context_prepare($vocabulary->terms[$key], FALSE); } else { unset($vocabulary->terms[$key]); } } } if (count($vocabularies) > 1) { $output .= theme('taxonomy_context_vocabulary_list', $vocabulary); } else { drupal_set_title(check_plain($vocabulary->name)); $output .= theme('taxonomy_context_vocabulary', $vocabulary); } } return $output; } /** * Return themed output of given term. * * @param $tid * The term id * @return * Themed output of term */ function taxonomy_context_show_term($tid) { $term = taxonomy_get_term($tid); // Load filter format data. taxonomy_context_load('term', $term); $term = taxonomy_context_prepare($term); $term->links = module_invoke_all('link', 'term', $term); return theme('taxonomy_context_term', $term); } /** * Return themed output of child terms of the given term. * * @param $tid * The term id * @return * Themed output of subterms */ function taxonomy_context_show_subterms($tid, $vid = 0) { $terms = taxonomy_get_children($tid, $vid); if (!variable_get('taxonomy_context_show_empty', 1)) { foreach ($terms as $key=>$term) { if (taxonomy_term_count_nodes($term->tid) == 0) { unset($terms[$key]); } } } $output = theme('taxonomy_context_subterms', $terms); return $output; } /** * Display subterms. */ function theme_taxonomy_context_subterms($terms) { $output = ''; if (count($terms)) { foreach ($terms as $term) { taxonomy_context_load('term', $term); $term->subterm = TRUE; $term->teaser = isset($term->description) ? node_teaser($term->description, isset($term->format) ? $term->format : NULL) : ''; $term = taxonomy_context_prepare($term, TRUE); $term->links = module_invoke_all('link', 'term', $term); $output .= theme('taxonomy_context_term', $term); } } return $output; } /** * Return a menu tree for a vocabulary. */ function taxonomy_context_menu_tree($vid, $tid = NULL) { static $context; if (!isset($context)) { $context = taxonomy_context_get_context(); } $parents = array(); $parents_terms = array_reverse(taxonomy_get_parents_all($context->tid)); foreach ($parents_terms as $parent) { $parents[] = $parent->tid; } $terms = $tid ? taxonomy_get_children($tid, $vid) : taxonomy_get_tree($vid, 0, -1, 1); if ($terms) { $output = "\n"; } return $output; } /** * Return ids of children terms */ function taxonomy_context_vocabulary_children($vid) { $children = array(); $result = db_query(db_rewrite_sql('SELECT t.tid FROM {term_hierarchy} h INNER JOIN {term_data} t ON h.tid = t.tid WHERE h.parent = 0 AND t.vid = %d ORDER BY t.weight, t.name', 't', 'tid'), $vid); while ($term = db_fetch_object($result)) { $children[] = $term->tid; } if (count($children) > 0) { return $children; } else { return FALSE; } } /** * Return a list of nodes below the given term */ function taxonomy_context_nodes_links($tid) { static $context; if (!isset($context)) { $context = taxonomy_context_get_context(); } $output = ''; $where = 'WHERE tn.tid = %d ' . $where .= "AND n.status = '1' "; switch (variable_get('taxonomy_context_node_block', TAXONOMY_CONTEXT_NODE_BLOCK_NONE)) { case TAXONOMY_CONTEXT_NODE_BLOCK_STICKY: $where .= "AND n.sticky = '1' "; break; case TAXONOMY_CONTEXT_NODE_BLOCK_PROMOTE: $where .= "AND n.promote = '1' "; break; } $sql = 'SELECT DISTINCT(n.nid), n.title, r.teaser ' . 'FROM {node} n ' . 'LEFT JOIN {term_node} tn ON n.nid = tn.nid ' . 'LEFT JOIN {users} u ON n.uid = u.uid ' . 'INNER JOIN {node_revisions} r ON n.vid = r.vid ' . $where . ' ORDER BY sticky DESC, n.created DESC'; $result = db_query(db_rewrite_sql($sql), $tid); while ($node = db_fetch_object($result)) { $params = array('title' => check_markup($node->teaser, $node->format)); $link = l($node->title, 'node/'. $node->nid, $params); $output .= '