taxonomy.module 39.8 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
/**
 * Implementation of hook_perm().
 */
Kjartan's avatar
Kjartan committed
7
function taxonomy_perm() {
Dries's avatar
 
Dries committed
8
  return array('administer taxonomy');
Kjartan's avatar
Kjartan committed
9
}
Dries's avatar
 
Dries committed
10

Dries's avatar
 
Dries committed
11 12 13 14 15 16 17 18 19 20 21
/**
 * 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
22
function taxonomy_link($type, $node = NULL) {
Dries's avatar
 
Dries committed
23
  if ($type == 'taxonomy terms' && $node != NULL) {
Kjartan's avatar
 
Kjartan committed
24
    $links = array();
Dries's avatar
 
Dries committed
25
    if (array_key_exists('taxonomy', $node)) {
Dries's avatar
 
Dries committed
26 27
      foreach ($node->taxonomy as $tid) {
        $term = taxonomy_get_term($tid);
Dries's avatar
 
Dries committed
28
        $links[] = l($term->name, 'taxonomy/term/'. $term->tid, $term->description ? array ('title' => $term->description) : array());
Dries's avatar
 
Dries committed
29 30 31 32 33
      }
    }
    else {
      $links = array();
      foreach (taxonomy_node_get_terms($node->nid) as $term) {
Dries's avatar
 
Dries committed
34
        $links[] = l($term->name, 'taxonomy/term/'. $term->tid, $term->description ? array ('title' => $term->description) : array());
Dries's avatar
 
Dries committed
35
      }
Dries's avatar
 
Dries committed
36 37 38 39

    }
    return $links;
  }
Kjartan's avatar
Kjartan committed
40 41
}

Dries's avatar
 
Dries committed
42 43 44 45 46 47 48 49
/**
 * Implementation of hook_menu().
 */
function taxonomy_menu() {
  $items = array();
  $items[] = array('path' => 'admin/taxonomy', 'title' => t('categories'),
    'callback' => 'taxonomy_admin',
    'access' => user_access('administer taxonomy'));
Dries's avatar
 
Dries committed
50 51
  $items[] = array('path' => 'admin/taxonomy/list', 'title' => t('list'),
    'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
Dries's avatar
 
Dries committed
52 53 54 55
  $items[] = array('path' => 'admin/taxonomy/add/vocabulary', 'title' => t('add vocabulary'),
    'callback' => 'taxonomy_admin',
    'access' => user_access('administer taxonomy'),
    'type' => MENU_LOCAL_TASK);
Dries's avatar
 
Dries committed
56 57 58

  $items[] = array('path' => 'taxonomy/term', 'title' => t('taxonomy term'),
    'callback' => 'taxonomy_term_page',
Dries's avatar
 
Dries committed
59 60 61 62
    'access' => user_access('access content'),
    'type' => MENU_CALLBACK);
  return $items;
}
Dries's avatar
 
Dries committed
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77
/**
 * 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;
  }
  else if (user_access('access content')) {
    $result = db_query("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 ORDER BY updated DESC, d.name");
    $items = array();
    while ($category = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
78
      $items[] = l("$category->name ($category->count)", 'taxonomy/term/'. $category->tid) .'<br />'. format_interval(time() - $category->updated) .' '. t('ago');
79 80 81 82 83 84 85 86
    }

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

Kjartan's avatar
Kjartan committed
87
function taxonomy_form_vocabulary($edit = array()) {
Dries's avatar
 
Dries committed
88
  foreach (node_list() as $type) {
Dries's avatar
 
Dries committed
89
    $nodetypes[$type] = node_invoke($type, 'node_name');
Kjartan's avatar
Kjartan committed
90
  }
Dries's avatar
 
Dries committed
91

Dries's avatar
 
Dries committed
92 93 94 95 96 97 98
  $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.'));
  $form .= form_checkboxes(t('Types'), 'nodes', explode(',', $edit['nodes']), $nodetypes, t('A list of node types you want to associate with this vocabulary.'), NULL, TRUE);
  $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/taxonomy/help', 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/taxonomy/help', NULL, NULL, 'hierarchy'))));
  $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
99
  $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
100
  $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
101
  $form .= form_submit(t('Submit'));
Dries's avatar
 
Dries committed
102

Dries's avatar
 
Dries committed
103 104 105
  if ($edit['vid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('vid', $edit['vid']);
Dries's avatar
 
Dries committed
106 107
  }

Kjartan's avatar
Kjartan committed
108 109
  return form($form);
}
Kjartan's avatar
Kjartan committed
110

Kjartan's avatar
Kjartan committed
111
function taxonomy_save_vocabulary($edit) {
Dries's avatar
 
Dries committed
112 113
  if (!$edit['nodes']) {
    $edit['nodes'] = array();
Dries's avatar
 
Dries committed
114 115
  }

Dries's avatar
 
Dries committed
116 117
  $data = array('name' => $edit['name'], 'nodes' => implode(',', $edit['nodes']), 'description' => $edit['description'], 'help' => $edit['help'], 'multiple' => $edit['multiple'], 'required' => $edit['required'], 'hierarchy' => $edit['hierarchy'], 'relations' => $edit['relations'], 'weight' => $edit['weight']);
  if ($edit['vid'] && $edit['name']) {
Dries's avatar
 
Dries committed
118
    db_query('UPDATE {vocabulary} SET '. _taxonomy_prepare_update($data) .' WHERE vid = %d', $edit['vid']);
Dries's avatar
 
Dries committed
119 120
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
    $message = t('updated vocabulary "%name".', array('%name' => $edit['name']));
Dries's avatar
 
Dries committed
121
  }
Dries's avatar
 
Dries committed
122 123
  else if ($edit['vid']) {
    $message = taxonomy_del_vocabulary($edit['vid']);
Kjartan's avatar
Kjartan committed
124 125
  }
  else {
Dries's avatar
 
Dries committed
126
    $data['vid'] = $edit['vid'] = db_next_id('{vocabulary}_vid');
Dries's avatar
 
Dries committed
127
    db_query('INSERT INTO {vocabulary} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
 
Dries committed
128 129
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
    $message = t('created new vocabulary "%name".', array('%name' => $edit['name']));
Kjartan's avatar
Kjartan committed
130
  }
Dries's avatar
 
Dries committed
131 132

  cache_clear_all();
Dries's avatar
 
Dries committed
133

Dries's avatar
 
Dries committed
134
  drupal_set_message($message);
Dries's avatar
 
Dries committed
135
  
Dries's avatar
 
Dries committed
136
  return $edit;
Kjartan's avatar
Kjartan committed
137
}
Dries's avatar
 
Dries committed
138

Kjartan's avatar
Kjartan committed
139
function taxonomy_del_vocabulary($vid) {
Dries's avatar
 
Dries committed
140 141
  $vocabulary = taxonomy_get_vocabulary($vid);

Dries's avatar
 
Dries committed
142 143
  db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid);
  $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid);
Kjartan's avatar
Kjartan committed
144 145
  while ($term = db_fetch_object($result)) {
    taxonomy_del_term($term->tid);
Dries's avatar
 
Dries committed
146
  }
Dries's avatar
 
Dries committed
147

Dries's avatar
 
Dries committed
148
  module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
Dries's avatar
 
Dries committed
149

Dries's avatar
 
Dries committed
150 151
  cache_clear_all();

Dries's avatar
 
Dries committed
152
  return t('deleted vocabulary "%name".', array('%name' => $vocabulary->name));
Dries's avatar
 
Dries committed
153 154 155 156 157
}

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

Dries's avatar
 
Dries committed
158 159 160 161 162
  $form .= form_hidden('confirm', 1);
  $form .= form_hidden('type', 'vocabulary');
  $form .= form_hidden('vid', $vid);
  $form .= form_submit(t('Delete'));
  $form .= form_submit(t('Cancel'));
Dries's avatar
 
Dries committed
163

Dries's avatar
 
Dries committed
164
  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
165
}
Dries's avatar
 
Dries committed
166

Kjartan's avatar
Kjartan committed
167
function taxonomy_form_term($edit = array()) {
Dries's avatar
 
Dries committed
168
  $vocabulary_id = isset($edit['vid']) ? $edit['vid'] : arg(4);
Kjartan's avatar
Kjartan committed
169
  $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
Dries's avatar
 
Dries committed
170

Dries's avatar
 
Dries committed
171 172
  $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
173

Kjartan's avatar
Kjartan committed
174
  if ($vocabulary->hierarchy) {
Dries's avatar
 
Dries committed
175 176
    $parent = array_keys(taxonomy_get_parents($edit['tid']));
    $children = taxonomy_get_tree($vocabulary_id, $edit['tid']);
Dries's avatar
 
Dries committed
177

Dries's avatar
 
Dries committed
178
    // A term can't be the child of itself, nor of its children.
Dries's avatar
 
Dries committed
179 180 181
    foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
Dries's avatar
 
Dries committed
182
    $exclude[] = $edit['tid'];
Dries's avatar
 
Dries committed
183

Kjartan's avatar
Kjartan committed
184
    if ($vocabulary->hierarchy == 1) {
Dries's avatar
 
Dries committed
185
      $form .= _taxonomy_term_select(t('Parent'), 'parent', $parent, $vocabulary_id, l(t('Parent term'), 'admin/taxonomy/help', NULL, NULL, 'parent') .'.', 0, '<'. t('root') .'>', $exclude);
Dries's avatar
 
Dries committed
186
    }
Kjartan's avatar
Kjartan committed
187
    elseif ($vocabulary->hierarchy == 2) {
Dries's avatar
 
Dries committed
188
      $form .= _taxonomy_term_select(t('Parents'), 'parent', $parent, $vocabulary_id, l(t('Parent terms'), 'admin/taxonomy/help', NULL, NULL, 'parent') .'.', 1, '<'. t('root') .'>', $exclude);
Dries's avatar
 
Dries committed
189
    }
Kjartan's avatar
Kjartan committed
190
  }
Dries's avatar
 
Dries committed
191

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

Dries's avatar
 
Dries committed
196 197
  $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/taxonomy/help', NULL, NULL, 'synonyms'))));
  $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
198 199
  $form .= form_hidden('vid', $vocabulary->vid);
  $form .= form_submit(t('Submit'));
Kjartan's avatar
Kjartan committed
200

Dries's avatar
 
Dries committed
201 202 203
  if ($edit['tid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('tid', $edit['tid']);
Dries's avatar
 
Dries committed
204 205
  }

Kjartan's avatar
Kjartan committed
206 207
  return form($form);
}
Dries's avatar
 
Dries committed
208

Kjartan's avatar
Kjartan committed
209
function taxonomy_save_term($edit) {
Dries's avatar
 
Dries committed
210 211
  if ($edit['tid'] && $edit['name']) {
    $data = array('name' => $edit['name'], 'description' => $edit['description'], 'weight' => $edit['weight']);
Dries's avatar
 
Dries committed
212

Dries's avatar
 
Dries committed
213
    db_query('UPDATE {term_data} SET '. _taxonomy_prepare_update($data) .' WHERE tid = %d', $edit['tid']);
Dries's avatar
 
Dries committed
214 215
    module_invoke_all('taxonomy', 'update', 'term', $edit);
    $message = t('the term "%a" has been updated.', array('%a' => $edit['name']));
Kjartan's avatar
Kjartan committed
216
  }
Dries's avatar
 
Dries committed
217 218
  else if ($edit['tid']) {
    return taxonomy_del_term($edit['tid']);
Kjartan's avatar
Kjartan committed
219 220
  }
  else {
Dries's avatar
 
Dries committed
221 222
    $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
223
    db_query('INSERT INTO {term_data} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
 
Dries committed
224 225
    module_invoke_all('taxonomy', 'insert', 'term', $edit);
    $message = t('created new term "%name".', array('%name' => $edit['name']));
Kjartan's avatar
Kjartan committed
226
  }
Dries's avatar
 
Dries committed
227

Dries's avatar
 
Dries committed
228 229 230
  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
231
      if ($related_id != 0) {
Dries's avatar
 
Dries committed
232
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $edit['tid'], $related_id);
Dries's avatar
 
Dries committed
233
      }
Kjartan's avatar
Kjartan committed
234
    }
Kjartan's avatar
Kjartan committed
235
  }
Dries's avatar
 
Dries committed
236

Dries's avatar
 
Dries committed
237 238 239
  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $edit['tid']);
  if (!isset($edit['parent'])) {
    $edit['parent'] = 0;
Kjartan's avatar
Kjartan committed
240
  }
Dries's avatar
 
Dries committed
241 242 243
  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
244
    }
Kjartan's avatar
Kjartan committed
245 246
  }
  else {
Dries's avatar
 
Dries committed
247
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $edit['parent'][0]);
Dries's avatar
 
Dries committed
248 249
  }

Dries's avatar
 
Dries committed
250 251 252
  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
253
      if ($synonym) {
Dries's avatar
 
Dries committed
254
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $edit['tid'], chop($synonym));
Dries's avatar
 
Dries committed
255
      }
Kjartan's avatar
Kjartan committed
256
    }
Dries's avatar
 
Dries committed
257
  }
Dries's avatar
 
Dries committed
258

Dries's avatar
 
Dries committed
259 260
  cache_clear_all();

Dries's avatar
 
Dries committed
261
  drupal_set_message($message);
Dries's avatar
 
Dries committed
262
  return $edit;
Kjartan's avatar
Kjartan committed
263
}
Dries's avatar
 
Dries committed
264

Kjartan's avatar
Kjartan committed
265
function taxonomy_del_term($tid) {
Dries's avatar
 
Dries committed
266 267
  $term = taxonomy_get_term($tid);

Dries's avatar
 
Dries committed
268 269 270 271 272
  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
273

Dries's avatar
 
Dries committed
274
  module_invoke_all('taxonomy', 'delete', 'term', $term);
Dries's avatar
 
Dries committed
275

Dries's avatar
 
Dries committed
276 277
  cache_clear_all();

Dries's avatar
 
Dries committed
278
  return t('deleted term "%name".', array('%name' => $term->name));
Dries's avatar
 
Dries committed
279 280 281 282 283
}

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

Dries's avatar
 
Dries committed
284 285 286 287 288
  $form .= form_hidden('confirm', 1);
  $form .= form_hidden('type', 'term');
  $form .= form_hidden('tid', $tid);
  $form .= form_submit(t('Delete'));
  $form .= form_submit(t('Cancel'));
Dries's avatar
 
Dries committed
289

Dries's avatar
 
Dries committed
290
  return form(form_item(t('Delete term "%name"', array('%name' => $term->name)), $form, t('Are you sure you want to delete the term?')));
Kjartan's avatar
Kjartan committed
291
}
Dries's avatar
 
Dries committed
292

Dries's avatar
 
Dries committed
293 294 295
/**
 * Generate a tabular listing of administrative functions for vocabularies.
 */
Kjartan's avatar
Kjartan committed
296
function taxonomy_overview() {
Dries's avatar
 
Dries committed
297
  $header = array(t('name'), t('node types'), array('data' => t('operations'), 'colspan' => 3));
Dries's avatar
 
Dries committed
298

Kjartan's avatar
Kjartan committed
299
  $vocabularies = taxonomy_get_vocabularies();
Dries's avatar
 
Dries committed
300

Kjartan's avatar
Kjartan committed
301 302
  foreach ($vocabularies as $vocabulary) {
    $links = array();
Dries's avatar
 
Dries committed
303
    $types = array();
Dries's avatar
 
Dries committed
304 305
    foreach(explode(',', $vocabulary->nodes) as $type) {
      $types[] = node_invoke($type, 'node_name');
Dries's avatar
 
Dries committed
306
    }
Dries's avatar
 
Dries committed
307
    $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
308

Dries's avatar
 
Dries committed
309
    $tree = taxonomy_get_tree($vocabulary->vid);
Kjartan's avatar
Kjartan committed
310
    if ($tree) {
Dries's avatar
 
Dries committed
311
      unset($data);
Kjartan's avatar
Kjartan committed
312
      foreach ($tree as $term) {
Dries's avatar
 
Dries committed
313
        $data .= _taxonomy_depth($term->depth) .' '. $term->name .' ('. l(t('edit term'), "admin/taxonomy/edit/term/$term->tid") .')<br />';
Dries's avatar
 
Dries committed
314
      }
Dries's avatar
 
Dries committed
315
      $rows[] = array(array('data' => $data, 'colspan' => 5));
Dries's avatar
 
Dries committed
316 317 318
    }
  }

Dries's avatar
 
Dries committed
319
  return theme('table', $header, $rows);
Kjartan's avatar
Kjartan committed
320 321
}

Dries's avatar
 
Dries committed
322 323 324
/**
 * Generate a form element for selecting terms from a vocabulary.
 */
Dries's avatar
 
Dries committed
325
function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
Dries's avatar
 
Dries committed
326
  $vocabulary = taxonomy_get_vocabulary($vid);
327
  $help = ($help) ? $help : $vocabulary->help;
Kjartan's avatar
Kjartan committed
328 329 330 331
  if ($vocabulary->required) {
    $blank = 0;
  }
  else {
Dries's avatar
 
Dries committed
332
    $blank = '<'. t('none') .'>';
Kjartan's avatar
Kjartan committed
333
  }
Dries's avatar
 
Dries committed
334

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

Dries's avatar
 
Dries committed
338 339 340 341 342 343 344
/**
 * Return an array of all vocabulary objects.
 *
 * @param $type
 *   If set, return only those vocabularies associated with this node type.
 */
function taxonomy_get_vocabularies($type = '', $key = 'vid') {
Kjartan's avatar
Kjartan committed
345
  if ($type) {
Dries's avatar
 
Dries committed
346
    $result = db_query("SELECT * FROM {vocabulary} WHERE nodes LIKE '%%%s%%' ORDER BY weight, name", $type);
Kjartan's avatar
Kjartan committed
347 348
  }
  else {
Dries's avatar
 
Dries committed
349
    $result = db_query('SELECT * FROM {vocabulary} ORDER BY weight, name');
Kjartan's avatar
Kjartan committed
350 351 352 353
  }
  $vocabularies = array();
  while ($voc = db_fetch_object($result)) {
    $vocabularies[$voc->$key] = $voc;
Dries's avatar
 
Dries committed
354
  }
Dries's avatar
 
Dries committed
355

Kjartan's avatar
Kjartan committed
356 357
  return $vocabularies;
}
Dries's avatar
 
Dries committed
358

Dries's avatar
 
Dries committed
359 360 361
/**
 * Generate a form for selecting terms to associate with a node.
 */
Dries's avatar
 
Dries committed
362
function taxonomy_node_form($type, $node = '', $help = NULL, $name = 'taxonomy') {
Kjartan's avatar
Kjartan committed
363 364 365
  if (!$node->taxonomy) {
    if ($node->nid) {
      $terms = array_keys(taxonomy_node_get_terms($node->nid));
Kjartan's avatar
Kjartan committed
366 367
    }
    else {
Kjartan's avatar
Kjartan committed
368
      $terms = 0;
Dries's avatar
 
Dries committed
369
    }
Kjartan's avatar
Kjartan committed
370 371 372 373
  }
  else {
    $terms = $node->taxonomy;
  }
Dries's avatar
 
Dries committed
374

Dries's avatar
 
Dries committed
375
  $c = db_query("SELECT * FROM {vocabulary} WHERE nodes LIKE '%%%s%%' ORDER BY weight, name", $type);
Kjartan's avatar
Kjartan committed
376
  while ($vocabulary = db_fetch_object($c)) {
Dries's avatar
 
Dries committed
377
    $result[] = taxonomy_form($vocabulary->vid, $terms, $help, $name);
Dries's avatar
 
Dries committed
378
  }
Kjartan's avatar
Kjartan committed
379 380
  return $result ? $result : array();
}
Dries's avatar
 
Dries committed
381

Dries's avatar
 
Dries committed
382 383 384
/**
 * Determine whether a node mentions the name of a term.
 */
Kjartan's avatar
Kjartan committed
385
function taxonomy_node_has_term($nid, $tid) {
Dries's avatar
 
Dries committed
386
  $term_name = db_result(db_query('SELECT name FROM {term_data} WHERE tid = %d', $tid));
Dries's avatar
 
Dries committed
387

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

Dries's avatar
 
Dries committed
391 392 393 394 395
/**
 * 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
396 397 398
  $terms = array();
  while ($term = db_fetch_object($result)) {
    $terms[$term->$key] = $term;
Dries's avatar
 
Dries committed
399
  }
Kjartan's avatar
Kjartan committed
400 401 402
  return $terms;
}

Dries's avatar
 
Dries committed
403 404 405 406
/**
 * Find all terms associated to the given node.
 */
function taxonomy_node_get_terms($nid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
407
  static $terms;
Dries's avatar
 
Dries committed
408

Dries's avatar
 
Dries committed
409
  if (!isset($terms[$nid])) {
Dries's avatar
 
Dries committed
410
    $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
411
    $terms[$nid] = array();
Dries's avatar
 
Dries committed
412
    while ($term = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
413
      $terms[$nid][$term->$key] = $term;
Dries's avatar
 
Dries committed
414 415
    }
  }
Kjartan's avatar
Kjartan committed
416 417
  return $terms[$nid];
}
Dries's avatar
 
Dries committed
418

Dries's avatar
 
Dries committed
419 420 421
/**
 * Save term associations for a given node.
 */
Kjartan's avatar
Kjartan committed
422 423
function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);
Dries's avatar
 
Dries committed
424

Kjartan's avatar
Kjartan committed
425
  if ($terms) {
Dries's avatar
 
Dries committed
426
    foreach ($terms as $term) {
427
      if ($term) {
Dries's avatar
 
Dries committed
428
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
429
      }
Dries's avatar
 
Dries committed
430 431
    }
  }
Kjartan's avatar
Kjartan committed
432
}
Dries's avatar
 
Dries committed
433

Dries's avatar
 
Dries committed
434 435 436
/**
 * Remove associations of a node to its terms.
 */
Kjartan's avatar
Kjartan committed
437
function taxonomy_node_delete($nid) {
Dries's avatar
 
Dries committed
438
  db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
Kjartan's avatar
Kjartan committed
439
}
Dries's avatar
 
Dries committed
440

Dries's avatar
 
Dries committed
441 442 443 444
/**
 * Find all term objects related to a given term ID.
 */
function taxonomy_get_related($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
445
  if ($tid) {
Dries's avatar
 
Dries committed
446
    $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
447 448 449
    $related = array();
    while ($term = db_fetch_object($result)) {
      $related[$term->$key] = $term;
Dries's avatar
 
Dries committed
450
    }
Kjartan's avatar
Kjartan committed
451
    return $related;
Dries's avatar
 
Dries committed
452
  }
Kjartan's avatar
Kjartan committed
453 454
  else {
    return array();
Dries's avatar
 
Dries committed
455
  }
Kjartan's avatar
Kjartan committed
456
}
Dries's avatar
 
Dries committed
457

Dries's avatar
 
Dries committed
458 459 460 461
/**
 * Find all parents of a given term ID.
 */
function taxonomy_get_parents($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
462
  if ($tid) {
Dries's avatar
 
Dries committed
463
    $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
464 465 466
    $parents = array();
    while ($parent = db_fetch_object($result)) {
      $parents[$parent->$key] = $parent;
Dries's avatar
 
Dries committed
467
    }
Kjartan's avatar
Kjartan committed
468
    return $parents;
Dries's avatar
 
Dries committed
469
  }
Kjartan's avatar
Kjartan committed
470 471 472 473
  else {
    return array();
  }
}
Dries's avatar
 
Dries committed
474

Dries's avatar
 
Dries committed
475 476 477 478
/**
 * Find all ancestors of a given term ID.
 */
function taxonomy_get_parents_all($tid) {
Dries's avatar
 
Dries committed
479 480 481 482 483 484 485 486 487 488 489 490
  $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
491 492 493 494
/**
 * Find all children of a term ID.
 */
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
Kjartan's avatar
Kjartan committed
495
  if ($vid) {
Dries's avatar
 
Dries committed
496
    $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
497
  }
Kjartan's avatar
Kjartan committed
498
  else {
Dries's avatar
 
Dries committed
499
    $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
500 501 502 503 504 505 506
  }
  $children = array();
  while ($term = db_fetch_object($result)) {
    $children[$term->$key] = $term;
  }
  return $children;
}
Dries's avatar
 
Dries committed
507

Dries's avatar
 
Dries committed
508 509 510 511 512 513 514 515 516 517 518 519 520
/**
 * 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
521 522 523
 * @param $max_depth
 *   The number of levels of the tree to return. Leave NULL to return all levels.
 *
Dries's avatar
 
Dries committed
524 525 526 527 528
 * @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
529
  static $children, $parents, $terms;
Dries's avatar
 
Dries committed
530

Kjartan's avatar
Kjartan committed
531
  $depth++;
Dries's avatar
 
Dries committed
532

Dries's avatar
 
Dries committed
533 534 535 536
  // 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
537

Dries's avatar
 
Dries committed
538
    $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
539
    while ($term = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
540 541 542
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
Dries's avatar
 
Dries committed
543 544
    }
  }
Dries's avatar
 
Dries committed
545

Dries's avatar
 
Dries committed
546
  $max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
Dries's avatar
 
Dries committed
547 548
  if ($children[$vid][$parent]) {
    foreach ($children[$vid][$parent] as $child) {
Dries's avatar
 
Dries committed
549
      if ($max_depth > $depth) {
Dries's avatar
 
Dries committed
550 551 552 553 554
        $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
555

Dries's avatar
 
Dries committed
556
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
Dries's avatar
 
Dries committed
557
      }
Dries's avatar
 
Dries committed
558
    }
Kjartan's avatar
Kjartan committed
559
  }
Dries's avatar
 
Dries committed
560

Dries's avatar
 
Dries committed
561
  return $tree ? $tree : array();
Kjartan's avatar
Kjartan committed
562
}
Dries's avatar
 
Dries committed
563

Dries's avatar
 
Dries committed
564 565 566
/**
 * Return an array of synonyms of the given term ID.
 */
Kjartan's avatar
Kjartan committed
567 568
function taxonomy_get_synonyms($tid) {
  if ($tid) {
Dries's avatar
 
Dries committed
569
    $result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid);
Kjartan's avatar
Kjartan committed
570
    while ($synonym = db_fetch_array($result)) {
Dries's avatar
 
Dries committed
571
      $synonyms[] = $synonym['name'];
Dries's avatar
 
Dries committed
572
    }
Kjartan's avatar
Kjartan committed
573
    return $synonyms ? $synonyms : array();
Dries's avatar
 
Dries committed
574
  }
Kjartan's avatar
Kjartan committed
575 576
  else {
    return array();
Dries's avatar
 
Dries committed
577
  }
Kjartan's avatar
Kjartan committed
578
}
Dries's avatar
 
Dries committed
579

Dries's avatar
 
Dries committed
580 581 582 583 584
/**
 * 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
585
}
Dries's avatar
 
Dries committed
586

Dries's avatar
 
Dries committed
587 588 589
/**
 * Given a term id, count the number of published nodes in it.
 */
Dries's avatar
 
Dries committed
590
function taxonomy_term_count_nodes($tid, $type = 0) {
Kjartan's avatar
Kjartan committed
591
  static $count;
Dries's avatar
 
Dries committed
592

Dries's avatar
 
Dries committed
593 594 595
  if (!isset($count[$type])) {
    // $type == 0 always evaluates true is $type is a string
    if (is_numeric($type)) {
Dries's avatar
 
Dries committed
596
      $result = db_query('SELECT t.tid, COUNT(*) 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
597 598
    }
    else {
Dries's avatar
 
Dries committed
599
      $result = db_query("SELECT t.tid, COUNT(*) 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
600
    }
Kjartan's avatar
Kjartan committed
601
    while ($term = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
602
      $count[$type][$term->tid] = $term->c;
Dries's avatar
 
Dries committed
603 604 605
    }
  }

Kjartan's avatar
Kjartan committed
606
  foreach (_taxonomy_term_children($tid) as $c) {
Dries's avatar
 
Dries committed
607
    $children_count += taxonomy_term_count_nodes($c, $type);
Kjartan's avatar
Kjartan committed
608
  }
Dries's avatar
 
Dries committed
609
  return $count[$type][$tid] + $children_count;
Kjartan's avatar
Kjartan committed
610 611
}

Dries's avatar
 
Dries committed
612 613 614
/**
 * Helper for taxonomy_term_count_nodes().
 */
Kjartan's avatar
Kjartan committed
615 616
function _taxonomy_term_children($tid) {
  static $children;
Dries's avatar
 
Dries committed
617

Dries's avatar
 
Dries committed
618
  if (!isset($children)) {
Dries's avatar
 
Dries committed
619
    $result = db_query('SELECT tid, parent FROM {term_hierarchy}');
Kjartan's avatar
Kjartan committed
620 621
    while ($term = db_fetch_object($result)) {
      $children[$term->parent][] = $term->tid;
Dries's avatar
 
Dries committed
622
    }
Dries's avatar
 
Dries committed
623
  }
Kjartan's avatar
Kjartan committed
624 625
  return $children[$tid] ? $children[$tid] : array();
}
Dries's avatar
 
Dries committed
626

Dries's avatar
 
Dries committed
627
/**
Dries's avatar
 
Dries committed
628 629 630 631 632 633 634
 * Try to map a string to an existing vocabulary.
 *
 * Provides a case-insensitive and trimmed mapping, to maximize the
 * likelihood of a successful match.
 *
 * @param name
 *   Name of the vocabulary to search for.
Dries's avatar
 
Dries committed
635
 *
Dries's avatar
 
Dries committed
636 637
 * @return
 *   An array of matching vocabulary objects.
Dries's avatar
 
Dries committed
638 639
 */
function taxonomy_get_vocabulary_by_name($name) {
Dries's avatar
 
Dries committed
640
  $db_result = db_query("SELECT * FROM {vocabulary} WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
Dries's avatar
 
Dries committed
641 642 643 644 645 646 647 648 649
  $result = array();
  while ($vocabulary = db_fetch_object($db_result)) {
    $result[] = $vocabulary;
  }

  return $result;
}

/**
Dries's avatar
 
Dries committed
650
 * Try to map a string to an existing term, as for glossary use.
Dries's avatar
 
Dries committed
651
 *
Dries's avatar
 
Dries committed
652 653 654 655 656 657 658 659
 * 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
660 661
 */
function taxonomy_get_term_by_name($name) {
Dries's avatar
 
Dries committed
662
  $db_result = db_query("SELECT * FROM {term_data} WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
Dries's avatar
 
Dries committed
663 664 665 666 667 668 669 670
  $result = array();
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }

  return $result;
}

Dries's avatar
 
Dries committed
671 672 673
/**
 * Return the vocabulary object matching a vocabulary ID.
 */
Kjartan's avatar
Kjartan committed
674
function taxonomy_get_vocabulary($vid) {
Dries's avatar
 
Dries committed
675
  return db_fetch_object(db_query('SELECT * FROM {vocabulary} WHERE vid = %d', $vid));
Kjartan's avatar
Kjartan committed
676
}
Dries's avatar
 
Dries committed
677

Dries's avatar
 
Dries committed
678 679 680
/**
 * Return the term object matching a term ID.
 */
Kjartan's avatar
Kjartan committed
681 682
function taxonomy_get_term($tid) {
  // simple cache using a static var?
Dries's avatar
 
Dries committed
683
  return db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid));
Kjartan's avatar
Kjartan committed
684
}
Dries's avatar
 
Dries committed
685

Kjartan's avatar
Kjartan committed
686
function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
Dries's avatar
 
Dries committed
687
  $tree = taxonomy_get_tree($vocabulary_id);
Dries's avatar
Dries committed
688 689 690
  if ($blank) {
    $options[] = array('tid' => 0, 'name' => $blank);
  }
Kjartan's avatar
Kjartan committed
691 692 693
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
Dries's avatar
 
Dries committed
694
        $options[] = array('tid' => $term->tid, 'name' => _taxonomy_depth($term->depth, '-').$term->name);
Dries's avatar
 
Dries committed
695 696
      }
    }
Kjartan's avatar
Kjartan committed
697 698 699 700 701
    if (!$blank && !$value) {
      // required but without a predefined value, so set first as predefined
      $value = $tree[0]->tid;
    }
  }
Dries's avatar
 
Dries committed
702

Kjartan's avatar
Kjartan committed
703
  if (count($options) > 0) {
Dries's avatar
 
Dries committed
704
    foreach ($options as $option) {
Dries's avatar
 
Dries committed
705
      $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
706
    }
Dries's avatar
 
Dries committed
707

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

Dries's avatar
 
Dries committed
710
    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
711
  }
Kjartan's avatar
Kjartan committed
712
}
Dries's avatar
 
Dries committed
713

Kjartan's avatar
Kjartan committed
714 715 716
function _taxonomy_depth($depth, $graphic = '--') {
  for ($n = 0; $n < $depth; $n++) {
    $result .= $graphic;
Dries's avatar
 
Dries committed
717
  }
Kjartan's avatar
Kjartan committed
718 719
  return $result;
}
Dries's avatar
 
Dries committed
720

Dries's avatar
 
Dries committed
721
function _taxonomy_prepare_update($data) {
Kjartan's avatar
Kjartan committed
722 723
  foreach ($data as $key => $value) {
    $q[] = "$key = '". check_query($value) ."'";
Dries's avatar
 
Dries committed
724
  }
Dries's avatar
 
Dries committed
725
  $result = implode(', ', $q);
Kjartan's avatar
Kjartan committed
726 727
  return $result;
}
Dries's avatar
 
Dries committed
728

Dries's avatar
 
Dries committed
729
function _taxonomy_prepare_insert($data, $stage) {
Kjartan's avatar
Kjartan committed
730
  if ($stage == 1) {
Dries's avatar
 
Dries committed
731
    $result = implode(', ', array_keys($data));
Kjartan's avatar
Kjartan committed
732 733 734 735
  }
  else {
    foreach (array_values($data) as $value) {
      $q[] = "'". check_query($value) ."'";
Dries's avatar
 
Dries committed
736
    }
Dries's avatar
 
Dries committed
737
    $result = implode(', ', $q);
Dries's avatar
 
Dries committed
738
  }
Kjartan's avatar
Kjartan committed
739 740
  return "($result)";
}
Dries's avatar
 
Dries committed
741

Dries's avatar
 
Dries committed
742 743 744
/**
 * Finds all nodes that match selected taxonomy conditions.
 *
Dries's avatar
 
Dries committed
745 746 747 748 749 750 751
 * @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
752 753 754 755 756 757
 * @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
758 759 760 761 762 763 764 765 766 767 768
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);
      $descendant_tids[] = array_merge($tid, array_map('_taxonomy_get_tid_from_term', $tree));
769
    }
Dries's avatar
 
Dries committed
770

Dries's avatar
 
Dries committed
771 772 773 774 775 776 777 778 779 780 781 782 783 784
    if ($operator == 'or') {
      $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
      $sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql() .' ORDER BY n.sticky DESC, n.created DESC';
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql();
    }
    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) .')';
      }
      $sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres .' ORDER BY n.sticky DESC, n.created DESC';
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres;
785
    }
Dries's avatar
 
Dries committed
786