View source
<?php
function tagadelic_help($path, $arg) {
switch ($path) {
case 'admin/help#tagadelic':
return t('Tagadelic offers dynamic urls. <br/>Visit example.com/tagadelic/list/2,1,5 to get the vocabularies 2,1 and 5 listed as tag groups. <br/>Visit example.com/tagadelic/chunk/2,1,5 to get a tag cloud of the terms in the vocabularies 2,1 and 5.<br/> Note that we limit to five vocabularies.');
}
}
function tagadelic_init() {
drupal_add_css(drupal_get_path('module', 'tagadelic') . '/tagadelic.css');
}
function tagadelic_menu() {
$items = array();
$items['admin/config/content/tagadelic'] = array(
'title' => 'Tagadelic configuration',
'description' => 'Configure the tag clouds. Set the order, the number of tags, and the depth of the clouds.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'tagadelic_settings',
),
'access callback' => 'user_access',
'access arguments' => array(
'administer taxonomy',
),
);
$items['tagadelic'] = array(
'title' => 'Tags',
'page callback' => 'tagadelic_page_chunk',
'page arguments' => array(
NULL,
),
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
$items['tagadelic/list/%tagadelic_vocs'] = array(
'title' => 'Tags',
'page callback' => 'tagadelic_page_list',
'page arguments' => array(
2,
),
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_CALLBACK,
);
$items['tagadelic/chunk/%tagadelic_vocs'] = array(
'title' => 'Tags',
'page callback' => 'tagadelic_page_chunk',
'page arguments' => array(
2,
),
'access callback' => 'user_access',
'access arguments' => array(
'access content',
),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
function tagadelic_settings() {
$options = array(
'weight,asc' => t('by weight, ascending'),
'weight,desc' => t('by weight, descending'),
'title,asc' => t('by title, ascending'),
'title,desc' => t('by title, descending'),
'random,none' => t('random'),
);
$form['tagadelic_sort_order'] = array(
'#type' => 'radios',
'#title' => t('Tagadelic sort order'),
'#options' => $options,
'#default_value' => variable_get('tagadelic_sort_order', 'title,asc'),
'#description' => t('Determines the sort order of the tags on the freetagging page.'),
);
$form['tagadelic_page_amount'] = array(
'#type' => 'textfield',
'#size' => 5,
'#title' => t('Amount of tags on the pages'),
'#default_value' => variable_get('tagadelic_page_amount', '60'),
'#description' => t('The amount of tags that will show up in a cloud on the pages. Amount of tags in blocks must be configured in the block settings of the various cloud blocks.'),
);
$form['tagadelic_levels'] = array(
'#type' => 'textfield',
'#size' => 5,
'#title' => t('Number of levels'),
'#default_value' => variable_get('tagadelic_levels', 6),
'#description' => t('The number of levels between the least popular tags and the most popular ones. Different levels will be assigned a different class to be themed in tagadelic.css'),
);
return system_settings_form($form);
}
function tagadelic_vocs_load($vocs) {
if (is_numeric($vocs)) {
$vocs = array(
$vocs,
);
}
elseif (preg_match('/^([0-9]+,){1,5}[0-9]+$/', $vocs)) {
$vocs = explode(',', $vocs);
}
return $vocs;
}
function tagadelic_page_chunk($vocs) {
if ($vocs == NULL) {
foreach (taxonomy_get_vocabularies(NULL) as $vocabulary) {
$vocs[] = $vocabulary->vid;
}
}
$tags = tagadelic_get_weighted_tags($vocs, variable_get('tagadelic_levels', 6), variable_get('tagadelic_page_amount', '60'));
$tags = tagadelic_sort_tags($tags);
$output = theme('tagadelic_weighted', array(
'terms' => $tags,
));
if (!$output) {
return drupal_not_found();
}
$output = "<div class=\"wrapper tagadelic\">{$output}</div>";
return $output;
}
function tagadelic_page_list($vocs) {
if ($vocs == NULL) {
return drupal_not_found();
}
$output = '';
foreach ($vocs as $vid) {
$vocabulary = taxonomy_vocabulary_load($vid);
$vocabulary->description = filter_xss_admin($vocabulary->description);
$vocabulary->name = filter_xss_admin($vocabulary->name);
$tags = tagadelic_get_weighted_tags(array(
$vocabulary->vid,
), variable_get('tagadelic_levels', 6), variable_get('tagadelic_page_amount', '60'));
$tags = tagadelic_sort_tags($tags);
$output .= theme('tagadelic_list_box', array(
'vocabulary' => $vocabulary,
'tags' => $tags,
));
}
if (!$output) {
return drupal_not_found();
}
$output = "<div class=\"wrapper tagadelic\">{$output}</div>";
return $output;
}
function tagadelic_node_get_terms($node) {
static $vocs;
if ($terms = taxonomy_node_get_terms($node, 'tid')) {
if (!isset($vocs[$node->type])) {
$vocs[$node->type] = taxonomy_get_vocabularies($node->type);
}
$tags = array();
foreach ($terms as $tid => $term) {
if ($vocs[$node->type][$term->vid]->tags) {
$tags[$term->vid][$tid] = $term;
}
}
return $tags;
}
}
function tagadelic_get_weighted_tags($vids, $steps = 6, $size = 60) {
global $language;
$options = implode('_', $vids) . '_' . $language->language . '_' . $steps . '_' . $size;
$cache_name = 'tagadelic_cache_' . $options;
$cache = cache_get($cache_name, 'cache_page');
$tags = array();
if (isset($cache->data)) {
$tags = $cache->data;
}
else {
if (!is_array($vids) || count($vids) == 0) {
return array();
}
$result = db_query_range('SELECT COUNT(*) AS count, td.tid, td.vid, td.name, td.description FROM {taxonomy_term_data} td INNER JOIN {taxonomy_index} tn ON td.tid = tn.tid INNER JOIN {node} n ON tn.nid = n.nid WHERE td.vid IN (' . implode(',', array_fill(0, count($vids), '?')) . ') GROUP BY td.tid, td.vid, td.name, td.description HAVING COUNT(*) > 0 ORDER BY count DESC', 0, $size, $vids);
foreach ($result as $tag) {
$tags[$tag->tid] = $tag;
}
$tags = tagadelic_build_weighted_tags($tags, $steps);
cache_set($cache_name, $tags, 'cache_page', CACHE_TEMPORARY);
}
return $tags;
}
function tagadelic_build_weighted_tags($tags, $steps = 6) {
$tags_tmp = array();
$min = 1000000000.0;
$max = -1000000000.0;
foreach ($tags as $id => $tag) {
$tag->number_of_posts = $tag->count;
$tag->count = log($tag->count);
$min = min($min, $tag->count);
$max = max($max, $tag->count);
$tags_tmp[$id] = $tag;
}
$range = max(0.01, $max - $min) * 1.0001;
foreach ($tags_tmp as $key => $value) {
$tags[$key]->weight = 1 + floor($steps * ($value->count - $min) / $range);
}
return $tags;
}
function tagadelic_sort_tags($tags) {
list($sort, $order) = explode(',', variable_get('tagadelic_sort_order', 'title,asc'));
switch ($sort) {
case 'title':
usort($tags, "_tagadelic_sort_by_title");
break;
case 'weight':
usort($tags, "_tagadelic_sort_by_weight");
break;
case 'random':
shuffle($tags);
break;
}
if ($order == 'desc') {
$tags = array_reverse($tags);
}
return $tags;
}
function _tagadelic_sort_by_title($a, $b) {
return strnatcasecmp($a->name, $b->name);
}
function _tagadelic_sort_by_weight($a, $b) {
if ($a->weight == $b->weight) {
return $a->count > $b->count;
}
return $a->weight > $b->weight;
}
function theme_tagadelic_weighted(array $vars) {
$terms = $vars['terms'];
$output = '';
foreach ($terms as $term) {
$output .= l($term->name, 'taxonomy/term/' . $term->tid, array(
'attributes' => array(
'class' => array(
"tagadelic",
"level" . $term->weight,
),
'rel' => 'tag',
'title' => $term->description,
),
)) . " \n";
}
if (count($terms) >= variable_get('tagadelic_block_tags_' . $vars['voc']->vid, 12)) {
$output .= theme('more_link', array(
'title' => t('more tags'),
'url' => "tagadelic/chunk/{$vars['voc']->vid}",
));
}
return $output;
}
function theme_tagadelic_list_box(array $vars) {
$vocabulary = $vars['vocabulary'];
$tags = $vars['tags'];
$content = theme('tagadelic_weighted', array(
'terms' => $tags,
));
$output = '';
if ($vocabulary->description) {
$content = '<h2></h2><div>' . $vocabulary->description . '</div>' . $content;
}
$output .= '<h2>' . $vocabulary->name . '</h2><div>' . $content . '</div>';
return $output;
}
function tagadelic_block_info() {
$blocks = array();
foreach (taxonomy_get_vocabularies() as $voc) {
$blocks[$voc->vid] = array();
$blocks[$voc->vid]['info'] = variable_get('tagadelic_block_title_' . $voc->vid, t('Tags in @voc', array(
'@voc' => $voc->name,
)));
$blocks[$voc->vid]['cache'] = DRUPAL_CACHE_GLOBAL;
}
return $blocks;
}
function tagadelic_block_view($delta = '') {
$block = array();
if ($voc = taxonomy_vocabulary_load($delta)) {
$blocks['subject'] = variable_get('tagadelic_block_title_' . $delta, t('Tags in @voc', array(
'@voc' => $voc->name,
)));
$tags = tagadelic_get_weighted_tags(array(
$delta,
), variable_get('tagadelic_levels', 6), variable_get('tagadelic_block_tags_' . $delta, 12));
$tags = tagadelic_sort_tags($tags);
$blocks['content'] = theme('tagadelic_weighted', array(
'terms' => $tags,
'voc' => $voc,
));
}
return $blocks;
}
function tagadelic_block_configure($delta = '') {
$voc = taxonomy_vocabulary_load($delta);
$form = array();
$form['tags'] = array(
'#type' => 'textfield',
'#title' => t('Tags to show'),
'#default_value' => variable_get('tagadelic_block_tags_' . $delta, 12),
'#maxlength' => 3,
'#description' => t('The number of tags to show in this block.'),
);
return $form;
}
function tagadelic_block_save($delta = '', $edit = array()) {
variable_set('tagadelic_block_tags_' . $delta, $edit['tags']);
}
function tagadelic_theme() {
return array(
'tagadelic_list_box' => array(
'arguments' => array(
'vocabulary' => NULL,
'tags' => NULL,
),
),
'tagadelic_weighted' => array(
'arguments' => array(
'terms' => array(),
),
),
);
}