diff --git a/htdocs/core/class/html.formmail.class.php b/htdocs/core/class/html.formmail.class.php index a3a37c86e8c..94222c6c5df 100644 --- a/htdocs/core/class/html.formmail.class.php +++ b/htdocs/core/class/html.formmail.class.php @@ -2,7 +2,7 @@ /* Copyright (C) 2005-2012 Laurent Destailleur * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2010-2011 Juanjo Menent - * Copyright (C) 2015 Marcos García + * Copyright (C) 2015-2017 Marcos García * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -283,7 +283,7 @@ class FormMail extends Form } // Get message template for $this->param["models"] into c_email_templates - $arraydefaultmessage=array(); + $arraydefaultmessage = -1; if ($this->param['models'] != 'none') { $model_id=0; @@ -291,12 +291,10 @@ class FormMail extends Form { $model_id=$this->param["models_id"]; } - $arraydefaultmessage=$this->getEMailTemplate($this->db, $this->param["models"], $user, $outputlangs, ($model_id ? $model_id : -1)); // we set -1 if model_id empty - } - //var_dump($this->param["models"]); - //var_dump($model_id); - //var_dump($arraydefaultmessage); + // we set -1 if model_id empty + $arraydefaultmessage = $this->getEMailTemplate($this->db, $this->param["models"], $user, $outputlangs, ($model_id ? $model_id : -1)); + } // Define list of attached files $listofpaths=array(); @@ -307,7 +305,7 @@ class FormMail extends Form if (GETPOST('mode','alpha') == 'init' || (GETPOST('modelmailselected','alpha') && GETPOST('modelmailselected','alpha') != '-1')) { $this->clear_attached_files(); - if (! empty($arraydefaultmessage['joinfiles']) && is_array($this->param['fileinit'])) + if (! empty($arraydefaultmessage->joinfiles) && is_array($this->param['fileinit'])) { foreach($this->param['fileinit'] as $file) { @@ -755,8 +753,11 @@ class FormMail extends Form $defaulttopic=GETPOST('subject','none'); if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1') { - if (count($arraydefaultmessage) > 0 && $arraydefaultmessage['topic']) $defaulttopic=$arraydefaultmessage['topic']; - elseif (! is_numeric($this->withtopic)) $defaulttopic=$this->withtopic; + if ($arraydefaultmessage && $arraydefaultmessage->topic) { + $defaulttopic = $arraydefaultmessage->topic; + } elseif (! is_numeric($this->withtopic)) { + $defaulttopic = $this->withtopic; + } } $defaulttopic=make_substitutions($defaulttopic,$this->substit); @@ -847,8 +848,11 @@ class FormMail extends Form $defaultmessage=GETPOST('message','none'); if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1') { - if (count($arraydefaultmessage) > 0 && $arraydefaultmessage['content']) $defaultmessage=$arraydefaultmessage['content']; - elseif (! is_numeric($this->withbody)) $defaultmessage=$this->withbody; + if ($arraydefaultmessage && $arraydefaultmessage->content) { + $defaultmessage = $arraydefaultmessage['content']; + } elseif (! is_numeric($this->withbody)) { + $defaultmessage = $this->withbody; + } } // Complete substitution array @@ -874,7 +878,7 @@ class FormMail extends Form //Add lines substitution key from each line $lines = ''; - $defaultlines = $arraydefaultmessage['content_lines']; + $defaultlines = $arraydefaultmessage->content_lines; if (isset($defaultlines)) { foreach ($this->substit_lines as $substit_line) @@ -984,11 +988,11 @@ class FormMail extends Form * @param Translate $outputlangs Output lang object * @param int $id Id of template to find, or -1 for first found with position = 0, or 0 for all * @param int $active 1=Only active template, 0=Only disabled, -1=All - * @return array array('topic'=>,'content'=>,..) + * @return ModelMail */ public function getEMailTemplate($db, $type_template, $user, $outputlangs, $id=0, $active=1) { - $ret=array(); + $ret = new ModelMail(); $sql = "SELECT label, topic, joinfiles, content, content_lines, lang"; $sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates'; @@ -1001,20 +1005,20 @@ class FormMail extends Form if ($id == -1) $sql.= " AND position=0"; $sql.= $db->order("position,lang,label","ASC"); if ($id == -1) $sql.= $db->plimit(1); - //print $sql; $resql = $db->query($sql); if ($resql) { - $obj = $db->fetch_object($resql); // Get first found - if ($obj) - { - $ret['label']=$obj->label; - $ret['lang']=$obj->lang; - $ret['topic']=$obj->topic; - $ret['joinfiles']=$obj->joinfiles; - $ret['content']=$obj->content; - $ret['content_lines']=$obj->content_lines; + // Get first found + $obj = $db->fetch_object($resql); + + if ($obj) { + $ret->label = $obj->label; + $ret->lang = $obj->lang; + $ret->topic = $obj->topic; + $ret->content = $obj->content; + $ret->content_lines = $obj->content_lines; + $ret->joinfiles = $obj->joinfiles; } else // If there is no template at all { @@ -1031,12 +1035,12 @@ class FormMail extends Form elseif ($type_template=='thirdparty') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentThirdparty"); } elseif ($type_template=='user') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentUser"); } - $ret['label']='default'; - $ret['lang']=$outputlangs->defaultlang; - $ret['topic']=''; - $ret['joinfiles']=1; - $ret['content']=$defaultmessage; - $ret['content_lines']=''; + $ret->label = 'default'; + $ret->lang = $outputlangs->defaultlang; + $ret->topic = ''; + $ret->joinfiles = 1; + $ret->content = $defaultmessage; + $ret->content_lines =''; } $db->free($resql); @@ -1297,4 +1301,5 @@ class ModelMail public $content; public $content_lines; public $lang; + public $joinfiles; }