taxonomy.module 42.2 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 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) {
Kjartan's avatar
 
Kjartan committed
29
    $links = array();
Dries's avatar
 
Dries committed
30
    if (array_key_exists('taxonomy', $node)) {
Dries's avatar
 
Dries committed
31 32
      foreach ($node->taxonomy as $tid) {
        $term = taxonomy_get_term($tid);
Dries's avatar
 
Dries committed
33
        $links[] = l($term->name, 'taxonomy/term/'. $term->tid);
Dries's avatar
 
Dries committed
34 35 36 37 38
      }
    }
    else {
      $links = array();
      foreach (taxonomy_node_get_terms($node->nid) as $term) {
Dries's avatar
 
Dries committed
39
        $links[] = l($term->name, 'taxonomy/term/'. $term->tid);
Dries's avatar
 
Dries committed
40
      }
Dries's avatar
 
Dries committed
41 42 43 44

    }
    return $links;
  }
Kjartan's avatar
Kjartan committed
45 46
}

Dries's avatar
 
Dries committed
47 48 49
/**
 * Implementation of hook_menu().
 */
Dries's avatar
 
Dries committed
50
function taxonomy_menu($may_cache) {
Dries's avatar
 
Dries committed
51
  $items = array();
Dries's avatar
 
Dries committed
52

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

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

Dries's avatar
 
Dries committed
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 81 82 83 84 85
    $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/preview/vocabulary', 'title' => t('preview 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);

Dries's avatar
 
Dries committed
86 87 88 89 90 91
    $items[] = array('path' => 'taxonomy/term', 'title' => t('taxonomy term'),
      'callback' => 'taxonomy_term_page',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK);
  }

Dries's avatar
 
Dries committed
92 93
  return $items;
}
Dries's avatar
 
Dries committed
94

95 96 97 98 99 100 101 102 103 104
/**
 * Implementation of hook_block().
 *
 * Generates a block with all categories.
 */
function taxonomy_block($op = 'list', $delta = 0) {
  if ($op == 'list') {
    $blocks[0]['info'] = t('Categories');
    return $blocks;
  }
105
  else if ($op == 'view' && user_access('access content')) {
Steven Wittens's avatar
Steven Wittens committed
106
    $result = db_query(node_rewrite_sql('SELECT d.tid, d.name, MAX(n.created) AS updated, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE n.status = 1 GROUP BY d.tid, d.name ORDER BY updated DESC, d.name'));
107 108
    $items = array();
    while ($category = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
109
      $items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid) .'<br />'. t('%time ago', array('%time' => format_interval(time() - $category->updated)));
110 111 112 113 114 115 116 117
    }

    $block['subject'] = t('Categories');
    $block['content'] = theme('item_list', $items);
    return $block;
  }
}

Kjartan's avatar
Kjartan committed
118
function taxonomy_form_vocabulary($edit = array()) {
Dries's avatar
 
Dries committed
119 120 121
  foreach (array_merge(node_list(), $edit['nodes']) as $type) {
    $node_type = node_invoke($type, 'node_name');
    $nodes[$type] = $node_type ? $node_type : $type;
Kjartan's avatar
Kjartan committed
122
  }
Dries's avatar
 
Dries committed
123

Dries's avatar
 
Dries committed
124 125 126
  $form .= form_textfield(t('Vocabulary name'), 'name', $edit['name'], 50, 64, t('The name for this vocabulary.  Example: "Topic".'), NULL, TRUE);
  $form .= form_textarea(t('Description'), 'description', $edit['description'], 60, 5, t('Description of the vocabulary; can be used by modules.'));
  $form .= form_textfield(t('Help text'), 'help', $edit['help'], 50, 255, t('Instructions to present to the user when choosing a term.'));
Dries's avatar
 
Dries committed
127
  $form .= form_checkboxes(t('Types'), 'nodes', $edit['nodes'], $nodes, t('A list of node types you want to associate with this vocabulary.'), NULL, TRUE);
128 129
  $form .= form_checkbox(t('Related terms'), 'relations', 1, $edit['relations'], t('Allows <a href="%help-url">related terms</a> in this vocabulary.', array('%help-url' => url('admin/help/taxonomy', NULL, NULL, 'related-terms'))));
  $form .= form_radios(t('Hierarchy'), 'hierarchy', $edit['hierarchy'], array(t('Disabled'), t('Single'), t('Multiple')), 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'))));
Dries's avatar
 
Dries committed
130
  $form .= form_checkbox(t('Multiple select'), 'multiple', 1, $edit['multiple'], t('Allows nodes to have more than one term in this vocabulary.'));
Dries's avatar
 
Dries committed
131
  $form .= form_checkbox(t('Required'), 'required', 1, $edit['required'], t('If enabled, every node <strong>must</strong> have at least one term in this vocabulary.'));
Dries's avatar
 
Dries committed
132
  $form .= form_weight(t('Weight'), 'weight', $edit['weight'], 10, t('In listings, the heavier vocabularies will sink and the lighter vocabularies will be positioned nearer the top.'));
Dries's avatar
 
Dries committed
133
  $form .= form_submit(t('Submit'));
Dries's avatar
 
Dries committed
134

Dries's avatar
 
Dries committed
135 136 137
  if ($edit['vid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('vid', $edit['vid']);
Dries's avatar
 
Dries committed
138 139
  }

Kjartan's avatar
Kjartan committed
140 141
  return form($form);
}
Kjartan's avatar
Kjartan committed
142

Kjartan's avatar
Kjartan committed
143
function taxonomy_save_vocabulary($edit) {
Dries's avatar
 
Dries committed
144 145
  if (!$edit['nodes']) {
    $edit['nodes'] = array();
Dries's avatar
 
Dries committed
146 147
  }

Dries's avatar
 
Dries committed
148
  $data = array('name' => $edit['name'], 'description' => $edit['description'], 'help' => $edit['help'], 'multiple' => $edit['multiple'], 'required' => $edit['required'], 'hierarchy' => $edit['hierarchy'], 'relations' => $edit['relations'], 'weight' => $edit['weight']);
Dries's avatar
 
Dries committed
149
  if ($edit['vid'] && $edit['name']) {
Dries's avatar
 
Dries committed
150
    db_query('UPDATE {vocabulary} SET '. _taxonomy_prepare_update($data) .' WHERE vid = %d', $edit['vid']);
Dries's avatar
 
Dries committed
151 152 153 154
    db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']);
    foreach ($edit['nodes'] as $type) {
      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
    }
Dries's avatar
 
Dries committed
155
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
Dries's avatar
 
Dries committed
156
    $message = t('Updated vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Dries's avatar
 
Dries committed
157
  }
Dries's avatar
 
Dries committed
158 159
  else if ($edit['vid']) {
    $message = taxonomy_del_vocabulary($edit['vid']);
Kjartan's avatar
Kjartan committed
160 161
  }
  else {
Dries's avatar
 
Dries committed
162
    $data['vid'] = $edit['vid'] = db_next_id('{vocabulary}_vid');
Dries's avatar
 
Dries committed
163
    db_query('INSERT INTO {vocabulary} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
 
Dries committed
164 165 166
    foreach ($edit['nodes'] as $type) {
      db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type);
    }
Dries's avatar
 
Dries committed
167
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
Dries's avatar
 
Dries committed
168
    $message = t('Created new vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
169
  }
Dries's avatar
 
Dries committed
170 171

  cache_clear_all();
Dries's avatar
 
Dries committed
172

Dries's avatar
 
Dries committed
173
  drupal_set_message($message);
174

Dries's avatar
 
Dries committed
175
  return $edit;
Kjartan's avatar
Kjartan committed
176
}
Dries's avatar
 
Dries committed
177

Kjartan's avatar
Kjartan committed
178
function taxonomy_del_vocabulary($vid) {
Dries's avatar
 
Dries committed
179 180
  $vocabulary = taxonomy_get_vocabulary($vid);

Dries's avatar
 
Dries committed
181
  db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid);
Dries's avatar
 
Dries committed
182
  db_query('DELETE FROM {vocabulary_node_types} WHERE vid = %d', $vid);
Dries's avatar
 
Dries committed
183
  $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid);
Kjartan's avatar
Kjartan committed
184 185
  while ($term = db_fetch_object($result)) {
    taxonomy_del_term($term->tid);
Dries's avatar
 
Dries committed
186
  }
Dries's avatar
 
Dries committed
187

Dries's avatar
 
Dries committed
188
  module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
Dries's avatar
 
Dries committed
189

Dries's avatar
 
Dries committed
190 191
  cache_clear_all();

Dries's avatar
 
Dries committed
192
  return t('deleted vocabulary "%name".', array('%name' => $vocabulary->name));
Dries's avatar
 
Dries committed
193 194 195 196 197
}

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

Dries's avatar
 
Dries committed
198 199 200 201
  $form .= form_hidden('confirm', 1);
  $form .= form_hidden('type', 'vocabulary');
  $form .= form_hidden('vid', $vid);
  $form .= form_submit(t('Delete'));
Dries's avatar
 
Dries committed
202

Dries's avatar
 
Dries committed
203
  return form(form_item(t('Delete vocabulary "%name"', array('%name' => $vocabulary->name)), $form, t('Are you sure you want to delete the vocabulary and all its terms?')));
Kjartan's avatar
Kjartan committed
204
}
Dries's avatar
 
Dries committed
205

Kjartan's avatar
Kjartan committed
206
function taxonomy_form_term($edit = array()) {
Dries's avatar
 
Dries committed
207
  $vocabulary_id = isset($edit['vid']) ? $edit['vid'] : arg(4);
Kjartan's avatar
Kjartan committed
208
  $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
Dries's avatar
 
Dries committed
209

Dries's avatar
 
Dries committed
210 211
  $form = form_textfield(t('Term name'), 'name', $edit['name'], 50, 64, t('The name for this term.  Example: "Linux".'), NULL, TRUE);
  $form .= form_textarea(t('Description'), 'description', $edit['description'], 60, 5, t('A description of the term.'));
Dries's avatar
 
Dries committed
212

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

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

Kjartan's avatar
Kjartan committed
223
    if ($vocabulary->hierarchy == 1) {
224
      $form .= _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
225
    }
Kjartan's avatar
Kjartan committed
226
    elseif ($vocabulary->hierarchy == 2) {
227
      $form .= _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
228
    }
Kjartan's avatar
Kjartan committed
229
  }
Dries's avatar
 
Dries committed
230

231
  if ($vocabulary->relations) {
Dries's avatar
 
Dries committed
232
    $form .= _taxonomy_term_select(t('Related terms'), 'relations', array_keys(taxonomy_get_related($edit['tid'])), $vocabulary_id, NULL, 1, '<'. t('none') .'>', array($edit['tid']));
233 234
  }

235
  $form .= form_textarea(t('Synonyms'), 'synonyms', implode("\n", taxonomy_get_synonyms($edit['tid'])), 30, 5, t('<a href="%help-url">Synonyms</a> of this term, one synonym per line.', array('%help-url' => url('admin/help/taxonomy', NULL, NULL, 'synonyms'))));
Dries's avatar
 
Dries committed
236
  $form .= form_weight(t('Weight'), 'weight', $edit['weight'], 10, t('In listings, the heavier terms will sink and the lighter terms will be positioned nearer the top.'));
Dries's avatar
 
Dries committed
237 238
  $form .= form_hidden('vid', $vocabulary->vid);
  $form .= form_submit(t('Submit'));
Kjartan's avatar
Kjartan committed
239

Dries's avatar
 
Dries committed
240 241 242
  if ($edit['tid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('tid', $edit['tid']);
Dries's avatar
 
Dries committed
243 244
  }

Kjartan's avatar
Kjartan committed
245 246
  return form($form);
}
Dries's avatar
 
Dries committed
247

Kjartan's avatar
Kjartan committed
248
function taxonomy_save_term($edit) {
Dries's avatar
 
Dries committed
249 250
  if ($edit['tid'] && $edit['name']) {
    $data = array('name' => $edit['name'], 'description' => $edit['description'], 'weight' => $edit['weight']);
Dries's avatar
 
Dries committed
251

Dries's avatar
 
Dries committed
252
    db_query('UPDATE {term_data} SET '. _taxonomy_prepare_update($data) .' WHERE tid = %d', $edit['tid']);
Dries's avatar
 
Dries committed
253
    module_invoke_all('taxonomy', 'update', 'term', $edit);
Dries's avatar
 
Dries committed
254
    $message = t('The term %term has been updated.', array('%term' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
255
  }
Dries's avatar
 
Dries committed
256 257
  else if ($edit['tid']) {
    return taxonomy_del_term($edit['tid']);
Kjartan's avatar
Kjartan committed
258 259
  }
  else {
Dries's avatar
 
Dries committed
260 261
    $edit['tid'] = db_next_id('{term_data}_tid');
    $data = array('tid' => $edit['tid'], 'name' => $edit['name'], 'description' => $edit['description'], 'vid' => $edit['vid'], 'weight' => $edit['weight']);
Dries's avatar
 
Dries committed
262
    db_query('INSERT INTO {term_data} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
 
Dries committed
263
    module_invoke_all('taxonomy', 'insert', 'term', $edit);
Dries's avatar
 
Dries committed
264
    $message = t('Created new term %term.', array('%term' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
265
  }
Dries's avatar
 
Dries committed
266

Dries's avatar
 
Dries committed
267 268 269
  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
270
      if ($related_id != 0) {
Dries's avatar
 
Dries committed
271
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $edit['tid'], $related_id);
Dries's avatar
 
Dries committed
272
      }
Kjartan's avatar
Kjartan committed
273
    }
Kjartan's avatar
Kjartan committed
274
  }
Dries's avatar
 
Dries committed
275

Dries's avatar
 
Dries committed
276 277 278
  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $edit['tid']);
  if (!isset($edit['parent'])) {
    $edit['parent'] = 0;
Kjartan's avatar
Kjartan committed
279
  }
Dries's avatar
 
Dries committed
280 281 282
  if (is_array($edit['parent'])) {
    foreach ($edit['parent'] as $parent) {
      db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $parent);
Dries's avatar
 
Dries committed
283
    }
Kjartan's avatar
Kjartan committed
284 285
  }
  else {
Dries's avatar
 
Dries committed
286
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $edit['parent'][0]);
Dries's avatar
 
Dries committed
287 288
  }

Dries's avatar
 
Dries committed
289 290 291
  db_query('DELETE FROM {term_synonym} WHERE tid = %d', $edit['tid']);
  if ($edit['synonyms']) {
    foreach (explode ("\n", str_replace("\r", '', $edit['synonyms'])) as $synonym) {
Dries's avatar
 
Dries committed
292
      if ($synonym) {
Dries's avatar
 
Dries committed
293
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $edit['tid'], chop($synonym));
Dries's avatar
 
Dries committed
294
      }
Kjartan's avatar
Kjartan committed
295
    }
Dries's avatar
 
Dries committed
296
  }
Dries's avatar
 
Dries committed
297

Dries's avatar
 
Dries committed
298 299
  cache_clear_all();

Dries's avatar
 
Dries committed
300
  drupal_set_message($message);
Dries's avatar
 
Dries committed
301
  return $edit;
Kjartan's avatar
Kjartan committed
302
}
Dries's avatar
 
Dries committed
303

Kjartan's avatar
Kjartan committed
304
function taxonomy_del_term($tid) {
305 306 307 308 309 310 311 312 313 314 315 316 317 318
  $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
319

320
      $term = taxonomy_get_term($tid);
Dries's avatar
 
Dries committed
321

322 323 324 325 326
      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
327

328
      module_invoke_all('taxonomy', 'delete', 'term', $term);
Dries's avatar
Dries committed
329
      drupal_set_message(t('Deleted term "%name".', array('%name' => $term->name)));
330
    }
Dries's avatar
 
Dries committed
331

332 333
    $tids = $orphans;
  }
Dries's avatar
 
Dries committed
334

Dries's avatar
 
Dries committed
335
  cache_clear_all();
Dries's avatar
 
Dries committed
336 337 338 339 340
}

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

Dries's avatar
 
Dries committed
341 342 343 344
  $form .= form_hidden('confirm', 1);
  $form .= form_hidden('type', 'term');
  $form .= form_hidden('tid', $tid);
  $form .= form_submit(t('Delete'));
Dries's avatar
 
Dries committed
345

346 347 348
  $output = form(form_item(t('Delete term "%name" and all its children', array('%name' => $term->name)), $form, t('Are you sure you want to delete the term and all its children (if any)?')));

  return $output;
Kjartan's avatar
Kjartan committed
349
}
Dries's avatar
 
Dries committed
350

Dries's avatar
 
Dries committed
351 352 353
/**
 * Generate a tabular listing of administrative functions for vocabularies.
 */
Kjartan's avatar
Kjartan committed
354
function taxonomy_overview() {
Dries's avatar
 
Dries committed
355
  $header = array(t('Name'), t('Type'), array('data' => t('Operations'), 'colspan' => '3'));
Dries's avatar
 
Dries committed
356

Kjartan's avatar
Kjartan committed
357
  $vocabularies = taxonomy_get_vocabularies();
Dries's avatar
 
Dries committed
358

Kjartan's avatar
Kjartan committed
359
  foreach ($vocabularies as $vocabulary) {
Dries's avatar
 
Dries committed
360
    $types = array();
Dries's avatar
 
Dries committed
361 362 363
    foreach($vocabulary->nodes as $type) {
      $node_type = node_invoke($type, 'node_name');
      $types[] = $node_type ? $node_type : $type;
Dries's avatar
 
Dries committed
364
    }
Dries's avatar
 
Dries committed
365

Dries's avatar
 
Dries committed
366
    $rows[] = array($vocabulary->name, array('data' => implode(', ', $types), 'align' => 'center'), l(t('edit vocabulary'), "admin/taxonomy/edit/vocabulary/$vocabulary->vid"), l(t('add term'), "admin/taxonomy/add/term/$vocabulary->vid"), l(t('preview form'), "admin/taxonomy/preview/vocabulary/$vocabulary->vid"));
Kjartan's avatar
Kjartan committed
367

Dries's avatar
 
Dries committed
368
    $tree = taxonomy_get_tree($vocabulary->vid);
Kjartan's avatar
Kjartan committed
369
    if ($tree) {
Dries's avatar
 
Dries committed
370
      unset($data);
Kjartan's avatar
Kjartan committed
371
      foreach ($tree as $term) {
Dries's avatar
 
Dries committed
372
        $data .= _taxonomy_depth($term->depth) .' '. $term->name .' ('. l(t('edit term'), "admin/taxonomy/edit/term/$term->tid") .')<br />';
Dries's avatar
 
Dries committed
373
      }
Dries's avatar
 
Dries committed
374
      $rows[] = array(array('data' => $data, 'colspan' => '5'));
Dries's avatar
 
Dries committed
375 376 377
    }
  }

Dries's avatar
 
Dries committed
378 379 380 381
  if (!$rows) {
    $rows[] = array(array('data' => t('No categories available.'), 'colspan' => '5'));
  }

Dries's avatar
 
Dries committed
382
  return theme('table', $header, $rows);
Kjartan's avatar
Kjartan committed
383 384
}

Dries's avatar
 
Dries committed
385 386 387
/**
 * Generate a form element for selecting terms from a vocabulary.
 */
Dries's avatar
 
Dries committed
388
function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
Dries's avatar
 
Dries committed
389
  $vocabulary = taxonomy_get_vocabulary($vid);
390
  $help = ($help) ? $help : $vocabulary->help;
Kjartan's avatar
Kjartan committed
391 392 393 394
  if ($vocabulary->required) {
    $blank = 0;
  }
  else {
Dries's avatar
 
Dries committed
395
    $blank = '<'. t('none') .'>';
Kjartan's avatar
Kjartan committed
396
  }
Dries's avatar
 
Dries committed
397

Dries's avatar
 
Dries committed
398
  return _taxonomy_term_select($vocabulary->name, $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
Kjartan's avatar
Kjartan committed
399
}
Dries's avatar
 
Dries committed
400

Dries's avatar
 
Dries committed
401 402 403 404 405 406
/**
 * 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
407 408
function taxonomy_get_vocabularies($type = NULL) {

Kjartan's avatar
Kjartan committed
409
  if ($type) {
Dries's avatar
 
Dries committed
410
    $result = db_query("SELECT v.*, n.type FROM {vocabulary_node_types} n INNER JOIN {vocabulary} v ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", $type);
Kjartan's avatar
Kjartan committed
411 412
  }
  else {
Dries's avatar
 
Dries committed
413
    $result = db_query('SELECT v.*, n.type FROM {vocabulary_node_types} n INNER JOIN {vocabulary} v ON v.vid = n.vid ORDER BY v.weight, v.name');
Kjartan's avatar
Kjartan committed
414
  }
Dries's avatar
 
Dries committed
415

Kjartan's avatar
Kjartan committed
416
  $vocabularies = array();
Dries's avatar
 
Dries committed
417
  $node_types = array();
Kjartan's avatar
Kjartan committed
418
  while ($voc = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
419 420 421 422
    $node_types[$voc->vid][] = $voc->type;
    unset($voc->type);
    $voc->nodes = $node_types[$voc->vid];
    $vocabularies[$voc->vid] = $voc;
Dries's avatar
 
Dries committed
423
  }
Dries's avatar
 
Dries committed
424

Kjartan's avatar
Kjartan committed
425 426
  return $vocabularies;
}
Dries's avatar
 
Dries committed
427

Dries's avatar
 
Dries committed
428 429 430
/**
 * Generate a form for selecting terms to associate with a node.
 */
Dries's avatar
 
Dries committed
431
function taxonomy_node_form($type, $node = '', $help = NULL, $name = 'taxonomy') {
Kjartan's avatar
Kjartan committed
432 433 434
  if (!$node->taxonomy) {
    if ($node->nid) {
      $terms = array_keys(taxonomy_node_get_terms($node->nid));
Kjartan's avatar
Kjartan committed
435 436
    }
    else {
Kjartan's avatar
Kjartan committed
437
      $terms = 0;
Dries's avatar
 
Dries committed
438
    }
Kjartan's avatar
Kjartan committed
439 440 441 442
  }
  else {
    $terms = $node->taxonomy;
  }
Dries's avatar
 
Dries committed
443

Dries's avatar
 
Dries committed
444
  $c = db_query("SELECT v.*, n.type FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", $type);
Kjartan's avatar
Kjartan committed
445
  while ($vocabulary = db_fetch_object($c)) {
Dries's avatar
 
Dries committed
446
    $result[] = taxonomy_form($vocabulary->vid, $terms, $help, $name);
Dries's avatar
 
Dries committed
447
  }
Kjartan's avatar
Kjartan committed
448 449
  return $result ? $result : array();
}
Dries's avatar
 
Dries committed
450

Dries's avatar
 
Dries committed
451 452 453
/**
 * Determine whether a node mentions the name of a term.
 */
Kjartan's avatar
Kjartan committed
454
function taxonomy_node_has_term($nid, $tid) {
Dries's avatar
 
Dries committed
455
  $term_name = db_result(db_query('SELECT name FROM {term_data} WHERE tid = %d', $tid));
Dries's avatar
 
Dries committed
456

457
  return db_result(db_query("SELECT COUNT(n.nid) FROM {node} n WHERE n.nid = %d AND ((n.title LIKE '%%%s%%') OR (n.body LIKE '%%%s%%'))", $nid, $term_name, $term_name));
Kjartan's avatar
Kjartan committed
458 459
}

Dries's avatar
 
Dries committed
460 461 462 463 464
/**
 * Find all terms associated to the given node, within one vocabulary.
 */
function taxonomy_node_get_terms_by_vocabulary($nid, $vid, $key = 'tid') {
  $result = db_query('SELECT t.* FROM {term_data} t, {term_node} r WHERE t.tid = r.tid AND t.vid = %d AND r.nid = %d ORDER BY weight', $vid, $nid);
Kjartan's avatar
Kjartan committed
465 466 467
  $terms = array();
  while ($term = db_fetch_object($result)) {
    $terms[$term->$key] = $term;
Dries's avatar
 
Dries committed
468
  }
Kjartan's avatar
Kjartan committed
469 470 471
  return $terms;
}

Dries's avatar
 
Dries committed
472 473 474 475
/**
 * Find all terms associated to the given node.
 */
function taxonomy_node_get_terms($nid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
476
  static $terms;
Dries's avatar
 
Dries committed
477

Dries's avatar
 
Dries committed
478
  if (!isset($terms[$nid])) {
Dries's avatar
 
Dries committed
479
    $result = db_query('SELECT t.* FROM {term_data} t, {term_node} r WHERE r.tid = t.tid AND r.nid = %d ORDER BY weight, name', $nid);
Kjartan's avatar
Kjartan committed
480
    $terms[$nid] = array();
Dries's avatar
 
Dries committed
481
    while ($term = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
482
      $terms[$nid][$term->$key] = $term;
Dries's avatar
 
Dries committed
483 484
    }
  }
Kjartan's avatar
Kjartan committed
485 486
  return $terms[$nid];
}
Dries's avatar
 
Dries committed
487

Dries's avatar
 
Dries committed
488 489 490
/**
 * Save term associations for a given node.
 */
Kjartan's avatar
Kjartan committed
491 492
function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);
Dries's avatar
 
Dries committed
493

Kjartan's avatar
Kjartan committed
494
  if ($terms) {
Dries's avatar
 
Dries committed
495
    foreach ($terms as $term) {
496
      if ($term) {
Dries's avatar
 
Dries committed
497
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
498
      }
Dries's avatar
 
Dries committed
499 500
    }
  }
Kjartan's avatar
Kjartan committed
501
}
Dries's avatar
 
Dries committed
502

Dries's avatar
 
Dries committed
503 504 505
/**
 * Remove associations of a node to its terms.
 */
Kjartan's avatar
Kjartan committed
506
function taxonomy_node_delete($nid) {
Dries's avatar
 
Dries committed
507
  db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
Kjartan's avatar
Kjartan committed
508
}
Dries's avatar
 
Dries committed
509

Dries's avatar
 
Dries committed
510 511 512 513
/**
 * Find all term objects related to a given term ID.
 */
function taxonomy_get_related($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
514
  if ($tid) {
Dries's avatar
 
Dries committed
515
    $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
516 517 518
    $related = array();
    while ($term = db_fetch_object($result)) {
      $related[$term->$key] = $term;
Dries's avatar
 
Dries committed
519
    }
Kjartan's avatar
Kjartan committed
520
    return $related;
Dries's avatar
 
Dries committed
521
  }
Kjartan's avatar
Kjartan committed
522 523
  else {
    return array();
Dries's avatar
 
Dries committed
524
  }
Kjartan's avatar
Kjartan committed
525
}
Dries's avatar
 
Dries committed
526

Dries's avatar
 
Dries committed
527 528 529 530
/**
 * Find all parents of a given term ID.
 */
function taxonomy_get_parents($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
531
  if ($tid) {
Dries's avatar
 
Dries committed
532
    $result = db_query('SELECT t.* FROM {term_hierarchy} h, {term_data} t WHERE h.parent = t.tid AND h.tid = %d ORDER BY weight, name', $tid);
Kjartan's avatar
Kjartan committed
533 534 535
    $parents = array();
    while ($parent = db_fetch_object($result)) {
      $parents[$parent->$key] = $parent;
Dries's avatar
 
Dries committed
536
    }
Kjartan's avatar
Kjartan committed
537
    return $parents;
Dries's avatar
 
Dries committed
538
  }
Kjartan's avatar
Kjartan committed
539 540 541 542
  else {
    return array();
  }
}
Dries's avatar
 
Dries committed
543

Dries's avatar
 
Dries committed
544 545 546 547
/**
 * Find all ancestors of a given term ID.
 */
function taxonomy_get_parents_all($tid) {
Dries's avatar
 
Dries committed
548 549 550 551 552 553 554 555 556 557 558 559
  $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
560 561 562 563
/**
 * Find all children of a term ID.
 */
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
Kjartan's avatar
Kjartan committed
564
  if ($vid) {
Dries's avatar
 
Dries committed
565
    $result = db_query('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', $vid, $tid);
Dries's avatar
 
Dries committed
566
  }
Kjartan's avatar
Kjartan committed
567
  else {
Dries's avatar
 
Dries committed
568
    $result = db_query('SELECT t.* FROM {term_hierarchy} h, {term_data} t WHERE h.tid = t.tid AND parent = %d ORDER BY weight', $tid);
Kjartan's avatar
Kjartan committed
569 570 571 572 573 574 575
  }
  $children = array();
  while ($term = db_fetch_object($result)) {
    $children[$term->$key] = $term;
  }
  return $children;
}
Dries's avatar
 
Dries committed
576

Dries's avatar
 
Dries committed
577 578 579 580 581 582 583 584 585 586 587 588 589
/**
 * 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.
 *
Dries's avatar
 
Dries committed
590 591 592
 * @param $max_depth
 *   The number of levels of the tree to return. Leave NULL to return all levels.
 *
Dries's avatar
 
Dries committed
593 594 595 596 597
 * @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
598
  static $children, $parents, $terms;
Dries's avatar
 
Dries committed
599

Kjartan's avatar
Kjartan committed
600
  $depth++;
Dries's avatar
 
Dries committed
601

Dries's avatar
 
Dries committed
602 603 604 605
  // 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
606

Dries's avatar
 
Dries committed
607
    $result = db_query('SELECT t.*, parent FROM {term_data} t, {term_hierarchy} h WHERE t.tid = h.tid AND t.vid = %d ORDER BY weight, name', $vid);
Dries's avatar
 
Dries committed
608
    while ($term = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
609 610 611
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
Dries's avatar
 
Dries committed
612 613
    }
  }
Dries's avatar
 
Dries committed
614

Dries's avatar
 
Dries committed
615
  $max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
Dries's avatar
 
Dries committed
616 617
  if ($children[$vid][$parent]) {
    foreach ($children[$vid][$parent] as $child) {
Dries's avatar
 
Dries committed
618
      if ($max_depth > $depth) {
Dries's avatar
 
Dries committed
619 620 621 622 623
        $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];
Dries's avatar
 
Dries committed
624

Dries's avatar
 
Dries committed
625
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
Dries's avatar
 
Dries committed
626
      }
Dries's avatar
 
Dries committed
627
    }
Kjartan's avatar
Kjartan committed
628
  }
Dries's avatar
 
Dries committed
629

Dries's avatar
 
Dries committed
630
  return $tree ? $tree : array();
Kjartan's avatar
Kjartan committed
631
}
Dries's avatar
 
Dries committed
632

Dries's avatar
 
Dries committed
633 634 635
/**
 * Return an array of synonyms of the given term ID.
 */
Kjartan's avatar
Kjartan committed
636 637
function taxonomy_get_synonyms($tid) {
  if ($tid) {
Dries's avatar
 
Dries committed
638
    $result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid);
Kjartan's avatar
Kjartan committed
639
    while ($synonym = db_fetch_array($result)) {
Dries's avatar
 
Dries committed
640
      $synonyms[] = $synonym['name'];
Dries's avatar
 
Dries committed
641
    }
Kjartan's avatar
Kjartan committed
642
    return $synonyms ? $synonyms : array();
Dries's avatar
 
Dries committed
643
  }
Kjartan's avatar
Kjartan committed
644 645
  else {
    return array();
Dries's avatar
 
Dries committed
646
  }
Kjartan's avatar
Kjartan committed
647
}
Dries's avatar
 
Dries committed
648

Dries's avatar
 
Dries committed
649 650 651 652 653
/**
 * 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
654
}
Dries's avatar
 
Dries committed
655

Dries's avatar
 
Dries committed
656 657 658
/**
 * Given a term id, count the number of published nodes in it.
 */
Dries's avatar
 
Dries committed
659
function taxonomy_term_count_nodes($tid, $type = 0) {
Kjartan's avatar
Kjartan committed
660
  static $count;
Dries's avatar
 
Dries committed
661

Dries's avatar
 
Dries committed
662 663 664
  if (!isset($count[$type])) {
    // $type == 0 always evaluates true is $type is a string
    if (is_numeric($type)) {
Dries's avatar
 
Dries committed
665
      $result = db_query(node_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'));
Dries's avatar
 
Dries committed
666 667
    }
    else {
Dries's avatar
 
Dries committed
668
      $result = db_query(node_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);
Dries's avatar
 
Dries committed
669
    }
Kjartan's avatar
Kjartan committed
670
    while ($term = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
671
      $count[$type][$term->tid] = $term->c;
Dries's avatar
 
Dries committed
672 673 674
    }
  }

Kjartan's avatar
Kjartan committed
675
  foreach (_taxonomy_term_children($tid) as $c) {
Dries's avatar
 
Dries committed
676
    $children_count += taxonomy_term_count_nodes($c, $type);
Kjartan's avatar
Kjartan committed
677
  }
Dries's avatar
 
Dries committed
678
  return $count[$type][$tid] + $children_count;
Kjartan's avatar
Kjartan committed
679 680
}

Dries's avatar
 
Dries committed
681 682 683
/**
 * Helper for taxonomy_term_count_nodes().
 */
Kjartan's avatar
Kjartan committed
684 685
function _taxonomy_term_children($tid) {
  static $children;
Dries's avatar
 
Dries committed
686

Dries's avatar
 
Dries committed
687
  if (!isset($children)) {
Dries's avatar
 
Dries committed
688
    $result = db_query('SELECT tid, parent FROM {term_hierarchy}');
Kjartan's avatar
Kjartan committed
689 690
    while ($term = db_fetch_object($result)) {
      $children[$term->parent][] = $term->tid;
Dries's avatar
 
Dries committed
691
    }
Dries's avatar
 
Dries committed
692
  }
Kjartan's avatar
Kjartan committed
693 694
  return $children[$tid] ? $children[$tid] : array();
}
Dries's avatar
 
Dries committed
695

Dries's avatar
 
Dries committed
696
/**
Dries's avatar
 
Dries committed
697
 * Try to map a string to an existing term, as for glossary use.
Dries's avatar
 
Dries committed
698
 *
Dries's avatar
 
Dries committed
699 700 701 702 703 704 705 706
 * 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
707 708
 */
function taxonomy_get_term_by_name($name) {
Dries's avatar
 
Dries committed
709
  $db_result = db_query("SELECT * FROM {term_data} WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
Dries's avatar
 
Dries committed
710 711 712 713 714 715 716 717
  $result = array();
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }

  return $result;
}

Dries's avatar
 
Dries committed
718 719 720
/**
 * Return the vocabulary object matching a vocabulary ID.
 */
Kjartan's avatar
Kjartan committed
721
function taxonomy_get_vocabulary($vid) {
Dries's avatar
 
Dries committed
722 723 724 725 726 727 728 729 730 731
  $result = db_query('SELECT v.*, n.type FROM {vocabulary_node_types} n INNER JOIN {vocabulary} v 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;
    $vocabulary = $voc;
  }

  return $vocabulary;
Kjartan's avatar
Kjartan committed
732
}
Dries's avatar
 
Dries committed
733

Dries's avatar
 
Dries committed
734 735 736
/**
 * Return the term object matching a term ID.
 */
Kjartan's avatar
Kjartan committed
737 738
function taxonomy_get_term($tid) {
  // simple cache using a static var?
Dries's avatar
 
Dries committed
739
  return db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid));
Kjartan's avatar
Kjartan committed
740
}
Dries's avatar
 
Dries committed
741

Kjartan's avatar
Kjartan committed
742
function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
Dries's avatar
 
Dries committed
743
  $tree = taxonomy_get_tree($vocabulary_id);
Dries's avatar
Dries committed
744 745 746
  if ($blank) {
    $options[] = array('tid' => 0, 'name' => $blank);
  }
Kjartan's avatar
Kjartan committed
747 748 749
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
Dries's avatar
 
Dries committed
750
        $options[] = array('tid' => $term->tid, 'name' => _taxonomy_depth($term->depth, '-').$term->name);
Dries's avatar
 
Dries committed
751 752
      }
    }
Kjartan's avatar
Kjartan committed
753 754 755 756 757
    if (!$blank && !$value) {
      // required but without a predefined value, so set first as predefined
      $value = $tree[0]->tid;
    }
  }
Dries's avatar
 
Dries committed
758

Kjartan's avatar
Kjartan committed
759
  if (count($options) > 0) {
Dries's avatar
 
Dries committed
760
    foreach ($options as $option) {
Dries's avatar
 
Dries committed
761
      $select .= '<option value="'. $option['tid'] .'"'. (is_array($value) ? (in_array($option['tid'], $value) ? ' selected="selected"' : '') : ($option['tid'] == $value ? ' selected="selected"' : '')) .'>'. check_form($option['name']) .'</option>';
Kjartan's avatar
Kjartan committed
762
    }
Dries's avatar
 
Dries committed
763

Dries's avatar
 
Dries committed
764
    $size = min(12, count($options));
Dries's avatar
 
Dries committed
765

Dries's avatar
 
Dries committed
766
    return form_item($title, "<select name=\"edit[$name][]\"". ($multiple ? " multiple=\"multiple\" size=\"$size\"" : '') . ($extra ? " $extra" : '') .' class="'. _form_get_class('', false, _form_get_error($name)) ."\">$select</select>", $description, NULL, false, _form_get_error($name));
Dries's avatar
 
Dries committed
767
  }
Kjartan's avatar
Kjartan committed
768
}
Dries's avatar
 
Dries committed
769

Kjartan's avatar
Kjartan committed
770 771 772
function _taxonomy_depth($depth, $graphic = '--') {
  for ($n = 0; $n < $depth; $n++) {
    $result .= $graphic;
Dries's avatar
 
Dries committed
773
  }
Kjartan's avatar
Kjartan committed
774 775
  return $result;
}
Dries's avatar
 
Dries committed
776

Dries's avatar
 
Dries committed
777
function _taxonomy_prepare_update($data) {
Kjartan's avatar
Kjartan committed
778
  foreach ($data as $key => $value) {
Dries's avatar
 
Dries committed
779
    $q[] = "$key = '". str_replace('%', '%%', db_escape_string($value)) ."'";
Dries's avatar
 
Dries committed
780
  }
Dries's avatar
 
Dries committed
781
  $result = implode(', ', $q);
Kjartan's avatar
Kjartan committed
782 783
  return $result;
}
Dries's avatar
 
Dries committed
784

Dries's avatar
 
Dries committed
785
function _taxonomy_prepare_insert($data, $stage) {
Kjartan's avatar
Kjartan committed
786
  if ($stage == 1) {
Dries's avatar
 
Dries committed
787
    $result = implode(', ', array_keys($data));
Kjartan's avatar
Kjartan committed
788 789 790
  }
  else {
    foreach (array_values($data) as $value) {
Dries's avatar
 
Dries committed
791
      $q[] = "'". str_replace('%', '%%', db_escape_string($value)) ."'";
Dries's avatar
 
Dries committed
792
    }
Dries's avatar
 
Dries committed
793
    $result = implode(', ', $q);
Dries's avatar
 
Dries committed
794
  }
Kjartan's avatar
Kjartan committed
795 796
  return "($result)";
}
Dries's avatar
 
Dries committed
797

Dries's avatar
 
Dries committed
798 799 800
/**
 * Finds all nodes that match selected taxonomy conditions.
 *
Dries's avatar
 
Dries committed
801 802 803 804 805 806 807
 * @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
808 809 810 811 812 813
 * @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).
 * @return
 *   A resource identifier pointing to the query results.
 */
Dries's avatar
 
Dries committed
814 815 816 817 818 819 820 821 822 823
function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE) {
  if (count($tids) > 0) {
    // For each term ID, generate an array of descendant term IDs to the right depth.
    $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);
Steven Wittens's avatar
Steven Wittens committed
824
      $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
825
    }
Dries's avatar
 
Dries committed
826

Dries's avatar
 
Dries committed
827 828
    if ($operator == 'or') {
      $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
Dries's avatar
 
Dries committed
829 830
      $sql = 'SELECT n.nid, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC';
      $sql_count = 'SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1';
Dries's avatar
 
Dries committed
831 832 833 834 835 836 837 838
    }
    else {
      $joins = '';
      $wheres = '';
      foreach ($descendant_tids as $index => $tids) {
        $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
        $wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
      }
Dries's avatar