From 5b7a8e9c90f82b4abe7c0a4dff84603b16f5884f Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Sat, 17 Jun 2017 10:17:36 +0200
Subject: [PATCH] Fix missing signature when sending email from user card

---
 htdocs/commande/card.php                  | 25 ++++++++-----
 htdocs/core/class/html.formmail.class.php |  6 ++--
 htdocs/langs/en_US/other.lang             |  1 +
 htdocs/user/card.php                      | 43 ++++-------------------
 4 files changed, 28 insertions(+), 47 deletions(-)

diff --git a/htdocs/commande/card.php b/htdocs/commande/card.php
index 83135a8c0f5..f837119730c 100644
--- a/htdocs/commande/card.php
+++ b/htdocs/commande/card.php
@@ -2621,6 +2621,19 @@ if ($action == 'create' && $user->rights->commande->creer)
 				$outputlangs->load('commercial');
 			}
 
+			// Show email form
+
+			// By default if $action=='presend'
+			$titreform='SendOrderByMail';
+			$topicmail='';
+			if (empty($object->ref_client)) {
+			    $topicmail = $outputlangs->trans('SendOrderRef', '__ORDERREF__');
+			} else if (! empty($object->ref_client)) {
+			    $topicmail = $outputlangs->trans('SendOrderRef', '__ORDERREF__ (__REFCLIENT__)');
+			}
+			$action='send';
+			$modelmail='order_send';
+
 			// Build document if it not exists
 			if (! $file || ! is_readable($file)) {
 				$result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
@@ -2635,7 +2648,7 @@ if ($action == 'create' && $user->rights->commande->creer)
 			print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
 			print '<div class="clearboth"></div>';
 			print '<br>';
-			print load_fiche_titre($langs->trans('SendOrderByMail'));
+			print load_fiche_titre($langs->trans($titreform));
 
 			dol_fiche_head('');
 
@@ -2662,11 +2675,7 @@ if ($action == 'create' && $user->rights->commande->creer)
 			$formmail->withto = GETPOST('sendto') ? GETPOST('sendto') : $liste;
 			$formmail->withtocc = $liste;
 			$formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
-			if (empty($object->ref_client)) {
-				$formmail->withtopic = $outputlangs->trans('SendOrderRef', '__ORDERREF__');
-			} else if (! empty($object->ref_client)) {
-				$formmail->withtopic = $outputlangs->trans('SendOrderRef', '__ORDERREF__ (__REFCLIENT__)');
-			}
+			$formmail->withtopic = $topicmail;
 			$formmail->withfile = 2;
 			$formmail->withbody = 1;
 			$formmail->withdeliveryreceipt = 1;
@@ -2696,8 +2705,8 @@ if ($action == 'create' && $user->rights->commande->creer)
 			}
 
 			// Tableau des parametres complementaires
-			$formmail->param['action'] = 'send';
-			$formmail->param['models'] = 'order_send';
+			$formmail->param['action'] = $action;
+			$formmail->param['models'] = $modelmail;
 			$formmail->param['models_id']=GETPOST('modelmailselected','int');
 			$formmail->param['orderid'] = $object->id;
 			$formmail->param['returnurl'] = $_SERVER["PHP_SELF"] . '?id=' . $object->id;
diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php
index 860d3fcfb1c..d59ac6c25af 100644
--- a/htdocs/core/class/html.formmail.class.php
+++ b/htdocs/core/class/html.formmail.class.php
@@ -288,14 +288,15 @@ class FormMail extends Form
 				$outputlangs->load('other');
 			}
 
-        	// Get message template
+        	// Get message template for $this->param["models"] into c_email_templates
 			$model_id=0;
         	if (array_key_exists('models_id',$this->param))
         	{
         		$model_id=$this->param["models_id"];
         	}
         	$arraydefaultmessage=$this->getEMailTemplate($this->db, $this->param["models"], $user, $outputlangs, $model_id);
-			//var_dump($arraydefaultmessage);
+			//var_dump($this->param["models"]);
+        	//var_dump($arraydefaultmessage);
 
         	$out.= "\n".'<!-- Begin form mail --><div id="mailformdiv"></div>'."\n";
         	if ($this->withform == 1)
@@ -876,6 +877,7 @@ class FormMail extends Form
 	        	elseif ($type_template=='shipping_send')			{ $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendShipping"); }
 	        	elseif ($type_template=='fichinter_send')			{ $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendFichInter"); }
 	        	elseif ($type_template=='thirdparty')				{ $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentThirdparty"); }
+	        	elseif ($type_template=='user')				        { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentUser"); }
 
 	        	$ret['label']='default';
 	        	$ret['topic']='';
diff --git a/htdocs/langs/en_US/other.lang b/htdocs/langs/en_US/other.lang
index a14bfffffb6..b151614ce3c 100644
--- a/htdocs/langs/en_US/other.lang
+++ b/htdocs/langs/en_US/other.lang
@@ -81,6 +81,7 @@ PredefinedMailContentSendSupplierInvoice=__CONTACTCIVNAME__\n\nYou will find her
 PredefinedMailContentSendShipping=__CONTACTCIVNAME__\n\nYou will find here the shipping __SHIPPINGREF__\n\n__PERSONALIZED__Sincerely\n\n__SIGNATURE__
 PredefinedMailContentSendFichInter=__CONTACTCIVNAME__\n\nYou will find here the intervention __FICHINTERREF__\n\n__PERSONALIZED__Sincerely\n\n__SIGNATURE__
 PredefinedMailContentThirdparty=__CONTACTCIVNAME__\n\n__PERSONALIZED__\n\n__SIGNATURE__
+PredefinedMailContentUser=aa__PERSONALIZED__\n\n__SIGNATURE__
 DemoDesc=Dolibarr is a compact ERP/CRM supporting several business modules. A demo showcasing all modules makes no sense as this scenario never occurs (several hundred available). So, several demo profiles are available.
 ChooseYourDemoProfil=Choose the demo profile that best suits your needs...
 ChooseYourDemoProfilMore=...or build your own profile<br>(manual module selection)
diff --git a/htdocs/user/card.php b/htdocs/user/card.php
index 089820da442..a2938a89712 100644
--- a/htdocs/user/card.php
+++ b/htdocs/user/card.php
@@ -1659,13 +1659,11 @@ else
             }
             if ($action == 'presend')
             {
-                /*
-                 * Affiche formulaire mail
-                 */
+                // Show email form
 
                 // By default if $action=='presend'
                 $titreform='SendMail';
-                $topicmail='';
+                $topicmail=1;
                 $action='send';
                 $modelmail='user';
 
@@ -1680,9 +1678,8 @@ else
                 $newlang = '';
                 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id']))
                     $newlang = $_REQUEST['lang_id'];
-
-                if ($conf->global->MAIN_MULTILANGS && empty($newlang))
-                    $newlang = $object->default_lang;
+                //if ($conf->global->MAIN_MULTILANGS && empty($newlang))
+                //    $newlang = $object->thirdparty->default_lang;
 
                 // Cree l'objet formulaire mail
                 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
@@ -1701,7 +1698,7 @@ else
                     $formmail->frommail=dolAddEmailTrackId($formmail->frommail, 'thi'.$object->id);
                 }
                 $formmail->withfrom=1;
-                $formmail->withtopic=1;
+                $formmail->withtopic=$topicmail;
                 $formmail->withto=GETPOST('sendto')?GETPOST('sendto'):$object->email;
                 $formmail->withtofree=1;
                 $formmail->withtocc=1;
@@ -1711,37 +1708,9 @@ else
                 $formmail->withdeliveryreceipt=1;
                 $formmail->withcancel=1;
                 // Tableau des substitutions
-                //$formmail->setSubstitFromObject($object);
+                $formmail->setSubstitFromObject($object);
                 $formmail->substit['__LASTNAME__']=$object->lastname;
                 $formmail->substit['__FIRSTNAME__']=$object->firstname;
-                $formmail->substit['__SIGNATURE__']=$user->signature;
-                $formmail->substit['__PERSONALIZED__']='';
-
-                //Find the good contact adress
-                /*
-                $custcontact='';
-                $contactarr=array();
-                $contactarr=$object->liste_contact(-1,'external');
-
-                if (is_array($contactarr) && count($contactarr)>0)
-                {
-                foreach($contactarr as $contact)
-                {
-                if ($contact['libelle']==$langs->trans('TypeContact_facture_external_BILLING')) {
-
-                require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
-
-                $contactstatic=new Contact($db);
-                $contactstatic->fetch($contact['id']);
-                $custcontact=$contactstatic->getFullName($langs,1);
-                }
-                }
-
-                if (!empty($custcontact)) {
-                $formmail->substit['__CONTACTCIVNAME__']=$custcontact;
-                }
-                }*/
-
 
                 // Tableau des parametres complementaires du post
                 $formmail->param['action']=$action;
-- 
GitLab