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

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
}
15

16 17 18 19 20 21 22 23 24 25 26
/**
 * 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:
 *
 * if (module_exist('taxonomy')) {
 *   $this->links(taxonomy_link('taxonomy terms', $node));
 * }
 */
Dries's avatar
Dries committed
27
function taxonomy_link($type, $node = NULL) {
Dries's avatar
Dries committed
28
  if ($type == 'taxonomy terms' && $node != NULL) {
29
    $links = array();
30
    if (array_key_exists('taxonomy', $node)) {
31
      foreach ($node->taxonomy as $term) {
32
        $links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => $term->description));
Dries's avatar
Dries committed
33
      }
Dries's avatar
Dries committed
34 35 36
    }
    return $links;
  }
Kjartan's avatar
Kjartan committed
37 38
}

39 40 41 42 43 44 45 46
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
47 48 49
/**
 * Implementation of hook_menu().
 */
50
function taxonomy_menu($may_cache) {
Dries's avatar
Dries committed
51
  $items = array();
52

53 54 55 56
  if ($may_cache) {
    $items[] = array('path' => 'admin/taxonomy', 'title' => t('categories'),
      'callback' => 'taxonomy_admin',
      'access' => user_access('administer taxonomy'));
57

58 59
    $items[] = array('path' => 'admin/taxonomy/list', 'title' => t('list'),
      'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
60

61 62 63 64 65
    $items[] = array('path' => 'admin/taxonomy/add/vocabulary', 'title' => t('add vocabulary'),
      'callback' => 'taxonomy_admin',
      'access' => user_access('administer taxonomy'),
      'type' => MENU_LOCAL_TASK);

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    $items[] = array('path' => 'admin/taxonomy/edit/vocabulary', 'title' => t('edit vocabulary'),
      'callback' => 'taxonomy_admin',
      'access' => user_access('administer taxonomy'),
      'type' => MENU_CALLBACK);

    $items[] = array('path' => 'admin/taxonomy/add/term', 'title' => t('add term'),
      'callback' => 'taxonomy_admin',
      'access' => user_access('administer taxonomy'),
      'type' => MENU_CALLBACK);

    $items[] = array('path' => 'admin/taxonomy/edit/term', 'title' => t('edit term'),
      'callback' => 'taxonomy_admin',
      'access' => user_access('administer taxonomy'),
      'type' => MENU_CALLBACK);

81 82 83 84
    $items[] = array('path' => 'taxonomy/term', 'title' => t('taxonomy term'),
      'callback' => 'taxonomy_term_page',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK);
85 86 87 88 89

    $items[] = array('path' => 'taxonomy/autocomplete', 'title' => t('autocomplete taxonomy'),
      'callback' => 'taxonomy_autocomplete',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK);
90
  }
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  else {
    if (is_numeric(arg(2))) {
      $items[] = array('path' => 'admin/taxonomy/' . arg(2), 'title' => t('add term'),
        'callback' => 'taxonomy_admin',
        'access' => user_access('administer taxonomy'),
        'type' => MENU_CALLBACK);

      $items[] = array('path' => 'admin/taxonomy/' . arg(2) . '/list', 'title' => t('list'),
        'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);

      $items[] = array('path' => 'admin/taxonomy/' . arg(2) . '/add/term', 'title' => t('add term'),
        'callback' => 'taxonomy_admin',
        'access' => user_access('administer taxonomy'),
        'type' => MENU_LOCAL_TASK);
    }
  }
107

Dries's avatar
Dries committed
108 109
  return $items;
}
110

Kjartan's avatar
Kjartan committed
111
function taxonomy_form_vocabulary($edit = array()) {
112
  $form['name'] = array('#type' => 'textfield', '#title' => t('Vocabulary name'), '#default_value' => $edit['name'], '#maxlength' => 64, '#description' => t('The name for this vocabulary.  Example: "Topic".'), '#required' => TRUE);
113

114 115
  $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'], '#maxlength' => 255, '#description' => t('Instructions to present to the user when choosing a term.'));
116 117
  $form['nodes'] = array('#type' => 'checkboxes', '#title' => t('Types'), '#default_value' => $edit['nodes'], '#options' => node_get_types(), '#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'))));
118 119 120 121 122
  $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.'));
123

124 125 126 127
  // Add extra vocabulary form elements.
  $extra = module_invoke_all('taxonomy', 'form', 'vocabulary');
  if (is_array($extra)) {
    foreach ($extra as $key => $element) {
128
      $extra[$key]['#weight'] = isset($extra[$key]['#weight']) ? $extra[$key]['#weight'] : -18;
129 130 131
    }
    $form = array_merge($form, $extra);
  }
132

133
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
Dries's avatar
Dries committed
134
  if ($edit['vid']) {
135 136
    $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
    $form['vid'] = array('#type' => 'hidden', '#value' => $edit['vid']);
137
  }
138
  return drupal_get_form('taxonomy_form_vocabulary', $form);
Kjartan's avatar
Kjartan committed
139
}
Kjartan's avatar
Kjartan committed
140

141
function taxonomy_save_vocabulary(&$edit) {
142
  $edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes'];
143

Dries's avatar
Dries committed
144
  if ($edit['vid'] && $edit['name']) {
145
    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']);
146
    db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']);
147
    foreach ($edit['nodes'] as $type => $selected) {
148 149
      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
    }
Dries's avatar
Dries committed
150
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
151
    $status = SAVED_UPDATED;
152
  }
Dries's avatar
Dries committed
153
  else if ($edit['vid']) {
154
    $status = taxonomy_del_vocabulary($edit['vid']);
Kjartan's avatar
Kjartan committed
155 156
  }
  else {
157 158
    $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');
159
    foreach ($edit['nodes'] as $type => $selected) {
160 161
      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
    }
Dries's avatar
Dries committed
162
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
163
    $status = SAVED_NEW;
Kjartan's avatar
Kjartan committed
164
  }
Dries's avatar
Dries committed
165 166

  cache_clear_all();
Dries's avatar
Dries committed
167

168
  return $status;
Kjartan's avatar
Kjartan committed
169
}
170

Kjartan's avatar
Kjartan committed
171
function taxonomy_del_vocabulary($vid) {
172
  $vocabulary = (array) taxonomy_get_vocabulary($vid);
173

Dries's avatar
Dries committed
174
  db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid);
175
  db_query('DELETE FROM {vocabulary_node_types} WHERE vid = %d', $vid);
Dries's avatar
Dries committed
176
  $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid);
Kjartan's avatar
Kjartan committed
177 178
  while ($term = db_fetch_object($result)) {
    taxonomy_del_term($term->tid);
179
  }
180

Dries's avatar
Dries committed
181
  module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
Dries's avatar
Dries committed
182

Dries's avatar
Dries committed
183 184
  cache_clear_all();

185
  return SAVED_DELETED;
186 187 188 189 190
}

function _taxonomy_confirm_del_vocabulary($vid) {
  $vocabulary = taxonomy_get_vocabulary($vid);

191 192 193
  $form['type'] = array('#type' => 'hidden', '#value' => 'vocabulary');
  $form['vid'] = array('#type' => 'hidden', '#value' => $vid);
  $form['name'] = array('#type' => 'hidden', '#value' => $vocabulary->name);
194 195 196 197
  return confirm_form('vocabulary_confirm_delete', $form,
                  t('Are you sure you want to delete the vocabulary %title?',
                  array('%title' => theme('placeholder', $vocabulary->name))),
                  'admin/taxonomy', t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'),
198
                  t('Delete'),
199
                  t('Cancel'));
Kjartan's avatar
Kjartan committed
200
}
201

Kjartan's avatar
Kjartan committed
202
function taxonomy_form_term($edit = array()) {
Dries's avatar
Dries committed
203
  $vocabulary_id = isset($edit['vid']) ? $edit['vid'] : arg(4);
Kjartan's avatar
Kjartan committed
204
  $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
205

206
  $form['name'] = array('#type' => 'textfield', '#title' => t('Term name'), '#default_value' => $edit['name'], '#maxlength' => 64, '#description' => t('The name for this term.  Example: "Linux".'), '#required' => TRUE);
207

208
  $form['description'] = array('#type' => 'textarea', '#title' => t('Description'), '#default_value' => $edit['description'], '#description' => t('A description of the term.'));
209

Kjartan's avatar
Kjartan committed
210
  if ($vocabulary->hierarchy) {
Dries's avatar
Dries committed
211 212
    $parent = array_keys(taxonomy_get_parents($edit['tid']));
    $children = taxonomy_get_tree($vocabulary_id, $edit['tid']);
213

Dries's avatar
Dries committed
214
    // A term can't be the child of itself, nor of its children.
215 216 217
    foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
Dries's avatar
Dries committed
218
    $exclude[] = $edit['tid'];
219

Kjartan's avatar
Kjartan committed
220
    if ($vocabulary->hierarchy == 1) {
221
      $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);
222
    }
Kjartan's avatar
Kjartan committed
223
    elseif ($vocabulary->hierarchy == 2) {
224
      $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);
225
    }
Kjartan's avatar
Kjartan committed
226
  }
227

228
  if ($vocabulary->relations) {
229
    $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']));
230 231
  }

232 233
  $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.'));
234 235 236 237 238

  // Add extra term form elements.
  $extra = module_invoke_all('taxonomy', 'term', 'vocabulary');
  if (is_array($extra)) {
    foreach ($extra as $key => $element) {
239
      $extra[$key]['#weight'] = isset($extra[$key]['#weight']) ? $extra[$key]['#weight'] : -18;
240 241 242 243 244
    }
    $form = array_merge($form, $extra);
  }


245 246
  $form['vid'] = array('#type' => 'hidden', '#value' => $vocabulary->vid);
  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
Kjartan's avatar
Kjartan committed
247

Dries's avatar
Dries committed
248
  if ($edit['tid']) {
249 250
    $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
    $form['tid'] = array('#type' => 'hidden', '#value' => $edit['tid']);
251
  }
252
  else {
253
    $form['destination'] = array('#type' => 'hidden', '#value' => $_GET['q']);
254
  }
255

256
  return drupal_get_form('taxonomy_form_term', $form);
Kjartan's avatar
Kjartan committed
257
}
258

259
function taxonomy_save_term(&$edit) {
Dries's avatar
Dries committed
260
  if ($edit['tid'] && $edit['name']) {
261
    db_query("UPDATE {term_data} SET name = '%s', description = '%s', weight = %d WHERE tid = %d", $edit['name'], $edit['description'], $edit['weight'], $edit['tid']);
Dries's avatar
Dries committed
262
    module_invoke_all('taxonomy', 'update', 'term', $edit);
263
    $status = SAVED_UPDATED;
Kjartan's avatar
Kjartan committed
264
  }
Dries's avatar
Dries committed
265 266
  else if ($edit['tid']) {
    return taxonomy_del_term($edit['tid']);
Kjartan's avatar
Kjartan committed
267 268
  }
  else {
Dries's avatar
Dries committed
269
    $edit['tid'] = db_next_id('{term_data}_tid');
270
    db_query("INSERT INTO {term_data} (tid, name, description, vid, weight) VALUES (%d, '%s', '%s', %d, %d)", $edit['tid'], $edit['name'], $edit['description'], $edit['vid'], $edit['weight']);
Dries's avatar
Dries committed
271
    module_invoke_all('taxonomy', 'insert', 'term', $edit);
272
    $status = SAVED_NEW;
Kjartan's avatar
Kjartan committed
273
  }
274

Dries's avatar
Dries committed
275 276 277
  db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $edit['tid'], $edit['tid']);
  if ($edit['relations']) {
    foreach ($edit['relations'] as $related_id) {
Kjartan's avatar
Kjartan committed
278
      if ($related_id != 0) {
Dries's avatar
Dries committed
279
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $edit['tid'], $related_id);
280
      }
Kjartan's avatar
Kjartan committed
281
    }
Kjartan's avatar
Kjartan committed
282
  }
283

Dries's avatar
Dries committed
284 285
  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $edit['tid']);
  if (!isset($edit['parent'])) {
286
    $edit['parent'] = array(0);
Kjartan's avatar
Kjartan committed
287
  }
Dries's avatar
Dries committed
288 289
  if (is_array($edit['parent'])) {
    foreach ($edit['parent'] as $parent) {
290 291 292 293 294 295 296 297
      if (is_array($parent)) {
        foreach ($parent as $tid) {
          db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $tid);
        }
      }
      else {
        db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $parent);
      }
298
    }
Kjartan's avatar
Kjartan committed
299
  }
300 301 302
  else {
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $edit['parent']);
  }
303

Dries's avatar
Dries committed
304 305 306
  db_query('DELETE FROM {term_synonym} WHERE tid = %d', $edit['tid']);
  if ($edit['synonyms']) {
    foreach (explode ("\n", str_replace("\r", '', $edit['synonyms'])) as $synonym) {
307
      if ($synonym) {
Dries's avatar
Dries committed
308
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $edit['tid'], chop($synonym));
309
      }
Kjartan's avatar
Kjartan committed
310
    }
311
  }
312

Dries's avatar
Dries committed
313 314
  cache_clear_all();

315
  return $status;
Kjartan's avatar
Kjartan committed
316
}
317

Kjartan's avatar
Kjartan committed
318
function taxonomy_del_term($tid) {
319 320 321 322 323 324 325 326 327 328 329 330 331 332
  $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;
          }
        }
      }
333

334
      $term = (array) taxonomy_get_term($tid);
335

336 337 338 339 340
      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);
341

342
      module_invoke_all('taxonomy', 'delete', 'term', $term);
343
      drupal_set_message(t('Deleted term %name.', array('%name' => theme('placeholder', $term['name']))));
344
    }
345

346 347
    $tids = $orphans;
  }
348

Dries's avatar
Dries committed
349
  cache_clear_all();
350 351 352 353 354
}

function _taxonomy_confirm_del_term($tid) {
  $term = taxonomy_get_term($tid);

355 356
  $form['type'] = array('#type' => 'hidden', '#value' => 'term');
  $form['tid'] = array('#type' => 'hidden', '#value' => $tid);
357 358 359
  return confirm_form('term_confirm_delete', $form,
                  t('Are you sure you want to delete the term %title?',
                  array('%title' => theme('placeholder', $term->name))),
360 361 362
                  'admin/taxonomy',
                  t('Deleting a term will delete all its children if there are any. This action cannot be undone.'),
                  t('Delete'),
363
                  t('Cancel'));
Kjartan's avatar
Kjartan committed
364
}
365

Dries's avatar
Dries committed
366 367 368
/**
 * Generate a tabular listing of administrative functions for vocabularies.
 */
Kjartan's avatar
Kjartan committed
369
function taxonomy_overview() {
370 371
  $vid = arg(2);

372
  // Show all vocabularies, and a "view terms" link to the pagers.
373
  if (!$vid) {
374
    $header = array(t('Name'), t('Type'), array('data' => t('Operations'), 'colspan' => '2'));
375 376
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $vocabulary) {
377 378
      $types = array();
      foreach ($vocabulary->nodes as $type) {
379
        $node_type = node_get_name($type);
380 381
        $types[] = $node_type ? $node_type : $type;
      }
Dries's avatar
Dries committed
382
      $rows[] = array(check_plain($vocabulary->name), implode(', ', $types), l(t('edit vocabulary'), "admin/taxonomy/edit/vocabulary/$vocabulary->vid"), l(t('edit terms'), "admin/taxonomy/$vocabulary->vid"));
383 384 385
    }

    if (!$rows) {
386
      $rows[] = array(array('data' => t('No categories available.'), 'colspan' => '4', 'class' => 'message'));
387 388
    }
  }
389

390
  // Show the vocabulary's terms with a pager.
391
  else {
392 393 394
    $destination = drupal_get_destination();

    $header = array(t('Name'), t('Operations'));
395
    $vocabulary = taxonomy_get_vocabulary($vid);
396

Dries's avatar
Dries committed
397 398 399 400 401 402 403 404 405 406
    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
      if (($start_from && ($start_from * $page_increment) >= $total_entries) || ($displayed_count == $page_increment)) { continue; }
407
      $rows[] = array(_taxonomy_depth($term->depth) . ' ' . l(check_plain($term->name), "taxonomy/term/$term->tid"), l(t('edit'), "admin/taxonomy/edit/term/$term->tid", array(), $destination));
Dries's avatar
Dries committed
408 409 410 411 412 413
      $displayed_count++; // we're counting tids displayed
    }

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

Dries's avatar
Dries committed
415 416
    $GLOBALS['pager_page_array'][] = $start_from;  // FIXME
    $GLOBALS['pager_total'][] = intval($total_entries / $page_increment) + 1; // FIXME
417

Dries's avatar
Dries committed
418 419
    if ($total_entries >= $page_increment) {
      $rows[] = array(array('data' => theme('pager', NULL, $page_increment), 'colspan' => '2'));
420
    }
421 422
  }

423
  return theme('table', $header, $rows, array('id' => 'taxonomy'));
Kjartan's avatar
Kjartan committed
424 425
}

Dries's avatar
Dries committed
426 427 428
/**
 * Generate a form element for selecting terms from a vocabulary.
 */
429
function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
Dries's avatar
Dries committed
430
  $vocabulary = taxonomy_get_vocabulary($vid);
431
  $help = ($help) ? $help : $vocabulary->help;
Kjartan's avatar
Kjartan committed
432 433 434 435
  if ($vocabulary->required) {
    $blank = 0;
  }
  else {
Dries's avatar
Dries committed
436
    $blank = '<'. t('none') .'>';
Kjartan's avatar
Kjartan committed
437
  }
438

439
  return _taxonomy_term_select(check_plain($vocabulary->name), $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
Kjartan's avatar
Kjartan committed
440
}
441

Dries's avatar
Dries committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
/**
 * Generate a set of options for selecting a term from all vocabularies. Can be
 * passed to form_select.
 */
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);
    $options[$vocabulary->name] = array();
    if ($tree) {
      foreach ($tree as $term) {
        $options[$vocabulary->name][$term->tid] = _taxonomy_depth($term->depth, '-') . $term->name;
      }
    }
  }
  return $options;
}

Dries's avatar
Dries committed
462 463 464 465 466 467
/**
 * Return an array of all vocabulary objects.
 *
 * @param $type
 *   If set, return only those vocabularies associated with this node type.
 */
468
function taxonomy_get_vocabularies($type = NULL) {
Kjartan's avatar
Kjartan committed
469
  if ($type) {
470
    $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
471 472
  }
  else {
473
    $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
474
  }
475

Kjartan's avatar
Kjartan committed
476
  $vocabularies = array();
477
  $node_types = array();
Kjartan's avatar
Kjartan committed
478
  while ($voc = db_fetch_object($result)) {
479 480 481 482
    $node_types[$voc->vid][] = $voc->type;
    unset($voc->type);
    $voc->nodes = $node_types[$voc->vid];
    $vocabularies[$voc->vid] = $voc;
483
  }
484

Kjartan's avatar
Kjartan committed
485 486
  return $vocabularies;
}
487

Dries's avatar
Dries committed
488 489 490
/**
 * Generate a form for selecting terms to associate with a node.
 */
491 492 493 494
function taxonomy_form_alter($form_id, &$form) {
  if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
    $node = $form['#node'];

495
    if (!isset($node->taxonomy)) {
496 497 498 499 500 501
      if ($node->nid) {
        $terms = taxonomy_node_get_terms($node->nid);
      }
      else {
        $terms = array();
      }
Kjartan's avatar
Kjartan committed
502 503
    }
    else {
504
      $terms = $node->taxonomy;
505 506
    }

507
    $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);
508

509 510 511 512 513
    while ($vocabulary = db_fetch_object($c)) {
      if ($vocabulary->tags) {
        $typed_terms = array();
        foreach ($terms as $term) {
          if ($term->vid == $vocabulary->vid) {
514

515 516 517 518 519 520 521
            // Commas and quotes in terms are special cases, so encode 'em.
            if (preg_match('/,/', $term->name) || preg_match('/"/', $term->name)) {
              $term->name = '"'.preg_replace('/"/', '""', $term->name).'"';
            }

            $typed_terms[] = $term->name;
          }
522
        }
523
        $typed_string = implode(', ', $typed_terms) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL);
524

525 526 527 528 529 530 531
        if ($vocabulary->help) {
          $help = $vocabulary->help;
        }
        else {
          $help = t('A comma-separated list of terms describing this content.  Example: funny, bungie jumping, "Company, Inc.".');
        }
        $form['taxonomy']['tags'][$vocabulary->vid] = array('#type' => 'textfield', '#default_value' => $typed_string, '#maxlength' => 100, '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid, '#required' => $vocabulary->required, '#title' => $vocabulary->name, '#description' => $help);
532 533
      }
      else {
534
        $ntterms = isset($node->taxonomy) ? $terms : array_keys($terms);
535
        $form['taxonomy'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, $ntterms, $help, 'taxonomy');
536
        $form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight;
537
      }
538
    }
539
    if (isset($form['taxonomy'])) {
540
      $form['taxonomy'] = array_merge($form['taxonomy'], array('#type' => 'fieldset', '#title' => t('Categories'), '#collapsible' => TRUE, '#collapsed' => FALSE, '#tree' => TRUE, '#weight' => -3));
541
    }
542
  }
Kjartan's avatar
Kjartan committed
543
}
544

Dries's avatar
Dries committed
545 546 547 548
/**
 * Find all terms associated to the given node, within one vocabulary.
 */
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
549
  $result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t, {term_node} r WHERE t.tid = r.tid AND t.vid = %d AND r.nid = %d ORDER BY weight', 't', 'tid'), $vid, $nid);
Kjartan's avatar
Kjartan committed
550 551 552
  $terms = array();
  while ($term = db_fetch_object($result)) {
    $terms[$term->$key] = $term;
553
  }
Kjartan's avatar
Kjartan committed
554 555 556
  return $terms;
}

Dries's avatar
Dries committed
557
/**
558
 * Find all terms associated to the given node, ordered by vocabulary and term weight.
Dries's avatar
Dries committed
559 560
 */
function taxonomy_node_get_terms($nid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
561
  static $terms;
562

563
  if (!isset($terms[$nid])) {
564
    $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
565
    $terms[$nid] = array();
566
    while ($term = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
567
      $terms[$nid][$term->$key] = $term;
568 569
    }
  }
Kjartan's avatar
Kjartan committed
570 571
  return $terms[$nid];
}
572

573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
/**
 * Make sure incoming vids are free tagging enabled.
 */
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' => theme('placeholder', $vocabulary->name))));
        }
      }
    }
  }
}

Dries's avatar
Dries committed
590 591 592
/**
 * Save term associations for a given node.
 */
Kjartan's avatar
Kjartan committed
593
function taxonomy_node_save($nid, $terms) {
594
  taxonomy_node_delete($nid);
595 596 597

  // Free tagging vocabularies do not send their tids in the form,
  // so we'll detect them here and process them independently.
598
  if (isset($terms['tags'])) {
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
    $typed_input = $terms['tags'];
    unset($terms['tags']);

    foreach ($typed_input as $vid => $vid_value) {
      // This regexp allows the following types of user input:
      // this, "somecmpany, llc", "and ""this"" w,o.rks", foo bar
      $regexp = '%(?:^|,\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
      preg_match_all($regexp, $vid_value, $matches);
      $typed_terms = $matches[1];

      foreach ($typed_terms as $typed_term) {
        // If a user has escaped a term (to demonstrate that it is a group,
        // or includes a comma or quote character), we remove the escape
        // formatting so to save the term into the DB as the user intends.
        $typed_term = str_replace('""', '"', preg_replace('/^"(.*)"$/', '\1', $typed_term));
        $typed_term = trim($typed_term);
        if ($typed_term == "") { continue; }

        // See if the term exists in the chosen vocabulary
        // and return the tid, otherwise, add a new record.
        $possibilities = taxonomy_get_term_by_name($typed_term);
        $typed_term_tid = NULL; // tid match if any.
        foreach ($possibilities as $possibility) {
          if ($possibility->vid == $vid) {
            $typed_term_tid = $possibility->tid;
          }
        }

        if (!$typed_term_tid) {
628 629 630
          $edit = array('vid' => $vid, 'name' => $typed_term);
          $status = taxonomy_save_term($edit);
          $typed_term_tid = $edit['tid'];
631 632 633 634 635 636
        }

        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $typed_term_tid);
      }
    }
  }
637

638
  if (is_array($terms)) {
639
    foreach ($terms as $term) {
640 641 642 643 644 645 646
      if (is_array($term)) {
        foreach ($term as $tid) {
          if ($tid) {
            db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
          }
        }
      }
647
      else if ($term) {
Dries's avatar
Dries committed
648
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
649
      }
650 651
    }
  }
Kjartan's avatar
Kjartan committed
652
}
653

Dries's avatar
Dries committed
654 655 656
/**
 * Remove associations of a node to its terms.
 */
Kjartan's avatar
Kjartan committed
657
function taxonomy_node_delete($nid) {
Dries's avatar
Dries committed
658
  db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
Kjartan's avatar
Kjartan committed
659
}
660

Dries's avatar
Dries committed
661 662 663 664
/**
 * Find all term objects related to a given term ID.
 */
function taxonomy_get_related($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
665
  if ($tid) {
Dries's avatar
Dries committed
666
    $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);
Kjartan's avatar
Kjartan committed
667 668 669
    $related = array();
    while ($term = db_fetch_object($result)) {
      $related[$term->$key] = $term;
670
    }
Kjartan's avatar
Kjartan committed
671
    return $related;
672
  }
Kjartan's avatar
Kjartan committed
673 674
  else {
    return array();
675
  }
Kjartan's avatar
Kjartan committed
676
}
677

Dries's avatar
Dries committed
678 679 680 681
/**
 * Find all parents of a given term ID.
 */
function taxonomy_get_parents($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
682
  if ($tid) {
683
    $result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_hierarchy} h, {term_data} t WHERE h.parent = t.tid AND h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid);
Kjartan's avatar
Kjartan committed
684 685 686
    $parents = array();
    while ($parent = db_fetch_object($result)) {
      $parents[$parent->$key] = $parent;
687
    }
Kjartan's avatar
Kjartan committed
688
    return $parents;
689
  }
Kjartan's avatar
Kjartan committed
690 691 692 693
  else {
    return array();
  }
}
694

Dries's avatar
Dries committed
695 696 697 698
/**
 * Find all ancestors of a given term ID.
 */
function taxonomy_get_parents_all($tid) {
699 700 701 702 703 704 705 706 707 708 709 710
  $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;
}

Dries's avatar
Dries committed
711 712 713 714
/**
 * Find all children of a term ID.
 */
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
Kjartan's avatar
Kjartan committed
715
  if ($vid) {
716
    $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_hierarchy} h, {term_data} t WHERE t.vid = %d AND h.tid = t.tid AND h.parent = %d ORDER BY weight, name', 't', 'tid'), $vid, $tid);
717
  }
Kjartan's avatar
Kjartan committed
718
  else {
719
    $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_hierarchy} h, {term_data} t WHERE h.tid = t.tid AND parent = %d ORDER BY weight, name', 't', 'tid'), $tid);
Kjartan's avatar
Kjartan committed
720 721 722 723 724 725 726
  }
  $children = array();
  while ($term = db_fetch_object($result)) {
    $children[$term->$key] = $term;
  }
  return $children;
}
727

Dries's avatar
Dries committed
728 729 730 731 732 733 734 735 736 737 738 739 740
/**
 * Create a hierarchical representation of a vocabulary.
 *
 * @param $vid
 *   Which vocabulary to generate the tree for.
 *
 * @param $parent
 *   The term ID under which to generate the tree. If 0, generate the tree
 *   for the entire vocabulary.
 *
 * @param $depth
 *   Internal use only.
 *
741 742 743
 * @param $max_depth
 *   The number of levels of the tree to return. Leave NULL to return all levels.
 *
Dries's avatar
Dries committed
744 745 746 747 748
 * @return
 *   An array of all term objects in the tree. Each term object is extended
 *   to have "depth" and "parents" attributes in addition to its normal ones.
 */
function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) {
Dries's avatar
Dries committed
749
  static $children, $parents, $terms;
750

Kjartan's avatar
Kjartan committed
751
  $depth++;
752

Dries's avatar
Dries committed
753 754 755 756
  // We cache trees, so it's not CPU-intensive to call get_tree() on a term
  // and its children, too.
  if (!isset($children[$vid])) {
    $children[$vid] = array();
Dries's avatar
Dries committed
757

758
    $result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t, {term_hierarchy} h WHERE t.tid = h.tid AND t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid);
759
    while ($term = db_fetch_object($result)) {
Dries's avatar
Dries committed
760 761 762
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
763 764
    }
  }
765

766
  $max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
Dries's avatar
Dries committed
767 768 769 770 771 772 773 774 775 776 777 778
  if ($children[$vid][$parent]) {
    foreach ($children[$vid][$parent] as $child) {
      if ($max_depth > $depth) {
        $terms[$vid][$child]->depth = $depth;
        // The "parent" attribute is not useful, as it would show one parent only.
        unset($terms[$vid][$child]->parent);
        $terms[$vid][$child]->parents = $parents[$vid][$child];
        $tree[] = $terms[$vid][$child];

        if ($children[$vid][$child]) {
          $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
        }
779
      }
780
    }
Kjartan's avatar
Kjartan committed
781
  }
782

Dries's avatar
Dries committed
783
  return $tree ? $tree : array();
Kjartan's avatar
Kjartan committed
784
}
785

Dries's avatar
Dries committed
786 787 788
/**
 * Return an array of synonyms of the given term ID.
 */
Kjartan's avatar
Kjartan committed
789 790
function taxonomy_get_synonyms($tid) {
  if ($tid) {
Dries's avatar
Dries committed
791
    $result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid);
Kjartan's avatar
Kjartan committed
792
    while ($synonym = db_fetch_array($result)) {
Dries's avatar
Dries committed
793
      $synonyms[] = $synonym['name'];
794
    }
Kjartan's avatar
Kjartan committed
795
    return $synonyms ? $synonyms : array();
796
  }
Kjartan's avatar
Kjartan committed
797 798
  else {
    return array();
799
  }
Kjartan's avatar
Kjartan committed
800
}
801

Dries's avatar
Dries committed
802 803 804 805 806
/**
 * Return the term object that has the given string as a synonym.
 */
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));
Kjartan's avatar
Kjartan committed
807
}
808

Dries's avatar
Dries committed
809 810 811
/**
 * Given a term id, count the number of published nodes in it.
 */
812
function taxonomy_term_count_nodes($tid, $type = 0) {
Kjartan's avatar
Kjartan committed
813
  static $count;
814

815 816 817
  if (!isset($count[$type])) {
    // $type == 0 always evaluates true is $type is a string
    if (is_numeric($type)) {
818
      $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'));
819 820
    }
    else {
821
      $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t, {node} n WHERE t.nid = n.nid AND n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
822
    }
Kjartan's avatar
Kjartan committed
823
    while ($term = db_fetch_object($result)) {
824
      $count[$type][$term->tid] = $term->c;
825 826 827
    }
  }

Kjartan's avatar
Kjartan committed
828
  foreach (_taxonomy_term_children($tid) as $c) {
829
    $children_count += taxonomy_term_count_nodes($c, $type);
Kjartan's avatar
Kjartan committed
830
  }
831
  return $count[$type][$tid] + $children_count;
Kjartan's avatar
Kjartan committed
832 833
}

Dries's avatar
Dries committed
834 835 836
/**
 * Helper for taxonomy_term_count_nodes().
 */
Kjartan's avatar
Kjartan committed
837 838
function _taxonomy_term_children($tid) {
  static $children;
839

840
  if (!isset($children)) {
Dries's avatar
Dries committed
841
    $result = db_query('SELECT tid, parent FROM {term_hierarchy}');
Kjartan's avatar
Kjartan committed
842 843
    while ($term = db_fetch_object($result)) {
      $children[$term->parent][] = $term->tid;
844
    }
845
  }
Kjartan's avatar
Kjartan committed
846 847
  return $children[$tid] ? $children[$tid] : array();
}
848

Dries's avatar
Dries committed
849
/**
Dries's avatar
Dries committed
850
 * Try to map a string to an existing term, as for glossary use.
Dries's avatar
Dries committed
851
 *
Dries's avatar
Dries committed
852 853 854 855 856 857 858 859
 * Provides a case-insensitive and trimmed mapping, to maximize the
 * likelihood of a successful match.
 *
 * @param name
 *   Name of the term to search for.
 *
 * @return
 *   An array of matching term objects.
Dries's avatar
Dries committed
860 861
 */
function taxonomy_get_term_by_name($name) {
862
  $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER('%s') LIKE LOWER(name)", 't', 'tid'), trim($name));
Dries's avatar
Dries committed
863 864 865 866 867 868 869 870
  $result = array();
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }

  return $result;
}

Dries's avatar
Dries committed
871 872 873
/**
 * Return the vocabulary object matching a vocabulary ID.
 */
Kjartan's avatar
Kjartan committed
874
function taxonomy_get_vocabulary($vid) {
875 876 877 878 879 880 881 882 883 884 885
  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;
    }
886 887
  }

888
  return $vocabularies[$vid];
Kjartan's avatar
Kjartan committed
889
}
890

Dries's avatar
Dries committed
891 892 893
/**
 * Return the term object matching a term ID.
 */
Kjartan's avatar
Kjartan committed
894 895
function taxonomy_get_term($tid) {
  // simple cache using a static var?
Dries's avatar
Dries committed
896
  return db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid));
Kjartan's avatar
Kjartan committed
897
}
898

Kjartan's avatar
Kjartan committed
899
function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
900
  $tree = taxonomy_get_tree($vocabulary_id);
901 902
  $options = array();

Dries's avatar
Dries committed
903
  if ($blank) {
904
    $options[0] = $blank;
Dries's avatar
Dries committed
905
  }
Kjartan's avatar
Kjartan committed
906 907 908
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
909
        $options[$term->tid] = _taxonomy_depth($term->depth, '-') . $term->name;
910 911
      }
    }
Kjartan's avatar
Kjartan committed
912 913 914 915 916
    if (!$blank && !$value) {
      // required but without a predefined value, so set first as predefined
      $value = $tree[0]->tid;
    }
  }
917
  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');
918 919 920 921
}

function theme_taxonomy_term_select($element) {
  return theme('select', $element);
Kjartan's avatar
Kjartan committed
922
}
923

Kjartan's avatar
Kjartan committed
924 925 926
function _taxonomy_depth($depth, $graphic = '--') {
  for ($n = 0; $n < $depth; $n++) {
    $result .= $graphic;
927
  }
Kjartan's avatar
Kjartan committed
928 929
  return $result;
}
930

Dries's avatar
Dries committed
931 932 933
/**
 * Finds all nodes that match selected taxonomy conditions.
 *
934 935 936 937 938 939 940
 * @param $tids
 *   An array of term IDs to match.
 * @param $operator
 *   How to interpret multiple IDs in the array. Can be "or" or "and".
 * @param $depth
 *   How many levels deep to traverse the taxonomy tree. Can be a nonnegative
 *   integer or "all".
Dries's avatar
Dries committed
941 942 943
 * @param $pager
 *   Whether the nodes are to be used with a pager (the case on most Drupal
 *   pages) or not (in an XML feed, for example).
944
 * @param $order
Dries's avatar
Dries committed
945
 *   The order clause for the query that retrieve the nodes.
Dries's avatar
Dries committed
946 947 948
 * @return
 *   A resource identifier pointing to the query results.
 */
949
function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC') {
950 951