--- og_hierarchy/og_hierarchy.module	2005-08-04 16:19:33.000000000 -0700
+++ og_hierarchy_modified/og_hierarchy.module	2005-08-05 17:33:58.127651380 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og.module,v 1.53 2005/04/14 03:47:17 weitzman Exp $
+// $Id: og_hierarchy.module,v 1.2 2005/08/05 22:58:51 rtoren Exp $
 
 /**
 * Implementation of hook_help().
@@ -78,7 +78,8 @@
     case 'validate':
       if ($node->type == 'og') {
          $group = og_hierarchy_get_group_by_name(trim($node->title));
-         if ((isset($node->nid) && count($group) > 1) || (!isset($node->nid) && count($group) >= 1)) {
+         if ((isset($node->nid) && count($group) > 1) || 
+	     $group && (!isset($node->nid) && count($group) >= 1)) {
            form_set_error('title', t('There already is a group with the name %title. This will cause unexpected results with the og_hierarchy module.', array('%title' => theme('placeholder', trim($node->title)))));
          }
       }
@@ -88,6 +89,14 @@
         db_query('INSERT INTO {og_hierarchy} (nid) VALUES (%d)', $node->nid);
       }
       break;
+
+  case 'delete':
+    if ($node->type == 'og') {
+      db_query('DELETE FROM {og_hierarchy} WHERE nid = %d OR parent = %d', 
+	       $node->nid, $node->nid);
+    }
+    break;
+    
   }
 }
 
@@ -198,13 +207,13 @@
     $children = array();
     $result = db_query('SELECT o.*, h.parent, n.title FROM {og} o LEFT JOIN {og_hierarchy} h ON o.nid = h.nid INNER JOIN {node} n ON o.nid = n.nid ORDER BY n.title');
     while ($term = db_fetch_object($result)) {
-print_r($term);
+      //print_r($term);
       $children[$term->parent][] = $term->nid;
       $parents[$term->nid][] = $term->parent;
       $terms[$term->nid] = $term;
     }
   }
-print_r($children[$parent]);
+  //print_r($children[$parent]);
   $max_depth = (is_null($max_depth)) ? count($children) : $max_depth;
   if (isset($children[$parent])) {
     foreach ($children[$parent] as $child) {
@@ -229,7 +238,7 @@
 * Get group node ID by group name.
 */
 function og_hierarchy_get_group_by_name($name) {
-  $result = db_query("SELECT nid, title FROM node WHERE title = '%s'", $name);
+  $result = db_query("SELECT nid, title FROM {node} WHERE title = '%s'", $name);
   if (db_num_rows($result)) {
     $group = array();
     while ($node = db_fetch_object($result)) {
@@ -267,4 +276,66 @@
   return $groups;
 }
 
+
+/**
+ * API for getting "adopted" by a parent
+ *
+ */
+function og_hierarchy_is_direct_parent($nid, $gid){
+  $sql = "SELECT parent FROM {og_hierarchy} WHERE nid = %d AND parent = %d";
+  $result = db_query($sql, $nid, $gid);
+  if(!$result)
+    return false;
+  return db_num_rows($result) > 0;
+}
+
+
+/**
+ * API currently doesn't guarantee that all groups are known
+ * to og_hierarchies.  So best be safe; create a record for our
+ * gid, or tell us we can't for some reason.
+ *
+ * @author Rob Thorne <rob@torenware.com>
+ * @param $gid -- group's NID
+ * @return boolean, with a false value when it was not legal to add
+ *       a record to the list
+ * @exeption
+ * @see og.module
+ */
+function og_hierarchy_register_for_parenthood($gid){
+  //Is our prospective parent actually a group?
+  $conditions = array('nid' => $gid, 'type' => 'og');
+  $group = node_load($conditions);
+  if(!$group){
+    watchdog('content', "Called og_hierarchy routine on non-group $gid",
+	     WATCHDOG_WARNING);
+    return false;
+  }
+  $sql = "SELECT parent FROM {og_hierarchy} WHERE nid = %d";
+  $result = db_query($sql, $gid);
+  if(!$result)
+    return false;
+  if(db_num_rows($result) > 0)
+    return true; 
+  //Otherwise, create a record with a parent of '0'
+  og_hierarchy_adopt_parent($gid, 0);
+  return true;
+}
+
+function og_hierarchy_adopt_parent($nid, $gid){
+  //For now,  just deal with og_hierarchy table
+  if(og_hierarchy_is_direct_parent($nid, $gid))
+    return; //once is enough!
+  $sql = "INSERT INTO {og_hierarchy} (nid, parent) VALUES(%d, %d)";
+  db_query($sql, $nid, $gid);
+}
+
+
+function og_hierarchy_divorce_parent($nid, $gid){
+  //Delete any records one and all
+  $sql = "DELETE FROM {og_hierarchy} WHERE  nid = %d AND parent = %d";
+  db_query($sql, $nid, $gid);
+}
+
+
 ?>
\ No newline at end of file
