From 4fb6448f78292f6bafa10fd912a9e7251ceacfe0 Mon Sep 17 00:00:00 2001
From: Alexandr Kirienko <alex@kirienko.net>
Date: Sat, 21 Nov 2015 16:52:32 +0300
Subject: [PATCH] #2009772: Add support for editing mail produced by core's
 Contact form

---
 mail_edit.module              |  3 ++
 mail_edit.tokens.inc          | 99 +++++++++++++++++++++++++++++++++++++++++++
 modules/mail_edit.contact.inc | 69 ++++++++++++++++++++++++++++++
 3 files changed, 171 insertions(+)
 create mode 100644 mail_edit.tokens.inc
 create mode 100644 modules/mail_edit.contact.inc

diff --git a/mail_edit.module b/mail_edit.module
index f5ce468..42f7b7c 100644
--- a/mail_edit.module
+++ b/mail_edit.module
@@ -177,6 +177,9 @@ function mail_edit_modules_uninstalled(array $modules) {
  */
 function _mail_edit_include() {
   _mail_edit_module_load_include('modules/mail_edit', 'user.inc');
+  if (module_exists('contact')) {
+    _mail_edit_module_load_include('modules/mail_edit', 'contact.inc');
+  }
   if (module_exists('logintoboggan')) {
     _mail_edit_module_load_include('modules/mail_edit', 'logintoboggan.inc');
   }
diff --git a/mail_edit.tokens.inc b/mail_edit.tokens.inc
new file mode 100644
index 0000000..43238ce
--- /dev/null
+++ b/mail_edit.tokens.inc
@@ -0,0 +1,99 @@
+<?php
+
+/**
+ * @file
+ * Token callbacks for the Contact core module..
+ */
+
+/**
+ * Implements hook_token_info().
+ *
+ */
+function mail_edit_token_info() {
+  $types['contact'] = array(
+    'name' => t('Contact'),
+    'description' => t('Tokens related to contact form.'),
+  );
+
+  $contact['subject'] = array(
+    'name' => t('Subject'),
+    'description' => t('Subject field content.'),
+  );
+  $contact['message'] = array(
+    'name' => t('Message'),
+    'description' => t('Message field content.'),
+  );
+  $contact['category'] = array(
+    'name' => t('Category'),
+    'description' => t('Category.'),
+  );
+  $contact['category-reply'] = array(
+    'name' => t('Category reply'),
+    'description' => t('Category reply.'),
+  );
+
+  $contact['sender'] = array(
+    'name' => t('Sender'),
+    'description' => t('Sender user.'),
+    'type' => 'user',
+  );
+  $contact['recipient'] = array(
+    'name' => t('Recipient'),
+    'description' => t('Recipient uUser.'),
+    'type' => 'user',
+  );
+
+  return array(
+    'types' => $types,
+    'tokens' => array('contact' => $contact),
+  );
+}
+
+/**
+ * Implements hook_tokens().
+ *
+ */
+function mail_edit_tokens($type, $tokens, array $data = array(), array $options = array()) {
+  $replacements = array();
+
+  $url_options = array('absolute' => TRUE);
+  if (isset($options['language'])) {
+    $url_options['language'] = $options['language'];
+  }
+
+  if ($type == 'contact') {
+    foreach ($tokens as $name => $original) {
+      switch ($name) {
+        case 'subject':
+          $replacements[$original] = isset($data['subject']) ? $data['subject'] : '';
+          break;
+
+        case 'message':
+          $replacements[$original] = isset($data['message']) ? $data['message'] : '';
+          break;
+
+        case 'category':
+          $replacements[$original] = isset($data['category']['category']) ? $data['category']['category'] : '';
+          break;
+
+        case 'category-reply':
+          $replacements[$original] = isset($data['category']['reply']) ? $data['category']['reply'] : '';
+          break;
+
+        case 'form-url':
+          $replacements[$original] = url($_GET['q'], $url_options);
+          break;
+      }
+    }
+
+    if (($sender_tokens = token_find_with_prefix($tokens, 'sender'))) {
+      $replacements += token_generate('user', $sender_tokens, array('user' => $data['sender']), $options);
+    }
+
+    if (($recipient_tokens = token_find_with_prefix($tokens, 'recipient'))) {
+      $replacements += token_generate('user', $recipient_tokens, array('user' => $data['recipient']), $options);
+    }
+  }
+
+  return $replacements;
+}
diff --git a/modules/mail_edit.contact.inc b/modules/mail_edit.contact.inc
new file mode 100644
index 0000000..c012d77
--- /dev/null
+++ b/modules/mail_edit.contact.inc
@@ -0,0 +1,69 @@
+<?php
+
+/**
+ * @file
+ * A sample implementation of Mail Editor hooks for Contact core module.
+ */
+
+/**
+ * Implements hook_mailkeys().
+ *
+ * @return array
+ */
+function contact_mailkeys() {
+  return array(
+    'page_mail'         => t('Contact form message'),
+    'page_copy'         => t('Contact form message copy'),
+    'page_autoreply'    => t('Contact form message autoreply'),
+    'user_mail'         => t('Contact user form message'),
+    'user_copy'         => t('Contact user form message copy'),
+  );
+}
+
+/**
+ * Implements hook_mail_edit_text().
+ *
+ * @param string $mailkey
+ * @param object $language
+ *
+ * @return array
+ */
+function contact_mail_edit_text($mailkey, $language) {
+  $return = array();
+
+  switch ($mailkey) {
+    case 'page_mail':
+    case 'page_copy':
+      $return['subject'] = '[contact:category] [contact:subject]';
+      $return['body'] = '[contact:sender:name] ([contact:sender:url:absolute]) sent a message using the contact form at [contact:form-url].
+[contact:message]';
+      break;
+
+    case 'page_autoreply':
+      $return['subject'] = '[contact:category] [contact:subject]';
+      $return['body'] = '[contact:category-reply]';
+      break;
+
+    case 'user_mail':
+    case 'user_copy':
+      $return['subject'] = '[site:name] [contact:subject]';
+      $return['body'] = 'Hello [contact:recipient:name]
+[contact:sender:name] ([contact:sender:url:absolute]) has sent you a message via your contact form at [contact:form-url].
+If you don\'t want to receive such e-mails, you can change your settings at [contact:recipient:edit-url]
+Message:
+[contact:message]';
+      break;
+  }
+  return $return;
+}
+
+/**
+ * Implements hook_mail_edit_token_types().
+ *
+ * @param string $mailkey
+ *
+ * @return array
+ */
+function contact_mail_edit_token_types($mailkey) {
+  return array('contact');
+}
-- 
1.9.1

