--- og_user_roles.module.5.x-2.3.orig	2007-09-05 11:13:42.000000000 -0700
+++ og_user_roles.module	2007-09-13 10:29:56.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_user_roles.module,v 1.1.2.7 2007/08/29 04:11:29 somebodysysop Exp $
+// $Id: og_user_roles.module,v 1.1.2.3 2007/07/08 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -77,6 +77,19 @@ function og_user_roles_admin_settings() 
     );
   }
 
+  $form['og_user_roles_approval'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Allow group admins to approve new signups.'),
+    '#collapsible' => TRUE,
+    '#description' => t('<strong>If user registration to your site requires administrator approval</strong> and you allow users to subscribe to groups at registration, then you can optionally allow the administrator(s) of the group(s) to which the user is subscribing to approve the signup request.  This will require that the administrator of each group that you wish to give this privilege have a role which includes the <strong>administer users</strong> permission. (Note that this feature requires ' . l("mimemail.module", "http://www.drupal.org/project/mimemail") . ' to be installed)' ),
+  );
+  $form['og_user_roles_approval']['og_user_roles_approval_default'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Allow group admins to approve new signups?'),
+    '#default_value' => variable_get('og_user_roles_approval_default', 0),
+    '#description' => t('<strong>If your site requires administrator approval for signups</strong>: When a user registers and elects to subscribe to a group (group is on registration form), then the admin of the group will receive an email notification of the pending signup and be allowed to approve it.  Note that this requires that the group admin have a group role which includes the <strong>administer users</strong> permission.'),
+  );
+
   $form['og_user_roles_default'] = array(
     '#type' => 'fieldset',
     '#title' => t('Default Non-Group Role for new users.'),
@@ -141,7 +154,7 @@ function og_user_roles_admin_settings() 
     '#type' => 'checkbox',
     '#title' => t('Send email notification to group admin when new subscriber is added to group?'),
     '#default_value' => variable_get('og_user_roles_notify_default', 0),
-    '#description' => t('Do you wish to automatically send an email notification to the group administrator when a new subscriber is added to a group? (Note that this feature requires mimemail to be installed)'),
+    '#description' => t('Do you wish to automatically send an email notification to the group administrator when a new subscriber is added to a group? (Note that this feature requires ' . l("mimemail.module", "http://www.drupal.org/project/mimemail") . ' to be installed)'),
   );
 
   $form['og_user_roles_all_groups'] = array(
@@ -167,7 +180,7 @@ function og_user_roles_admin_settings() 
     '#type' => 'checkbox',
     '#title' => t('Integrate TAC and OG Access Control?'),
     '#default_value' => variable_get('og_user_roles_tac_og_value', 0),
-    '#description' => t('Do you have Taxonomy Access installed and do you wish to make its access control work with Organic Groups?  Details here: http://groups.drupal.org/node/3700'),
+    '#description' => t('Do you have Taxonomy Access installed and do you wish to make its access control work with Organic Groups?  Details here: ' . l("How to Make OG and TAC Work Together: Step 2", "http://groups.drupal.org/node/3700")),
   );
 
   if (module_exists('og_subgroups')) {
@@ -213,7 +226,7 @@ function og_user_roles_admin_settings() 
     '#type' => 'checkbox',
     '#title' => t('Output debug data to og_user_test table?'),
     '#default_value' => variable_get('og_user_roles_test_default', 0),
-    '#description' => t('Every time user_access() is called, it will call og_user_all_roles() which adds group roles to $user->roles.  Do you wish to see the output from this function? (Note that this feature is for testing/debug purposes, and could create a very large output file.  This feature also requires that the table og_user_test already exist. Details here: http://drupal.org/node/164038)'),
+    '#description' => t('Every time user_access() is called, it will call og_user_all_roles() which adds group roles to $user->roles.  Do you wish to see the output from this function? (Note that this feature is for testing/debug purposes, and could create a very large output file.  This feature also requires that the table og_user_test already exist. Details here: ' . l("OG User Roles: Test", "http://drupal.org/node/164038")),
   );
 
   return system_settings_form($form);
@@ -258,6 +271,21 @@ function og_user_roles_menu($may_cache) 
       'access' => $user->uid,
       'type' => MENU_CALLBACK
     );
+    $items[] = array(
+      'path' => 'oguseredit/' . arg(1),
+      'title' => t('Edit user'),
+      'callback' => 'og_user_roles_oguseredit',
+      'callback arguments' => array(arg(1)),
+      'access' => $user->uid,
+      'type' => MENU_CALLBACK
+    );
+    $items[] = array('path' => 'ogusermanage/' . arg(1), 
+      'title' => t('Manage users'),
+	  'callback' => 'og_user_roles_user_manage',
+      'callback arguments' => array(arg(1)),
+      'access' => $user->uid,
+      'type' => MENU_CALLBACK
+    );
   }
   else {
     // modr8 modification
@@ -476,8 +504,17 @@ function og_user_roles_is_allowed($nid) 
  * Add role to og_users_roles table.
  */
 function og_user_roles_role_join($uid, $rid, $gid) {
-  db_query("INSERT INTO {og_users_roles} (rid, uid, gid) VALUES ('%d','%d','%d')", $rid, $uid, $gid);
-}
+  // Modification.  http://drupal.org/node/174773
+  // Check to see if this user doesn't already have this role in this group;
+  // If not, then assign it.
+  $sql = "SELECT COUNT(*) FROM {og_users_roles} WHERE uid = %d AND rid = %d and gid = %d";
+  $result = db_query($sql, $uid, $rid, $gid);
+  $output = (db_result($result));
+
+  if ($output == 0) {
+    db_query("INSERT INTO {og_users_roles} (rid, uid, gid) VALUES ('%d','%d','%d')", $rid, $uid, $gid);
+  }
+}
 
 /**
  * Remove all roles for a user in a group table.
@@ -721,6 +758,36 @@ function og_user_roles_init() {
       }
     }
 
+    // Modification as per: http://drupal.org/node/174959
+    // If this is a non-group node/add, but og_last session cookie is set
+    // then re-direct to ognodeadd
+    if (arg(0) == 'node' && arg(1) == 'add' && $_SESSION['og_last'] && (!is_null(arg(2))) ) {
+
+      $gid = $_SESSION['og_last']->nid;
+
+      $type = arg(2);
+      $path = 'node/ognodeadd';
+
+      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 to make it work with module "Relativity"
+      // http://drupal.org/node/166253
+      if (module_exists('relativity')) {
+      if (arg(3) == 'parent' && is_numeric(arg(4))) {
+          $query .= '&parent_node='.arg(4);
+        }
+        elseif (!empty($_GET['parent_node'])) {
+        $query .= '&parent_node='.$_GET['parent_node'];
+        }
+      }
+
+      drupal_goto($path, $query);
+    }
+
     // Modification for og_vocab: http://drupal.org/node/162649
     // This allows a group role with "administer taxonomy" permission to edit
     // og_vocab terms.  If this is an og_vocab term edit:
@@ -770,6 +837,56 @@ function og_user_roles_init() {
       }
 	}
 
+    // Modification user edit
+    // If this is going to http://clients.brixrealtyinc.com/user/76
+    // But the referrer is http://clients.brixrealtyinc.com/oguseredit/76/edit?gids[]=47
+    // Then redirect back to referrer.
+    if ((!user_access('access administration pages')) && (arg(0) == 'user' && is_numeric(arg(1)) && arg(1) != 1) || (arg(0) == 'admin' && arg(1) == 'user' && arg(2) == 'user')) {
+      $ref = $_SERVER["HTTP_REFERER"];
+      $ref_url = parse_url($ref);
+      $ref_path = $ref_url[path];
+      $ref_query = $ref_url[query];
+      $ref_arg = explode('/', $ref_path);
+      // Hijack only if the referrer contains oguseredit;
+      if ($ref_arg[1] == 'oguseredit') {
+        // If user clicks delete from user edit screen. Gets you to the
+		// "delete" confirmation.
+        if (arg(2) == 'delete') {
+          $temp = substr($ref_path, 1);
+          $path = str_replace("/edit", "/delete", $temp);
+          $query = $ref_query;
+          drupal_goto($path, $query);
+		}
+        // If user clicks "submit" from user edit screen - just takes you back
+		// to the same edit screen.
+        if ($ref_arg[3] == 'edit') {
+          $path = substr($ref_path, 1);
+          $query = $ref_query;
+          drupal_goto($path, $query);
+		}
+        // If user clicks "delete" or "cancel" from delete confirmation screen.
+		// Takes you to group home page.
+        if ($ref_arg[3] == 'delete') {
+          parse_str($ref_query);
+          $gid = $gids[0];
+          $path = 'node/' . $gid;
+          drupal_goto($path);
+		}
+      }
+      // If user clicks "Users" link while on group home page:
+      // Using referrer node ID (assuming it's a group)
+      if ($ref_arg[1] == 'node' && is_numeric($ref_arg[2])) {
+          $path = "ogusermanage/" . $ref_arg[2];
+          drupal_goto($path);
+      }
+      // If user clicks "Users" link while on group home page:
+      // Using og_last
+      if ($_SESSION['og_last']) {
+          $gid = $_SESSION['og_last']->nid;
+          $path = "ogusermanage/" . $gid;
+          drupal_goto($path);
+      }
+    }
   }
 }
 
@@ -781,18 +898,44 @@ function og_user_roles_init() {
  */
 function og_user_roles_user($op, &$edit, &$user, $category = NULL) {
 
-  // If the user opted to create a default role
-  if ($op == 'insert' && variable_get('og_user_roles_assign_default', 0) == 1) {
-    // Check to see if this user doesn't already have this role;
-    // If not, then assign it.
-    $rid = variable_get('og_user_roles_default_value', 0);
-    $sql = "SELECT COUNT(*) FROM {users_roles} WHERE uid = %d AND rid = %d";
-    $result = db_query($sql, $user->uid, $rid);
-    $output = (db_result($result));
+  if ($op == 'insert') {
+    // If site requires admin approval and group admins are allowed to approve users
+    if (variable_get('og_user_roles_approval_default', 0) == 1 && (variable_get('user_email_verification', TRUE) || ($user->status == 0))) {
+       watchdog('new_user: status', 'user->status = ' . $user->status, WATCHDOG_NOTICE);
+      if (is_array($edit['og_register'])) {
+        $sender = variable_get('site_mail', '');
+        $edit_test1 = $edit['og_register'];
+        foreach (array_keys(array_filter($edit['og_register'])) as $gid) {
+          watchdog('new_user: gid', 'gid = ' . $gid, WATCHDOG_NOTICE);
+          $node = node_load($gid);
+          $group = check_plain($node->title);
+          $subject = t('User @user pending account approval. Subscription request to : @group', array('@user' => $user->name, '@group' => $group));
+          $message = "The registration application for user " . l($user->name, "oguseredit/$user->uid", array(), "gids[]=$gid", NULL, TRUE) . " is pending approval.  You are the administrator for at least one of the groups to which this user has also applied to subscribe : " . l($group, "node/$gid", array(), NULL, NULL, TRUE) . ".<p>You can click " . l("here", "oguseredit/$user->uid/edit", array(), "gids[]=$gid", NULL, TRUE) . " to Activate, Block or Delete the user. (Don't forget to first log into group for which you are admin.)";
+          $result = db_query("SELECT uid FROM {og_uid} WHERE is_admin = 1 AND nid = %d", $gid);
+
+          while ($obj = db_fetch_object($result)) {
+            $recipient_uid = $obj->uid;
+            $recipient_user = user_load(array('uid' => $recipient_uid));
+            $recipient = $recipient_user->mail;
+            mimemail($sender, $recipient, $subject, $message);
+          } // end while
+        }
+	  }
+	}
+
+    // If site admin opted to create a default role
+    if (variable_get('og_user_roles_assign_default', 0) == 1) {
+      // Check to see if this user doesn't already have this role;
+      // If not, then assign it.
+      $rid = variable_get('og_user_roles_default_value', 0);
+      $sql = "SELECT COUNT(*) FROM {users_roles} WHERE uid = %d AND rid = %d";
+      $result = db_query($sql, $user->uid, $rid);
+      $output = (db_result($result));
 
-    if ($output == 0) {
-      db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $user->uid, $rid);
-    }
+      if ($output == 0) {
+        db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $user->uid, $rid);
+      }
+    }
   }
 
   // Add the group roles to $user->roles if this is a group
@@ -841,7 +984,7 @@ function og_user_roles_og($op, $nid, $ui
         $sender = variable_get('site_mail', '');
         $subject = t('User @user added to group: @group', array('@user' => $user->name, '@group' => $group));
         // http://www.scbbs.com/node/135/219#comment-219
-        $message = "User " . l($user->name, "user/$user->uid", array(), NULL, NULL, TRUE) . " added to group: " . l($group, "node/$nid", array(), NULL, NULL, TRUE);
+        $message = "User " . l($user->name, "oguseredit/$user->uid", array(), "gids[]=$gid", NULL, TRUE) . " added to group: " . l($group, "node/$nid", array(), NULL, NULL, TRUE);
         $result = db_query("SELECT uid FROM {og_uid} WHERE is_admin = 1 AND nid = %d", $nid);
 
         while ($obj = db_fetch_object($result)) {
@@ -1020,6 +1163,8 @@ function og_user_roles_all_roles($user) 
   $gid = $gid02;
   if ($gid02 === NULL) {
     $gid = 0;
+  }else{
+    $_SESSION['og_last'] = $group_node;
   }
 
   //
@@ -1042,12 +1187,42 @@ 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' || arg(2) == 'delete')) {
+    // Modified 2007-09-13 for "galleries";
+    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' || arg(2) == 'galleries')) {
       $location = 2;
       $nid = (int)arg(1);	
       $gid = og_user_roles_getgid($nid, $uid);
     }
 
+    // Edit users (og_user_roles_approval)
+	//
+	// This will get a group context if the current user and the
+	// user he is trying to access both belong to at least the same
+	// group that is also the last group set by OG in "og_last" session
+	// (which logically should be the current group);
+    //
+    // http://doadance.scbbs.com/oguseredit/72?gids[]=47
+    // http://doadance.scbbs.com/oguseredit/72/delete?gids[]=47
+    // http://doadance.scbbs.com/oguseredit/72/edit?gids[]=47
+    if (arg(0) == 'oguseredit' && is_numeric(arg(1)) && (is_null(arg(2)) || arg(2) == 'edit' || arg(2) == 'delete') ) {
+      if (isset($_REQUEST['gids'])) {
+        $location = 200;
+        $gids = $_GET['gids'];
+        $gid = intval(current($_REQUEST['gids']));
+      }
+    }
+
+    // Manage users (og_user_roles_approval)
+	//
+	// This will get a group context from og_last session
+	// and list all users in the group if the user listing
+	// is admin for the group and has the 'administer users' permission.
+    //
+    // http://doadance.scbbs.com/ogusermanage/47
+    if (arg(0) == 'ogusermanage' AND is_numeric(arg(1))) {
+      $gid = (int)arg(1);
+    }
+
     //                           0    1    2
     // http://www.mysite.com/comment/edit/14
     if (arg(0) == 'comment' && is_numeric(arg(2)) && arg(1) == 'edit') {
@@ -1057,6 +1232,15 @@ function og_user_roles_all_roles($user) 
       $gid = og_user_roles_getgid($nid, $uid);
     }
 
+    //                           0    1    2
+    // http://www.mysite.com/comment/delete/14
+    if (arg(0) == 'comment' && is_numeric(arg(2)) && arg(1) == 'delete') {
+      $location = 31;
+      $comment = _comment_load(arg(2));
+      $nid = $comment->nid;
+      $gid = og_user_roles_getgid($nid, $uid);
+    }
+
     //                           0      1    2
     // http://www.mysite.com/comment/reply/128#comment_form
     if (arg(0) == 'comment' && arg(1) == 'reply') {
@@ -1282,6 +1466,8 @@ function og_user_roles_all_roles($user) 
   //
   if (empty($gid)) {
     $gid = 0; // This prevents us from getting error on non-group node/add
+  }else{
+    $_SESSION['og_last']->nid = $gid;
   }
 
   $query = 'SELECT r.rid, r.name FROM {role} r INNER JOIN {og_users_roles} ogr ON r.rid = ogr.rid WHERE ogr.uid = %d AND ogr.gid = %d';
@@ -1628,6 +1814,95 @@ function og_user_roles_ogmodr8() {
 }
 
 /**
+ * Edit user
+ * Format: http://www.scbbs.com/oguseredit/76?gids[]=47
+ * Format: http://www.scbbs.com/oguseredit/76/edit?gids[]=47
+ */
+function og_user_roles_oguseredit($uid = 0) {
+  global $user;
+
+  $roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
+
+  $user->roles = $roles;
+  $gids = $_GET['gids'];
+  if ($gids) {
+     $group_node = node_load($gids[0]);
+     og_set_group_context($group_node);
+  }
+
+//  $account = user_load(array('uid' => $uid));
+//  if (array_keys($account->og_groups)) {
+//    $gids = array_intersect(array_keys($account->og_groups), array_keys($user->og_groups));
+//	if ($gids) {
+//      if ($_SESSION['og_last']) {
+//        $this_gid = $_SESSION['og_last']->nid;
+//          foreach ($gids as $item) {
+//		  if ($item == $this_gid) {
+//            $gid = $item;		  
+//            $group_node = node_load($gid);
+//            og_set_group_context($group_node);
+//		  }
+//		}
+//	  }
+//	}
+//  }
+
+
+  $access = user_access('administer users');
+
+  if ($access === TRUE && $uid != 1) {
+    if (arg(2) == 'edit') $output = drupal_get_form('user_edit');
+    if (arg(2) == 'delete') $output = user_edit();
+    if (is_null(arg(2))) $output = user_view($uid);
+  } else {
+    $output = 'Access denied.';
+    watchdog('access denied', 'oguseredit/'. $uid, WATCHDOG_WARNING);
+  }
+
+  return $output;
+}
+
+/**
+ * Manage users from OG User Roles
+ * ogusermanage
+ */
+function og_user_roles_user_manage() {
+  global $user;
+  $roles = og_user_roles_all_roles($user); // This returns normal $user->roles and includes OG roles if any
+
+  $user->roles = $roles;
+  $gids = array_keys($user->og_groups);
+
+  $this_gid = arg(1);
+  if ($this_gid) {
+    $group_node = node_load($this_gid);
+    og_set_group_context($group_node);
+  }
+
+  $uid = arg(1);
+  $items = db_query("SELECT uid FROM {users} ORDER BY name");
+  $output = "";
+
+  while ($item = db_fetch_object($items)) {
+	$uid = $item->uid;
+    $account = user_load(array('uid' => $uid));
+    $account_gids = array_keys($account->og_groups);
+	$common_gids = array_intersect($gids, $account_gids);
+    if ($common_gids) {
+      foreach ($common_gids as $item) {
+        if ($item == $this_gid && $user->og_groups[$this_gid]['is_admin'] == 1 && user_access('administer users') && $uid != 1) {
+ 	        $output .= "<li>" . l($account->name, 'oguseredit/' . $uid, $attributes = array(), 'gids[]=' . $this_gid) . " | " . l('(edit)', 'oguseredit/' . $uid . '/edit', $attributes = array(), 'gids[]=' . $this_gid) . "</li>";
+         }
+      }
+    }
+  } // end while
+
+  if ($output == "") $output = "<b>No users have been located for this group or you are not an admin user for this group!</b>";
+
+  return $output;
+}
+
+/**
  * using the og_forum 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)
@@ -1814,7 +2089,10 @@ function og_user_roles_node_access_join_
 	$return .= 'left outer join {og_users_roles} ogr on ogr.rid = '. $node_access_alias .'.gid '; // uses og_users_roles rid
 	$return .= 'left outer join {og_uid} ogu on ogu.nid = '. $node_access_alias .'.nid '; // added og_uid table to the mix
 	$return .= 'left outer join {nodefamily} nof on nof.child_nid = '. $node_access_alias .'.nid '; // added nodefamily table to the mix
-	$return .= 'left outer join {usernode} un on nof.parent_nid = un.nid '; // added usernode table to the mix
+    // http://drupal.org/node/173194
+    if (module_exists('usernode')) {
+      $return .= 'left outer join {usernode} un on nof.parent_nid = un.nid '; // added usernode table to the mix
+    }
     if (module_exists('acl')) {
       $return .= 'LEFT OUTER JOIN {acl_node} an ON an.nid = '. $node_access_alias .'.nid ';
       $return .= 'LEFT OUTER JOIN {acl_user} aclu ON aclu.acl_id = an.acl_id ';
@@ -1906,7 +2184,10 @@ function og_user_roles_node_access_where
 	$sql .= " OR (ogm.nid > 0 and (" . $node_access_alias . ".nid in ('".implode("','",$gids)."')) or ogm.directory = 1 ) "; // this allows us to see group nodes including those which allow directory listing
 	$sql .= " OR (" . $node_access_alias . ".realm = 'og_public' AND  " . $node_access_alias . ".gid = 0) ";
 	$sql .= " OR (ogr.uid = " . $uid . " AND ta.rid = ogr.rid AND ogan.group_nid = ogr.gid AND ta.grant_list = true) "; // original mod
-	$sql .= " OR (nof.child_nid = " . $node_access_alias . ".nid AND " . $node_access_alias . ".realm = 'term_access' AND un.uid = ". $uid ." AND " . $node_access_alias . ".grant_view = 1) "; // To list only your own uprofile node
+    // http://drupal.org/node/173194
+    if (module_exists('usernode')) {
+      $sql .= " OR (nof.child_nid = " . $node_access_alias . ".nid AND " . $node_access_alias . ".realm = 'term_access' AND un.uid = ". $uid ." AND " . $node_access_alias . ".grant_view = 1) "; // To list only your own uprofile node
+    }
 	$sql .= " OR (nof.child_nid = " . $node_access_alias . ".nid 
 				AND " . $node_access_alias . ".realm = 'term_access' 
 				AND ta.rid in ('".implode("','",$rids)."') 
