function _taxonomy_edge_build_parents in Taxonomy Edge 7.2
Build parent paths for a term.
Parameters
integer $tid: Term ID to build parent paths
array $parents: Parent term IDs for this term
1 call to _taxonomy_edge_build_parents()
- _taxonomy_edge_taxonomy_term_insert in ./
taxonomy_edge.module - Insert a term into the edge tree.
File
- ./
taxonomy_edge.module, line 372 - Selecting all children of a given taxonomy term can be a pain. This module makes it easier to do this, by maintaining a complete list of edges for each term using the adjecency matrix graph theory.
Code
function _taxonomy_edge_build_parents($vid, $tid, $parents = array()) {
// Add root node if we have parent = 0
$addroot = FALSE;
foreach ($parents as $idx => $parent) {
if ($parent == 0) {
$addroot = TRUE;
unset($parents[$idx]);
}
}
$addroot = $addroot || empty($parents);
if ($parents) {
$tx = db_transaction();
// Add paths
db_query("INSERT INTO {taxonomy_term_edge_path} (tid, vid, temp_pid) SELECT :tid, :vid, p.pid FROM {taxonomy_term_edge_path} p WHERE p.tid IN (:parents)", array(
':tid' => $tid,
':vid' => $vid,
':parents' => $parents,
));
// Add edges
db_query("INSERT INTO {taxonomy_term_edge} (vid, pid, parent, distance) SELECT :vid, p.pid, p.pid, 0 FROM {taxonomy_term_edge_path} p WHERE p.tid = :tid", array(
':tid' => $tid,
':vid' => $vid,
));
db_query("INSERT INTO {taxonomy_term_edge} (vid, pid, parent, distance) SELECT :vid, p.pid, e.parent, e.distance + 1 FROM {taxonomy_term_edge_path} p INNER JOIN {taxonomy_term_edge} e ON p.temp_pid = e.pid WHERE p.tid = :tid", array(
':tid' => $tid,
':vid' => $vid,
));
db_query("UPDATE {taxonomy_term_edge_path} SET temp_pid = 0 WHERE tid = :tid", array(
':tid' => $tid,
));
}
// Use optimized way of inserting for root nodes ...
if ($addroot) {
// Add path
$path = new stdClass();
$path->tid = $tid;
$path->vid = $vid;
drupal_write_record('taxonomy_term_edge_path', $path);
// Add edge
db_insert('taxonomy_term_edge')
->fields(array(
'pid' => $path->pid,
'parent' => $path->pid,
'distance' => 0,
'vid' => $vid,
))
->execute();
db_insert('taxonomy_term_edge')
->fields(array(
'pid' => $path->pid,
'parent' => taxonomy_edge_get_root_pid(),
'distance' => 1,
'vid' => $vid,
))
->execute();
}
}