--- relativity.module.orig	2007-03-20 13:30:59.000000000 +0200
+++ relativity.module	2007-03-25 11:15:32.000000000 +0200
@@ -633,6 +633,19 @@
       'body' => t('node body'), 
       'hide' => t('hide this child type')
     );
+		if (module_exists('views')) {
+			$views = array();
+			$result = db_query("SELECT name FROM {view_view} ORDER BY name");
+			while ($view = db_fetch_array($result)) {
+				$views[t('Existing Views')]['view:'.$view['name']] = $view['name'];
+			}
+			views_load_cache();
+			$default_views = _views_get_default_views();
+			foreach ($default_views as $view) {
+				$views[t('Default Views')]['view:'.$view->name] = $view->name;
+			}
+			$render_opts = array_merge($render_opts, $views);
+		}
     foreach (relativity_node_list() as $chtype=>$chname) {
       if (in_array($chtype, variable_get('relativity_type_'.$type, array()))) {
         $group['node_'.$type.'_options']['relativity_render_'.$type.'_'.$chtype] = array(
@@ -1104,12 +1117,14 @@
       }
 
       // output links to attach allowed children types and remove existing children
+			/*
       if ($types = variable_get('relativity_type_'. $node->type, FALSE)) {
         $node->content['relativity_operations'] = array(
           '#value' => theme('relativity_links', $types, $node, 'view'),
           '#weight' => 20,
         );
-      }
+			}
+			 */
       break;
   }  
 }
@@ -1272,6 +1287,64 @@
   while($child = db_fetch_object($result)) {
     $child_nodes[] = node_load($child->nid);
   }
+	
+	foreach (relativity_sort_types() as $nodetype) {
+		$variable = variable_get('relativity_render_'.$node->type.'_'.$nodetype, '');
+		if(strpos($variable, 'view:') !== FALSE){
+			$viewname = str_replace('view:', '', $variable);
+			views_load_cache();
+			$view = views_get_view($viewname);
+			// the $view will maintain its field contents, so I wanna make sure
+			// I will add the extra field only once..
+			static $fields_count;
+			$fields_count	= count($view->field);
+			$view->field[$field_count] = array(
+				'vid' => count($view->field),
+				'tablename' => 'node',
+				'field' => 'nid',
+				'label' => '',
+				'handler' => 'relativity_handle_view_links',
+				'sortable' => 0,
+				'defaultsort' => 0,
+				'fullname' => 'node.nid',
+				'queryname' => 'node_nid',
+				);
+			// send the parent ID as the view's first argument:
+			$view_args = array($node->nid);
+			// add the parent node type as third parameter:
+			$view_args[] = $node->type;
+			// as per JohnG's idea, send the child node type as the third argument:
+			$view_args[] = $nodetype;
+
+			// get the view output, i.e. build the view
+			$output .= views_build_view('embed', $view, $view_args, FALSE, NULL);
+			// add links to create/attach children
+			if (relativity_may_add_child($node, $nodetype)) {
+				$type_name = node_get_types('name',$nodetype);
+				if (!$type_name) {
+					$type_name = $nodetype;
+				}
+				$may_create = relativity_may_attach_new_child_type($node->type, $nodetype);
+				// only offer to let user create new child if user has 'create' access 
+				// and common child relationship isn't required
+				if ($may_create && node_access('update', $node)) {
+					$output .= l(t('Create New !type', array('!type'=>$type_name)), "node/add/$nodetype/parent/$node->nid", array(), drupal_get_destination());
+				}
+				// only show link to attach existing node when the potential child doesn't require a parent.
+				if ((!relativity_requires_parent($nodetype) || relativity_multi_parent($nodetype)) && node_access('update', $node)) {
+					// Use 'create' privilege to change up the link text
+					if ($may_create) {
+						$ltitle = t('Attach Existing !type', array('!type'=> $type_name));
+					}
+					else {
+						$ltitle = t('Attach !type', array('!type'=>$type_name));
+					}
+					$output .= ' | ' . l($ltitle, "relativity/listnodes/$nodetype/parent/$node->nid");
+				}
+				$output .= "<br />\n";
+			}
+		} 
+	}
 
   // sort output by node type
   if (is_array($child_nodes)) {
@@ -1293,19 +1366,26 @@
                                                  '#children' => node_view($child_node, FALSE)));
               break;
           }
-          $output .= ($link ? '<div span="relativity_link">' . $link . "</div>\n" : '');
+          $children .= ($link ? '<div span="relativity_link">' . $link . "</div>\n" : '');
         }
       }
     }
   }
-  if ($output && $fieldset) {
-    $output = theme('fieldset', array('#title' => variable_get('relativity_children_label', t('Children nodes')), 
-                                      '#children' => $output));
+  if ($children && $fieldset) {
+    $output .= theme('fieldset', array('#title' => variable_get('relativity_children_label', t('Children nodes')), 
+                                      '#children' => $children));
   }
 
   return $output;
 }
 
+function relativity_handle_view_links(&$field, &$view, $nid, &$node){
+  $parent = db_fetch_object(db_query("SELECT parent_nid from {relativity} where nid=%d", $nid));
+	if (relativity_may_unchild(node_load($parent->parent_nid), $node)) {
+		return l(t('Remove'), 'relativity/unparent/'.$parent->parent_nid.'/'.$nid);
+	}
+}
+
 function theme_relativity_ancestors($node, $ancestors, $fieldset=1) {
   $output = '';
   $indent = 0;
@@ -1390,7 +1470,7 @@
       // only offer to let user create new child if user has 'create' access 
       // and common child relationship isn't required
       if ($may_create && node_access('update', $parent)) {
-        $output .= l(t('create new !type', array('!type'=>$type_name)), "node/add/$type/parent/$parent_nid");
+        $output .= l(t('create new !type', array('!type'=>$type_name)), "node/add/$type/parent/$parent_nid", array(), drupal_get_destination());
       }
 
       // only show link to attach existing node when the potential child doesn't require a parent.
