--- og_user_roles.module.5.x-2.4.orig	2007-09-18 11:34:27.000000000 -0700
+++ og_user_roles.module	2007-09-20 19:38:00.000000000 -0700
@@ -1,5 +1,5 @@
 <?php
-// $Id: og_user_roles.module,v 1.1.2.8 2007/09/17 03:30:01 somebodysysop Exp $
+// $Id: og_user_roles.module,v 1.1.2.3 2007/07/08 19:50:44 somebodysysop Exp $
 
 /**
  * @file
@@ -94,6 +94,7 @@ function og_user_roles_admin_settings() 
     '#type' => 'fieldset',
     '#title' => t('Default Non-Group Role for new users.'),
     '#description' => t('Allows you to select a role to automatically assign to all new signups to your site.'),
+    '#collapsible' => TRUE,
   );
   $form['og_user_roles_default']['og_user_roles_assign_default'] = array(
     '#type' => 'checkbox',
@@ -112,6 +113,7 @@ function og_user_roles_admin_settings() 
     '#type' => 'fieldset',
     '#title' => t('Default Basic Group Role for new group subscribers.'),
     '#description' => t('Allows you to select a group role to automatically assign to users who join a group on your site. The role is specific to the group(s) to which the user is subscribing.  That is, the user will only have the privileges of the role in the group he is subscribed to.'),
+    '#collapsible' => TRUE,
   );
   $form['og_user_roles_basicgrouprole']['og_user_roles_assign_basicgrouprole'] = array(
     '#type' => 'checkbox',
@@ -127,10 +129,24 @@ function og_user_roles_admin_settings() 
     '#description' => t('Select the role you wish to use as the "basic group role" for every new groupmember.'),
   );
 
+  $form['og_user_roles_basicgrouprole_gid_default'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Allow Group Admins to define Default Basic Group Role for new group subscribers.'),
+    '#description' => t('Allows you to allow Group Administrators to define a default group role to automatically assign to users who join their groups. The role is specific to the group to which the user is subscribing.  That is, the user will only have the privileges of the role in the group he is subscribed to.  Your Group Admins must have the <b>configure member roles</b> permission to use this setting.'),
+    '#collapsible' => TRUE,
+  );
+  $form['og_user_roles_basicgrouprole_gid_default']['og_user_roles_assign_basicgrouprole_gid'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Allow Group Admins to set default basic group (group limited) role for users who join their groups?'),
+    '#default_value' => variable_get('og_user_roles_assign_basicgrouprole_gid', 0),
+    '#description' => t('Do you wish to allow Group Admins to define a specific "basic group role" for <b>every new subscriber to their group</b> at the time he subscribes to the group? The role is limited to the group that he is subscribed to. This role assignment can be be removed by the groups\' admin(s).  The Group Admin will be able to define the default group role on <b>edit</b> (not creation) of the group node.  Group Admin must have <b>configure member roles</b> permission.'),
+  );
+
   $form['og_user_roles_founder'] = array(
     '#type' => 'fieldset',
     '#title' => t('Default Founder Role for users who create groups.'),
     '#description' => t('Allows you to select a group role to automatically assign to users who create groups on your site. The role is specific to the group(s) this user creates.  That is, the user will only have the privileges of the role in the group that he creates.'),
+    '#collapsible' => TRUE,
   );
   $form['og_user_roles_founder']['og_user_roles_assign_founder'] = array(
     '#type' => 'checkbox',
@@ -149,6 +165,7 @@ function og_user_roles_admin_settings() 
   $form['og_user_roles_notify'] = array(
     '#type' => 'fieldset',
     '#title' => t('Default Group Admin Notification for new subscribers.'),
+    '#collapsible' => TRUE,
   );
   $form['og_user_roles_notify']['og_user_roles_notify_default'] = array(
     '#type' => 'checkbox',
@@ -160,6 +177,7 @@ function og_user_roles_admin_settings() 
   $form['og_user_roles_all_groups'] = array(
     '#type' => 'fieldset',
     '#title' => t('Nodes to automatically place into all available groups.'),
+    '#collapsible' => TRUE,
   );
   $form['og_user_roles_all_groups']['og_user_roles_all_groups_nodes'] = array(
     '#type' => 'textarea',
@@ -198,6 +216,7 @@ function og_user_roles_admin_settings() 
       '#type' => 'fieldset',
       '#title' => t('Create Subgroups.'),
       '#description' => t('Allows you to select a group type to use for displaying a "Create Subgroup" link on your group menus. Users will need to have the "create og_subgroups" permission in order to access this link on their group menus. Note that this functionality requires the og_subgroups.module'),
+      '#collapsible' => TRUE,
     );
     $form['og_user_roles_subgroup']['og_user_roles_create_subgroup'] = array(
       '#type' => 'checkbox',
@@ -218,6 +237,7 @@ function og_user_roles_admin_settings() 
     '#type' => 'fieldset',
     '#title' => t('Clear the cache?'),
     '#description' => t('We found that there are some modules which will return "Access denied" message even when OG User Roles returns permissions which allow the user access.  This is due to cached permissions being used instead of the group permissions. This settiing allows you to reset these permissions for a user on an ongoing basis if you are having this problem.'),
+    '#collapsible' => TRUE,
   );
   $description = 'Do you wish to automatically clear the cache_menu item and reset the user_access() cached permissions for this user?  See ' . l("OG User Roles and File Upload", "http://drupal.org/node/166566") . ' for details.  Please note that this setting creates a heavy overload as permissions must be re-created on each page load.  Use this setting only if absolutely necessary.';
   $form['og_user_roles_clear_cache']['og_user_roles_clear_cache_value'] = array(
@@ -850,15 +870,17 @@ 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')) {
+    // Then redirect back to referrer.
+	// As a result of http://drupal.org/node/176662 this can only be done if the
+	// "Allow group admins to approve users" setting is checked on.
+    if (variable_get('og_user_roles_approval_default', 0) == 1 && ((!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 ($ref_arg[1] == 'oguseredit' && arg(2) != 'user') {
         // If user clicks delete from user edit screen. Gets you to the
 		// "delete" confirmation.
         if (arg(2) == 'delete') {
@@ -920,7 +942,12 @@ function og_user_roles_user($op, &$edit,
           $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.)";
+		  if (variable_get('og_user_roles_approval_default', 0) == 1) {
+            $view_user = "oguseredit/$user->uid";
+		  }else{
+            $view_user = "user/$user->uid";
+		  }
+          $message = "The registration application for user " . l($user->name, $view_user, 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", "$view_user/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)) {
@@ -985,6 +1012,16 @@ function og_user_roles_og($op, $nid, $ui
         og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
       }
 
+      // --- Modification - 2007-09-20 - as per: http://drupal.org/node/177016
+      // Places all new group subscribers into default role.
+      // Same as above, except these settings are set by Group Admin in the
+      // group node.  So, the variables are set by group.
+	  if (variable_get('og_user_roles_assign_basicgrouprole_' . $nid, 0)) { // check to see if this variable exists
+        if (variable_get('og_user_roles_assign_basicgrouprole_gid', 0) == 1 && variable_get('og_user_roles_assign_basicgrouprole_' . $nid, 0) == 1 ) {
+          $rid = variable_get('og_user_roles_basicgrouprole_value_' . $nid, 0);
+          og_user_roles_role_join($uid, $rid, $nid); // assign user to group role in that group
+        }
+      }
       $user = user_load(array('uid' => $uid));
       $node = node_load($nid);
 
@@ -993,8 +1030,13 @@ function og_user_roles_og($op, $nid, $ui
         $group = check_plain($node->title);
         $sender = variable_get('site_mail', '');
         $subject = t('User @user added to group: @group', array('@user' => $user->name, '@group' => $group));
+        if (variable_get('og_user_roles_approval_default', 0) == 1) {
+          $view_user = "oguseredit/$user->uid";
+		}else{
+          $view_user = "user/$user->uid";
+		}
         // http://www.scbbs.com/node/135/219#comment-219
-        $message = "User " . l($user->name, "oguseredit/$user->uid", array(), "gids[]=$nid", NULL, TRUE) . " added to group: " . l($group, "node/$nid", array(), NULL, NULL, TRUE);
+        $message = "User " . l($user->name, $view_user, array(), "gids[]=$nid", 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)) {
@@ -1470,6 +1512,14 @@ function og_user_roles_all_roles($user) 
       if ($_SESSION['og_last']) $gid = $_SESSION['og_last']->nid;
 	}
 
+   // Modification. As per: http://drupal.org/node/176390
+   // Present a hook for other modules: hook_og_user_roles_gid()  
+   if ($results = module_invoke_all('og_user_roles_gid')) {
+     foreach ($results as $result) {
+       if ($result) $gid = $result;
+     }
+   }
+
   } // end $gid if
 
   //
@@ -1890,7 +1940,18 @@ function og_user_roles_user_manage() {
     og_set_group_context($group_node);
   }
 
-  $uid = arg(1);
+  // Get the admin status for this user in this group;
+  $is_admin = 0;
+  if ($subscriptions = og_get_subscriptions($user->uid)) {
+    foreach ($subscriptions as $key => $val) {
+      // Debug
+      // print "key = " . $key . " is_admin = " . $val['is_admin'];
+      if ($key == $this_gid) {
+        $is_admin = $val['is_admin'];
+      }
+    }
+  }
+
   $items = db_query("SELECT uid FROM {users} ORDER BY name");
   $output = "";
 
@@ -1900,8 +1961,8 @@ function og_user_roles_user_manage() {
     $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) {
+      foreach ($common_gids as $common) {
+        if ($common == $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>";
          }
       }
@@ -1919,13 +1980,12 @@ function og_user_roles_user_manage() {
  * version of og_forum module which includes this)
  */
 function og_user_roles_gid_from_tid($tid = 0) {
-  $gid = 0;
   // Modification as per: http://drupal.org/node/172923
   if (module_exists('og_forum')) {
     $sql = "SELECT nid FROM {og_term} WHERE tid = %d";
     $gid = db_result(db_query($sql, $tid));
+    return $gid;
   }
-  return $gid;
 }
 
 /**
@@ -2352,4 +2412,62 @@ function og_user_roles_checkview($viewna
     $result = 0;
   }
   return $result;
-}
\ No newline at end of file
+}
+
+/**
+ * Implementation of hook_form_alter
+ * Adds ability to set default group role for new members for an individual
+ * group.
+ */
+function og_user_roles_form_alter($form_id, &$form) {
+  // If this is a group node edit (use og_last to determine if this is a group node) and
+  // the user has the 'configure member roles' permission, and we allow group admins
+  // to set default group roles, then print the 'basicgrouprole' form on the
+  // group edit form.
+  if (arg(0) == 'node' && ($_SESSION['og_last']->nid == arg(1)) && arg(2) == 'edit' && user_access('configure member roles') && variable_get('og_user_roles_assign_basicgrouprole_gid', 0) == 1) {
+    $gid = arg(1);
+
+    // First, we need to get a list of all og-enabled node types
+    $group_types = variable_get('og_node_types', array('og'));
+    foreach ($group_types as $type) {
+      // Get list of assignable group roles for this group type
+      $role_ids = variable_get("og_user_roles_roles_{$type}", array());
+      $all_roles = user_roles();
+      foreach ($role_ids as $rid => $checked) {
+        if ($checked != 0) {
+          $roles[$rid] = $all_roles[$rid];
+        }
+      }
+      // If the form loaded is this type then we can move on
+      if ($form_id == $type . "_node_form")  {
+        $form['og_user_roles_basicgrouprole_gid'] = array(
+          '#type' => 'fieldset',
+          '#title' => t('Default Basic Group Role for new subscribers to this group.'),
+          '#description' => t('Allows you to select a group role to automatically assign to users who join this group. The role is specific to this group. That is, the user will only have the privileges of the role while he is visiting this group.  You must have the <b>configure member roles</b> permission to use this setting.'),
+          '#collapsible' => TRUE,
+          );
+        $form['og_user_roles_basicgrouprole_gid']['og_user_roles_assign_basicgrouprole_' . $gid] = array(
+          '#type' => 'checkbox',
+          '#title' => t('Set default basic group (group limited) role for users who join this group?'),
+          '#default_value' => variable_get('og_user_roles_assign_basicgrouprole_' . $gid, 0),
+          '#description' => t('Do you wish to automatically assign a specific "basic group role" to <b>every new subscriber to this group</b> at the time he subscribes to this group? The role is limited to this group. This role assignment can be be removed by the groups\' admin(s)'),
+          );
+        $form['og_user_roles_basicgrouprole_gid']['og_user_roles_basicgrouprole_value_' . $gid] = array(
+          '#type' => 'select',
+          '#title' => t('Role to use as a basic group role'),
+          '#options' => $roles,
+          '#default_value' => variable_get('og_user_roles_basicgrouprole_value_' . $gid, 0),
+          '#description' => t('Select the role you wish to use as the "basic group role" for every new subscriber to this group.'),
+          );      
+         $form['#submit'] += array('og_user_roles_basicgrouprole_form_submit' => array($form_id, &$form));          
+//        $form['#validate'] += array('og_user_roles_basicgrouprole_form_validate' => array());
+      }
+    }
+  }
+}
+
+function og_user_roles_basicgrouprole_form_submit($form_id, $form_values) {
+  $gid = arg(1);
+  variable_set('og_user_roles_assign_basicgrouprole_' . $gid, $form_values['og_user_roles_assign_basicgrouprole_' . $gid]);
+  variable_set('og_user_roles_basicgrouprole_value_' . $gid, $form_values['og_user_roles_basicgrouprole_value_' . $gid]);
+}
