<?php
function taxonomy_perm() {
return array('administer taxonomy');
}
function taxonomy_link($type, $node = NULL) {
if ($type == 'taxonomy terms' && $node != NULL) {
$links = array();
if (array_key_exists('taxonomy', $node)) {
foreach ($node->taxonomy as $term) {
$links['taxonomy_term_'. $term->tid] = array(
'title' => $term->name,
'href' => taxonomy_term_path($term),
'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
);
}
}
foreach (module_implements('link_alter') as $module) {
$function = $module .'_link_alter';
$function($node, $links);
}
return $links;
}
}
function taxonomy_term_path($term) {
$vocabulary = taxonomy_get_vocabulary($term->vid);
if ($vocabulary->module != 'taxonomy' && $path = module_invoke($vocabulary->module, 'term_path', $term)) {
return $path;
}
return 'taxonomy/term/'. $term->tid;
}
function taxonomy_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'admin/content/taxonomy',
'title' => t('Categories'),
'description' => t('Create vocabularies and terms to categorize your content.'),
'callback' => 'taxonomy_overview_vocabularies',
'access' => user_access('administer taxonomy'));
$items[] = array('path' => 'admin/content/taxonomy/list',
'title' => t('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10);
$items[] = array('path' => 'admin/content/taxonomy/add/vocabulary',
'title' => t('Add vocabulary'),
'callback' => 'drupal_get_form',
'callback arguments' => array('taxonomy_form_vocabulary'),
'access' => user_access('administer taxonomy'),
'type' => MENU_LOCAL_TASK);
$items[] = array('path' => 'admin/content/taxonomy/edit/vocabulary',
'title' => t('Edit vocabulary'),
'callback' => 'taxonomy_admin_vocabulary_edit',
'access' => user_access('administer taxonomy'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/content/taxonomy/edit/term',
'title' => t('Edit term'),
'callback' => 'taxonomy_admin_term_edit',
'access' => user_access('administer taxonomy'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'taxonomy/term',
'title' => t('Taxonomy term'),
'callback' => 'taxonomy_term_page',
'access' => user_access('access content'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'taxonomy/autocomplete',
'title' => t('Autocomplete taxonomy'),
'callback' => 'taxonomy_autocomplete',
'access' => user_access('access content'),
'type' => MENU_CALLBACK);
}
else {
if (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'taxonomy' && is_numeric(arg(3))) {
$vid = arg(3);
$items[] = array('path' => 'admin/content/taxonomy/'. $vid,
'title' => t('List terms'),
'callback' => 'taxonomy_overview_terms',
'callback arguments' => array($vid),
'access' => user_access('administer taxonomy'),
'type' => MENU_CALLBACK);
$items[] = array('path' => 'admin/content/taxonomy/'. $vid .'/list',
'title' => t('List'),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10);
$items[] = array('path' => 'admin/content/taxonomy/'. $vid .'/add/term',
'title' => t('Add term'),
'callback' => 'drupal_get_form',
'callback arguments' => array('taxonomy_form_term', $vid),
'access' => user_access('administer taxonomy'),
'type' => MENU_LOCAL_TASK);
}
}
return $items;
}
function taxonomy_overview_vocabularies() {
$vocabularies = taxonomy_get_vocabularies();
$rows = array();
foreach ($vocabularies as $vocabulary) {
$types = array();
foreach ($vocabulary->nodes as $type) {
$node_type = node_get_types('name', $type);
$types[] = $node_type ? check_plain($node_type) : check_plain($type);
}
$rows[] = array('name' => check_plain($vocabulary->name),
'type' => implode(', ', $types),
'edit' => l(t('edit vocabulary'), "admin/content/taxonomy/edit/vocabulary/$vocabulary->vid"),
'list' => l(t('list terms'), "admin/content/taxonomy/$vocabulary->vid"),
'add' => l(t('add terms'), "admin/content/taxonomy/$vocabulary->vid/add/term")
);
}
if (empty($rows)) {
$rows[] = array(array('data' => t('No categories available.'), 'colspan' => '5'));
}
$header = array(t('Name'), t('Type'), array('data' => t('Operations'), 'colspan' => '3'));
return theme('table', $header, $rows, array('id' => 'taxonomy'));
}
function taxonomy_overview_terms($vid) {
$destination = drupal_get_destination();
$header = array(t('Name'), t('Operations'));
$vocabulary = taxonomy_get_vocabulary($vid);
if (!$vocabulary) {
return drupal_not_found();
}
drupal_set_title(check_plain($vocabulary->name));
$start_from = $_GET['page'] ? $_GET['page'] : 0;
$total_entries = 0; $page_increment = 25; $displayed_count = 0;
if ($vocabulary->tags) {
$results = pager_query(db_rewrite_sql('SELECT t.*, h.parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $page_increment, 0, NULL, $vid);
while ($term = db_fetch_object($results)) {
$rows[] = array(
l($term->name, "taxonomy/term/$term->tid"),
l(t('edit'), "admin/content/taxonomy/edit/term/$term->tid", array(), $destination),
);
}
}
else {
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $term) {
$total_entries++; if (($start_from && ($start_from * $page_increment) >= $total_entries) || ($displayed_count == $page_increment)) {
continue;
}
$rows[] = array(str_repeat('--', $term->depth) .' '. l($term->name, "taxonomy/term/$term->tid"), l(t('edit'), "admin/content/taxonomy/edit/term/$term->tid", array(), $destination));
$displayed_count++; }
if (!$rows) {
$rows[] = array(array('data' => t('No terms available.'), 'colspan' => '2'));
}
$GLOBALS['pager_page_array'][] = $start_from; $GLOBALS['pager_total'][] = intval($total_entries / $page_increment) + 1; }
$output .= theme('table', $header, $rows, array('id' => 'taxonomy'));
if ($vocabulary->tags || $total_entries >= $page_increment) {
$output .= theme('pager', NULL, $page_increment);
}
return $output;
}
function taxonomy_form_vocabulary($edit = array()) {
$form['name'] = array('#type' => 'textfield',
'#title' => t('Vocabulary name'),
'#default_value' => $edit['name'],
'#maxlength' => 255,
'#description' => t('The name for this vocabulary. Example: "Topic".'),
'#required' => TRUE,
);
$form['description'] = array('#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $edit['description'],
'#description' => t('Description of the vocabulary; can be used by modules.'),
);
$form['help'] = array('#type' => 'textfield',
'#title' => t('Help text'),
'#maxlength' => 255,
'#default_value' => $edit['help'],
'#description' => t('Instructions to present to the user when choosing a term.'),
);
$form['nodes'] = array('#type' => 'checkboxes',
'#title' => t('Types'),
'#default_value' => $edit['nodes'],
'#options' => array_map('check_plain', node_get_types('names')),
'#description' => t('A list of node types you want to associate with this vocabulary.'),
'#required' => TRUE,
);
$form['hierarchy'] = array('#type' => 'radios',
'#title' => t('Hierarchy'),
'#default_value' => $edit['hierarchy'],
'#options' => array(t('Disabled'), t('Single'), t('Multiple')),
'#description' => t('Allows <a href="@help-url">a tree-like hierarchy</a> between terms of this vocabulary.', array('@help-url' => url('admin/help/taxonomy', NULL, NULL, 'hierarchy'))),
);
$form['relations'] = array('#type' => 'checkbox',
'#title' => t('Related terms'),
'#default_value' => $edit['relations'],
'#description' => t('Allows <a href="@help-url">related terms</a> in this vocabulary.', array('@help-url' => url('admin/help/taxonomy', NULL, NULL, 'related-terms'))),
);
$form['tags'] = array('#type' => 'checkbox',
'#title' => t('Free tagging'),
'#default_value' => $edit['tags'],
'#description' => t('Content is categorized by typing terms instead of choosing from a list.'),
);
$form['multiple'] = array('#type' => 'checkbox',
'#title' => t('Multiple select'),
'#default_value' => $edit['multiple'],
'#description' => t('Allows nodes to have more than one term from this vocabulary (always true for free tagging).'),
);
$form['required'] = array('#type' => 'checkbox',
'#title' => t('Required'),
'#default_value' => $edit['required'],
'#description' => t('If enabled, every node <strong>must</strong> have at least one term in this vocabulary.'),
);
$form['weight'] = array('#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $edit['weight'],
'#description' => t('In listings, the heavier vocabularies will sink and the lighter vocabularies will be positioned nearer the top.'),
);
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
if ($edit['vid']) {
$form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
$form['vid'] = array('#type' => 'value', '#value' => $edit['vid']);
$form['module'] = array('#type' => 'value', '#value' => $edit['module']);
}
return $form;
}
function taxonomy_form_vocabulary_submit($form_id, $form_values) {
$form_values['nodes'] = array_filter($form_values['nodes']);
switch (taxonomy_save_vocabulary($form_values)) {
case SAVED_NEW:
drupal_set_message(t('Created new vocabulary %name.', array('%name' => $form_values['name'])));
watchdog('taxonomy', t('Created new vocabulary %name.', array('%name' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/vocabulary/'. $form_values['vid']));
break;
case SAVED_UPDATED:
drupal_set_message(t('Updated vocabulary %name.', array('%name' => $form_values['name'])));
watchdog('taxonomy', t('Updated vocabulary %name.', array('%name' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/vocabulary/'. $form_values['vid']));
break;
}
return 'admin/content/taxonomy';
}
function taxonomy_save_vocabulary(&$edit) {
$edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
if ($edit['vid'] && $edit['name']) {
db_query("UPDATE {vocabulary} SET name = '%s', description = '%s', help = '%s', multiple = %d, required = %d, hierarchy = %d, relations = %d, tags = %d, weight = %d, module = '%s' WHERE vid = %d", $edit['name'], $edit['description'], $edit['help'], $edit['multiple'], $edit['required'], $edit['hierarchy'], $edit['relations'], $edit['tags'], $edit['weight'], isset($edit['module']) ? $edit['module'] : 'taxonomy', $edit['vid']);
db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']);
foreach ($edit['nodes'] as $type => $selected) {
db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
}
module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
$status = SAVED_UPDATED;
}
else if ($edit['vid']) {
$status = taxonomy_del_vocabulary($edit['vid']);
}
else {
$edit['vid'] = db_next_id('{vocabulary}_vid');
db_query("INSERT INTO {vocabulary} (vid, name, description, help, multiple, required, hierarchy, relations, tags, weight, module) VALUES (%d, '%s', '%s', '%s', %d, %d, %d, %d, %d, %d, '%s')", $edit['vid'], $edit['name'], $edit['description'], $edit['help'], $edit['multiple'], $edit['required'], $edit['hierarchy'], $edit['relations'], $edit['tags'], $edit['weight'], isset($edit['module']) ? $edit['module'] : 'taxonomy');
foreach ($edit['nodes'] as $type => $selected) {
db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
}
module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
$status = SAVED_NEW;
}
cache_clear_all();
return $status;
}
function taxonomy_del_vocabulary($vid) {
$vocabulary = (array) taxonomy_get_vocabulary($vid);
db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid);
db_query('DELETE FROM {vocabulary_node_types} WHERE vid = %d', $vid);
$result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid);
while ($term = db_fetch_object($result)) {
taxonomy_del_term($term->tid);
}
module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
cache_clear_all();
return SAVED_DELETED;
}
function taxonomy_vocabulary_confirm_delete($vid) {
$vocabulary = taxonomy_get_vocabulary($vid);
$form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
$form['vid'] = array('#type' => 'value', '#value' => $vid);
$form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
return confirm_form($form,
t('Are you sure you want to delete the vocabulary %title?',
array('%title' => $vocabulary->name)),
'admin/content/taxonomy',
t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'),
t('Delete'),
t('Cancel'));
}
function taxonomy_vocabulary_confirm_delete_submit($form_id, $form_values) {
$status = taxonomy_del_vocabulary($form_values['vid']);
drupal_set_message(t('Deleted vocabulary %name.', array('%name' => $form_values['name'])));
watchdog('taxonomy', t('Deleted vocabulary %name.', array('%name' => $form_values['name'])), WATCHDOG_NOTICE);
return 'admin/content/taxonomy';
}
function taxonomy_form_term($vocabulary_id, $edit = array()) {
$vocabulary = taxonomy_get_vocabulary($vocabulary_id);
drupal_set_title(check_plain($vocabulary->name));
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Term name'),
'#default_value' => $edit['name'],
'#maxlength' => 255,
'#description' => t('The name of this term.'),
'#required' => TRUE);
$form['description'] = array(
'#type' => 'textarea',
'#title' => t('Description'),
'#default_value' => $edit['description'],
'#description' => t('A description of the term.'));
if ($vocabulary->hierarchy) {
$parent = array_keys(taxonomy_get_parents($edit['tid']));
$children = taxonomy_get_tree($vocabulary_id, $edit['tid']);
foreach ($children as $child) {
$exclude[] = $child->tid;
}
$exclude[] = $edit['tid'];
if ($vocabulary->hierarchy == 1) {
$form['parent'] = _taxonomy_term_select(t('Parent'), 'parent', $parent, $vocabulary_id, l(t('Parent term'), 'admin/help/taxonomy', NULL, NULL, 'parent') .'.', 0, '<'. t('root') .'>', $exclude);
}
elseif ($vocabulary->hierarchy == 2) {
$form['parent'] = _taxonomy_term_select(t('Parents'), 'parent', $parent, $vocabulary_id, l(t('Parent terms'), 'admin/help/taxonomy', NULL, NULL, 'parent') .'.', 1, '<'. t('root') .'>', $exclude);
}
}
if ($vocabulary->relations) {
$form['relations'] = _taxonomy_term_select(t('Related terms'), 'relations', array_keys(taxonomy_get_related($edit['tid'])), $vocabulary_id, NULL, 1, '<'. t('none') .'>', array($edit['tid']));
}
$form['synonyms'] = array(
'#type' => 'textarea',
'#title' => t('Synonyms'),
'#default_value' => implode("\n", taxonomy_get_synonyms($edit['tid'])),
'#description' => t('<a href="@help-url">Synonyms</a> of this term, one synonym per line.', array('@help-url' => url('admin/help/taxonomy', NULL, NULL, 'synonyms'))));
$form['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $edit['weight'],
'#description' => t('In listings, the heavier terms will sink and the lighter terms will be positioned nearer the top.'));
$form['vid'] = array(
'#type' => 'value',
'#value' => $vocabulary->vid);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'));
if ($edit['tid']) {
$form['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'));
$form['tid'] = array(
'#type' => 'value',
'#value' => $edit['tid']);
}
else {
$form['destination'] = array('#type' => 'hidden', '#value' => $_GET['q']);
}
return $form;
}
function taxonomy_form_term_submit($form_id, $form_values) {
switch (taxonomy_save_term($form_values)) {
case SAVED_NEW:
drupal_set_message(t('Created new term %term.', array('%term' => $form_values['name'])));
watchdog('taxonomy', t('Created new term %term.', array('%term' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_values['tid']));
break;
case SAVED_UPDATED:
drupal_set_message(t('Updated term %term.', array('%term' => $form_values['name'])));
watchdog('taxonomy', t('Updated term %term.', array('%term' => $form_values['name'])), WATCHDOG_NOTICE, l(t('edit'), 'admin/content/taxonomy/edit/term/'. $form_values['tid']));
break;
}
return 'admin/content/taxonomy';
}
function taxonomy_save_term(&$form_values) {
if ($form_values['tid'] && $form_values['name']) {
db_query("UPDATE {term_data} SET name = '%s', description = '%s', weight = %d WHERE tid = %d", $form_values['name'], $form_values['description'], $form_values['weight'], $form_values['tid']);
$hook = 'update';
$status = SAVED_UPDATED;
}
else if ($form_values['tid']) {
return taxonomy_del_term($form_values['tid']);
}
else {
$form_values['tid'] = db_next_id('{term_data}_tid');
db_query("INSERT INTO {term_data} (tid, name, description, vid, weight) VALUES (%d, '%s', '%s', %d, %d)", $form_values['tid'], $form_values['name'], $form_values['description'], $form_values['vid'], $form_values['weight']);
$hook = 'insert';
$status = SAVED_NEW;
}
db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $form_values['tid'], $form_values['tid']);
if ($form_values['relations']) {
foreach ($form_values['relations'] as $related_id) {
if ($related_id != 0) {
db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $form_values['tid'], $related_id);
}
}
}
db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $form_values['tid']);
if (!isset($form_values['parent']) || empty($form_values['parent'])) {
$form_values['parent'] = array(0);
}
if (is_array($form_values['parent'])) {
foreach ($form_values['parent'] as $parent) {
if (is_array($parent)) {
foreach ($parent as $tid) {
db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $tid);
}
}
else {
db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $parent);
}
}
}
else {
db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $form_values['parent']);
}
db_query('DELETE FROM {term_synonym} WHERE tid = %d', $form_values['tid']);
if ($form_values['synonyms']) {
foreach (explode ("\n", str_replace("\r", '', $form_values['synonyms'])) as $synonym) {
if ($synonym) {
db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $form_values['tid'], chop($synonym));
}
}
}
if (isset($hook)) {
module_invoke_all('taxonomy', $hook, 'term', $form_values);
}
cache_clear_all();
return $status;
}
function taxonomy_del_term($tid) {
$tids = array($tid);
while ($tids) {
$children_tids = $orphans = array();
foreach ($tids as $tid) {
if ($children = taxonomy_get_children($tid)) {
foreach ($children as $child) {
$parents = taxonomy_get_parents($child->tid);
if (count($parents) == 1) {
$orphans[] = $child->tid;
}
}
}
$term = (array) taxonomy_get_term($tid);
db_query('DELETE FROM {term_data} WHERE tid = %d', $tid);
db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $tid);
db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $tid, $tid);
db_query('DELETE FROM {term_synonym} WHERE tid = %d', $tid);
db_query('DELETE FROM {term_node} WHERE tid = %d', $tid);
module_invoke_all('taxonomy', 'delete', 'term', $term);
}
$tids = $orphans;
}
cache_clear_all();
return SAVED_DELETED;
}
function taxonomy_term_confirm_delete($tid) {
$term = taxonomy_get_term($tid);
$form['type'] = array('#type' => 'value', '#value' => 'term');
$form['name'] = array('#type' => 'value', '#value' => $term->name);
$form['tid'] = array('#type' => 'value', '#value' => $tid);
return confirm_form($form,
t('Are you sure you want to delete the term %title?',
array('%title' => $term->name)),
'admin/content/taxonomy',
t('Deleting a term will delete all its children if there are any. This action cannot be undone.'),
t('Delete'),
t('Cancel'));
}
function taxonomy_term_confirm_delete_submit($form_id, $form_values) {
taxonomy_del_term($form_values['tid']);
drupal_set_message(t('Deleted term %name.', array('%name' => $form_values['name'])));
watchdog('taxonomy', t('Deleted term %name.', array('%name' => $form_values['name'])), WATCHDOG_NOTICE);
return 'admin/content/taxonomy';
}
function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
$vocabulary = taxonomy_get_vocabulary($vid);
$help = ($help) ? $help : filter_xss_admin($vocabulary->help);
if (!$vocabulary->multiple) {
$blank = ($vocabulary->required) ? t('- Please choose -') : t('- None selected -');
}
else {
$blank = ($vocabulary->required) ? 0 : t('- None -');
}
return _taxonomy_term_select(check_plain($vocabulary->name), $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
}
function taxonomy_form_all($free_tags = 0) {
$vocabularies = taxonomy_get_vocabularies();
$options = array();
foreach ($vocabularies as $vid => $vocabulary) {
if ($vocabulary->tags && !$free_tags) { continue; }
$tree = taxonomy_get_tree($vid);
if ($tree && (count($tree) > 0)) {
$options[$vocabulary->name] = array();
foreach ($tree as $term) {
$options[$vocabulary->name][$term->tid] = str_repeat('-', $term->depth) . $term->name;
}
}
}
return $options;
}
function taxonomy_get_vocabularies($type = NULL) {
if ($type) {
$result = db_query(db_rewrite_sql("SELECT v.vid, v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $type);
}
else {
$result = db_query(db_rewrite_sql('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid ORDER BY v.weight, v.name', 'v', 'vid'));
}
$vocabularies = array();
$node_types = array();
while ($voc = db_fetch_object($result)) {
$node_types[$voc->vid][] = $voc->type;
unset($voc->type);
$voc->nodes = $node_types[$voc->vid];
$vocabularies[$voc->vid] = $voc;
}
return $vocabularies;
}
function taxonomy_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
$node = $form['#node'];
if (!isset($node->taxonomy)) {
if ($node->nid) {
$terms = taxonomy_node_get_terms($node->nid);
}
else {
$terms = array();
}
}
else {
$terms = $node->taxonomy;
}
$c = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $node->type);
while ($vocabulary = db_fetch_object($c)) {
if ($vocabulary->tags) {
$typed_terms = array();
foreach ($terms as $term) {
if ($term->vid == $vocabulary->vid) {
if (strpos($term->name, ',') !== FALSE || strpos($term->name, '"') !== FALSE) {
$term->name = '"'.str_replace('"', '""', $term->name).'"';
}
$typed_terms[] = $term->name;
}
}
$typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
if ($vocabulary->help) {
$help = filter_xss_admin($vocabulary->help);
}
else {
$help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".');
}
$form['taxonomy']['tags'][$vocabulary->vid] = array('#type' => 'textfield',
'#title' => $vocabulary->name,
'#description' => $help,
'#required' => $vocabulary->required,
'#default_value' => $typed_string,
'#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
'#weight' => $vocabulary->weight,
'#maxlength' => 1024,
);
}
else {
$default_terms = array();
foreach ($terms as $term) {
if ($term->vid == $vocabulary->vid) {
$default_terms[$term->tid] = $term;
}
}
$form['taxonomy'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, array_keys($default_terms), filter_xss_admin($vocabulary->help));
$form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
$form['taxonomy'][$vocabulary->vid]['#required'] = $vocabulary->required;
}
}
if (is_array($form['taxonomy']) && !empty($form['taxonomy'])) {
if (count($form['taxonomy']) > 1) { $form['taxonomy'] += array(
'#type' => 'fieldset',
'#title' => t('Categories'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
}
$form['taxonomy']['#weight'] = -3;
$form['taxonomy']['#tree'] = TRUE;
}
}
}
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
$terms = array();
while ($term = db_fetch_object($result)) {
$terms[$term->$key] = $term;
}
return $terms;
}
function taxonomy_node_get_terms($nid, $key = 'tid') {
static $terms;
if (!isset($terms[$nid][$key])) {
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.nid = %d ORDER BY v.weight, t.weight, t.name', 't', 'tid'), $nid);
$terms[$nid][$key] = array();
while ($term = db_fetch_object($result)) {
$terms[$nid][$key][$term->$key] = $term;
}
}
return $terms[$nid][$key];
}
function taxonomy_node_validate(&$node) {
if ($node->taxonomy) {
$terms = $node->taxonomy;
if ($terms['tags']) {
foreach ($terms['tags'] as $vid => $vid_value) {
$vocabulary = taxonomy_get_vocabulary($vid);
if (!$vocabulary->tags) {
form_set_error("taxonomy][tags][$vid", t('The %name vocabulary can not be modified in this way.', array('%name' => $vocabulary->name)));
}
}
}
}
}
function taxonomy_node_save($nid, $terms) {
taxonomy_node_delete($nid);
if (isset($terms['tags'])) {
$typed_input = $terms['tags'];
unset($terms['tags']);
foreach ($typed_input as $vid => $vid_value) {
$regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
preg_match_all($regexp, $vid_value, $matches);
$typed_terms = array_unique($matches[1]);
$inserted = array();
foreach ($typed_terms as $typed_term) {
$typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term));
$typed_term = trim($typed_term);
if ($typed_term == "") { continue; }
$possibilities = taxonomy_get_term_by_name($typed_term);
$typed_term_tid = NULL; foreach ($possibilities as $possibility) {
if ($possibility->vid == $vid) {
$typed_term_tid = $possibility->tid;
}
}
if (!$typed_term_tid) {
$edit = array('vid' => $vid, 'name' => $typed_term);
$status = taxonomy_save_term($edit);
$typed_term_tid = $edit['tid'];
}
if (!isset($inserted[$typed_term_tid])) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $typed_term_tid);
$inserted[$typed_term_tid] = TRUE;
}
}
}
}
if (is_array($terms)) {
foreach ($terms as $term) {
if (is_array($term)) {
foreach ($term as $tid) {
if ($tid) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
}
}
}
else if (is_object($term)) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term->tid);
}
else if ($term) {
db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
}
}
}
}
function taxonomy_node_delete($nid) {
db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
}
function taxonomy_node_type($op, $info) {
if ($op == 'update' && !empty($info->old_type) && $info->type != $info->old_type) {
db_query("UPDATE {vocabulary_node_types} SET type = '%s' WHERE type = '%s'", $info->type, $info->old_type);
}
elseif ($op == 'delete') {
db_query("DELETE FROM {vocabulary_node_types} WHERE type = '%s'", $info->type);
}
}
function taxonomy_get_related($tid, $key = 'tid') {
if ($tid) {
$result = db_query('SELECT t.*, tid1, tid2 FROM {term_relation}, {term_data} t WHERE (t.tid = tid1 OR t.tid = tid2) AND (tid1 = %d OR tid2 = %d) AND t.tid != %d ORDER BY weight, name', $tid, $tid, $tid);
$related = array();
while ($term = db_fetch_object($result)) {
$related[$term->$key] = $term;
}
return $related;
}
else {
return array();
}
}
function taxonomy_get_parents($tid, $key = 'tid') {
if ($tid) {
$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.parent = t.tid WHERE h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid);
$parents = array();
while ($parent = db_fetch_object($result)) {
$parents[$parent->$key] = $parent;
}
return $parents;
}
else {
return array();
}
}
function taxonomy_get_parents_all($tid) {
$parents = array();
if ($tid) {
$parents[] = taxonomy_get_term($tid);
$n = 0;
while ($parent = taxonomy_get_parents($parents[$n]->tid)) {
$parents = array_merge($parents, $parent);
$n++;
}
}
return $parents;
}
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
if ($vid) {
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE t.vid = %d AND h.parent = %d ORDER BY weight, name', 't', 'tid'), $vid, $tid);
}
else {
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE parent = %d ORDER BY weight, name', 't', 'tid'), $tid);
}
$children = array();
while ($term = db_fetch_object($result)) {
$children[$term->$key] = $term;
}
return $children;
}
function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
static $children, $parents, $terms;
$depth++;
if (!isset($children[$vid])) {
$children[$vid] = array();
$result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
while ($term = db_fetch_object($result)) {
$children[$vid][$term->parent][] = $term->tid;
$parents[$vid][$term->tid][] = $term->parent;
$terms[$vid][$term->tid] = $term;
}
}
$max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
if ($children[$vid][$parent]) {
foreach ($children[$vid][$parent] as $child) {
if ($max_depth > $depth) {
$term = drupal_clone($terms[$vid][$child]);
$term->depth = $depth;
unset($term->parent);
$term->parents = $parents[$vid][$child];
$tree[] = $term;
if ($children[$vid][$child]) {
$tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
}
}
}
}
return $tree ? $tree : array();
}
function taxonomy_get_synonyms($tid) {
if ($tid) {
$result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid);
while ($synonym = db_fetch_array($result)) {
$synonyms[] = $synonym['name'];
}
return $synonyms ? $synonyms : array();
}
else {
return array();
}
}
function taxonomy_get_synonym_root($synonym) {
return db_fetch_object(db_query("SELECT * FROM {term_synonym} s, {term_data} t WHERE t.tid = s.tid AND s.name = '%s'", $synonym));
}
function taxonomy_term_count_nodes($tid, $type = 0) {
static $count;
if (!isset($count[$type])) {
if (is_numeric($type)) {
$result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid'));
}
else {
$result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
}
$count[$type] = array();
while ($term = db_fetch_object($result)) {
$count[$type][$term->tid] = $term->c;
}
}
foreach (_taxonomy_term_children($tid) as $c) {
$children_count += taxonomy_term_count_nodes($c, $type);
}
return $count[$type][$tid] + $children_count;
}
function _taxonomy_term_children($tid) {
static $children;
if (!isset($children)) {
$result = db_query('SELECT tid, parent FROM {term_hierarchy}');
while ($term = db_fetch_object($result)) {
$children[$term->parent][] = $term->tid;
}
}
return $children[$tid] ? $children[$tid] : array();
}
function taxonomy_get_term_by_name($name) {
$db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER('%s') = LOWER(t.name)", 't', 'tid'), trim($name));
$result = array();
while ($term = db_fetch_object($db_result)) {
$result[] = $term;
}
return $result;
}
function taxonomy_get_vocabulary($vid) {
static $vocabularies = array();
if (!array_key_exists($vid, $vocabularies)) {
$result = db_query('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE v.vid = %d ORDER BY v.weight, v.name', $vid);
$node_types = array();
while ($voc = db_fetch_object($result)) {
$node_types[] = $voc->type;
unset($voc->type);
$voc->nodes = $node_types;
$vocabularies[$vid] = $voc;
}
}
return $vocabularies[$vid];
}
function taxonomy_get_term($tid) {
static $terms = array();
if (!isset($terms[$tid])) {
$terms[$tid] = db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid));
}
return $terms[$tid];
}
function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
$tree = taxonomy_get_tree($vocabulary_id);
$options = array();
if ($blank) {
$options[''] = $blank;
}
if ($tree) {
foreach ($tree as $term) {
if (!in_array($term->tid, $exclude)) {
$choice = new stdClass();
$choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
$options[] = $choice;
}
}
}
return array('#type' => 'select',
'#title' => $title,
'#default_value' => $value,
'#options' => $options,
'#description' => $description,
'#multiple' => $multiple,
'#size' => $multiple ? min(9, count($options)) : 0,
'#weight' => -15,
'#theme' => 'taxonomy_term_select',
);
}
function theme_taxonomy_term_select($element) {
return theme('select', $element);
}
function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC') {
if (count($tids) > 0) {
$descendant_tids = array();
if ($depth === 'all') {
$depth = NULL;
}
foreach ($tids as $index => $tid) {
$term = taxonomy_get_term($tid);
$tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
$descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
}
if ($operator == 'or') {
$args = call_user_func_array('array_merge', $descendant_tids);
$placeholders = implode(',', array_fill(0, count($args), '%d'));
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1 ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1';
}
else {
$joins = '';
$wheres = '';
$args = array();
foreach ($descendant_tids as $index => $tids) {
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
$placeholders = implode(',', array_fill(0, count($tids), '%d'));
$wheres .= ' AND tn'. $index .'.tid IN ('. $placeholders .')';
$args = array_merge($args, $tids);
}
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY '. $order;
$sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres;
}
$sql = db_rewrite_sql($sql);
$sql_count = db_rewrite_sql($sql_count);
if ($pager) {
$result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args);
}
else {
$result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10));
}
}
return $result;
}
function taxonomy_render_nodes($result) {
$output = '';
if (db_num_rows($result) > 0) {
while ($node = db_fetch_object($result)) {
$output .= node_view(node_load($node->nid), 1);
}
$output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
}
else {
$output .= '<p>'. t('There are currently no posts in this category.') .'</p>';
}
return $output;
}
function taxonomy_nodeapi($node, $op, $arg = 0) {
switch ($op) {
case 'load':
$output['taxonomy'] = taxonomy_node_get_terms($node->nid);
return $output;
case 'insert':
taxonomy_node_save($node->nid, $node->taxonomy);
break;
case 'update':
taxonomy_node_save($node->nid, $node->taxonomy);
break;
case 'delete':
taxonomy_node_delete($node->nid);
break;
case 'validate':
taxonomy_node_validate($node);
break;
case 'rss item':
return taxonomy_rss_item($node);
case 'update index':
return taxonomy_node_update_index($node);
}
}
function taxonomy_node_update_index(&$node) {
$output = array();
foreach ($node->taxonomy as $term) {
$output[] = $term->name;
}
if (count($output)) {
return '<strong>('. implode(', ', $output) .')</strong>';
}
}
function taxonomy_terms_parse_string($str_tids) {
$terms = array();
if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_tids)) {
$terms['operator'] = 'or';
$terms['tids'] = preg_split('/[+ ]/', $str_tids);
}
else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str_tids)) {
$terms['operator'] = 'and';
$terms['tids'] = explode(',', $str_tids);
}
return $terms;
}
function taxonomy_term_page($str_tids = '', $depth = 0, $op = 'page') {
$terms = taxonomy_terms_parse_string($str_tids);
if ($terms['operator'] != 'and' && $terms['operator'] != 'or') {
drupal_not_found();
}
if ($terms['tids']) {
$placeholders = implode(',', array_fill(0, count($terms['tids']), '%d'));
$result = db_query(db_rewrite_sql('SELECT t.tid, t.name FROM {term_data} t WHERE t.tid IN ('. $placeholders .')', 't', 'tid'), $terms['tids']);
$tids = array(); $names = array();
while ($term = db_fetch_object($result)) {
$tids[] = $term->tid;
$names[] = $term->name;
}
if ($names) {
$title = check_plain(implode(', ', $names));
drupal_set_title($title);
switch ($op) {
case 'page':
$current->tid = $tids[0];
$breadcrumbs = array(array('path' => $_GET['q'], 'title' => $names[0]));
while ($parents = taxonomy_get_parents($current->tid)) {
$current = array_shift($parents);
$breadcrumbs[] = array('path' => 'taxonomy/term/'. $current->tid, 'title' => $current->name);
}
$breadcrumbs = array_reverse($breadcrumbs);
menu_set_location($breadcrumbs);
$output = taxonomy_render_nodes(taxonomy_select_nodes($tids, $terms['operator'], $depth, TRUE));
drupal_add_feed(url('taxonomy/term/'. $str_tids .'/'. $depth .'/feed'), 'RSS - '. $title);
return $output;
break;
case 'feed':
$term = taxonomy_get_term($tids[0]);
$channel['link'] = url('taxonomy/term/'. $str_tids .'/'. $depth, NULL, NULL, TRUE);
$channel['title'] = variable_get('site_name', 'Drupal') .' - '. $title;
$channel['description'] = $term->description;
$result = taxonomy_select_nodes($tids, $terms['operator'], $depth, FALSE);
node_feed($result, $channel);
break;
default:
drupal_not_found();
}
}
else {
drupal_not_found();
}
}
}
function taxonomy_admin_vocabulary_edit($vid = NULL) {
if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
return drupal_get_form('taxonomy_vocabulary_confirm_delete', $vid);
}
if ($vocabulary = (array)taxonomy_get_vocabulary($vid)) {
return drupal_get_form('taxonomy_form_vocabulary', $vocabulary);
}
return drupal_not_found();
}
function taxonomy_admin_term_edit($tid) {
if ($_POST['op'] == t('Delete') || $_POST['confirm']) {
return drupal_get_form('taxonomy_term_confirm_delete', $tid);
}
if ($term = (array)taxonomy_get_term($tid)) {
return drupal_get_form('taxonomy_form_term', $term['vid'], $term);
}
return drupal_not_found();
}
function taxonomy_rss_item($node) {
$output = array();
foreach ($node->taxonomy as $term) {
$output[] = array('key' => 'category',
'value' => check_plain($term->name),
'attributes' => array('domain' => url('taxonomy/term/'. $term->tid, NULL, NULL, TRUE)));
}
return $output;
}
function taxonomy_help($section) {
switch ($section) {
case 'admin/help#taxonomy':
$output = '<p>'. t('The taxonomy module is one of the most popular features because users often want to create categories to organize content by type. A simple example would be organizing a list of music reviews by musical genre.') .'</p>';
$output .= '<p>'. t('Taxonomy is the study of classification. The taxonomy module allows you to define vocabularies (sets of categories) which are used to classify content. The module supports hierarchical classification and association between terms, allowing for truly flexible information retrieval and classification. The taxonomy module allows multiple lists of categories for classification (controlled vocabularies) and offers the possibility of creating thesauri (controlled vocabularies that indicate the relationship of terms) and taxonomies (controlled vocabularies where relationships are indicated hierarchically). To view and manage the terms of each vocabulary, click on the associated <em>list terms</em> link. To delete a vocabulary and all its terms, choose <em>edit vocabulary.</em>') .'</p>';
$output .= '<p>'. t('A controlled vocabulary is a set of terms to use for describing content (known as descriptors in indexing lingo). Drupal allows you to describe each piece of content (blog, story, etc.) using one or many of these terms. For simple implementations, you might create a set of categories without subcategories, similar to Slashdot\'s sections. For more complex implementations, you might create a hierarchical list of categories.') .'</p>';
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="@taxonomy">Taxonomy page</a>.', array('@taxonomy' => 'http://drupal.org/handbook/modules/taxonomy/')) .'</p>';
return $output;
case 'admin/content/taxonomy':
return '<p>'. t('The taxonomy module allows you to classify content into categories and subcategories; it allows multiple lists of categories for classification (controlled vocabularies) and offers the possibility of creating thesauri (controlled vocabularies that indicate the relationship of terms), taxonomies (controlled vocabularies where relationships are indicated hierarchically), and free vocabularies where terms, or tags, are defined during content creation. To view and manage the terms of each vocabulary, click on the associated <em>list terms</em> link. To delete a vocabulary and all its terms, choose "edit vocabulary".') .'</p>';
case 'admin/content/taxonomy/add/vocabulary':
return '<p>'. t("When you create a controlled vocabulary you are creating a set of terms to use for describing content (known as descriptors in indexing lingo). Drupal allows you to describe each piece of content (blog, story, etc.) using one or many of these terms. For simple implementations, you might create a set of categories without subcategories. For more complex implementations, you might create a hierarchical list of categories.") .'</p>';
}
}
function _taxonomy_get_tid_from_term($term) {
return $term->tid;
}
function taxonomy_autocomplete($vid, $string = '') {
$regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
preg_match_all($regexp, $string, $matches);
$array = $matches[1];
$last_string = trim(array_pop($array));
if ($last_string != '') {
$result = db_query_range(db_rewrite_sql("SELECT t.tid, t.name FROM {term_data} t WHERE t.vid = %d AND LOWER(t.name) LIKE LOWER('%%%s%%')", 't', 'tid'), $vid, $last_string, 0, 10);
$prefix = count($array) ? implode(', ', $array) .', ' : '';
$matches = array();
while ($tag = db_fetch_object($result)) {
$n = $tag->name;
if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) {
$n = '"'. str_replace('"', '""', $tag->name) .'"';
}
$matches[$prefix . $n] = check_plain($tag->name);
}
print drupal_to_js($matches);
exit();
}
}