Index: directory.css
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/directory/directory.css,v
retrieving revision 1.4
diff -u -p -r1.4 directory.css
--- directory.css	8 Nov 2006 07:54:42 -0000	1.4
+++ directory.css	21 May 2007 12:28:27 -0000
@@ -1,7 +1,7 @@
 .directory-home-vocabulary { }
 .directory-child-vocabulary {  }
 .directory-resources { }
-.directory-sub-category { }
+.directory-sub-categories { }
 .directory-category {  }
 .directory-toolbar {  }
 .directory-filter-wrapper {  }
@@ -23,4 +23,4 @@ span.directory-nodecount { font-style:it
 table.directory-resources ul li { display: block; list-style:none; }
 table.directory-resources { width: 98%; }
 table.directory-resources { margin-top: 1em; margin-bottom: .25em; }
-table.directory-resources tr td { width: 50%; } /* make columns evenly spaced */
\ No newline at end of file
+table.directory-resources tr td { width: 50%; } /* make columns evenly spaced */
Index: directory.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/directory/directory.module,v
retrieving revision 1.11.2.13
diff -u -p -r1.11.2.13 directory.module
--- directory.module	20 May 2007 12:26:08 -0000	1.11.2.13
+++ directory.module	21 May 2007 12:28:29 -0000
@@ -110,22 +110,6 @@ function directory_admin_settings() {
   	'#extra' => 0,
   	'#multiple' => TRUE,
   );
-  $form['directory_cat_res_count'] = array(
-  	'#type' => 'textfield',
-  	'#title' => t('Resource count threshold to display subcategories'),
-  	'#default_value' => variable_get('directory_cat_res_count', 10),
-  	'#size' => 5,
-  	'#maxlength' => 6,
-  	'#description' => t('If a category has fewer than this number of resources, the directory will try to display the resources available in subcategories.'),
-  );
-  $form['directory_subcat_res_count'] = array(
-  	'#type' => 'textfield',
-  	'#title' => t('Number of resources to display for each visible subcategory'),
-  	'#default_value' => variable_get('directory_subcat_res_count', 5),
-  	'#size' => 5,
-  	'#maxlength' => 6,
-  	'#description' => t('If subcategories are visible, how many resources for each subcategory should be displayed?'),
-  );
   $form['directory_show_child_counts'] = array(
     '#type' => 'checkbox', 
     '#title'=>'Show count of nodes in categories', 
@@ -182,7 +166,7 @@ function directory_page($tid = null, $fi
     $allowed_vids = variable_get('directory_vocabularies_root', array());
     foreach (taxonomy_get_vocabularies() as $v) {
       if (in_array($v->vid, $allowed_vids)) {
-        $output .= theme('directory_home_section', $v->vid);
+        $output .= theme('directory_home_vocabulary', $v->vid);
       }
     }
   }
@@ -372,7 +356,7 @@ function directory_display_category($tid
   $taxonomy = taxonomy_get_children($tid);
   if (DIRECTORY_MODULE_DEBUG) ed_module_support_vd($taxonomy, "taxonomy_get_children($tid) returns");
   $numset   = count($taxonomy);
-  $split    = floor($numset / 2);
+  $split    = ceil($numset / 2);
   if (DIRECTORY_MODULE_DEBUG) echo "numset=$numset<br/>";
   if ($numset == 0) return NULL;
   if (DIRECTORY_MODULE_DEBUG) echo "ok, we have ($numset) subcats";
@@ -415,59 +399,14 @@ function directory_display_category($tid
  * @return $output
  *   formatted html.
  */
-function theme_directory_home_section($vid) {
-  $tree = taxonomy_get_tree($vid);
+function theme_directory_home_vocabulary($vid) {
   $vocabulary = taxonomy_get_vocabulary($vid);
-  $numset   = count($tree);
-
-  //FIXME: $plit is the wrong value if empty terms are not displayed. But I might change the way the terms are listed anyway. beginner. 
-  $split    = floor($numset / 2);
-  if ($numset == 0) return NULL;
-
-  $output .= "<div class=\"directory-home-vocabulary\" id=\"directory-vid-$vocabulary->vid\">\n";
-
-  $prev = 0;
-  $open_tag = false;
-  $close = true;
-  $i = 0;
-  $showcounts = variable_get('directory_show_child_counts', TRUE);
-  
-  // FIXME: Need to fix this - Must get count of all nodes belonging to all children
-  $hideempty = variable_get('directory_hide_empty_terms', FALSE);
+  $terms = taxonomy_get_children(0, $vid);
 
-  $item_span = '';
-   
-  // Build a list of categories (taxonomy terms) and child counts.
-  foreach ($tree as $term) {
-    $nodecount = 0;
-    if ($showcounts || $hideempty) {
-      $nodecount = directory_taxonomy_term_count_nodes($term->tid);
-    }
-    $nodecount_span = '';
-    if ($showcounts) {
-      $item_span='<span class="directory-category-nochildren">';
-      if ($nodecount['count_own'] || $nodecount['count_children']) {
-        $item_span='<span class="directory-category-haschildren">';
-        $nodecount_span = "<span class='directory-nodecount'> [". $nodecount['count_own'];
-        $nodecount_span .= $nodecount['count_children'] ? ' + '. $nodecount['count_children'] .']</span>' : ']</span>';
-      }
-    }
-    // if desired, exclude items 
-    if ((!$hideempty) || ($nodecount['count_own'] || $nodecount['count_children'])) {
-      $items[] = $item_span . l(str_repeat('-', $term->depth) .' '. $term->name, "directory/$term->tid", (($term->description) ? array('title' => $term->description) : array())) 
-         . $nodecount_span . '</span>';
-    }
-  }
-  $col[] = array_slice($items, 0, $split);
-  $col[] = array_slice($items, $split);
-  $header = array(array('data' => t('By <em>%vocabulary-name</em>', array('%vocabulary-name' => $vocabulary->name)), 'colspan' => count($col)));
-
-  foreach ($col as $c) {
-    $r[] = array('data' => theme('item_list', $c), 'valign' => 'top');
-  }
-  $row[] = $r;
-  $output .= theme('table', $header, $row, array('class' => 'directory-resources'));
+  $output = '<div class="directory-home-vocabulary" id="directory-vid-'. $vocabulary->vid .'">';
+  $output .= theme('directory_list_terms', $terms,  t('By <em>%vocabulary-name</em>', array('%vocabulary-name' => $vocabulary->name)));
   $output .= '</div>';
+  
   return $output;
 }
 
@@ -589,7 +528,7 @@ function theme_directory_resource($tid, 
 	$node = _first($nodes);
 	$term = $node->taxonomy[$tid]; // mjc: nodes array is indexed by nid, not 0-based
       $output = directory_toolbar_alpha($tid);
-      $output .= theme('directory_resource_section', $nodes, $term, TRUE);
+      $output .= theme('directory_resource_section', $nodes, $term);
       if ($pager = theme('pager', NULL, $num_results_per_page, $pager_count - 1)) {
         $output .= $pager;
       }
@@ -603,30 +542,193 @@ function theme_directory_resource($tid, 
       $node = _first($nodes);
       if (DIRECTORY_MODULE_DEBUG) ed_module_support_vd($node, '_first() returns');
 	    $term = $node->taxonomy[$tid]; // MJC: was $nodes[0] which seems not to exist.. why? get_nodes_by_term uses nid as index into array!
-      $output = theme('directory_resource_section', $nodes, $term, TRUE);
+      $output = theme('directory_resource_sub_section', $tid);
+      $output .= theme('directory_resource_section', $nodes, $term);
     }
     else {
+      // FIXME: what is the use of this else{} block? We are returning $output, not $term!
       $term = taxonomy_get_term($tid);
     }
+  }
+
+  if (DIRECTORY_MODULE_DEBUG) ed_module_support_vd($output, 'display_resource() returns');
+  return $output;
+}
+
+/**
+ * Display a box with links to the sub-categories
+ * This function only displays the shell (title + <div> elements) where the links will be.
+ * It calls theme_directory_list_terms() which will print the content (list of links).
+ *
+ * @param $tid
+ *   The term ID whose sub-categories must be displayed.
+ *
+ * @return
+ *  formatted html 
+ */
+function theme_directory_resource_sub_section($tid) {
+  $output = '';
+
+  if ($terms = taxonomy_get_children($tid)) {
+    $output .= '<div class="directory-sub-categories">';
+    $output .= theme('directory_list_terms', $terms, t('Sub-categories'));
+    $output .= '</div>';
+  }
 
-    $num_nodes = count($nodes);
-    $threshold = variable_get('directory_cat_res_count', 10);
-    if ($num_nodes < $threshold && $taxonomy = taxonomy_get_children($tid)) {
-	    $subcat_count = variable_get('directory_subcat_res_count', 5);
-      foreach ($taxonomy as $term) {
-        $count = directory_taxonomy_term_count_nodes($term->tid);
-        if ($count['count_own'] + $count['count_children'] > 0) {
-          $nodes = directory_get_nodes_by_term($term->tid, $subcat_count);
-          $output .= "<div class=\"directory-sub-category\">". theme('directory_resource_section', $nodes, $term, false, $subcat_count) . "</div>\n";
+  return $output;
+}
+
+/**
+ * Display a list of terms 
+ * Each list item is a link to its corresponding term page.
+ *
+ * @param $terms
+ *   An array of term objects.
+ *
+ * @param $title
+ *   Translated text.
+ *
+ // FIXME: $parent not needed ?? beginner.
+ * @param $parent
+ *   Display all the children whose parent tid is $parent.
+ *   If we are on the main page, $parent = 0 and all terms within the vocabulary are displayed.
+ *   If we are on a term page, $parent = $tid of that term and we display only the sub-terms.
+ *
+ * @return
+ *  formatted html 
+ */
+function theme_directory_list_terms($terms, $title = '', $parent = 0) {
+  $output = '';
+  $showcounts = variable_get('directory_show_child_counts', TRUE);
+  $hideempty = variable_get('directory_hide_empty_terms', FALSE);
+  
+  $item_span = '';
+   
+  // Build a list of categories (taxonomy terms) and child counts.
+  foreach ($terms as $term) {
+    $nodecount = 0;
+    if ($showcounts || $hideempty) {
+      $nodecount = directory_taxonomy_term_count_nodes($term->tid);
+    }
+    $nodecount_span = '';
+    if ($showcounts) {
+      $item_span = '<span class="directory-category-nochildren">';
+      if ($nodecount['count_own'] || $nodecount['count_children']) {
+        $item_span = '<span class="directory-category-haschildren">';
+        $nodecount_span = "<span class='directory-nodecount'> [". $nodecount['count_own'];
+        $nodecount_span .= $nodecount['count_children'] ? ' + '. $nodecount['count_children'] .']</span>' : ']</span>';
+      }
+    }
+    // if desired, exclude items 
+    if ((!$hideempty) || ($nodecount['count_own'] || $nodecount['count_children'])) {
+      $children = NULL;
+      $nb = 1;
+      $children = taxonomy_get_children($term->tid);
+      if (is_array($children)) {
+        $children = theme('directory_list_subterms', $children);
+        if (is_array($children)) {
+          foreach ($children AS $child) {
+            $nb += $child['count']; 
+          }
         }
       }
+      $items[] = array('data' => $item_span . l($term->name, "directory/$term->tid", (($term->description) ? array('title' => $term->description) : array())) . $nodecount_span . '</span>', 'children' => $children, 'count' => $nb);
     }
   }
-  if (DIRECTORY_MODULE_DEBUG) ed_module_support_vd($output, 'display_resource() returns');
+
+  // We count the total number of <li> items there is. 
+  $count = 0;
+  foreach ($items as $item) {
+    $count += $item['count'];
+  }
+
+  // We calculate how best to slice the array, so that all the displayed <li> elements are evenly distributed in two columns. 
+  // The trick is not to cut one <ul>...</ul> into two. The whole list must be on one side. 
+  $split    = $count / 2;
+  $i = 0; // count the total nb of items (including children);
+  $slice = 0; // nb of items to slice.
+  reset($items);
+  $item = current($items);
+  $done = FALSE;
+  while (!$done) {
+    $slice += 1;
+    $j = $i + $item['count'];
+    if ($j >= $split) { 
+      if ($j - $split > $split - $i) {
+        $slice -= 1;
+      }
+      $done = TRUE;
+    }
+    $i = $j;
+    $item = next($items);
+  }
+
+  // Create two lists out of the original one.
+  $col = array();
+  $col[] = array_slice($items, 0, $slice);
+  $col[] = array_slice($items, $slice);
+  $header = array(array('data' => $title, 'colspan' => count($col)));
+
+  $r = array();
+  foreach ($col as $c) {
+    $r[] = array('data' => theme('item_list', $c), 'valign' => 'middle');
+  }
+
+  // And present the two lists into a table. 
+  $row = array($r);
+  $output .= theme('table', $header, $row, array('class' => 'directory-resources'));
+  
   return $output;
 }
 
 /**
+ * Recursively display the sub-terms within a themed list
+ *
+ * @param $terms
+ *   An array of term objects.
+ *
+ * @return
+ *   array of elements to put in the themed list. 
+ */
+function theme_directory_list_subterms($terms) { 
+  $count = 0;
+  $showcounts = variable_get('directory_show_child_counts', TRUE);
+  $hideempty = variable_get('directory_hide_empty_terms', FALSE);
+  foreach ($terms as $term) {
+    $nodecount = 0;
+    if ($showcounts || $hideempty) {
+      $nodecount = directory_taxonomy_term_count_nodes($term->tid);
+    }
+    $nodecount_span = '';
+    if ($showcounts) {
+      $item_span = '<span class="directory-category-nochildren">';
+      if ($nodecount['count_own'] || $nodecount['count_children']) {
+        $item_span = '<span class="directory-category-haschildren">';
+        $nodecount_span = "<span class='directory-nodecount'> [". $nodecount['count_own'];
+        $nodecount_span .= $nodecount['count_children'] ? ' + '. $nodecount['count_children'] .']</span>' : ']</span>';
+      }
+    }
+    // if desired, exclude items 
+    if ((!$hideempty) || ($nodecount['count_own'] || $nodecount['count_children'])) {
+      $children = NULL;
+      if ($get_children = taxonomy_get_children($term->tid)) {
+        $children = theme('directory_list_subterms', $get_children);
+      }
+      
+      $nb = 1;
+      if (isset($children)) {
+        foreach ($children AS $child) {
+          $nb += $child['count'];
+        }
+      }
+
+      $items[] = array('data' => $item_span . l($term->name, "directory/$term->tid", (($term->description) ? array('title' => $term->description) : array())) . $nodecount_span . '</span>', 'children' => $children, 'count' => $nb);
+    }
+  }
+  return $items;
+}
+
+/**
  * Display a term with all the nodes that belong in it.
  *
  * @param $nodes
@@ -635,22 +737,16 @@ function theme_directory_resource($tid, 
  * @param $term
  *   Object representing a taxonomy term.
  *
- * @param $no_link
- *   Don't link to the category view. This is used if your displaying all the resources in the category.
- *
  * @param $limit
  *   An integer used to only display n amount of resources instead of all resources for a category.
  *
  * @return
  *   HTML
  */
-function theme_directory_resource_section($nodes, $term, $no_link = TRUE, $limit = 0) {
+function theme_directory_resource_section($nodes, $term, $limit = 0) {
   $output = '';
   if (!is_numeric($term->tid) || !$nodes) return NULL;
 
-  if ($term->name) {
-    $output .= $no_link ? "<h2 class=\"directory-category\">$term->name</h2>\n" : "<h2 class=\"directory-category\">". l(t($term->name), "directory/$term->tid", array('title' => t('View all items in this category.'))) ."</h2>\n";
-  }
   $output .= "<ul class=\"directory-resources\">\n";
 
   foreach ($nodes as $nid => $node) {
@@ -868,17 +964,22 @@ function directory_taxonomy_term_count_n
     $count[$type][$tid]['count_children'] = 0;
     $count[$type][$tid]['children_nodes'] = array();
     foreach (_taxonomy_term_children($tid) as $c) {
-      $children = directory_taxonomy_term_count_nodes($c, $type, FALSE); // FALSE: we do not need to save $count in the db at this iteration.
-      // Add the children's own nodes:
-      foreach ($children['own_nodes'] AS $child_nid => $n) {
-        if (!isset($count[$type][$tid]['own_nodes'][$child_nid])) { // make sure the nid is not already counted for the parent.
-          $count[$type][$tid]['children_nodes'][$child_nid] = 1;
+      if ($children = directory_taxonomy_term_count_nodes($c, $type, FALSE)) { // FALSE: we do not need to save $count in the db at this iteration.
+        // Add the children's own nodes:
+        if (is_array($children['own_nodes'])) {
+          foreach ($children['own_nodes'] AS $child_nid => $n) {
+            if (!isset($count[$type][$tid]['own_nodes'][$child_nid])) { // make sure the nid is not already counted for the parent.
+              $count[$type][$tid]['children_nodes'][$child_nid] = 1;
+            }
+          }
         }
-      }
-      // Add the nodes of the children's children.
-      foreach ($children['children_nodes'] AS $child_nid => $n) {
-        if (!isset($count[$type][$tid]['own_nodes'][$child_nid])) { // make sure the nid is not already counted for the parent.
-          $count[$type][$tid]['children_nodes'][$child_nid] = 1;
+        // Add the nodes of the children's children.
+        if (is_array($children['children_nodes'])) {
+          foreach ($children['children_nodes'] AS $child_nid => $n) {
+            if (!isset($count[$type][$tid]['own_nodes'][$child_nid])) { // make sure the nid is not already counted for the parent.
+              $count[$type][$tid]['children_nodes'][$child_nid] = 1;
+            }
+          }
         }
       }
     }
@@ -889,7 +990,6 @@ function directory_taxonomy_term_count_n
     $cache = serialize($count);
     // the cached data will be used for at least one hour before being flushed.
     // TODO, make the nb of seconds before this cache is cleared configurable: time() + variable_get();
-    
     cache_set('taxonomy_term_count_nodes', 'cache_page', $cache, time() + 60 * 60);
   }
 
