taxonomy.module 52.5 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
Kjartan's avatar
Kjartan committed
2
// $Id$
Dries's avatar
 
Dries committed
3

Dries's avatar
 
Dries committed
4 5 6 7 8
/**
 * @file
 * Enables the organization of content into categories.
 */

Dries's avatar
 
Dries committed
9 10 11
/**
 * Implementation of hook_perm().
 */
Kjartan's avatar
Kjartan committed
12
function taxonomy_perm() {
Dries's avatar
 
Dries committed
13
  return array('administer taxonomy');
Kjartan's avatar
Kjartan committed
14
}
Dries's avatar
 
Dries committed
15

Dries's avatar
 
Dries committed
16 17 18 19 20 21 22
/**
 * Implementation of hook_link().
 *
 * This hook is extended with $type = 'taxonomy terms' to allow themes to
 * print lists of terms associated with a node. Themes can print taxonomy
 * links with:
 *
23
 * if (module_exists('taxonomy')) {
24 25
 *   $terms = taxonomy_link('taxonomy terms', $node);
 *   print theme('links', $terms);
Dries's avatar
 
Dries committed
26 27
 * }
 */
Dries's avatar
 
Dries committed
28
function taxonomy_link($type, $node = NULL) {
Dries's avatar
 
Dries committed
29
  if ($type == 'taxonomy terms' && $node != NULL) {
Kjartan's avatar
 
Kjartan committed
30
    $links = array();
Dries's avatar
 
Dries committed
31
    if (array_key_exists('taxonomy', $node)) {
32
      foreach ($node->taxonomy as $term) {
33
        $links['taxonomy_term_'. $term->tid] = array(
34 35 36
          'title' => $term->name,
          'href' => taxonomy_term_path($term),
          'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description))
37
        );
Dries's avatar
 
Dries committed
38
      }
Dries's avatar
 
Dries committed
39
    }
Kjartan's avatar
Kjartan committed
40

41
    // We call this hook again because some modules and themes call taxonomy_link('taxonomy terms') directly
42
    foreach (module_implements('link_alter') as $module) {
43 44 45 46 47
      $function = $module .'_link_alter';
      $function($node, $links);
    }

    return $links;
48 49 50
  }
}

51 52 53 54 55 56 57 58 59 60
/**
 * For vocabularies not maintained by taxonomy.module, give the maintaining
 * module a chance to provide a path for terms in that vocabulary.
 *
 * @param $term
 *   A term object.
 * @return
 *   An internal Drupal path.
 */

61 62 63 64 65 66 67 68
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;
}

Dries's avatar
 
Dries committed
69 70 71
/**
 * Implementation of hook_menu().
 */
Dries's avatar
 
Dries committed
72
function taxonomy_menu($may_cache) {
Dries's avatar
 
Dries committed
73
  $items = array();
Dries's avatar
 
Dries committed
74

Dries's avatar
 
Dries committed
75
  if ($may_cache) {
76
    $items[] = array('path' => 'admin/content/taxonomy',
77
      'title' => t('Categories'),
78
      'description' => t('Create vocabularies and terms to categorize your content.'),
79
      'callback' => 'taxonomy_overview_vocabularies',
Dries's avatar
 
Dries committed
80
      'access' => user_access('administer taxonomy'));
81

82
    $items[] = array('path' => 'admin/content/taxonomy/list',
83
      'title' => t('List'),
84 85
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => -10);
86

87
    $items[] = array('path' => 'admin/content/taxonomy/add/vocabulary',
88
      'title' => t('Add vocabulary'),
89 90
      'callback' => 'drupal_get_form',
      'callback arguments' => array('taxonomy_form_vocabulary'),
Dries's avatar
 
Dries committed
91 92 93
      'access' => user_access('administer taxonomy'),
      'type' => MENU_LOCAL_TASK);

94
    $items[] = array('path' => 'admin/content/taxonomy/edit/vocabulary',
95
      'title' => t('Edit vocabulary'),
96
      'callback' => 'taxonomy_admin_vocabulary_edit',
97 98 99
      'access' => user_access('administer taxonomy'),
      'type' => MENU_CALLBACK);

100
    $items[] = array('path' => 'admin/content/taxonomy/edit/term',
101
      'title' => t('Edit term'),
102
      'callback' => 'taxonomy_admin_term_edit',
103 104 105
      'access' => user_access('administer taxonomy'),
      'type' => MENU_CALLBACK);

106
    $items[] = array('path' => 'taxonomy/term',
107
      'title' => t('Taxonomy term'),
Dries's avatar
 
Dries committed
108 109 110
      'callback' => 'taxonomy_term_page',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK);
Steven Wittens's avatar
Steven Wittens committed
111

112
    $items[] = array('path' => 'taxonomy/autocomplete',
113
      'title' => t('Autocomplete taxonomy'),
Steven Wittens's avatar
Steven Wittens committed
114 115 116
      'callback' => 'taxonomy_autocomplete',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK);
Dries's avatar
 
Dries committed
117
  }
Dries's avatar
Dries committed
118
  else {
119
    if (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'taxonomy' && is_numeric(arg(3))) {
120 121
      $vid = arg(3);
      $items[] = array('path' => 'admin/content/taxonomy/'. $vid,
122
        'title' => t('List terms'),
123
        'callback' => 'taxonomy_overview_terms',
124
        'callback arguments' => array($vid),
Dries's avatar
Dries committed
125 126 127
        'access' => user_access('administer taxonomy'),
        'type' => MENU_CALLBACK);

128
      $items[] = array('path' => 'admin/content/taxonomy/'. $vid .'/list',
129
        'title' => t('List'),
130 131
        'type' => MENU_DEFAULT_LOCAL_TASK,
        'weight' => -10);
Dries's avatar
Dries committed
132

133
      $items[] = array('path' => 'admin/content/taxonomy/'. $vid .'/add/term',
134
        'title' => t('Add term'),
135 136
        'callback' => 'drupal_get_form',
        'callback arguments' => array('taxonomy_form_term', $vid),
Dries's avatar
Dries committed
137 138 139 140
        'access' => user_access('administer taxonomy'),
        'type' => MENU_LOCAL_TASK);
    }
  }
Dries's avatar
 
Dries committed
141

Dries's avatar
 
Dries committed
142 143
  return $items;
}
Dries's avatar
 
Dries committed
144

145 146 147 148
/**
 * List and manage vocabularies.
 */
function taxonomy_overview_vocabularies() {
149
  $vocabularies = taxonomy_get_vocabularies();
150
  $rows = array();
151 152 153
  foreach ($vocabularies as $vocabulary) {
    $types = array();
    foreach ($vocabulary->nodes as $type) {
154
      $node_type = node_get_types('name', $type);
155 156
      $types[] = $node_type ? $node_type : $type;
    }
157 158
    $rows[] = array('name' => check_plain($vocabulary->name),
      'type' => implode(', ', $types),
159 160 161
      '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")
162 163 164
    );
  }
  if (empty($rows)) {
Steven Wittens's avatar
Steven Wittens committed
165
    $rows[] = array(array('data' => t('No categories available.'), 'colspan' => '5'));
166
  }
drumm's avatar
drumm committed
167
  $header = array(t('Name'), t('Type'), array('data' => t('Operations'), 'colspan' => '3'));
168

169
  return theme('table', $header, $rows, array('id' => 'taxonomy'));
170 171 172 173 174 175 176 177 178 179 180
}

/**
 * Display a tree of all the terms in a vocabulary, with options to edit
 * each one.
 */
function taxonomy_overview_terms($vid) {
  $destination = drupal_get_destination();

  $header = array(t('Name'), t('Operations'));
  $vocabulary = taxonomy_get_vocabulary($vid);
181 182 183
  if (!$vocabulary) {
    return drupal_not_found();
  }
184 185 186 187 188 189 190 191 192 193

  drupal_set_title(check_plain($vocabulary->name));
  $start_from      = $_GET['page'] ? $_GET['page'] : 0;
  $total_entries   = 0;  // total count for pager
  $page_increment  = 25; // number of tids per page
  $displayed_count = 0;  // number of tids shown

  $tree = taxonomy_get_tree($vocabulary->vid);
  foreach ($tree as $term) {
    $total_entries++; // we're counting all-totals, not displayed
194 195 196
    if (($start_from && ($start_from * $page_increment) >= $total_entries) || ($displayed_count == $page_increment)) {
      continue;
    }
197
    $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));
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    $displayed_count++; // we're counting tids displayed
  }

  if (!$rows) {
    $rows[] = array(array('data' => t('No terms available.'), 'colspan' => '2'));
  }

  $GLOBALS['pager_page_array'][] = $start_from;  // FIXME
  $GLOBALS['pager_total'][] = intval($total_entries / $page_increment) + 1; // FIXME

  if ($total_entries >= $page_increment) {
    $rows[] = array(array('data' => theme('pager', NULL, $page_increment), 'colspan' => '2'));
  }

  return theme('table', $header, $rows, array('id' => 'taxonomy'));
}

/**
 * Display form for adding and editing vocabularies.
 */
Kjartan's avatar
Kjartan committed
218
function taxonomy_form_vocabulary($edit = array()) {
219 220 221 222
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Vocabulary name'),
    '#default_value' => $edit['name'],
    '#maxlength' => 64,
223
    '#description' => t('The name for this vocabulary. Example: "Topic".'),
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
    '#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'),
    '#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'],
239
    '#options' => node_get_types('names'),
240 241 242 243 244 245 246
    '#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')),
247
    '#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'))),
248 249 250 251
  );
  $form['relations'] = array('#type' => 'checkbox',
    '#title' => t('Related terms'),
    '#default_value' => $edit['relations'],
252
    '#description' => t('Allows <a href="@help-url">related terms</a> in this vocabulary.', array('@help-url' => url('admin/help/taxonomy', NULL, NULL, 'related-terms'))),
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  );
  $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.'),
  );
274

275
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
Dries's avatar
 
Dries committed
276
  if ($edit['vid']) {
277
    $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
278
    $form['vid'] = array('#type' => 'value', '#value' => $edit['vid']);
279
    $form['module'] = array('#type' => 'value', '#value' => $edit['module']);
Dries's avatar
 
Dries committed
280
  }
281
  return $form;
Kjartan's avatar
Kjartan committed
282
}
Kjartan's avatar
Kjartan committed
283

284 285 286 287 288 289 290
/**
 * Accept the form submission for a vocabulary and save the results.
 */
function taxonomy_form_vocabulary_submit($form_id, $form_values) {
  // Fix up the nodes array to remove unchecked nodes.
  $form_values['nodes'] = array_filter($form_values['nodes']);
  switch (taxonomy_save_vocabulary($form_values)) {
291 292 293 294 295 296
    case SAVED_NEW:
      drupal_set_message(t('Created new vocabulary %name.', array('%name' => $form_values['name'])));
      break;
    case SAVED_UPDATED:
      drupal_set_message(t('Updated vocabulary %name.', array('%name' => $form_values['name'])));
      break;
297
  }
298
  return 'admin/content/taxonomy';
299 300
}

301
function taxonomy_save_vocabulary(&$edit) {
302
  $edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
Dries's avatar
 
Dries committed
303

Dries's avatar
 
Dries committed
304
  if ($edit['vid'] && $edit['name']) {
305
    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']);
Dries's avatar
 
Dries committed
306
    db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']);
307
    foreach ($edit['nodes'] as $type => $selected) {
Dries's avatar
 
Dries committed
308 309
      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
    }
Dries's avatar
 
Dries committed
310
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
311
    $status = SAVED_UPDATED;
Dries's avatar
 
Dries committed
312
  }
Dries's avatar
 
Dries committed
313
  else if ($edit['vid']) {
314
    $status = taxonomy_del_vocabulary($edit['vid']);
Kjartan's avatar
Kjartan committed
315 316
  }
  else {
317 318
    $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');
319
    foreach ($edit['nodes'] as $type => $selected) {
Dries's avatar
 
Dries committed
320 321
      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
    }
Dries's avatar
 
Dries committed
322
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
323
    $status = SAVED_NEW;
Kjartan's avatar
Kjartan committed
324
  }
Dries's avatar
 
Dries committed
325 326

  cache_clear_all();
Dries's avatar
 
Dries committed
327

328
  return $status;
Kjartan's avatar
Kjartan committed
329
}
Dries's avatar
 
Dries committed
330

331 332 333 334 335 336 337 338
/**
 * Delete a vocabulary.
 *
 * @param $vid
 *   A vocabulary ID.
 * @return
 *   Constant indicating items were deleted.
 */
Kjartan's avatar
Kjartan committed
339
function taxonomy_del_vocabulary($vid) {
340
  $vocabulary = (array) taxonomy_get_vocabulary($vid);
Dries's avatar
 
Dries committed
341

Dries's avatar
 
Dries committed
342
  db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid);
Dries's avatar
 
Dries committed
343
  db_query('DELETE FROM {vocabulary_node_types} WHERE vid = %d', $vid);
Dries's avatar
 
Dries committed
344
  $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid);
Kjartan's avatar
Kjartan committed
345 346
  while ($term = db_fetch_object($result)) {
    taxonomy_del_term($term->tid);
Dries's avatar
 
Dries committed
347
  }
Dries's avatar
 
Dries committed
348

Dries's avatar
 
Dries committed
349
  module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
Dries's avatar
 
Dries committed
350

Dries's avatar
 
Dries committed
351 352
  cache_clear_all();

353
  return SAVED_DELETED;
Dries's avatar
 
Dries committed
354 355
}

356
function taxonomy_vocabulary_confirm_delete($vid) {
Dries's avatar
 
Dries committed
357 358
  $vocabulary = taxonomy_get_vocabulary($vid);

359 360 361
  $form['type'] = array('#type' => 'value', '#value' => 'vocabulary');
  $form['vid'] = array('#type' => 'value', '#value' => $vid);
  $form['name'] = array('#type' => 'value', '#value' => $vocabulary->name);
362
  return confirm_form($form,
363
                  t('Are you sure you want to delete the vocabulary %title?',
364
                  array('%title' => $vocabulary->name)),
365 366
                  'admin/content/taxonomy',
                  t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'),
367
                  t('Delete'),
368
                  t('Cancel'));
Kjartan's avatar
Kjartan committed
369
}
Dries's avatar
 
Dries committed
370

371 372
function taxonomy_vocabulary_confirm_delete_submit($form_id, $form_values) {
  $status = taxonomy_del_vocabulary($form_values['vid']);
373
  drupal_set_message(t('Deleted vocabulary %name.', array('%name' => $form_values['name'])));
374
  return 'admin/content/taxonomy';
375 376
}

377
function taxonomy_form_term($vocabulary_id, $edit = array()) {
Kjartan's avatar
Kjartan committed
378
  $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
Dries's avatar
 
Dries committed
379

380 381 382 383 384 385 386
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Term name'),
    '#default_value' => $edit['name'],
    '#maxlength' => 64,
    '#description' => t('The name of this term.'),
    '#required' => TRUE);
387

388 389 390 391 392
  $form['description'] = array(
    '#type' => 'textarea',
    '#title' => t('Description'),
    '#default_value' => $edit['description'],
    '#description' => t('A description of the term.'));
Dries's avatar
 
Dries committed
393

Kjartan's avatar
Kjartan committed
394
  if ($vocabulary->hierarchy) {
Dries's avatar
 
Dries committed
395 396
    $parent = array_keys(taxonomy_get_parents($edit['tid']));
    $children = taxonomy_get_tree($vocabulary_id, $edit['tid']);
Dries's avatar
 
Dries committed
397

Dries's avatar
 
Dries committed
398
    // A term can't be the child of itself, nor of its children.
Dries's avatar
 
Dries committed
399 400 401
    foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
Dries's avatar
 
Dries committed
402
    $exclude[] = $edit['tid'];
Dries's avatar
 
Dries committed
403

Kjartan's avatar
Kjartan committed
404
    if ($vocabulary->hierarchy == 1) {
405
      $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);
Dries's avatar
 
Dries committed
406
    }
Kjartan's avatar
Kjartan committed
407
    elseif ($vocabulary->hierarchy == 2) {
408
      $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);
Dries's avatar
 
Dries committed
409
    }
Kjartan's avatar
Kjartan committed
410
  }
Dries's avatar
 
Dries committed
411

412
  if ($vocabulary->relations) {
413
    $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']));
414 415
  }

416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  $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'));
Kjartan's avatar
Kjartan committed
432

Dries's avatar
 
Dries committed
433
  if ($edit['tid']) {
434 435 436 437 438 439
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'));
    $form['tid'] = array(
      '#type' => 'value',
      '#value' => $edit['tid']);
Dries's avatar
 
Dries committed
440
  }
441
  else {
442
    $form['destination'] = array('#type' => 'hidden', '#value' => $_GET['q']);
443
  }
Dries's avatar
 
Dries committed
444

445
  return $form;
Kjartan's avatar
Kjartan committed
446
}
Dries's avatar
 
Dries committed
447

448 449 450 451 452 453
/**
 * Accept the form submission for a taxonomy term and save the result.
 */
function taxonomy_form_term_submit($form_id, $form_values) {
  switch (taxonomy_save_term($form_values)) {
    case SAVED_NEW:
454
      drupal_set_message(t('Created new term %term.', array('%term' => $form_values['name'])));
455 456
      break;
    case SAVED_UPDATED:
457
      drupal_set_message(t('The term %term has been updated.', array('%term' => $form_values['name'])));
458 459
      break;
  }
460
  return 'admin/content/taxonomy';
461 462
}

463 464 465 466 467 468 469 470 471 472
/**
 * Helper function for taxonomy_form_term_submit().
 *
 * @param $form_values
 * @return
 *   Status constant indicating if term was inserted or updated.
 */
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']);
473
    $hook = 'update';
474
    $status = SAVED_UPDATED;
Kjartan's avatar
Kjartan committed
475
  }
476 477
  else if ($form_values['tid']) {
    return taxonomy_del_term($form_values['tid']);
Kjartan's avatar
Kjartan committed
478 479
  }
  else {
480 481
    $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']);
482
    $hook = 'insert';
483
    $status = SAVED_NEW;
Kjartan's avatar
Kjartan committed
484
  }
Dries's avatar
 
Dries committed
485

486 487 488
  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) {
Kjartan's avatar
Kjartan committed
489
      if ($related_id != 0) {
490
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $form_values['tid'], $related_id);
Dries's avatar
 
Dries committed
491
      }
Kjartan's avatar
Kjartan committed
492
    }
Kjartan's avatar
Kjartan committed
493
  }
Dries's avatar
 
Dries committed
494

495 496 497
  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);
Kjartan's avatar
Kjartan committed
498
  }
499 500
  if (is_array($form_values['parent'])) {
    foreach ($form_values['parent'] as $parent) {
501 502
      if (is_array($parent)) {
        foreach ($parent as $tid) {
503
          db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $tid);
504 505 506
        }
      }
      else {
507
        db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $parent);
508
      }
Dries's avatar
 
Dries committed
509
    }
Kjartan's avatar
Kjartan committed
510
  }
511
  else {
512
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $form_values['parent']);
513
  }
Dries's avatar
 
Dries committed
514

515 516 517
  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) {
Dries's avatar
 
Dries committed
518
      if ($synonym) {
519
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $form_values['tid'], chop($synonym));
Dries's avatar
 
Dries committed
520
      }
Kjartan's avatar
Kjartan committed
521
    }
Dries's avatar
 
Dries committed
522
  }
Dries's avatar
 
Dries committed
523

524
  if (isset($hook)) {
525
    module_invoke_all('taxonomy', $hook, 'term', $form_values);
526 527
  }

Dries's avatar
 
Dries committed
528 529
  cache_clear_all();

530
  return $status;
Kjartan's avatar
Kjartan committed
531
}
Dries's avatar
 
Dries committed
532

533 534 535 536 537 538 539 540
/**
 * Delete a term.
 *
 * @param $tid
 *   The term ID.
 * @return
 *   Status constant indicating deletion.
 */
Kjartan's avatar
Kjartan committed
541
function taxonomy_del_term($tid) {
542 543 544 545 546 547 548 549 550 551 552 553 554 555
  $tids = array($tid);
  while ($tids) {
    $children_tids = $orphans = array();
    foreach ($tids as $tid) {
      // See if any of the term's children are about to be become orphans:
      if ($children = taxonomy_get_children($tid)) {
        foreach ($children as $child) {
          // If the term has multiple parents, we don't delete it.
          $parents = taxonomy_get_parents($child->tid);
          if (count($parents) == 1) {
            $orphans[] = $child->tid;
          }
        }
      }
Dries's avatar
 
Dries committed
556

557
      $term = (array) taxonomy_get_term($tid);
Dries's avatar
 
Dries committed
558

559 560 561 562 563
      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);
Dries's avatar
 
Dries committed
564

565 566
      module_invoke_all('taxonomy', 'delete', 'term', $term);
    }
Dries's avatar
 
Dries committed
567

568 569
    $tids = $orphans;
  }
Dries's avatar
 
Dries committed
570

Dries's avatar
 
Dries committed
571
  cache_clear_all();
572 573

  return SAVED_DELETED;
Dries's avatar
 
Dries committed
574 575
}

576
function taxonomy_term_confirm_delete($tid) {
Dries's avatar
 
Dries committed
577 578
  $term = taxonomy_get_term($tid);

579 580 581
  $form['type'] = array('#type' => 'value', '#value' => 'term');
  $form['name'] = array('#type' => 'value', '#value' => $term->name);
  $form['tid'] = array('#type' => 'value', '#value' => $tid);
582
  return confirm_form($form,
583
                  t('Are you sure you want to delete the term %title?',
584
                  array('%title' => $term->name)),
585
                  'admin/content/taxonomy',
586 587
                  t('Deleting a term will delete all its children if there are any. This action cannot be undone.'),
                  t('Delete'),
588
                  t('Cancel'));
Kjartan's avatar
Kjartan committed
589
}
Dries's avatar
 
Dries committed
590

591 592
function taxonomy_term_confirm_delete_submit($form_id, $form_values) {
  taxonomy_del_term($form_values['tid']);
593
  drupal_set_message(t('Deleted term %name.', array('%name' => $form_values['name'])));
594
  return 'admin/content/taxonomy';
Kjartan's avatar
Kjartan committed
595 596
}

Dries's avatar
 
Dries committed
597 598 599
/**
 * Generate a form element for selecting terms from a vocabulary.
 */
Dries's avatar
 
Dries committed
600
function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
Dries's avatar
 
Dries committed
601
  $vocabulary = taxonomy_get_vocabulary($vid);
602
  $help = ($help) ? $help : $vocabulary->help;
Kjartan's avatar
Kjartan committed
603 604 605 606
  if ($vocabulary->required) {
    $blank = 0;
  }
  else {
Dries's avatar
 
Dries committed
607
    $blank = '<'. t('none') .'>';
Kjartan's avatar
Kjartan committed
608
  }
Dries's avatar
 
Dries committed
609

610
  return _taxonomy_term_select(check_plain($vocabulary->name), $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
Kjartan's avatar
Kjartan committed
611
}
Dries's avatar
 
Dries committed
612

613
/**
614
 * Generate a set of options for selecting a term from all vocabularies.
615 616 617 618 619 620 621
 */
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);
622 623
    if ($tree && (count($tree) > 1)) {
      $options[$vocabulary->name] = array();
624
      foreach ($tree as $term) {
625
        $options[$vocabulary->name][$term->tid] = str_repeat('-', $term->depth) . $term->name;
626 627 628 629 630 631
      }
    }
  }
  return $options;
}

Dries's avatar
 
Dries committed
632 633 634 635 636 637
/**
 * Return an array of all vocabulary objects.
 *
 * @param $type
 *   If set, return only those vocabularies associated with this node type.
 */
Dries's avatar
 
Dries committed
638
function taxonomy_get_vocabularies($type = NULL) {
Kjartan's avatar
Kjartan committed
639
  if ($type) {
640
    $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);
Kjartan's avatar
Kjartan committed
641 642
  }
  else {
643
    $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'));
Kjartan's avatar
Kjartan committed
644
  }
Dries's avatar
 
Dries committed
645

Kjartan's avatar
Kjartan committed
646
  $vocabularies = array();
Dries's avatar
 
Dries committed
647
  $node_types = array();
Kjartan's avatar
Kjartan committed
648
  while ($voc = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
649 650 651 652
    $node_types[$voc->vid][] = $voc->type;
    unset($voc->type);
    $voc->nodes = $node_types[$voc->vid];
    $vocabularies[$voc->vid] = $voc;
Dries's avatar
 
Dries committed
653
  }
Dries's avatar
 
Dries committed
654

Kjartan's avatar
Kjartan committed
655 656
  return $vocabularies;
}
Dries's avatar
 
Dries committed
657

Dries's avatar
 
Dries committed
658
/**
659
 * Implementation of hook_form_alter().
Dries's avatar
 
Dries committed
660 661
 * Generate a form for selecting terms to associate with a node.
 */
662 663 664 665
function taxonomy_form_alter($form_id, &$form) {
  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $node = $form['#node'];

666
    if (!isset($node->taxonomy)) {
667 668 669 670 671 672
      if ($node->nid) {
        $terms = taxonomy_node_get_terms($node->nid);
      }
      else {
        $terms = array();
      }
Kjartan's avatar
Kjartan committed
673 674
    }
    else {
675
      $terms = $node->taxonomy;
Dries's avatar
 
Dries committed
676 677
    }

678
    $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);
Dries's avatar
Dries committed
679

680 681 682 683
    while ($vocabulary = db_fetch_object($c)) {
      if ($vocabulary->tags) {
        $typed_terms = array();
        foreach ($terms as $term) {
684
          // Extract terms belonging to the vocabulary in question.
685
          if ($term->vid == $vocabulary->vid) {
Dries's avatar
Dries committed
686

687
            // Commas and quotes in terms are special cases, so encode 'em.
688 689
            if (strpos($term->name, ',') !== FALSE || strpos($term->name, '"') !== FALSE) {
              $term->name = '"'.str_replace('"', '""', $term->name).'"';
690 691 692 693
            }

            $typed_terms[] = $term->name;
          }
Dries's avatar
Dries committed
694
        }
695
        $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
696

697 698 699 700
        if ($vocabulary->help) {
          $help = $vocabulary->help;
        }
        else {
701
          $help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".');
702
        }
703 704 705 706 707 708 709
        $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,
710
          '#maxlength' => 255,
711
        );
712 713
      }
      else {
714 715 716 717 718 719 720 721
        // Extract terms belonging to the vocabulary in question.
        $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), $vocabulary->help);
722
        $form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
723
        $form['taxonomy'][$vocabulary->vid]['#required'] = $vocabulary->required;
724
      }
Dries's avatar
Dries committed
725
    }
726 727 728 729 730 731 732 733 734
    if (is_array($form['taxonomy']) && !empty($form['taxonomy'])) {
      if (count($form['taxonomy']) > 1) { // Add fieldset only if form has more than 1 element.
        $form['taxonomy'] += array(
          '#type' => 'fieldset',
          '#title' => t('Categories'),
          '#collapsible' => TRUE,
          '#collapsed' => FALSE,
        );
      }
735 736
      $form['taxonomy']['#weight'] = -3;
      $form['taxonomy']['#tree'] = TRUE;
Dries's avatar
Dries committed
737
    }
Dries's avatar
 
Dries committed
738
  }
Kjartan's avatar
Kjartan committed
739
}
Dries's avatar
 
Dries committed
740

Dries's avatar
 
Dries committed
741
/**
742
 * Find all terms associated with the given node, within one vocabulary.
Dries's avatar
 
Dries committed
743 744
 */
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
745
  $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);
Kjartan's avatar
Kjartan committed
746 747 748
  $terms = array();
  while ($term = db_fetch_object($result)) {
    $terms[$term->$key] = $term;
Dries's avatar
 
Dries committed
749
  }
Kjartan's avatar
Kjartan committed
750 751 752
  return $terms;
}

Dries's avatar
 
Dries committed
753
/**
754
 * Find all terms associated with the given node, ordered by vocabulary and term weight.
Dries's avatar
 
Dries committed
755 756
 */
function taxonomy_node_get_terms($nid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
757
  static $terms;
Dries's avatar
 
Dries committed
758

Dries's avatar
 
Dries committed
759
  if (!isset($terms[$nid])) {
760
    $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);
Kjartan's avatar
Kjartan committed
761
    $terms[$nid] = array();
Dries's avatar
 
Dries committed
762
    while ($term = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
763
      $terms[$nid][$term->$key] = $term;
Dries's avatar
 
Dries committed
764 765
    }
  }
Kjartan's avatar
Kjartan committed
766 767
  return $terms[$nid];
}
Dries's avatar
 
Dries committed
768

Dries's avatar
Dries committed
769 770 771
/**
 * Make sure incoming vids are free tagging enabled.
 */
772 773 774
function taxonomy_node_validate(&$node) {
  if ($node->taxonomy) {
    $terms = $node->taxonomy;
Dries's avatar
Dries committed
775 776 777 778
    if ($terms['tags']) {
      foreach ($terms['tags'] as $vid => $vid_value) {
        $vocabulary = taxonomy_get_vocabulary($vid);
        if (!$vocabulary->tags) {
779 780
          // see form_get_error $key = implode('][', $element['#parents']);
          // on why this is the key
781
          form_set_error("taxonomy][tags][$vid", t('The %name vocabulary can not be modified in this way.', array('%name' => $vocabulary->name)));
Dries's avatar
Dries committed
782 783 784 785 786 787
        }
      }
    }
  }
}

Dries's avatar
 
Dries committed
788 789 790
/**
 * Save term associations for a given node.
 */