aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio <silvio@socioambiental.org>2014-03-26 16:19:12 -0300
committerSilvio <silvio@socioambiental.org>2014-03-26 16:19:12 -0300
commit06823f76fe2e91139f371a3d94dc34d92017712e (patch)
tree9725c024cfbabb971d629f42927a2f77422c8376
parent4f1619374b6001f62b4b198f79ec72fee5461c7f (diff)
downloadtaxonomy_node_tree-06823f76fe2e91139f371a3d94dc34d92017712e.tar.gz
taxonomy_node_tree-06823f76fe2e91139f371a3d94dc34d92017712e.tar.bz2
Rollback some changes from master branch
-rw-r--r--taxonomy_node_tree.module245
1 files changed, 232 insertions, 13 deletions
diff --git a/taxonomy_node_tree.module b/taxonomy_node_tree.module
index 66fb20c..ba2989e 100644
--- a/taxonomy_node_tree.module
+++ b/taxonomy_node_tree.module
@@ -42,6 +42,7 @@ function taxonomy_node_tree_parents($vid) {
*
* @param $nodes
* Array with node objects.
+ *
* @param $terms
* Array with term objects.
*
@@ -51,6 +52,9 @@ function taxonomy_node_tree_parents($vid) {
* @todo
* It is assumed that nodes are just associated with a single term.
* This could be changed to support multiple relationships.
+ *
+ * Also, current result rewind method should support postgresql
+ * data structures.
*/
function taxonomy_node_tree_index($nodes, $terms) {
while ($node = db_fetch_object($nodes)) {
@@ -63,6 +67,15 @@ function taxonomy_node_tree_index($nodes, $terms) {
$tree[$term->tid] = $term;
}
}
+
+ // Rewind result so it can be used again.
+ if (is_object($nodes)) {
+ $nodes->data_seek(0);
+ }
+ else if (mysql_num_rows($nodes) > 0) {
+ mysql_data_seek($nodes, 0);
+ }
+
return $tree;
}
@@ -93,6 +106,7 @@ function taxonomy_node_tree_relation($tree) {
*
* @param $tree
* Tree with term objects to be changed (by reference).
+ *
* @param $term
* Term object.
*/
@@ -129,6 +143,117 @@ function taxonomy_node_tree_hierarchy($tree) {
}
/**
+ * Count descendant nodes.
+ *
+ * @param $term
+ * Term object.
+ *
+ * @param $count
+ * Optional initial count offset. Also used for recursion.
+ *
+ * @return
+ * Number of descendant nodes.
+ */
+function taxonomy_node_tree_count($term, &$count = 0) {
+ if (isset($term->below)) {
+ foreach ($term->below as &$below) {
+ taxonomy_node_tree_count($below, $count);
+ }
+ }
+
+ if (isset($term->nodes)) {
+ $count += count($term->nodes);
+ }
+
+ return $count;
+}
+
+/**
+ * Build a list of empty terms.
+ *
+ * @param $term
+ * Term object.
+ *
+ * @param $clean
+ * Set to true to cleanup term before adding to list.
+ *
+ * @param $parents
+ * Set to FALSE to exclude parent items even if they
+ * don't have descendant nodes.
+ *
+ * @param $list
+ * Array to store term list.
+ *
+ * @return
+ * Array with terms without descendant nodes.
+ */
+function taxonomy_node_tree_get_empty($term, $clean = FALSE, $parents = TRUE, &$list = array()) {
+ if (taxonomy_node_tree_count($term) == 0) {
+ // Skip parents.
+ if ($term->parents[0] != 0 || ($term->parents[0] == 0 && $parents)) {
+ $save = drupal_clone($term);
+
+ if ($clean == TRUE) {
+ unset($save->nodes);
+ unset($save->below);
+ unset($save->children);
+ }
+
+ $list[] = $save;
+ }
+ }
+ else if (isset($term->below)) {
+ foreach ($term->below as $below) {
+ taxonomy_node_tree_get_empty($below, $clean, $parents, $list);
+ }
+ }
+
+ return $list;
+}
+
+/**
+ * Build a list of non-nempty terms.
+ *
+ * @param $term
+ * Term object.
+ *
+ * @param $clean
+ * Set to true to cleanup term before adding to list.
+ *
+ * @param $parents
+ * Set to TRUE to keep parent items even if they don't have
+ * descendant nodes.
+ *
+ * @param $list
+ * Array to store term list.
+ *
+ * @return
+ * Array with terms with descendant nodes.
+ */
+function taxonomy_node_tree_get_non_empty($term, $clean = FALSE, $parents = FALSE, &$list = array()) {
+ // Add parents and terms with descendant nodes.
+ if (($term->parents[0] == 0 && $parents) || taxonomy_node_tree_count($term) > 0) {
+ $save = drupal_clone($term);
+
+ if ($clean == TRUE) {
+ unset($save->nodes);
+ unset($save->below);
+ unset($save->children);
+ }
+
+ $list[] = $save;
+ }
+
+ if (is_array($term->below)) {
+ foreach ($term->below as $below) {
+ taxonomy_node_tree_get_non_empty($below, $clean, $parents, $list);
+ }
+ }
+
+ return $list;
+}
+
+/**
* Setup a full taxonomy node hierarchical tree.
*
* Build a tree with taxonomy terms with full dept and add child node
@@ -136,16 +261,35 @@ function taxonomy_node_tree_hierarchy($tree) {
*
* @param $nodes
* Array with node objects.
+ *
* @param $terms
* Array with term objects.
*
+ * @param $clean
+ * Set to TRUE to remove branches without descendant nodes.
+ *
+ * @param $parents
+ * Set to TRUE to keep parent items even if they don't have
+ * descendant nodes. Used just when $clean is also set to TRUE.
+ *
* @return
* Hierarchical tree.
*/
-function taxonomy_node_tree_build($nodes, $terms) {
+function taxonomy_node_tree_build($nodes, $terms, $clean = FALSE, $parents = FALSE) {
$tree = taxonomy_node_tree_index($nodes, $terms);
$tree = taxonomy_node_tree_relation($tree);
$tree = taxonomy_node_tree_hierarchy($tree);
+
+ if ($clean == TRUE && is_array($tree)) {
+ $terms = array();
+ foreach ($tree as $term) {
+ $list = taxonomy_node_tree_get_non_empty($term, TRUE, $parents);
+ $terms = array_merge($terms, $list);
+ }
+
+ $tree = taxonomy_node_tree_build($nodes, $terms, FALSE);
+ }
+
return $tree;
}
@@ -156,20 +300,23 @@ function taxonomy_node_tree_build($nodes, $terms) {
*
* @param $vid
* Which vocabulary to generate the tree for.
+ *
* @param $parent
* The term ID under which to generate the tree. If 0, generate the tree
* for the entire vocabulary.
+ *
* @param $depth
* Internal use only.
+ *
* @param $max_depth
* The number of levels of the tree to return. Leave NULL to return all levels.
+ *
* @param $cache
* Whether to use cache results.
*
* @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;
@@ -209,12 +356,47 @@ function taxonomy_node_tree_taxonomy_get_tree($vid, $parent = 0, $depth = -1, $m
}
/**
+ * Implementation of hook_theme().
+ */
+function taxonomy_node_tree_theme() {
+ return array(
+ 'taxonomy_node_tree_list' => array(
+ 'arguments' => array(
+ 'term' => NULL,
+ 'id' => NULL,
+ 'class' => NULL,
+ 'baselink' => NULL,
+ 'level' => NULL,
+ ),
+ 'function' => 'theme_taxonomy_node_tree_list',
+ ),
+ );
+}
+
+/**
* Recursively build an HTML taxonomy node tree.
*
* @ingroup themeable
- * @todo Usage with hook_theme().
+ *
+ * @param $term
+ * Current of starting term.
+ *
+ * @param $id
+ * Base id for list elements.
+ *
+ * @param $class
+ * Class for list elements.
+ *
+ * @param $baselink
+ * Base link for urls.
+ *
+ * @param $level
+ * Nesting level user for recursion.
+ *
+ * @return
+ * Rendered HTML tree.
*/
-function taxonomy_node_tree_list($term, $id, $class, $baselink = NULL, &$level = NULL, $active_link = NULL) {
+function theme_taxonomy_node_tree_list($term, $id, $class, $baselink = NULL, &$level = NULL) {
if ($level == NULL) {
$level = 0;
@@ -223,31 +405,68 @@ function taxonomy_node_tree_list($term, $id, $class, $baselink = NULL, &$level =
$level++;
if (isset($term->tid)) {
- $output .= '<li>';
- $output .= $term->name;
+ $output = '<li>';
+
if (isset($term->below)) {
+ $output .= $term->name;
$output .= '<ul id="' . $id .'-ul-'. $level .'" class="'. $class .'">';
foreach ($term->below as $child) {
- $output .= taxonomy_node_tree_list($child, $id, $class, $baselink, $level, $active_link);
+ $output .= theme_taxonomy_node_tree_list($child, $id, $class, $baselink, $level);
}
$output .= '</ul>';
}
elseif (isset($term->nodes)) {
+ $output .= $term->name;
$output .= '<ul id="' . $id .'-ul-'. $level .'" class="'. $class .'">';
foreach ($term->nodes as $node) {
$node = node_load($node->nid);
- path_nodeapi($node,'load','');
- //drupal_get_path_alias(request_uri())
- $output .= '<li class="'.($node->nid === arg(1)?$active_link:'item') .'">';
- $output .= ($node->path?l($node->title,$node->path):l($node->title, "$baselink/$node->nid"));
-
- // $output .= drupal_get_path_alias("node/$node->nid");
+ path_nodeapi($node, 'load', '');
+ //drupal_get_path_alias(request_uri());
+ $output .= '<li class="'. ($node->nid === arg(1) ? $active_link : 'item') .'">';
+ $output .= ($node->path ? l($node->title,$node->path) : l($node->title, "$baselink/$node->nid"));
+ //$output .= drupal_get_path_alias("node/$node->nid");
$output .= '</li>';
}
$output .= '</ul>';
}
+ else {
+ $output .= '<span>'. $term->name .'</span>';
+ }
+
$output .= '</li>';
}
return $output;
}
+
+/**
+ * Build an array with a node and it's parent terms.
+ *
+ * @param $parents
+ * Direct or all parent terms.
+ *
+ * @return
+ * Array with node and parent terms.
+ */
+function taxonomy_node_tree_trail($nid, $tid, $parents = 'direct') {
+ $trail = array();
+
+ // Add term hierarchy
+ if ($parents == 'direct') {
+ $function = 'taxonomy_get_parents';
+ }
+ else {
+ $function = 'taxonomy_get_parents_all';
+ }
+
+ // Build array
+ foreach (array_reverse($function($tid)) as $term) {
+ $trail[] = $term->name;
+ }
+
+ // Add node title
+ $node = node_load($nid);
+ $trail[] = $node->title;
+
+ return $trail;
+}