diff --git a/htdocs/core/actions_sendmails.inc.php b/htdocs/core/actions_sendmails.inc.php
index 10712b971bc..fce813aaa05 100644
--- a/htdocs/core/actions_sendmails.inc.php
+++ b/htdocs/core/actions_sendmails.inc.php
@@ -63,7 +63,7 @@ if (! empty($_POST['removedfile']))
/*
* Send mail
*/
-if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_POST['removedfile'] && ! $_POST['cancel'])
+if (($action == 'send' || $action == 'relance') && ! $_POST['addfile'] && ! $_POST['removedfile'] && ! $_POST['cancel'] && !$_POST['modelselected'])
{
$langs->load('mails');
diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php
index 9e7686f70ce..6a18717324c 100644
--- a/htdocs/core/class/html.formmail.class.php
+++ b/htdocs/core/class/html.formmail.class.php
@@ -32,7 +32,7 @@ require_once DOL_DOCUMENT_ROOT .'/core/class/html.form.class.php';
* $formmail->proprietes=1 ou chaine ou tableau de valeurs
* $formmail->show_form() affiche le formulaire
*/
-class FormMail
+class FormMail extends Form
{
var $db;
@@ -71,6 +71,8 @@ class FormMail
var $error;
+ public $lines_model;
+
/**
* Constructor
@@ -242,7 +244,7 @@ class FormMail
else
{
$out='';
-
+
// Define list of attached files
$listofpaths=array();
$listofnames=array();
@@ -263,8 +265,11 @@ class FormMail
}
// Get message template
- $arraydefaultmessage=$this->getEMailTemplate($this->db, $this->param["models"], $user, $outputlangs);
-
+ $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);
$out.= "\n\n";
if ($this->withform == 1)
@@ -277,6 +282,27 @@ class FormMail
{
$out.= ''."\n";
}
+
+ $result = $this->fetchAllEMailTemplate($this->param["models"], $user, $outputlangs);
+ if ($result<0) {
+ setEventMessage($this->error,'errors');
+ }
+ $modelmail_array=array();
+ foreach($this->lines_model as $line) {
+ $modelmail_array[$line->id]=$line->label;
+ }
+ if (count($modelmail_array>0)) {
+ $out.= '
';
+ }
+
+
$out.= ''."\n";
// Substitution array
@@ -617,8 +643,8 @@ class FormMail
$defaultmessage = dol_nl2br($defaultmessage);
}
-
- if (isset($_POST["message"])) $defaultmessage=$_POST["message"];
+
+ if (isset($_POST["message"]) && ! $_POST['modelselected']) $defaultmessage=$_POST["message"];
else
{
$defaultmessage=make_substitutions($defaultmessage,$this->substit);
@@ -690,9 +716,10 @@ class FormMail
* @param string $type_template Get message for key module
* @param string $user Use template public or limited to this user
* @param Translate $outputlangs Output lang object
+ * @param int $id Id template to find
* @return array array('topic'=>,'content'=>,..)
*/
- private function getEMailTemplate($db, $type_template, $user, $outputlangs)
+ private function getEMailTemplate($db, $type_template, $user, $outputlangs,$id=0)
{
$ret=array();
@@ -702,6 +729,7 @@ class FormMail
$sql.= " AND entity IN (".getEntity("c_email_templates").")";
$sql.= " AND (fk_user is NULL or fk_user = 0 or fk_user = ".$user->id.")";
if (is_object($outputlangs)) $sql.= " AND (lang = '".$outputlangs->defaultlang."' OR lang IS NULL OR lang = '')";
+ if (!empty($id)) $sql.= " AND rowid=".$id;
$sql.= $db->order("lang,label","ASC");
//print $sql;
@@ -745,5 +773,96 @@ class FormMail
return -1;
}
}
+
+ /**
+ * Find if template exists
+ * Search into table c_email_templates
+ *
+ * @param string $type_template Get message for key module
+ * @param string $user Use template public or limited to this user
+ * @param Translate $outputlangs Output lang object
+ * @return int <0 if KO,
+ */
+ public function isEMailTemplate($type_template, $user, $outputlangs)
+ {
+ $ret=array();
+
+ $sql = "SELECT label, topic, content, lang";
+ $sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates';
+ $sql.= " WHERE type_template='".$this->db->escape($type_template)."'";
+ $sql.= " AND entity IN (".getEntity("c_email_templates").")";
+ $sql.= " AND (fk_user is NULL or fk_user = 0 or fk_user = ".$user->id.")";
+ if (is_object($outputlangs)) $sql.= " AND (lang = '".$outputlangs->defaultlang."' OR lang IS NULL OR lang = '')";
+ $sql.= $this->db->order("lang,label","ASC");
+ //print $sql;
+
+ $resql = $this->db->query($sql);
+ if ($resql)
+ {
+ $num= $this->db->num_rows($resql);
+ $this->db->free($resql);
+ return $num;
+ }
+ else
+ {
+ $this->error=get_class($this).' '.__METHOD__.' ERROR:'.$this->db->lasterror();
+ return -1;
+ }
+ }
+
+ /**
+ * Find if template exists
+ * Search into table c_email_templates
+ *
+ * @param string $type_template Get message for key module
+ * @param string $user Use template public or limited to this user
+ * @param Translate $outputlangs Output lang object
+ * @return int <0 if KO,
+ */
+ public function fetchAllEMailTemplate($type_template, $user, $outputlangs)
+ {
+ $ret=array();
+
+ $sql = "SELECT rowid, label, topic, content, lang";
+ $sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates';
+ $sql.= " WHERE type_template='".$this->db->escape($type_template)."'";
+ $sql.= " AND entity IN (".getEntity("c_email_templates").")";
+ $sql.= " AND (fk_user is NULL or fk_user = 0 or fk_user = ".$user->id.")";
+ if (is_object($outputlangs)) $sql.= " AND (lang = '".$outputlangs->defaultlang."' OR lang IS NULL OR lang = '')";
+ $sql.= $this->db->order("lang,label","ASC");
+ //print $sql;
+
+ $resql = $this->db->query($sql);
+ if ($resql)
+ {
+ $this->lines_model=array();
+ while ($obj = $this->db->fetch_object($resql)) {
+ $line = new ModelMailLine();
+ $line->id=$obj->rowid;
+ $line->label=$obj->label;
+ $line->topic=$obj->topic;
+ $line->content=$obj->lacontentbel;
+ $line->lang=$obj->lang;
+ $this->lines_model[]=$line;
+
+ }
+ $this->db->free($resql);
+ return $num;
+ }
+ else
+ {
+ $this->error=get_class($this).' '.__METHOD__.' ERROR:'.$this->db->lasterror();
+ return -1;
+ }
+ }
+}
+
+class ModelMailLine {
+ public $id;
+ public $label;
+ public $topic;
+ public $content;
+ public $lang;
+
}
diff --git a/htdocs/langs/en_US/main.lang b/htdocs/langs/en_US/main.lang
index 85e1e931574..d022a7dcf42 100644
--- a/htdocs/langs/en_US/main.lang
+++ b/htdocs/langs/en_US/main.lang
@@ -732,3 +732,4 @@ ShortThursday=T
ShortFriday=F
ShortSaturday=S
ShortSunday=S
+SelectMailModel=Select email template
\ No newline at end of file
diff --git a/htdocs/societe/soc.php b/htdocs/societe/soc.php
index c59894fd3dc..f5497aed0ac 100644
--- a/htdocs/societe/soc.php
+++ b/htdocs/societe/soc.php
@@ -2163,7 +2163,10 @@ else
print ''."\n";
-
+ //Select mail models is same action as presend
+ if (!empty(GETPOST('modelselected'))) {
+ $action = 'presend';
+ }
if ($action == 'presend')
{
/*
@@ -2185,7 +2188,7 @@ else
if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id']))
$newlang = $_REQUEST['lang_id'];
if ($conf->global->MAIN_MULTILANGS && empty($newlang))
- $newlang = $object->client->default_lang;
+ $newlang = $object->default_lang;
// Cree l'objet formulaire mail
include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
@@ -2246,6 +2249,7 @@ else
// Tableau des parametres complementaires du post
$formmail->param['action']=$action;
$formmail->param['models']=$modelmail;
+ $formmail->param['models_id']=GETPOST('modelmailselected','int');
$formmail->param['socid']=$object->id;
$formmail->param['returnurl']=$_SERVER["PHP_SELF"].'?socid='.$object->id;
@@ -2255,7 +2259,6 @@ else
$formmail->clear_attached_files();
$formmail->add_attached_files($file,basename($file),dol_mimetype($file));
}
-
print $formmail->get_form();
print '
';