You are here

regcode_mailer.module in Registration codes 6

File

regcode_mailer/regcode_mailer.module
View source
<?php

/**
 * Implementation of hook_help().
 */
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;
}

/**
 * Implementation of hook_menu().
 */
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;
}

/**
 * Return the code list page content with(in) the according filter form
 *
 * @return
 *   The settings form.
 */
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;
}

/**
 * Submit action for the form
 */
function regcode_mailer_list_action_mail($form, $form_state) {

  // Grab the list of filtered codes
  $resource = regcode_admin_list_getresource();

  // Iterate the email addresses provided
  $emails = array_filter(explode("\n", $form_state['values']['regcode_mailer_emails']));
  array_map('trim', $emails);
  foreach ($emails as $email) {

    // Grab a code or break
    $regcode = db_fetch_object($resource);
    if (!$regcode) {
      drupal_set_message(t('Ran out of codes, stopped sending before %email', array(
        '%email' => $email,
      )));
      break;
    }

    // Format the email
    $params = array(
      'regcode' => $regcode,
      'message' => $form_state['values']['message'],
      'subject' => $form_state['values']['subject'],
    );

    // Send
    $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,
    )));

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

  // Go back to the list
  drupal_goto('admin/user/regcodes/list');
}

/**
 * Redirect to the mail page
 */
function regcode_mailer_regcode_action_gotomail() {
  drupal_goto('admin/user/regcodes/mail');
}

/**
 * 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) === 'regcodes' && arg(3) === 'mail') {
    $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;
  }
}

/**
 * Implements hook_regcode_prepareheading().
 */
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);
}

/**
 * Implements hook_regcode_preparerow().
 */
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'];
}

/**
 * Implements hook_regcode_filters()
 *
 * Add "Mail these codes" action
 */
function regcode_mailer_regcode_filters(&$form) {

  // Create an export list button
  $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;
}

/**
 * Implements hook_regcode_query_alter().
 */
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';
}

Functions

Namesort descending Description
regcode_mailer_admin_list_form Return the code list page content with(in) the according filter form
regcode_mailer_help Implementation of hook_help().
regcode_mailer_list_action_mail Submit action for the form
regcode_mailer_mail Implements hook_mail()
regcode_mailer_menu Implementation of hook_menu().
regcode_mailer_regcode_action_gotomail Redirect to the mail page
regcode_mailer_regcode_filters Implements hook_regcode_filters()
regcode_mailer_regcode_prepareheading Implements hook_regcode_prepareheading().
regcode_mailer_regcode_preparerow Implements hook_regcode_preparerow().
regcode_mailer_regcode_query_alter Implements hook_regcode_query_alter().
regcode_mailer_token_list Implements hook_token_list().
regcode_mailer_token_values Implements hook_token_values().