FIX #6980 FormMail::getEmailTemplate returns an array instead of a ModelMail object

Close #6980
This commit is contained in:
Marcos García
2017-12-17 23:42:50 +01:00
parent 6f9822ddb3
commit f45c3e8231

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net> /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com> * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2015 Marcos García <marcosgdf@gmail.com> * Copyright (C) 2015-2017 Marcos García <marcosgdf@gmail.com>
* *
* This program is free software; you can redistribute it and/or modify * 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 * 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 // Get message template for $this->param["models"] into c_email_templates
$arraydefaultmessage=array(); $arraydefaultmessage = -1;
if ($this->param['models'] != 'none') if ($this->param['models'] != 'none')
{ {
$model_id=0; $model_id=0;
@@ -291,12 +291,10 @@ class FormMail extends Form
{ {
$model_id=$this->param["models_id"]; $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 // Define list of attached files
$listofpaths=array(); $listofpaths=array();
@@ -307,7 +305,7 @@ class FormMail extends Form
if (GETPOST('mode','alpha') == 'init' || (GETPOST('modelmailselected','alpha') && GETPOST('modelmailselected','alpha') != '-1')) if (GETPOST('mode','alpha') == 'init' || (GETPOST('modelmailselected','alpha') && GETPOST('modelmailselected','alpha') != '-1'))
{ {
$this->clear_attached_files(); $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) foreach($this->param['fileinit'] as $file)
{ {
@@ -755,8 +753,11 @@ class FormMail extends Form
$defaulttopic=GETPOST('subject','none'); $defaulttopic=GETPOST('subject','none');
if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1') if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1')
{ {
if (count($arraydefaultmessage) > 0 && $arraydefaultmessage['topic']) $defaulttopic=$arraydefaultmessage['topic']; if ($arraydefaultmessage && $arraydefaultmessage->topic) {
elseif (! is_numeric($this->withtopic)) $defaulttopic=$this->withtopic; $defaulttopic = $arraydefaultmessage->topic;
} elseif (! is_numeric($this->withtopic)) {
$defaulttopic = $this->withtopic;
}
} }
$defaulttopic=make_substitutions($defaulttopic,$this->substit); $defaulttopic=make_substitutions($defaulttopic,$this->substit);
@@ -847,8 +848,11 @@ class FormMail extends Form
$defaultmessage=GETPOST('message','none'); $defaultmessage=GETPOST('message','none');
if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1') if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1')
{ {
if (count($arraydefaultmessage) > 0 && $arraydefaultmessage['content']) $defaultmessage=$arraydefaultmessage['content']; if ($arraydefaultmessage && $arraydefaultmessage->content) {
elseif (! is_numeric($this->withbody)) $defaultmessage=$this->withbody; $defaultmessage = $arraydefaultmessage['content'];
} elseif (! is_numeric($this->withbody)) {
$defaultmessage = $this->withbody;
}
} }
// Complete substitution array // Complete substitution array
@@ -874,7 +878,7 @@ class FormMail extends Form
//Add lines substitution key from each line //Add lines substitution key from each line
$lines = ''; $lines = '';
$defaultlines = $arraydefaultmessage['content_lines']; $defaultlines = $arraydefaultmessage->content_lines;
if (isset($defaultlines)) if (isset($defaultlines))
{ {
foreach ($this->substit_lines as $substit_line) foreach ($this->substit_lines as $substit_line)
@@ -984,11 +988,11 @@ class FormMail extends Form
* @param Translate $outputlangs Output lang object * @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 $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 * @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) 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 = "SELECT label, topic, joinfiles, content, content_lines, lang";
$sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates'; $sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates';
@@ -1001,20 +1005,20 @@ class FormMail extends Form
if ($id == -1) $sql.= " AND position=0"; if ($id == -1) $sql.= " AND position=0";
$sql.= $db->order("position,lang,label","ASC"); $sql.= $db->order("position,lang,label","ASC");
if ($id == -1) $sql.= $db->plimit(1); if ($id == -1) $sql.= $db->plimit(1);
//print $sql;
$resql = $db->query($sql); $resql = $db->query($sql);
if ($resql) if ($resql)
{ {
$obj = $db->fetch_object($resql); // Get first found // Get first found
if ($obj) $obj = $db->fetch_object($resql);
{
$ret['label']=$obj->label; if ($obj) {
$ret['lang']=$obj->lang; $ret->label = $obj->label;
$ret['topic']=$obj->topic; $ret->lang = $obj->lang;
$ret['joinfiles']=$obj->joinfiles; $ret->topic = $obj->topic;
$ret['content']=$obj->content; $ret->content = $obj->content;
$ret['content_lines']=$obj->content_lines; $ret->content_lines = $obj->content_lines;
$ret->joinfiles = $obj->joinfiles;
} }
else // If there is no template at all 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=='thirdparty') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentThirdparty"); }
elseif ($type_template=='user') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentUser"); } elseif ($type_template=='user') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentUser"); }
$ret['label']='default'; $ret->label = 'default';
$ret['lang']=$outputlangs->defaultlang; $ret->lang = $outputlangs->defaultlang;
$ret['topic']=''; $ret->topic = '';
$ret['joinfiles']=1; $ret->joinfiles = 1;
$ret['content']=$defaultmessage; $ret->content = $defaultmessage;
$ret['content_lines']=''; $ret->content_lines ='';
} }
$db->free($resql); $db->free($resql);
@@ -1297,4 +1301,5 @@ class ModelMail
public $content; public $content;
public $content_lines; public $content_lines;
public $lang; public $lang;
public $joinfiles;
} }