--- C:/TMP/Original/indexpage.module	Thu Nov 20 01:51:48 2008
+++ C:/TreeView/indexpage.module	Thu Jan 15 12:17:14 2009
@@ -24,7 +24,7 @@
 /**
  * Implementation of hook_help().
  */
- function indexpage_help($path, $args) {
+function indexpage_help($path, $args) {
   switch ($path) {
     case 'admin/help#indexpage':
       return t('Displays index pages for each node type, with alphabetical and taxonomy filters.');
@@ -99,7 +99,7 @@
     '#description' => t('Shows a count of how many nodes that have no taxonomy term associated.'),
     );
 
-  $form['general']['indexpage_show_description'] = array(
+$form['general']['indexpage_show_description'] = array(
     '#type' => 'checkbox',
     '#title' => t('Show term description'),
     '#default_value' => variable_get('indexpage_show_description', FALSE),
@@ -182,6 +182,34 @@
         '#return_value' => 1,
         '#default_value' => variable_get($var_prefix .'_vocfilter', 1),
         );
+
+       // Add treeview Support
+       $form[$set][$var_prefix .'_treeview'] = array(
+        '#type' => 'checkbox',
+        '#title' => t('Show vocabulary filters within a Tree View'),
+        '#return_value' => 1,
+        '#default_value' => variable_get($var_prefix .'_treeview', 1),
+        );
+        
+      // Expanded Deep in TreeView hierarchy
+	  $form[$set][$var_prefix.'_deep_tree_expanded'] = array(
+	    '#type' => 'select',
+	    '#title' => t('Expanded Deep in TreeView hierarchy'),
+	  	'#options' => array ("0"=> "0","1"=>"1","2"=>"2","3"=>"3","4"=>"4","5"=>"5","6"=>"6","7"=>"7","8"=>"8","9"=>"9"),
+	    '#default_value' => variable_get($var_prefix.'_deep_tree_expanded', '0'),
+	    '#description' => t('How deep by default will be expanded.'),
+	    );  
+        
+	  // Deep in TreeView hierarchy
+	  $form[$set][$var_prefix.'_deep_tree'] = array(
+	    '#type' => 'select',
+	    '#title' => t('Deep in TreeView hierarchy'),
+	  	'#options' => array ("all"=> "all","1"=>"1","2"=>"2","3"=>"3","4"=>"4","5"=>"5","6"=>"6","7"=>"7","8"=>"8","9"=>"9"),
+	    '#default_value' => variable_get($var_prefix.'_deep_tree', 'all'),
+	    '#description' => t('When using a Treeview to show the hierarchy of terms, how deep to use to show nodes?.'),
+	    );        
+        
+        
     }
     else {
       $form[$set][$var_prefix .'_vocfilter'] = array(
@@ -244,15 +272,36 @@
       'access arguments' => array('administer site configuration'),
       'type' => MENU_NORMAL_ITEM,
       );
-      
+
 
   return $items;
 }
 
 function indexpage_page($type, $op = NULL) {
   global $user;
-  // Is this the overview index?
-  if (!arg(1)) {
+  
+  // if there is only one type, go directly to it!!
+  if ( !arg(1) ){
+  	$how_much_to_show = 0;
+  	$the_type = "";
+    $types = node_get_types('names');
+    foreach ($types as $type => $name) {
+      if (variable_get('indexpage_'. $type .'_enable', 1)) {
+        $how_much_to_show += 1;
+        $the_type = $type;
+      }
+    }
+    
+    if ($how_much_to_show == 1)
+    	$arg_1 = $the_type;
+    	$arg_2 = arg(2);
+  } else {
+  	$arg_1 = arg(1);
+    $arg_2 = arg(2);
+  }
+    
+// Is this the overview index?
+  if (!$arg_1) {
     $output .= '<div class="content">';
     $output .= nl2br(variable_get('indexpage_description', ''));
     $output .= '<ul>';
@@ -261,8 +310,8 @@
       if (variable_get('indexpage_'. $type .'_enable', 1)) {
         $output .= '<li>'. l(t('Index page for !s', array('!s' => check_plain(variable_get('indexpage_'. $type .'_name', $type)))), drupal_get_path_alias('indexpage/'. $type)) .'</li>';
       }
-    }
-
+    } 
+    
     $output .= '</ul></div>';
 
     return $output;
@@ -270,14 +319,15 @@
   else {
     // Do the index for a specific type.
     $access = _node_access_where_sql();
-    $exist_type = @db_result(db_query(_indexpage_check_status("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n, {node_access} na WHERE n.type = '%s'") . ($access ? ' AND '. $access : ''), arg(1)));
-    if ($exist_type && variable_get('indexpage_'. arg(1) .'_enable', 1)) {
+    $exist_type = @db_result(db_query(_indexpage_check_status("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n, {node_access} na WHERE n.type = '%s'") . ($access ? ' AND '. $access : ''), $arg_1));
+    if ($exist_type && variable_get('indexpage_'. $arg_1 .'_enable', 1)) {
+//      if (!arg(2)) {
       if (empty($op) && $op !== '0') {
-        $unpublished = @db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n, {node_access} na WHERE n.type = '%s' AND n.status = 0". ($access ? ' AND '. $access : ''), arg(1)));
-        return indexpage_page_index(arg(1), $exist_type, $unpublished);
+        $unpublished = @db_result(db_query("SELECT COUNT(DISTINCT(n.nid)) FROM {node} n, {node_access} na WHERE n.type = '%s' AND n.status = 0". ($access ? ' AND '. $access : ''), $arg_1));
+        return indexpage_page_index($arg_1, $exist_type, $unpublished);
       }
       else {
-        return indexpage_page_list(arg(1), arg(2));
+        return indexpage_page_list($arg_1, $arg_2);
       }
     }
     else {
@@ -294,9 +344,13 @@
   $show_description = variable_get('indexpage_show_description', FALSE);
   $list_format = variable_get('indexpage_list_format', 'list');
   $links = array();
-
+  
   drupal_set_title(t('Index Page for !type', array('!type' => $name)));
-
+  
+  // Deep to include nodes in the tree hierarchy 
+  $hierarchal_deep = variable_get('indexpage_'. $type.'_deep_tree', 'all') ;
+  $hierarchal_deep_ex = variable_get('indexpage_'. $type.'_deep_tree_expanded', '0') ;
+  
   $output = '<div class="indexpage_index indexpage_'. $type .'">';
 
   // Get node info.
@@ -318,9 +372,11 @@
     else {
       $count_msg .= '.';
     }
-    $output .= '<p>'. $count_msg .' '. t('This content type uses these options: !opts.', array('!opts' => implode(', ', $node_opts))) .'</p>';
+    // RSC Eliminado el texto de las opciones del nodo.  No aporta nada interesante.
+    //.' '. t('This content type uses these options: !opts.', array('!opts' => implode(', ', $node_opts)))
+    $output .= '<p>'. $count_msg  .'</p>';
   }
-
+  
   // Index by letter block.
   if (variable_get('indexpage_'. $type .'_alphaindex', 1)) {
     $output .= '<div class="indexpage-node-index">';
@@ -347,41 +403,114 @@
 
   // Index by taxonomy terms block.
   $voc_list = array();
+  $first = TRUE;
+  $suppress_unused = variable_get('indexpage_suppress_unused', FALSE);
   if (variable_get('indexpage_'. $type .'_vocfilter', 1)) {
     $vocs = db_query("SELECT v.name, v.vid FROM {vocabulary} v LEFT JOIN {vocabulary_node_types} t ON t.vid=v.vid WHERE t.type='%s'", $type);
     while ($voc = db_fetch_array($vocs)) {
+      if ($first) {
+        $first = FALSE;
+        $output .= '<h3>'. t('Category Index') .'</h3>';
+        $output .= '<p><em>'. t('Terms that have content will link to a list of titles tagged with that term.') .'</em></p>';
+      }
       $term_list = array();
       $voc_list[$voc['vid']] = check_plain($voc['name']);
-      $output .= '<div class="indexpage-filter-'. $voc['vid'] .'">';
+
       $voc_count = 0;
 
-      $terms = taxonomy_get_tree($voc['vid']);
-      foreach ($terms as $term) {
-        $sql = _indexpage_check_status("SELECT COUNT(n.nid) FROM {node} n LEFT JOIN {term_node} t on t.nid=n.nid AND t.vid=n.vid WHERE type='%s' AND t.tid=%d");
-        $term->node_count = $how_many = db_result(db_query($sql, $type, $term->tid));
-        $voc_count += $how_many;
-        if ($how_many || !$suppress_unused) {
-          $term_list[] = theme('indexpage_term', $term, $type);
-        }
-      }
-
-      if ($term_list) {
-        if ($list_format == 'list') {
-          $stuff = theme('item_list', $term_list, NULL, 'ul', array('class' => 'indexpage_term_list'));
-        }
-        else {
-          $header = array();
-          if ($show_description) {
-            $header = array(
-              t('Category'),
-              t('Description'),
-              );
+      // Check if Tree View checkbox is checked
+      if (variable_get('indexpage_'. $type .'_treeview', 1)) {
+		  // Include css and js needed to manage the treeview
+		  $path_tax_man = drupal_get_path('module', 'taxonomy_manager');
+		  drupal_add_css($path_tax_man.'/css/taxonomy_manager.css');
+		  drupal_add_js($path_tax_man.'/js/tree.js');
+		  drupal_add_js(array('taxonomy_manager' => array('modulePath' => (url($module_path) == $module_path) ? $module_path : (base_path() . $module_path))), 'setting');
+		  drupal_add_js(array('taxonomytree' => array('id' => 'IndexPage-TreeView', 'vid' => $voc['vid'])), 'setting');
+
+		  $output .= '<div id="IndexPage-TreeView" class="indexpage-filter-'. $voc['vid'] .'">';		  
+		  
+		  // Query with Hierarchy structure
+	      $sql = "SELECT td.tid, td.vid, td.name"
+	         . "  FROM {term_data} td"
+	         . "  JOIN {term_hierarchy} th on th.tid = td.tid"
+	         . " WHERE th.parent = %d"
+	         . "   AND td.vid = %d"
+	         . " ORDER BY td.weight, td.name";
+	      $terms = db_query($sql, 0, $voc['vid']);
+	      
+	      $stuff = "";
+	      while ($avoc = db_fetch_object($terms)) {
+	    	$child_content = get_child_terms($avoc->tid, $voc['vid'],$type, 2 );
+	    	if ( $child_content === '' )
+	    		$li_type = "";
+	    	else {
+	    		if ($hierarchal_deep_ex >= 1 )
+	    			$li_type = "class='collapsable '";
+	    		else
+	    			$li_type = "class='expandable '";
+	    	}
+
+		    $todos = indexpage_term_count_nodes($avoc->tid, $type, "all");
+	    	$solo = indexpage_term_count_nodes($avoc->tid, $type, "alone");
+            $voc_count += $solo;
+	    	if ($todos > 0 ){
+	    		$stodos = "<strong>$todos</strong>";
+	    	} else {
+	    		$stodos = "0";
+	    	}
+	    	if ($solo > 0 ){
+	    		$ssolo = "<strong>$solo</strong>";
+	    	} else {
+	    		$ssolo = "0";
+	    	}    		    		
+	    		
+		    $enlace ="";
+	    	if ($solo == 0 ){
+	    		$enlace = "$avoc->name";
+	    	} else {
+	    		$enlace = l("$avoc->name", "taxonomy/term/$avoc->tid/".$hierarchal_deep."/".$type);
+	    	}	    		
+	    	
+	        $stuff .= "<li $li_type>\n".pre_li();
+	        $stuff .=  " "
+	                .  $enlace . " ("
+                    .  $ssolo."/".$stodos.")"	                
+	                .  $child_content
+	                .  post_li()                
+	                .  "</li>\n";
+		  }
+	      $stuff = ($stuff != "") ? "<ul>\n". $stuff ."</ul>\n" : t('None found');      	
+      } else {
+      	  $output .= '<div class="indexpage-filter-'. $voc['vid'] .'">';
+	      $terms = taxonomy_get_tree($voc['vid']);
+	      foreach ($terms as $term) {
+            $sql = _indexpage_check_status("SELECT COUNT(n.nid) FROM {node} n LEFT JOIN {term_node} t on t.nid=n.nid AND t.vid=n.vid WHERE type='%s' AND t.tid=%d");
+            $term->node_count = $how_many = db_result(db_query($sql, $type, $term->tid));
+            $voc_count += $how_many;
+            if ($how_many || !$suppress_unused) {
+                $term_list[] = theme('indexpage_term', $term, $type);
+            }
           }
-          $stuff = theme('table', $header, $term_list, array('class' => 'indexpage_term_list'));
-        }
-      }
-      else {
-        $stuff = '<p>'. t('None found') .'</p>';
+          
+          if ($term_list) {
+            if ($list_format == 'list') {
+              $stuff = theme('item_list', $term_list, NULL, 'ul', array('class' => 'indexpage_term_list'));
+            }
+            else {
+              $header = array();
+              if ($show_description) {
+                $header = array(
+                  t('Category'),
+                  t('Description'),
+                  );
+              }
+              $stuff = theme('table', $header, $term_list, array('class' => 'indexpage_term_list'));
+            }
+          }
+          else {
+            $stuff = '<p>'. t('None found') .'</p>';
+          }          
+          
       }
       $fieldset = array(
         '#title' => t('List by !s Categories', array('!s' => $voc['name'])) . ($show_count ? ' ('. $voc_count .')' : NULL),
@@ -390,7 +519,7 @@
         '#collapsed' => (count($term_list) > variable_get('indexpage_maxresults', 10)),
         '#value' => $stuff,
         '#attributes' => array('class' => 'indexpage_category indexpage_category_'. $voc['vid']),
-        );
+        );  
       $output .= theme('fieldset', $fieldset) .'</div>';
     }
 
@@ -648,3 +777,100 @@
   $output .= theme('table', $header, $rows) . theme('pager', NULL, $max_results, 0);
   return $output .'</div>';
 }
+
+
+/*
+ * New functions to support Tree View
+ * Based on code :
+ * http://drupal.org/node/53085
+ */
+
+function pre_li(){
+  	return "   <div class='term-line'>\n"
+		.  "	   <div class='term-item'>\n";
+}
+  
+function post_li(){
+  	return  "</div>\n" 
+		 .  "   </div>\n";;
+}
+
+/*
+ * Esta función de vuelve en base a la info del módulo Taxonomy todos (all) los nodos debajo de un término
+ * ó solo (alone) los que pertenecen a ese termino.
+ */
+
+function indexpage_term_count_nodes($tid, $type = 0,$value = 'all'){
+	$result = 0;
+	
+	switch ($value) {
+		case 'all':
+			$result = taxonomy_term_count_nodes($tid, $type);
+		break;
+		case 'alone':
+			$result = taxonomy_term_count_nodes($tid, $type);
+			foreach (_taxonomy_term_children($tid) as $c) {
+				$result -= taxonomy_term_count_nodes($c, $type);
+		    }
+		break;
+	}
+	return $result;
+}
+  
+function get_child_terms($parent, $vid, $type, $actual_deep ) {
+    // RSC, profundidad a la hora de incluir los nodos en el árbol de jerarquia
+    $hierarchal_deep = variable_get('indexpage_'. $type.'_deep_tree', 'all');
+    $hierarchal_deep_ex = variable_get('indexpage_'. $type.'_deep_tree_expanded', '0');  	
+  	
+    $sql = "SELECT td.tid, td.vid, td.name"
+         . "  FROM {term_data} td"
+         . "  JOIN {term_hierarchy} th on th.tid = td.tid"
+         . " WHERE th.parent = %d"
+         . "   AND td.vid = %d"
+         . " ORDER BY td.weight, td.name";
+    $terms = db_query($sql, $parent, $vid);
+    $output = "";
+    while ($aterm = db_fetch_object($terms)) {
+    	$child_content = get_child_terms($aterm->tid, $vid, $type, $actual_deep + 1 );
+    	if ( $child_content === '' )
+    		$li_type = "";
+    	else {
+    		if ($hierarchal_deep_ex >= $actual_deep )
+    			$li_type = "class='collapsable '";
+    		else
+    			$li_type = "class='expandable '";
+    	}
+    	
+    	$todos = indexpage_term_count_nodes($aterm->tid, $type, "all");
+    	$solo = indexpage_term_count_nodes($aterm->tid, $type, "alone");
+
+    	if ($todos > 0 ){
+    		$stodos = "<strong>$todos</strong>";
+    	} else {
+    		$stodos = "0";
+    	}
+    	
+    	if ($solo > 0 ){
+    		$ssolo = "<strong>$solo</strong>";
+    	} else {
+    		$ssolo = "0";
+    	}    	
+
+       	$enlace ="";
+    	if ($solo == 0 ){
+    		$enlace = "$aterm->name";
+    	} else {
+    		$enlace = l("$aterm->name", "taxonomy/term/$aterm->tid/".$hierarchal_deep."/".$type);
+    	}    	
+   	
+        $output .= "<li $li_type>\n".pre_li();
+        $output .=  " "
+                .  $enlace . " ("
+                .  $ssolo."/".$stodos.")"
+                .  $child_content
+                .  post_li()                
+                .  "</li>\n";
+
+    }
+    return ($output != "") ? "<ul>\n". $output ."</ul>\n" : "";
+}
\ No newline at end of file
