| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file Taxidermy
|
| 6 |
* Taxidermy puts a modern face on taxonomy.module
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Load a term object from the database.
|
| 11 |
*
|
| 12 |
* @param $param
|
| 13 |
* Either the tid of the term or
|
| 14 |
* TODO an array of conditions to match against in the database query, such as name and vid
|
| 15 |
* @param $reset
|
| 16 |
* Whether to reset the internal taxidermy_term_load cache.
|
| 17 |
*
|
| 18 |
* @return
|
| 19 |
* A fully-populated term object.
|
| 20 |
*/
|
| 21 |
function taxidermy_term_load($param = array(), $reset = NULL) {
|
| 22 |
static $terms = array();
|
| 23 |
|
| 24 |
if ($reset) {
|
| 25 |
$terms = array();
|
| 26 |
}
|
| 27 |
|
| 28 |
$arguments = array();
|
| 29 |
if (is_numeric($param)) {
|
| 30 |
// Is the term statically cached?
|
| 31 |
if (isset($terms[$param])) {
|
| 32 |
return is_object($terms[$param]) ? drupal_clone($terms[$param]) : $terms[$param];
|
| 33 |
}
|
| 34 |
else {
|
| 35 |
$tid = $param;
|
| 36 |
}
|
| 37 |
}
|
| 38 |
else {
|
| 39 |
return FALSE;
|
| 40 |
}
|
| 41 |
|
| 42 |
$term = taxonomy_get_term($tid);
|
| 43 |
|
| 44 |
if ($term && $term->tid) {
|
| 45 |
module_invoke_all('taxonomy_term_load', $term);
|
| 46 |
$terms[$term->tid] = is_object($term) ? drupal_clone($term) : $term;
|
| 47 |
}
|
| 48 |
|
| 49 |
return $term;
|
| 50 |
}
|
| 51 |
|
| 52 |
/**
|
| 53 |
* Save a term object into the database.
|
| 54 |
*/
|
| 55 |
function taxidermy_term_save(&$term) {
|
| 56 |
// Let modules modify the term before it is saved to the database.
|
| 57 |
module_invoke_all('taxonomy_term_presave', $term);
|
| 58 |
|
| 59 |
$form_values = (array)$term;
|
| 60 |
$is_new = taxonomy_save_term($form_values);
|
| 61 |
|
| 62 |
// Call the term specific callback (if any).
|
| 63 |
module_invoke_all('taxonomy_term_save', $term);
|
| 64 |
}
|
| 65 |
|
| 66 |
/**
|
| 67 |
* Delete a term.
|
| 68 |
* diff against taxonomy_del_term()
|
| 69 |
*
|
| 70 |
* @param $tid
|
| 71 |
* The term ID.
|
| 72 |
* @return
|
| 73 |
* Status constant indicating deletion.
|
| 74 |
*/
|
| 75 |
function taxidermy_term_delete($tid) {
|
| 76 |
$tids = array($tid);
|
| 77 |
while ($tids) {
|
| 78 |
$children_tids = $orphans = array();
|
| 79 |
foreach ($tids as $tid) {
|
| 80 |
// See if any of the term's children are about to be become orphans:
|
| 81 |
if ($children = taxonomy_get_children($tid)) {
|
| 82 |
foreach ($children as $child) {
|
| 83 |
// If the term has multiple parents, we don't delete it.
|
| 84 |
$parents = taxonomy_get_parents($child->tid);
|
| 85 |
if (count($parents) == 1) {
|
| 86 |
$orphans[] = $child->tid;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
$term = taxidermy_term_load($tid);
|
| 92 |
module_invoke_all('taxonomy_term_delete', $term);
|
| 93 |
|
| 94 |
db_query('DELETE FROM {term_data} WHERE tid = %d', $tid);
|
| 95 |
db_query('DELETE FROM {term_hierarchy} WHERE tid = %d', $tid);
|
| 96 |
db_query('DELETE FROM {term_relation} WHERE tid1 = %d OR tid2 = %d', $tid, $tid);
|
| 97 |
db_query('DELETE FROM {term_synonym} WHERE tid = %d', $tid);
|
| 98 |
db_query('DELETE FROM {term_node} WHERE tid = %d', $tid);
|
| 99 |
|
| 100 |
module_invoke_all('taxonomy', 'delete', 'term', $term);
|
| 101 |
}
|
| 102 |
|
| 103 |
$tids = $orphans;
|
| 104 |
}
|
| 105 |
|
| 106 |
cache_clear_all();
|
| 107 |
|
| 108 |
return SAVED_DELETED;
|
| 109 |
}
|
| 110 |
|
| 111 |
function taxidermy_taxonomy_term_load(&$term) {
|
| 112 |
$term->parent = array_map('_taxonomy_get_tid_from_term', taxonomy_get_parents($term->tid));
|
| 113 |
$term->related = array_map('_taxonomy_get_tid_from_term', taxonomy_get_related($term->tid));
|
| 114 |
$term->synonyms = implode("\n", taxonomy_get_synonyms($term->tid));
|
| 115 |
}
|
| 116 |
|
| 117 |
function taxidermy_vocabulary_load($vid) {
|
| 118 |
static $vocabularies = array();
|
| 119 |
|
| 120 |
$vocabulary = taxonomy_vocabulary_load($vid);
|
| 121 |
|
| 122 |
if ($vocabulary && $vocabulary->vid) {
|
| 123 |
module_invoke_all('taxonomy_vocabulary_load', $vocabulary);
|
| 124 |
$vocabularies[$vocabulary->vid] = is_object($vocabulary) ? drupal_clone($vocabulary) : $vocabulary;
|
| 125 |
}
|
| 126 |
|
| 127 |
return $vocabulary;
|
| 128 |
}
|
| 129 |
|
| 130 |
function taxidermy_vocabulary_delete($vid) {
|
| 131 |
$vocabulary = taxidermy_vocabulary_load($vid);
|
| 132 |
module_invoke_all('taxonomy_vocabulary_delete', $vocabulary);
|
| 133 |
return taxonomy_del_vocabulary($vid);
|
| 134 |
}
|
| 135 |
|
| 136 |
function taxidermy_vocabulary_save($vocabulary) {
|
| 137 |
// Let modules modify the term before it is saved to the database.
|
| 138 |
module_invoke_all('taxonomy_vocabulary_presave', $vocabulary);
|
| 139 |
|
| 140 |
$form_values = (array)$vocabulary;
|
| 141 |
$is_new = taxonomy_save_vocabulary($form_values);
|
| 142 |
|
| 143 |
if ($is_new === SAVED_NEW) {
|
| 144 |
$op = 'insert';
|
| 145 |
}
|
| 146 |
else {
|
| 147 |
$op = 'update';
|
| 148 |
}
|
| 149 |
|
| 150 |
// Call the term specific callback (if any).
|
| 151 |
module_invoke_all('taxonomy_vocabulary_save', $vocabulary);
|
| 152 |
}
|