Skip to content
mail_edit.module 6.97 KiB
Newer Older
<?php

function mail_edit_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'access' => user_access('administer site configuration'),
      'callback' => 'mail_edit_overview',
      'description' => t('Edit mails being sent out by Drupal.'),
      'path' => 'admin/build/mail_edit',
      'title' => t('Mail templates'),
    );
  }
  return $items;
}

/**
 * Implementation of hook_mail_alter().
 */
function mail_edit_mail_alter(&$mailkey, &$to, &$subject, &$body, &$from, &$headers) {
chx's avatar
chx committed
  static $stored_variables, $alters;
  if (!isset($stored_variables)) {
    if (!empty($GLOBALS['form_values'])) {
      foreach ($GLOBALS['form_values'] as $key => $value) {
        $stored_variables["!$key"] = $value;
      }
    }
    $stored_variables = array(
      '!site' => variable_get('site_name', 'Drupal'),
      '!uri' => $base_url,
      '!uri_brief' => substr($base_url, strlen('http://')),
      '!date' => format_date(time()),
      '!login_uri' => url('user', NULL, NULL, TRUE),
    );
chx's avatar
chx committed
  if (!isset($alters[$mailkey])) {
    $sql = "SELECT * FROM {mail_edit} m";
    if (module_exists('privatemsg')) {
      $sql .= ' INNER JOIN {privatemsg_mail_edit} p ON m.mailkey = p.mailkey';
    }
    $sql .= " WHERE m.mailkey = '%s'";
    $alters[$mailkey] = db_fetch_object(db_query($sql, $mailkey));
  }
  if (!empty($alters[$mailkey])) {
    $recipient = user_load(array('mail' => $to));
    $is_privatemsg = !empty($alter->type) && $recipient;
chx's avatar
chx committed
    $variables = $stored_variables;
    $variables['!sender_name'] = $sender->name;
    $variables['!sender_name'] = url("user/$sender->uid", NULL, NULL, TRUE);
    if (!empty($sender->contact)) {
      $variables['!sender_page_contact'] = url("user/$sender->uid/contact", NULL, NULL, TRUE);
    }
    if ($recipient) {
chx's avatar
chx committed
      $variables['!recipient_name'] = $recipient->name;
      $variables['!recipient_name'] = url("user/$recipient->uid", NULL, NULL, TRUE);
      $variables['!edit_uri'] = url('user/'. $account->uid .'/edit', NULL, NULL, TRUE);
      if (substr($mailkey, 0, 5) == 'user-') {
        $variables['!username'] = $recipient->name;
        $variables['!login_url'] = user_pass_reset_url($recipient);
        $variables['!mailto'] = $recipient->mail;
chx's avatar
chx committed
    foreach (module_implements('mail_edit_variables') as $module) {
      $function = $module .'_mail_edit_variables';
      $function($variables, $mailkey, $sender, $recipient);
    }
    if ($alter->subject) {
      $subject = strtr($alter->subject, $variables);
    }
    if ($alter->body) {
      $body = strtr($alter->body, $variables);
    }
    if ($is_privatemsg) {
      $to = NULL;
      module_invoke('privatemsg', 'send_privatemsg', $recipient, $subject, $body, FILTER_FORMAT_DEFAULT, 0, $alter->type);
    }
  }
}

/**
 * Menu callback; administrative mail editing overview.
 */
function mail_edit_overview($mailkey = '') {
  $keys = array();
  if ($cache = cache_get('mail_edit_overview')) {
    $keys = unserialize($cache->data);
  }
  else {
    foreach (module_list() as $module) {
      $file = file_get_contents(drupal_get_path('module', $module) ."/$module.module");
      preg_match_all('/drupal_mail\((.)(.+)\1,/U', $file, $matches);
      $keys = array_merge($keys, $matches[2]);
    }
chx's avatar
chx committed
    $keys = array_merge($keys, module_invoke_all('mailkeys'));
    cache_set('mail_edit_overview', 'cache', serialize($keys));
  }
  if ($mailkey && (array_search($mailkey, $keys) !== FALSE)) {
    return drupal_get_form('mail_edit_form', $mailkey);
  }
  sort($keys);

  $header = array(t('Key'), t('Description'));
  $rows = array();
  foreach ($keys as $key) {
    $rows[$key] = array(
      l($key, 'admin/build/mail_edit/'. $key),
      t('<em>No description has been set.</em>'),
    );
  }
  $result = db_query('SELECT mailkey, description FROM {mail_edit}');
  while ($d = db_fetch_object($result)) {
    $rows[$d->mailkey][1] = $d->description;
  }

  $output = theme('table', $header, $rows, array('style' => 'width:98%;'));

  return $output;
}

function mail_edit_form($mailkey) {
  drupal_set_title(t('Settings for %mailkey', array('%mailkey' => $mailkey)));
  if ($defaults = db_fetch_array(db_query("SELECT * FROM {mail_edit} WHERE mailkey = '%s'", $mailkey))) {
    $insert = FALSE;
  }
  else {
    $insert = TRUE;
    $defaults = array('description' => '', 'subject' => '', 'body' => '');
  }
  $form['insert'] = array('#type' => 'value', '#value' => $insert);
  $form['mailkey'] = array('#type' => 'value', '#value' => $mailkey);
chx's avatar
chx committed
  $form['_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#weight' => -40,
  );
  $form['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Message description'),
    '#default_value' => $defaults['description'],
    '#description' => t("The description of this message (for admins only; users don't see this.)"),
    '#weight' => 0,
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => $defaults['subject'],
chx's avatar
chx committed
    '#description' => t('The subject of the mail or the private message sent instead of the mail.'),
chx's avatar
chx committed
    '#weight' => 10,
  );
  $form['body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#default_value' => $defaults['body'],
chx's avatar
chx committed
    '#description' => t('The body of the mail or the private message sent instead of the mail.'),
chx's avatar
chx committed
    '#weight' => 20,
chx's avatar
chx committed
  $form['help'] = array(
    '#theme' => 'mail_edit_variables',
    '#variables' => array(
      '!recipient_name' => t('Name of the recipient.'),
      '!recipient_page' => ('The user page of the recipient.'),
      '!sender_page' => t('The user page of the sender.'),
      '!sender_page_contact' => t('The contact page of the sender.'),
    ),
    '#weight' => 30,
chx's avatar
chx committed
  if (substr($mailkey, 0, 7) == 'contact') {
    $form['help']['#variables'] += array(
      '!message' => t('The contact message itself.'),
      '!subject' => t('The subject of the contact message.'),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
chx's avatar
chx committed
    '#weight' => 40,
  );
  return $form;
}

function mail_edit_form_submit($form_id, $form_values) {
  $args = array($form_values['description'], $form_values['subject'], $form_values['body'], $form_values['mailkey']);
  if ($form_values['insert']) {
    db_query("INSERT INTO {mail_edit} (description, subject, body, mailkey) VALUES ('%s', '%s', '%s', '%s')", $args);
  }
  else {
    db_query("UPDATE {mail_edit} SET description = '%s', subject = '%s', body = '%s' WHERE mailkey = '%s'", $args);
  }
  return 'admin/build/mail_edit';
}

chx's avatar
chx committed
function theme_mail_edit_variables($element) {
  $output = "<p>Usable variables are:</p>";
  $output .= "<dl>\n";
  foreach($element['#variables'] as $dt => $dd) {
    $output .= "<dt>$dt</dt><dd>$dd</dd>\n";
  }
  $output .= "</dl>\n";
  return $output;
}

chx's avatar
chx committed
/**
 * We provide some of the mailkeys for user module which are not catched by the
 * regexp in mail_edit_overview.
 */
function mail_edit_mailkeys() {
  return array('user-register-notify', 'user-register-welcome');
}