--- og_multiple_mandatory_groups_by_role/og_multiple_mandatory_groups_by_role.module	2008-07-19 11:56:59.000000000 -0500
+++ og_multiple_mandatory_groups_by_role/og_multiple_mandatory_groups_by_roleNew.module	2010-08-04 10:42:31.000000000 -0500
@@ -642,7 +642,7 @@ function og_multiple_mandatory_groups_by
 function og_multiple_mandatory_groups_by_role_groups($gid = NULL) {
   if ($admin = user_access('administer og mandatory groups by role')) {
 
-    $option_result = db_query("SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE (o.selective = %d OR o.selective = %d) ORDER BY n.title", OG_CLOSED, OG_OPEN);
+    $option_result = db_query("SELECT n.nid, n.title, o.* FROM {node} n INNER JOIN {og} o ON n.nid = o.nid WHERE (o.og_selective = %d OR o.og_selective = %d) ORDER BY n.title", OG_CLOSED, OG_OPEN);
     while ($group = db_fetch_object($option_result)) {
       $options[$group->nid] = $group->title;
       switch ($group->selective) {
@@ -1030,6 +1030,7 @@ function og_multiple_mandatory_groups_by
       break;
     
     case 'insert':
+	//drupal_set_message("INSERT");
       $sql = "SELECT ommgr.rid, ommgr.role_name, ommgr.mand_groups FROM {og_multi_mand_groups_role} ommgr WHERE ommgr.rid = -1";
       $result = db_fetch_object(db_query($sql));
       $mandatory_groups = unserialize($result->mand_groups);
@@ -1059,6 +1060,7 @@ function og_multiple_mandatory_groups_by
       }
       break;
     case 'update': 
+	//drupal_set_message("UPDATE");
       GLOBAL $form_state;
       //for now, let's process users from the user_admin_account form another way...via og_multiple_mandatory_groups_by_role_user_admin_account, above
       if (isset($form_state['values']['operation'])) {
@@ -1075,8 +1077,67 @@ function og_multiple_mandatory_groups_by
       else {
         $operation = NULL;
       }
+
+      //Compatibility with LDAP Groups helper here
+      //LDAP tries to save an array with role names called 'ldap_drupal_roles' we need to change this to 'roles' and 
+      //save away the rid field instead of the name
+      if(isset($edit['ldap_drupal_roles'])){
+	//DEBUG LINES HERE
+	//foreach($edit['ldap_drupal_roles'] as $pr_key => $pr_item){
+	//	drupal_set_message("LDAPROLES: " . $pr_key . " : " . $pr_item);
+	//}
+
+	//get the current users roles
+	$existing_roles=array();
+	foreach($account->roles as $item_key => $item){
+	  //DEBUG PRINT existing account roles
+//	  drupal_set_message("ROLE- " . $item_key . ":" . $item);
+	  $existing_roles[$item_key]=$item;
+	}
+	//check each existing role to see if they are valid and are NOT managed by og_mult_mand_groups_by_role and add them to the 'ldap_drupal_roles' array 
+	//query to see if this is still a valid role
+	$valid_roles=array();
+	foreach($existing_roles as $akrole_key => $akrole){
+		$valid_role_sql = "select rid from role where name='%s'";
+		$valid_role_results = db_query($valid_role_sql, $akrole);
+		while($valid_role_result = db_fetch_object($valid_role_results)){
+			$valid_roles[$akrole_key]=$akrole;
+		}
+	}	
+
+	//we have our valid roles, now let's see which ones are not managed
+	foreach($valid_roles as $nmrole_key => $nmrole){
+		$nmrole_sql="select rid from og_multi_mand_groups_role where role_name='%s'";
+		$nmrole_results=db_query($nmrole_sql,$nmrole);
+		if(!db_fetch_object($nmrole_results)){
+			//this is not a  managed role - add this to our main array 
+			$edit['ldap_drupal_roles'][]=$nmrole;
+		}
+	}
+//DEBUG LINES HERE
+//	foreach($edit['ldap_drupal_roles'] as $pr_key => $pr_item){
+//		drupal_set_message("LDAPROLES: " . $pr_key . " : " . $pr_item);
+//	}
+
+	unset( $edit['roles'] );
+	$temparray=array();
+	foreach ($edit['ldap_drupal_roles'] as $myVal){
+	  $tempsql="SELECT rid from role where name='%s'"; 
+	  $tempresults=db_query($tempsql, $myVal);
+	  while ($tempresult = db_fetch_object($tempresults)){
+	    //get role id (rid) from result and store away in the $edit['roles'] array
+	    //oddly enough, the array index must be the same as the rid in the array below
+//	    drupal_set_message("RESULTS--" . $tempresult->rid);
+ 	    $temparray[$tempresult->rid]=$tempresult->rid;
+	  }
+	}
+	$edit['roles']=$temparray;
+      }
+      //END LDAP Groups help here
+
       
       if (isset($edit['roles'])) {
+       // drupal_set_message("MMGBR:Update is being called now " . print_r($edit,true));
         $unsubscribe_ok = 1; //TODO make this receive value from checkbox as to whether the admin wants to unsubscribe from no-longer-allowed groups
         $all_subscriptions = array();
         //get all monitored roles
