From fe8250ffea8d817587db48079d06f9357419dfa0 Mon Sep 17 00:00:00 2001
From: mrded <mrded@556088.no-reply.drupal.org>
Date: Thu, 12 Sep 2013 11:57:31 +0100
Subject: [PATCH] Add plaintext field

---
 mail_edit.admin.inc | 16 ++++++++++++++++
 mail_edit.alter.inc | 10 ++++++++--
 mail_edit.install   | 17 +++++++++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/mail_edit.admin.inc b/mail_edit.admin.inc
index 07e912b..93884c7 100644
--- a/mail_edit.admin.inc
+++ b/mail_edit.admin.inc
@@ -459,6 +459,22 @@ function mail_edit_template_subform($template) {
       '#default_value' => $template['body'],
       '#rows'          => 15,
     );
+
+    $form['rewrite_plaintext'] = array(
+      '#type' =>'checkbox',
+      '#title' => t('Rewrite plaintext'),
+      '#default_value' => isset($template['rewrite_plaintext']) ? $template['rewrite_plaintext'] : '',
+    );
+
+    $form['plaintext'] = array(
+      '#title'         => t('Plaintext'),
+      '#type'          => 'textarea',
+      '#default_value' => isset($template['plaintext']) ? $template['plaintext'] : '',
+      '#rows'          => 15,
+      '#states' => array(
+        'visible' => array(':input[name="rewrite_plaintext"]' => array("checked" => TRUE)),
+      ),
+    );
   }
 
   if (module_exists('token')) {
diff --git a/mail_edit.alter.inc b/mail_edit.alter.inc
index 437c94c..c94b7ea 100644
--- a/mail_edit.alter.inc
+++ b/mail_edit.alter.inc
@@ -93,15 +93,21 @@ function _mail_edit_mail_alter(array &$message) {
     $options['callback'] = 'user_mail_tokens';
   }
   $message['subject'] = trim(drupal_html_to_text(mail_edit_format($template['subject'], $data, $options)));
-  $args = array($template['body'], $data, $options);
+  $args = array($data, $options);
   if (isset($message['params']['context']['mail_edit'])) {
     $args[] = $message['params']['context']['mail_edit'];
   }
-  $body = call_user_func_array('mail_edit_format', $args);
+  $body = call_user_func_array('mail_edit_format', array_merge(array($template['body']), $args));
   // Remove trailing spaces because these may be interpreted as soft line
   // breaks by the email client.
   $message['body'] = array(preg_replace('/ +(\r?\n)/', '\\1', $body));
 
+  // Add plaintext.
+  if ($template['rewrite_plaintext'] && isset($template['plaintext'])) {
+    $plaintext = call_user_func_array('mail_edit_format', array_merge(array($template['plaintext']), $args));
+    $message['params']['plaintext'] = $plaintext;
+  }
+
   /* Uncomment this line for debugging...
   dpm($message, 'drupal_mail() is disabled in _mail_edit_mail_alter(), this would be sent');
   $message['to'] = '';
diff --git a/mail_edit.install b/mail_edit.install
index dd0d2d8..1ca4824 100644
--- a/mail_edit.install
+++ b/mail_edit.install
@@ -39,6 +39,15 @@ function mail_edit_schema() {
         'type' => 'text',
         'size' => 'normal',
       ),
+      'rewrite_plaintext' => array(
+        'type' => 'int',
+        'not null' => TRUE,
+        'default' => 0,
+      ),
+      'plaintext' => array(
+        'type' => 'text',
+        'size' => 'normal',
+      ),
     ),
     'primary key' => array('id', 'language'),
     'indexes' => array(
@@ -146,3 +155,11 @@ function mail_edit_update_7001() {
   }
   return NULL;
 }
+
+/**
+ * Add 'plaintext' column to 'mail_edit' table.
+ */
+function mail_edit_update_7101() {
+  db_add_field('mail_edit', 'rewrite_plaintext', array('type' => 'int', 'not null' => TRUE, 'default' => 0));
+  db_add_field('mail_edit', 'plaintext', array('type' => 'text', 'size' => 'normal'));
+}
-- 
1.7.12.4 (Apple Git-37)

