You are here

function _taxonomy_edge_copy_subtree in Taxonomy Edge 7.2

Copy path and children from to new parent

Parameters

$vid: Vocabulary ID

$pid: Path ID

$new_parent_pid: Path ID of new parent

1 call to _taxonomy_edge_copy_subtree()
_taxonomy_edge_taxonomy_term_update in ./taxonomy_edge.module
Update a term in the edge tree.

File

./taxonomy_edge.module, line 485
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_copy_subtree($vid, $pid, $new_parent_pid) {

  // Copy paths of all children
  db_query("INSERT INTO {taxonomy_term_edge_path} (tid, vid, temp_pid)\n  SELECT p.tid, :vid, p.pid AS temp_pid\n  FROM {taxonomy_term_edge_path} p\n  JOIN {taxonomy_term_edge} e ON e.pid = p.pid\n  WHERE e.parent = :pid\n  AND e.vid = :vid\n  ", array(
    ':pid' => $pid,
    ':vid' => $vid,
  ));

  // Copy subtree
  db_query("INSERT INTO {taxonomy_term_edge} (vid, pid, parent, distance)\n  SELECT :vid, p.pid, p2.pid AS parent, e2.distance\n  FROM {taxonomy_term_edge} e\n  JOIN {taxonomy_term_edge} e2 ON e2.pid = e.pid\n  JOIN {taxonomy_term_edge_path} p ON p.temp_pid = e2.pid AND p.vid = :vid\n  JOIN {taxonomy_term_edge_path} p2 ON p2.temp_pid = e2.parent AND p2.vid = :vid\n  WHERE e.parent = :pid\n  AND e2.distance <= e.distance\n  AND e.vid = :vid AND e2.vid = :vid\n  ", array(
    ':pid' => $pid,
    ':vid' => $vid,
  ));

  // Build parents for new subtree
  db_query("INSERT INTO {taxonomy_term_edge} (vid, pid, parent, distance)\n  SELECT :vid, p.pid, e.parent, e.distance + e2.distance + 1 AS distance\n  FROM {taxonomy_term_edge} e\n  INNER JOIN {taxonomy_term_edge} e2 ON e.pid  = :parent AND e2.parent = :pid\n  INNER JOIN {taxonomy_term_edge_path} p ON p.temp_pid = e2.pid AND p.vid = :vid\n  WHERE e.vid = :vid AND e2.vid = :vid\n  ", array(
    ':pid' => $pid,
    ':parent' => $new_parent_pid,
    ':vid' => $vid,
  ));

  // Cleanup
  db_query("UPDATE {taxonomy_term_edge_path} SET temp_pid = 0 WHERE vid = :vid AND temp_pid > 0", array(
    ':vid' => $vid,
  ));
}