You are here

regcode_mailer.module in Registration codes 7

Main code functionality and hooks of regcode_mailer module.

File

regcode_mailer/regcode_mailer.module
View source
<?php

/**
 * @file
 * Main code functionality and hooks of regcode_mailer module.
 */

/**
 * Implements hook_action_info().
 */
function regcode_mailer_action_info() {
  return array(
    'regcode_mailer_mail_action' => array(
      'type' => 'regcode',
      'label' => t('Mail codes'),
      'configurable' => TRUE,
      'triggers' => 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']['token_tree'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array(
      'regcode',
    ),
  );
  $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) {
  $message = $form_state['values']['regcode_mailer_message'];
  if (strpos($message, '[regcode:code]') === FALSE && strpos($message, '[regcode:regurl]') === FALSE) {
    form_set_error('regcode_mailer_message', t('Cannot send message without registration code - you should insert either [regcode:code] or [regcode:regurl] 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];
  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_insert('regcode_mailer')
    ->fields(array(
    'rid' => $object->rid,
    'mailed' => REQUEST_TIME,
    'recipient' => $email,
  ))
    ->execute();

  // Tag the code.
  foreach ($context['terms'] as $term_id) {
    $record = array(
      'rid' => $object->rid,
      'tid' => $term_id,
    );
    db_merge('regcode_term')
      ->key($record)
      ->fields($record)
      ->execute();
  }
  $i++;
}

/**
 * Implements hook_mail().
 */
function regcode_mailer_mail($key, &$message, $params) {
  switch ($key) {
    case 'regcode':
      $token_data = array(
        'regcode' => $params['regcode'],
      );
      $message['subject'] = token_replace($params['subject'], $token_data);
      $message['body'][] = token_replace($params['message'], $token_data);
      break;
  }
}

Functions

Namesort descending Description
regcode_mailer_action_info Implements 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.