| 1 |
<?php
|
| 2 |
|
| 3 |
function actions_token_email_send_email_action_form($context) {
|
| 4 |
// Set default values for form.
|
| 5 |
if (!isset($context['recipient'])) {
|
| 6 |
$context['recipient'] = '';
|
| 7 |
}
|
| 8 |
if (!isset($context['subject'])) {
|
| 9 |
$context['subject'] = '';
|
| 10 |
}
|
| 11 |
if (!isset($context['message'])) {
|
| 12 |
$context['message'] = '';
|
| 13 |
}
|
| 14 |
|
| 15 |
$form['recipient'] = array(
|
| 16 |
'#type' => 'textfield',
|
| 17 |
'#title' => t('Recipient'),
|
| 18 |
'#default_value' => $context['recipient'],
|
| 19 |
'#maxlength' => '254',
|
| 20 |
'#description' => t('The email address to which the message should be sent OR enter %author if you would like to send an e-mail to the author of the original post.', array('%author' => '%author')),
|
| 21 |
);
|
| 22 |
$form['subject'] = array(
|
| 23 |
'#type' => 'textfield',
|
| 24 |
'#title' => t('Subject'),
|
| 25 |
'#default_value' => $context['subject'],
|
| 26 |
'#maxlength' => '254',
|
| 27 |
'#description' => t('The subject of the message. You may include variables from the available tokens below.'),
|
| 28 |
);
|
| 29 |
$form['message'] = array(
|
| 30 |
'#type' => 'textarea',
|
| 31 |
'#title' => t('Message'),
|
| 32 |
'#default_value' => $context['message'],
|
| 33 |
'#cols' => '80',
|
| 34 |
'#rows' => '20',
|
| 35 |
'#description' => t('The message that should be sent. You may include variables from the available tokens below.'),
|
| 36 |
);
|
| 37 |
$form['tokens_list'] = array(
|
| 38 |
'#type' => 'fieldset',
|
| 39 |
'#title' => t('Available tokens'),
|
| 40 |
'#collapsible' => TRUE,
|
| 41 |
'#collapsed' => TRUE,
|
| 42 |
'list' => array(
|
| 43 |
'#value' => theme('token_help', 'node', '%', ''),
|
| 44 |
),
|
| 45 |
);
|
| 46 |
return $form;
|
| 47 |
}
|
| 48 |
|
| 49 |
function actions_token_email_send_email_action_validate($form_id, $form_values) {
|
| 50 |
// Validate the configuration form.
|
| 51 |
if (!valid_email_address($form_values['recipient']) && $form_values['recipient'] != '%author') {
|
| 52 |
// We want the literal %author placeholder to be emphasized in the error message.
|
| 53 |
form_set_error('recipient', t('Please enter a valid email address or %author.', array('%author' => '%author')));
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
function actions_token_email_send_email_action_submit($form_id, $form_values) {
|
| 58 |
// Process the HTML form to store configuration. The keyed array that
|
| 59 |
// we return will be serialized to the database.
|
| 60 |
$params = array(
|
| 61 |
'recipient' => $form_values['recipient'],
|
| 62 |
'subject' => $form_values['subject'],
|
| 63 |
'message' => $form_values['message'],
|
| 64 |
);
|
| 65 |
return $params;
|
| 66 |
}
|
| 67 |
|
| 68 |
function actions_token_email_send_email_action($object, $context) {
|
| 69 |
global $user;
|
| 70 |
|
| 71 |
switch ($context['hook']) {
|
| 72 |
case 'nodeapi':
|
| 73 |
// Because this is not an action of type 'node' the node
|
| 74 |
// will not be passed as $object, but it will still be available
|
| 75 |
// in $context.
|
| 76 |
$node = $context['node'];
|
| 77 |
break;
|
| 78 |
case 'comment':
|
| 79 |
// The comment hook provides nid, in $context.
|
| 80 |
$comment = $context['comment'];
|
| 81 |
$node = node_load($comment->nid);
|
| 82 |
break;
|
| 83 |
case 'user':
|
| 84 |
// Because this is not an action of type 'user' the user
|
| 85 |
// object is not passed as $object, but it will still be available
|
| 86 |
// in $context.
|
| 87 |
$account = $context['account'];
|
| 88 |
if (isset($context['node'])) {
|
| 89 |
$node = $context['node'];
|
| 90 |
}
|
| 91 |
elseif ($context['recipient'] == '%author') {
|
| 92 |
// If we don't have a node, we don't have a node author.
|
| 93 |
watchdog('error', t("Cannot use '%author' token in this context."));
|
| 94 |
return;
|
| 95 |
}
|
| 96 |
break;
|
| 97 |
default:
|
| 98 |
// Check context for node.
|
| 99 |
if (!isset($object) && isset($context['node'])) {
|
| 100 |
$node = $context['node'];
|
| 101 |
}
|
| 102 |
else {
|
| 103 |
// We are being called directly.
|
| 104 |
$node = $object;
|
| 105 |
}
|
| 106 |
}
|
| 107 |
|
| 108 |
$recipient = $context['recipient'];
|
| 109 |
|
| 110 |
if (isset($node)) {
|
| 111 |
if (!isset($account)) {
|
| 112 |
$account = user_load(array('uid' => $node->uid));
|
| 113 |
}
|
| 114 |
if ($recipient == '%author') {
|
| 115 |
$recipient = $account->mail;
|
| 116 |
}
|
| 117 |
}
|
| 118 |
|
| 119 |
if (!isset($account)) {
|
| 120 |
$account = $user;
|
| 121 |
}
|
| 122 |
|
| 123 |
$subject = token_replace($context['subject'], 'node', $node, '%', '');
|
| 124 |
$subject = str_replace(array("\r", "\n"), '', $subject);
|
| 125 |
$message = token_replace($context['message'], 'node', $node, '%', '');
|
| 126 |
|
| 127 |
if (drupal_mail('action_send_email', $recipient, $subject, $message, $from)) {
|
| 128 |
watchdog('action', t('Sent email to %recipient', array('%recipient' => $recipient)));
|
| 129 |
}
|
| 130 |
else {
|
| 131 |
watchdog('error', t('Unable to send email to %recipient', array('%recipient' => $recipient)));
|
| 132 |
}
|
| 133 |
}
|
| 134 |
|
| 135 |
function actions_token_email_action_info() {
|
| 136 |
return array(
|
| 137 |
'actions_token_email_send_email_action' => array(
|
| 138 |
'description' => t('Send tokened e-mail'),
|
| 139 |
'type' => 'system',
|
| 140 |
'configurable' => TRUE,
|
| 141 |
'hooks' => array(
|
| 142 |
'nodeapi' => array('view', 'insert', 'update', 'delete'),
|
| 143 |
'comment' => array('view', 'insert', 'update', 'delete'),
|
| 144 |
'user' => array('view', 'insert', 'update', 'delete', 'login'),
|
| 145 |
'taxonomy' => array('insert', 'update', 'delete'),
|
| 146 |
)
|
| 147 |
),
|
| 148 |
);
|
| 149 |
}
|
| 150 |
|
| 151 |
|