From 57f21874a4bdccf89ce9b11203ff3d44de6692b5 Mon Sep 17 00:00:00 2001
From: Jimmy Berry <jimmy@boombatower.com>
Date: Mon, 19 Dec 2011 17:03:02 -0600
Subject: Issue #1376674: Implement invite-message token.

---
 invite.admin.inc  |    6 ++++++
 invite.module     |   12 ++++++++++--
 invite.tokens.inc |    6 ++++++
 3 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/invite.admin.inc b/invite.admin.inc
index 31afe27..a75350b 100644
--- a/invite.admin.inc
+++ b/invite.admin.inc
@@ -125,6 +125,12 @@ function invite_settings() {
     '#required' => TRUE,
     '#description' => t('Use the syntax [token] if you want to insert a replacement pattern.'),
   );
+  $form['email']['invite_mail_template_editable'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Editable mail template'),
+    '#description' => t('Choose whether users should be able to customize the mail template or enter a message that can then be embedded as a token.'),
+    '#default_value' => variable_get('invite_mail_template_editable', TRUE),
+  );
   $form['email']['token_help'] = array(
     '#title' => t('Replacement patterns'),
     '#type' => 'fieldset',
diff --git a/invite.module b/invite.module
index e580ede..d8c91f5 100644
--- a/invite.module
+++ b/invite.module
@@ -1093,10 +1093,11 @@ function invite_page_form($remaining_invites, $edit = array()) {
     );
   }
 
+  $default_message = variable_get('invite_mail_template_editable', TRUE) ? token_replace(_invite_get_mail_template(), invite_token_data()) : '';
   $form['message'] = array(
     '#title' => t('Message'),
     '#type' => 'textarea',
-    '#default_value' => ($edit && !empty($edit['data']['message'])) ? $edit['data']['message'] : token_replace(_invite_get_mail_template(), invite_token_data()),
+    '#default_value' => ($edit && !empty($edit['data']['message'])) ? $edit['data']['message'] : $default_message,
     '#description' => t('This is the message the invitation email will contain. The <i>[invite:join-link]</i> token in the message will be replaced by the registration link the recipient can use to join. Make sure this token is always included in the message.'),
   );
 
@@ -1266,7 +1267,14 @@ function invite_form_submit($form, &$form_state) {
       $invite->email = $email;
     }
 
-    $invite->data = array('subject' => trim($form_state['values']['subject']), 'message' => token_replace($form_state['values']['message'], invite_token_data($invite)));
+    if (variable_get('invite_mail_template_editable', TRUE)) {
+      $message = $form_state['values']['message'];
+    }
+    else {
+      $message = _invite_get_mail_template();
+      $invite->message = $form_state['values']['message'];
+    }
+    $invite->data = array('subject' => trim($form_state['values']['subject']), 'message' => token_replace($message, invite_token_data($invite)));
 
     if (invite_send($invite, !$show_only)) {
       $num_succeeded++;
diff --git a/invite.tokens.inc b/invite.tokens.inc
index 973eddc..3bb5089 100644
--- a/invite.tokens.inc
+++ b/invite.tokens.inc
@@ -28,6 +28,12 @@ function invite_tokens($type, $tokens, array $data = array(), array $options = a
           }
           break;
 
+        case 'invite-message':
+          if (!empty($invite->message)) {
+            $replacements[$original] = $invite->message;
+          }
+        break;
+
         case 'join-link':
           if (!empty($invite->reg_code)) {
             $replacements[$original] = url('invite/accept/' . $invite->reg_code, array('absolute' => TRUE));
-- 
1.7.7.3

