From ac37af4d442cd60d3db4de561c6006e37e92127e Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 7 Aug 2017 16:44:25 +0100
Subject: [PATCH] FIX #5799 sendmail : use model and signature in raw body

---
 modules/sendmail/js/functions.js              | 25 ++++++++++++++-----
 modules/sendmail/lang/en.php                  |  2 +-
 modules/sendmail/lang/fr.php                  |  2 +-
 modules/sendmail/mail_form.php                |  4 +--
 modules/sendmail/sendmail_to_contact_ajax.php | 12 +--------
 .../templates_ajax_content_for_mails.php      |  6 +++++
 .../templates/xml/IVS/validation_rules.xml    |  1 +
 7 files changed, 31 insertions(+), 21 deletions(-)

diff --git a/modules/sendmail/js/functions.js b/modules/sendmail/js/functions.js
index 61b0fc7c021..777c3b0f310 100755
--- a/modules/sendmail/js/functions.js
+++ b/modules/sendmail/js/functions.js
@@ -17,14 +17,20 @@ function addTemplateToEmail(templateMails, path){
     {
         method      :'post',
         parameters  :{
-                        templateId : templateMails
+                        templateId : templateMails,
+                        mode : mode
                      },
         onSuccess   :function(answer){
             eval("response = " + answer.responseText);
             if (response.status == 0) {
                 var strContent = response.content;
-                var strContentReplace = strContent.replace(/\\n/g, '');
-                tinyMCE.execCommand('mceSetContent',false,strContentReplace);
+                if(mode == 'html'){
+                    var strContentReplace = strContent.replace(/\\n/g, '');
+                    tinyMCE.execCommand('mceSetContent', false, strContentReplace);
+                } else {
+                    var strContentReplace = strContent.replace(/\\n/g, '\n');
+                    $j("textarea#body_from_raw").html(strContentReplace);
+                }
             } 
         }
     });
@@ -35,13 +41,20 @@ function changeSignature(selected, mailSignaturesJS){
     var body = $('body_from_html_ifr').contentWindow.document.getElementById("tinymce");
     var customTag = body.getElementsByTagName("mailSignature");
 
-    if (customTag.length == 0) {
-        body.innerHTML += "<mailSignature>toto</mailSignature>";
+    if (mode == 'html' && customTag.length == 0) {
+        body.innerHTML += "<mailSignature>t</mailSignature>";
         customTag = body.getElementsByTagName("mailSignature");
     }
 
     if (nb >= 0) {
-        customTag[0].innerHTML = mailSignaturesJS[nb].signature;
+        var strContent = mailSignaturesJS[nb].signature;
+        if(mode == 'html'){
+            customTag[0].innerHTML = strContent;
+        } else {
+            var text = $j(strContent).text();
+            $j("textarea#body_from_raw").append(text);
+        }
+        
     } else {
         customTag[0].innerHTML = "";
     }
diff --git a/modules/sendmail/lang/en.php b/modules/sendmail/lang/en.php
index cdd186177e8..8a8d6221ec4 100755
--- a/modules/sendmail/lang/en.php
+++ b/modules/sendmail/lang/en.php
@@ -125,7 +125,7 @@ if (!defined("_EMAIL_REMOVED"))
     define("_EMAIL_REMOVED", "Email removed");
 
 if (!defined('_Label_ADD_TEMPLATE_MAIL'))
-    define('_Label_ADD_TEMPLATE_MAIL', 'Model of email');
+    define('_Label_ADD_TEMPLATE_MAIL', 'Model : ');
 if (!defined('_ADD_TEMPLATE_MAIL'))
     define('_ADD_TEMPLATE_MAIL', 'Choose a template');
 
diff --git a/modules/sendmail/lang/fr.php b/modules/sendmail/lang/fr.php
index 4c3b8021702..4c817beced2 100755
--- a/modules/sendmail/lang/fr.php
+++ b/modules/sendmail/lang/fr.php
@@ -127,7 +127,7 @@ if (!defined("_EMAIL_REMOVED"))
 
 
 if (!defined("_Label_ADD_TEMPLATE_MAIL"))
-    define("_Label_ADD_TEMPLATE_MAIL", "Modèle de courriel");
+    define("_Label_ADD_TEMPLATE_MAIL", "Modèle : ");
 if (!defined("_ADD_TEMPLATE_MAIL"))
     define("_ADD_TEMPLATE_MAIL", "Sélectionnez le modèle désiré");
 
diff --git a/modules/sendmail/mail_form.php b/modules/sendmail/mail_form.php
index ee38fd1f444..673bf506bc0 100755
--- a/modules/sendmail/mail_form.php
+++ b/modules/sendmail/mail_form.php
@@ -420,7 +420,7 @@ if ($mode == 'add') {
         $content .= "<option value='" . $result->template_id ."'>" . $result->template_label . "</option>";
     }
     $content .= '</select>';
-    $content .= '<label style="margin-left: 15%;padding-right:10px">' . 'Signature de mail' . '</label>';
+    $content .= '<label style="margin-left: 15%;padding-right:10px">' . 'Signature : ' . '</label>';
     $emailSignaturesClass = new EmailSignatures();
 
     $mailSignatures = $emailSignaturesClass->getForCurrentUser();
@@ -795,7 +795,7 @@ if ($mode == 'add') {
                 $content .= "<option value='" . $result->template_id ."'>" . $result->template_label . "</option>";
             }
             $content .='</select>';
-            $content .= '<label style="margin-left: 15%;padding-right:10px">' . 'Signature de mail' . '</label>';
+            $content .= '<label style="margin-left: 15%;padding-right:10px">' . 'Signature : ' . '</label>';
             $emailSignaturesClass = new EmailSignatures();
 
             $mailSignatures = $emailSignaturesClass->getForCurrentUser();
diff --git a/modules/sendmail/sendmail_to_contact_ajax.php b/modules/sendmail/sendmail_to_contact_ajax.php
index 6cfd9a82846..a938fe3686d 100755
--- a/modules/sendmail/sendmail_to_contact_ajax.php
+++ b/modules/sendmail/sendmail_to_contact_ajax.php
@@ -1,19 +1,9 @@
 <?php
-require_once('core'.DIRECTORY_SEPARATOR.'class'.DIRECTORY_SEPARATOR.'class_request.php');
-// if($_POST['status'] != '_NOSTATUS_'){
-// $db = new Database();
-
-// $db->query("UPDATE res_letterbox SET status = ? WHERE res_id = ?", array($_POST['status'],$_REQUEST['identifier']));
 
-// }
+require_once('core'.DIRECTORY_SEPARATOR.'class'.DIRECTORY_SEPARATOR.'class_request.php');
 
 $db = new Database();
 
 $db->query("UPDATE mlb_coll_ext SET sve_start_date = CURRENT_TIMESTAMP WHERE res_id = ?", array($_REQUEST['identifier']));
 
-// header('Location: ' . $_SESSION['config']['businessappurl']
-//                     . 'index.php?page=view_baskets&module=basket'
-//                 );
 ?>
-
-
diff --git a/modules/templates/templates_ajax_content_for_mails.php b/modules/templates/templates_ajax_content_for_mails.php
index 197c43615ae..bed42750e24 100755
--- a/modules/templates/templates_ajax_content_for_mails.php
+++ b/modules/templates/templates_ajax_content_for_mails.php
@@ -45,5 +45,11 @@ $template->template_content = str_replace("\r", "\n", $template->template_conten
 $template->template_content = str_replace("\n", "\\n ", $template->template_content);
 $template->template_content = str_replace("''", "'", $template->template_content);
 
+if($_REQUEST['mode'] == 'raw'){
+    $template->template_content = str_replace("<br>", "\\n", $template->template_content);
+    $template->template_content = str_replace("<br />", "\\n", $template->template_content);
+    $template->template_content = strip_tags($template->template_content);
+}
+
 echo "{status : 0, content : '" . addslashes($template->template_content) . "'}";
 exit();
\ No newline at end of file
diff --git a/modules/templates/xml/IVS/validation_rules.xml b/modules/templates/xml/IVS/validation_rules.xml
index 1d3cfb99aa0..c4736847257 100755
--- a/modules/templates/xml/IVS/validation_rules.xml
+++ b/modules/templates/xml/IVS/validation_rules.xml
@@ -27,6 +27,7 @@
 		<parameter name="module" type="identifier" />
 		<parameter name="id" type="integer" />
 		<parameter name="templateId" type="integer" />
+        <parameter name="mode" type="string" />
 	</validationRule>
 
 	<validationRule name="templates_ajax_content_for_notes" extends="standardForm" mode="error">
-- 
GitLab