Index: workflow_ng_system.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/workflow_ng/workflow_ng/modules/Attic/workflow_ng_system.inc,v
retrieving revision 1.1.2.6
diff -u -r1.1.2.6 workflow_ng_system.inc
--- workflow_ng_system.inc	12 Dec 2007 17:14:09 -0000	1.1.2.6
+++ workflow_ng_system.inc	18 Dec 2007 20:48:12 -0000
@@ -160,6 +160,12 @@
       '#module' => t('System'),
       '#description' => t('You may use the token replacements for the mail\'s message body as well as the mail\'s subject.'),
     ),
+    'workflow_ng_action_mail_to_users_of_role' => array(
+      '#label' => t('Send mail to all users of a role'),
+      '#arguments' => array(),
+      '#module' => t('System'),
+      '#description' => t('You may use the token replacements for the mail\'s message body as well as the mail\'s subject.'),
+    ),
     'workflow_ng_action_mail' => array(
       '#label' => t('Send a mail to an arbitrary mail address'),
       '#module' => t('System'),
@@ -186,6 +192,94 @@
   );
 }
 
+function workflow_ng_action_mail_to_users_of_role_form($settings = array(), $argument_info) {
+  // Select only non-anonymous user roles.
+  $roles = user_roles(TRUE);
+
+  $form['recipients'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Recipient Role Groups'),
+    '#default_value' => $settings['recipients'],
+    '#options' => $roles,
+    '#description' => t('Select the roles whose users should receive this email. WARNING: This may cause problems if there are too many users of these roles on your site, as your server may not be able to handle all the mail requests.'),
+  );
+  $form['from'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Sender'),
+    '#default_value' => isset($settings['from']) ? $settings['from'] : variable_get('site_mail', ini_get('sendmail_from')),
+    '#description' => t('The mail\'s "from" address.'),
+    '#required' => TRUE,
+  );
+  $form['subject'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Subject'),
+    '#default_value' => $settings['subject'],
+    '#description' => t('The mail\'s subject.'),
+  );
+  $form['message'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Message'),
+    '#default_value' => $settings['message'],
+    '#description' => t('The mail\'s message body.'),
+  );
+  workflow_ng_token_replacement_help($form, $argument_info);
+  return $form;
+}
+
+function workflow_ng_action_mail_to_users_of_role_submit($form_id, $form_values) {
+  $settings = array();
+  $form_values['recipients'] = array_filter($form_values['recipients']);
+  foreach (array('from', 'subject', 'message', 'recipients') as $key) {
+    $settings[$key] = $form_values[$key];
+    $settings[$key .'_args'] = workflow_ng_token_get_used_arguments($form_values[$key], $form_values['arguments']);
+  }
+  return $settings;
+}
+
+function workflow_ng_action_mail_to_users_of_role($settings, &$arguments, &$log) {
+  $names = array('from', 'subject', 'recipients');
+  extract( workflow_ng_token_replace_all(array('message' => 'message') + $names, $settings, $arguments, $log) );
+
+  foreach ($names as $key) {
+    $$key = str_replace(array("\r", "\n"), '', $$key);
+  }
+
+  $recipient_roles = array();
+
+  foreach ($recipients as $rid => $chose_role) {
+    if ($chose_role) {
+      $recipient_roles[$rid] = $rid;
+    }
+  }
+
+  $recipient_addresses = array();
+
+  // All authenticated users, which is everybody.
+  if ($recipient_roles[2]) {
+    $result = db_query('SELECT mail FROM {users} WHERE uid > 0');
+    while($account = db_fetch_object($result)) {
+      $recipient_addresses[] = $account->mail;
+    }
+  }
+  else {
+    $roles = implode(',', $recipient_roles);
+    // Avoid sending emails to members of two target role groups.
+    $result = db_query('SELECT DISTINCT u.mail FROM {users} u INNER JOIN {users_roles} r ON u.uid = r.uid WHERE r.rid IN (%s)', $roles);
+    while($account = db_fetch_object($result)) {
+      $recipient_addresses[] = $account->mail;
+    }
+  }
+
+  foreach ($recipient_addresses as $recipient) {
+    if (drupal_mail('workflow-ng-mass-user-mail-by-role', $recipient, $subject, $message, $from)) {
+      watchdog('action', t('Sent email to %recipient', array('%recipient' => $recipient)), WATCHDOG_NOTICE);
+    }
+    else {
+      watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient)), WATCHDOG_ERROR);
+    }
+  }
+}
+
 /*
  * Action Implementation: Show a configureable message 
  */
