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

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

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

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

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

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

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

Dries Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert committed
92
93
  return $items;
}
Dries Buytaert's avatar
 
Dries Buytaert committed
94

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

Dries Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert committed
110
  $form .= form_submit(t('Submit'));
Dries Buytaert's avatar
 
Dries Buytaert committed
111

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

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

Kjartan Mannes's avatar
Kjartan Mannes committed
120
function taxonomy_save_vocabulary($edit) {
Dries Buytaert's avatar
   
Dries Buytaert committed
121
122
  if (!$edit['nodes']) {
    $edit['nodes'] = array();
Dries Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert committed
126
  if ($edit['vid'] && $edit['name']) {
Dries Buytaert's avatar
   
Dries Buytaert committed
127
    db_query('UPDATE {vocabulary} SET '. _taxonomy_prepare_update($data) .' WHERE vid = %d', $edit['vid']);
Dries Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert committed
132
    module_invoke_all('taxonomy', 'update', 'vocabulary', $edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
133
    $message = t('Updated vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Dries Buytaert's avatar
 
Dries Buytaert committed
134
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
135
136
  else if ($edit['vid']) {
    $message = taxonomy_del_vocabulary($edit['vid']);
Kjartan Mannes's avatar
Kjartan Mannes committed
137
138
  }
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
139
    $data['vid'] = $edit['vid'] = db_next_id('{vocabulary}_vid');
Dries Buytaert's avatar
   
Dries Buytaert committed
140
    db_query('INSERT INTO {vocabulary} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries Buytaert's avatar
   
Dries Buytaert 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 Buytaert's avatar
   
Dries Buytaert committed
144
    module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
145
    $message = t('Created new vocabulary %name.', array('%name' => '<em>'. $edit['name'] .'</em>'));
Kjartan Mannes's avatar
Kjartan Mannes committed
146
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
147
148

  cache_clear_all();
Dries Buytaert's avatar
   
Dries Buytaert committed
149

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

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

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

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

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

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

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

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

Dries Buytaert's avatar
   
Dries Buytaert committed
175
176
177
178
  $form .= form_hidden('confirm', 1);
  $form .= form_hidden('type', 'vocabulary');
  $form .= form_hidden('vid', $vid);
  $form .= form_submit(t('Delete'));
Dries Buytaert's avatar
   
Dries Buytaert committed
179

Dries Buytaert's avatar
   
Dries Buytaert committed
180
  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 Mannes's avatar
Kjartan Mannes committed
181
}
Dries Buytaert's avatar
 
Dries Buytaert committed
182

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

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

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

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

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

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

212
  $form .= form_textarea(t('Synonyms'), 'synonyms', implode("\n", taxonomy_get_synonyms($edit['tid'])), 30, 5, t('<a href="%help-url">Synonyms</a> of this term, one synonym per line.', array('%help-url' => url('admin/help/taxonomy', NULL, NULL, 'synonyms'))));
Dries Buytaert's avatar
   
Dries Buytaert committed
213
  $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 Buytaert's avatar
   
Dries Buytaert committed
214
215
  $form .= form_hidden('vid', $vocabulary->vid);
  $form .= form_submit(t('Submit'));
Kjartan Mannes's avatar
Kjartan Mannes committed
216

Dries Buytaert's avatar
   
Dries Buytaert committed
217
218
219
  if ($edit['tid']) {
    $form .= form_submit(t('Delete'));
    $form .= form_hidden('tid', $edit['tid']);
Dries Buytaert's avatar
 
Dries Buytaert committed
220
  }
221
222
223
  else {
    $form .= form_hidden('destination', $_GET['q']);
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
224

Kjartan Mannes's avatar
Kjartan Mannes committed
225
226
  return form($form);
}
Dries Buytaert's avatar
 
Dries Buytaert committed
227

Kjartan Mannes's avatar
Kjartan Mannes committed
228
function taxonomy_save_term($edit) {
Dries Buytaert's avatar
   
Dries Buytaert committed
229
230
  if ($edit['tid'] && $edit['name']) {
    $data = array('name' => $edit['name'], 'description' => $edit['description'], 'weight' => $edit['weight']);
Dries Buytaert's avatar
 
Dries Buytaert committed
231

Dries Buytaert's avatar
   
Dries Buytaert committed
232
    db_query('UPDATE {term_data} SET '. _taxonomy_prepare_update($data) .' WHERE tid = %d', $edit['tid']);
Dries Buytaert's avatar
   
Dries Buytaert committed
233
    module_invoke_all('taxonomy', 'update', 'term', $edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
234
    $message = t('The term %term has been updated.', array('%term' => '<em>'. $edit['name'] .'</em>'));
Kjartan Mannes's avatar
Kjartan Mannes committed
235
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
236
237
  else if ($edit['tid']) {
    return taxonomy_del_term($edit['tid']);
Kjartan Mannes's avatar
Kjartan Mannes committed
238
239
  }
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
240
241
    $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 Buytaert's avatar
   
Dries Buytaert committed
242
    db_query('INSERT INTO {term_data} '. _taxonomy_prepare_insert($data, 1) .' VALUES '. _taxonomy_prepare_insert($data, 2));
Dries Buytaert's avatar
   
Dries Buytaert committed
243
    module_invoke_all('taxonomy', 'insert', 'term', $edit);
Dries Buytaert's avatar
   
Dries Buytaert committed
244
    $message = t('Created new term %term.', array('%term' => '<em>'. $edit['name'] .'</em>'));
Kjartan Mannes's avatar
Kjartan Mannes committed
245
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
246

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

Dries Buytaert's avatar
   
Dries Buytaert committed
256
257
  db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $edit['tid']);
  if (!isset($edit['parent'])) {
258
    $edit['parent'] = array(0);
Kjartan Mannes's avatar
Kjartan Mannes committed
259
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
260
261
  if (is_array($edit['parent'])) {
    foreach ($edit['parent'] as $parent) {
262
263
264
265
266
267
268
269
      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 Buytaert's avatar
 
Dries Buytaert committed
270
    }
Kjartan Mannes's avatar
Kjartan Mannes committed
271
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
272

Dries Buytaert's avatar
   
Dries Buytaert committed
273
274
275
  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 Buytaert's avatar
   
Dries Buytaert committed
276
      if ($synonym) {
Dries Buytaert's avatar
   
Dries Buytaert committed
277
        db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $edit['tid'], chop($synonym));
Dries Buytaert's avatar
   
Dries Buytaert committed
278
      }
Kjartan Mannes's avatar
Kjartan Mannes committed
279
    }
Dries Buytaert's avatar
 
Dries Buytaert committed
280
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
281

Dries Buytaert's avatar
   
Dries Buytaert committed
282
283
  cache_clear_all();

Dries Buytaert's avatar
   
Dries Buytaert committed
284
  drupal_set_message($message);
Dries Buytaert's avatar
   
Dries Buytaert committed
285
  return $edit;
Kjartan Mannes's avatar
Kjartan Mannes committed
286
}
Dries Buytaert's avatar
 
Dries Buytaert committed
287

Kjartan Mannes's avatar
Kjartan Mannes committed
288
function taxonomy_del_term($tid) {
289
290
291
292
293
294
295
296
297
298
299
300
301
302
  $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 Buytaert's avatar
   
Dries Buytaert committed
303

304
      $term = taxonomy_get_term($tid);
Dries Buytaert's avatar
   
Dries Buytaert committed
305

306
307
308
309
310
      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 Buytaert's avatar
   
Dries Buytaert committed
311

312
      module_invoke_all('taxonomy', 'delete', 'term', $term);
313
      drupal_set_message(t('Deleted term %name.', array('%name' => '<em>'. $term->name .'</em>')));
314
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
315

316
317
    $tids = $orphans;
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
318

Dries Buytaert's avatar
   
Dries Buytaert committed
319
  cache_clear_all();
Dries Buytaert's avatar
   
Dries Buytaert committed
320
321
322
323
324
}

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

Dries Buytaert's avatar
   
Dries Buytaert committed
325
326
327
328
  $form .= form_hidden('confirm', 1);
  $form .= form_hidden('type', 'term');
  $form .= form_hidden('tid', $tid);
  $form .= form_submit(t('Delete'));
Dries Buytaert's avatar
   
Dries Buytaert committed
329

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

  return $output;
Kjartan Mannes's avatar
Kjartan Mannes committed
333
}
Dries Buytaert's avatar
 
Dries Buytaert committed
334

Dries Buytaert's avatar
   
Dries Buytaert committed
335
336
337
/**
 * Generate a tabular listing of administrative functions for vocabularies.
 */
Kjartan Mannes's avatar
Kjartan Mannes committed
338
function taxonomy_overview() {
Dries Buytaert's avatar
   
Dries Buytaert committed
339
  $header = array(t('Name'), t('Type'), array('data' => t('Operations'), 'colspan' => '3'));
Dries Buytaert's avatar
   
Dries Buytaert committed
340

Kjartan Mannes's avatar
Kjartan Mannes committed
341
  $vocabularies = taxonomy_get_vocabularies();
Dries Buytaert's avatar
   
Dries Buytaert committed
342

Kjartan Mannes's avatar
Kjartan Mannes committed
343
  foreach ($vocabularies as $vocabulary) {
344
345
346
347
348
349
    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 Buytaert's avatar
   
Dries Buytaert committed
350

351
      $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 Mannes's avatar
Kjartan Mannes committed
352

353
354
355
      $tree = taxonomy_get_tree($vocabulary->vid);
      if ($tree) {
        unset($data);
Kjartan Mannes's avatar
Kjartan Mannes committed
356
      foreach ($tree as $term) {
Dries Buytaert's avatar
   
Dries Buytaert committed
357
        $data .= _taxonomy_depth($term->depth) .' '. $term->name .' ('. l(t('edit term'), "admin/taxonomy/edit/term/$term->tid") .')<br />';
Dries Buytaert's avatar
 
Dries Buytaert committed
358
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
359
      $rows[] = array(array('data' => $data, 'colspan' => '5'));
360
      }
Dries Buytaert's avatar
 
Dries Buytaert committed
361
362
363
    }
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
364
365
366
367
  if (!$rows) {
    $rows[] = array(array('data' => t('No categories available.'), 'colspan' => '5'));
  }

Dries Buytaert's avatar
   
Dries Buytaert committed
368
  return theme('table', $header, $rows);
Kjartan Mannes's avatar
Kjartan Mannes committed
369
370
}

Dries Buytaert's avatar
   
Dries Buytaert committed
371
372
373
/**
 * Generate a form element for selecting terms from a vocabulary.
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
374
function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') {
Dries Buytaert's avatar
   
Dries Buytaert committed
375
  $vocabulary = taxonomy_get_vocabulary($vid);
376
  $help = ($help) ? $help : $vocabulary->help;
Kjartan Mannes's avatar
Kjartan Mannes committed
377
378
379
380
  if ($vocabulary->required) {
    $blank = 0;
  }
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
381
    $blank = '<'. t('none') .'>';
Kjartan Mannes's avatar
Kjartan Mannes committed
382
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
383

Dries Buytaert's avatar
   
Dries Buytaert committed
384
  return _taxonomy_term_select($vocabulary->name, $name, $value, $vid, $help, intval($vocabulary->multiple), $blank);
Kjartan Mannes's avatar
Kjartan Mannes committed
385
}
Dries Buytaert's avatar
 
Dries Buytaert committed
386

387
/**
388
389
390
 * Generate a set of options for selecting a term from all vocabularies. Can be
 * passed to form_select.
 */
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
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 Buytaert's avatar
   
Dries Buytaert committed
406
407
408
409
410
411
/**
 * Return an array of all vocabulary objects.
 *
 * @param $type
 *   If set, return only those vocabularies associated with this node type.
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
412
function taxonomy_get_vocabularies($type = NULL) {
Kjartan Mannes's avatar
Kjartan Mannes committed
413
  if ($type) {
414
    $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 Mannes's avatar
Kjartan Mannes committed
415
416
  }
  else {
417
    $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 Mannes's avatar
Kjartan Mannes committed
418
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
419

Kjartan Mannes's avatar
Kjartan Mannes committed
420
  $vocabularies = array();
Dries Buytaert's avatar
   
Dries Buytaert committed
421
  $node_types = array();
Kjartan Mannes's avatar
Kjartan Mannes committed
422
  while ($voc = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
423
424
425
426
    $node_types[$voc->vid][] = $voc->type;
    unset($voc->type);
    $voc->nodes = $node_types[$voc->vid];
    $vocabularies[$voc->vid] = $voc;
Dries Buytaert's avatar
 
Dries Buytaert committed
427
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
428

Kjartan Mannes's avatar
Kjartan Mannes committed
429
430
  return $vocabularies;
}
Dries Buytaert's avatar
 
Dries Buytaert committed
431

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

Dries Buytaert's avatar
   
Dries Buytaert committed
448
  $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 Mannes's avatar
Kjartan Mannes committed
449
  while ($vocabulary = db_fetch_object($c)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
450
    $result[] = taxonomy_form($vocabulary->vid, $terms, $help, $name);
Dries Buytaert's avatar
 
Dries Buytaert committed
451
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
452
453
  return $result ? $result : array();
}
Dries Buytaert's avatar
 
Dries Buytaert committed
454

Dries Buytaert's avatar
   
Dries Buytaert committed
455
456
457
458
459
/**
 * 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 Mannes's avatar
Kjartan Mannes committed
460
461
462
  $terms = array();
  while ($term = db_fetch_object($result)) {
    $terms[$term->$key] = $term;
Dries Buytaert's avatar
 
Dries Buytaert committed
463
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
464
465
466
  return $terms;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
467
468
469
470
/**
 * Find all terms associated to the given node.
 */
function taxonomy_node_get_terms($nid, $key = 'tid') {
Kjartan Mannes's avatar
Kjartan Mannes committed
471
  static $terms;
Dries Buytaert's avatar
 
Dries Buytaert committed
472

Dries Buytaert's avatar
   
Dries Buytaert committed
473
  if (!isset($terms[$nid])) {
Dries Buytaert's avatar
   
Dries Buytaert committed
474
    $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 Mannes's avatar
Kjartan Mannes committed
475
    $terms[$nid] = array();
Dries Buytaert's avatar
 
Dries Buytaert committed
476
    while ($term = db_fetch_object($result)) {
Kjartan Mannes's avatar
Kjartan Mannes committed
477
      $terms[$nid][$term->$key] = $term;
Dries Buytaert's avatar
 
Dries Buytaert committed
478
479
    }
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
480
481
  return $terms[$nid];
}
Dries Buytaert's avatar
 
Dries Buytaert committed
482

Dries Buytaert's avatar
   
Dries Buytaert committed
483
484
485
/**
 * Save term associations for a given node.
 */
Kjartan Mannes's avatar
Kjartan Mannes committed
486
487
function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);
Dries Buytaert's avatar
 
Dries Buytaert committed
488

489
  if (is_array($terms)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
490
    foreach ($terms as $term) {
491
492
493
494
495
496
497
      if (is_array($term)) {
        foreach ($term as $tid) {
          if ($tid) {
            db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
          }
        }
      }
498
      if ($term) {
Dries Buytaert's avatar
   
Dries Buytaert committed
499
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
500
      }
Dries Buytaert's avatar
 
Dries Buytaert committed
501
502
    }
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
503
}
Dries Buytaert's avatar
 
Dries Buytaert committed
504

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

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

Dries Buytaert's avatar
   
Dries Buytaert committed
529
530
531
532
/**
 * Find all parents of a given term ID.
 */
function taxonomy_get_parents($tid, $key = 'tid') {
Kjartan Mannes's avatar
Kjartan Mannes committed
533
  if ($tid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
534
    $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 Mannes's avatar
Kjartan Mannes committed
535
536
537
    $parents = array();
    while ($parent = db_fetch_object($result)) {
      $parents[$parent->$key] = $parent;
Dries Buytaert's avatar
   
Dries Buytaert committed
538
    }
Kjartan Mannes's avatar
Kjartan Mannes committed
539
    return $parents;
Dries Buytaert's avatar
 
Dries Buytaert committed
540
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
541
542
543
544
  else {
    return array();
  }
}
Dries Buytaert's avatar
 
Dries Buytaert committed
545

Dries Buytaert's avatar
   
Dries Buytaert committed
546
547
548
549
/**
 * Find all ancestors of a given term ID.
 */
function taxonomy_get_parents_all($tid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
550
551
552
553
554
555
556
557
558
559
560
561
  $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 Buytaert's avatar
   
Dries Buytaert committed
562
563
564
565
/**
 * Find all children of a term ID.
 */
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
Kjartan Mannes's avatar
Kjartan Mannes committed
566
  if ($vid) {
Dries Buytaert's avatar
   
Dries Buytaert committed
567
    $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 Buytaert's avatar
 
Dries Buytaert committed
568
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
569
  else {
Dries Buytaert's avatar
   
Dries Buytaert committed
570
    $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 Mannes's avatar
Kjartan Mannes committed
571
572
573
574
575
576
577
  }
  $children = array();
  while ($term = db_fetch_object($result)) {
    $children[$term->$key] = $term;
  }
  return $children;
}
Dries Buytaert's avatar
 
Dries Buytaert committed
578

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

Kjartan Mannes's avatar
Kjartan Mannes committed
602
  $depth++;
Dries Buytaert's avatar
   
Dries Buytaert committed
603

Dries Buytaert's avatar
   
Dries Buytaert committed
604
605
606
607
  // 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 Buytaert's avatar
   
Dries Buytaert committed
608

Dries Buytaert's avatar
   
Dries Buytaert committed
609
    $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 Buytaert's avatar
   
Dries Buytaert committed
610
    while ($term = db_fetch_object($result)) {
Dries Buytaert's avatar
   
Dries Buytaert committed
611
612
613
      $children[$vid][$term->parent][] = $term->tid;
      $parents[$vid][$term->tid][] = $term->parent;
      $terms[$vid][$term->tid] = $term;
Dries Buytaert's avatar
 
Dries Buytaert committed
614
615
    }
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
616

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

Dries Buytaert's avatar
   
Dries Buytaert committed
627
        $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth));
Dries Buytaert's avatar
   
Dries Buytaert committed
628
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
629
    }
Kjartan Mannes's avatar
Kjartan Mannes committed
630
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
631

Dries Buytaert's avatar
   
Dries Buytaert committed
632
  return $tree ? $tree : array();
Kjartan Mannes's avatar
Kjartan Mannes committed
633
}
Dries Buytaert's avatar
 
Dries Buytaert committed
634

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

Dries Buytaert's avatar
   
Dries Buytaert committed
651
652
653
654
655
/**
 * 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 Mannes's avatar
Kjartan Mannes committed
656
}
Dries Buytaert's avatar
   
Dries Buytaert committed
657

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

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

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

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

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

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

  return $result;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
720
721
722
/**
 * Return the vocabulary object matching a vocabulary ID.
 */
Kjartan Mannes's avatar
Kjartan Mannes committed
723
function taxonomy_get_vocabulary($vid) {
724
  $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 Buytaert's avatar
   
Dries Buytaert committed
725
726
727
728
729
730
731
732
733
  $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 Mannes's avatar
Kjartan Mannes committed
734
}
Dries Buytaert's avatar
 
Dries Buytaert committed
735

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

Kjartan Mannes's avatar
Kjartan Mannes committed
744
function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) {
Dries Buytaert's avatar
   
Dries Buytaert committed
745
  $tree = taxonomy_get_tree($vocabulary_id);
746
747
  $options = array();

Dries Buytaert's avatar
Dries Buytaert committed
748
  if ($blank) {
749
    $options[0] = $blank;
Dries Buytaert's avatar
Dries Buytaert committed
750
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
751
752
753
  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
754
        $options[$term->tid] = _taxonomy_depth($term->depth, '-') . $term->name;
Dries Buytaert's avatar
 
Dries Buytaert committed
755
756
      }
    }
Kjartan Mannes's avatar
Kjartan Mannes committed
757
758
759
760
761
    if (!$blank && !$value) {
      // required but without a predefined value, so set first as predefined
      $value = $tree[0]->tid;
    }
  }
Dries Buytaert's avatar
 
Dries Buytaert committed
762

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

Kjartan Mannes's avatar
Kjartan Mannes committed
766
767
768
function _taxonomy_depth($depth, $graphic = '--') {
  for ($n = 0; $n < $depth; $n++) {
    $result .= $graphic;
Dries Buytaert's avatar
 
Dries Buytaert committed
769
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
770
771
  return $result;
}
Dries Buytaert's avatar
 
Dries Buytaert committed
772

Dries Buytaert's avatar
   
Dries Buytaert committed
773
function _taxonomy_prepare_update($data) {
Kjartan Mannes's avatar
Kjartan Mannes committed
774
  foreach ($data as $key => $value) {
Dries Buytaert's avatar
   
Dries Buytaert committed
775
    $q[] = "$key = '". str_replace('%', '%%', db_escape_string($value)) ."'";
Dries Buytaert's avatar
 
Dries Buytaert committed
776
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
777
  $result = implode(', ', $q);
Kjartan Mannes's avatar
Kjartan Mannes committed
778
779
  return $result;
}
Dries Buytaert's avatar
 
Dries Buytaert committed
780

Dries Buytaert's avatar
   
Dries Buytaert committed
781
function _taxonomy_prepare_insert($data, $stage) {
Kjartan Mannes's avatar
Kjartan Mannes committed
782
  if ($stage == 1) {
Dries Buytaert's avatar
   
Dries Buytaert committed
783
    $result = implode(', ', array_keys($data));
Kjartan Mannes's avatar
Kjartan Mannes committed
784
785
786
  }
  else {
    foreach (array_values($data) as $value) {
787
788
789
790
791
792
793
      //Escape strings, but not integers
      if (is_int($value)) {
        $q[] = $value;
      }
      else {
        $q[] = "'". str_replace('%', '%%', db_escape_string($value)) ."'";
      }
Dries Buytaert's avatar
 
Dries Buytaert committed
794
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
795
    $result = implode(', ', $q);
Dries Buytaert's avatar
 
Dries Buytaert committed
796
  }
Kjartan Mannes's avatar
Kjartan Mannes committed
797
798
  return "($result)";
}
Dries Buytaert's avatar
   
Dries Buytaert committed
799

Dries Buytaert's avatar
   
Dries Buytaert committed
800
801
802
/**
 * Finds all nodes that match selected taxonomy conditions.
 *
Dries Buytaert's avatar
   
Dries Buytaert committed
803
804
805
806
807
808
809
 * @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 Buytaert's avatar
   
Dries Buytaert committed
810
811
812
813
814
815
 * @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 Buytaert's avatar
   
Dries Buytaert committed
816
817
818
819
820
821
822
823
824
825
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
826
      $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
827
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
828

Dries Buytaert's avatar
   
Dries Buytaert committed
829
830
    if ($operator == 'or') {
      $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));
Dries Buytaert's avatar
   
Dries Buytaert committed
831
832
      $sql = 'SELECT n.nid, n.sticky, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC';
      $sql_count = 'SELECT COUNT(n.nid) FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1';
Dries Buytaert's avatar
   
Dries Buytaert committed
833
834
835
836
837
838
839
840
    }
    else {
      $joins = '';
      $wheres = '';
      foreach ($descendant_tids as $index => $tids) {
        $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
        $wheres .= ' AND tn'. $index .'.tid IN ('. implode(',', $tids) .')';
      }
Dries Buytaert's avatar
   
Dries Buytaert committed
841
      $sql = 'SELECT n.nid, n.sticky, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY n.sticky DESC, n.created DESC';
842
      $sql_count = 'SELECT COUNT(n.nid) FROM {node} n '. $joins .' WHERE n.status = 1 ' . $wheres;
843
    }
844
845
    $sql = db_rewrite_sql($sql);
    $sql_count = db_rewrite_sql($sql_count);
846
    if ($pager) {
Steven Wittens's avatar
Steven Wittens committed
847
      $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count);
848
849
850
851
    }
    else {
      $result = db_query_range($sql, 0, 15);
    }
Dries Buytaert's avatar
   
Dries Buytaert committed
852
853
854
855
856
  }

  return $result;
}

Dries Buytaert's avatar
   
Dries Buytaert committed
857
858
859
/**
 * 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 Buytaert's avatar
   
Dries Buytaert committed
860
861
*/
function taxonomy_render_nodes($result) {
862
863
864
865
866
867
868
869
  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 Buytaert's avatar
   
Dries Buytaert committed
870
  }
Dries Buytaert's avatar
   
Dries Buytaert committed
871
  return $output;
Dries Buytaert's avatar
   
Dries Buytaert committed
872
873
}

Dries Buytaert's avatar
   
Dries Buytaert committed
874
875
876
/**
 * Implementation of hook_nodeapi().
 */
Dries Buytaert's avatar
   
Dries Buytaert committed
877
878
function taxonomy_nodeapi($node, $op, $arg = 0) {
  switch ($op) {
Dries Buytaert's avatar
   
Dries Buytaert committed
879
    case 'insert':
Dries Buytaert's avatar
   
Dries Buytaert committed
880
881
      taxonomy_node_save($node->nid, $node->taxonomy);
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
882
    case 'update':
Dries Buytaert's avatar
   
Dries Buytaert committed
883
884
      taxonomy_node_save($node->nid, $node->taxonomy);
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
885
    case 'delete':
Dries Buytaert's avatar
   
Dries Buytaert committed
886
887
      taxonomy_node_delete($node->nid);
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
888
889
890
    case 'rss item':
      return taxonomy_rss_item($node);
      break;
Dries Buytaert's avatar
   
Dries Buytaert committed
891
892
893
  }
}

Dries Buytaert's avatar
   
Dries Buytaert committed
894
895
896
897
898
899
900
901
902
903
904