taxonomy.module 40.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
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);
33
        $links[] = l($term->name, 'taxonomy/term/'. $term->tid, $term->description ? array ('title' => check_output($term->description)) : array());
Dries's avatar
   
Dries committed
34
35
36
37
38
      }
    }
    else {
      $links = array();
      foreach (taxonomy_node_get_terms($node->nid) as $term) {
39
        $links[] = l($term->name, 'taxonomy/term/'. $term->tid, $term->description ? array ('title' => check_output($term->description)) : array());
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
50
51
52
53
54
/**
 * 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
55
56
  $items[] = array('path' => 'admin/taxonomy/list', 'title' => t('list'),
    'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
Dries's avatar
   
Dries committed
57
58
59
60
  $items[] = array('path' => 'admin/taxonomy/add/vocabulary', 'title' => t('add vocabulary'),
    'callback' => 'taxonomy_admin',
    'access' => user_access('administer taxonomy'),
    'type' => MENU_LOCAL_TASK);
Dries's avatar
   
Dries committed
61
62
63

  $items[] = array('path' => 'taxonomy/term', 'title' => t('taxonomy term'),
    'callback' => 'taxonomy_term_page',
Dries's avatar
   
Dries committed
64
65
66
67
    'access' => user_access('access content'),
    'type' => MENU_CALLBACK);
  return $items;
}
Dries's avatar
   
Dries committed
68

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

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

Kjartan's avatar
Kjartan committed
92
function taxonomy_form_vocabulary($edit = array()) {
Dries's avatar
   
Dries committed
93
  foreach (node_list() as $type) {
Dries's avatar
   
Dries committed
94
    $nodetypes[$type] = node_invoke($type, 'node_name');
Kjartan's avatar
Kjartan committed
95
  }
Dries's avatar
   
Dries committed
96

Dries's avatar
   
Dries committed
97
98
99
100
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.'));
  $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
104
  $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
105
  $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
106
  $form .= form_submit(t('Submit'));
Dries's avatar
   
Dries committed
107

Dries's avatar
   
Dries committed
108
109
110
  if ($edit['vid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('vid', $edit['vid']);
Dries's avatar
   
Dries committed
111
112
  }

Kjartan's avatar
Kjartan committed
113
114
  return form($form);
}
Kjartan's avatar
Kjartan committed
115

Kjartan's avatar
Kjartan committed
116
function taxonomy_save_vocabulary($edit) {
Dries's avatar
   
Dries committed
117
118
  if (!$edit['nodes']) {
    $edit['nodes'] = array();
Dries's avatar
   
Dries committed
119
120
  }

Dries's avatar
   
Dries committed
121
122
  $data = array('name' => $edit['name'], 'nodes' => implode(',', $edit['nodes']), 'description' => $edit['description'], 'help' => $edit['help'], 'multiple' => $edit['multiple'], 'required' => $edit['required'], 'hierarchy' => $edit['hierarchy'], 'relations' => $edit['relations'], 'weight' => $edit['weight']);
  if ($edit['vid'] && $edit['name']) {
Dries's avatar
   
Dries committed
123
    db_query('UPDATE {vocabulary} SET '. _taxonomy_prepare_update($data) .' WHERE vid = %d', $edit['vid']);
Dries's avatar
   
Dries committed
124
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
Dries's avatar
   
Dries committed
125
    $message = t('Updated vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Dries's avatar
   
Dries committed
126
  }
Dries's avatar
   
Dries committed
127
128
  else if ($edit['vid']) {
    $message = taxonomy_del_vocabulary($edit['vid']);
Kjartan's avatar
Kjartan committed
129
130
  }
  else {
Dries's avatar
   
Dries committed
131
    $data['vid'] = $edit['vid'] = db_next_id('{vocabulary}_vid');
Dries's avatar
   
Dries committed
132
    db_query('INSERT INTO {vocabulary} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
   
Dries committed
133
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
Dries's avatar
   
Dries committed
134
    $message = t('Created new vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
135
  }
Dries's avatar
   
Dries committed
136
137

  cache_clear_all();
Dries's avatar
   
Dries committed
138

Dries's avatar
   
Dries committed
139
  drupal_set_message($message);
140

Dries's avatar
   
Dries committed
141
  return $edit;
Kjartan's avatar
Kjartan committed
142
}
Dries's avatar
   
Dries committed
143

Kjartan's avatar
Kjartan committed
144
function taxonomy_del_vocabulary($vid) {
Dries's avatar
   
Dries committed
145
146
  $vocabulary = taxonomy_get_vocabulary($vid);

Dries's avatar
   
Dries committed
147
148
  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
149
150
  while ($term = db_fetch_object($result)) {
    taxonomy_del_term($term->tid);
Dries's avatar
   
Dries committed
151
  }
Dries's avatar
   
Dries committed
152

Dries's avatar
   
Dries committed
153
  module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary);
Dries's avatar
   
Dries committed
154

Dries's avatar
   
Dries committed
155
156
  cache_clear_all();

Dries's avatar
   
Dries committed
157
  return t('deleted vocabulary "%name".', array('%name' => $vocabulary->name));
Dries's avatar
   
Dries committed
158
159
160
161
162
}

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

Dries's avatar
   
Dries committed
163
164
165
166
167
  $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
168

Dries's avatar
   
Dries committed
169
  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
170
}
Dries's avatar
   
Dries committed
171

Kjartan's avatar
Kjartan committed
172
function taxonomy_form_term($edit = array()) {
Dries's avatar
   
Dries committed
173
  $vocabulary_id = isset($edit['vid']) ? $edit['vid'] : arg(4);
Kjartan's avatar
Kjartan committed
174
  $vocabulary = taxonomy_get_vocabulary($vocabulary_id);
Dries's avatar
   
Dries committed
175

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

Kjartan's avatar
Kjartan committed
179
  if ($vocabulary->hierarchy) {
Dries's avatar
   
Dries committed
180
181
    $parent = array_keys(taxonomy_get_parents($edit['tid']));
    $children = taxonomy_get_tree($vocabulary_id, $edit['tid']);
Dries's avatar
   
Dries committed
182

Dries's avatar
   
Dries committed
183
    // A term can't be the child of itself, nor of its children.
Dries's avatar
   
Dries committed
184
185
186
    foreach ($children as $child) {
      $exclude[] = $child->tid;
    }
Dries's avatar
   
Dries committed
187
    $exclude[] = $edit['tid'];
Dries's avatar
   
Dries committed
188

Kjartan's avatar
Kjartan committed
189
    if ($vocabulary->hierarchy == 1) {
Dries's avatar
   
Dries committed
190
      $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
191
    }
Kjartan's avatar
Kjartan committed
192
    elseif ($vocabulary->hierarchy == 2) {
Dries's avatar
   
Dries committed
193
      $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
194
    }
Kjartan's avatar
Kjartan committed
195
  }
Dries's avatar
   
Dries committed
196

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

Dries's avatar
   
Dries committed
201
202
  $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
203
204
  $form .= form_hidden('vid', $vocabulary->vid);
  $form .= form_submit(t('Submit'));
Kjartan's avatar
Kjartan committed
205

Dries's avatar
   
Dries committed
206
207
208
  if ($edit['tid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('tid', $edit['tid']);
Dries's avatar
   
Dries committed
209
210
  }

Kjartan's avatar
Kjartan committed
211
212
  return form($form);
}
Dries's avatar
   
Dries committed
213

Kjartan's avatar
Kjartan committed
214
function taxonomy_save_term($edit) {
Dries's avatar
   
Dries committed
215
216
  if ($edit['tid'] && $edit['name']) {
    $data = array('name' => $edit['name'], 'description' => $edit['description'], 'weight' => $edit['weight']);
Dries's avatar
   
Dries committed
217

Dries's avatar
   
Dries committed
218
    db_query('UPDATE {term_data} SET '. _taxonomy_prepare_update($data) .' WHERE tid = %d', $edit['tid']);
Dries's avatar
   
Dries committed
219
    module_invoke_all('taxonomy', 'update', 'term', $edit);
Dries's avatar
   
Dries committed
220
    $message = t('The term %term has been updated.', array('%term' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
221
  }
Dries's avatar
   
Dries committed
222
223
  else if ($edit['tid']) {
    return taxonomy_del_term($edit['tid']);
Kjartan's avatar
Kjartan committed
224
225
  }
  else {
Dries's avatar
   
Dries committed
226
227
    $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
228
    db_query('INSERT INTO {term_data} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries's avatar
   
Dries committed
229
    module_invoke_all('taxonomy', 'insert', 'term', $edit);
Dries's avatar
   
Dries committed
230
    $message = t('Created new term %term.', array('%term' => '<em>'. $edit['name'] .'</em>'));
Kjartan's avatar
Kjartan committed
231
  }
Dries's avatar
   
Dries committed
232

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

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

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

Dries's avatar
   
Dries committed
264
265
  cache_clear_all();

Dries's avatar
   
Dries committed
266
  drupal_set_message($message);
Dries's avatar
   
Dries committed
267
  return $edit;
Kjartan's avatar
Kjartan committed
268
}
Dries's avatar
   
Dries committed
269

Kjartan's avatar
Kjartan committed
270
function taxonomy_del_term($tid) {
Dries's avatar
   
Dries committed
271
272
  $term = taxonomy_get_term($tid);

Dries's avatar
   
Dries committed
273
274
275
276
277
  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
278

Dries's avatar
   
Dries committed
279
  module_invoke_all('taxonomy', 'delete', 'term', $term);
Dries's avatar
   
Dries committed
280

Dries's avatar
   
Dries committed
281
282
  cache_clear_all();

Dries's avatar
   
Dries committed
283
  return t('deleted term "%name".', array('%name' => $term->name));
Dries's avatar
   
Dries committed
284
285
286
287
288
}

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

Dries's avatar
   
Dries committed
289
290
291
292
293
  $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
294

Dries's avatar
   
Dries committed
295
  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
296
}
Dries's avatar
   
Dries committed
297

Dries's avatar
   
Dries committed
298
299
300
/**
 * Generate a tabular listing of administrative functions for vocabularies.
 */
Kjartan's avatar
Kjartan committed
301
function taxonomy_overview() {
Dries's avatar
   
Dries committed
302
  $header = array(t('Name'), t('Node types'), array('data' => t('Operations'), 'colspan' => 3));
Dries's avatar
   
Dries committed
303

Kjartan's avatar
Kjartan committed
304
  $vocabularies = taxonomy_get_vocabularies();
Dries's avatar
   
Dries committed
305

Kjartan's avatar
Kjartan committed
306
307
  foreach ($vocabularies as $vocabulary) {
    $links = array();
Dries's avatar
   
Dries committed
308
    $types = array();
Dries's avatar
   
Dries committed
309
310
    foreach(explode(',', $vocabulary->nodes) as $type) {
      $types[] = node_invoke($type, 'node_name');
Dries's avatar
   
Dries committed
311
    }
Dries's avatar
   
Dries committed
312
    $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
313

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

Dries's avatar
   
Dries committed
324
  return theme('table', $header, $rows);
Kjartan's avatar
Kjartan committed
325
326
}

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

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

Dries's avatar
   
Dries committed
343
344
345
346
347
348
349
/**
 * 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
350
  if ($type) {
Dries's avatar
   
Dries committed
351
    $result = db_query("SELECT * FROM {vocabulary} WHERE nodes LIKE '%%%s%%' ORDER BY weight, name", $type);
Kjartan's avatar
Kjartan committed
352
353
  }
  else {
Dries's avatar
   
Dries committed
354
    $result = db_query('SELECT * FROM {vocabulary} ORDER BY weight, name');
Kjartan's avatar
Kjartan committed
355
356
357
358
  }
  $vocabularies = array();
  while ($voc = db_fetch_object($result)) {
    $vocabularies[$voc->$key] = $voc;
Dries's avatar
   
Dries committed
359
  }
Dries's avatar
   
Dries committed
360

Kjartan's avatar
Kjartan committed
361
362
  return $vocabularies;
}
Dries's avatar
   
Dries committed
363

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

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

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

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

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

Dries's avatar
   
Dries committed
408
409
410
411
/**
 * Find all terms associated to the given node.
 */
function taxonomy_node_get_terms($nid, $key = 'tid') {
Kjartan's avatar
Kjartan committed
412
  static $terms;
Dries's avatar
   
Dries committed
413

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

Dries's avatar
   
Dries committed
424
425
426
/**
 * Save term associations for a given node.
 */
Kjartan's avatar
Kjartan committed
427
428
function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);
Dries's avatar
   
Dries committed
429

Kjartan's avatar
Kjartan committed
430
  if ($terms) {
Dries's avatar
   
Dries committed
431
    foreach ($terms as $term) {
432
      if ($term) {
Dries's avatar
   
Dries committed
433
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
434
      }
Dries's avatar
   
Dries committed
435
436
    }
  }
Kjartan's avatar
Kjartan committed
437
}
Dries's avatar
   
Dries committed
438

Dries's avatar
   
Dries committed
439
440
441
/**
 * Remove associations of a node to its terms.
 */
Kjartan's avatar
Kjartan committed
442
function taxonomy_node_delete($nid) {
Dries's avatar
   
Dries committed
443
  db_query('DELETE FROM {term_node} WHERE nid = %d', $nid);
Kjartan's avatar
Kjartan committed
444
}
Dries's avatar
   
Dries committed
445

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

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

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

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

Kjartan's avatar
Kjartan committed
536
  $depth++;
Dries's avatar
   
Dries committed
537

Dries's avatar
   
Dries committed
538
539
540
541
  // 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
542

Dries's avatar
   
Dries committed
543
    $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
544
    while ($term = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
545
546
547
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
Dries's avatar
   
Dries committed
548
549
    }
  }
Dries's avatar
   
Dries committed
550

Dries's avatar
   
Dries committed
551
  $max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth;
Dries's avatar
   
Dries committed
552
553
  if ($children[$vid][$parent]) {
    foreach ($children[$vid][$parent] as $child) {
Dries's avatar
   
Dries committed
554
      if ($max_depth > $depth) {
Dries's avatar
   
Dries committed
555
556
557
558
559
        $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
560

Dries's avatar
   
Dries committed
561
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
Dries's avatar
   
Dries committed
562
      }
Dries's avatar
   
Dries committed
563
    }
Kjartan's avatar
Kjartan committed
564
  }
Dries's avatar
   
Dries committed
565

Dries's avatar
   
Dries committed
566
  return $tree ? $tree : array();
Kjartan's avatar
Kjartan committed
567
}
Dries's avatar
   
Dries committed
568

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

Dries's avatar
   
Dries committed
585
586
587
588
589
/**
 * 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
590
}
Dries's avatar
   
Dries committed
591

Dries's avatar
   
Dries committed
592
593
594
/**
 * Given a term id, count the number of published nodes in it.
 */
Dries's avatar
   
Dries committed
595
function taxonomy_term_count_nodes($tid, $type = 0) {
Kjartan's avatar
Kjartan committed
596
  static $count;
Dries's avatar
   
Dries committed
597

Dries's avatar
   
Dries committed
598
599
600
  if (!isset($count[$type])) {
    // $type == 0 always evaluates true is $type is a string
    if (is_numeric($type)) {
Dries's avatar
   
Dries committed
601
      $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
602
603
    }
    else {
Dries's avatar
   
Dries committed
604
      $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
605
    }
Kjartan's avatar
Kjartan committed
606
    while ($term = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
607
      $count[$type][$term->tid] = $term->c;
Dries's avatar
   
Dries committed
608
609
610
    }
  }

Kjartan's avatar
Kjartan committed
611
  foreach (_taxonomy_term_children($tid) as $c) {
Dries's avatar
   
Dries committed
612
    $children_count += taxonomy_term_count_nodes($c, $type);
Kjartan's avatar
Kjartan committed
613
  }
Dries's avatar
   
Dries committed
614
  return $count[$type][$tid] + $children_count;
Kjartan's avatar
Kjartan committed
615
616
}

Dries's avatar
   
Dries committed
617
618
619
/**
 * Helper for taxonomy_term_count_nodes().
 */
Kjartan's avatar
Kjartan committed
620
621
function _taxonomy_term_children($tid) {
  static $children;
Dries's avatar
   
Dries committed
622

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

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

  return $result;
}

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

  return $result;
}

Dries's avatar
   
Dries committed
676
677
678
/**
 * Return the vocabulary object matching a vocabulary ID.
 */
Kjartan's avatar
Kjartan committed
679
function taxonomy_get_vocabulary($vid) {
Dries's avatar
   
Dries committed
680
  return db_fetch_object(db_query('SELECT * FROM {vocabulary} WHERE vid = %d', $vid));
Kjartan's avatar
Kjartan committed
681
}
Dries's avatar
   
Dries committed
682

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

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

Kjartan's avatar
Kjartan committed
708
  if (count($options) > 0) {
Dries's avatar
   
Dries committed
709
    foreach ($options as $option) {
Dries's avatar
   
Dries committed
710
      $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
711
    }
Dries's avatar
   
Dries committed
712

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

Dries's avatar
   
Dries committed
715
    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
716
  }
Kjartan's avatar
Kjartan committed
717
}
Dries's avatar
   
Dries committed
718

Kjartan's avatar
Kjartan committed
719
720
721
function _taxonomy_depth($depth, $graphic = '--') {
  for ($n = 0; $n < $depth; $n++) {
    $result .= $graphic;
Dries's avatar
   
Dries committed
722
  }
Kjartan's avatar
Kjartan committed
723
724
  return $result;
}
Dries's avatar
   
Dries committed
725

Dries's avatar
   
Dries committed
726
function _taxonomy_prepare_update($data) {
Kjartan's avatar
Kjartan committed
727
  foreach ($data as $key => $value) {
728
    $q[] = "$key = '". str_replace('%', '%%', check_query($value)) ."'";
Dries's avatar
   
Dries committed
729
  }
Dries's avatar
   
Dries committed
730
  $result = implode(', ', $q);
Kjartan's avatar
Kjartan committed
731
732
  return $result;
}
Dries's avatar
   
Dries committed
733

Dries's avatar
   
Dries committed
734
function _taxonomy_prepare_insert($data, $stage) {
Kjartan's avatar
Kjartan committed
735
  if ($stage == 1) {
Dries's avatar
   
Dries committed
736
    $result = implode(', ', array_keys($data));
Kjartan's avatar
Kjartan committed
737
738
739
  }
  else {
    foreach (array_values($data) as $value) {
740
      $q[] = "'". str_replace('%', '%%', check_query($value)) ."'";
Dries's avatar
   
Dries committed
741
    }
Dries's avatar
   
Dries committed
742
    $result = implode(', ', $q);
Dries's avatar
   
Dries committed
743
  }
Kjartan's avatar
Kjartan committed
744
745
  return "($result)";
}
Dries's avatar
   
Dries committed
746

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

Dries's avatar
   
Dries committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
    if ($operator == 'or') {
      $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
      $sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql() .' ORDER BY n.sticky DESC, n.created DESC';
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() .' INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 AND '. node_access_where_sql();
    }
    else {
      $joins = '';
      $wheres = '';
      foreach ($descendant_tids as $index => $tids) {
        $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
        $wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
      }
      $sql = 'SELECT DISTINCT(n.nid) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres .' ORDER BY n.sticky DESC, n.created DESC';
      $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. node_access_join_sql() . $joins .' WHERE n.status = 1 AND '. node_access_where_sql() . $wheres;
790
    }
Dries's avatar
   
Dries committed
791

792
    if ($pager) {
Dries's avatar
   
Dries committed
793
      $result = pager_query($sql, variable_get('default_nodes_main', 10) , 0, $sql_count);
794
795
796
797
    }
    else {
      $result = db_query_range($sql, 0, 15);
    }
Dries's avatar
   
Dries committed
798
799
800
801
802
  }

  return $result;
}

Dries's avatar
   
Dries committed
803
804
805
/**
 * 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
806
807
*/
function taxonomy_render_nodes($result) {
808
809
810
811
812
813
814
815
  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
816
  }
Dries's avatar
   
Dries committed
817
  return $output;
Dries's avatar
   
Dries committed
818
819
}

Dries's avatar
   
Dries committed
820
821
822
/**
 * Implementation of hook_nodeapi().
 */
Dries's avatar
   
Dries committed
823
824
function taxonomy_nodeapi($node, $op, $arg = 0) {
  switch ($op) {
Dries's avatar
   
Dries committed
825
    case 'insert':
Dries's avatar
   
Dries committed
826
827
      taxonomy_node_save($node->nid, $node->taxonomy);
      break;
Dries's avatar
   
Dries committed
828
    case 'update':
Dries's avatar
   
Dries committed
829
830
      taxonomy_node_save($node->nid, $node->taxonomy);
      break;
Dries's avatar
   
Dries committed
831
    case 'delete':
Dries's avatar
   
Dries committed
832
833
834
835
836
      taxonomy_node_delete($node->nid);
      break;
  }
}

Dries's avatar
   
Dries committed
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
/**
 * 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
   
Dries committed
853

Dries's avatar
   
Dries committed
854
855
856
857
858
859
860
  // Build title:
  $result = db_query('SELECT name FROM {term_data} WHERE tid IN (%s)', implode(',', $tids));
  $names = array();
  while ($term = db_fetch_object($result)) {
    $names[] = $term->name;
  }
  $title = implode(', ', $names);
Dries's avatar
   
Dries committed
861

Dries's avatar
   
Dries committed
862
863
  switch ($op) {
    case 'page':
Dries's avatar
   
Dries committed
864
      // Build breadcrumb based on first hierarchy of first term:
Dries's avatar
   
Dries committed
865
      $current->tid = $tids[0];
Dries's avatar
   
Dries committed
866
      $breadcrumbs = array(array('path' => $_GET['q']));
Dries's avatar
   
Dries committed
867
868
      while ($parents = taxonomy_get_parents($current->tid)) {
        $current = array_shift($parents);
Dries's avatar
   
Dries committed
869
        $breadcrumbs[] = array('path' => 'taxonomy/term/'. $current->tid, 'title' => $current->name);
Dries's avatar
   
Dries committed
870
871
      }
      $breadcrumbs = array_reverse($breadcrumbs);
Dries's avatar
   
Dries committed
872
      menu_set_location($breadcrumbs);
Dries's avatar
   
Dries committed
873

Dries's avatar
   
Dries committed
874
      drupal_set_html_head('<link rel="alternate" type="application/rss+xml" title="RSS - '. $title .'" href="'. url('taxonomy/term/'. $str_tids .'/'. $depth .'/feed') .'" />');
Dries's avatar
Dries committed
875

Dries's avatar
   
Dries committed
876
877
      $output = taxonomy_render_nodes(taxonomy_select_nodes($tids, $operator, $depth, TRUE));
      print theme('page', $output, $title);
Dries's avatar
   
Dries committed
878
      break;
Dries's avatar
   
Dries committed
879
880
881
882
883
884
885
886
887
888
889
890
891

    case 'feed':
      $term = taxonomy_get_term($tids[0]);
      $channel['link'] = url('taxonomy/term/'. $str_tids .'/'. $depth, NULL, NULL, TRUE);
      $channel['title'] = variable_get('site_name', 'drupal') .' - '. $title;
      $channel['description'] = $term->description;

      $result = taxonomy_select_nodes($tids, $operator, $depth, FALSE);
      node_feed($result, $channel);
      break;

    default:
      drupal_not_found();
Dries's avatar
   
Dries committed
892
  }
Kjartan's avatar
Kjartan committed
893
}
Dries's avatar
   
Dries committed
894

Dries's avatar
   
Dries committed
895
896
897
/**
 * Menu callback; dispatches to the proper taxonomy administration function.
 */
Kjartan's avatar
Kjartan committed
898
function taxonomy_admin() {
Dries's avatar
   
Dries committed
899
900
  $op = $_POST['op'];
  $edit = $_POST['edit'];
Kjartan's avatar
Kjartan committed
901

Dries's avatar
   
Dries committed
902
903
904
  if (empty($op)) {
    $op = arg(2);
  }
Dries's avatar
   
Dries committed
905

Dries's avatar
   
Dries committed
906
  switch ($op) {
Dries's avatar
   
Dries committed
907
908
    case 'add':
      if (arg(3) == 'vocabulary') {
909
        $output = taxonomy_form_vocabulary();
Dries's avatar
   
Dries committed
910
      }
Dries's avatar
   
Dries committed
911
      else if (arg(3) == 'term') {
912
        $output = taxonomy_form_term();
Dries's avatar
   
Dries committed
913
914
      }
      break;
Dries's avatar
   
Dries committed
915
916
    case 'edit':
      if (arg(3) == 'vocabulary') {