--- og_user_roles.module.070704.orig	2007-07-04 13:50:56.000000000 -0700
+++ og_user_roles.module	2007-07-04 20:22:08.000000000 -0700
@@ -692,8 +692,32 @@ function og_user_roles_init ()
 			if ($type == 'og_user_roles_subgroup') $type = variable_get("og_user_roles_create_subgroup_value", ''); // change OG Subgroups type to 'group'
 
 			$query = 'type=' . $type . '&gids[]=' . $gid;
+
+			// Modification as per: http://drupal.org/node/156568
+
+			if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && is_numeric(arg(3))) {
+				$query = 'type=' . $type . '&gids[]=' . $gid . '&tid=' . intval(arg(3));
+			}		
+
 			drupal_goto($path, $query);
 		}
+
+		// Modification as per: http://drupal.org/node/156568
+		// If this is a group node/add/forum with no gids, re-direct to ognodeadd and include gids
+		if (arg(0) == 'node' && arg(1) == 'add' && arg(2) == 'forum' && is_numeric(arg(3)) && (!isset($_REQUEST['gids'])) ) {
+			$type = arg(2);
+			$tid = intval(arg(3));
+
+			$gid = og_user_roles_gid_from_tid($tid);
+
+			// Hijack only if there is a gid value;
+			if (!empty($gid)){
+				$path = 'node/ognodeadd';
+				$query = 'type=' . $type . '&gids[]=' . $gid . '&tid=' . $tid;
+				drupal_goto($path, $query);
+			}
+		}
+
 	}
 }
 
@@ -1148,12 +1172,28 @@ function og_user_roles_all_roles($user) 
 		//				  0		  1	  2			      3
 		// http://clients.brixrealtyinc.com/node/ognodeadd?type=document&gids[]=12
 		//
-			if ($arg[1] == 'node') {
-			  if (strpos($arg[2], 'ognodeadd') !== false ) {
-			     if (strpos($arg[2], 'gids') !== false ) {
-					$location = 13;
-					$subsections = explode("=", $arg[2]);
-					$gid = (int)$subsections[2];
+//			if ($arg[1] == 'node') {
+//			  if (strpos($arg[2], 'ognodeadd') !== false ) {
+//			     if (strpos($arg[2], 'gids') !== false ) {
+//					$location = 13;
+//					 $subsections = explode("=", $arg[2]);
+//					 $gid = (int)$subsections[2];
+//				  }
+			//
+			// Modification as per: http://drupal.org/node/155882
+			// We use arg() instead of $arg[] ($uri_request_id)
+			//
+            if (arg(0) == 'node') {
+              if (arg(1) == 'ognodeadd')  {
+				//
+				// Modification -- Get the gid from the variable instead of trying to calculate it.
+				//				-- In case we need to add "tid=" to url as per: 
+				//				-- http://drupal.org/node/156568
+				//
+			      if (isset($_REQUEST['gids'])) {
+						$location = 13;
+						$gids = $_GET['gids'];
+		    			$gid = intval(current($_REQUEST['gids']));
 			      }
 			  }
 			}
@@ -1392,4 +1432,14 @@ function og_user_roles_ognodeadd() {
 		}
 	}
 	return $output;
-}
\ No newline at end of file
+}
+/**
+ * using the tid, get the group id
+ * stolen from og_forum (had to in the case where og_user_roles user doesn't have latest
+ * version of og_forum module which includes this)
+ */
+function og_user_roles_gid_from_tid($tid=0) {
+  $sql = "SELECT nid FROM {og_term} WHERE tid = %d";
+  $gid = db_result(db_query($sql, $tid));
+  return $gid;
+}
