--- og_user_roles.module.5.x-2.2.orig	2007-08-23 18:48:01.000000000 -0700
+++ og_user_roles.module	2007-08-25 13:39:45.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_user_roles.module,v 1.1.2.6 2007/08/14 18:29:25 somebodysysop Exp $
+// $Id: og_user_roles.module,v 1.1.2.3 2007/07/08 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -55,9 +55,10 @@ function og_user_roles_admin_settings() 
   }
 
   // If no assignable roles, advise user to add some.
+  // Modification as per: http://drupal.org/node/167373 
   if (!count($roles)) {
-    $form['og_user_roles'] = array(
-    '#value' => '<p>'. t('No assignable roles were found. Please create at least one role under <a href="%access">administer >> access >> roles</a>.', array('%access' => url('admin/access/roles'))) .'</p>',
+    $form['og_user_roles'] = array(					  
+      '#value' => '<p>'. t('No assignable roles were found. Please create at least one role under !roles.', array('!roles' => l(t('Administer >> User management >> Roles'), 'admin/user/roles'))) .'</p>',
     );
     return $form;
   }
@@ -209,8 +210,8 @@ function og_user_roles_admin_settings() 
  * Implementation of hook_menu().
  */
 function og_user_roles_menu($may_cache) {
-  global $user;
-
+  global $user;
+    
   $items = array();
 
   if ($may_cache) {
@@ -646,7 +647,10 @@ function og_user_roles_init() {
   // Don't call og_user_roles_all_roles unless this user is logged in
   if ($user->uid > 0) {
     $roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
-    $user->roles = $roles;
+    $user->roles = $roles;
+	cache_clear_all($user->uid, 'cache_menu', TRUE);
+	// Clear the user_access $perm cache
+	user_access('', NULL, TRUE);
   }
 
   // Looking for this format: http://www.scbbs.com/node/add/link?gids[]=29
@@ -1023,7 +1027,7 @@ function og_user_roles_all_roles($user) 
     // Modified 2007-07-05 for webform "results";
     // Modified 2007-07-23 for content_access "access";
     // Modified 2007-08-06 for modr8 "modr8" and "ogmodr8" (our own callback);
-    if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'outline' || arg(2) == 'track' || arg(2) == 'results' || arg(2) == 'access' || arg(2) == 'modr8' || arg(2) == 'ogmodr8')) {
+    if (arg(0) == 'node' && is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'outline' || arg(2) == 'track' || arg(2) == 'results' || arg(2) == 'access' || arg(2) == 'modr8' || arg(2) == 'ogmodr8' || arg(2) == 'delete')) {
       $location = 2;
       $nid = (int)arg(1);	
       $gid = og_user_roles_getgid($nid, $uid);
@@ -1326,8 +1330,33 @@ function og_user_roles_getgid($nid, $uid
       $gid = $t->nid;
     }
   }
-
+
+  // As per: http://drupal.org/node/167180
+  // If gid still not found
+  if ($gid == 0) {
+    $node = db_fetch_object(db_query('SELECT nid, type FROM {node} WHERE nid = ' . $nid));
+    if (og_is_group_type($node->type)) {
+      $gid = $node->nid;
+    } elseif (is_array($groups = og_get_node_groups($node))) {
+      $last_id = 0;
+      foreach ($groups as $id => $group) {
+        if ($group) {
+          // Need some way to determine the group to select in case of
+		  // multiple groups.  Going to try using og_last session cookie.
+		  // Just not sure if session will be set for current group by now.
+		  if ($id == $_SESSION['og_last']->nid) $gid = $id;
+		  $last_id = $id;
+		}
+      }
+	  // If there was no session with og_last session cookie, then set gid to last
+	  // id located in groups.  Roll of the dice until we can find something more
+	  // certain.
+	  if ($gid == 0) $gid = $last_id;
+    }
+  }
+  
   return $gid;
+
 } // end getgid()
 
 /**
