| 1 |
<?php
|
| 2 |
// $Id: broadcast.inc,v 1.3 2009/01/19 16:44:36 dww Exp $
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Code related to sending signup broadcast messages.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Form builder for the signup broadcast form.
|
| 12 |
*
|
| 13 |
* @param $node
|
| 14 |
* The node that the broadcast form is being attached to.
|
| 15 |
*/
|
| 16 |
function signup_broadcast_form($form_state, $node) {
|
| 17 |
$signups = signup_get_signups($node->nid);
|
| 18 |
if (empty($signups)) {
|
| 19 |
$form['no_users'] = array(
|
| 20 |
'#value' => t('No users have signup up for this %node_type.', array('%node_type' => node_get_types('name', $node->type))),
|
| 21 |
);
|
| 22 |
return $form;
|
| 23 |
}
|
| 24 |
|
| 25 |
$tokens = array('%node_title', '%node_url', '%user_mail', '%user_name', t('%cancel_signup_url (access to this link is denied to users without the "cancel own signups" permission)'));
|
| 26 |
if (_signup_get_node_scheduler($node) != 'none') {
|
| 27 |
$tokens = array_merge(array('%node_start_time'), $tokens);
|
| 28 |
}
|
| 29 |
if (module_exists('token')) {
|
| 30 |
$token_text = t('Supported string substitutions: %tokens, and any tokens in the %replacement_tokens list.', array('%tokens' => implode(', ', $tokens), '%replacement_tokens' => t('Replacement tokens')));
|
| 31 |
}
|
| 32 |
else {
|
| 33 |
$token_text = t('Supported string substitutions: %tokens.', array('%tokens' => implode(', ', $tokens)));
|
| 34 |
}
|
| 35 |
|
| 36 |
$form['subject'] = array(
|
| 37 |
'#type' => 'textfield',
|
| 38 |
'#title' => t('Subject'),
|
| 39 |
'#required' => TRUE,
|
| 40 |
);
|
| 41 |
$form['message'] = array(
|
| 42 |
'#type' => 'textarea',
|
| 43 |
'#title' => t('Message body'),
|
| 44 |
'#required' => TRUE,
|
| 45 |
'#description' => t('Body of the email message you wish to send to all users who have signed up for this %node_type. !token_description', array('%node_type' => node_get_types('name', $node->type), '!token_description' => $token_text)),
|
| 46 |
'#rows' => 10,
|
| 47 |
);
|
| 48 |
|
| 49 |
if (module_exists('token')) {
|
| 50 |
module_load_include('inc', 'signup', 'includes/token_help');
|
| 51 |
_signup_token_help($form, 'message_tokens_fieldset');
|
| 52 |
}
|
| 53 |
|
| 54 |
$form['copy'] = array(
|
| 55 |
'#type' => 'checkbox',
|
| 56 |
'#title' => t('Send yourself a copy.'),
|
| 57 |
);
|
| 58 |
|
| 59 |
$form['send'] = array(
|
| 60 |
'#type' => 'submit',
|
| 61 |
'#value' => t('Send'),
|
| 62 |
);
|
| 63 |
$form['nid'] = array(
|
| 64 |
'#type' => 'value',
|
| 65 |
'#value' => $node->nid,
|
| 66 |
);
|
| 67 |
|
| 68 |
global $user;
|
| 69 |
if (user_access('administer site configuration')) {
|
| 70 |
$form['from'] = array(
|
| 71 |
'#type' => 'textfield',
|
| 72 |
'#title' => t('From'),
|
| 73 |
'#required' => TRUE,
|
| 74 |
'#default_value' => $user->mail,
|
| 75 |
'#weight' => '-10',
|
| 76 |
);
|
| 77 |
}
|
| 78 |
else {
|
| 79 |
$form['from'] = array(
|
| 80 |
'#value' => t('This message will be sent from: %from', array('%from' => $user->mail)),
|
| 81 |
'#pre' => '<strong>',
|
| 82 |
'#post' => '</strong>',
|
| 83 |
);
|
| 84 |
}
|
| 85 |
return $form;
|
| 86 |
}
|
| 87 |
|
| 88 |
/**
|
| 89 |
* Submit callback for the signup broadcast form.
|
| 90 |
*
|
| 91 |
* @param $form
|
| 92 |
* The broadcast form being submitted.
|
| 93 |
* @param $form_state
|
| 94 |
* The state of the submitted form (including values).
|
| 95 |
*
|
| 96 |
* @see signup_send_broadcast()
|
| 97 |
*/
|
| 98 |
function signup_broadcast_form_submit($form, &$form_state) {
|
| 99 |
global $user;
|
| 100 |
if (user_access('administer site configuration')) {
|
| 101 |
$from = $form_state['values']['from'];
|
| 102 |
}
|
| 103 |
else {
|
| 104 |
$from = $user->mail;
|
| 105 |
}
|
| 106 |
signup_send_broadcast($form_state['values']['nid'], $form_state['values']['subject'], $form_state['values']['message'], $from, !empty($form_state['values']['copy']));
|
| 107 |
}
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Send an email message to users who have signed up to a node.
|
| 111 |
*
|
| 112 |
* @param $nid
|
| 113 |
* Node ID of the signup node to send a broadcast about.
|
| 114 |
* @param $subject
|
| 115 |
* The subject of the broadcast message to send.
|
| 116 |
* @param $body
|
| 117 |
* The body of the broadcast message to send.
|
| 118 |
* @param $from
|
| 119 |
* The from address to use when sending the broadcast message.
|
| 120 |
* @param $copy
|
| 121 |
* Optional boolean indicating if the current user should get a copy.
|
| 122 |
* @param $signups
|
| 123 |
* Optional array of signup objects to send the broadcast to. Each object
|
| 124 |
* in the array should at least contain the following fields:
|
| 125 |
* - "sid": The unique signup ID
|
| 126 |
* - "mail" or "anon_mail": E-mail address to send to depending on if it's
|
| 127 |
* an authenticated or anonymous signup.
|
| 128 |
* - "name": Username of an authenticated signup.
|
| 129 |
* - "form_data": The optional custom signup form data for this signup.
|
| 130 |
* - "language": The user's prefered language (if known).
|
| 131 |
*
|
| 132 |
* @see signup_get_signup()
|
| 133 |
* @see _signup_get_email()
|
| 134 |
* @see _signup_get_email_tokens()
|
| 135 |
*/
|
| 136 |
function signup_send_broadcast($nid, $subject, $body, $from, $copy = FALSE, $signups = array()) {
|
| 137 |
global $user;
|
| 138 |
$send_all = FALSE;
|
| 139 |
// If the parameter isn't defined, grab all users who signed up to the node.
|
| 140 |
if (empty($signups)) {
|
| 141 |
$signups = signup_get_signups($nid);
|
| 142 |
$send_all = TRUE;
|
| 143 |
}
|
| 144 |
// Make sure that signup_get_signups() returned something for this node.
|
| 145 |
if (!empty($signups)) {
|
| 146 |
$node = node_load($nid);
|
| 147 |
foreach ($signups as $signup) {
|
| 148 |
$user_mail = _signup_get_email($signup);
|
| 149 |
$params = array(
|
| 150 |
'subject' => $subject,
|
| 151 |
'body' => $body,
|
| 152 |
'node' => $node,
|
| 153 |
'signup' => $signup,
|
| 154 |
);
|
| 155 |
if (module_exists('token')) {
|
| 156 |
$params['body'] = token_replace_multiple($params['body'], array('node' => $node, 'signup' => $signup, 'global' => NULL));
|
| 157 |
}
|
| 158 |
$language = user_preferred_language($signup);
|
| 159 |
drupal_mail('signup', 'signup_broadcast_mail', $user_mail, $language, $params, $from);
|
| 160 |
watchdog('signup', 'Broadcast email for %title sent to %email.', array('%title' => $node->title, '%email' => $user_mail), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
|
| 161 |
}
|
| 162 |
if ($copy) {
|
| 163 |
$sender_email = _signup_get_email($user);
|
| 164 |
$signup_tokens = _signup_get_email_tokens($node, $user);
|
| 165 |
$message = strtr($body, $signup_tokens);
|
| 166 |
if (module_exists('token')) {
|
| 167 |
// If the token.module is enabled, also handle any tokens it provides.
|
| 168 |
$message = token_replace_multiple($message, array('node' => $node, 'signup' => $signup, 'global' => NULL));
|
| 169 |
}
|
| 170 |
$final_text = theme('signup_broadcast_sender_copy', $body, $message);
|
| 171 |
$params = array(
|
| 172 |
'subject' => $subject,
|
| 173 |
'body' => $final_text,
|
| 174 |
'ignore_tokens' => TRUE,
|
| 175 |
);
|
| 176 |
$language = user_preferred_language($user);
|
| 177 |
drupal_mail('signup', 'signup_broadcast_mail', $sender_email, $language, $params, $from);
|
| 178 |
watchdog('signup', 'Broadcast email copy for %title sent to %email.', array('%title' => $node->title, '%email' => $sender_email), WATCHDOG_NOTICE, l(t('view'), 'node/'. $node->nid));
|
| 179 |
drupal_set_message(t('Sent a copy of this message to %email', array('%email' => $sender_email)));
|
| 180 |
}
|
| 181 |
}
|
| 182 |
if ($send_all) {
|
| 183 |
drupal_set_message(t('Message sent to all users who have signed up'));
|
| 184 |
}
|
| 185 |
else {
|
| 186 |
drupal_set_message(t('Message sent to selected users who have signed up'));
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|