|
1 <?php |
|
2 // $Id: taxonomy.module,v 1.414.2.11 2009/05/13 19:38:33 goba Exp $ |
|
3 |
|
4 /** |
|
5 * @file |
|
6 * Enables the organization of content into categories. |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Implementation of hook_perm(). |
|
11 */ |
|
12 function taxonomy_perm() { |
|
13 return array('administer taxonomy'); |
|
14 } |
|
15 |
|
16 /** |
|
17 * Implementation of hook_theme() |
|
18 */ |
|
19 function taxonomy_theme() { |
|
20 return array( |
|
21 'taxonomy_term_select' => array( |
|
22 'arguments' => array('element' => NULL), |
|
23 ), |
|
24 'taxonomy_term_page' => array( |
|
25 'arguments' => array('tids' => array(), 'result' => NULL), |
|
26 ), |
|
27 'taxonomy_overview_vocabularies' => array( |
|
28 'arguments' => array('form' => array()), |
|
29 ), |
|
30 'taxonomy_overview_terms' => array( |
|
31 'arguments' => array('form' => array()), |
|
32 ), |
|
33 ); |
|
34 } |
|
35 |
|
36 /** |
|
37 * Implementation of hook_link(). |
|
38 * |
|
39 * This hook is extended with $type = 'taxonomy terms' to allow themes to |
|
40 * print lists of terms associated with a node. Themes can print taxonomy |
|
41 * links with: |
|
42 * |
|
43 * if (module_exists('taxonomy')) { |
|
44 * $terms = taxonomy_link('taxonomy terms', $node); |
|
45 * print theme('links', $terms); |
|
46 * } |
|
47 */ |
|
48 function taxonomy_link($type, $node = NULL) { |
|
49 if ($type == 'taxonomy terms' && $node != NULL) { |
|
50 $links = array(); |
|
51 // If previewing, the terms must be converted to objects first. |
|
52 if (isset($node->build_mode) && $node->build_mode == NODE_BUILD_PREVIEW) { |
|
53 $node->taxonomy = taxonomy_preview_terms($node); |
|
54 } |
|
55 if (!empty($node->taxonomy)) { |
|
56 foreach ($node->taxonomy as $term) { |
|
57 // During preview the free tagging terms are in an array unlike the |
|
58 // other terms which are objects. So we have to check if a $term |
|
59 // is an object or not. |
|
60 if (is_object($term)) { |
|
61 $links['taxonomy_term_'. $term->tid] = array( |
|
62 'title' => $term->name, |
|
63 'href' => taxonomy_term_path($term), |
|
64 'attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)) |
|
65 ); |
|
66 } |
|
67 // Previewing free tagging terms; we don't link them because the |
|
68 // term-page might not exist yet. |
|
69 else { |
|
70 foreach ($term as $free_typed) { |
|
71 $typed_terms = drupal_explode_tags($free_typed); |
|
72 foreach ($typed_terms as $typed_term) { |
|
73 $links['taxonomy_preview_term_'. $typed_term] = array( |
|
74 'title' => $typed_term, |
|
75 ); |
|
76 } |
|
77 } |
|
78 } |
|
79 } |
|
80 } |
|
81 |
|
82 // We call this hook again because some modules and themes |
|
83 // call taxonomy_link('taxonomy terms') directly. |
|
84 drupal_alter('link', $links, $node); |
|
85 |
|
86 return $links; |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * For vocabularies not maintained by taxonomy.module, give the maintaining |
|
92 * module a chance to provide a path for terms in that vocabulary. |
|
93 * |
|
94 * @param $term |
|
95 * A term object. |
|
96 * @return |
|
97 * An internal Drupal path. |
|
98 */ |
|
99 function taxonomy_term_path($term) { |
|
100 $vocabulary = taxonomy_vocabulary_load($term->vid); |
|
101 if ($vocabulary->module != 'taxonomy' && $path = module_invoke($vocabulary->module, 'term_path', $term)) { |
|
102 return $path; |
|
103 } |
|
104 return 'taxonomy/term/'. $term->tid; |
|
105 } |
|
106 |
|
107 /** |
|
108 * Implementation of hook_menu(). |
|
109 */ |
|
110 function taxonomy_menu() { |
|
111 $items['admin/content/taxonomy'] = array( |
|
112 'title' => 'Taxonomy', |
|
113 'description' => 'Manage tagging, categorization, and classification of your content.', |
|
114 'page callback' => 'drupal_get_form', |
|
115 'page arguments' => array('taxonomy_overview_vocabularies'), |
|
116 'access arguments' => array('administer taxonomy'), |
|
117 'file' => 'taxonomy.admin.inc', |
|
118 ); |
|
119 |
|
120 $items['admin/content/taxonomy/list'] = array( |
|
121 'title' => 'List', |
|
122 'type' => MENU_DEFAULT_LOCAL_TASK, |
|
123 'weight' => -10, |
|
124 ); |
|
125 |
|
126 $items['admin/content/taxonomy/add/vocabulary'] = array( |
|
127 'title' => 'Add vocabulary', |
|
128 'page callback' => 'drupal_get_form', |
|
129 'page arguments' => array('taxonomy_form_vocabulary'), |
|
130 'access arguments' => array('administer taxonomy'), |
|
131 'type' => MENU_LOCAL_TASK, |
|
132 'parent' => 'admin/content/taxonomy', |
|
133 'file' => 'taxonomy.admin.inc', |
|
134 ); |
|
135 |
|
136 $items['admin/content/taxonomy/edit/vocabulary/%taxonomy_vocabulary'] = array( |
|
137 'title' => 'Edit vocabulary', |
|
138 'page callback' => 'taxonomy_admin_vocabulary_edit', |
|
139 'page arguments' => array(5), |
|
140 'access arguments' => array('administer taxonomy'), |
|
141 'type' => MENU_CALLBACK, |
|
142 'file' => 'taxonomy.admin.inc', |
|
143 ); |
|
144 |
|
145 $items['admin/content/taxonomy/edit/term'] = array( |
|
146 'title' => 'Edit term', |
|
147 'page callback' => 'taxonomy_admin_term_edit', |
|
148 'access arguments' => array('administer taxonomy'), |
|
149 'type' => MENU_CALLBACK, |
|
150 'file' => 'taxonomy.admin.inc', |
|
151 ); |
|
152 |
|
153 $items['taxonomy/term/%'] = array( |
|
154 'title' => 'Taxonomy term', |
|
155 'page callback' => 'taxonomy_term_page', |
|
156 'page arguments' => array(2), |
|
157 'access arguments' => array('access content'), |
|
158 'type' => MENU_CALLBACK, |
|
159 'file' => 'taxonomy.pages.inc', |
|
160 ); |
|
161 |
|
162 $items['taxonomy/autocomplete'] = array( |
|
163 'title' => 'Autocomplete taxonomy', |
|
164 'page callback' => 'taxonomy_autocomplete', |
|
165 'access arguments' => array('access content'), |
|
166 'type' => MENU_CALLBACK, |
|
167 'file' => 'taxonomy.pages.inc', |
|
168 ); |
|
169 $items['admin/content/taxonomy/%taxonomy_vocabulary'] = array( |
|
170 'title' => 'List terms', |
|
171 'page callback' => 'drupal_get_form', |
|
172 'page arguments' => array('taxonomy_overview_terms', 3), |
|
173 'access arguments' => array('administer taxonomy'), |
|
174 'type' => MENU_CALLBACK, |
|
175 'file' => 'taxonomy.admin.inc', |
|
176 ); |
|
177 |
|
178 $items['admin/content/taxonomy/%taxonomy_vocabulary/list'] = array( |
|
179 'title' => 'List', |
|
180 'type' => MENU_DEFAULT_LOCAL_TASK, |
|
181 'weight' => -10, |
|
182 ); |
|
183 |
|
184 $items['admin/content/taxonomy/%taxonomy_vocabulary/add/term'] = array( |
|
185 'title' => 'Add term', |
|
186 'page callback' => 'taxonomy_add_term_page', |
|
187 'page arguments' => array(3), |
|
188 'access arguments' => array('administer taxonomy'), |
|
189 'type' => MENU_LOCAL_TASK, |
|
190 'parent' => 'admin/content/taxonomy/%taxonomy_vocabulary', |
|
191 'file' => 'taxonomy.admin.inc', |
|
192 ); |
|
193 |
|
194 return $items; |
|
195 } |
|
196 |
|
197 function taxonomy_save_vocabulary(&$edit) { |
|
198 $edit['nodes'] = empty($edit['nodes']) ? array() : $edit['nodes']; |
|
199 |
|
200 if (!isset($edit['module'])) { |
|
201 $edit['module'] = 'taxonomy'; |
|
202 } |
|
203 |
|
204 if (!empty($edit['vid']) && !empty($edit['name'])) { |
|
205 drupal_write_record('vocabulary', $edit, 'vid'); |
|
206 db_query("DELETE FROM {vocabulary_node_types} WHERE vid = %d", $edit['vid']); |
|
207 foreach ($edit['nodes'] as $type => $selected) { |
|
208 db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type); |
|
209 } |
|
210 module_invoke_all('taxonomy', 'update', 'vocabulary', $edit); |
|
211 $status = SAVED_UPDATED; |
|
212 } |
|
213 else if (!empty($edit['vid'])) { |
|
214 $status = taxonomy_del_vocabulary($edit['vid']); |
|
215 } |
|
216 else { |
|
217 drupal_write_record('vocabulary', $edit); |
|
218 foreach ($edit['nodes'] as $type => $selected) { |
|
219 db_query("INSERT INTO {vocabulary_node_types} (vid, type) VALUES (%d, '%s')", $edit['vid'], $type); |
|
220 } |
|
221 module_invoke_all('taxonomy', 'insert', 'vocabulary', $edit); |
|
222 $status = SAVED_NEW; |
|
223 } |
|
224 |
|
225 cache_clear_all(); |
|
226 |
|
227 return $status; |
|
228 } |
|
229 |
|
230 /** |
|
231 * Delete a vocabulary. |
|
232 * |
|
233 * @param $vid |
|
234 * A vocabulary ID. |
|
235 * @return |
|
236 * Constant indicating items were deleted. |
|
237 */ |
|
238 function taxonomy_del_vocabulary($vid) { |
|
239 $vocabulary = (array) taxonomy_vocabulary_load($vid); |
|
240 |
|
241 db_query('DELETE FROM {vocabulary} WHERE vid = %d', $vid); |
|
242 db_query('DELETE FROM {vocabulary_node_types} WHERE vid = %d', $vid); |
|
243 $result = db_query('SELECT tid FROM {term_data} WHERE vid = %d', $vid); |
|
244 while ($term = db_fetch_object($result)) { |
|
245 taxonomy_del_term($term->tid); |
|
246 } |
|
247 |
|
248 module_invoke_all('taxonomy', 'delete', 'vocabulary', $vocabulary); |
|
249 |
|
250 cache_clear_all(); |
|
251 |
|
252 return SAVED_DELETED; |
|
253 } |
|
254 |
|
255 /** |
|
256 * Dynamicly check and update the hierarachy flag of a vocabulary. |
|
257 * |
|
258 * Checks the current parents of all terms in a vocabulary and updates the |
|
259 * vocabularies hierarchy setting to the lowest possible level. A hierarchy with |
|
260 * no parents in any of its terms will be given a hierarchy of 0. If terms |
|
261 * contain at most a single parent, the vocabulary will be given a hierarchy of |
|
262 * 1. If any term contain multiple parents, the vocabulary will be given a |
|
263 * hieararchy of 2. |
|
264 * |
|
265 * @param $vocabulary |
|
266 * An array of the vocabulary structure. |
|
267 * @param $changed_term |
|
268 * An array of the term structure that was updated. |
|
269 */ |
|
270 function taxonomy_check_vocabulary_hierarchy($vocabulary, $changed_term) { |
|
271 $tree = taxonomy_get_tree($vocabulary['vid']); |
|
272 $hierarchy = 0; |
|
273 foreach ($tree as $term) { |
|
274 // Update the changed term with the new parent value before comparision. |
|
275 if ($term->tid == $changed_term['tid']) { |
|
276 $term = (object)$changed_term; |
|
277 $term->parents = $term->parent; |
|
278 } |
|
279 // Check this term's parent count. |
|
280 if (count($term->parents) > 1) { |
|
281 $hierarchy = 2; |
|
282 break; |
|
283 } |
|
284 elseif (count($term->parents) == 1 && 0 !== array_shift($term->parents)) { |
|
285 $hierarchy = 1; |
|
286 } |
|
287 } |
|
288 if ($hierarchy != $vocabulary['hierarchy']) { |
|
289 $vocabulary['hierarchy'] = $hierarchy; |
|
290 taxonomy_save_vocabulary($vocabulary); |
|
291 } |
|
292 |
|
293 return $hierarchy; |
|
294 } |
|
295 |
|
296 /** |
|
297 * Helper function for taxonomy_form_term_submit(). |
|
298 * |
|
299 * @param $form_state['values'] |
|
300 * @return |
|
301 * Status constant indicating if term was inserted or updated. |
|
302 */ |
|
303 function taxonomy_save_term(&$form_values) { |
|
304 $form_values += array( |
|
305 'description' => '', |
|
306 'weight' => 0 |
|
307 ); |
|
308 |
|
309 if (!empty($form_values['tid']) && $form_values['name']) { |
|
310 drupal_write_record('term_data', $form_values, 'tid'); |
|
311 $hook = 'update'; |
|
312 $status = SAVED_UPDATED; |
|
313 } |
|
314 else if (!empty($form_values['tid'])) { |
|
315 return taxonomy_del_term($form_values['tid']); |
|
316 } |
|
317 else { |
|
318 drupal_write_record('term_data', $form_values); |
|
319 $hook = 'insert'; |
|
320 $status = SAVED_NEW; |
|
321 } |
|
322 |
|
323 db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $form_values['tid'], $form_values['tid']); |
|
324 if (!empty($form_values['relations'])) { |
|
325 foreach ($form_values['relations'] as $related_id) { |
|
326 if ($related_id != 0) { |
|
327 db_query('INSERT INTO {term_relation} (tid1, tid2) VALUES (%d, %d)', $form_values['tid'], $related_id); |
|
328 } |
|
329 } |
|
330 } |
|
331 |
|
332 db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $form_values['tid']); |
|
333 if (!isset($form_values['parent']) || empty($form_values['parent'])) { |
|
334 $form_values['parent'] = array(0); |
|
335 } |
|
336 if (is_array($form_values['parent'])) { |
|
337 foreach ($form_values['parent'] as $parent) { |
|
338 if (is_array($parent)) { |
|
339 foreach ($parent as $tid) { |
|
340 db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $tid); |
|
341 } |
|
342 } |
|
343 else { |
|
344 db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $parent); |
|
345 } |
|
346 } |
|
347 } |
|
348 else { |
|
349 db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $form_values['tid'], $form_values['parent']); |
|
350 } |
|
351 |
|
352 db_query('DELETE FROM {term_synonym} WHERE tid = %d', $form_values['tid']); |
|
353 if (!empty($form_values['synonyms'])) { |
|
354 foreach (explode ("\n", str_replace("\r", '', $form_values['synonyms'])) as $synonym) { |
|
355 if ($synonym) { |
|
356 db_query("INSERT INTO {term_synonym} (tid, name) VALUES (%d, '%s')", $form_values['tid'], chop($synonym)); |
|
357 } |
|
358 } |
|
359 } |
|
360 |
|
361 if (isset($hook)) { |
|
362 module_invoke_all('taxonomy', $hook, 'term', $form_values); |
|
363 } |
|
364 |
|
365 cache_clear_all(); |
|
366 |
|
367 return $status; |
|
368 } |
|
369 |
|
370 /** |
|
371 * Delete a term. |
|
372 * |
|
373 * @param $tid |
|
374 * The term ID. |
|
375 * @return |
|
376 * Status constant indicating deletion. |
|
377 */ |
|
378 function taxonomy_del_term($tid) { |
|
379 $tids = array($tid); |
|
380 while ($tids) { |
|
381 $children_tids = $orphans = array(); |
|
382 foreach ($tids as $tid) { |
|
383 // See if any of the term's children are about to be become orphans: |
|
384 if ($children = taxonomy_get_children($tid)) { |
|
385 foreach ($children as $child) { |
|
386 // If the term has multiple parents, we don't delete it. |
|
387 $parents = taxonomy_get_parents($child->tid); |
|
388 if (count($parents) == 1) { |
|
389 $orphans[] = $child->tid; |
|
390 } |
|
391 } |
|
392 } |
|
393 |
|
394 $term = (array) taxonomy_get_term($tid); |
|
395 |
|
396 db_query('DELETE FROM {term_data} WHERE tid = %d', $tid); |
|
397 db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $tid); |
|
398 db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $tid, $tid); |
|
399 db_query('DELETE FROM {term_synonym} WHERE tid = %d', $tid); |
|
400 db_query('DELETE FROM {term_node} WHERE tid = %d', $tid); |
|
401 |
|
402 module_invoke_all('taxonomy', 'delete', 'term', $term); |
|
403 } |
|
404 |
|
405 $tids = $orphans; |
|
406 } |
|
407 |
|
408 cache_clear_all(); |
|
409 |
|
410 return SAVED_DELETED; |
|
411 } |
|
412 |
|
413 /** |
|
414 * Generate a form element for selecting terms from a vocabulary. |
|
415 */ |
|
416 function taxonomy_form($vid, $value = 0, $help = NULL, $name = 'taxonomy') { |
|
417 $vocabulary = taxonomy_vocabulary_load($vid); |
|
418 $help = ($help) ? $help : filter_xss_admin($vocabulary->help); |
|
419 |
|
420 if (!$vocabulary->multiple) { |
|
421 $blank = ($vocabulary->required) ? t('- Please choose -') : t('- None selected -'); |
|
422 } |
|
423 else { |
|
424 $blank = ($vocabulary->required) ? 0 : t('- None -'); |
|
425 } |
|
426 |
|
427 return _taxonomy_term_select(check_plain($vocabulary->name), $name, $value, $vid, $help, intval($vocabulary->multiple), $blank); |
|
428 } |
|
429 |
|
430 /** |
|
431 * Generate a set of options for selecting a term from all vocabularies. |
|
432 */ |
|
433 function taxonomy_form_all($free_tags = 0) { |
|
434 $vocabularies = taxonomy_get_vocabularies(); |
|
435 $options = array(); |
|
436 foreach ($vocabularies as $vid => $vocabulary) { |
|
437 if ($vocabulary->tags && !$free_tags) { continue; } |
|
438 $tree = taxonomy_get_tree($vid); |
|
439 if ($tree && (count($tree) > 0)) { |
|
440 $options[$vocabulary->name] = array(); |
|
441 foreach ($tree as $term) { |
|
442 $options[$vocabulary->name][$term->tid] = str_repeat('-', $term->depth) . $term->name; |
|
443 } |
|
444 } |
|
445 } |
|
446 return $options; |
|
447 } |
|
448 |
|
449 /** |
|
450 * Return an array of all vocabulary objects. |
|
451 * |
|
452 * @param $type |
|
453 * If set, return only those vocabularies associated with this node type. |
|
454 */ |
|
455 function taxonomy_get_vocabularies($type = NULL) { |
|
456 if ($type) { |
|
457 $result = db_query(db_rewrite_sql("SELECT v.vid, 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", 'v', 'vid'), $type); |
|
458 } |
|
459 else { |
|
460 $result = db_query(db_rewrite_sql('SELECT v.*, n.type FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} n ON v.vid = n.vid ORDER BY v.weight, v.name', 'v', 'vid')); |
|
461 } |
|
462 |
|
463 $vocabularies = array(); |
|
464 $node_types = array(); |
|
465 while ($voc = db_fetch_object($result)) { |
|
466 // If no node types are associated with a vocabulary, the LEFT JOIN will |
|
467 // return a NULL value for type. |
|
468 if (isset($voc->type)) { |
|
469 $node_types[$voc->vid][$voc->type] = $voc->type; |
|
470 unset($voc->type); |
|
471 $voc->nodes = $node_types[$voc->vid]; |
|
472 } |
|
473 elseif (!isset($voc->nodes)) { |
|
474 $voc->nodes = array(); |
|
475 } |
|
476 $vocabularies[$voc->vid] = $voc; |
|
477 } |
|
478 |
|
479 return $vocabularies; |
|
480 } |
|
481 |
|
482 /** |
|
483 * Implementation of hook_form_alter(). |
|
484 * Generate a form for selecting terms to associate with a node. |
|
485 * We check for taxonomy_override_selector before loading the full |
|
486 * vocabulary, so contrib modules can intercept before hook_form_alter |
|
487 * and provide scalable alternatives. |
|
488 */ |
|
489 function taxonomy_form_alter(&$form, $form_state, $form_id) { |
|
490 if (isset($form['type']) && isset($form['#node']) && (!variable_get('taxonomy_override_selector', FALSE)) && $form['type']['#value'] .'_node_form' == $form_id) { |
|
491 $node = $form['#node']; |
|
492 |
|
493 if (!isset($node->taxonomy)) { |
|
494 $terms = empty($node->nid) ? array() : taxonomy_node_get_terms($node); |
|
495 } |
|
496 else { |
|
497 // After preview the terms must be converted to objects. |
|
498 if (isset($form_state['node_preview'])) { |
|
499 $node->taxonomy = taxonomy_preview_terms($node); |
|
500 } |
|
501 $terms = $node->taxonomy; |
|
502 } |
|
503 |
|
504 $c = db_query(db_rewrite_sql("SELECT v.* FROM {vocabulary} v INNER JOIN {vocabulary_node_types} n ON v.vid = n.vid WHERE n.type = '%s' ORDER BY v.weight, v.name", 'v', 'vid'), $node->type); |
|
505 |
|
506 while ($vocabulary = db_fetch_object($c)) { |
|
507 if ($vocabulary->tags) { |
|
508 if (isset($form_state['node_preview'])) { |
|
509 // Typed string can be changed by the user before preview, |
|
510 // so we just insert the tags directly as provided in the form. |
|
511 $typed_string = $node->taxonomy['tags'][$vocabulary->vid]; |
|
512 } |
|
513 else { |
|
514 $typed_string = taxonomy_implode_tags($terms, $vocabulary->vid) . (array_key_exists('tags', $terms) ? $terms['tags'][$vocabulary->vid] : NULL); |
|
515 } |
|
516 if ($vocabulary->help) { |
|
517 $help = filter_xss_admin($vocabulary->help); |
|
518 } |
|
519 else { |
|
520 $help = t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".'); |
|
521 } |
|
522 $form['taxonomy']['tags'][$vocabulary->vid] = array('#type' => 'textfield', |
|
523 '#title' => $vocabulary->name, |
|
524 '#description' => $help, |
|
525 '#required' => $vocabulary->required, |
|
526 '#default_value' => $typed_string, |
|
527 '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid, |
|
528 '#weight' => $vocabulary->weight, |
|
529 '#maxlength' => 1024, |
|
530 ); |
|
531 } |
|
532 else { |
|
533 // Extract terms belonging to the vocabulary in question. |
|
534 $default_terms = array(); |
|
535 foreach ($terms as $term) { |
|
536 // Free tagging has no default terms and also no vid after preview. |
|
537 if (isset($term->vid) && $term->vid == $vocabulary->vid) { |
|
538 $default_terms[$term->tid] = $term; |
|
539 } |
|
540 } |
|
541 $form['taxonomy'][$vocabulary->vid] = taxonomy_form($vocabulary->vid, array_keys($default_terms), filter_xss_admin($vocabulary->help)); |
|
542 $form['taxonomy'][$vocabulary->vid]['#weight'] = $vocabulary->weight; |
|
543 $form['taxonomy'][$vocabulary->vid]['#required'] = $vocabulary->required; |
|
544 } |
|
545 } |
|
546 if (!empty($form['taxonomy']) && is_array($form['taxonomy'])) { |
|
547 if (count($form['taxonomy']) > 1) { |
|
548 // Add fieldset only if form has more than 1 element. |
|
549 $form['taxonomy'] += array( |
|
550 '#type' => 'fieldset', |
|
551 '#title' => t('Vocabularies'), |
|
552 '#collapsible' => TRUE, |
|
553 '#collapsed' => FALSE, |
|
554 ); |
|
555 } |
|
556 $form['taxonomy']['#weight'] = -3; |
|
557 $form['taxonomy']['#tree'] = TRUE; |
|
558 } |
|
559 } |
|
560 } |
|
561 |
|
562 /** |
|
563 * Helper function to convert terms after a preview. |
|
564 * |
|
565 * After preview the tags are an array instead of proper objects. This function |
|
566 * converts them back to objects with the exception of 'free tagging' terms, |
|
567 * because new tags can be added by the user before preview and those do not |
|
568 * yet exist in the database. We therefore save those tags as a string so |
|
569 * we can fill the form again after the preview. |
|
570 */ |
|
571 function taxonomy_preview_terms($node) { |
|
572 $taxonomy = array(); |
|
573 if (isset($node->taxonomy)) { |
|
574 foreach ($node->taxonomy as $key => $term) { |
|
575 unset($node->taxonomy[$key]); |
|
576 // A 'Multiple select' and a 'Free tagging' field returns an array. |
|
577 if (is_array($term)) { |
|
578 foreach ($term as $tid) { |
|
579 if ($key == 'tags') { |
|
580 // Free tagging; the values will be saved for later as strings |
|
581 // instead of objects to fill the form again. |
|
582 $taxonomy['tags'] = $term; |
|
583 } |
|
584 else { |
|
585 $taxonomy[$tid] = taxonomy_get_term($tid); |
|
586 } |
|
587 } |
|
588 } |
|
589 // A 'Single select' field returns the term id. |
|
590 elseif ($term) { |
|
591 $taxonomy[$term] = taxonomy_get_term($term); |
|
592 } |
|
593 } |
|
594 } |
|
595 return $taxonomy; |
|
596 } |
|
597 |
|
598 /** |
|
599 * Find all terms associated with the given node, within one vocabulary. |
|
600 */ |
|
601 function taxonomy_node_get_terms_by_vocabulary($node, $vid, $key = 'tid') { |
|
602 $result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_node} r ON r.tid = t.tid WHERE t.vid = %d AND r.vid = %d ORDER BY weight', 't', 'tid'), $vid, $node->vid); |
|
603 $terms = array(); |
|
604 while ($term = db_fetch_object($result)) { |
|
605 $terms[$term->$key] = $term; |
|
606 } |
|
607 return $terms; |
|
608 } |
|
609 |
|
610 /** |
|
611 * Find all terms associated with the given node, ordered by vocabulary and term weight. |
|
612 */ |
|
613 function taxonomy_node_get_terms($node, $key = 'tid') { |
|
614 static $terms; |
|
615 |
|
616 if (!isset($terms[$node->vid][$key])) { |
|
617 $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_node} r INNER JOIN {term_data} t ON r.tid = t.tid INNER JOIN {vocabulary} v ON t.vid = v.vid WHERE r.vid = %d ORDER BY v.weight, t.weight, t.name', 't', 'tid'), $node->vid); |
|
618 $terms[$node->vid][$key] = array(); |
|
619 while ($term = db_fetch_object($result)) { |
|
620 $terms[$node->vid][$key][$term->$key] = $term; |
|
621 } |
|
622 } |
|
623 return $terms[$node->vid][$key]; |
|
624 } |
|
625 |
|
626 /** |
|
627 * Make sure incoming vids are free tagging enabled. |
|
628 */ |
|
629 function taxonomy_node_validate(&$node) { |
|
630 if (!empty($node->taxonomy)) { |
|
631 $terms = $node->taxonomy; |
|
632 if (!empty($terms['tags'])) { |
|
633 foreach ($terms['tags'] as $vid => $vid_value) { |
|
634 $vocabulary = taxonomy_vocabulary_load($vid); |
|
635 if (empty($vocabulary->tags)) { |
|
636 // see form_get_error $key = implode('][', $element['#parents']); |
|
637 // on why this is the key |
|
638 form_set_error("taxonomy][tags][$vid", t('The %name vocabulary can not be modified in this way.', array('%name' => $vocabulary->name))); |
|
639 } |
|
640 } |
|
641 } |
|
642 } |
|
643 } |
|
644 |
|
645 /** |
|
646 * Save term associations for a given node. |
|
647 */ |
|
648 function taxonomy_node_save($node, $terms) { |
|
649 |
|
650 taxonomy_node_delete_revision($node); |
|
651 |
|
652 // Free tagging vocabularies do not send their tids in the form, |
|
653 // so we'll detect them here and process them independently. |
|
654 if (isset($terms['tags'])) { |
|
655 $typed_input = $terms['tags']; |
|
656 unset($terms['tags']); |
|
657 |
|
658 foreach ($typed_input as $vid => $vid_value) { |
|
659 $typed_terms = drupal_explode_tags($vid_value); |
|
660 |
|
661 $inserted = array(); |
|
662 foreach ($typed_terms as $typed_term) { |
|
663 // See if the term exists in the chosen vocabulary |
|
664 // and return the tid; otherwise, add a new record. |
|
665 $possibilities = taxonomy_get_term_by_name($typed_term); |
|
666 $typed_term_tid = NULL; // tid match, if any. |
|
667 foreach ($possibilities as $possibility) { |
|
668 if ($possibility->vid == $vid) { |
|
669 $typed_term_tid = $possibility->tid; |
|
670 } |
|
671 } |
|
672 |
|
673 if (!$typed_term_tid) { |
|
674 $edit = array('vid' => $vid, 'name' => $typed_term); |
|
675 $status = taxonomy_save_term($edit); |
|
676 $typed_term_tid = $edit['tid']; |
|
677 } |
|
678 |
|
679 // Defend against duplicate, differently cased tags |
|
680 if (!isset($inserted[$typed_term_tid])) { |
|
681 db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $typed_term_tid); |
|
682 $inserted[$typed_term_tid] = TRUE; |
|
683 } |
|
684 } |
|
685 } |
|
686 } |
|
687 |
|
688 if (is_array($terms)) { |
|
689 foreach ($terms as $term) { |
|
690 if (is_array($term)) { |
|
691 foreach ($term as $tid) { |
|
692 if ($tid) { |
|
693 db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $tid); |
|
694 } |
|
695 } |
|
696 } |
|
697 else if (is_object($term)) { |
|
698 db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $term->tid); |
|
699 } |
|
700 else if ($term) { |
|
701 db_query('INSERT INTO {term_node} (nid, vid, tid) VALUES (%d, %d, %d)', $node->nid, $node->vid, $term); |
|
702 } |
|
703 } |
|
704 } |
|
705 } |
|
706 |
|
707 /** |
|
708 * Remove associations of a node to its terms. |
|
709 */ |
|
710 function taxonomy_node_delete($node) { |
|
711 db_query('DELETE FROM {term_node} WHERE nid = %d', $node->nid); |
|
712 } |
|
713 |
|
714 /** |
|
715 * Remove associations of a node to its terms. |
|
716 */ |
|
717 function taxonomy_node_delete_revision($node) { |
|
718 db_query('DELETE FROM {term_node} WHERE vid = %d', $node->vid); |
|
719 } |
|
720 |
|
721 /** |
|
722 * Implementation of hook_node_type(). |
|
723 */ |
|
724 function taxonomy_node_type($op, $info) { |
|
725 if ($op == 'update' && !empty($info->old_type) && $info->type != $info->old_type) { |
|
726 db_query("UPDATE {vocabulary_node_types} SET type = '%s' WHERE type = '%s'", $info->type, $info->old_type); |
|
727 } |
|
728 elseif ($op == 'delete') { |
|
729 db_query("DELETE FROM {vocabulary_node_types} WHERE type = '%s'", $info->type); |
|
730 } |
|
731 } |
|
732 |
|
733 /** |
|
734 * Find all term objects related to a given term ID. |
|
735 */ |
|
736 function taxonomy_get_related($tid, $key = 'tid') { |
|
737 if ($tid) { |
|
738 $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); |
|
739 $related = array(); |
|
740 while ($term = db_fetch_object($result)) { |
|
741 $related[$term->$key] = $term; |
|
742 } |
|
743 return $related; |
|
744 } |
|
745 else { |
|
746 return array(); |
|
747 } |
|
748 } |
|
749 |
|
750 /** |
|
751 * Find all parents of a given term ID. |
|
752 */ |
|
753 function taxonomy_get_parents($tid, $key = 'tid') { |
|
754 if ($tid) { |
|
755 $result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.parent = t.tid WHERE h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid); |
|
756 $parents = array(); |
|
757 while ($parent = db_fetch_object($result)) { |
|
758 $parents[$parent->$key] = $parent; |
|
759 } |
|
760 return $parents; |
|
761 } |
|
762 else { |
|
763 return array(); |
|
764 } |
|
765 } |
|
766 |
|
767 /** |
|
768 * Find all ancestors of a given term ID. |
|
769 */ |
|
770 function taxonomy_get_parents_all($tid) { |
|
771 $parents = array(); |
|
772 if ($tid) { |
|
773 $parents[] = taxonomy_get_term($tid); |
|
774 $n = 0; |
|
775 while ($parent = taxonomy_get_parents($parents[$n]->tid)) { |
|
776 $parents = array_merge($parents, $parent); |
|
777 $n++; |
|
778 } |
|
779 } |
|
780 return $parents; |
|
781 } |
|
782 |
|
783 /** |
|
784 * Find all children of a term ID. |
|
785 */ |
|
786 function taxonomy_get_children($tid, $vid = 0, $key = 'tid') { |
|
787 if ($vid) { |
|
788 $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE t.vid = %d AND h.parent = %d ORDER BY weight, name', 't', 'tid'), $vid, $tid); |
|
789 } |
|
790 else { |
|
791 $result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE parent = %d ORDER BY weight, name', 't', 'tid'), $tid); |
|
792 } |
|
793 $children = array(); |
|
794 while ($term = db_fetch_object($result)) { |
|
795 $children[$term->$key] = $term; |
|
796 } |
|
797 return $children; |
|
798 } |
|
799 |
|
800 /** |
|
801 * Create a hierarchical representation of a vocabulary. |
|
802 * |
|
803 * @param $vid |
|
804 * Which vocabulary to generate the tree for. |
|
805 * |
|
806 * @param $parent |
|
807 * The term ID under which to generate the tree. If 0, generate the tree |
|
808 * for the entire vocabulary. |
|
809 * |
|
810 * @param $depth |
|
811 * Internal use only. |
|
812 * |
|
813 * @param $max_depth |
|
814 * The number of levels of the tree to return. Leave NULL to return all levels. |
|
815 * |
|
816 * @return |
|
817 * An array of all term objects in the tree. Each term object is extended |
|
818 * to have "depth" and "parents" attributes in addition to its normal ones. |
|
819 * Results are statically cached. |
|
820 */ |
|
821 function taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL) { |
|
822 static $children, $parents, $terms; |
|
823 |
|
824 $depth++; |
|
825 |
|
826 // We cache trees, so it's not CPU-intensive to call get_tree() on a term |
|
827 // and its children, too. |
|
828 if (!isset($children[$vid])) { |
|
829 $children[$vid] = array(); |
|
830 |
|
831 $result = db_query(db_rewrite_sql('SELECT t.tid, t.*, parent FROM {term_data} t INNER JOIN {term_hierarchy} h ON t.tid = h.tid WHERE t.vid = %d ORDER BY weight, name', 't', 'tid'), $vid); |
|
832 while ($term = db_fetch_object($result)) { |
|
833 $children[$vid][$term->parent][] = $term->tid; |
|
834 $parents[$vid][$term->tid][] = $term->parent; |
|
835 $terms[$vid][$term->tid] = $term; |
|
836 } |
|
837 } |
|
838 |
|
839 $max_depth = (is_null($max_depth)) ? count($children[$vid]) : $max_depth; |
|
840 $tree = array(); |
|
841 if ($max_depth > $depth && !empty($children[$vid][$parent])) { |
|
842 foreach ($children[$vid][$parent] as $child) { |
|
843 $term = drupal_clone($terms[$vid][$child]); |
|
844 $term->depth = $depth; |
|
845 // The "parent" attribute is not useful, as it would show one parent only. |
|
846 unset($term->parent); |
|
847 $term->parents = $parents[$vid][$child]; |
|
848 $tree[] = $term; |
|
849 if (!empty($children[$vid][$child])) { |
|
850 $tree = array_merge($tree, taxonomy_get_tree($vid, $child, $depth, $max_depth)); |
|
851 } |
|
852 } |
|
853 } |
|
854 |
|
855 return $tree; |
|
856 } |
|
857 |
|
858 /** |
|
859 * Return an array of synonyms of the given term ID. |
|
860 */ |
|
861 function taxonomy_get_synonyms($tid) { |
|
862 if ($tid) { |
|
863 $synonyms = array(); |
|
864 $result = db_query('SELECT name FROM {term_synonym} WHERE tid = %d', $tid); |
|
865 while ($synonym = db_fetch_array($result)) { |
|
866 $synonyms[] = $synonym['name']; |
|
867 } |
|
868 return $synonyms; |
|
869 } |
|
870 else { |
|
871 return array(); |
|
872 } |
|
873 } |
|
874 |
|
875 /** |
|
876 * Return the term object that has the given string as a synonym. |
|
877 */ |
|
878 function taxonomy_get_synonym_root($synonym) { |
|
879 return db_fetch_object(db_query("SELECT * FROM {term_synonym} s, {term_data} t WHERE t.tid = s.tid AND s.name = '%s'", $synonym)); |
|
880 } |
|
881 |
|
882 /** |
|
883 * Count the number of published nodes classified by a term. |
|
884 * |
|
885 * @param $tid |
|
886 * The term's ID |
|
887 * |
|
888 * @param $type |
|
889 * The $node->type. If given, taxonomy_term_count_nodes only counts |
|
890 * nodes of $type that are classified with the term $tid. |
|
891 * |
|
892 * @return int |
|
893 * An integer representing a number of nodes. |
|
894 * Results are statically cached. |
|
895 */ |
|
896 function taxonomy_term_count_nodes($tid, $type = 0) { |
|
897 static $count; |
|
898 |
|
899 if (!isset($count[$type])) { |
|
900 // $type == 0 always evaluates TRUE if $type is a string |
|
901 if (is_numeric($type)) { |
|
902 $result = db_query(db_rewrite_sql('SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 GROUP BY t.tid')); |
|
903 } |
|
904 else { |
|
905 $result = db_query(db_rewrite_sql("SELECT t.tid, COUNT(n.nid) AS c FROM {term_node} t INNER JOIN {node} n ON t.vid = n.vid WHERE n.status = 1 AND n.type = '%s' GROUP BY t.tid"), $type); |
|
906 } |
|
907 $count[$type] = array(); |
|
908 while ($term = db_fetch_object($result)) { |
|
909 $count[$type][$term->tid] = $term->c; |
|
910 } |
|
911 } |
|
912 $children_count = 0; |
|
913 foreach (_taxonomy_term_children($tid) as $c) { |
|
914 $children_count += taxonomy_term_count_nodes($c, $type); |
|
915 } |
|
916 return $children_count + (isset($count[$type][$tid]) ? $count[$type][$tid] : 0); |
|
917 } |
|
918 |
|
919 /** |
|
920 * Helper for taxonomy_term_count_nodes(). Used to find out |
|
921 * which terms are children of a parent term. |
|
922 * |
|
923 * @param $tid |
|
924 * The parent term's ID |
|
925 * |
|
926 * @return array |
|
927 * An array of term IDs representing the children of $tid. |
|
928 * Results are statically cached. |
|
929 * |
|
930 */ |
|
931 function _taxonomy_term_children($tid) { |
|
932 static $children; |
|
933 |
|
934 if (!isset($children)) { |
|
935 $result = db_query('SELECT tid, parent FROM {term_hierarchy}'); |
|
936 while ($term = db_fetch_object($result)) { |
|
937 $children[$term->parent][] = $term->tid; |
|
938 } |
|
939 } |
|
940 return isset($children[$tid]) ? $children[$tid] : array(); |
|
941 } |
|
942 |
|
943 /** |
|
944 * Try to map a string to an existing term, as for glossary use. |
|
945 * |
|
946 * Provides a case-insensitive and trimmed mapping, to maximize the |
|
947 * likelihood of a successful match. |
|
948 * |
|
949 * @param name |
|
950 * Name of the term to search for. |
|
951 * |
|
952 * @return |
|
953 * An array of matching term objects. |
|
954 */ |
|
955 function taxonomy_get_term_by_name($name) { |
|
956 $db_result = db_query(db_rewrite_sql("SELECT t.tid, t.* FROM {term_data} t WHERE LOWER(t.name) = LOWER('%s')", 't', 'tid'), trim($name)); |
|
957 $result = array(); |
|
958 while ($term = db_fetch_object($db_result)) { |
|
959 $result[] = $term; |
|
960 } |
|
961 |
|
962 return $result; |
|
963 } |
|
964 |
|
965 /** |
|
966 * Return the vocabulary object matching a vocabulary ID. |
|
967 * |
|
968 * @param $vid |
|
969 * The vocabulary's ID |
|
970 * |
|
971 * @return |
|
972 * The vocabulary object with all of its metadata, if exists, FALSE otherwise. |
|
973 * Results are statically cached. |
|
974 */ |
|
975 function taxonomy_vocabulary_load($vid) { |
|
976 static $vocabularies = array(); |
|
977 |
|
978 if (!isset($vocabularies[$vid])) { |
|
979 // Initialize so if this vocabulary does not exist, we have |
|
980 // that cached, and we will not try to load this later. |
|
981 $vocabularies[$vid] = FALSE; |
|
982 // Try to load the data and fill up the object. |
|
983 $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', $vid); |
|
984 $node_types = array(); |
|
985 while ($voc = db_fetch_object($result)) { |
|
986 if (!empty($voc->type)) { |
|
987 $node_types[$voc->type] = $voc->type; |
|
988 } |
|
989 unset($voc->type); |
|
990 $voc->nodes = $node_types; |
|
991 $vocabularies[$vid] = $voc; |
|
992 } |
|
993 } |
|
994 |
|
995 // Return FALSE if this vocabulary does not exist. |
|
996 return !empty($vocabularies[$vid]) ? $vocabularies[$vid] : FALSE; |
|
997 } |
|
998 |
|
999 /** |
|
1000 * Return the term object matching a term ID. |
|
1001 * |
|
1002 * @param $tid |
|
1003 * A term's ID |
|
1004 * |
|
1005 * @return Object |
|
1006 * A term object. Results are statically cached. |
|
1007 */ |
|
1008 function taxonomy_get_term($tid) { |
|
1009 static $terms = array(); |
|
1010 |
|
1011 if (!isset($terms[$tid])) { |
|
1012 $terms[$tid] = db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = %d', $tid)); |
|
1013 } |
|
1014 |
|
1015 return $terms[$tid]; |
|
1016 } |
|
1017 |
|
1018 /** |
|
1019 * Create a select form element for a given taxonomy vocabulary. |
|
1020 * |
|
1021 * NOTE: This function expects input that has already been sanitized and is |
|
1022 * safe for display. Callers must properly sanitize the $title and |
|
1023 * $description arguments to prevent XSS vulnerabilities. |
|
1024 * |
|
1025 * @param $title |
|
1026 * The title of the vocabulary. This MUST be sanitized by the caller. |
|
1027 * @param $name |
|
1028 * Ignored. |
|
1029 * @param $value |
|
1030 * The currently selected terms from this vocabulary, if any. |
|
1031 * @param $vocabulary_id |
|
1032 * The vocabulary ID to build the form element for. |
|
1033 * @param $description |
|
1034 * Help text for the form element. This MUST be sanitized by the caller. |
|
1035 * @param $multiple |
|
1036 * Boolean to control if the form should use a single or multiple select. |
|
1037 * @param $blank |
|
1038 * Optional form choice to use when no value has been selected. |
|
1039 * @param $exclude |
|
1040 * Optional array of term ids to exclude in the selector. |
|
1041 * @return |
|
1042 * A FAPI form array to select terms from the given vocabulary. |
|
1043 * |
|
1044 * @see taxonomy_form() |
|
1045 * @see taxonomy_form_term() |
|
1046 */ |
|
1047 function _taxonomy_term_select($title, $name, $value, $vocabulary_id, $description, $multiple, $blank, $exclude = array()) { |
|
1048 $tree = taxonomy_get_tree($vocabulary_id); |
|
1049 $options = array(); |
|
1050 |
|
1051 if ($blank) { |
|
1052 $options[''] = $blank; |
|
1053 } |
|
1054 if ($tree) { |
|
1055 foreach ($tree as $term) { |
|
1056 if (!in_array($term->tid, $exclude)) { |
|
1057 $choice = new stdClass(); |
|
1058 $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name); |
|
1059 $options[] = $choice; |
|
1060 } |
|
1061 } |
|
1062 } |
|
1063 |
|
1064 return array('#type' => 'select', |
|
1065 '#title' => $title, |
|
1066 '#default_value' => $value, |
|
1067 '#options' => $options, |
|
1068 '#description' => $description, |
|
1069 '#multiple' => $multiple, |
|
1070 '#size' => $multiple ? min(9, count($options)) : 0, |
|
1071 '#weight' => -15, |
|
1072 '#theme' => 'taxonomy_term_select', |
|
1073 ); |
|
1074 } |
|
1075 |
|
1076 /** |
|
1077 * Format the selection field for choosing terms |
|
1078 * (by deafult the default selection field is used). |
|
1079 * |
|
1080 * @ingroup themeable |
|
1081 */ |
|
1082 function theme_taxonomy_term_select($element) { |
|
1083 return theme('select', $element); |
|
1084 } |
|
1085 |
|
1086 /** |
|
1087 * Finds all nodes that match selected taxonomy conditions. |
|
1088 * |
|
1089 * @param $tids |
|
1090 * An array of term IDs to match. |
|
1091 * @param $operator |
|
1092 * How to interpret multiple IDs in the array. Can be "or" or "and". |
|
1093 * @param $depth |
|
1094 * How many levels deep to traverse the taxonomy tree. Can be a nonnegative |
|
1095 * integer or "all". |
|
1096 * @param $pager |
|
1097 * Whether the nodes are to be used with a pager (the case on most Drupal |
|
1098 * pages) or not (in an XML feed, for example). |
|
1099 * @param $order |
|
1100 * The order clause for the query that retrieve the nodes. |
|
1101 * @return |
|
1102 * A resource identifier pointing to the query results. |
|
1103 */ |
|
1104 function taxonomy_select_nodes($tids = array(), $operator = 'or', $depth = 0, $pager = TRUE, $order = 'n.sticky DESC, n.created DESC') { |
|
1105 if (count($tids) > 0) { |
|
1106 // For each term ID, generate an array of descendant term IDs to the right depth. |
|
1107 $descendant_tids = array(); |
|
1108 if ($depth === 'all') { |
|
1109 $depth = NULL; |
|
1110 } |
|
1111 foreach ($tids as $index => $tid) { |
|
1112 $term = taxonomy_get_term($tid); |
|
1113 $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth); |
|
1114 $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree)); |
|
1115 } |
|
1116 |
|
1117 if ($operator == 'or') { |
|
1118 $args = call_user_func_array('array_merge', $descendant_tids); |
|
1119 $placeholders = db_placeholders($args, 'int'); |
|
1120 $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1 ORDER BY '. $order; |
|
1121 $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n INNER JOIN {term_node} tn ON n.vid = tn.vid WHERE tn.tid IN ('. $placeholders .') AND n.status = 1'; |
|
1122 } |
|
1123 else { |
|
1124 $joins = ''; |
|
1125 $wheres = ''; |
|
1126 $args = array(); |
|
1127 foreach ($descendant_tids as $index => $tids) { |
|
1128 $joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.vid = tn'. $index .'.vid'; |
|
1129 $wheres .= ' AND tn'. $index .'.tid IN ('. db_placeholders($tids, 'int') .')'; |
|
1130 $args = array_merge($args, $tids); |
|
1131 } |
|
1132 $sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY '. $order; |
|
1133 $sql_count = 'SELECT COUNT(DISTINCT(n.nid)) FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres; |
|
1134 } |
|
1135 $sql = db_rewrite_sql($sql); |
|
1136 $sql_count = db_rewrite_sql($sql_count); |
|
1137 if ($pager) { |
|
1138 $result = pager_query($sql, variable_get('default_nodes_main', 10), 0, $sql_count, $args); |
|
1139 } |
|
1140 else { |
|
1141 $result = db_query_range($sql, $args, 0, variable_get('feed_default_items', 10)); |
|
1142 } |
|
1143 } |
|
1144 |
|
1145 return $result; |
|
1146 } |
|
1147 |
|
1148 /** |
|
1149 * Accepts the result of a pager_query() call, such as that performed by |
|
1150 * taxonomy_select_nodes(), and formats each node along with a pager. |
|
1151 */ |
|
1152 function taxonomy_render_nodes($result) { |
|
1153 $output = ''; |
|
1154 $has_rows = FALSE; |
|
1155 while ($node = db_fetch_object($result)) { |
|
1156 $output .= node_view(node_load($node->nid), 1); |
|
1157 $has_rows = TRUE; |
|
1158 } |
|
1159 if ($has_rows) { |
|
1160 $output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0); |
|
1161 } |
|
1162 else { |
|
1163 $output .= '<p>'. t('There are currently no posts in this category.') .'</p>'; |
|
1164 } |
|
1165 return $output; |
|
1166 } |
|
1167 |
|
1168 /** |
|
1169 * Implementation of hook_nodeapi(). |
|
1170 */ |
|
1171 function taxonomy_nodeapi($node, $op, $arg = 0) { |
|
1172 switch ($op) { |
|
1173 case 'load': |
|
1174 $output['taxonomy'] = taxonomy_node_get_terms($node); |
|
1175 return $output; |
|
1176 |
|
1177 case 'insert': |
|
1178 if (!empty($node->taxonomy)) { |
|
1179 taxonomy_node_save($node, $node->taxonomy); |
|
1180 } |
|
1181 break; |
|
1182 |
|
1183 case 'update': |
|
1184 if (!empty($node->taxonomy)) { |
|
1185 taxonomy_node_save($node, $node->taxonomy); |
|
1186 } |
|
1187 break; |
|
1188 |
|
1189 case 'delete': |
|
1190 taxonomy_node_delete($node); |
|
1191 break; |
|
1192 |
|
1193 case 'delete revision': |
|
1194 taxonomy_node_delete_revision($node); |
|
1195 break; |
|
1196 |
|
1197 case 'validate': |
|
1198 taxonomy_node_validate($node); |
|
1199 break; |
|
1200 |
|
1201 case 'rss item': |
|
1202 return taxonomy_rss_item($node); |
|
1203 |
|
1204 case 'update index': |
|
1205 return taxonomy_node_update_index($node); |
|
1206 } |
|
1207 } |
|
1208 |
|
1209 /** |
|
1210 * Implementation of hook_nodeapi('update_index'). |
|
1211 */ |
|
1212 function taxonomy_node_update_index(&$node) { |
|
1213 $output = array(); |
|
1214 foreach ($node->taxonomy as $term) { |
|
1215 $output[] = $term->name; |
|
1216 } |
|
1217 if (count($output)) { |
|
1218 return '<strong>('. implode(', ', $output) .')</strong>'; |
|
1219 } |
|
1220 } |
|
1221 |
|
1222 /** |
|
1223 * Parses a comma or plus separated string of term IDs. |
|
1224 * |
|
1225 * @param $str_tids |
|
1226 * A string of term IDs, separated by plus or comma. |
|
1227 * comma (,) means AND |
|
1228 * plus (+) means OR |
|
1229 * |
|
1230 * @return an associative array with an operator key (either 'and' |
|
1231 * or 'or') and a tid key containing an array of the term ids. |
|
1232 */ |
|
1233 function taxonomy_terms_parse_string($str_tids) { |
|
1234 $terms = array('operator' => '', 'tids' => array()); |
|
1235 if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str_tids)) { |
|
1236 $terms['operator'] = 'or'; |
|
1237 // The '+' character in a query string may be parsed as ' '. |
|
1238 $terms['tids'] = preg_split('/[+ ]/', $str_tids); |
|
1239 } |
|
1240 else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str_tids)) { |
|
1241 $terms['operator'] = 'and'; |
|
1242 $terms['tids'] = explode(',', $str_tids); |
|
1243 } |
|
1244 return $terms; |
|
1245 } |
|
1246 |
|
1247 /** |
|
1248 * Provides category information for RSS feeds. |
|
1249 */ |
|
1250 function taxonomy_rss_item($node) { |
|
1251 $output = array(); |
|
1252 foreach ($node->taxonomy as $term) { |
|
1253 $output[] = array('key' => 'category', |
|
1254 'value' => $term->name, |
|
1255 'attributes' => array('domain' => url('taxonomy/term/'. $term->tid, array('absolute' => TRUE)))); |
|
1256 } |
|
1257 return $output; |
|
1258 } |
|
1259 |
|
1260 /** |
|
1261 * Implementation of hook_help(). |
|
1262 */ |
|
1263 function taxonomy_help($path, $arg) { |
|
1264 switch ($path) { |
|
1265 case 'admin/help#taxonomy': |
|
1266 $output = '<p>'. t('The taxonomy module allows you to categorize content using various systems of classification. Free-tagging vocabularies are created by users on the fly when they submit posts (as commonly found in blogs and social bookmarking applications). Controlled vocabularies allow for administrator-defined short lists of terms as well as complex hierarchies with multiple relationships between different terms. These methods can be applied to different content types and combined together to create a powerful and flexible method of classifying and presenting your content.') .'</p>'; |
|
1267 $output .= '<p>'. t('For example, when creating a recipe site, you might want to classify posts by both the type of meal and preparation time. A vocabulary for each allows you to categorize using each criteria independently instead of creating a tag for every possible combination.') .'</p>'; |
|
1268 $output .= '<p>'. t('Type of Meal: <em>Appetizer, Main Course, Salad, Dessert</em>') .'</p>'; |
|
1269 $output .= '<p>'. t('Preparation Time: <em>0-30mins, 30-60mins, 1-2 hrs, 2hrs+</em>') .'</p>'; |
|
1270 $output .= '<p>'. t("Each taxonomy term (often called a 'category' or 'tag' in other systems) automatically provides lists of posts and a corresponding RSS feed. These taxonomy/term URLs can be manipulated to generate AND and OR lists of posts classified with terms. In our recipe site example, it then becomes easy to create pages displaying 'Main courses', '30 minute recipes', or '30 minute main courses and appetizers' by using terms on their own or in combination with others. There are a significant number of contributed modules which you to alter and extend the behavior of the core module for both display and organization of terms.") .'</p>'; |
|
1271 $output .= '<p>'. t("Terms can also be organized in parent/child relationships from the admin interface. An example would be a vocabulary grouping countries under their parent geo-political regions. The taxonomy module also enables advanced implementations of hierarchy, for example placing Turkey in both the 'Middle East' and 'Europe'.") .'</p>'; |
|
1272 $output .= '<p>'. t('The taxonomy module supports the use of both synonyms and related terms, but does not directly use this functionality. However, optional contributed or custom modules may make full use of these advanced features.') .'</p>'; |
|
1273 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@taxonomy">Taxonomy module</a>.', array('@taxonomy' => 'http://drupal.org/handbook/modules/taxonomy/')) .'</p>'; |
|
1274 return $output; |
|
1275 case 'admin/content/taxonomy': |
|
1276 $output = '<p>'. t("The taxonomy module allows you to categorize your content using both tags and administrator defined terms. It is a flexible tool for classifying content with many advanced features. To begin, create a 'Vocabulary' to hold one set of terms or tags. You can create one free-tagging vocabulary for everything, or separate controlled vocabularies to define the various properties of your content, for example 'Countries' or 'Colors'.") .'</p>'; |
|
1277 $output .= '<p>'. t('Use the list below to configure and review the vocabularies defined on your site, or to list and manage the terms (tags) they contain. A vocabulary may (optionally) be tied to specific content types as shown in the <em>Type</em> column and, if so, will be displayed when creating or editing posts of that type. Multiple vocabularies tied to the same content type will be displayed in the order shown below. To change the order of a vocabulary, grab a drag-and-drop handle under the <em>Name</em> column and drag it to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Remember that your changes will not be saved until you click the <em>Save</em> button at the bottom of the page.') .'</p>'; |
|
1278 return $output; |
|
1279 case 'admin/content/taxonomy/%': |
|
1280 $vocabulary = taxonomy_vocabulary_load($arg[3]); |
|
1281 if ($vocabulary->tags) { |
|
1282 return '<p>'. t('%capital_name is a free-tagging vocabulary. To change the name or description of a term, click the <em>edit</em> link next to the term.', array('%capital_name' => drupal_ucfirst($vocabulary->name))) .'</p>'; |
|
1283 } |
|
1284 switch ($vocabulary->hierarchy) { |
|
1285 case 0: |
|
1286 return '<p>'. t('%capital_name is a flat vocabulary. You may organize the terms in the %name vocabulary by using the handles on the left side of the table. To change the name or description of a term, click the <em>edit</em> link next to the term.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name)) .'</p>'; |
|
1287 case 1: |
|
1288 return '<p>'. t('%capital_name is a single hierarchy vocabulary. You may organize the terms in the %name vocabulary by using the handles on the left side of the table. To change the name or description of a term, click the <em>edit</em> link next to the term.', array('%capital_name' => drupal_ucfirst($vocabulary->name), '%name' => $vocabulary->name)) .'</p>'; |
|
1289 case 2: |
|
1290 return '<p>'. t('%capital_name is a multiple hierarchy vocabulary. To change the name or description of a term, click the <em>edit</em> link next to the term. Drag and drop of multiple hierarchies is not supported, but you can re-enable drag and drop support by editing each term to include only a single parent.', array('%capital_name' => drupal_ucfirst($vocabulary->name))) .'</p>'; |
|
1291 } |
|
1292 case 'admin/content/taxonomy/add/vocabulary': |
|
1293 return '<p>'. t('Define how your vocabulary will be presented to administrators and users, and which content types to categorize with it. Tags allows users to create terms when submitting posts by typing a comma separated list. Otherwise terms are chosen from a select list and can only be created by users with the "administer taxonomy" permission.') .'</p>'; |
|
1294 } |
|
1295 } |
|
1296 |
|
1297 /** |
|
1298 * Helper function for array_map purposes. |
|
1299 */ |
|
1300 function _taxonomy_get_tid_from_term($term) { |
|
1301 return $term->tid; |
|
1302 } |
|
1303 |
|
1304 /** |
|
1305 * Implode a list of tags of a certain vocabulary into a string. |
|
1306 */ |
|
1307 function taxonomy_implode_tags($tags, $vid = NULL) { |
|
1308 $typed_tags = array(); |
|
1309 foreach ($tags as $tag) { |
|
1310 // Extract terms belonging to the vocabulary in question. |
|
1311 if (is_null($vid) || $tag->vid == $vid) { |
|
1312 |
|
1313 // Commas and quotes in tag names are special cases, so encode 'em. |
|
1314 if (strpos($tag->name, ',') !== FALSE || strpos($tag->name, '"') !== FALSE) { |
|
1315 $tag->name = '"'. str_replace('"', '""', $tag->name) .'"'; |
|
1316 } |
|
1317 |
|
1318 $typed_tags[] = $tag->name; |
|
1319 } |
|
1320 } |
|
1321 return implode(', ', $typed_tags); |
|
1322 } |
|
1323 |
|
1324 /** |
|
1325 * Implementation of hook_hook_info(). |
|
1326 */ |
|
1327 function taxonomy_hook_info() { |
|
1328 return array( |
|
1329 'taxonomy' => array( |
|
1330 'taxonomy' => array( |
|
1331 'insert' => array( |
|
1332 'runs when' => t('After saving a new term to the database'), |
|
1333 ), |
|
1334 'update' => array( |
|
1335 'runs when' => t('After saving an updated term to the database'), |
|
1336 ), |
|
1337 'delete' => array( |
|
1338 'runs when' => t('After deleting a term') |
|
1339 ), |
|
1340 ), |
|
1341 ), |
|
1342 ); |
|
1343 } |