You are here

regcode_mailer.module in Registration codes 6.2

File

regcode_mailer/regcode_mailer.module
View source
<?php

/**
 * Implementation of hook_action_info().
 */
function regcode_mailer_action_info() {
  return array(
    'regcode_mailer_mail_action' => array(
      'type' => 'regcode',
      'description' => t('Mail codes'),
      'configurable' => TRUE,
      'hooks' => array(
        'any' => TRUE,
      ),
    ),
  );
}

/**
 * Form for the mail action.
 */
function regcode_mailer_mail_action_form() {
  $form = array();
  $form['regcode_mailer_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#required' => TRUE,
  );
  $form['regcode_mailer_message'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#description' => t('Enter the plaintext email to be sent to the user.'),
    '#rows' => 8,
    '#required' => TRUE,
  );
  $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' => 8,
    '#required' => TRUE,
  );
  $form['regcode_mailer_tags'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Tags'),
    '#options' => regcode_get_vocab_terms(),
    '#description' => t('You may assign tags to track which codes have been sent.'),
  );
  return $form;
}

/**
 * Validate action for the form
 *
 * We need to ensure that they have actually used [regcode] somewhere.
 */
function regcode_mailer_mail_action_validate($form, $form_state) {
  if (strpos($form_state['values']['regcode_mailer_message'], '[regcode]') === FALSE) {
    form_set_error('regcode_mailer_message', t('Cannot send message without registration code - you should insert the [regcode] token in the message body.'));
  }
}

/**
 * Submit action for the form
 */
function regcode_mailer_mail_action_submit($form, $form_state) {
  $emails = array_filter(explode("\n", $form_state['values']['regcode_mailer_emails']));
  array_map('trim', $emails);
  return array(
    'subject' => $form_state['values']['regcode_mailer_subject'],
    'message' => $form_state['values']['regcode_mailer_message'],
    'emails' => $emails,
    'terms' => array_filter($form_state['values']['regcode_mailer_tags']),
  );
}

/**
 * Implements the mail action
 */
function regcode_mailer_mail_action(&$object, &$context = array()) {
  static $i = 0;

  // Check we have enough emails for this action
  if (!isset($context['emails'][$i])) {
    drupal_set_message(t('Ran out of emails, unable to send code (%code).', array(
      '%code' => $object->code,
    )));
    return FALSE;
  }

  // Params for the email
  $params = array(
    'regcode' => $object,
    'message' => $context['message'],
    'subject' => $context['subject'],
  );

  // Send
  $email = $context['emails'][$i];
  $message = drupal_mail('regcode_mailer', 'regcode', $email, language_default(), $params);
  drupal_set_message(t("Sent message with code %code to %email", array(
    '%code' => $object->code,
    '%email' => $email,
  )));

  // Update the database
  db_query('INSERT INTO {regcode_mailer} (rid, mailed, recipient) VALUES (%d, %d, "%s")', $object->rid, time(), $email);

  // Tag the code
  foreach ($context['terms'] as $term_id) {
    db_query('REPLACE INTO {regcode_term} (rid, tid) VALUES (%d, %d)', $object->rid, $term_id);
  }
  $i++;
}

/**
 * Implements hook_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;
  }
}

/**
 * Implements hook_token_list().
 */
function regcode_mailer_token_list($type = 'site') {

  // It only makes sense to provide this token when using the regcode mailer
  if (arg(2) === 'regcode') {
    $tokens['user']['regcode'] = t('The next available registration code.');
    return $tokens;
  }
}

/**
 * Implements hook_token_values().
 */
function regcode_mailer_token_values($type, $object = NULL, $options = array()) {
  if ($type == 'regcode') {
    $tokens['regcode'] = $object->code;
    return $tokens;
  }
}

Functions

Namesort descending Description
regcode_mailer_action_info Implementation of hook_action_info().
regcode_mailer_mail Implements hook_mail()
regcode_mailer_mail_action Implements the mail action
regcode_mailer_mail_action_form Form for the mail action.
regcode_mailer_mail_action_submit Submit action for the form
regcode_mailer_mail_action_validate Validate action for the form
regcode_mailer_token_list Implements hook_token_list().
regcode_mailer_token_values Implements hook_token_values().