View source
<?php
define('COMMUNITY_TAGS_MODE_BLOCK', 0);
define('COMMUNITY_TAGS_MODE_TAB', 1);
define('COMMUNITY_TAGS_MODE_INLINE', 2);
define('COMMUNITY_TAGS_OPMODE_NOSYNC', 0x0);
define('COMMUNITY_TAGS_OPMODE_SYNC', 0x1);
define('COMMUNITY_TAGS_OPMODE_DELETE_ORPHAN_TERMS', 0x2);
function community_tags_help($path, $arg) {
switch ($path) {
case 'admin/settings/community-tags':
return t('To set up community tagging, you must first <a href="@taxonomy">create a normal free tagged vocabulary</a>. Then activate community tagging on such a vocabulary below, and set the <a href="@workflow">workflow options</a> for node types to control how users can tag nodes.', array(
'@taxonomy' => url('admin/content/taxonomy'),
'@workflow' => url('admin/content/types'),
));
break;
}
}
function community_tags_theme() {
return array(
'community_tags_form' => array(
'arguments' => array(
'form' => NULL,
),
'file' => 'community_tags.pages.inc',
),
'community_tags' => array(
'arguments' => array(
'tags' => NULL,
),
),
'community_tags_links' => array(
'arguments' => array(
'tags' => NULL,
),
),
'community_tags_settings' => array(
'arguments' => array(
'element' => NULL,
),
),
);
}
function community_tags_menu() {
$items = array();
$items['admin/settings/community-tags'] = array(
'title' => 'Community tags',
'description' => 'Configure community tagging.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'community_tags_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'community_tags.admin.inc',
);
$items['admin/settings/community-tags/ops/broken'] = array(
'title' => 'Delete broken community tags',
'description' => 'Delete broken community tags.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'community_tags_delete_broken_tags_form',
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
'file' => 'community_tags.admin.inc',
);
$items['admin/settings/community-tags/ops/rebuild/%taxonomy_vocabulary'] = array(
'title' => 'Rebuild community tags',
'description' => 'Rebuild community tags.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'community_tags_rebuild_form',
5,
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
'file' => 'community_tags.admin.inc',
);
$items['admin/settings/community-tags/ops/purge/%taxonomy_vocabulary'] = array(
'title' => 'Delete community tags',
'description' => 'Delete community tags.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'community_tags_delete_all_form',
5,
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
'file' => 'community_tags.admin.inc',
);
$items['community-tags/js/%node'] = array(
'page callback' => 'community_tags_from_js',
'page arguments' => array(
2,
),
'access callback' => '_community_tags_menu_access',
'type' => MENU_CALLBACK,
'file' => 'community_tags.ajax.inc',
);
$items['community-tags/user'] = array(
'page callback' => 'community_tags_by_user',
'access callback' => '_community_tags_menu_access',
'type' => MENU_CALLBACK,
);
$items['node/%node/tag'] = array(
'title' => 'Tags',
'page callback' => 'community_tags_node_view',
'page arguments' => array(
1,
FALSE,
),
'access callback' => '_community_tags_tab_access',
'access arguments' => array(
1,
),
'type' => MENU_LOCAL_TASK,
'weight' => 2,
'file' => 'community_tags.pages.inc',
);
return $items;
}
function community_tags_block($op = 'list', $delta = 0, $edit = array()) {
global $user;
switch ($op) {
case 'list':
$block[0] = array(
'info' => t('Community tagging form'),
'cache' => BLOCK_NO_CACHE,
);
return $block;
case 'view':
if (user_access('access content') && user_access('tag content')) {
if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == '' || arg(2) == 'view')) {
$node = menu_get_object();
if (_community_tags_is_tagging_view_visible($node, COMMUNITY_TAGS_MODE_BLOCK)) {
$block['subject'] = t('Tag this');
$block['content'] = community_tags_node_view($node, TRUE);
return $block;
}
}
}
break;
}
}
function community_tags_perm() {
return array(
'tag content',
'edit own tags',
);
}
function community_tags_nodeapi(&$node, $op, $teaser) {
switch ($op) {
case 'load':
$node->community_tags_form = _community_tags_is_tagging_view_visible($node, COMMUNITY_TAGS_MODE_INLINE);
break;
case 'insert':
_community_tags_node_insert($node);
break;
case 'update':
if (!isset($node->ct_user_tags)) {
_community_tags_node_update($node);
}
break;
case 'delete':
_community_tags_node_delete($node);
case 'view':
global $user;
if (!$teaser && $node->build_mode != NODE_BUILD_SEARCH_INDEX && $node->community_tags_form) {
$node->content['community_tags'] = array(
'#value' => community_tags_node_view($node, TRUE),
'#weight' => 50,
);
}
break;
}
}
function community_tags_taxonomy($op = NULL, $type = NULL, $term = NULL) {
if ($type == 'term' && $term['tid']) {
switch ($op) {
case 'delete':
$term = (object) $term;
_community_tags_term_delete($term);
break;
}
}
}
function community_tags_user($op, &$edit, &$user) {
if ($op == 'delete') {
_community_tags_user_delete($user);
}
}
function community_tags_content_extra_fields($type_name) {
$extra = array();
if (variable_get('community_tags_display_' . $type_name, COMMUNITY_TAGS_MODE_TAB) == COMMUNITY_TAGS_MODE_INLINE) {
$extra['community_tags'] = array(
'label' => t('Community Tags'),
'description' => t('Community Tags Form'),
'weight' => 100,
);
}
return $extra;
}
function community_tags_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'node_type_form' && isset($form['#node_type']->type)) {
$supported_vids = _community_tags_vids_for_node_type($form['#node_type']->type);
if (!empty($supported_vids)) {
$modes = array(
COMMUNITY_TAGS_MODE_BLOCK => t('Block'),
COMMUNITY_TAGS_MODE_TAB => t('Tab'),
COMMUNITY_TAGS_MODE_INLINE => t('Inline'),
);
$form['workflow']['community_tags_display'] = array(
'#type' => 'radios',
'#title' => t('Community tagging form'),
'#default_value' => variable_get('community_tags_display_' . $form['#node_type']->type, COMMUNITY_TAGS_MODE_TAB),
'#options' => $modes,
'#description' => t('How should users be allowed to tag content?'),
);
}
}
}
function community_tags_taxonomy_node_save($node, $tags_and_terms, $is_owner, $uid) {
$vids = community_tags_vids_for_node($node);
$processed_tags_and_terms = _community_tags_node_process_tags_and_terms($tags_and_terms, $vids);
$processed_terms = _community_tags_convert_new_tags_to_terms($processed_tags_and_terms);
$node_save_required = FALSE;
$possible_redundant_term_tids = array();
foreach ($processed_terms as $vid => $processed_terms_for_vocabulary) {
$existing_tags = _community_tags_get_node_user_vid_tags($node->nid, $uid, $vid);
$new_tags = array_diff_key($processed_terms_for_vocabulary, $existing_tags);
$removed_tags = array_diff_key($existing_tags, $processed_terms_for_vocabulary);
foreach ($new_tags as $tid => $value) {
_community_tags_add_tag($node->nid, $tid, $uid);
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_SYNC, $vid, $node->type) && !isset($node->taxonomy[$tid])) {
$node->taxonomy[$tid] = $processed_terms_for_vocabulary[$tid];
$node_save_required = TRUE;
}
}
foreach ($removed_tags as $tid => $value) {
_community_tags_delete_tag($node->nid, $tid, $uid);
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_SYNC, $vid, $node->type) && $existing_tags[$tid]->tag_count <= 1 && isset($node->taxonomy[$tid])) {
unset($node->taxonomy[$tid]);
$node_save_required = TRUE;
}
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_DELETE_ORPHAN_TERMS, $vid, $node->type)) {
$possible_redundant_term_tids[] = $tid;
}
}
}
if ($node_save_required) {
$node->ct_user_tags = $tags_and_terms;
node_save($node);
}
if (!empty($possible_redundant_term_tids)) {
_community_tags_cleanup_orphaned_tags_by_tids($possible_redundant_term_tids);
}
return;
}
function _community_tags_get_tag_result($type = 'global', $limit = NULL, $arg1 = NULL, $arg2 = NULL) {
$sql = '';
switch ($type) {
case 'node':
$arg1 = (int) $arg1;
if ($arg2) {
$arg2 = (int) $arg2;
$sql = "SELECT COUNT(t.tid) AS count, t.tid, t.name, t.vid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.nid = %d AND t.vid = %d GROUP BY t.tid, t.name, t.vid ORDER BY count DESC";
}
else {
$arg2 = NULL;
$sql = "SELECT COUNT(t.tid) AS count, t.tid, t.name, t.vid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.nid = %d GROUP BY t.tid, t.name, t.vid ORDER BY count DESC";
}
break;
case 'type':
$arg1 = (string) $arg1;
$arg2 = NULL;
$sql = "SELECT COUNT(t.tid) AS count, t.tid, t.name, t.vid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid INNER JOIN {node} n ON n.nid = c.nid WHERE n.type = '%s' GROUP BY t.tid, t.name, t.vid ORDER BY count DESC";
break;
case 'user':
$arg1 = (int) $arg1;
$arg2 = NULL;
$sql = "SELECT COUNT(t.tid) AS count, t.tid, t.name, t.vid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.uid = %d GROUP BY t.tid, t.name, t.vid ORDER BY count DESC";
break;
case 'user_node':
$arg1 = (int) $arg1;
$arg2 = (int) $arg2;
$sql = "SELECT COUNT(t.tid) AS count, t.tid, t.name, t.vid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.nid = %d AND c.uid = %d GROUP BY t.tid, t.name, t.vid ORDER BY count DESC";
default:
$sql = "SELECT COUNT(t.tid) AS count, t.tid, t.name, t.vid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid GROUP BY t.tid, t.name, t.vid ORDER BY count DESC";
}
if ($limit) {
$limit = (int) $limit;
return db_query_range($sql, $arg1, $arg2, 0, $limit);
}
else {
return db_query($sql, $arg1, $arg2);
}
}
function _community_tags_get_display_handlers() {
static $handlers;
if (!$handlers) {
$handlers = array(
'none' => array(
'id' => 'none',
'title' => t('None'),
'fn' => '_community_tags_display_handler_none',
),
'links' => array(
'id' => 'links',
'title' => t('Links'),
'fn' => '_community_tags_display_handler_links',
),
);
if (module_exists('tagadelic')) {
$handlers['tagadelic'] = array(
'id' => 'tagadelic',
'title' => t('Tagadelic'),
'fn' => '_community_tags_display_handler_tagadelic',
);
}
}
return $handlers;
}
function _community_tags_get_display_handler_options() {
$options = array();
foreach (_community_tags_get_display_handlers() as $key => $handler) {
$options[$key] = $handler['title'];
}
return $options;
}
function _community_tags_get_display_handler($vid, $content_type, $inline) {
$settings = _community_tags_get_settings($vid, $content_type);
$handlers = _community_tags_get_display_handlers();
return isset($handlers[$settings['display_handler']]) ? $handlers[$settings['display_handler']] : $handlers['links'];
}
function _community_tags_display_handler_none() {
return;
}
function _community_tags_display_handler_links($type = 'global', $limit = NULL, $arg1 = NULL, $arg2 = NULL) {
$result = _community_tags_get_tag_result($type, $limit, $arg1, $arg2);
$items = array();
while ($row = db_fetch_object($result)) {
$items[] = $row;
}
return theme('community_tags_links', $items);
}
function _community_tags_display_handler_tagadelic($type = 'global', $limit = NULL, $arg1 = NULL, $arg2 = NULL) {
$result = _community_tags_get_tag_result($type, $limit, $arg1, $arg2);
$weighted_tags = tagadelic_build_weighted_tags($result);
$sorted_tags = tagadelic_sort_tags($weighted_tags);
return theme('community_tags', $sorted_tags);
}
function community_tags_node_view($node, $inline = TRUE) {
global $user;
if (is_numeric($node)) {
$node = node_load($node);
}
if (!$node || !is_object($node)) {
return;
}
if (!$inline) {
drupal_set_title(check_plain($node->title));
}
module_load_include('inc', 'community_tags', 'community_tags.pages');
$output = '';
$vids = community_tags_vids_for_node($node);
foreach ($vids as $vid) {
$tags = community_tags_get_user_node_tags($user->uid, $node->nid, $vid);
$display_handler = _community_tags_get_display_handler($vid, $node->type, $inline);
$cloud = call_user_func($display_handler['fn'], 'node', NULL, $node->nid, $vid);
$names = array();
if (!count($tags)) {
$output .= drupal_get_form('community_tags_form', array(
'node' => $node,
'cloud' => $cloud,
'nid' => $node->nid,
'vid' => $vid,
'tags' => NULL,
'inline' => $inline,
'multiple' => count($vids),
));
}
elseif (user_access('edit own tags')) {
$names = community_tags_flatten($tags);
$tags = taxonomy_implode_tags($tags);
$output .= drupal_get_form('community_tags_form', array(
'node' => $node,
'cloud' => $cloud,
'nid' => $node->nid,
'vid' => $vid,
'tags' => $tags,
'inline' => $inline,
'multiple' => count($vids),
));
}
else {
$output .= '<p>' . t('You have already tagged this post. Your tags: ') . theme('community_tags', $tags) . '</p>';
}
drupal_add_js(array(
'communityTags' => array(
'n_' . $node->nid => array(
'v_' . $vid => array(
'tags' => $names,
'url' => url('community-tags/js/' . $node->nid . '/' . $vid),
'add' => t('Add'),
'token' => drupal_get_token('community_tags_form'),
),
),
),
), 'setting');
}
return $output;
}
function theme_community_tags($tags) {
return '<div class="cloud">' . (count($tags) ? theme('tagadelic_weighted', $tags) : t('None')) . '</div>';
}
function theme_community_tags_links($tags) {
foreach ($tags as $tag) {
$links[] = array(
'title' => $tag->name,
'href' => taxonomy_term_path($tag),
);
}
return theme('links', $links);
}
function _community_tags_menu_access() {
return user_access('access content') && user_access('tag content');
}
function _community_tags_tab_access($node) {
return _community_tags_is_tagging_view_visible($node, COMMUNITY_TAGS_MODE_TAB) && _community_tags_menu_access();
}
function community_tags_flatten($tags) {
$names = array();
foreach ($tags as $tag) {
$names[] = $tag->name;
}
return $names;
}
function community_tags_views_api() {
return array(
'api' => 2,
);
}
function _community_tags_node_insert($node) {
global $user;
$vids = community_tags_vids_for_node($node);
$processed_tags_and_terms = _community_tags_node_process_tags_and_terms($node->taxonomy, $vids);
$processed_terms = _community_tags_convert_new_tags_to_terms($processed_tags_and_terms);
foreach ($processed_terms as $vid => $terms) {
foreach ($terms as $tid => $term) {
_community_tags_add_tag($node->nid, $tid, $user->uid);
}
}
}
function _community_tags_node_update($node, $vids = NULL) {
global $user;
$vids = $vids ? $vids : community_tags_vids_for_node($node);
$processed_tags_and_terms = _community_tags_node_process_tags_and_terms($node->taxonomy, $vids);
$processed_terms = _community_tags_convert_new_tags_to_terms($processed_tags_and_terms);
$all_processed_terms = array();
foreach ($processed_terms as $vid => $terms) {
$all_processed_terms += $terms;
}
$existing_tags = _community_tags_get_node_tags($node->nid, $vids);
$new_tags = array_diff_key($all_processed_terms, $existing_tags);
$removed_tags = array_diff_key($existing_tags, $all_processed_terms);
$possible_redundant_term_tids = array();
foreach ($new_tags as $tid => $value) {
_community_tags_add_tag($node->nid, $tid, $user->uid);
}
foreach ($removed_tags as $tid => $value) {
$removed_node_term = $existing_tags[$tid];
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_SYNC, $removed_node_term->vid, $node->type)) {
_community_tags_delete_tags($node->nid, $tid);
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_DELETE_ORPHAN_TERMS, $removed_node_term->vid, $node->type)) {
$possible_redundant_term_tids[] = $tid;
}
}
else {
_community_tags_delete_tag($node->nid, $tid, $user->uid);
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_DELETE_ORPHAN_TERMS, $removed_node_term->vid, $node->type) && $removed_node_term->tag_count <= 1) {
$possible_redundant_term_tids[] = $tid;
}
}
}
_community_tags_cleanup_orphaned_tags_by_tids($possible_redundant_term_tids);
}
function _community_tags_node_delete($node) {
$existing_tags = _community_tags_get_node_tags($node->nid);
_community_tags_delete_tags_for_node($node->nid);
$possible_redundant_term_tids = array();
foreach ($existing_tags as $tid => $tag) {
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_DELETE_ORPHAN_TERMS, $tag->vid, $node->type) && $tag->tag_count <= 1) {
$possible_redundant_term_tids[] = $tid;
}
}
_community_tags_cleanup_orphaned_tags_by_tids($possible_redundant_term_tids);
}
function _community_tags_term_delete($term) {
_community_tags_delete_tags_for_term($term->tid);
}
function _community_tags_user_delete($user) {
$user_tags = _community_tags_get_user_tags($user->uid);
_community_tags_delete_tags_for_user($user->uid);
$node_terms_to_remove = array();
$possible_redundant_term_tids = array();
foreach ($user_tags as $ctag) {
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_SYNC, $ctag->vid, $ctag->type) && $ctag->tag_count <= 1) {
$node_terms_to_remove[$ctag->nid][$ctag->tid] = $ctag->name;
}
if (_community_tags_is_opmode(COMMUNITY_TAGS_OPMODE_DELETE_ORPHAN_TERMS, $ctag->vid, $ctag->type)) {
$possible_redundant_term_tids[] = $ctag->tid;
}
}
if (!empty($node_terms_to_remove)) {
foreach ($node_terms_to_remove as $nid => $terms_to_remove) {
$node = node_load($nid);
$node->taxonomy = array_diff_key($node->taxonomy, $terms_to_remove);
$node->ct_user_tags = array();
node_save($node);
}
}
if (!empty($possible_redundant_term_tids)) {
_community_tags_cleanup_orphaned_tags_by_tids($possible_redundant_term_tids);
}
}
function _community_tags_add_tag($nid, $tid, $uid) {
$time = time();
db_query('INSERT INTO {community_tags} (tid, nid, uid, date) VALUES (%d, %d, %d, %d)', $tid, $nid, $uid, $time);
}
function _community_tags_delete_tag($nid, $tid, $uid) {
db_query('DELETE FROM {community_tags} WHERE nid = %d AND tid = %d AND uid = %d', $nid, $tid, $uid);
}
function _community_tags_delete_tags($nid, $tid) {
db_query('DELETE FROM {community_tags} WHERE nid = %d AND tid = %d', $nid, $tid);
}
function _community_tags_delete_tags_for_node($nid) {
db_query('DELETE FROM {community_tags} WHERE nid = %d', $nid);
}
function _community_tags_delete_tags_for_term($tid) {
db_query('DELETE FROM {community_tags} WHERE tid = %d', $tid);
}
function _community_tags_delete_tags_for_user($uid) {
db_query('DELETE FROM {community_tags} WHERE uid = %d', $uid);
}
function _community_tags_cleanup_orphaned_tags_by_tids($tids) {
$count = 0;
if (!empty($tids)) {
$results = db_query("SELECT td.* FROM {term_data} td\n LEFT JOIN {term_hierarchy} th ON th.parent = td.tid\n LEFT JOIN {term_relation} tr ON tr.tid1 = td.tid OR tr.tid2 = td.tid\n LEFT JOIN {term_synonym} ts ON ts.tid = td.tid\n LEFT JOIN {term_node} tn ON tn.tid = td.tid\n LEFT JOIN {community_tags} ct ON ct.tid = td.tid\n WHERE td.tid IN (" . db_placeholders($tids) . ")\n AND tn.tid IS NULL\n AND ct.tid IS NULL\n AND th.parent IS NULL\n AND (tr.tid1 IS NULL AND tr.tid2 IS NULL)\n AND ts.tid IS NULL", $tids);
while ($row = db_fetch_object($results)) {
_community_tags_delete_redundant_term($row->tid);
}
}
return $count;
}
function _community_tags_delete_redundant_term($tid) {
taxonomy_del_term($tid);
}
function _community_tags_is_tagging_view_visible($node, $context) {
if ($node && variable_get('community_tags_display_' . $node->type, COMMUNITY_TAGS_MODE_TAB) == $context) {
$vids = community_tags_vids_for_node($node);
if (!empty($vids)) {
return TRUE;
}
}
}
function community_tags_vids_for_node($node) {
if (is_numeric($node)) {
$node = node_load($node);
}
return _community_tags_vids_for_node_type($node->type);
}
function _community_tags_vids_for_node_type($type) {
return _community_tags_vids($type);
}
function _community_tags_vids($type = NULL) {
$community_tagged = variable_get('community_tags_vocabularies', array());
if ($type) {
$result = db_query("SELECT vnt.vid FROM {vocabulary_node_types} vnt JOIN {vocabulary} v ON v.vid = vnt.vid AND v.tags = 1 WHERE vnt.type = '%s'", $type);
}
else {
$result = db_query("SELECT vnt.vid FROM {vocabulary_node_types} vnt JOIN {vocabulary} v ON v.vid = vnt.vid AND v.tags = 1");
}
$vids = array();
while ($vid = db_fetch_object($result)) {
if (isset($community_tagged[$vid->vid])) {
$vids[$vid->vid] = $vid->vid;
}
}
return $vids;
}
function _community_tags_is_opmode($modes, $vid, $content_type) {
$settings = _community_tags_get_settings($vid, $content_type);
if ($settings) {
return $settings['opmode'] & $modes;
}
return COMMUNITY_TAGS_OPMODE_SYNC & $modes;
}
function _community_tags_get_settings($vid = NULL, $content_type = NULL, $valid = FALSE) {
static $settings, $valid_settings;
$handlers = _community_tags_get_display_handlers();
$default_display_handler = isset($handlers['tagadelic']) ? 'tagadelic' : 'links';
if (!$settings) {
$valid_CT_vocabularies = variable_get('community_tags_vocabularies', array());
$result = db_query('SELECT v.vid, v.name, v.tags, nt.name type_name, nt.type
FROM {vocabulary} v
LEFT JOIN {vocabulary_node_types} vnt ON vnt.vid = v.vid
LEFT JOIN {node_type} nt ON nt.type = vnt.type
ORDER BY v.weight, v.name, nt.name');
$settings = array();
$valid_settings = array();
while ($row = db_fetch_object($result)) {
if (!isset($settings[$row->vid])) {
$settings[$row->vid] = array(
'name' => $row->name,
'tagging' => $row->tags,
'types' => array(),
);
$settings[$row->vid]['CT_enabled'] = isset($valid_CT_vocabularies[$row->vid]);
}
if (!empty($row->type)) {
if (isset($valid_CT_vocabularies[$row->vid]['types'][$row->type])) {
$settings[$row->vid]['types'][$row->type] = $valid_CT_vocabularies[$row->vid]['types'][$row->type];
$settings[$row->vid]['types'][$row->type]['type_name'] = $row->type_name;
$settings[$row->vid]['types'][$row->type]['assigned'] = TRUE;
}
else {
$settings[$row->vid]['types'][$row->type] = array(
'type_name' => $row->type_name,
'assigned' => TRUE,
'opmode' => COMMUNITY_TAGS_OPMODE_SYNC,
'display_handler' => $default_display_handler,
);
}
if ($settings[$row->vid]['tagging'] && isset($valid_CT_vocabularies[$row->vid])) {
$valid_settings[$row->vid] = $settings[$row->vid];
}
}
}
}
$rt = $valid ? $valid_settings : $settings;
if ($vid && $content_type) {
$return = !empty($rt[$vid]['types'][$content_type]) ? $rt[$vid]['types'][$content_type] : FALSE;
return $return;
}
elseif ($vid) {
return !empty($rt[$vid]) ? $rt[$vid] : FALSE;
}
else {
return $rt;
}
}
function _community_tags_get_node_tags($nid, $vids = NULL) {
$tags = array();
if ($vids) {
$args = $vids;
array_unshift($args, $nid);
$result = db_query("SELECT t.tid, t.vid, t.name, count(t.tid) tag_count FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.nid = %d AND t.vid IN (" . db_placeholders($vids, 'varchar') . ") GROUP BY t.tid", $args);
}
else {
$result = db_query("SELECT t.tid, t.vid, t.name, count(t.tid) tag_count FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.nid = %d GROUP BY t.tid", $nid);
}
while ($term = db_fetch_object($result)) {
$tags[$term->tid] = $term;
}
return $tags;
}
function _community_tags_get_node_user_vid_tags($nid, $uid, $vid) {
$tags = array();
$result = db_query("SELECT t.tid, t.name, count(ct2.uid) tag_count\n FROM (SELECT tid, nid FROM {community_tags} WHERE nid = %d AND uid = %d) AS ct\n INNER JOIN {term_data} t ON t.tid = ct.tid\n INNER JOIN {community_tags} ct2 ON ct2.tid = ct.tid AND ct2.nid = ct.nid\n WHERE t.vid = %d\n GROUP BY t.tid", $nid, $uid, $vid);
while ($term = db_fetch_object($result)) {
$tags[$term->tid] = $term;
}
return $tags;
}
function _community_tags_get_user_tags($uid) {
$tags = array();
$result = db_query("SELECT ct2.nid, t.tid, t.name, t.vid, n.type, count(ct2.uid) tag_count\n FROM (SELECT tid, nid FROM {community_tags} WHERE uid = %d) AS ct\n INNER JOIN {term_data} t ON t.tid = ct.tid\n INNER JOIN {node} n ON n.nid = ct.nid\n INNER JOIN {community_tags} ct2 ON ct2.tid = ct.tid AND ct2.nid = ct.nid\n GROUP BY ct2.nid, t.tid", $uid);
while ($term = db_fetch_object($result)) {
$tags[] = $term;
}
return $tags;
}
function _community_tags_check_user($user = NULL) {
if (!$user) {
if (!$GLOBALS['user'] && !variable_get('community_tags_allow_anonymous_attribution', 1)) {
return FALSE;
}
else {
$user = $GLOBALS['user'];
}
}
return $user;
}
function community_tags_get_user_node_tags($uid, $nid, $vid) {
$tags = array();
$result = db_query("SELECT t.tid, t.name, c.uid, c.nid FROM {term_data} t INNER JOIN {community_tags} c ON c.tid = t.tid WHERE c.nid = %d AND c.uid = %d AND t.vid = %d ORDER BY t.name", $nid, $uid, $vid);
while ($term = db_fetch_object($result)) {
$tags[$term->tid] = $term;
}
return $tags;
}
function _community_tags_node_process_tags_and_terms($tags_and_terms, $vids) {
$processed_terms = array();
if (is_array($tags_and_terms)) {
foreach ($tags_and_terms as $key => $term) {
if (!is_numeric($key) && $key == 'tags') {
foreach ($term as $vid => $vid_value) {
if (isset($vids[$vid])) {
$processed_terms[$vid] = array();
$vid_tags = is_array($vid_value) ? $vid_value : drupal_explode_tags($vid_value);
foreach ($vid_tags as $tag) {
$matching_terms = taxonomy_get_term_by_name($tag);
$matching_vocabulary_term = NULL;
foreach ($matching_terms as $matching_term) {
if ($matching_term->vid == $vid) {
$matching_vocabulary_term = $matching_term;
break;
}
}
if (!$matching_vocabulary_term) {
$processed_terms[$vid]['new tags'][] = $tag;
}
else {
$processed_terms[$vid]['terms'][$matching_vocabulary_term->tid] = $matching_vocabulary_term;
}
}
}
}
}
else {
if (is_array($term)) {
foreach ($term as $tid) {
if ($tid) {
$term_object = taxonomy_get_term($tid);
if ($term_object && isset($vids[$term_object->vid])) {
$processed_terms[$term_object->vid]['terms'][$tid] = $term_object;
}
}
}
}
else {
if ($term) {
$term_object = !is_object($term) ? taxonomy_get_term($term) : $term;
if ($term_object && isset($vids[$term_object->vid])) {
$processed_terms[$term_object->vid]['terms'][$term_object->tid] = $term_object;
}
}
}
}
}
}
return $processed_terms;
}
function _community_tags_convert_new_tags_to_terms($processed_tags_and_terms) {
$processed_terms = array();
foreach ($processed_tags_and_terms as $vid => $tags_and_terms) {
if (!empty($tags_and_terms['terms'])) {
$processed_terms[$vid] = $tags_and_terms['terms'];
}
else {
$processed_terms[$vid] = array();
}
if (!empty($tags_and_terms['new tags'])) {
foreach ($tags_and_terms['new tags'] as $tag_name) {
$edit = array(
'vid' => $vid,
'name' => $tag_name,
);
$status = taxonomy_save_term($edit);
$new_term = taxonomy_get_term($edit['tid']);
$processed_terms[$vid][$new_term->tid] = $new_term;
}
}
}
return $processed_terms;
}