<?php
define('NODETERMS_VOCABULARY_DISPLAY_NONE', 'none');
define('NODETERMS_VOCABULARY_DISPLAY_DEFAULT', 'default');
define('NODETERMS_VOCABULARY_DISPLAY_HIDDEN', 'hidden');
define('NODETERMS_VOCABULARY_DISPLAY_BLOCK', 'block');
define('NODETERMS_VOCABULARY_DISPLAY_FIELDSET', 'fieldset');
define('NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSED', 'fieldset-collapsed');
define('NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSIBLE', 'fieldset-collapsible');
define('NODETERMS_VOCABULARY_DISPLAY_INLINE', 'inline');
define('NODETERMS_VOCABULARY_DISPLAY_CUSTOM', 'custom');
define('NODETERMS_TERMS_DISPLAY_DEFAULT', 'default');
define('NODETERMS_TERMS_DISPLAY_LINKS', 'links');
define('NODETERMS_TERMS_DISPLAY_LIST', 'list');
define('NODETERMS_TERMS_DISPLAY_TREE', 'tree');
define('NODETERMS_TERMS_DISPLAY_BREADCRUMBS', 'breadcrumbs');
define('NODETERMS_TERMS_DISPLAY_DELIMITED', 'delimited');
define('NODETERMS_TERMS_DISPLAY_CUSTOM', 'custom');
function nodeterms_menu() {
$items['admin/content/taxonomy/nodeterms'] = array(
'title' => 'Node terms',
'page callback' => 'drupal_get_form',
'page arguments' => array('nodeterms_admin_form'),
'access arguments' => array('administer taxonomy'),
'file' => 'nodeterms.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function nodeterms_help($path, $args) {
switch ($path) {
case 'admin/content/taxonomy/nodeterms':
return t("The 'Node terms' module allows selected node vocabulary terms to be group by vocabulary and re-formatted with a custom display.");
}
}
function nodeterms_form_taxonomy_form_vocabulary_alter(&$form, &$form_state) {
drupal_add_js( drupal_get_path('module', 'nodeterms') .'/nodeterms.toggle.js');
$data = nodeterms_data($form['vid']['#value']);
$vocab_options = array(
NODETERMS_VOCABULARY_DISPLAY_DEFAULT => t('<use default>'),
NODETERMS_VOCABULARY_DISPLAY_HIDDEN => t('hidden'),
NODETERMS_VOCABULARY_DISPLAY_BLOCK => t('block'),
NODETERMS_VOCABULARY_DISPLAY_INLINE => t('inline'),
NODETERMS_VOCABULARY_DISPLAY_FIELDSET => t('fieldset'),
NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSIBLE => t('fieldset - collapsible'),
NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSED => t('fieldset - collapsible+collapsed'),
NODETERMS_VOCABULARY_DISPLAY_CUSTOM => t('custom'),
);
$terms_options = array(
NODETERMS_TERMS_DISPLAY_DEFAULT => t('<use default>'),
NODETERMS_TERMS_DISPLAY_LINKS => t('links'),
NODETERMS_TERMS_DISPLAY_LIST => t('list'),
NODETERMS_TERMS_DISPLAY_DELIMITED => t('delimited values'),
NODETERMS_TERMS_DISPLAY_TREE => t('tree'),
NODETERMS_TERMS_DISPLAY_BREADCRUMBS => t('breadcrumbs'),
NODETERMS_TERMS_DISPLAY_CUSTOM => t('custom'),
);
$form['identification']['#weight'] = -1;
$form['nodeterms']= array(
'#type' => 'fieldset',
'#title' => t('Node terms display settings'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => -0.5,
);
$default_teaser_vocabulary_display = variable_get('nodeterms_default_teaser_vocabulary_display', NODETERMS_VOCABULARY_DISPLAY_INLINE);
$vocab_options[NODETERMS_VOCABULARY_DISPLAY_DEFAULT] = t('<use default setting>') .' - '. $vocab_options[$default_teaser_vocabulary_display];
$default_teaser_terms_display = variable_get('nodeterms_default_teaser_terms_display', NODETERMS_TERMS_DISPLAY_DELIMITED);
$terms_options[NODETERMS_TERMS_DISPLAY_DEFAULT] = t('<use default setting>') .' - '. $terms_options[$default_teaser_terms_display];
$form['nodeterms']['teaser_vocabulary_display'] = array(
'#type' => 'select',
'#title' => t('Teaser vocabulary display'),
'#default_value' => $data['teaser_vocabulary_display'],
'#options' => $vocab_options,
'#required' => TRUE,
);
$style = _nodeterms_get_form_terms_display_style($data['teaser_vocabulary_display']);
$form['nodeterms']['teaser_terms_display'] = array(
'#type' => 'select',
'#title' => t('Teaser terms display'),
'#default_value' => $data['teaser_terms_display'],
'#options' => $terms_options,
'#required' => TRUE,
'#prefix' => '<div id="toggle-teaser-terms-display" class="vocabulary-terms"'. $style .'>',
'#suffix' => '</div>',
);
$default_page_vocabulary_display = variable_get('nodeterms_default_page_vocabulary_display', NODETERMS_VOCABULARY_DISPLAY_BLOCK);
$vocab_options[NODETERMS_VOCABULARY_DISPLAY_DEFAULT] = t('<use default setting>') .' - '. $vocab_options[$default_page_vocabulary_display];
$default_page_terms_display = variable_get('nodeterms_default_page_terms_display', NODETERMS_TERMS_DISPLAY_LINKS);
$terms_options[NODETERMS_TERMS_DISPLAY_DEFAULT] = t('<use default setting>') .' - '. $terms_options[$default_page_terms_display];
$form['nodeterms']['page_vocabulary_display'] = array(
'#type' => 'select',
'#title' => t('Page vocabulary display'),
'#default_value' => $data['page_vocabulary_display'],
'#options' => $vocab_options,
'#required' => TRUE,
);
$style = _nodeterms_get_form_terms_display_style($data['page_vocabulary_display']);
$form['nodeterms']['page_terms_display'] = array(
'#type' => 'select',
'#title' => t('Page terms display'),
'#default_value' => $data['page_terms_display'],
'#options' => $terms_options,
'#required' => TRUE,
'#prefix' => '<div id="toggle-page-terms-display" class="vocabulary-terms"'. $style .'>',
'#suffix' => '</div>',
);
}
function _nodeterms_get_form_terms_display_style($display_type) {
if ($display_type == NODETERMS_VOCABULARY_DISPLAY_HIDDEN) {
return ' style="display:none"';
}
else {
return '';
}
}
function nodeterms_data($vid) {
static $nodeterms_data;
if (empty($nodeterms_data)) {
$result = db_query('SELECT vid, teaser_vocabulary_display, teaser_terms_display, page_vocabulary_display, page_terms_display FROM {nodeterms}');
while ($vocabulary = db_fetch_array($result)) {
$nodeterms_data[$vocabulary['vid']] = $vocabulary;
}
}
if (isset($nodeterms_data[$vid])) {
return $nodeterms_data[$vid];
}
else {
return array(
'teaser_vocabulary_display' => NODETERMS_VOCABULARY_DISPLAY_DEFAULT,
'teaser_terms_display' => NODETERMS_TERMS_DISPLAY_DEFAULT,
'page_vocabulary_display' => NODETERMS_VOCABULARY_DISPLAY_DEFAULT,
'page_terms_display' => NODETERMS_TERMS_DISPLAY_DEFAULT,
);
}
}
function nodeterms_taxonomy($op, $type, $object = NULL) {
if ($type =! 'vocabulary') {
return;
}
switch ($op) {
case 'delete':
db_query('DELETE FROM {nodeterms} WHERE vid = %d', $object['vid']);
break;
case 'update':
$sql = "UPDATE {nodeterms} SET teaser_vocabulary_display = '%s', teaser_terms_display = '%s', page_vocabulary_display = '%s', page_terms_display = '%s' WHERE vid = %d";
db_query($sql, $object['teaser_vocabulary_display'], $object['teaser_terms_display'], $object['page_vocabulary_display'], $object['page_terms_display'], $object['vid']);
break;
case 'insert':
$sql = "INSERT INTO {nodeterms} (teaser_vocabulary_display, teaser_terms_display, page_vocabulary_display, page_terms_display, vid) VALUES ('%s', '%s', '%s', '%s', %d)";
db_query($sql, $object['teaser_vocabulary_display'], $object['teaser_terms_display'], $object['page_vocabulary_display'], $object['page_terms_display'], $object['vid']);
break;
}
if ($op == 'update' || $op == 'insert') {
nodeterms_cache_clear();
}
}
function nodeterms_preprocess_node(&$variables) {
$output = nodeterms_vocabularies_output(node_load($variables['nid']), $variables['teaser']);
if ($output !== NULL) {
$variables['terms'] = $output;
}
}
function nodeterms_vocabularies_output($node, $teaser = FALSE) {
drupal_add_css(drupal_get_path('module', 'nodeterms') .'/nodeterms.css');
$display = ($teaser) ? 'teaser' : 'page';
$cached = nodeterms_cache_get($node, $display);
if ($cached !== NULL) {
return $cached;
}
if ($teaser) {
$display_default_type = variable_get('nodeterms_default_teaser_vocabulary_display', NODETERMS_VOCABULARY_DISPLAY_INLINE);
$terms_default_type = variable_get('nodeterms_default_teaser_terms_display', NODETERMS_TERMS_DISPLAY_DELIMITED);
}
else {
$display_default_type = variable_get('nodeterms_default_page_vocabulary_display', NODETERMS_VOCABULARY_DISPLAY_BLOCK);
$terms_default_type = variable_get('nodeterms_default_page_terms_display', NODETERMS_TERMS_DISPLAY_LINK);
}
if (!$node->taxonomy || $display_default_type == NODETERMS_VOCABULARY_DISPLAY_DEFAULT) {
return NULL;
}
if ($display_default_type == NODETERMS_VOCABULARY_DISPLAY_HIDDEN) {
return '';
}
$output = '';
$vocabularies = taxonomy_get_vocabularies($node->type);
foreach ($vocabularies as $vocabulary) {
$data = nodeterms_data($vocabulary->vid);
$display_type = $data[$display .'_vocabulary_display'];
if ($display_type == NODETERMS_VOCABULARY_DISPLAY_DEFAULT) {
$display_type = $display_default_type;
}
$terms_type = $data[$display .'_terms_display'];
if ($terms_type == NODETERMS_TERMS_DISPLAY_DEFAULT) {
$terms_type = $terms_default_type;
}
$output .= nodeterms_vocabulary_output($node, $vocabulary, $display_type, $terms_type);
}
if ($output != '') {
$output = '<div class="nodeterms-terms">'. $output .'</div>';
}
nodeterms_cache_set($node, $display, $output);
return $output;
}
function nodeterms_cache_get($node, $display = 'teaser') {
$cache_lifetime = variable_get('nodeterms_cache_lifetime', 0);
if (!$cache_lifetime) {
return NULL;
}
$settings_last_updated = variable_get('nodeterms_last_updated', '0');
$cid = 'nodeterms-'. $display .'-'. $node->nid;
$cached = cache_get($cid, 'cache');
if ($cached && $cached->expire >= time() && $cached->created >= $node->revision_timestamp && $cached->created >= $settings_last_updated ) {
return $cached->data;
}
}
function nodeterms_cache_set($node, $display = 'teaser', $data = NULL) {
$cache_lifetime = variable_get('nodeterms_cache_lifetime', 0);
if (!$cache_lifetime) {
return;
}
$cid = 'nodeterms-'. $display .'-'. $node->nid;
$cache_expire = $cache_lifetime * 60 + time();
cache_set($cid, $data, 'cache', $cache_expire);
}
function nodeterms_cache_clear() {
$cache_lifetime = variable_get('nodeterms_cache_lifetime', 0);
if (!$cache_lifetime) {
return NULL;
}
cache_clear_all('nodeterms', 'cache', TRUE);
variable_set('nodeterms_last_updated', time());
}
function nodeterms_vocabulary_output($node, $vocabulary, $display_type = NODETERMS_VOCABULARY_DISPLAY_BLOCK, $terms_type = NODETERMS_TERMS_DISPLAY_LINKS) {
if ($display_type == NODETERMS_VOCABULARY_DISPLAY_HIDDEN) {
return '';
}
switch ($terms_type) {
case NODETERMS_TERMS_DISPLAY_BREADCRUMBS;
$vocabulary_breadcrumbs = _nodeterms_get_vocabulary_breadcrumbs($node, $vocabulary);
$content = ($vocabulary_breadcrumbs) ? theme('nodeterms_terms_display_breadcrumbs', $vocabulary_breadcrumbs) : NULL;
break;
case NODETERMS_TERMS_DISPLAY_TREE;
$vocabulary_tree = _nodeterms_get_vocabulary_tree($node, $vocabulary);
$content = ($vocabulary_tree) ? theme('nodeterms_terms_display_tree', $vocabulary_tree) : NULL;
break;
case NODETERMS_TERMS_DISPLAY_LINKS;
$vocabulary_links = _nodeterms_get_vocabulary_links($node, $vocabulary);
$content = ($vocabulary_links) ? theme('nodeterms_terms_display_links', $vocabulary_links) : NULL;
break;
case NODETERMS_TERMS_DISPLAY_LIST;
$vocabulary_links = _nodeterms_get_vocabulary_links($node, $vocabulary);
$content = ($vocabulary_links) ? theme('nodeterms_terms_display_list', $vocabulary_links) : NULL;
break;
case NODETERMS_TERMS_DISPLAY_DELIMITED;
$vocabulary_links = _nodeterms_get_vocabulary_links($node, $vocabulary);
$content = ($vocabulary_links) ? theme('nodeterms_terms_display_delimited', $vocabulary_links) : NULL;
break;
case NODETERMS_TERMS_DISPLAY_CUSTOM;
$content = theme('nodeterms_terms_display_custom', $node, $vocabulary);
break;
default:
$content = '';
break;
}
if (!$content) {
return '';
}
switch ($display_type) {
case NODETERMS_VOCABULARY_DISPLAY_BLOCK;
return theme('nodeterms_vocabulary_display_block', $node, $vocabulary, $content);
case NODETERMS_VOCABULARY_DISPLAY_FIELDSET;
case NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSIBLE;
case NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSED;
return theme('nodeterms_vocabulary_display_fieldset', $node, $vocabulary, $content, $display_type);
break;
case NODETERMS_VOCABULARY_DISPLAY_INLINE;
return theme('nodeterms_vocabulary_display_inline', $node, $vocabulary, $content);
case NODETERMS_VOCABULARY_DISPLAY_CUSTOM;
return theme('nodeterms_vocabulary_display_custom', $node, $vocabulary, $content);
default:
return '';
}
}
function _nodeterms_get_vocabulary_tree($node, $vocabulary) {
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
if (!$terms) {
return NULL;
}
$tree = array();
foreach ($terms as $term) {
$branch = &$tree;
$parents = array_reverse( taxonomy_get_parents_all($term->tid) );
foreach ($parents as $parent) {
$tkey = $parent->name;
if (!isset($branch[$tkey]['term'])) {
$parent->path = NULL;
$branch[$tkey]['term'] = $parent;
}
if ($parent->tid == $term->tid) {
$branch[$tkey]['term']->path = taxonomy_term_path($term);
}
$branch = &$branch[$tkey]['below'];
}
}
return $tree;
}
function _nodeterms_get_vocabulary_breadcrumbs($node, $vocabulary) {
$delimiter = variable_get('nodeterms_breadcrumbs_delimiter', ' > ');
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
if (!$terms) {
return NULL;
}
$breadcrumbs = array();
foreach ($terms as $term) {
$parents = array_reverse( taxonomy_get_parents_all($term->tid) );
$breadcrumb = '';
$key = ''; foreach ($parents as $parent) {
if ($parent->tid == $term->tid) {
$breadcrumb .= l($parent->name, taxonomy_term_path($term) );
$key .= $parent->name;
}
else {
$breadcrumb .= '<span>'. $parent->name .'</span> '. $delimiter;
$key .= $parent->name .'-';
}
}
$breadcrumbs[$key] = $breadcrumb;
}
ksort(&$breadcrumbs);
return $breadcrumbs;
}
function _nodeterms_get_vocabulary_links($node, $vocabulary) {
foreach ($node->taxonomy as $term) {
if ($term->vid == $vocabulary->vid) {
$links['taxonomy_term_'. $term->tid] = array(
'title' => $term->name,
'href' => taxonomy_term_path($term),
'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
);
}
}
return $links;
}
function nodeterms_theme() {
return array(
'nodeterms_vocabulary_display_block' => array(
'arguments' => array('node' => NULL, 'vocabulary' => NULL, 'content' => NULL),
),
'nodeterms_vocabulary_display_fieldset' => array(
'arguments' => array('node' => NULL, 'vocabulary' => NULL, 'content' => NULL, 'type' => NULL, 'form_id' => NULL),
),
'nodeterms_vocabulary_display_inline' => array(
'arguments' => array('node' => NULL, 'vocabulary' => NULL, 'content' => NULL),
),
'nodeterms_vocabulary_display_custom' => array(
'arguments' => array('node' => NULL, 'vocabulary' => NULL, 'content' => NULL),
),
'nodeterms_terms_display_tree' => array(
'arguments' => array('branch' => NULL),
),
'nodeterms_terms_display_breadcrumbs' => array(
'arguments' => array('breadcrumbs' => NULL),
),
'nodeterms_terms_display_links' => array(
'arguments' => array('links' => NULL),
),
'nodeterms_terms_display_list' => array(
'arguments' => array('links' => NULL),
),
'nodeterms_terms_display_delimited' => array(
'arguments' => array('links' => NULL),
),
'nodeterms_terms_display_custom' => array(
'arguments' => array('node' => NULL, 'vocabulary' => NULL),
),
);
}
function theme_nodeterms_vocabulary_display_block($node, $vocabulary, $content) {
$block = new stdClass();
$block->module = 'nodeterms';
$block->delta = 'nid_'. $node->nid .'_vid_'. $vocabulary->vid;
$block->subject = $vocabulary->name;
$block->content = $content;
$block->node = $node;
$block->node = $vocabulary;
return theme('block', $block);
}
function theme_nodeterms_vocabulary_display_fieldset($node, $vocabulary, $content, $type) {
$element = array(
'#type' => 'fieldset',
'#title' => $vocabulary->name,
'#value' => $content,
'#attributes' => array(
'id' => 'fieldset-nid-'. $node->nid .'-vid-'. $vocabulary->vid,
'class' => 'nodeterms-vocabulary-fieldset'
),
'#container_id' => 'node-'. $node->nid,
);
switch ($type) {
case NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSIBLE;
$element['#collapsible'] = TRUE;
break;
case NODETERMS_VOCABULARY_DISPLAY_FIELDSET_COLLAPSED;
$element['#collapsible'] = TRUE;
$element['#collapsed'] = TRUE;
break;
default:
break;
}
return theme('fieldset', $element);
}
function theme_nodeterms_vocabulary_display_inline($node, $vocabulary, $content) {
$delimiter = variable_get('nodeterms_inline_delimiter', ': ');
$output = '<div class="nodeterms-vocabulary-inline">';
$output .= '<strong>'. $vocabulary->name . $delimiter .'</strong>';
$output .= $content;
$output .= '</div>';
return $output;
}
function theme_nodeterms_vocabulary_display_custom($node, $vocabulary, $content) {
$output = '<div class="nodeterms-vocabulary-custom">';
$output .= '<h3>'. $vocabulary->name .'</h3>';
$output .= $content;
$output .= '</div>';
return $output;
}
function theme_nodeterms_terms_display_tree($branch) {
ksort(&$branch);
$output = '<ul>';
foreach ($branch as $key => $value) {
$term = $value['term'];
$output .= '<li>';
$output .= ($term->path) ? l($term->name, $term->path ) : '<span>'. $term->name .'</span>';
if ($value['below']) {
$output .= theme_nodeterms_terms_display_tree($value['below']);
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
function theme_nodeterms_terms_display_breadcrumbs($breadcrumbs) {
$output = '<ul>';
foreach ($breadcrumbs as $breadcrumb) {
$output .= '<li>'. $breadcrumb .'</li>';
}
$output .= '</ul>';
return $output;
}
function theme_nodeterms_terms_display_links($links) {
return theme('links', $links);
}
function theme_nodeterms_terms_display_list($links) {
return theme('links', $links, NULL);
}
function theme_nodeterms_terms_display_delimited($links) {
foreach ($links as $key => $link) {
$links[$key] = l($link['title'], $link['href'], $link);
}
$delimiter = variable_get('nodeterms_delimited_delimiter', ', ');
return implode($delimiter, $links);
}
function theme_nodeterms_terms_display_custom($node, $vocabulary) {
$links = _nodeterms_get_vocabulary_links($node, $vocabulary);
foreach ($links as $key => $link) {
$links[$key] = l($link['title'], $link['href'], $link);
}
$delimiter = variable_get('nodeterms_delimited_delimiter', ', ');
return implode($delimiter, $links);
}