Index: vr_list/vr_list.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/vr/vr_list/vr_list.module,v
retrieving revision 1.1.2.9
diff -u -p -r1.1.2.9 vr_list.module
--- vr_list/vr_list.module	22 Jun 2010 17:10:16 -0000	1.1.2.9
+++ vr_list/vr_list.module	13 Jul 2010 16:01:08 -0000
@@ -370,4 +370,130 @@ function vr_list_update_finish($success,
     $message = t('An error occurred while processing %error_operation with arguments: @arguments', array('%error_operation' => $error_operation[0], '@arguments' => print_r($error_operation[1], TRUE)));
     drupal_set_message($message);
   }
+}
+
+/**
+ * Create a bulk operation for managing lists.
+ */
+function vr_list_user_operations() {
+  $lists = vr_get_lists();
+  $operations = array();
+  foreach ($lists as $list_id => $list_name) {
+    $operations[$list_id . '_subscribe'] = array(
+      'label' => t('Subscribe to !list', array('!list' => $list_name)),
+      'callback' => 'vr_list_bulk_subscribe',
+      'callback arguments' => array($list_id, 'subscribe'),
+      );
+    $operations[$list_id . '_unsubscribe'] = array(
+      'label' => t('Remove from !list', array('!list' => $list_name)),
+      'callback' => 'vr_list_bulk_subscribe',
+      'callback arguments' => array($list_id, 'unsubscribe'),
+      );
+  }
+  // Make certain that the acting user has permission for these operations.
+  if (user_access('administer VerticalResponse')) {
+    return $operations;
+  }
+}
+
+/**
+ * Callback for user_operations.
+ */
+function vr_list_bulk_subscribe($users, $list_id, $method) {
+  // Make certain that the acting user has permission for these operations.
+  if (!user_access('administer VerticalResponse')) {
+    return;
+  }
+  $action = array(
+    '@action' => ucfirst($method),
+    );
+  // Add users to list.
+  $operations[] = array(
+    '_vr_list_subscribe',
+    array($users, $list_id, $method),
+    );
+  // Update the list if there is a field mapping.
+  $operations[] = array(
+    '_vr_list_update',
+    array($list_id, NULL),
+    );
+  $batch = array(
+    'operations' => $operations,
+    'finished' => '_vr_list_subscribe_finish',
+    'title' => t('@action users to VerticalResponse mailing list.', $action),
+    'progress_message' => t('Processed @current out of @total.'),
+    );
+  batch_set($batch);
+}
+
+/**
+ * Bulk operation for adding and removing users from a list.
+ * 
+ * @param array
+ *  an array of uids keyed on uid.
+ * @param int
+ *  VerticalResponse list id
+ * @param string
+ *  The desired action (subscribe || unsubscribe)
+ * @param array
+ *  context array for batch operation.
+ */
+function _vr_list_subscribe($users, $list_id, $method, &$context) {
+  if (empty($context['sandbox'])) {
+    $list = vr_get_list_by_id($list_id);
+    $context['sandbox']['total'] = count($users);
+    $context['sandbox']['progress'] = 0;
+    $context['sandbox']['users'] = array_values($users);
+    $members = vr_api('getListMembers', array('list_id' => $list_id, 'max_records' => $list->size));
+    $path = drupal_get_path('module', 'vr_list');
+    include_once($path . '/vr_list_member.inc');
+    $list_member = new vr_list_member($list->id);
+    foreach ($members as $member) {
+      $list_member_array = $list_member->make_array($member);
+      $list_members[$list_member_array['email_address']] = $list_member_array['hash'];
+    }
+    $context['sandbox']['members'] = $list_members;
+  }
+  $i &= $context['sandbox']['progress'];
+  $max = $i + variable_get('vr_list_throttle_' . $list_id, 10);
+  $max = ($max < $context['sandbox']['total']) ? $max : $context['sandbox']['total'];
+  for ($i; $i < $max; $i++) {
+    $user = user_load($context['sandbox']['users'][$i]);
+    $param['list_member']['list_id'] = $list_id;
+    // Only subscribe new users
+    if ($method == 'subscribe' AND empty($context['sandbox']['members'][$user->mail])) {
+      $param['list_member']['member_data'] = array(
+        array(
+          'name' => 'email_address',
+          'value' => $user->mail,
+          ),
+        );
+      vr_api('addListMember', $param);
+    }
+    // Remove users only if they are subscribed.
+    if ($method == 'unsubscribe' AND !empty($context['sandbox']['members'][$user->mail])) {
+      $param['list_member']['member_data'] = array(
+        array(
+          'name' => 'hash',
+          'value' => $context['sandbox']['members'][$user->mail],
+          ),
+        );
+      vr_api('deleteListMember', $param);
+    }
+  }
+  return ($i > 0) ? $i / $context['sandbox']['total'] : 0;
+}
+
+/**
+ * Finished callback for _vr_list_subscribe
+ */
+function _vr_list_subscribe_finish($success, $results, $operations) {
+  if ($success) {
+    $message = t('VerticalResponse has been updated.');
+    drupal_set_message($message);
+  }
+  else {
+    $message = t('An error occured during this operation. Please try again.');
+    drupal_set_message($message);
+  }
 }
\ No newline at end of file
