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

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

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

Dries's avatar
   
Dries committed
16
17
18
19
20
21
22
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

Kjartan's avatar
Kjartan committed
95
function taxonomy_form_vocabulary($edit = array()) {
Dries's avatar
Dries committed
96
  foreach (node_list() as $type) {
Dries's avatar
   
Dries committed
97
98
    $node_type = node_invoke($type, 'node_name');
    $nodes[$type] = $node_type ? $node_type : $type;
Kjartan's avatar
Kjartan committed
99
  }
Dries's avatar
   
Dries committed
100

Dries's avatar
   
Dries committed
101
102
103
  $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
104
  $form .= form_checkboxes(t('Types'), 'nodes', $edit['nodes'], $nodes, t('A list of node types you want to associate with this vocabulary.'), NULL, TRUE);
105
106
  $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
107
  $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
108
  $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
109
  $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
110
  $form .= form_submit(t('Submit'));
Dries's avatar
 
Dries committed
111

Dries's avatar
   
Dries committed
112
113
114
  if ($edit['vid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('vid', $edit['vid']);
Dries's avatar
 
Dries committed
115
116
  }

Kjartan's avatar
Kjartan committed
117
118
  return form($form);
}
Kjartan's avatar
Kjartan committed
119

Kjartan's avatar
Kjartan committed
120
function taxonomy_save_vocabulary($edit) {
Dries's avatar
   
Dries committed
121
122
  if (!$edit['nodes']) {
    $edit['nodes'] = array();
Dries's avatar
   
Dries committed
123
124
  }

125
  $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'],  'module' => isset($edit['module']) ? $edit['module'] : 'taxonomy');
Dries's avatar
   
Dries committed
126
  if ($edit['vid'] && $edit['name']) {
Dries's avatar
   
Dries committed
127
    db_query('UPDATE {vocabulary} SET '. _taxonomy_prepare_update($data) .' WHERE vid = %d', $edit['vid']);
Dries's avatar
   
Dries committed
128
129
130
131
    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
132
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
Dries's avatar
   
Dries committed
133
    $message = t('Updated vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Dries's avatar
 
Dries committed
134
  }
Dries's avatar
   
Dries committed
135
136
  else if ($edit['vid']) {
    $message = taxonomy_del_vocabulary($edit['vid']);
Kjartan's avatar
Kjartan committed
137
138
  }
  else {
Dries's avatar
   
Dries committed
139
    $data['vid'] = $edit['vid'] = db_next_id('{vocabulary}_vid');
Dries's avatar
   
Dries committed
140
    db_query('INSERT INTO {vocabulary} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
   
Dries committed
141
142
143
    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
144
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
Dries's avatar
   
Dries committed
145
    $message = t('Created new vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
146
  }
Dries's avatar
   
Dries committed
147
148

  cache_clear_all();
Dries's avatar
   
Dries committed
149

Dries's avatar
   
Dries committed
150
  drupal_set_message($message);
151

Dries's avatar
   
Dries committed
152
  return $edit;
Kjartan's avatar
Kjartan committed
153
}
Dries's avatar
 
Dries committed
154

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

Dries's avatar
   
Dries committed
158
  db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid);
Dries's avatar
   
Dries committed
159
  db_query('DELETE FROM {vocabulary_node_types} WHERE vid = %d', $vid);
Dries's avatar
   
Dries committed
160
  $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid);
Kjartan's avatar
Kjartan committed
161
162
  while ($term = db_fetch_object($result)) {
    taxonomy_del_term($term->tid);
Dries's avatar
 
Dries committed
163
  }
Dries's avatar
   
Dries committed
164

Dries's avatar
   
Dries committed
165
  module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
Dries's avatar
   
Dries committed
166

Dries's avatar
   
Dries committed
167
168
  cache_clear_all();

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

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

175
176
177
178
179
180
181
182
183
184
185
  $extra  = form_hidden('type', 'vocabulary');
  $extra .= form_hidden('vid', $vid);

  $output = theme('confirm',
                  t('Are you sure you want to delete the vocabulary %title?', array('%title' => $vocabulary->name)),
                  'admin/taxonomy',
                  t('Deleting a vocabulary will delete all the terms in it. This action cannot be undone.'),
                  t('Delete'),
                  t('Cancel'),
                  $extra);
  return $output;
Kjartan's avatar
Kjartan committed
186
}
Dries's avatar
 
Dries committed
187

Kjartan's avatar
Kjartan committed
188
function taxonomy_form_term($edit = array()) {
Dries's avatar
   
Dries committed
189
  $vocabulary_id = isset($edit['vid']) ? $edit['vid'] : arg(4);
Kjartan's avatar
Kjartan committed
190
  $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
Dries's avatar
   
Dries committed
191

Dries's avatar
   
Dries committed
192
193
  $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
194

Kjartan's avatar
Kjartan committed
195
  if ($vocabulary->hierarchy) {
Dries's avatar
   
Dries committed
196
197
    $parent = array_keys(taxonomy_get_parents($edit['tid']));
    $children = taxonomy_get_tree($vocabulary_id, $edit['tid']);
Dries's avatar
   
Dries committed
198

Dries's avatar
   
Dries committed
199
    // A term can't be the child of itself, nor of its children.
Dries's avatar
   
Dries committed
200
201
202
    foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
Dries's avatar
   
Dries committed
203
    $exclude[] = $edit['tid'];
Dries's avatar
   
Dries committed
204

Kjartan's avatar
Kjartan committed
205
    if ($vocabulary->hierarchy == 1) {
206
      $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
207
    }
Kjartan's avatar
Kjartan committed
208
    elseif ($vocabulary->hierarchy == 2) {
209
      $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
210
    }
Kjartan's avatar
Kjartan committed
211
  }
Dries's avatar
 
Dries committed
212

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

217
  $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
218
  $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
219
220
  $form .= form_hidden('vid', $vocabulary->vid);
  $form .= form_submit(t('Submit'));
Kjartan's avatar
Kjartan committed
221

Dries's avatar
   
Dries committed
222
223
224
  if ($edit['tid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('tid', $edit['tid']);
Dries's avatar
 
Dries committed
225
  }
226
227
228
  else {
    $form .= form_hidden('destination', $_GET['q']);
  }
Dries's avatar
 
Dries committed
229

Kjartan's avatar
Kjartan committed
230
231
  return form($form);
}
Dries's avatar
 
Dries committed
232

Kjartan's avatar
Kjartan committed
233
function taxonomy_save_term($edit) {
Dries's avatar
   
Dries committed
234
235
  if ($edit['tid'] && $edit['name']) {
    $data = array('name' => $edit['name'], 'description' => $edit['description'], 'weight' => $edit['weight']);
Dries's avatar
 
Dries committed
236

Dries's avatar
   
Dries committed
237
    db_query('UPDATE {term_data} SET '. _taxonomy_prepare_update($data) .' WHERE tid = %d', $edit['tid']);
Dries's avatar
   
Dries committed
238
    module_invoke_all('taxonomy', 'update', 'term', $edit);
Dries's avatar
   
Dries committed
239
    $message = t('The term %term has been updated.', array('%term' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
240
  }
Dries's avatar
   
Dries committed
241
242
  else if ($edit['tid']) {
    return taxonomy_del_term($edit['tid']);
Kjartan's avatar
Kjartan committed
243
244
  }
  else {
Dries's avatar
   
Dries committed
245
246
    $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
247
    db_query('INSERT INTO {term_data} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
   
Dries committed
248
    module_invoke_all('taxonomy', 'insert', 'term', $edit);
Dries's avatar
   
Dries committed
249
    $message = t('Created new term %term.', array('%term' => '<em>'. $edit['name'] .'</em>'));
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_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
255
      if ($related_id != 0) {
Dries's avatar
   
Dries committed
256
        db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $edit['tid'], $related_id);
Dries's avatar
 
Dries committed
257
      }
Kjartan's avatar
Kjartan committed
258
    }
Kjartan's avatar
Kjartan committed
259
  }
Dries's avatar
 
Dries committed
260

Dries's avatar
   
Dries committed
261
262
  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $edit['tid']);
  if (!isset($edit['parent'])) {
263
    $edit['parent'] = array(0);
Kjartan's avatar
Kjartan committed
264
  }
Dries's avatar
   
Dries committed
265
266
  if (is_array($edit['parent'])) {
    foreach ($edit['parent'] as $parent) {
267
268
269
270
271
272
273
274
      if (is_array($parent)) {
        foreach ($parent as $tid) {
          db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $tid);
        }
      }
      else {
        db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $edit['tid'], $parent);
      }
Dries's avatar
 
Dries committed
275
    }
Kjartan's avatar
Kjartan committed
276
  }
Dries's avatar
 
Dries committed
277

Dries's avatar
   
Dries committed
278
279
280
  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
281
      if ($synonym) {
Dries's avatar
   
Dries committed
282
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $edit['tid'], chop($synonym));
Dries's avatar
   
Dries committed
283
      }
Kjartan's avatar
Kjartan committed
284
    }
Dries's avatar
 
Dries committed
285
  }
Dries's avatar
   
Dries committed
286

Dries's avatar
   
Dries committed
287
288
  cache_clear_all();

Dries's avatar
   
Dries committed
289
  drupal_set_message($message);
Dries's avatar
   
Dries committed
290
  return $edit;
Kjartan's avatar
Kjartan committed
291
}
Dries's avatar
 
Dries committed
292

Kjartan's avatar
Kjartan committed
293
function taxonomy_del_term($tid) {
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  $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
308

309
      $term = taxonomy_get_term($tid);
Dries's avatar
   
Dries committed
310

311
312
313
314
315
      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
316

317
      module_invoke_all('taxonomy', 'delete', 'term', $term);
318
      drupal_set_message(t('Deleted term %name.', array('%name' => '<em>'. $term->name .'</em>')));
319
    }
Dries's avatar
   
Dries committed
320

321
322
    $tids = $orphans;
  }
Dries's avatar
   
Dries committed
323

Dries's avatar
   
Dries committed
324
  cache_clear_all();
Dries's avatar
   
Dries committed
325
326
327
328
329
}

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

330
331
  $extra  = form_hidden('type', 'term');
  $extra .= form_hidden('tid', $tid);
Dries's avatar
   
Dries committed
332

333
  $output = theme('confirm',
Steven Wittens's avatar
Steven Wittens committed
334
                  t('Are you sure you want to delete the term %title?', array('%title' => '<em>'. $term->name .'</em>')),
335
336
337
338
339
                  'admin/taxonomy',
                  t('Deleting a term will delete all its children if there are any. This action cannot be undone.'),
                  t('Delete'),
                  t('Cancel'),
                  $extra);
340
341

  return $output;
Kjartan's avatar
Kjartan committed
342
}
Dries's avatar
 
Dries committed
343

Dries's avatar
   
Dries committed
344
345
346
/**
 * Generate a tabular listing of administrative functions for vocabularies.
 */
Kjartan's avatar
Kjartan committed
347
function taxonomy_overview() {
Dries's avatar
   
Dries committed
348
  $header = array(t('Name'), t('Type'), array('data' => t('Operations'), 'colspan' => '3'));
Dries's avatar
   
Dries committed
349

Kjartan's avatar
Kjartan committed
350
  $vocabularies = taxonomy_get_vocabularies();
Dries's avatar
   
Dries committed
351

Kjartan's avatar
Kjartan committed
352
  foreach ($vocabularies as $vocabulary) {
353
354
355
356
357
358
    if ($vocabulary->module == 'taxonomy') { // only show vocabularies that can be configured through the vocabulary module
      $types = array();
      foreach($vocabulary->nodes as $type) {
        $node_type = node_invoke($type, 'node_name');
        $types[] = $node_type ? $node_type : $type;
      }
Dries's avatar
   
Dries committed
359

360
      $rows[] = array($vocabulary->name, implode(', ', $types), 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
361

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

Dries's avatar
   
Dries committed
373
374
375
376
  if (!$rows) {
    $rows[] = array(array('data' => t('No categories available.'), 'colspan' => '5'));
  }

Dries's avatar
   
Dries committed
377
  return theme('table', $header, $rows);
Kjartan's avatar
Kjartan committed
378
379
}

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

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

396
/**
397
398
399
 * Generate a set of options for selecting a term from all vocabularies. Can be
 * passed to form_select.
 */
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
function taxonomy_form_all($value = 0, $help = NULL, $name = 'taxonomy') {
  $vocabularies = taxonomy_get_vocabularies();
  $options = array();
  foreach ($vocabularies as $vid => $vocabulary) {
    $tree = taxonomy_get_tree($vid);
    $options[$vocabulary->name] = array();
    if ($tree) {
      foreach ($tree as $term) {
        $options[$vocabulary->name][$term->tid] = _taxonomy_depth($term->depth, '-') . $term->name;
      }
    }
  }
  return $options;
}

Dries's avatar
   
Dries committed
415
416
417
418
419
420
/**
 * 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
421
function taxonomy_get_vocabularies($type = NULL) {
Kjartan's avatar
Kjartan committed
422
  if ($type) {
423
    $result = db_query("SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", $type);
Kjartan's avatar
Kjartan committed
424
425
  }
  else {
426
    $result = db_query('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid ORDER BY v.weight, v.name');
Kjartan's avatar
Kjartan committed
427
  }
Dries's avatar
   
Dries committed
428

Kjartan's avatar
Kjartan committed
429
  $vocabularies = array();
Dries's avatar
   
Dries committed
430
  $node_types = array();
Kjartan's avatar
Kjartan committed
431
  while ($voc = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
432
433
434
435
    $node_types[$voc->vid][] = $voc->type;
    unset($voc->type);
    $voc->nodes = $node_types[$voc->vid];
    $vocabularies[$voc->vid] = $voc;
Dries's avatar
 
Dries committed
436
  }
Dries's avatar
   
Dries committed
437

Kjartan's avatar
Kjartan committed
438
439
  return $vocabularies;
}
Dries's avatar
 
Dries committed
440

Dries's avatar
   
Dries committed
441
442
443
/**
 * Generate a form for selecting terms to associate with a node.
 */
Dries's avatar
   
Dries committed
444
function taxonomy_node_form($type, $node = '', $help = NULL, $name = 'taxonomy') {
Kjartan's avatar
Kjartan committed
445
446
447
  if (!$node->taxonomy) {
    if ($node->nid) {
      $terms = array_keys(taxonomy_node_get_terms($node->nid));
Kjartan's avatar
Kjartan committed
448
449
    }
    else {
Kjartan's avatar
Kjartan committed
450
      $terms = 0;
Dries's avatar
 
Dries committed
451
    }
Kjartan's avatar
Kjartan committed
452
453
454
455
  }
  else {
    $terms = $node->taxonomy;
  }
Dries's avatar
 
Dries committed
456

Dries's avatar
   
Dries committed
457
  $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
458
  while ($vocabulary = db_fetch_object($c)) {
Dries's avatar
   
Dries committed
459
    $result[] = taxonomy_form($vocabulary->vid, $terms, $help, $name);
Dries's avatar
 
Dries committed
460
  }
Kjartan's avatar
Kjartan committed
461
462
  return $result ? $result : array();
}
Dries's avatar
 
Dries committed
463

Dries's avatar
   
Dries committed
464
465
466
467
468
/**
 * 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
469
470
471
  $terms = array();
  while ($term = db_fetch_object($result)) {
    $terms[$term->$key] = $term;
Dries's avatar
 
Dries committed
472
  }
Kjartan's avatar
Kjartan committed
473
474
475
  return $terms;
}

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

Dries's avatar
   
Dries committed
482
  if (!isset($terms[$nid])) {
Dries's avatar
   
Dries committed
483
    $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
484
    $terms[$nid] = array();
Dries's avatar
 
Dries committed
485
    while ($term = db_fetch_object($result)) {
Kjartan's avatar
Kjartan committed
486
      $terms[$nid][$term->$key] = $term;
Dries's avatar
 
Dries committed
487
488
    }
  }
Kjartan's avatar
Kjartan committed
489
490
  return $terms[$nid];
}
Dries's avatar
 
Dries committed
491

Dries's avatar
   
Dries committed
492
493
494
/**
 * Save term associations for a given node.
 */
Kjartan's avatar
Kjartan committed
495
496
function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);
Dries's avatar
 
Dries committed
497

498
  if (is_array($terms)) {
Dries's avatar
   
Dries committed
499
    foreach ($terms as $term) {
500
501
502
503
504
505
506
      if (is_array($term)) {
        foreach ($term as $tid) {
          if ($tid) {
            db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
          }
        }
      }
507
      else if ($term) {
Dries's avatar
   
Dries committed
508
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
509
      }
Dries's avatar
 
Dries committed
510
511
    }
  }
Kjartan's avatar
Kjartan committed
512
}
Dries's avatar
 
Dries committed
513

Dries's avatar
   
Dries committed
514
515
516
/**
 * Remove associations of a node to its terms.
 */
Kjartan's avatar
Kjartan committed
517
function taxonomy_node_delete($nid) {
Dries's avatar
   
Dries committed
518
  db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
Kjartan's avatar
Kjartan committed
519
}
Dries's avatar
 
Dries committed
520

Dries's avatar
   
Dries committed
521
522
523
524
/**
 * Find all term objects related to a given term ID.
 */
function taxonomy_get_related($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
525
  if ($tid) {
Dries's avatar
   
Dries committed
526
    $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
527
528
529
    $related = array();
    while ($term = db_fetch_object($result)) {
      $related[$term->$key] = $term;
Dries's avatar
 
Dries committed
530
    }
Kjartan's avatar
Kjartan committed
531
    return $related;
Dries's avatar
 
Dries committed
532
  }
Kjartan's avatar
Kjartan committed
533
534
  else {
    return array();
Dries's avatar
 
Dries committed
535
  }
Kjartan's avatar
Kjartan committed
536
}
Dries's avatar
 
Dries committed
537

Dries's avatar
   
Dries committed
538
539
540
541
/**
 * Find all parents of a given term ID.
 */
function taxonomy_get_parents($tid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
542
  if ($tid) {
Dries's avatar
   
Dries committed
543
    $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
544
545
546
    $parents = array();
    while ($parent = db_fetch_object($result)) {
      $parents[$parent->$key] = $parent;
Dries's avatar
   
Dries committed
547
    }
Kjartan's avatar
Kjartan committed
548
    return $parents;
Dries's avatar
 
Dries committed
549
  }
Kjartan's avatar
Kjartan committed
550
551
552
553
  else {
    return array();
  }
}
Dries's avatar
 
Dries committed
554

Dries's avatar
   
Dries committed
555
556
557
558
/**
 * Find all ancestors of a given term ID.
 */
function taxonomy_get_parents_all($tid) {
Dries's avatar
   
Dries committed
559
560
561
562
563
564
565
566
567
568
569
570
  $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
571
572
573
574
/**
 * Find all children of a term ID.
 */
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
Kjartan's avatar
Kjartan committed
575
  if ($vid) {
Dries's avatar
   
Dries committed
576
    $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
577
  }
Kjartan's avatar
Kjartan committed
578
  else {
Dries's avatar
   
Dries committed
579
    $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
580
581
582
583
584
585
586
  }
  $children = array();
  while ($term = db_fetch_object($result)) {
    $children[$term->$key] = $term;
  }
  return $children;
}
Dries's avatar
 
Dries committed
587

Dries's avatar
   
Dries committed
588
589
590
591
592
593
594
595
596
597
598
599
600
/**
 * 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
601
602
603
 * @param $max_depth
 *   The number of levels of the tree to return. Leave NULL to return all levels.
 *
Dries's avatar
   
Dries committed
604
605
606
607
608
 * @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
609
  static $children, $parents, $terms;
Dries's avatar
   
Dries committed
610

Kjartan's avatar
Kjartan committed
611
  $depth++;
Dries's avatar
   
Dries committed
612

Dries's avatar
   
Dries committed
613
614
615
616
  // 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
617

Dries's avatar
   
Dries committed
618
    $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
619
    while ($term = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
620
621
622
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
Dries's avatar
 
Dries committed
623
624
    }
  }
Dries's avatar
   
Dries committed
625

Dries's avatar
   
Dries committed
626
  $max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
Dries's avatar
   
Dries committed
627
628
  if ($children[$vid][$parent]) {
    foreach ($children[$vid][$parent] as $child) {
Dries's avatar
   
Dries committed
629
      if ($max_depth > $depth) {
Dries's avatar
   
Dries committed
630
631
632
633
634
        $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
635

Dries's avatar
   
Dries committed
636
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
Dries's avatar
   
Dries committed
637
      }
Dries's avatar
   
Dries committed
638
    }
Kjartan's avatar
Kjartan committed
639
  }
Dries's avatar
   
Dries committed
640

Dries's avatar
   
Dries committed
641
  return $tree ? $tree : array();
Kjartan's avatar
Kjartan committed
642
}
Dries's avatar
 
Dries committed
643

Dries's avatar
   
Dries committed
644
645
646
/**
 * Return an array of synonyms of the given term ID.
 */
Kjartan's avatar
Kjartan committed
647
648
function taxonomy_get_synonyms($tid) {
  if ($tid) {
Dries's avatar
   
Dries committed
649
    $result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid);
Kjartan's avatar
Kjartan committed
650
    while ($synonym = db_fetch_array($result)) {
Dries's avatar
   
Dries committed
651
      $synonyms[] = $synonym['name'];
Dries's avatar
 
Dries committed
652
    }
Kjartan's avatar
Kjartan committed
653
    return $synonyms ? $synonyms : array();
Dries's avatar
 
Dries committed
654
  }
Kjartan's avatar
Kjartan committed
655
656
  else {
    return array();
Dries's avatar
   
Dries committed
657
  }
Kjartan's avatar
Kjartan committed
658
}
Dries's avatar
   
Dries committed
659

Dries's avatar
   
Dries committed
660
661
662
663
664
/**
 * 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
665
}
Dries's avatar
   
Dries committed
666

Dries's avatar
   
Dries committed
667
668
669
/**
 * Given a term id, count the number of published nodes in it.
 */
Dries's avatar
   
Dries committed
670
function taxonomy_term_count_nodes($tid, $type = 0) {
Kjartan's avatar
Kjartan committed
671
  static $count;
Dries's avatar
   
Dries committed
672

Dries's avatar
   
Dries committed
673
674
675
  if (!isset($count[$type])) {
    // $type == 0 always evaluates true is $type is a string
    if (is_numeric($type)) {
676
      $result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.nid = n.nid WHERE n.status = 1 GROUP BY t.tid'));
Dries's avatar
   
Dries committed
677
678
    }
    else {
679
      $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t, {node} n WHERE t.nid = n.nid AND n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type);
Dries's avatar
   
Dries committed
680
    }
Kjartan's avatar
Kjartan committed
681
    while ($term = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
682
      $count[$type][$term->tid] = $term->c;
Dries's avatar
   
Dries committed
683
684
685
    }
  }

Kjartan's avatar
Kjartan committed
686
  foreach (_taxonomy_term_children($tid) as $c) {
Dries's avatar
   
Dries committed
687
    $children_count += taxonomy_term_count_nodes($c, $type);
Kjartan's avatar
Kjartan committed
688
  }
Dries's avatar
   
Dries committed
689
  return $count[$type][$tid] + $children_count;
Kjartan's avatar
Kjartan committed
690
691
}

Dries's avatar
   
Dries committed
692
693
694
/**
 * Helper for taxonomy_term_count_nodes().
 */
Kjartan's avatar
Kjartan committed
695
696
function _taxonomy_term_children($tid) {
  static $children;
Dries's avatar
   
Dries committed
697

Dries's avatar
   
Dries committed
698
  if (!isset($children)) {
Dries's avatar
   
Dries committed
699
    $result = db_query('SELECT tid, parent FROM {term_hierarchy}');
Kjartan's avatar
Kjartan committed
700
701
    while ($term = db_fetch_object($result)) {
      $children[$term->parent][] = $term->tid;
Dries's avatar
   
Dries committed
702
    }
Dries's avatar
 
Dries committed
703
  }
Kjartan's avatar
Kjartan committed
704
705
  return $children[$tid] ? $children[$tid] : array();
}
Dries's avatar
 
Dries committed
706

Dries's avatar
   
Dries committed
707
/**
Dries's avatar
   
Dries committed
708
 * Try to map a string to an existing term, as for glossary use.
Dries's avatar
   
Dries committed
709
 *
Dries's avatar
   
Dries committed
710
711
712
713
714
715
716
717
 * 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
718
719
 */
function taxonomy_get_term_by_name($name) {
Dries's avatar
   
Dries committed
720
  $db_result = db_query("SELECT * FROM {term_data} WHERE LOWER('%s') LIKE LOWER(name)", trim($name));
Dries's avatar
   
Dries committed
721
722
723
724
725
726
727
728
  $result = array();
  while ($term = db_fetch_object($db_result)) {
    $result[] = $term;
  }

  return $result;
}

Dries's avatar
   
Dries committed
729
730
731
/**
 * Return the vocabulary object matching a vocabulary ID.
 */
Kjartan's avatar
Kjartan committed
732
function taxonomy_get_vocabulary($vid) {
733
  $result = db_query('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE v.vid = %d ORDER BY v.weight, v.name', $vid);
Dries's avatar
   
Dries committed
734
735
736
737
738
739
740
741
742
  $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
743
}
Dries's avatar
 
Dries committed
744

Dries's avatar
   
Dries committed
745
746
747
/**
 * Return the term object matching a term ID.
 */
Kjartan's avatar
Kjartan committed
748
749
function taxonomy_get_term($tid) {
  // simple cache using a static var?
Dries's avatar
   
Dries committed
750
  return db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid));
Kjartan's avatar
Kjartan committed
751
}
Dries's avatar
 
Dries committed
752

Kjartan's avatar
Kjartan committed
753
function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
Dries's avatar
   
Dries committed
754
  $tree = taxonomy_get_tree($vocabulary_id);
755
756
  $options = array();

Dries's avatar
Dries committed
757
  if ($blank) {
758
    $options[0] = $blank;
Dries's avatar
Dries committed
759
  }
Kjartan's avatar
Kjartan committed
760
761
762
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
763
        $options[$term->tid] = _taxonomy_depth($term->depth, '-') . $term->name;
Dries's avatar
 
Dries committed
764
765
      }
    }
Kjartan's avatar
Kjartan committed
766
767
768
769
770
    if (!$blank && !$value) {
      // required but without a predefined value, so set first as predefined
      $value = $tree[0]->tid;
    }
  }
Dries's avatar
 
Dries committed
771

772
  return form_select($title, $name .'][', $value, $options, $description, $multiple ? 'size="'. min(12, count($options)) .'"' : 0, $multiple);
Kjartan's avatar
Kjartan committed
773
}
Dries's avatar
 
Dries committed
774

Kjartan's avatar
Kjartan committed
775
776
777
function _taxonomy_depth($depth, $graphic = '--') {
  for ($n = 0; $n < $depth; $n++) {
    $result .= $graphic;
Dries's avatar
 
Dries committed
778
  }
Kjartan's avatar
Kjartan committed
779
780
  return $result;
}
Dries's avatar
 
Dries committed
781

Dries's avatar
   
Dries committed
782
function _taxonomy_prepare_update($data) {
Kjartan's avatar
Kjartan committed
783
  foreach ($data as $key => $value) {
Dries's avatar
   
Dries committed
784
    $q[] = "$key = '". str_replace('%', '%%', db_escape_string($value)) ."'";
Dries's avatar
 
Dries committed
785
  }
Dries's avatar
   
Dries committed
786
  $result = implode(', ', $q);
Kjartan's avatar
Kjartan committed
787
788
  return $result;
}
Dries's avatar
 
Dries committed
789

Dries's avatar
   
Dries committed
790
function _taxonomy_prepare_insert($data, $stage) {
Kjartan's avatar
Kjartan committed
791
  if ($stage == 1) {
Dries's avatar
   
Dries committed
792
    $result = implode(', ', array_keys($data));
Kjartan's avatar
Kjartan committed
793
794
795
  }
  else {
    foreach (array_values($data) as $value) {
796
797
798
799
800
801
802
      //Escape strings, but not integers
      if (is_int($value)) {
        $q[] = $value;
      }
      else {
        $q[] = "'". str_replace('%', '%%', db_escape_string($value)) ."'";
      }
Dries's avatar
 
Dries committed
803
    }
Dries's avatar
   
Dries committed
804
    $result = implode(', ', $q);
Dries's avatar
 
Dries committed
805
  }
Kjartan's avatar
Kjartan committed
806
807
  return "($result)";
}
Dries's avatar
   
Dries committed
808

Dries's avatar
   
Dries committed
809
810
811
/**
 * Finds all nodes that match selected taxonomy conditions.
 *
Dries's avatar
   
Dries committed
812
813
814
815
816
817
818
 * @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
819
820
821
822
823
824
 * @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
825
826
827
828
829
830
831
832
833
834
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
835
      $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
836
    }
Dries's avatar
   
Dries committed
837

Dries's avatar
   
Dries committed
838
839
    if ($operator == 'or') {
      $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
840
      $sql = 'SELECT n.nid, n.sticky, n.title, 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';
Dries's avatar
   
Dries committed
841
      $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
842
843
844
845
846
847
848
849
    }
    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) .')';
      }
850
      $sql = 'SELECT n.nid, n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY n.sticky DESC, n.created DESC';
851
      $sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE n.status = 1 ' . $wheres;
852
    }
853
854
    $sql = db_rewrite_sql($sql);
    $sql_count = db_rewrite_sql($sql_count);
855
    if ($pager) {
Steven Wittens's avatar
Steven Wittens committed
856
      $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count);
857
858
859
860
    }
    else {
      $result = db_query_range($sql, 0, 15);
    }
Dries's avatar
   
Dries committed
861
862
863
864
865
  }

  return $result;
}

Dries's avatar
   
Dries committed
866
867
868
/**
 * Accepts the result of a pager_query() call, such as that performed by
 * taxonomy_select_nodes(), and formats each node along with a pager.
Dries's avatar
   
Dries committed
869
870
*/
function taxonomy_render_nodes($result) {
871
872
873
874
875
876
877
878
  if (db_num_rows($result) > 0) {
    while ($node = db_fetch_object($result)) {
      $output .= node_view(node_load(array('nid' => $node->nid)), 1);
    }
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
  }
  else {
    $output .= t('There are currently no posts in this category.');
Dries's avatar
   
Dries committed
879
  }
Dries's avatar
   
Dries committed
880
  return $output;
Dries's avatar
   
Dries committed
881
882
}

Dries's avatar
   
Dries committed
883
884
885
/**
 * Implementation of hook_nodeapi().
 */
Dries's avatar
   
Dries committed
886
887
function taxonomy_nodeapi($node, $op, $arg = 0) {
  switch ($op) {
Dries's avatar
   
Dries committed
888
    case 'insert':
Dries's avatar
   
Dries committed
889
890
      taxonomy_node_save($node->nid, $node->taxonomy);
      break;
Dries's avatar
   
Dries committed
891
    case 'update':
Dries's avatar
   
Dries committed
892
893
      taxonomy_node_save($node->nid, $node->taxonomy);
      break;
Dries's avatar
   
Dries committed
894
    case 'delete':
Dries's avatar
   
Dries committed
895
896
      taxonomy_node_delete($node->nid);
      break;
Dries's avatar
   
Dries committed
897
898
899
    case 'rss item':
      return taxonomy_rss_item($node);
      break;
Dries's avatar
   
Dries committed
900
901
902
  }
}

Dries's avatar
   
Dries committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
/**
 * Menu callback; displays all nodes associated with a term.
 */
function taxonomy_term_page($str_tids = '', $depth = 0, $op = 'page') {
  if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_tids)) {
    $operator = 'or';
    // The '+' character in a query string may be parsed as ' '.
    $tids = preg_split('/[+ ]/', $str_tids);
  }
  else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str_tids)) {
    $operator = 'and';
    $tids = explode(',', $str_tids);
  }
  else {
    drupal_not_found();
  }
Dries's avatar