| 1 |
<?php
|
| 2 |
// $Id: action_email_role.module,v 1.6 2008/09/13 03:28:37 bec Exp $
|
| 3 |
|
| 4 |
/*
|
| 5 |
* @file
|
| 6 |
* Provides an action to email all users in selected role(s)
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_action_info().
|
| 11 |
*/
|
| 12 |
function action_email_role_action_info() {
|
| 13 |
return array(
|
| 14 |
'action_email_role_send_email_action' => array(
|
| 15 |
'description' => t('Send email to all users in specified role(s)'),
|
| 16 |
'type' => 'node',
|
| 17 |
'configurable' => TRUE,
|
| 18 |
'hooks' => array(
|
| 19 |
'nodeapi' => array('view', 'insert', 'update', 'delete'),
|
| 20 |
)
|
| 21 |
),
|
| 22 |
);
|
| 23 |
}
|
| 24 |
|
| 25 |
/**
|
| 26 |
* Return a form definition so the Send email action can be configured.
|
| 27 |
*
|
| 28 |
* @see action_email_role_send_email_action_validate()
|
| 29 |
* @see action_email_role_send_email_action_submit()
|
| 30 |
* @param $context
|
| 31 |
* Default values (if we are editing an existing action instance).
|
| 32 |
* @return
|
| 33 |
* Form definition.
|
| 34 |
*/
|
| 35 |
function action_email_role_send_email_action_form($context) {
|
| 36 |
// Set default values for form.
|
| 37 |
if (!isset($context['recipient'])) {
|
| 38 |
$context['recipient'] = array();
|
| 39 |
}
|
| 40 |
if (!isset($context['node_types_set'])) {
|
| 41 |
$context['node_types_set'] = 'all';
|
| 42 |
}
|
| 43 |
if (!isset($context['node_types_selected'])) {
|
| 44 |
$context['node_types_selected'] = array();
|
| 45 |
}
|
| 46 |
if (!isset($context['subject'])) {
|
| 47 |
$context['subject'] = '';
|
| 48 |
}
|
| 49 |
if (!isset($context['message'])) {
|
| 50 |
$context['message'] = '';
|
| 51 |
}
|
| 52 |
|
| 53 |
// roles checkboxes
|
| 54 |
$result = db_query('SELECT * FROM {role} WHERE rid NOT IN (1,2)');
|
| 55 |
while ($role = db_fetch_object($result)) {
|
| 56 |
$roles[$role->rid] = $role->name;
|
| 57 |
}
|
| 58 |
|
| 59 |
$form['recipient'] = array(
|
| 60 |
'#type' => 'checkboxes',
|
| 61 |
'#title' => t('Recipient Roles'),
|
| 62 |
'#default_value' => $context['recipient'],
|
| 63 |
'#options' => $roles,
|
| 64 |
'#description' => t('Select the role(s) you would like to email'),
|
| 65 |
'#required' => TRUE,
|
| 66 |
);
|
| 67 |
|
| 68 |
// node type checkboxes
|
| 69 |
$node_types = node_get_types();
|
| 70 |
foreach ($node_types as $key => $val) {
|
| 71 |
$node_types[$key] = $val->name;
|
| 72 |
}
|
| 73 |
|
| 74 |
$form['node_types'] = array(
|
| 75 |
'#type' => 'fieldset',
|
| 76 |
'#title' => t('Send notification for specific node types'),
|
| 77 |
'#collapsible' => TRUE,
|
| 78 |
'#collapsed' => ($context['node_types_set'] == 'all'),
|
| 79 |
);
|
| 80 |
|
| 81 |
$form['node_types']['node_types_set'] = array(
|
| 82 |
'#type' => 'radios',
|
| 83 |
'#title' => t('Perform action for'),
|
| 84 |
'#options' => array(
|
| 85 |
'all' => t('all node types'),
|
| 86 |
'selected' => t('selected node types (below)'),
|
| 87 |
),
|
| 88 |
'#default_value' => $context['node_types_set'],
|
| 89 |
'#required' => TRUE,
|
| 90 |
);
|
| 91 |
|
| 92 |
$form['node_types']['node_types_selected'] = array(
|
| 93 |
'#type' => 'checkboxes',
|
| 94 |
'#title' => t('Node types'),
|
| 95 |
'#default_value' => $context['node_types_selected'],
|
| 96 |
'#options' => $node_types,
|
| 97 |
'#description' => t('Select the node types this action should affect'),
|
| 98 |
);
|
| 99 |
|
| 100 |
// message components
|
| 101 |
$form['subject'] = array(
|
| 102 |
'#type' => 'textfield',
|
| 103 |
'#title' => t('Subject'),
|
| 104 |
'#default_value' => $context['subject'],
|
| 105 |
'#maxlength' => '254',
|
| 106 |
'#description' => t('The subject of the message.'),
|
| 107 |
'#required' => TRUE,
|
| 108 |
);
|
| 109 |
|
| 110 |
$form['message'] = array(
|
| 111 |
'#type' => 'textarea',
|
| 112 |
'#title' => t('Message'),
|
| 113 |
'#default_value' => $context['message'],
|
| 114 |
'#cols' => '80',
|
| 115 |
'#rows' => '20',
|
| 116 |
'#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.'),
|
| 117 |
'#required' => TRUE,
|
| 118 |
);
|
| 119 |
|
| 120 |
return $form;
|
| 121 |
}
|
| 122 |
|
| 123 |
/**
|
| 124 |
* Process action_email_role_send_email_action form submissions.
|
| 125 |
*/
|
| 126 |
function action_email_role_send_email_action_submit($form, &$form_state) {
|
| 127 |
// Process the HTML form to store configuration. The keyed array that
|
| 128 |
// we return will be serialized to the database.
|
| 129 |
$params = array(
|
| 130 |
'recipient' => $form_state['values']['recipient'],
|
| 131 |
'subject' => $form_state['values']['subject'],
|
| 132 |
'message' => $form_state['values']['message'],
|
| 133 |
'node_types_set' => $form_state['values']['node_types_set'],
|
| 134 |
'node_types_selected' => $form_state['values']['node_types_selected'],
|
| 135 |
);
|
| 136 |
return $params;
|
| 137 |
}
|
| 138 |
|
| 139 |
/**
|
| 140 |
* Implementation of a configurable Drupal action. Sends an email to specified role(s).
|
| 141 |
*/
|
| 142 |
function action_email_role_send_email_action($object, $context) {
|
| 143 |
if ($context['node_types_set'] == 'all' || $context['node_types_selected'][$object->type]) {
|
| 144 |
$from = variable_get('site_mail', ini_get('sendmail_from'));
|
| 145 |
$params['object'] = $object;
|
| 146 |
$params['context'] = $context;
|
| 147 |
|
| 148 |
foreach ($context['recipient'] as $rid => $rname) {
|
| 149 |
if (!empty($rname)) {
|
| 150 |
$roles[]=$rid;
|
| 151 |
}
|
| 152 |
}
|
| 153 |
|
| 154 |
$emailed = 0;
|
| 155 |
$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));
|
| 156 |
while ($account = db_fetch_object($result)) {
|
| 157 |
// @@@ 'en' shouldn't be hardcoded
|
| 158 |
if (drupal_mail('action_email_role', 'email_roles', $account->mail, 'en', $params, $from)) {
|
| 159 |
watchdog('action_email_rol', 'Sent email to %recipient', array('%recipient' => $account->mail));
|
| 160 |
$emailed++;
|
| 161 |
}
|
| 162 |
else {
|
| 163 |
watchdog('error', 'Unable to send email to %recipient from action_email_role', array('%recipient' => $account->mail));
|
| 164 |
}
|
| 165 |
}
|
| 166 |
watchdog('action_email_rol', "!emailed users emailed successfuly.", array('!emailed' => $emailed));
|
| 167 |
}
|
| 168 |
}
|
| 169 |
|
| 170 |
/**
|
| 171 |
* Implementation of hook_mail().
|
| 172 |
*/
|
| 173 |
function action_email_role_mail($key, &$message, $params) {
|
| 174 |
$node = $params['object'];
|
| 175 |
|
| 176 |
$recipient = $params['context']['recipient'];
|
| 177 |
$subject = $params['context']['subject'];
|
| 178 |
$message_body = $params['context']['message'];
|
| 179 |
|
| 180 |
$variables = array(
|
| 181 |
'%site_name' => variable_get('site_name', 'Drupal'),
|
| 182 |
'%uid' => $node->uid,
|
| 183 |
'%node_url' => url('node/' . $node->nid, array('absolute' => TRUE)),
|
| 184 |
'%node_type' => node_get_types('name', $node),
|
| 185 |
'%title' => check_plain($node->title),
|
| 186 |
'%teaser' => check_markup($node->teaser),
|
| 187 |
'%body' => check_markup($node->body)
|
| 188 |
);
|
| 189 |
|
| 190 |
$subject = strtr($subject, $variables);
|
| 191 |
$message['subject'] = str_replace(array("\r", "\n"), '', $subject);
|
| 192 |
$message['body'] = strtr($message_body, $variables);
|
| 193 |
}
|