--- sites/all/modules/advuser/advuser.module	2008-06-30 09:31:04.000000000 +0200
+++ sites/localhost.bm_intranet/modules/advuser/advuser.module	2008-07-02 18:53:32.000000000 +0200
@@ -70,6 +70,12 @@ function advuser_admin($callback_arg = '
       elseif ($_POST['accounts'] && $_POST['operation'] == 'email') {
         $output = drupal_get_form('advuser_multiple_email_confirm');
       } 
+      elseif ($_POST['accounts'] && strpos($_POST['operation'], 'add_og-') !== false) {
+        $output = drupal_get_form('advuser_multiple_add_og_confirm');
+      }
+      elseif ($_POST['accounts'] && strpos($_POST['operation'], 'remove_og-') !== false) {
+        $output = drupal_get_form('advuser_multiple_remove_og_confirm');
+      }
       else {
         $output = drupal_get_form('advuser_filter_form');
         $output .= drupal_get_form('advuser_admin_account');
@@ -325,7 +331,7 @@ function advuser_multiple_delete_confirm
 }
 
 /**
- * Email functionality
+ * Email and og functionality
  */
 function advuser_advuser_operations() {
   $operations = array(
@@ -333,9 +339,110 @@ function advuser_advuser_operations() {
     'label' => t('Email selected users')
     )
   );
+
+  // Organic group support
+  if (module_exists('og')) {
+    $result = db_query('SELECT node.nid as nid, title FROM {node} JOIN {og} WHERE node.nid = og.nid');
+    if (db_num_rows($result)) {
+      $add_og = array();
+      $remove_og = array();
+      while( $og = db_fetch_array($result)) {
+        $add_og['add_og-' . $og['nid']] = $og['title'];
+        $remove_og['remove_og-' . $og['nid']] = $og['title'];
+      }
+
+      $og_operations = array(
+        t('Add organic group to selected users ') => array('label' => $add_og), 
+        t('Remove organic group from selected users ') => array('label' => $remove_og)
+      );
+      $operations += $og_operations;
+    }
+  }
+
   return $operations;
 }
 
+function advuser_multiple_add_og_confirm() {
+  $edit = $_POST;
+
+  $form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
+  _form_add_accounts($form, $edit);
+
+  $exp = explode ("-", $edit['operation']);
+  $nid = $exp[count($exp)-1];
+
+  $form['operation'] = array(
+    '#type' => 'hidden', 
+    '#value' => $edit['operation']
+  );
+
+  $form['og_nid'] = array(
+    '#type' => 'hidden', 
+    '#value' => $nid
+  );
+
+  $og_name = db_result(db_query('SELECT title FROM {node} WHERE node.nid = %d', $nid));
+
+  return confirm_form(
+    $form,
+    t('Are you sure you want to add these users to organic group %1 ?', array('%1' => $og_name)),
+    'admin/user/user/advuser',
+    t('This action cannot be undone.'),
+    t('Add'),
+    t('Cancel')
+  );
+}
+
+function advuser_multiple_remove_og_confirm() {
+  $edit = $_POST;
+
+  $form['accounts'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE);
+  _form_add_accounts($form, $edit);
+
+  $exp = explode ("-", $edit['operation']);
+  $nid = $exp[count($exp)-1];
+
+  $form['operation'] = array(
+    '#type' => 'hidden', 
+    '#value' => $edit['operation']
+  );
+  $form['og_nid'] = array(
+    '#type' => 'hidden',
+    '#value' => $nid
+  );
+
+  $og_name = db_result(db_query('SELECT title FROM {node} WHERE node.nid = %d', $nid));
+
+  return confirm_form(
+    $form,
+    t('Are you sure you want to remove these users from organic group %1 ?', array('%1' => $og_name)),
+    'admin/user/user/advuser',
+    t('This action cannot be undone.'),
+    t('Remove'),
+    t('Cancel')
+  );
+}
+
+function advuser_multiple_add_og_confirm_submit($form_id, $form_values) {
+  if ($form_values['confirm']) {
+    foreach ($form_values['accounts'] as $uid => $value) {
+      og_save_subscription($form_values['og_nid'], $uid, array('is_active' => 1, 'is_admin' => 0));
+    }
+    drupal_set_message(t('The users have been added to group.'));
+  }
+  return 'admin/user/user/advuser';
+}
+
+function advuser_multiple_remove_og_confirm_submit($form_id, $form_values) {
+  if ($form_values['confirm']) {
+    foreach ($form_values['accounts'] as $uid => $value) {
+      og_delete_subscription($form_values['og_nid'], $uid);
+    }
+    drupal_set_message(t('The users have been removed from group.'));
+  }
+  return 'admin/user/user/advuser';
+}
+
 function advuser_multiple_email_confirm() {
   $edit = $_POST;
 
@@ -708,4 +815,23 @@ function advuser_user_roles() {
   return $roles;
 }
 
+/**
+ * Proposition for helper function. This should easy maintanance. Probably can be used also in 
+ * advuser_multiple_delete_confirm and advuser_multiple_email_confirm
+ * 
+ * Adds selected users uid to $form.
+ */
+function _form_add_accounts(&$form, $edit) {
+  // array_filter returns only elements with TRUE values
+  foreach (array_filter($edit['accounts']) as $uid => $value) {
+    $user = db_result(db_query('SELECT name FROM {users} WHERE uid = %d', $uid));
+    $form['accounts'][$uid] = array(
+      '#type' => 'hidden', 
+      '#value' => $uid, 
+      '#prefix' => '<li>', 
+      '#suffix' => check_plain($user) ."</li>\n"
+    );
+  }
+}
+
 // vim:ft=php:sts=2:sw=2:ts=2:et:ai:sta:ff=unix
