taxonomy.module 39.1 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
function taxonomy_feed($taxonomy) {
  global $id, $type;
Dries's avatar
 
Dries committed
6

Dries's avatar
 
Dries committed
7
  if ($type == 'voc') {
Dries's avatar
 
Dries committed
8
    //TODO - vocabulary feed.
Kjartan's avatar
Kjartan committed
9 10
  }
  else {
Dries's avatar
 
Dries committed
11 12
    $result = taxonomy_select_nodes($taxonomy, 0);
    $term = taxonomy_get_term($taxonomy->tids[0]);
Dries's avatar
 
Dries committed
13 14 15
    $channel['link'] = url("taxonomy/view/$taxonomy->operator/$taxonomy->str_tids", NULL, NULL, TRUE);
    $channel['title'] = variable_get('site_name', 'drupal') .' - '. $term->name;
    $channel['description'] = $term->description;
Kjartan's avatar
Kjartan committed
16
    node_feed($result, $channel);
Dries's avatar
 
Dries committed
17
  }
Kjartan's avatar
Kjartan committed
18
}
Dries's avatar
 
Dries committed
19

Dries's avatar
 
Dries committed
20 21 22
/**
 * Implementation of hook_perm().
 */
Kjartan's avatar
Kjartan committed
23
function taxonomy_perm() {
Dries's avatar
 
Dries committed
24
  return array('administer taxonomy');
Kjartan's avatar
Kjartan committed
25
}
Dries's avatar
 
Dries committed
26

Dries's avatar
 
Dries committed
27 28 29 30 31 32 33 34 35 36 37
/**
 * 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
38
function taxonomy_link($type, $node = NULL) {
Dries's avatar
 
Dries committed
39
  if ($type == 'taxonomy terms' && $node != NULL) {
Kjartan's avatar
 
Kjartan committed
40
    $links = array();
Dries's avatar
 
Dries committed
41
    if (array_key_exists('taxonomy', $node)) {
Dries's avatar
 
Dries committed
42 43
      foreach ($node->taxonomy as $tid) {
        $term = taxonomy_get_term($tid);
44
        $links[] = l($term->name, "taxonomy/page/or/$term->tid", $term->description ? array ('title' => $term->description) : array());
Dries's avatar
 
Dries committed
45 46 47 48 49
      }
    }
    else {
      $links = array();
      foreach (taxonomy_node_get_terms($node->nid) as $term) {
50
        $links[] = l($term->name, "taxonomy/page/or/$term->tid", $term->description ? array ('title' => $term->description) : array());
Dries's avatar
 
Dries committed
51
      }
Dries's avatar
 
Dries committed
52 53 54 55

    }
    return $links;
  }
Kjartan's avatar
Kjartan committed
56 57
}

Dries's avatar
 
Dries committed
58 59 60 61 62 63 64 65
/**
 * 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
66 67
  $items[] = array('path' => 'admin/taxonomy/list', 'title' => t('list'),
    'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
Dries's avatar
 
Dries committed
68 69 70 71 72 73 74 75 76 77
  $items[] = array('path' => 'admin/taxonomy/add/vocabulary', 'title' => t('add vocabulary'),
    'callback' => 'taxonomy_admin',
    'access' => user_access('administer taxonomy'),
    'type' => MENU_LOCAL_TASK);
  $items[] = array('path' => 'taxonomy', 'title' => t('taxonomy'),
    'callback' => 'taxonomy_page',
    'access' => user_access('access content'),
    'type' => MENU_CALLBACK);
  return $items;
}
Dries's avatar
 
Dries committed
78

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
/**
 * 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)) {
      $items[] = l("$category->name ($category->count)", "taxonomy/page/or/$category->tid") .'<br />'. format_interval(time() - $category->updated) .' '. t('ago');
    }

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

Kjartan's avatar
Kjartan committed
102
function taxonomy_form_vocabulary($edit = array()) {
Dries's avatar
 
Dries committed
103
  foreach (node_list() as $type) {
Dries's avatar
 
Dries committed
104
    $nodetypes[$type] = node_invoke($type, 'node_name');
Kjartan's avatar
Kjartan committed
105
  }
Dries's avatar
 
Dries committed
106

Dries's avatar
 
Dries committed
107 108 109 110 111 112 113
  $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
114
  $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
115
  $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
116
  $form .= form_submit(t('Submit'));
Dries's avatar
 
Dries committed
117

Dries's avatar
 
Dries committed
118 119 120
  if ($edit['vid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('vid', $edit['vid']);
Dries's avatar
 
Dries committed
121 122
  }

Kjartan's avatar
Kjartan committed
123 124
  return form($form);
}
Kjartan's avatar
Kjartan committed
125

Kjartan's avatar
Kjartan committed
126
function taxonomy_save_vocabulary($edit) {
Dries's avatar
 
Dries committed
127 128
  if (!$edit['nodes']) {
    $edit['nodes'] = array();
Dries's avatar
 
Dries committed
129 130
  }

Dries's avatar
 
Dries committed
131 132 133 134 135
  $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']) {
    db_query('UPDATE {vocabulary} SET '. _prepare_update($data) .' WHERE vid = %d', $edit['vid']);
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
    $message = t('updated vocabulary "%name".', array('%name' => $edit['name']));
Dries's avatar
 
Dries committed
136
  }
Dries's avatar
 
Dries committed
137 138
  else if ($edit['vid']) {
    $message = taxonomy_del_vocabulary($edit['vid']);
Kjartan's avatar
Kjartan committed
139 140
  }
  else {
Dries's avatar
 
Dries committed
141 142 143 144
    $data['vid'] = $edit['vid'] = db_next_id('{vocabulary}_vid');
    db_query('INSERT INTO {vocabulary} '. _prepare_insert($data, 1) .' VALUES '. _prepare_insert($data, 2));
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
    $message = t('created new vocabulary "%name".', array('%name' => $edit['name']));
Kjartan's avatar
Kjartan committed
145
  }
Dries's avatar
 
Dries committed
146 147

  cache_clear_all();
Dries's avatar
 
Dries committed
148

Dries's avatar
 
Dries committed
149
  drupal_set_message($message);
Dries's avatar
 
Dries committed
150
  
Dries's avatar
 
Dries committed
151
  return $edit;
Kjartan's avatar
Kjartan committed
152
}
Dries's avatar
 
Dries committed
153

Kjartan's avatar
Kjartan committed
154
function taxonomy_del_vocabulary($vid) {
Dries's avatar
 
Dries committed
155 156
  $vocabulary = taxonomy_get_vocabulary($vid);

Dries's avatar
 
Dries committed
157 158
  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
159 160
  while ($term = db_fetch_object($result)) {
    taxonomy_del_term($term->tid);
Dries's avatar
 
Dries committed
161
  }
Dries's avatar
 
Dries committed
162

Dries's avatar
 
Dries committed
163
  module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
Dries's avatar
 
Dries committed
164

Dries's avatar
 
Dries committed
165 166
  cache_clear_all();

Dries's avatar
 
Dries committed
167
  return t('deleted vocabulary "%name".', array('%name' => $vocabulary->name));
Dries's avatar
 
Dries committed
168 169 170 171 172
}

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

Dries's avatar
 
Dries committed
173 174 175 176 177
  $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
178

Dries's avatar
 
Dries committed
179
  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
180
}
Dries's avatar
 
Dries committed
181

Kjartan's avatar
Kjartan committed
182
function taxonomy_form_term($edit = array()) {
Dries's avatar
 
Dries committed
183
  $vocabulary_id = isset($edit['vid']) ? $edit['vid'] : arg(4);
Kjartan's avatar
Kjartan committed
184
  $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
Dries's avatar
 
Dries committed
185

Dries's avatar
 
Dries committed
186 187
  $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
188

Kjartan's avatar
Kjartan committed
189
  if ($vocabulary->hierarchy) {
Dries's avatar
 
Dries committed
190 191
    $parent = array_keys(taxonomy_get_parents($edit['tid']));
    $children = taxonomy_get_tree($vocabulary_id, $edit['tid']);
Dries's avatar
 
Dries committed
192

Dries's avatar
 
Dries committed
193
    // A term can't be the child of itself, nor of its children.
Dries's avatar
 
Dries committed
194 195 196
    foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
Dries's avatar
 
Dries committed
197
    $exclude[] = $edit['tid'];
Dries's avatar
 
Dries committed
198

Kjartan's avatar
Kjartan committed
199
    if ($vocabulary->hierarchy == 1) {
Dries's avatar
 
Dries committed
200
      $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
201
    }
Kjartan's avatar
Kjartan committed
202
    elseif ($vocabulary->hierarchy == 2) {
Dries's avatar
 
Dries committed
203
      $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
204
    }
Kjartan's avatar
Kjartan committed
205
  }
Dries's avatar
 
Dries committed
206

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

Dries's avatar
 
Dries committed
211 212
  $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
213 214
  $form .= form_hidden('vid', $vocabulary->vid);
  $form .= form_submit(t('Submit'));
Kjartan's avatar
Kjartan committed
215

Dries's avatar
 
Dries committed
216 217 218
  if ($edit['tid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('tid', $edit['tid']);
Dries's avatar
 
Dries committed
219 220
  }

Kjartan's avatar
Kjartan committed
221 222
  return form($form);
}
Dries's avatar
 
Dries committed
223

Kjartan's avatar
Kjartan committed
224
function taxonomy_save_term($edit) {
Dries's avatar
 
Dries committed
225 226
  if ($edit['tid'] && $edit['name']) {
    $data = array('name' => $edit['name'], 'description' => $edit['description'], 'weight' => $edit['weight']);
Dries's avatar
 
Dries committed
227

Dries's avatar
 
Dries committed
228 229 230
    db_query('UPDATE {term_data} SET '. _prepare_update($data) .' WHERE tid = %d', $edit['tid']);
    module_invoke_all('taxonomy', 'update', 'term', $edit);
    $message = t('the term "%a" has been updated.', array('%a' => $edit['name']));
Kjartan's avatar
Kjartan committed
231
  }
Dries's avatar
 
Dries committed
232 233
  else if ($edit['tid']) {
    return taxonomy_del_term($edit['tid']);
Kjartan's avatar
Kjartan committed
234 235
  }
  else {
Dries's avatar
 
Dries committed
236 237 238 239 240
    $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']);
    db_query('INSERT INTO {term_data} '. _prepare_insert($data, 1) .' VALUES '. _prepare_insert($data, 2));
    module_invoke_all('taxonomy', 'insert', 'term', $edit);
    $message = t('created new term "%name".', array('%name' => $edit['name']));
Kjartan's avatar
Kjartan committed
241
  }
Dries's avatar
 
Dries committed
242

Dries's avatar
 
Dries committed
243 244 245
  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
246
      if ($related_id != 0) {
Dries's avatar
 
Dries committed
247
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $edit['tid'], $related_id);
Dries's avatar
 
Dries committed
248
      }
Kjartan's avatar
Kjartan committed
249
    }
Kjartan's avatar
Kjartan committed
250
  }
Dries's avatar
 
Dries committed
251

Dries's avatar
 
Dries committed
252 253 254
  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $edit['tid']);
  if (!isset($edit['parent'])) {
    $edit['parent'] = 0;
Kjartan's avatar
Kjartan committed
255
  }
Dries's avatar
 
Dries committed
256 257 258
  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
259
    }
Kjartan's avatar
Kjartan committed
260 261
  }
  else {
Dries's avatar
 
Dries committed
262
    db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $edit['parent'][0]);
Dries's avatar
 
Dries committed
263 264
  }

Dries's avatar
 
Dries committed
265 266 267
  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
268
      if ($synonym) {
Dries's avatar
 
Dries committed
269
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $edit['tid'], chop($synonym));
Dries's avatar
 
Dries committed
270
      }
Kjartan's avatar
Kjartan committed
271
    }
Dries's avatar
 
Dries committed
272
  }
Dries's avatar
 
Dries committed
273

Dries's avatar
 
Dries committed
274 275
  cache_clear_all();

Dries's avatar
 
Dries committed
276
  drupal_set_message($message);
Dries's avatar
 
Dries committed
277
  return $edit;
Kjartan's avatar
Kjartan committed
278
}
Dries's avatar
 
Dries committed
279

Kjartan's avatar
Kjartan committed
280
function taxonomy_del_term($tid) {
Dries's avatar
 
Dries committed
281 282
  $term = taxonomy_get_term($tid);

Dries's avatar
 
Dries committed
283 284 285 286 287
  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
288

Dries's avatar
 
Dries committed
289
  module_invoke_all('taxonomy', 'delete', 'term', $term);
Dries's avatar
 
Dries committed
290

Dries's avatar
 
Dries committed
291 292
  cache_clear_all();

Dries's avatar
 
Dries committed
293
  return t('deleted term "%name".', array('%name' => $term->name));
Dries's avatar
 
Dries committed
294 295 296 297 298
}

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

Dries's avatar
 
Dries committed
299 300 301 302 303
  $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
304

Dries's avatar
 
Dries committed
305
  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
306
}
Dries's avatar
 
Dries committed
307

Dries's avatar
 
Dries committed
308 309 310
/**
 * Generate a tabular listing of administrative functions for vocabularies.
 */
Kjartan's avatar
Kjartan committed
311
function taxonomy_overview() {
Dries's avatar
 
Dries committed
312
  $header = array(t('name'), t('node types'), array('data' => t('operations'), 'colspan' => 3));
Dries's avatar
 
Dries committed
313

Kjartan's avatar
Kjartan committed
314
  $vocabularies = taxonomy_get_vocabularies();
Dries's avatar
 
Dries committed
315

Kjartan's avatar
Kjartan committed
316 317
  foreach ($vocabularies as $vocabulary) {
    $links = array();
Dries's avatar
 
Dries committed
318
    $types = array();
Dries's avatar
 
Dries committed
319 320
    foreach(explode(',', $vocabulary->nodes) as $type) {
      $types[] = node_invoke($type, 'node_name');
Dries's avatar
 
Dries committed
321
    }
Dries's avatar
 
Dries committed
322
    $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
323

Dries's avatar
 
Dries committed
324
    $tree = taxonomy_get_tree($vocabulary->vid);
Kjartan's avatar
Kjartan committed
325
    if ($tree) {
Dries's avatar
 
Dries committed
326
      unset($data);
Kjartan's avatar
Kjartan committed
327
      foreach ($tree as $term) {
Dries's avatar
 
Dries committed
328
        $data .= _taxonomy_depth($term->depth) .' '. $term->name .' ('. l(t('edit term'), "admin/taxonomy/edit/term/$term->tid") .')<br />';
Dries's avatar
 
Dries committed
329
      }
Dries's avatar
 
Dries committed
330
      $rows[] = array(array('data' => $data, 'colspan' => 5));
Dries's avatar
 
Dries committed
331 332 333
    }
  }

Dries's avatar
 
Dries committed
334
  return theme('table', $header, $rows);
Kjartan's avatar
Kjartan committed
335 336
}

Dries's avatar
 
Dries committed
337 338 339
/**
 * Generate a form element for selecting terms from a vocabulary.
 */
Dries's avatar
 
Dries committed
340
function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
Dries's avatar
 
Dries committed
341
  $vocabulary = taxonomy_get_vocabulary($vid);
342
  $help = ($help) ? $help : $vocabulary->help;
Kjartan's avatar
Kjartan committed
343 344 345 346
  if ($vocabulary->required) {
    $blank = 0;
  }
  else {
Dries's avatar
 
Dries committed
347
    $blank = '<'. t('none') .'>';
Kjartan's avatar
Kjartan committed
348
  }
Dries's avatar
 
Dries committed
349

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

Dries's avatar
 
Dries committed
353 354 355 356 357 358 359
/**
 * 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
360
  if ($type) {
Dries's avatar
 
Dries committed
361
    $result = db_query("SELECT * FROM {vocabulary} WHERE nodes LIKE '%%%s%%' ORDER BY weight, name", $type);
Kjartan's avatar
Kjartan committed
362 363
  }
  else {
Dries's avatar
 
Dries committed
364
    $result = db_query('SELECT * FROM {vocabulary} ORDER BY weight, name');
Kjartan's avatar
Kjartan committed
365 366 367 368
  }
  $vocabularies = array();
  while ($voc = db_fetch_object($result)) {
    $vocabularies[$voc->$key] = $voc;
Dries's avatar
 
Dries committed
369
  }
Dries's avatar
 
Dries committed
370

Kjartan's avatar
Kjartan committed
371 372
  return $vocabularies;
}
Dries's avatar
 
Dries committed
373

Dries's avatar
 
Dries committed
374 375 376
/**
 * Generate a form for selecting terms to associate with a node.
 */
Dries's avatar
 
Dries committed
377
function taxonomy_node_form($type, $node = '', $help = NULL, $name = 'taxonomy') {
Kjartan's avatar
Kjartan committed
378 379 380
  if (!$node->taxonomy) {
    if ($node->nid) {
      $terms = array_keys(taxonomy_node_get_terms($node->nid));
Kjartan's avatar
Kjartan committed
381 382
    }
    else {
Kjartan's avatar
Kjartan committed
383
      $terms = 0;
Dries's avatar
 
Dries committed
384
    }
Kjartan's avatar
Kjartan committed
385 386 387 388
  }
  else {
    $terms = $node->taxonomy;
  }
Dries's avatar
 
Dries committed
389

Dries's avatar
 
Dries committed
390
  $c = db_query("SELECT * FROM {vocabulary} WHERE nodes LIKE '%%%s%%' ORDER BY weight, name", $type);
Kjartan's avatar
Kjartan committed
391
  while ($vocabulary = db_fetch_object($c)) {
Dries's avatar
 
Dries committed
392
    $result[] = taxonomy_form($vocabulary->vid, $terms, $help, $name);
Dries's avatar
 
Dries committed
393
  }
Kjartan's avatar
Kjartan committed
394 395
  return $result ? $result : array();
}
Dries's avatar
 
Dries committed
396

Dries's avatar
 
Dries committed
397 398 399
/**
 * Determine whether a node mentions the name of a term.
 */
Kjartan's avatar
Kjartan committed
400
function taxonomy_node_has_term($nid, $tid) {
Dries's avatar
 
Dries committed
401
  $term_name = db_result(db_query('SELECT name FROM {term_data} WHERE tid = %d', $tid));
Dries's avatar
 
Dries committed
402

Dries's avatar
 
Dries committed
403
  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
404 405
}

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

Dries's avatar
 
Dries committed
418 419 420 421
/**
 * Find all terms associated to the given node.
 */
function taxonomy_node_get_terms($nid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
422
  static $terms;
Dries's avatar
 
Dries committed
423

Dries's avatar
 
Dries committed
424
  if (!isset($terms[$nid])) {
Dries's avatar
 
Dries committed
425
    $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
426
    $terms[$nid] = array();
Dries's avatar
 
Dries committed
427
    while ($term = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
428
      $terms[$nid][$term->$key] = $term;
Dries's avatar
 
Dries committed
429 430
    }
  }
Kjartan's avatar
Kjartan committed
431 432
  return $terms[$nid];
}
Dries's avatar
 
Dries committed
433

Dries's avatar
 
Dries committed
434 435 436
/**
 * Save term associations for a given node.
 */
Kjartan's avatar
Kjartan committed
437 438
function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);
Dries's avatar
 
Dries committed
439

Kjartan's avatar
Kjartan committed
440
  if ($terms) {
Dries's avatar
 
Dries committed
441
    foreach ($terms as $term) {
442
      if ($term) {
Dries's avatar
 
Dries committed
443
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
444
      }
Dries's avatar
 
Dries committed
445 446
    }
  }
Kjartan's avatar
Kjartan committed
447
}
Dries's avatar
 
Dries committed
448

Dries's avatar
 
Dries committed
449 450 451
/**
 * Remove associations of a node to its terms.
 */
Kjartan's avatar
Kjartan committed
452
function taxonomy_node_delete($nid) {
Dries's avatar
 
Dries committed
453
  db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
Kjartan's avatar
Kjartan committed
454
}
Dries's avatar
 
Dries committed
455

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

Dries's avatar
 
Dries committed
473 474 475 476
/**
 * Find all parents of a given term ID.
 */
function taxonomy_get_parents($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
477
  if ($tid) {
Dries's avatar
 
Dries committed
478
    $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
479 480 481
    $parents = array();
    while ($parent = db_fetch_object($result)) {
      $parents[$parent->$key] = $parent;
Dries's avatar
 
Dries committed
482
    }
Kjartan's avatar
Kjartan committed
483
    return $parents;
Dries's avatar
 
Dries committed
484
  }
Kjartan's avatar
Kjartan committed
485 486 487 488
  else {
    return array();
  }
}
Dries's avatar
 
Dries committed
489

Dries's avatar
 
Dries committed
490 491 492 493
/**
 * Find all ancestors of a given term ID.
 */
function taxonomy_get_parents_all($tid) {
Dries's avatar
 
Dries committed
494 495 496 497 498 499 500 501 502 503 504 505
  $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
506 507 508 509
/**
 * Find all children of a term ID.
 */
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
Kjartan's avatar
Kjartan committed
510
  if ($vid) {
Dries's avatar
 
Dries committed
511
    $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
512
  }
Kjartan's avatar
Kjartan committed
513
  else {
Dries's avatar
 
Dries committed
514
    $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
515 516 517 518 519 520 521
  }
  $children = array();
  while ($term = db_fetch_object($result)) {
    $children[$term->$key] = $term;
  }
  return $children;
}
Dries's avatar
 
Dries committed
522

Dries's avatar
 
Dries committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
/**
 * 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.
 *
 * @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
541
  static $children, $parents, $terms;
Dries's avatar
 
Dries committed
542

Kjartan's avatar
Kjartan committed
543
  $depth++;
Dries's avatar
 
Dries committed
544

Dries's avatar
 
Dries committed
545 546 547 548
  // 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
549

Dries's avatar
 
Dries committed
550
    $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
551
    while ($term = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
552 553 554
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
Dries's avatar
 
Dries committed
555 556
    }
  }
Dries's avatar
 
Dries committed
557

Dries's avatar
 
Dries committed
558 559 560
  $max_depth = ($max_depth == '') ? count($children[$vid]) : $max_depth;
  if ($children[$vid][$parent]) {
    foreach ($children[$vid][$parent] as $child) {
Dries's avatar
 
Dries committed
561
      if ($max_depth > $depth) {
Dries's avatar
 
Dries committed
562 563 564 565 566
        $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
567

Dries's avatar
 
Dries committed
568
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
Dries's avatar
 
Dries committed
569
      }
Dries's avatar
 
Dries committed
570
    }
Kjartan's avatar
Kjartan committed
571
  }
Dries's avatar
 
Dries committed
572

Dries's avatar
 
Dries committed
573
  return $tree ? $tree : array();
Kjartan's avatar
Kjartan committed
574
}
Dries's avatar
 
Dries committed
575

Dries's avatar
 
Dries committed
576 577 578
/**
 * Return an array of synonyms of the given term ID.
 */
Kjartan's avatar
Kjartan committed
579 580
function taxonomy_get_synonyms($tid) {
  if ($tid) {
Dries's avatar
 
Dries committed
581
    $result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid);
Kjartan's avatar
Kjartan committed
582
    while ($synonym = db_fetch_array($result)) {
Dries's avatar
 
Dries committed
583
      $synonyms[] = $synonym['name'];
Dries's avatar
 
Dries committed
584
    }
Kjartan's avatar
Kjartan committed
585
    return $synonyms ? $synonyms : array();
Dries's avatar
 
Dries committed
586
  }
Kjartan's avatar
Kjartan committed
587 588
  else {
    return array();
Dries's avatar
 
Dries committed
589
  }
Kjartan's avatar
Kjartan committed
590
}
Dries's avatar
 
Dries committed
591

Dries's avatar
 
Dries committed
592 593 594 595 596
/**
 * 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
597
}
Dries's avatar
 
Dries committed
598

Dries's avatar
 
Dries committed
599 600 601
/**
 * Given a term id, count the number of published nodes in it.
 */
Dries's avatar
 
Dries committed
602
function taxonomy_term_count_nodes($tid, $type = 0) {
Kjartan's avatar
Kjartan committed
603
  static $count;
Dries's avatar
 
Dries committed
604

Dries's avatar
 
Dries committed
605 606 607
  if (!isset($count[$type])) {
    // $type == 0 always evaluates true is $type is a string
    if (is_numeric($type)) {
Dries's avatar
 
Dries committed
608
      $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
609 610
    }
    else {
Dries's avatar
 
Dries committed
611
      $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
612
    }
Kjartan's avatar
Kjartan committed
613
    while ($term = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
614
      $count[$type][$term->tid] = $term->c;
Dries's avatar
 
Dries committed
615 616 617
    }
  }

Kjartan's avatar
Kjartan committed
618
  foreach (_taxonomy_term_children($tid) as $c) {
Dries's avatar
 
Dries committed
619
    $children_count += taxonomy_term_count_nodes($c, $type);
Kjartan's avatar
Kjartan committed
620
  }
Dries's avatar
 
Dries committed
621
  return $count[$type][$tid] + $children_count;
Kjartan's avatar
Kjartan committed
622 623
}

Dries's avatar
 
Dries committed
624 625 626
/**
 * Helper for taxonomy_term_count_nodes().
 */
Kjartan's avatar
Kjartan committed
627 628
function _taxonomy_term_children($tid) {
  static $children;
Dries's avatar
 
Dries committed
629

Dries's avatar
 
Dries committed
630
  if (!isset($children)) {
Dries's avatar
 
Dries committed
631
    $result = db_query('SELECT tid, parent FROM {term_hierarchy}');
Kjartan's avatar
Kjartan committed
632 633
    while ($term = db_fetch_object($result)) {
      $children[$term->parent][] = $term->tid;
Dries's avatar
 
Dries committed
634
    }
Dries's avatar
 
Dries committed
635
  }
Kjartan's avatar
Kjartan committed
636 637
  return $children[$tid] ? $children[$tid] : array();
}
Dries's avatar
 
Dries committed
638

Dries's avatar
 
Dries committed
639
/**
Dries's avatar
 
Dries committed
640 641 642 643 644 645 646
 * 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
647
 *
Dries's avatar
 
Dries committed
648 649
 * @return
 *   An array of matching vocabulary objects.
Dries's avatar
 
Dries committed
650 651
 */
function taxonomy_get_vocabulary_by_name($name) {
Dries's avatar
 
Dries committed
652
  $db_result = db_query("SELECT * FROM {vocabulary} WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
Dries's avatar
 
Dries committed
653 654 655 656 657 658 659 660 661
  $result = array();
  while ($vocabulary = db_fetch_object($db_result)) {
    $result[] = $vocabulary;
  }

  return $result;
}

/**
Dries's avatar
 
Dries committed
662
 * Try to map a string to an existing term, as for glossary use.
Dries's avatar
 
Dries committed
663
 *
Dries's avatar
 
Dries committed
664 665 666 667 668 669 670 671
 * 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
672 673
 */
function taxonomy_get_term_by_name($name) {
Dries's avatar
 
Dries committed
674
  $db_result = db_query("SELECT * FROM {term_data} WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
Dries's avatar
 
Dries committed
675 676 677 678 679 680 681 682
  $result = array();
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }

  return $result;
}

Dries's avatar
 
Dries committed
683 684 685
/**
 * Return the vocabulary object matching a vocabulary ID.
 */
Kjartan's avatar
Kjartan committed
686
function taxonomy_get_vocabulary($vid) {
Dries's avatar
 
Dries committed
687
  return db_fetch_object(db_query('SELECT * FROM {vocabulary} WHERE vid = %d', $vid));
Kjartan's avatar
Kjartan committed
688
}
Dries's avatar
 
Dries committed
689

Dries's avatar
 
Dries committed
690 691 692
/**
 * Return the term object matching a term ID.
 */
Kjartan's avatar
Kjartan committed
693 694
function taxonomy_get_term($tid) {
  // simple cache using a static var?
Dries's avatar
 
Dries committed
695
  return db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid));
Kjartan's avatar
Kjartan committed
696
}
Dries's avatar
 
Dries committed
697

Kjartan's avatar
Kjartan committed
698
function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
Dries's avatar
 
Dries committed
699
  $tree = taxonomy_get_tree($vocabulary_id);
Dries's avatar
Dries committed
700 701 702
  if ($blank) {
    $options[] = array('tid' => 0, 'name' => $blank);
  }
Kjartan's avatar
Kjartan committed
703 704 705
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
Dries's avatar
 
Dries committed
706
        $options[] = array('tid' => $term->tid, 'name' => _taxonomy_depth($term->depth, '-').$term->name);
Dries's avatar
 
Dries committed
707 708
      }
    }
Kjartan's avatar
Kjartan committed
709 710 711 712 713
    if (!$blank && !$value) {
      // required but without a predefined value, so set first as predefined
      $value = $tree[0]->tid;
    }
  }
Dries's avatar
 
Dries committed
714

Kjartan's avatar
Kjartan committed
715
  if (count($options) > 0) {
Dries's avatar
 
Dries committed
716
    foreach ($options as $option) {
Dries's avatar
 
Dries committed
717
      $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
718
    }
Dries's avatar
 
Dries committed
719

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

Dries's avatar
 
Dries committed
722
    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
723
  }
Kjartan's avatar
Kjartan committed
724
}
Dries's avatar
 
Dries committed
725

Kjartan's avatar
Kjartan committed
726 727 728
function _taxonomy_depth($depth, $graphic = '--') {
  for ($n = 0; $n < $depth; $n++) {
    $result .= $graphic;
Dries's avatar
 
Dries committed
729
  }
Kjartan's avatar
Kjartan committed
730 731
  return $result;
}
Dries's avatar
 
Dries committed
732

Kjartan's avatar
Kjartan committed
733 734 735
function _prepare_update($data) {
  foreach ($data as $key => $value) {
    $q[] = "$key = '". check_query($value) ."'";
Dries's avatar
 
Dries committed
736
  }
Dries's avatar
 
Dries committed
737
  $result = implode(', ', $q);
Kjartan's avatar
Kjartan committed
738 739
  return $result;
}
Dries's avatar
 
Dries committed
740

Kjartan's avatar
Kjartan committed
741 742
function _prepare_insert($data, $stage) {
  if ($stage == 1) {
Dries's avatar
 
Dries committed
743
    $result = implode(', ', array_keys($data));
Kjartan's avatar
Kjartan committed
744 745 746 747
  }
  else {
    foreach (array_values($data) as $value) {
      $q[] = "'". check_query($value) ."'";
Dries's avatar
 
Dries committed
748
    }
Dries's avatar
 
Dries committed
749
    $result = implode(', ', $q);
Dries's avatar
 
Dries committed
750
  }
Kjartan's avatar
Kjartan committed
751 752
  return "($result)";
}
Dries's avatar
 
Dries committed
753

Dries's avatar
 
Dries committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/**
 * Finds all nodes that match selected taxonomy conditions.
 *
 * @param $taxonomy
 *   An object containing the conditions to match. The attributes of this
 *   object are:
 *   - "tids": An array of term IDs to match.
 *   - "str_tids": A comma-separated list of the same IDs.
 *   - "operator": How to interpret multiple IDs in the array. Can be
 *     "or" or "and".
 *
 * @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.
 */
function taxonomy_select_nodes($taxonomy, $pager = TRUE) {
773
  if ($taxonomy->str_tids) {
Dries's avatar
 
Dries committed
774
    if ($taxonomy->operator == 'or') {
Dries's avatar
 
Dries committed
775 776
      $sql = "SELECT DISTINCT(n.nid), n.title, n.type, n.created, n.changed, n.uid, n.sticky, n.created, u.name FROM {node} n ". node_access_join_sql() ." INNER JOIN {term_node} r ON n.nid = r.nid INNER JOIN {users} u ON n.uid = u.uid WHERE r.tid IN ($taxonomy->str_tids) AND n.status = 1 AND ". node_access_where_sql() .' ORDER BY sticky DESC, created DESC';
      $sql_count = "SELECT COUNT(DISTINCT(n.nid)) FROM {node} n ". node_access_join_sql() ." INNER JOIN {term_node} r ON n.nid = r.nid INNER JOIN {users} u ON n.uid = u.uid WHERE r.tid IN ($taxonomy->str_tids) AND n.status = 1 AND ". node_access_where_sql();
777 778
    }
    else {
Dries's avatar
 
Dries committed
779
      $sql = "SELECT DISTINCT(n.nid), n.title, n.type, n.created, n.changed, n.uid, u.name FROM {node} n ". node_access_join_sql() ." INNER JOIN {term_node} r ON n.nid = r.nid INNER JOIN {users} u ON n.uid = u.uid WHERE r.tid IN ($taxonomy->str_tids) AND n.status = 1 AND ". node_access_where_sql() ." GROUP BY n.nid, n.title, n.type, n.created, n.changed, n.uid, u.name HAVING COUNT(n.nid) = ". count($taxonomy->tids) ." ORDER BY sticky DESC, created DESC";
Dries's avatar
 
Dries committed
780

781
      // Special trick as we could not find anything better:
Dries's avatar
 
Dries committed
782
      $count = db_num_rows(db_query("SELECT DISTINCT(n.nid) FROM {node} n ". node_access_join_sql() ." INNER JOIN {term_node} r ON n.nid = r.nid WHERE r.tid IN ($taxonomy->str_tids) AND n.status = 1 AND ". node_access_where_sql() ." GROUP BY n.nid HAVING COUNT(n.nid) = ". count($taxonomy->tids)));
783 784
      $sql_count = "SELECT $count";
    }
Dries's avatar
 
Dries committed
785

786
    if ($pager) {
Dries's avatar
 
Dries committed