--- taxonomy_access.module.5.x-1.1.orig	2007-07-14 14:48:52.000000000 -0700
+++ taxonomy_access.module	2007-07-14 14:49:24.000000000 -0700
@@ -655,6 +655,13 @@ function _taxonomy_access_defaults($vid,
 function taxonomy_access_db_rewrite_sql($query, $table, $field) {
   if (!user_access('administer taxonomy') && ($field =='vid' || $field =='tid')) {
     global $user;
+
+    /**
+     * Modification
+     */
+      // Get uid for this user
+      $uid = $user->uid;
+
     $op = (arg(0) == 'node' && (arg(1) == 'add' || arg(2) == 'edit')) ? 'create' : 'list';
     
     // let's cache
@@ -671,8 +678,74 @@ function taxonomy_access_db_rewrite_sql(
       else {
         $rids[] = 1;
       }
-      
-      $sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_$op) AS grant_$op FROM {term_access} t INNER JOIN {term_data} d ON t.tid = d.tid WHERE t.rid IN ('".implode("','",$rids)."') AND grant_$op = 1 GROUP BY t.tid, d.vid");
+
+    /**
+     * Modification
+     */
+      // Get groups that this user belongs to.
+      if (isset($user) && is_array($user->og_groups)) {
+		$gids = array_keys($user->og_groups);
+      } 
+      else {
+        $gids[] = 0;
+      }
+
+    /**
+     * Modification - Try to get this group's gid - $gid IS an array
+     */
+      $gid = array();
+      // First try getting group context
+      $group_node = og_get_group_context();
+      $gid02 = $group_node->nid;
+      $gid[0] = $gid02;
+      if ($gid02 === null) $gid = array();
+      if (empty($gid)) {
+        // Next, get it if this is an addform
+	    $nodeGid = $_GET['gids'];
+	    if ($nodeGid) $gid = $nodeGid;
+	    if (empty($gid)) {
+	      // Next, try and get it if this is a node/add/forum
+		  if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && is_numeric(arg(3))) {
+		    $gid[0] = db_result(db_query("SELECT nid FROM {og_term} WHERE tid = %d", arg(3)));
+		  }		
+		  if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'edit') {
+    	    $result = db_query('SELECT group_nid from og_ancestry where nid = %d', arg(1));
+    		while ($t = db_fetch_object($result)) {
+      		  $gid[] = $t->group_nid;
+    		}
+			// if $gid is still empty, then see if this is a group node edit
+			if (empty($gid)) $gid[0] = db_result(db_query("SELECT nid FROM {og} WHERE nid = %d", arg(1)));
+	      }		
+	    }
+	    // if $gid is still empty, then set it to 0
+	    if (empty($gid)) $gid[0] = 0;
+	  }
+	  // if $gid is still empty, then set it to 0
+	  if (empty($gid)) $gid[0] = 0;
+	  if ($gid[0] == NULL) $gid[0] = 0;
+
+      // Original SQL
+      // $sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_$op) AS grant_$op FROM {term_access} t INNER JOIN {term_data} d ON t.tid = d.tid WHERE t.rid IN ('".implode("','",$rids)."') AND grant_$op = 1 GROUP BY t.tid, d.vid");
+
+      /**
+       * Modification
+       *
+       * Add og_ancestry table to mix in order to preserve OG
+       * This is designed to only list terms a user can create for when clicking on
+       * create or edit content links.   
+       * We now use "ognodeadd" (in og_user_roles) instead of "addform"
+       */
+        // Determine if this is a create or edit content process.
+        if ($field == 'tid' && $table == 't') {
+		  if (arg(1) == 'add' || arg(1) == 'ognodeadd' || arg(2) == 'edit') { // this will include edits as well
+			$sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_create) AS grant_create FROM {term_access} t INNER JOIN {term_data} d ON t.tid=d.tid LEFT OUTER JOIN {og_term} ot on ot.tid = t.tid WHERE t.rid in ('".implode("','",$rids)."') AND grant_create = 1 and (ot.nid is null OR ot.nid in (".implode("','",$gid)."))	group by t.tid, d.vid"); // only get create term grants in *this* group ($gid IS an array);
+		  } else {
+			$sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_$op) AS grant_$op FROM {term_access} t INNER JOIN {term_data} d ON t.tid=d.tid WHERE t.rid in ('".implode("','",$rids)."') AND grant_$op = 1 group by t.tid, d.vid");
+		  }
+	    } else {
+          $sql = db_query("SELECT t.tid, d.vid, BIT_OR(t.grant_$op) AS grant_$op FROM {term_node} tn INNER JOIN {term_access} t ON t.tid=tn.tid INNER JOIN {term_data} d ON t.tid=d.tid INNER JOIN {og_ancestry} oa ON oa.nid=tn.nid WHERE t.rid in ('".implode("','",$rids)."') AND grant_$op = 1 AND oa.group_nid in ('".implode("','",$gids)."') group by t.tid, d.vid ");
+	    }
+    
       while ($result = db_fetch_object($sql)) {
         $tids[]= $result->tid;
         $vids[$result->vid]= $result->vid;
