You are here

mail_edit.module in Mail Editor 6

Main file for the Mail Editor module.

File

mail_edit.module
View source
<?php

/**
 * @file
 * Main file for the Mail Editor module.
 */

/**
 * Include all Mail Edit include files.
 */
function _mail_edit_include() {
  module_load_include('inc', 'mail_edit', 'mail_edit_user');
  if (module_exists('logintoboggan')) {
    module_load_include('inc', 'mail_edit', 'mail_edit_logintoboggan');
  }
}

/**
 * Implementation of hook_perm().
 */
function mail_edit_perm() {
  return array(
    'administer mail templates',
  );
}

/**
 * Implementation of hook_menu().
 */
function mail_edit_menu() {
  $items['admin/build/mail-edit'] = array(
    'access callback' => 'mail_edit_access',
    'file' => 'mail_edit.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mail_edit_list_form',
    ),
    'description' => 'Edit mails being sent out by Drupal.',
    'title' => 'Mail templates',
  );
  $items['admin/build/mail-edit/%/%'] = array(
    'access callback' => 'mail_edit_access',
    'file' => 'mail_edit.admin.inc',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mail_edit_trans',
      3,
      4,
    ),
    'description' => 'Edit mails being sent out by Drupal.',
    'title' => 'Mail templates',
  );
  return $items;
}

/**
 * Checks the current user's access to Mail Edit's pages.
 *
 * @return bool
 */
function mail_edit_access() {
  return user_access('administer mail templates') && user_access('access administration pages');
}

/**
 * Implementation of hook_theme().
 */
function mail_edit_theme() {
  return array(
    'mail_edit_table' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'mail_edit.admin.inc',
    ),
    'mail_edit_list_filter' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      'file' => 'mail_edit.admin.inc',
    ),
  );
}

/**
 * Implementation of hook_mail_alter().
 */
function mail_edit_mail_alter(&$mail) {
  _mail_edit_include();

  // Find out if this mail id in our registry.
  $query = "SELECT * FROM {mail_edit_registry} where id = '%s'";
  $result = db_query($query, $mail['id']);
  $mail_data = db_fetch_object($result);
  if (!$mail_data) {

    // If we dont have registry record of mail id in question we shouldnt attempt to alter it.
    return;
  }

  // Load mail template if we have altered it.
  $query = "SELECT * FROM {mail_edit} WHERE id = '%s' AND language = '%s'";
  $arg[] = $mail['id'];
  $arg[] = $mail['language']->language;
  $template = db_fetch_object(db_query($query, $arg));

  // Do not try to alter anything if we didn't modify this mail template.
  if ($template) {

    // Fetch our token values.
    $tokens = module_invoke($mail_data->module, 'mail_edit_tokens_value', $mail_data->mailkey, $mail);
    uksort($tokens, '_mail_edit_sort_long_keys_first');
    $result = new stdClass();
    $result->tokens = array_keys($tokens);
    $result->values = array_values($tokens);

    // Perform replacement of tokens.
    $mail['subject'] = str_replace($result->tokens, $result->values, $template->subject);
    unset($mail['body']);
    $mail['body'][] = str_replace($result->tokens, $result->values, $template->body);
  }
}

/*
 * mail_edit needs the tokens in order of longest keys first
 * (so [user-raw] comes before [user], for example),
 * to prevent premature interpolation.
 */
function _mail_edit_sort_long_keys_first($a, $b) {
  return drupal_strlen($b) - drupal_strlen($a);
}

Functions

Namesort descending Description
mail_edit_access Checks the current user's access to Mail Edit's pages.
mail_edit_mail_alter Implementation of hook_mail_alter().
mail_edit_menu Implementation of hook_menu().
mail_edit_perm Implementation of hook_perm().
mail_edit_theme Implementation of hook_theme().
_mail_edit_include Include all Mail Edit include files.
_mail_edit_sort_long_keys_first