From 9d21c0c88cf64733118451d5e11cf0e087abe0b9 Mon Sep 17 00:00:00 2001 From: Silvio Date: Mon, 13 Jun 2011 16:23:29 -0300 Subject: Initial code for term cleanup --- taxonomy_node_tree.module | 73 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/taxonomy_node_tree.module b/taxonomy_node_tree.module index 07ea515..cbd9b26 100644 --- a/taxonomy_node_tree.module +++ b/taxonomy_node_tree.module @@ -118,18 +118,78 @@ function taxonomy_node_tree_sort(&$tree, $term) { * @param $tree * Tree with term objects. * + * @param $clean + * Set to TRUE to remove branches without nodes. + * * @return * Hierarchical term tree. */ -function taxonomy_node_tree_hierarchy($tree) { +function taxonomy_node_tree_hierarchy($tree, $clean = FALSE) { if (is_array($tree) && $tree != NULL) { - foreach ($tree as $term) { + foreach ($tree as &$term) { taxonomy_node_tree_sort($tree, $term); + + if ($clean) { + taxonomy_node_tree_clean($term); + } } } return $tree; } +/** + * @todo + */ +function taxonomy_node_tree_clean(&$term) { + $list = taxonomy_node_tree_get_empty($term); + taxonomy_node_tree_remove($term, $list); +} + +function taxonomy_node_tree_remove(&$term, $list) { + if (in_array($term->tid, $list)) { + $term = NULL; + return; + } + + if (isset($term->below)) { + foreach ($term->below as &$below) { + taxonomy_node_tree_remove($below, $list); + } + } +} + +/** + * TODO: skip parents should be controlled by a parameter. + */ +function taxonomy_node_tree_get_empty($term, &$list = array()) { + if (taxonomy_node_tree_count($term) == 0) { + // Skip parents. + if ($term->parents[0] != 0) { + $list[] = $term->tid; + } + } + else if (isset($term->below)) { + foreach ($term->below as $below) { + taxonomy_node_tree_get_empty($below, $list); + } + } + + return $list; +} + +function taxonomy_node_tree_count($term, &$count = 0) { + if (isset($term->below)) { + foreach ($term->below as &$below) { + taxonomy_node_tree_count($below, $count); + } + } + else if (isset($term->nodes)) { + $count += count($term->nodes); + } + + return $count; +} + /** * Setup a full taxonomy node hierarchical tree. * @@ -142,13 +202,17 @@ function taxonomy_node_tree_hierarchy($tree) { * @param $terms * Array with term objects. * + * @param $clean + * Set to TRUE to remove branches without nodes. + * * @return * Hierarchical tree. */ -function taxonomy_node_tree_build($nodes, $terms) { +function taxonomy_node_tree_build($nodes, $terms, $clean = FALSE) { $tree = taxonomy_node_tree_index($nodes, $terms); $tree = taxonomy_node_tree_relation($tree); - $tree = taxonomy_node_tree_hierarchy($tree); + $tree = taxonomy_node_tree_hierarchy($tree, $clean); + return $tree; } @@ -176,7 +240,6 @@ function taxonomy_node_tree_build($nodes, $terms) { * @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. - * Results are statically cached. */ function taxonomy_node_tree_taxonomy_get_tree($vid, $parent = 0, $depth = -1, $max_depth = NULL, $cache = FALSE) { static $children, $parents, $terms; -- cgit v1.2.3