array( 'description' => t('Send email to all users in specified role(s)'), 'type' => 'node', 'configurable' => TRUE, 'hooks' => array( 'nodeapi' => array('view', 'insert', 'update', 'delete'), ) ), ); } /** * Return a form definition so the Send email action can be configured. * * @see action_email_role_send_email_action_validate() * @see action_email_role_send_email_action_submit() * @param $context * Default values (if we are editing an existing action instance). * @return * Form definition. */ function action_email_role_send_email_action_form($context) { // Set default values for form. if (!isset($context['recipient'])) { $context['recipient'] = ''; } if (!isset($context['subject'])) { $context['subject'] = ''; } if (!isset($context['message'])) { $context['message'] = ''; } // get available roles $result = db_query('SELECT * FROM {role} WHERE rid NOT IN (1,2)'); while ($role = db_fetch_object($result)) { $roles[$role->rid] = $role->name; } // add form components $form['recipient'] = array( '#type' => 'checkboxes', '#title' => t('Recipient Roles'), '#default_value' => $context['recipient'], '#options' => $roles, '#description' => t('Select the role(s) you would like to email'), '#required' => TRUE, ); $form['subject'] = array( '#type' => 'textfield', '#title' => t('Subject'), '#default_value' => $context['subject'], '#maxlength' => '254', '#description' => t('The subject of the message.'), '#required' => TRUE, ); $form['message'] = array( '#type' => 'textarea', '#title' => t('Message'), '#default_value' => $context['message'], '#cols' => '80', '#rows' => '20', '#description' => t('The message that should be sent. You may include the following variables: %site_name, %uid, %node_url, %node_type, %title, %teaser, %body. Not all variables will be available in all contexts.'), '#required' => TRUE, ); return $form; } /** * Process action_email_role_send_email_action form submissions. */ function action_email_role_send_email_action_submit($form_id, $form_values) { // Process the HTML form to store configuration. The keyed array that // we return will be serialized to the database. $params = array( 'recipient' => $form_values['recipient'], 'subject' => $form_values['subject'], 'message' => $form_values['message'], ); return $params; } /** * Implementation of a configurable Drupal action. Sends an email to specified role(s). */ function action_email_role_send_email_action($object, $context) { $node = $object; $from = variable_get('site_mail', ini_get('sendmail_from')); $recipient = $context['recipient']; $subject = $context['subject']; $message = $context['message']; $site_name = variable_get('site_name', 'DonorFirst Platform'); if (isset($node) && is_object($node)) { $variables = array( '%site_name' => $site_name, '%uid' => $node->uid, '%node_url' => url('node/' . $node->nid, NULL, NULL, TRUE), '%node_type' => $node->type, '%title' => $node->title, '%teaser' => strip_tags($node->teaser), '%body' => strip_tags($node->body) ); $subject = strtr($subject, $variables); $subject = str_replace(array("\r", "\n"), '', $subject); $message = strtr($message, $variables); } foreach($recipient as $rid => $rname) { if (!empty($rname)) { $roles[]=$rid; } } $emailed = 0; $result = db_query("SELECT ur.*, u.mail, u.name FROM {users_roles} ur LEFT JOIN {users} u ON ur.uid = u.uid WHERE ur.rid IN (%s) AND u.status = 1", implode(',', $roles)); while($role = db_fetch_object($result)) { if (drupal_mail('action_email_role', $role->mail, $subject, $message, $from)) { watchdog('action_email_role', t('Sent email to %recipient', array('%recipient' => $role->mail))); $emailed++; } else { watchdog('error', t('Unable to send email to %recipient from action_email_role', array('%recipient' => $role->mail))); } } watchdog('action_email_role',"$emailed users emailed successfuly from action_email_role."); }