--- og_user_roles.module.5.x-2.1.orig	2007-08-09 13:44:13.000000000 -0700
+++ og_user_roles.module	2007-08-09 17:55:46.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_user_roles.module,v 1.1.2.5 2007/08/07 18:55:18 somebodysysop Exp $
+// $Id: og_user_roles.module,v 1.1.2.3 2007/07/08 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -640,9 +640,13 @@ function views_handler_filter_group() {
  */
 function og_user_roles_init() {
   global $user;
+
+  // Don't call og_user_roles_all_roles unless this user is logged in
+  if ($user->uid > 0) {
   // Let's try getting og roles here, just to see what happens
     $roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
     $user->roles = $roles;
+  }
 
   // Looking for this format: http://www.scbbs.com/node/add/link?gids[]=29
   // We only need to process this if the user is logged in
@@ -967,7 +971,13 @@ function og_user_roles_all_roles($user) 
   $uri_request_id = request_uri();
   $arg = explode('/', $uri_request_id);
   $ogroles = array();
-  $x1 = 0;
+  $x1 = 0;
+  // Need to use the referrer to get OG group in some cases;
+  $ref = $_SERVER["HTTP_REFERER"];
+  $ref_url = parse_url($ref);
+  $ref_path = $ref_url[path];
+  $ref_query = $ref_url[query];
+  $ref_arg = explode('/', $ref_path);
 
   //
   // This will by default add the anonymous user role (no need since we merge user->roles)
@@ -1041,7 +1051,7 @@ function og_user_roles_all_roles($user) 
     // http://doadance.scbbs.com/node/79/view/members
     // http://doadance.scbbs.com/node/79/view
     if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'view') {
-      $location = 2;
+      $location = 21;
       $gid = (int)arg(1);
     }
 
@@ -1058,7 +1068,7 @@ function og_user_roles_all_roles($user) 
     //
     // http://doadance.scbbs.com/node/29/og/vocab
     if (arg(0) == 'node' && is_numeric(arg(1)) && arg(2) == 'og') {
-      $location = 6;
+      $location = 61;
       $gid = (int)arg(1);
     }
 
@@ -1071,7 +1081,7 @@ function og_user_roles_all_roles($user) 
     if (arg(0) == 'node' && arg(1) == 'ogtermedit') {
       // Modification -- Get the gid from the variable instead of trying to calculate it.
       if (isset($_REQUEST['gids'])) {
-        $location = 6;
+        $location = 62;
         $gids = $_GET['gids'];
         $gid = intval(current($_REQUEST['gids']));
       }
@@ -1086,7 +1096,7 @@ function og_user_roles_all_roles($user) 
     if (arg(0) == 'node' && arg(1) == 'ogmodr8') {
       // Modification -- Get the gid from the variable instead of trying to calculate it.
       if (isset($_REQUEST['gids'])) {
-        $location = 13;
+        $location = 131;
         $gids = $_GET['gids'];
         $gid = intval(current($_REQUEST['gids']));
       }
@@ -1097,7 +1107,7 @@ function og_user_roles_all_roles($user) 
     // http://doadance.scbbs.com/og/users/72/roles
     // http://doadance.scbbs.com/og/users/72/add
     if (arg(0) == 'og' && arg(1) == 'users' && is_numeric(arg(2)) && is_null(arg(4))) {
-      $location = 6;
+      $location = 63;
       $gid = (int)arg(2);
     }
 
@@ -1110,7 +1120,7 @@ function og_user_roles_all_roles($user) 
     // http://doadance.scbbs.com/og/unsubscribe/72
     // http://doadance.scbbs.com/og/users/72/add
     if (arg(0) == 'og' && is_numeric(arg(2))) {
-      $location = 6;
+      $location = 64;
       $gid = (int)arg(2);
     }
 
@@ -1118,14 +1128,14 @@ function og_user_roles_all_roles($user) 
     //
     // http://doadance.scbbs.com/og_forum/manage/72
     if (arg(0) == 'og_forum' && arg(1) == 'manage' && is_numeric(arg(2)) && is_null(arg(3))) {
-      $location = 6;
+      $location = 65;
       $gid = (int)arg(2);
     }
 
     // http://doadance.scbbs.com/og_forum/manage/edit/72
     // http://doadance.scbbs.com/og_forum/manage/add/72
     if (arg(0) == 'og_forum' && arg(1) == 'manage' && is_numeric(arg(3))) {
-      $location = 6;
+      $location = 66;
       $gid = (int)arg(3);
     }
 
@@ -1155,7 +1165,7 @@ function og_user_roles_all_roles($user) 
     // http://www.mysite.com/forum/39
     // Modification - 2007-07-04.  In the case where this is a forum
     if (arg(0) == 'forum' && is_numeric(arg(1)) && is_null(arg(2))) {
-      $location = 8;
+      $location = 81;
       $tid = (int)arg(1);
       $gid = og_user_roles_gid_from_tid($tid);
       // If no gid returned, then set $gid = 0
@@ -1195,7 +1205,7 @@ function og_user_roles_all_roles($user) 
       //        -- In case we need to add "tid=" to url as per:
       //        -- http://drupal.org/node/156568
       if (isset($_REQUEST['gids'])) {
-        $location = 13;
+        $location = 132;
         $gids = $_GET['gids'];
         $gid = intval(current($_REQUEST['gids']));
       }
@@ -1203,10 +1213,24 @@ function og_user_roles_all_roles($user) 
     
 	// File upload: http://www.scbbs.com/node/135/225#comment-225
 	if (arg(0) == 'upload' && arg(1) == 'js') {
+      $location = 14;
+      // First, try and get gid from the session cookie
       if ($_SESSION['og_last']) $gid = $_SESSION['og_last']->nid;
+	  // If gid is still = 0, then try getting it from referrer 
+	  if ($gid == 0) {
+		if ($ref_arg[1] == 'node' && is_numeric($ref_arg[2]) && $ref_arg[3] == 'edit') {
+          $nid = (int)$ref_arg[2];
+          $gid = og_user_roles_getgid($nid, $uid);
+   		}
+		if ($ref_arg[1] == 'node' && $ref_arg[2] == 'ognodeadd') {
+          parse_str($ref_query);
+          $gid = $gids[0];
+   		}		
+	  }
 	}
     // File download
 	if (arg(0) == 'system' && arg(1) == 'files') {
+      $location = 15;
       if ($_SESSION['og_last']) $gid = $_SESSION['og_last']->nid;
 	}
 
@@ -1250,7 +1274,7 @@ function og_user_roles_all_roles($user) 
 
   // Write the test data: http://drupal.org/node/164038
   if (variable_get('og_user_roles_test_default', 0) == 1) {
-    og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $query);
+    og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $ref);
   }
 
   // $d is either the merged results, or just $user->roles;
@@ -1294,7 +1318,7 @@ function og_user_roles_user_table_exists
 /**
  * See what is found in user_all_roles.
  */
-function og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $query) {
+function og_user_roles_write_test($user, $location, $gid02, $gid, $uri_request_id, $d, $ref) {
   $test_status = '';
   $test_date = format_date(time(), 'custom', 'Y-m-d h:i:s a');
   $test_user = $user->uid;
@@ -1303,7 +1327,7 @@ function og_user_roles_write_test($user,
   $test_string = 'group context: '. $gid02;
   $test_username = $user->name;
   $test_group = $gid;
-  $test_uri = $uri_request_id;
+  $test_uri = $uri_request_id . " (" . $ref . ")";
   $test_aarg0 = arg(0);
   $test_aarg1 = arg(1);
   $test_aarg2 = arg(2);
@@ -1922,3 +1946,18 @@ function og_user_roles_tac_db_rewrite($q
     return array();
   }
 }
+/**
+ * Created to check a view before I execute it.  Needed for OG Home Pages with multiple
+ * embedded views because if one has no ndoes, then the entire page won't print.
+ * Don't want that, so created this utility to check before the view is rendered.
+ */
+function og_user_roles_checkview($viewname, $args) {
+  $view = views_get_view($viewname);
+  $items = views_build_view('items', $view, $args, 4, 5);
+  if ($items['items']) {
+    $result = 1;
+  } else {
+    $result = 0;
+  }
+  return $result;
+}
\ No newline at end of file
