/** place code at end of template.php **/
/** 
  * Template Relativity to insert Views of related nodes.
  */

if (module_exists($views)) {

  // Catch function : theme_relativity_show_children 
function phptemplate_relativity_show_children($node, $fieldset=1) { 
  $output = '';
  return $output;
  }

/**
 * This function themes the output of the $types array.  It should
 * print a list of links to attach a node.
 */
function phptemplate_relativity_links($types, $parent, $op='view') {
  global $current_view;
  $current_view->args[0]=$parent->nid;
  $output = '';
  $parent_nid = $parent->nid;

  // remove "default" type
  if (is_array($types)) {
    $keys = array_keys($types, "default");
    if (count($keys))
      unset($types[$keys[0]]);
  }

  if (!is_array($types) || count($types) == 0 || !$parent_nid) {
    return;
  }

  $types = relativity_sort_types($types);     
  
  foreach($types as $type) {
    if (relativity_may_add_child($parent_nid, $type)) { 
      $type_name = node_get_types('name',$type);
      if (!$type_name) {
        $type_name = $type; 
      }
    $output .= '<div class="childview_' . $type . '">';
    $childview = views_get_view('childview_' . $type);
//  $output .= '<h2>' . t('children of type: ' . $type) . '</h2>';  
    $output .= views_build_view('embed', $childview, $current_view->args, false, false);
 
    $may_create = relativity_may_attach_new_child_type($parent->type, $type);

      // 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 .= 'Attach: '; //*Attach label*
      $output .= l(t('new !type', array('!type'=>$type_name)), "node/add/$type/parent/$parent_nid");
     }

      // only show link to attach existing node when the potential child doesn't require a parent.
    if ((!relativity_requires_parent($type) || relativity_multi_parent($type)) && node_access('update', $parent)) {
        // Use 'create' privilege to change up the link text
      if ($may_create) {
          $ltitle = t('existing !type', array('!type'=>$type_name));
        }
        else {
          $ltitle = t('attach !type', array('!type'=>$type_name));
        }
      $output .= ' || ';     /** seperator */
      $output .= l($ltitle, "relativity/listnodes/$type/parent/$parent_nid") . "<br />\n";
      }
    $output .= '</div>';
    }
  }
return $output;
}
}
/** end of template.php **/


