? relativity-350778.patch
Index: relativity.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/relativity/relativity.module,v
retrieving revision 1.46.2.11
diff -u -p -r1.46.2.11 relativity.module
--- relativity.module	11 Sep 2010 16:09:14 -0000	1.46.2.11
+++ relativity.module	11 Sep 2010 17:59:07 -0000
@@ -674,7 +674,7 @@ function relativity_display_settings() {
       $all_views = views_get_all_views();
       foreach ($all_views as $view) {
         if($view->type === 'Default'){
-          $views[t('Default Views')]['view:'. $view->name] = $view->name;      	  
+          $views[t('Default Views')]['view:'. $view->name] = $view->name;            
         }else{
         $views[t('Existing Views')]['view:'. $view->name] = $view->name;
         }
@@ -741,10 +741,10 @@ function relativity_block($op = 'list', 
       if (is_object($node) && node_access('view', $node)) {
         switch ($delta) {
           case 0:
-            $ancestors = relativity_load_ancestors($node, 1, TRUE);
+            $ancestors = relativity_load_ancestors($node, TRUE);
             if (is_array($ancestors) && count($ancestors) > 0) {
               $block['subject'] = variable_get('relativity_ancestors_label', t('Related Ancestors'));
-              $block['content'] = theme('relativity_block_ancestors', $node, $ancestors);
+              $block['content'] = theme('relativity_block_ancestors', $ancestors);
             }
             break;
           case 1:
@@ -776,68 +776,33 @@ function relativity_block($op = 'list', 
 }
 
 /**
- * Generate an array of ancestors for the given node.
- * This will keep looking for more ancestors until a circular path was found,
- * if a node has multiple or no parents at all.
- * If $load_multi_parent is TRUE, the first multi-parent result found will be placed
- * in the output array as an array of nodes.  Otherwise, all array elements will be nodes.
+ * Generate an array of ancestors for the given node. Uses recursion to follow every path to all ancestors.
  */
-function relativity_load_ancestors($node, $load_multi_parent=1, $block=FALSE) {
-  $search_max = 5; // make this a settings variable someday
+function relativity_load_ancestors($node, $block = FALSE) {
   if (is_numeric($node->nid)) {
     $ancestors = array();
-    $nids = array();
-    $search_nid = $node->nid;
-    $cnt = 0;
     $allowed_types = variable_get('relativity_allow_types', array());
     if ($block == TRUE) {
       $allowed_types = variable_get('relativity_nav_types', $allowed_types);
     }
-
-    do {
-      $keep_going = 0;
-      $result = db_query("SELECT parent_nid from {relativity} where nid=%d", $search_nid);
-      $parent_count = db_result(db_query("SELECT COUNT(parent_nid) from {relativity} where nid=%d", $search_nid));
-      if ($parent_count == 1 && $obj = db_fetch_object($result)) {
-        // make sure we're not pursuing a circular path and that we stop at $search_max parents
-        if (!in_array($obj->parent_nid, $nids) && $cnt++ < $search_max) {
-          //drupal_set_message("unshifting node ". $obj->parent_nid." onto the ancestors array");
-          $parent_node = node_load($obj->parent_nid);
-          if (node_access('view', $parent_node) && in_array($parent_node->type, $allowed_types)) {
-            array_unshift($ancestors, $parent_node);
-            $nids[] = $obj->parent_nid;
-            $search_nid = $obj->parent_nid;
-            $keep_going = 1;
-          }
-        }
-      }
-      elseif ($parent_count > 1 && $load_multi_parent && $cnt++ < $search_max) {
-        //drupal_set_message("found parent_count == $parent_count, about to add array to ancestors array");
-        // perhaps upon encountering multiple parents, it would suffice to print them
-        // all out at the same level and not follow any of them up, but still allow
-        // the user to navigate up whatever hierarchy they want to.
-        $siblings = array();
-        while ($obj = db_fetch_object($result)) {
-          // make an array of nodes to display at this level
-          $parent_node = node_load($obj->parent_nid);
-          if (!in_array($obj->parent_nid, $nids) && node_access('view', $parent_node) && in_array($parent_node->type, $allowed_types)) {
-            array_unshift($siblings, $parent_node);
-            $nids[] = $obj->parent_nid;
-            $search_nid = $obj->parent_nid; // only used when single result is rendered.
-          }
-        }
-        if (count($siblings) > 0) {
-          array_unshift($ancestors, $siblings);
-        }
-        if (count($siblings) == 1) {
-          $keep_going = 1;
+    
+    // If node has parents then initiate recursive assembly of ancestor tree.
+    if (isset($node->parent_node)) {
+      $parents = is_array($node->parent_node)?$node->parent_node:array($node->parent_node);
+      // Loop through each parent node and find ancestor tree for that node.
+      foreach ($parents as $parent) {
+        $parent_node = node_load($parent);
+        // Check if this parent is allowed to be added to the tree.
+        if (node_access('view', $parent_node) && in_array($parent_node->type, $allowed_types)) {
+          $ancestors[$parent]['ancestors'] = relativity_load_ancestors($parent_node, $block);
         }
       }
-    } while ($keep_going);
+    }
     return $ancestors;
   }
 }
 
+
 /**
  * Determines if the specified parent node may add a child of type $type.
  * @return  TRUE if possible, FALSE otherwise.
@@ -1082,7 +1047,7 @@ function relativity_nodeapi(&$node, $op,
         $ancestors = relativity_load_ancestors($node);
         if (is_array($ancestors) && count($ancestors) > 0) {
           $node->content['relativity_ancestors'] = array(
-            '#value' => theme('relativity_show_ancestors', $node, $ancestors),
+            '#value' => theme('relativity_show_ancestors', $ancestors),
             '#weight' => $w,
           );
         }
@@ -1463,7 +1428,7 @@ function relativity_theme($existing, $ty
       'arguments' => array('parent' => NULL, 'fieldset' => 1)
     ),
     'relativity_show_ancestors' => array(
-      'arguments' => array('node' => NULL, 'ancestors' => NULL, 'fieldset' => 1)
+      'arguments' => array('ancestors' => NULL, 'fieldset' => 1)
     ),
     'relativity_show_link_operations' => array(
       'arguments' => array('parent' => NULL, 'fieldset' => 1)
@@ -1476,14 +1441,14 @@ function relativity_theme($existing, $ty
       'arguments' => array('node' => NULL)
     ),
     'relativity_block_ancestors' => array(
-      'arguments' => array('node' => NULL, 'ancestors' => NULL)
+      'arguments' => array('ancestors' => NULL)
     ),
     'relativity_block_link_operations' => array(
       'arguments' => array('parent' => NULL)
     ),
 
     'relativity_ancestor' => array(
-      'arguments' => array('ancestor' => NULL, 'indent' => 0)
+      'arguments' => array('ancestor' => NULL)
     ),
     'relativity_link' => array(
       'arguments' => array('title' => NULL, 'target' => NULL, 'parent_nid' => 0, 'extra' => '')
@@ -1579,25 +1544,24 @@ function theme_relativity_show_children(
   return $output;
 }
 
-function theme_relativity_show_ancestors($node, $ancestors, $fieldset=1) {
+function theme_relativity_show_ancestors($ancestors, $fieldset = 1) {
   $output = '';
-  $indent = 0;
-
-  foreach ($ancestors as $ancestor) {
-    if (is_array($ancestor)) {
-      foreach ($ancestor as $sibling) {
-        $output .= theme('relativity_ancestor', $sibling, $indent);
-      }
-      $indent++;
-    }
-    else {
-      $output .= theme('relativity_ancestor', $ancestor, $indent++);
+  if (!empty($ancestors)) {
+    foreach ($ancestors as $nid => $data) {
+      $output .= '<li>';
+      $output .= theme('relativity_ancestor', $nid);
+      if (!empty($data['ancestors'])) {
+        $output .= theme('relativity_show_ancestors', $data['ancestors'], 0);
+      }
+      $output .= '</li>';
     }
   }
-
-  if ($output && $fieldset) {
-    $output = theme('fieldset', array('#title' => variable_get('relativity_ancestors_label', t('Ancestor nodes')),
+  if (!empty($output)) {
+    $output = '<ul>'.$output.'</ul>';
+    if ($fieldset) {
+      $output = theme('fieldset', array('#title' => variable_get('relativity_ancestors_label', t('Ancestor nodes')),
                                       '#children' => $output));
+    }
   }
   return $output;
 }
@@ -1637,7 +1601,6 @@ function theme_relativity_show_link_oper
 
   if ($output && $fieldset) {
     $output = theme('fieldset', array('#title' => variable_get('relativity_actions_label', t('Link operations')),
-
                                     //'#collapsible' => TRUE,
                                       '#children' => $output));
   }
@@ -1694,8 +1657,8 @@ function theme_relativity_block_children
   return theme('relativity_show_children', $node, 0);
 }
 
-function theme_relativity_block_ancestors($node, $ancestors) {
-  return theme('relativity_show_ancestors', $node, $ancestors, 0);
+function theme_relativity_block_ancestors($ancestors) {
+  return theme('relativity_show_ancestors', $ancestors, 0);
 }
 
 function theme_relativity_block_link_operations($parent) {
@@ -1704,13 +1667,11 @@ function theme_relativity_block_link_ope
 
 /************************ Helper themes *********************/
 
-function theme_relativity_ancestor($ancestor, $indent=0) {
-  $output = '';
-  if ($ancestor->type && $ancestor->title && $ancestor->nid) {
-    $output .= '<div style="padding-left: '. ($indent*10) .'px;" class="relativity_ancestor">';
-    $output .= node_get_types('name', $ancestor->type) .': '. l(t($ancestor->title), 'node/'. $ancestor->nid, array('class' => 'relativity_view_'. $ancestor->type));
-    $output .= '</div>';
-  }
+function theme_relativity_ancestor($ancestor) {
+  $node = node_load($ancestor);
+  $output = '<span class="relativity_ancestor">';
+  $output .= node_get_types('name', $node->type) .': '. l(t($node->title), 'node/'. $node->nid, array('class' => 'relativity_view_'. $node->type));
+  $output .= '</span>';
   return $output;
 }
 
