View source
<?php
function regcode_mailer_help($path, $arg) {
$output = '';
switch ($path) {
case 'admin/user/regcodes/mail':
$output = '<p>' . t('Email registration codes to users.') . '</p>';
break;
}
return $output;
}
function regcode_mailer_menu() {
$items = array();
$items['admin/user/regcodes/mail'] = array(
'title' => 'Mail',
'description' => 'Mail registration codes to users',
'page callback' => 'drupal_get_form',
'file' => '../regcode.admin.php',
'page arguments' => array(
'regcode_mailer_admin_list_form',
),
'type' => MENU_LOCAL_TASK,
'access arguments' => array(
'administer registration codes',
),
'weight' => 100,
);
return $items;
}
function regcode_mailer_admin_list_form() {
$form = array();
$num_codes = db_result(regcode_admin_list_getresource(true));
$form['regcode_mailer_info'] = array(
'#type' => 'markup',
'#value' => t('There are @codes codes selected for mailing. To reselect codes, go back to the List page.', array(
'@codes' => $num_codes,
)),
);
$form['regcode_mailer_subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject'),
);
$form['regcode_mailer_message'] = array(
'#type' => 'textarea',
'#title' => t('Body'),
'#description' => t('Enter the plaintext email to be sent to the user.'),
'#rows' => 15,
);
$form['regcode_mailer_tokens'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['regcode_mailer_tokens']['tokens'] = array(
'#value' => theme('token_help', 'site'),
);
$form['regcode_mailer_emails'] = array(
'#type' => 'textarea',
'#title' => t('Email addresses'),
'#description' => t('List the email addresses to send the registration code to. Separate each email with a new line.'),
'#rows' => 20,
);
$form['regcode_mailer_submit'] = array(
'#type' => 'submit',
'#value' => t('Send registration codes'),
'#submit' => array(
'regcode_mailer_list_action_mail',
),
);
return $form;
}
function regcode_mailer_list_action_mail($form, $form_state) {
$resource = regcode_admin_list_getresource();
$emails = array_filter(explode("\n", $form_state['values']['regcode_mailer_emails']));
array_map('trim', $emails);
foreach ($emails as $email) {
$regcode = db_fetch_object($resource);
if (!$regcode) {
drupal_set_message(t('Ran out of codes, stopped sending before %email', array(
'%email' => $email,
)));
break;
}
$params = array(
'regcode' => $regcode,
'message' => $form_state['values']['message'],
'subject' => $form_state['values']['subject'],
);
$message = drupal_mail('regcode_mailer', 'regcode', $email, language_default(), $params);
drupal_set_message(t("Sent message with code %code to %email", array(
'%code' => $regcode->code,
'%email' => $email,
)));
db_query('INSERT INTO {regcode_mailer} (rid, mailed, recipient) VALUES (%d, NOW(), "%s")', $regcode->rid, trim($email));
}
drupal_goto('admin/user/regcodes/list');
}
function regcode_mailer_regcode_action_gotomail() {
drupal_goto('admin/user/regcodes/mail');
}
function regcode_mailer_mail($key, &$message, $params) {
switch ($key) {
case 'regcode':
$message['subject'] = token_replace($params['subject'], 'regcode', $params['regcode']);
$message['body'][] = token_replace($params['message'], 'regcode', $params['regcode']);
break;
}
}
function regcode_mailer_token_list($type = 'site') {
if (arg(2) === 'regcodes' && arg(3) === 'mail') {
$tokens['user']['regcode'] = t("The next available registration code.");
return $tokens;
}
}
function regcode_mailer_token_values($type, $object = NULL, $options = array()) {
if ($type == 'regcode') {
$tokens['regcode'] = $object->code;
return $tokens;
}
}
function regcode_mailer_regcode_prepareheading(&$heading) {
$sent = array(
array(
'field' => 'mailed',
'data' => t('Sent'),
),
array(
'field' => 'recipient',
'data' => t('Recipient'),
),
);
array_splice($heading, count($heading) - 1, 0, $sent);
}
function regcode_mailer_regcode_preparerow(&$row) {
$res = db_query('SELECT mailed, recipient FROM {regcode_mailer} WHERE rid = %d', $row['rid']);
$dbrow = db_fetch_array($res);
unset($row['id']);
unset($row['is_sent']);
$row['mailed'] = $dbrow['mailed'] ? format_date(strtotime($dbrow['mailed']), 'small') : '-';
$row['recipient'] = $dbrow['recipient'];
}
function regcode_mailer_regcode_filters(&$form) {
$form['filter']['action_mail'] = array(
'#type' => 'submit',
'#value' => t('Mail codes'),
'#submit' => array(
'regcode_admin_list_savefilters',
'regcode_mailer_regcode_action_gotomail',
),
'#weight' => 50,
);
$is_sent_options = array(
'__all__' => t('-- All --'),
'1' => t('Yes'),
'0' => t('No'),
);
$form['filter']['filter_is_sent'] = array(
'#type' => 'select',
'#title' => t('Sent'),
'#options' => $is_sent_options,
'#default_value' => variable_get('regcode_filter_is_sent', '__all__'),
'#weight' => 25,
);
return $form;
}
function regcode_mailer_regcode_query_alter(&$query, $count_only, $conditions) {
$replace = 'LEFT JOIN {regcode_mailer} AS regcode_mailer
ON (regcode.rid = regcode_mailer.rid)
WHERE';
$query = str_replace('WHERE', $replace, $query);
$replace = ',regcode_mailer.is_sent,regcode_mailer.mailed,regcode_mailer.recipient FROM';
$query = str_replace(' FROM', $replace, $query);
$query .= ' GROUP BY regcode.rid';
}