* Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2010-2011 Juanjo Menent * Copyright (C) 2015 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 * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/core/class/html.formmail.class.php * \ingroup core * \brief Fichier de la classe permettant la generation du formulaire html d'envoi de mail unitaire */ require_once DOL_DOCUMENT_ROOT .'/core/class/html.form.class.php'; /** * Classe permettant la generation du formulaire html d'envoi de mail unitaire * Usage: $formail = new FormMail($db) * $formmail->proprietes=1 ou chaine ou tableau de valeurs * $formmail->show_form() affiche le formulaire */ class FormMail extends Form { var $db; var $withform; // 1=Include HTML form tag and show submit button, 0=Do not include form tag and submit button, -1=Do not include form tag but include submit button var $fromname; var $frommail; var $replytoname; var $replytomail; var $toname; var $tomail; var $trackid; var $withsubstit; // Show substitution array var $withfrom; /** * @var int * @deprecated Fill withto with array before calling method. * @see withto */ public $withtosocid; /** * @var int|int[] */ public $withto; // Show recipient emails var $withtofree; // Show free text for recipient emails var $withtocc; var $withtoccc; var $withtopic; var $withfile; // 0=No attaches files, 1=Show attached files, 2=Can add new attached files var $withbody; var $withfromreadonly; var $withreplytoreadonly; var $withtoreadonly; var $withtoccreadonly; var $withtocccreadonly; var $withtopicreadonly; var $withfilereadonly; var $withdeliveryreceipt; var $withcancel; var $withfckeditor; var $substit=array(); var $substit_lines=array(); var $param=array(); var $error; public $lines_model; /** * Constructor * * @param DoliDB $db Database handler */ function __construct($db) { $this->db = $db; $this->withform=1; $this->withfrom=1; $this->withto=1; $this->withtofree=1; $this->withtocc=1; $this->withtoccc=0; $this->witherrorsto=0; $this->withtopic=1; $this->withfile=0; $this->withbody=1; $this->withfromreadonly=1; $this->withreplytoreadonly=1; $this->withtoreadonly=0; $this->withtoccreadonly=0; $this->withtocccreadonly=0; $this->witherrorstoreadonly=0; $this->withtopicreadonly=0; $this->withfilereadonly=0; $this->withbodyreadonly=0; $this->withdeliveryreceiptreadonly=0; $this->withfckeditor=-1; // -1 = Auto return 1; } /** * Clear list of attached files in send mail form (also stored in session) * * @return void */ function clear_attached_files() { global $conf,$user; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; // Set tmp user directory $vardir=$conf->user->dir_output."/".$user->id; $upload_dir = $vardir.'/temp/'; // TODO Add $keytoavoidconflict in upload_dir path if (is_dir($upload_dir)) dol_delete_dir_recursive($upload_dir); $keytoavoidconflict = empty($this->trackid)?'':'-'.$this->trackid; // this->trackid must be defined unset($_SESSION["listofpaths".$keytoavoidconflict]); unset($_SESSION["listofnames".$keytoavoidconflict]); unset($_SESSION["listofmimes".$keytoavoidconflict]); } /** * Add a file into the list of attached files (stored in SECTION array) * * @param string $path Full absolute path on filesystem of file, including file name * @param string $file Only filename * @param string $type Mime type * @return void */ function add_attached_files($path,$file,$type) { $listofpaths=array(); $listofnames=array(); $listofmimes=array(); $keytoavoidconflict = empty($this->trackid)?'':'-'.$this->trackid; // this->trackid must be defined if (! empty($_SESSION["listofpaths".$keytoavoidconflict])) $listofpaths=explode(';',$_SESSION["listofpaths".$keytoavoidconflict]); if (! empty($_SESSION["listofnames".$keytoavoidconflict])) $listofnames=explode(';',$_SESSION["listofnames".$keytoavoidconflict]); if (! empty($_SESSION["listofmimes".$keytoavoidconflict])) $listofmimes=explode(';',$_SESSION["listofmimes".$keytoavoidconflict]); if (! in_array($file,$listofnames)) { $listofpaths[]=$path; $listofnames[]=$file; $listofmimes[]=$type; $_SESSION["listofpaths".$keytoavoidconflict]=join(';',$listofpaths); $_SESSION["listofnames".$keytoavoidconflict]=join(';',$listofnames); $_SESSION["listofmimes".$keytoavoidconflict]=join(';',$listofmimes); } } /** * Remove a file from the list of attached files (stored in SECTION array) * * @param string $keytodelete Key in file array (0, 1, 2, ...) * @return void */ function remove_attached_files($keytodelete) { $listofpaths=array(); $listofnames=array(); $listofmimes=array(); $keytoavoidconflict = empty($this->trackid)?'':'-'.$this->trackid; // this->trackid must be defined if (! empty($_SESSION["listofpaths".$keytoavoidconflict])) $listofpaths=explode(';',$_SESSION["listofpaths".$keytoavoidconflict]); if (! empty($_SESSION["listofnames".$keytoavoidconflict])) $listofnames=explode(';',$_SESSION["listofnames".$keytoavoidconflict]); if (! empty($_SESSION["listofmimes".$keytoavoidconflict])) $listofmimes=explode(';',$_SESSION["listofmimes".$keytoavoidconflict]); if ($keytodelete >= 0) { unset ($listofpaths[$keytodelete]); unset ($listofnames[$keytodelete]); unset ($listofmimes[$keytodelete]); $_SESSION["listofpaths".$keytoavoidconflict]=join(';',$listofpaths); $_SESSION["listofnames".$keytoavoidconflict]=join(';',$listofnames); $_SESSION["listofmimes".$keytoavoidconflict]=join(';',$listofmimes); //var_dump($_SESSION['listofpaths']); } } /** * Return list of attached files (stored in SECTION array) * * @return array array('paths'=> ,'names'=>, 'mimes'=> ) */ function get_attached_files() { $listofpaths=array(); $listofnames=array(); $listofmimes=array(); $keytoavoidconflict = empty($this->trackid)?'':'-'.$this->trackid; // this->trackid must be defined if (! empty($_SESSION["listofpaths".$keytoavoidconflict])) $listofpaths=explode(';',$_SESSION["listofpaths".$keytoavoidconflict]); if (! empty($_SESSION["listofnames".$keytoavoidconflict])) $listofnames=explode(';',$_SESSION["listofnames".$keytoavoidconflict]); if (! empty($_SESSION["listofmimes".$keytoavoidconflict])) $listofmimes=explode(';',$_SESSION["listofmimes".$keytoavoidconflict]); return array('paths'=>$listofpaths, 'names'=>$listofnames, 'mimes'=>$listofmimes); } /** * Show the form to input an email * this->withfile: 0=No attaches files, 1=Show attached files, 2=Can add new attached files * * @param string $addfileaction Name of action when posting file attachments * @param string $removefileaction Name of action when removing file attachments * @return void */ function show_form($addfileaction='addfile',$removefileaction='removefile') { print $this->get_form($addfileaction,$removefileaction); } /** * Get the form to input an email * this->withfile: 0=No attaches files, 1=Show attached files, 2=Can add new attached files * this->param: Contains more parameteres like email templates info * * @param string $addfileaction Name of action when posting file attachments * @param string $removefileaction Name of action when removing file attachments * @return string Form to show */ function get_form($addfileaction='addfile',$removefileaction='removefile') { global $conf, $langs, $user, $hookmanager, $form; if (! is_object($form)) $form=new Form($this->db); $langs->load("other"); $langs->load("mails"); $hookmanager->initHooks(array('formmail')); $parameters=array( 'addfileaction' => $addfileaction, 'removefileaction'=> $removefileaction, 'trackid'=> $this->trackid ); $reshook=$hookmanager->executeHooks('getFormMail', $parameters, $this); if (!empty($reshook)) { return $hookmanager->resPrint; } else { $out=''; $disablebademails=1; // Define list of attached files $listofpaths=array(); $listofnames=array(); $listofmimes=array(); $keytoavoidconflict = empty($this->trackid)?'':'-'.$this->trackid; // this->trackid must be defined if (! empty($_SESSION["listofpaths".$keytoavoidconflict])) $listofpaths=explode(';',$_SESSION["listofpaths".$keytoavoidconflict]); if (! empty($_SESSION["listofnames".$keytoavoidconflict])) $listofnames=explode(';',$_SESSION["listofnames".$keytoavoidconflict]); if (! empty($_SESSION["listofmimes".$keytoavoidconflict])) $listofmimes=explode(';',$_SESSION["listofmimes".$keytoavoidconflict]); // Define output language $outputlangs = $langs; $newlang = ''; if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $this->param['langsmodels']; if (! empty($newlang)) { $outputlangs = new Translate("", $conf); $outputlangs->setDefaultLang($newlang); $outputlangs->load('other'); } // Get message template $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); $out.= "\n".'
'."\n"; if ($this->withform == 1) { $out.= '
'."\n"; $out.= ''; $out.= ''; $out.= ''; $out.= ''; } foreach ($this->param as $key=>$value) { $out.= ''."\n"; } $result = $this->fetchAllEMailTemplate($this->param["models"], $user, $outputlangs); if ($result<0) { setEventMessages($this->error, $this->errors, 'errors'); } $modelmail_array=array(); foreach($this->lines_model as $line) { $modelmail_array[$line->id]=$line->label; } // Zone to select its email template if (count($modelmail_array)>0) { $out.= '
'."\n"; $out.= $langs->trans('SelectMailModel').': '.$this->selectarray('modelmailselected', $modelmail_array, 0, 1); if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFrom", $langs->transnoentitiesnoconv('Setup').' - '.$langs->transnoentitiesnoconv('EMails')),1); $out.= '   '; $out.= ''; $out.= '   '; $out.= '
'; } elseif (! empty($this->param['models']) && in_array($this->param['models'], array( 'propal_send','order_send','facture_send', 'shipping_send','fichinter_send','supplier_proposal_send','order_supplier_send', 'invoice_supplier_send','thirdparty' ))) { $out.= '
'."\n"; $out.= $langs->trans('SelectMailModel').': '; // Do not put disabled on option, it is already on select and it makes chrome crazy. if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFrom", $langs->transnoentitiesnoconv('Setup').' - '.$langs->transnoentitiesnoconv('EMails')),1); $out.= '   '; $out.= ''; $out.= '   '; $out.= '
'; } $out.= ''."\n"; // Substitution array if (! empty($this->withsubstit)) { $out.= '\n"; } // From if (! empty($this->withfrom)) { if (! empty($this->withfromreadonly)) { $out.= ''; $out.= ''; $out.= '\n"; $out.= "\n"; } else { $out.= "\n"; } } // Replyto if (! empty($this->withreplyto)) { if ($this->withreplytoreadonly) { $out.= ''; $out.= ''; $out.= "\n"; } } // Errorsto if (! empty($this->witherrorsto)) { //if (! $this->errorstomail) $this->errorstomail=$this->frommail; $errorstomail = (! empty($conf->global->MAIN_MAIL_ERRORS_TO) ? $conf->global->MAIN_MAIL_ERRORS_TO : $this->errorstomail); if ($this->witherrorstoreadonly) { $out.= ''; $out.= '\n"; } else { $out.= '\n"; } } // To if (! empty($this->withto) || is_array($this->withto)) { $out.= '\n"; } // CC if (! empty($this->withtocc) || is_array($this->withtocc)) { $out.= '\n"; } // CCC if (! empty($this->withtoccc) || is_array($this->withtoccc)) { $out.= '\n"; } // Ask delivery receipt if (! empty($this->withdeliveryreceipt)) { $out.= '\n"; } // Topic if (! empty($this->withtopic)) { $defaulttopic=""; if (count($arraydefaultmessage) > 0 && $arraydefaultmessage['topic']) $defaulttopic=$arraydefaultmessage['topic']; elseif (! is_numeric($this->withtopic)) $defaulttopic=$this->withtopic; $defaulttopic=make_substitutions($defaulttopic,$this->substit); $out.= ''; $out.= ''; $out.= '\n"; } // Attached files if (! empty($this->withfile)) { $out.= ''; $out.= ''; $out.= '\n"; } // Message if (! empty($this->withbody)) { $defaultmessage=""; if (count($arraydefaultmessage) > 0 && $arraydefaultmessage['content']) $defaultmessage=$arraydefaultmessage['content']; elseif (! is_numeric($this->withbody)) $defaultmessage=$this->withbody; // Complete substitution array if (! empty($conf->paypal->enabled) && ! empty($conf->global->PAYPAL_ADD_PAYMENT_URL)) { require_once DOL_DOCUMENT_ROOT.'/paypal/lib/paypal.lib.php'; $langs->load('paypal'); // Set the paypal message and url link into __PERSONALIZED__ key if ($this->param["models"]=='order_send') { $url=getPaypalPaymentUrl(0,'order',$this->substit['__ORDERREF__']?$this->substit['__ORDERREF__']:$this->substit['__REF__']); $this->substit['__PERSONALIZED__']=str_replace('\n',"\n",$langs->transnoentitiesnoconv("PredefinedMailContentLink",$url)); } if ($this->param["models"]=='facture_send') { $url=getPaypalPaymentUrl(0,'invoice',$this->substit['__REF__']); $this->substit['__PERSONALIZED__']=str_replace('\n',"\n",$langs->transnoentitiesnoconv("PredefinedMailContentLink",$url)); } } //Add lines substitution key from each line $lines = ''; $defaultlines = $arraydefaultmessage['content_lines']; if (isset($defaultlines)) { foreach ($this->substit_lines as $substit_line) { $lines .= make_substitutions($defaultlines,$substit_line)."\n"; } } $this->substit['__LINES__']=$lines; $defaultmessage=str_replace('\n',"\n",$defaultmessage); // Deal with format differences between message and signature (text / HTML) if(dol_textishtml($defaultmessage) && !dol_textishtml($this->substit['__SIGNATURE__'])) { $this->substit['__SIGNATURE__'] = dol_nl2br($this->substit['__SIGNATURE__']); } else if(!dol_textishtml($defaultmessage) && dol_textishtml($this->substit['__SIGNATURE__'])) { $defaultmessage = dol_nl2br($defaultmessage); } if (isset($_POST["message"]) && ! $_POST['modelselected']) $defaultmessage=$_POST["message"]; else { $defaultmessage=make_substitutions($defaultmessage,$this->substit); // Clean first \n and br (to avoid empty line when CONTACTCIVNAME is empty) $defaultmessage=preg_replace("/^(
)+/","",$defaultmessage); $defaultmessage=preg_replace("/^\n+/","",$defaultmessage); } $out.= ''; $out.= ''; $out.= '\n"; } $out.= '
'; $help=""; foreach($this->substit as $key => $val) { $help.=$key.' -> '.$langs->trans($val).'
'; } $out.= $form->textwithpicto($langs->trans("EMailTestSubstitutionReplacedByGenericValues"), $help); $out.= "
'.$langs->trans("MailFrom").''; if (! ($this->fromtype === 'user' && $this->fromid > 0) && ! ($this->fromtype === 'company') && ! preg_match('/user_aliases/', $this->fromtype) && ! preg_match('/global_aliases/', $this->fromtype)) { // Use this->fromname and this->frommail or error if not defined $out.= $this->fromname; if ($this->frommail) { $out.= ' <'.$this->frommail.'>'; } else { if ($this->fromtype) { $langs->load('errors'); $out.= ' <'.$langs->trans('ErrorNoMailDefinedForThisUser').'> '; } } } else { $liste = array(); if (empty($user->email)) { $langs->load('errors'); $liste['user'] = $user->getFullName($langs) . ' <'.$langs->trans('ErrorNoMailDefinedForThisUser').'>'; } else { $liste['user'] = $user->getFullName($langs) .' <'.$user->email.'>'; } $liste['company'] = $conf->global->MAIN_INFO_SOCIETE_NOM .' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>'; // Add also email aliases if there is one $listaliases=array('user_aliases'=>$user->email_aliases, 'global_aliases'=>$conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES); foreach($listaliases as $typealias => $listalias) { $posalias=0; $listaliasarray=explode(',', $listalias); foreach ($listaliasarray as $listaliasval) { $posalias++; $listaliasval=trim($listaliasval); if ($listaliasval) { $listaliasval=preg_replace('//', '>', $listaliasval); if (! preg_match('/</', $listaliasval)) $listaliasval='<'.$listaliasval.'>'; $liste[$typealias.'_'.$posalias]=$listaliasval; } } } $out.= ' '.$form->selectarray('fromtype', $liste, $this->fromtype, 0, 0, 0, '', 0, 0, 0, '', '', 0, '', $disablebademails); } $out.= "
".$langs->trans("MailFrom").""; $out.= $langs->trans("Name").':'; $out.= '    '; $out.= $langs->trans("EMail").':<>'; $out.= "
".$langs->trans("MailReply")."".$this->replytoname.($this->replytomail?(" <".$this->replytomail.">"):""); $out.= "
'.$langs->trans("MailErrorsTo").''; $out.= $errorstomail; $out.= "
'.$langs->trans("MailErrorsTo").''; $out.= ''; $out.= "
'; if ($this->withtofree) $out.= $form->textwithpicto($langs->trans("MailTo"),$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients")); else $out.= $langs->trans("MailTo"); $out.= ''; if ($this->withtoreadonly) { if (! empty($this->toname) && ! empty($this->tomail)) { $out.= ''; $out.= ''; if ($this->totype == 'thirdparty') { $soc=new Societe($this->db); $soc->fetch($this->toid); $out.= $soc->getNomUrl(1); } else if ($this->totype == 'contact') { $contact=new Contact($this->db); $contact->fetch($this->toid); $out.= $contact->getNomUrl(1); } else { $out.= $this->toname; } $out.= ' <'.$this->tomail.'>'; if ($this->withtofree) { $out.= '
'.$langs->trans("or").' withto) :"").'" />'; } } else { $out.= (! is_array($this->withto) && ! is_numeric($this->withto))?$this->withto:""; } } else { if (! empty($this->withtofree)) { $out.= 'withto) :"").'" />'; } if (! empty($this->withto) && is_array($this->withto)) { if (! empty($this->withtofree)) $out.= " ".$langs->trans("or")." "; // multiselect array convert html entities into options tags, even if we dont want this, so we encode them a second time $tmparray = $this->withto; foreach($tmparray as $key => $val) { $tmparray[$key]=dol_htmlentities($tmparray[$key], null, 'UTF-8', true); } $out.= $form->multiselectarray("receiver", $tmparray, GETPOST("receiver"), null, null, 'inline-block minwidth500', null, ""); } } $out.= "
'; $out.= $form->textwithpicto($langs->trans("MailCC"),$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients")); $out.= ''; if ($this->withtoccreadonly) { $out.= (! is_array($this->withtocc) && ! is_numeric($this->withtocc))?$this->withtocc:""; } else { $out.= 'withtocc) : (isset($_POST["sendtocc"])?$_POST["sendtocc"]:"") ).'" />'; if (! empty($this->withtocc) && is_array($this->withtocc)) { $out.= " ".$langs->trans("or")." "; // multiselect array convert html entities into options tags, even if we dont want this, so we encode them a second time $tmparray = $this->withtocc; foreach($tmparray as $key => $val) { $tmparray[$key]=dol_htmlentities($tmparray[$key], null, 'UTF-8', true); } $out.= $form->multiselectarray("receivercc", $tmparray, GETPOST("receivercc"), null, null, 'inline-block minwidth500',null, ""); } } $out.= "
'; $out.= $form->textwithpicto($langs->trans("MailCCC"),$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients")); $out.= ''; if (! empty($this->withtocccreadonly)) { $out.= (! is_array($this->withtoccc) && ! is_numeric($this->withtoccc))?$this->withtoccc:""; } else { $out.= 'withtoccc) : (isset($_POST["sendtoccc"])?$_POST["sendtoccc"]:"") ).'" />'; if (! empty($this->withtoccc) && is_array($this->withtoccc)) { $out.= " ".$langs->trans("or")." "; // multiselect array convert html entities into options tags, even if we dont want this, so we encode them a second time $tmparray = $this->withtoccc; foreach($tmparray as $key => $val) { $tmparray[$key]=dol_htmlentities($tmparray[$key], null, 'UTF-8', true); } //$out.= $form->selectarray("receiverccc", $this->withtoccc, GETPOST("receiverccc"), 1); $out.= $form->multiselectarray("receiverccc", $tmparray, GETPOST("receiverccc"), null, null, null,null, "90%"); } } $showinfobcc=''; if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_PROPOSAL_TO) && ! empty($this->param['models']) && $this->param['models'] == 'propal_send') $showinfobcc=$conf->global->MAIN_MAIL_AUTOCOPY_PROPOSAL_TO; if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO) && ! empty($this->param['models']) && $this->param['models'] == 'supplier_proposal_send') $showinfobcc=$conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO; if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_ORDER_TO) && ! empty($this->param['models']) && $this->param['models'] == 'order_send') $showinfobcc=$conf->global->MAIN_MAIL_AUTOCOPY_ORDER_TO; if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO) && ! empty($this->param['models']) && $this->param['models'] == 'facture_send') $showinfobcc=$conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO; if ($showinfobcc) $out.=' + '.$showinfobcc; $out.= "
'.$langs->trans("DeliveryReceipt").''; if (! empty($this->withdeliveryreceiptreadonly)) { $out.= yn($this->withdeliveryreceipt); } else { $defaultvaluefordeliveryreceipt=0; if (! empty($conf->global->MAIL_FORCE_DELIVERY_RECEIPT_PROPAL) && ! empty($this->param['models']) && $this->param['models'] == 'propal_send') $defaultvaluefordeliveryreceipt=1; if (! empty($conf->global->MAIL_FORCE_DELIVERY_RECEIPT_SUPPLIER_PROPOSAL) && ! empty($this->param['models']) && $this->param['models'] == 'supplier_proposal_send') $defaultvaluefordeliveryreceipt=1; if (! empty($conf->global->MAIL_FORCE_DELIVERY_RECEIPT_ORDER) && ! empty($this->param['models']) && $this->param['models'] == 'order_send') $defaultvaluefordeliveryreceipt=1; if (! empty($conf->global->MAIL_FORCE_DELIVERY_RECEIPT_INVOICE) && ! empty($this->param['models']) && $this->param['models'] == 'facture_send') $defaultvaluefordeliveryreceipt=1; $out.= $form->selectyesno('deliveryreceipt', (isset($_POST["deliveryreceipt"])?$_POST["deliveryreceipt"]:$defaultvaluefordeliveryreceipt), 1); } $out.= "
'.$langs->trans("MailTopic").''; if ($this->withtopicreadonly) { $out.= $defaulttopic; $out.= ''; } else { $out.= ''; } $out.= "
'.$langs->trans("MailFile").''; if (is_numeric($this->withfile)) { // TODO Trick to have param removedfile containing nb of image to delete. But this does not works without javascript $out.= ''."\n"; $out.= ''."\n"; if (count($listofpaths)) { foreach($listofpaths as $key => $val) { $out.= '
'; $out.= img_mime($listofnames[$key]).' '.$listofnames[$key]; if (! $this->withfilereadonly) { $out.= ' '; //$out.= ' '.img_delete($langs->trans("Delete").''; } $out.= '
'; } } else { $out.= $langs->trans("NoAttachedFiles").'
'; } if ($this->withfile == 2) // Can add other files { if (!empty($conf->global->FROM_MAIL_USE_INPUT_FILE_MULTIPLE)) $out.= ''; else $out.= ''; $out.= ' '; $out.= ''; } } else { $out.=$this->withfile; } $out.= "
'.$langs->trans("MailText").''; if ($this->withbodyreadonly) { $out.= nl2br($defaultmessage); $out.= ''; } else { if (! isset($this->ckeditortoolbar)) $this->ckeditortoolbar = 'dolibarr_notes'; // Editor wysiwyg require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; if ($this->withfckeditor == -1) { if (! empty($conf->global->FCKEDITOR_ENABLE_MAIL)) $this->withfckeditor=1; else $this->withfckeditor=0; } $doleditor=new DolEditor('message',$defaultmessage,'',280,$this->ckeditortoolbar,'In',true,true,$this->withfckeditor,8,'95%'); $out.= $doleditor->Create(1); } $out.= "
'."\n"; if ($this->withform == 1 || $this->withform == -1) { $out.= '
'; $out.= 'withfile == 2 && $conf->use_javascript_ajax) { $out.= ' onClick="if (document.mailform.addedfile.value != \'\') { alert(\''.dol_escape_js($langs->trans("FileWasNotUploaded")).'\'); return false; } else { return true; }"'; } $out.= ' />'; if ($this->withcancel) { $out.= '     '; $out.= ''; } $out.= '
'."\n"; } if ($this->withform == 1) $out.= '
'."\n"; // Disable enter key if option MAIN_MAILFORM_DISABLE_ENTERKEY is set if (! empty($conf->global->MAIN_MAILFORM_DISABLE_ENTERKEY)) { $out.= ''; } $out.= "\n"; return $out; } } /** * Return template of email * Search into table c_email_templates * * @param DoliDB $db Database handler * @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 * @param int $active 1=Only active template, 0=Only disabled, -1=All * @return array array('topic'=>,'content'=>,..) */ private function getEMailTemplate($db, $type_template, $user, $outputlangs, $id=0, $active=1) { $ret=array(); $sql = "SELECT label, topic, content, content_lines, lang"; $sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates'; $sql.= " WHERE type_template='".$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 ($active >= 0) $sql.=" AND active = ".$active; 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("position,lang,label","ASC"); //print $sql; $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); // Get first found if ($obj) { $ret['label']=$obj->label; $ret['topic']=$obj->topic; $ret['content']=$obj->content; $ret['content_lines']=$obj->content_lines; $ret['lang']=$obj->lang; } else { $defaultmessage=''; if ($type_template=='facture_send') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendInvoice"); } elseif ($type_template=='facture_relance') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendInvoiceReminder"); } elseif ($type_template=='propal_send') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendProposal"); } elseif ($type_template=='supplier_proposal_send') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendSupplierProposal"); } elseif ($type_template=='order_send') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendOrder"); } elseif ($type_template=='order_supplier_send') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendSupplierOrder"); } elseif ($type_template=='invoice_supplier_send') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentSendSupplierInvoice"); } 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"); } $ret['label']='default'; $ret['topic']=''; $ret['content']=$defaultmessage; $ret['content_lines']=''; $ret['lang']=$outputlangs->defaultlang; } $db->free($resql); return $ret; } else { dol_print_error($db); 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 * @param int $active 1=Only active template, 0=Only disabled, -1=All * @return int <0 if KO, nb of records found if OK */ public function fetchAllEMailTemplate($type_template, $user, $outputlangs, $active=1) { $ret=array(); $sql = "SELECT rowid, label, topic, content, content_lines, lang, position"; $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 ($active >= 0) $sql.=" AND active = ".$active; if (is_object($outputlangs)) $sql.= " AND (lang = '".$outputlangs->defaultlang."' OR lang IS NULL OR lang = '')"; $sql.= $this->db->order("position,lang,label","ASC"); //print $sql; $resql = $this->db->query($sql); if ($resql) { $num=$this->db->num_rows($resql); $this->lines_model=array(); while ($obj = $this->db->fetch_object($resql)) { $line = new ModelMail(); $line->id=$obj->rowid; $line->label=$obj->label; $line->topic=$obj->topic; $line->content=$obj->content; $line->content_lines=$obj->content_lines; $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; } } /** * Set substit array from object * * @param CommonObject $object Object to use * @param Translate $outputlangs Object lang * @return void */ function setSubstitFromObject($object, $outputlangs=null) { global $conf, $user; $this->substit['__REF__'] = $object->ref; $this->substit['__REFCLIENT__'] = isset($object->ref_client) ? $object->ref_client : ''; $this->substit['__REFSUPPLIER__'] = isset($object->ref_supplier) ? $object->ref_supplier : ''; $this->substit['__DATE_YMD__'] = isset($object->date) ? dol_print_date($object->date, 'day', 0, $outputlangs) : ''; $this->substit['__DATE_DUE_YMD__'] = isset($object->date_lim_reglement)? dol_print_date($object->date_lim_reglement, 'day', 0, $outputlangs) : ''; $this->substit['__AMOUNT__'] = price($object->total_ttc); $this->substit['__AMOUNT_WO_TAX__'] = price($object->total_ht); $this->substit['__AMOUNT_VAT__'] = price($object->total_tva); $this->substit['__THIRDPARTY_ID__'] = (is_object($object->thirdparty)?$object->thirdparty->id:''); $this->substit['__THIRDPARTY_NAME__'] = (is_object($object->thirdparty)?$object->thirdparty->name:''); $this->substit['__PROJECT_ID__'] = (is_object($object->projet)?$object->projet->id:''); $this->substit['__PROJECT_REF__'] = (is_object($object->projet)?$object->projet->ref:''); $this->substit['__PROJECT_NAME__'] = (is_object($object->projet)?$object->projet->title:''); $this->substit['__SIGNATURE__'] = $user->signature; $this->substit['__PERSONALIZED__'] = ''; $this->substit['__CONTACTCIVNAME__'] = ''; // Will be replace just before sending // Create dinamic tags for __EXTRAFIELD_FIELD__ $extrafields = new ExtraFields($this->db); $extralabels = $extrafields->fetch_name_optionals_label($object->table_element, true); $object->fetch_optionals($object->id, $extralabels); foreach ($extrafields->attribute_label as $key => $label) { $this->substit['__EXTRAFIELD_' . strtoupper($key) . '__'] = $object->array_options['options_' . $key]; } //Fill substit_lines with each object lines content if (is_array($object->lines)) { foreach ($object->lines as $line) { $substit_line = array( '__PRODUCT_REF__' => isset($line->product_ref) ? $line->product_ref : '', '__PRODUCT_LABEL__' => isset($line->product_label) ? $line->product_label : '', '__PRODUCT_DESCRIPTION__' => isset($line->product_desc) ? $line->product_desc : '', '__LABEL__' => isset($line->label) ? $line->label : '', '__DESCRIPTION__' => isset($line->desc) ? $line->desc : '', '__DATE_START_YMD__' => dol_print_date($line->date_start, 'day', 0, $outputlangs), '__DATE_END_YMD__' => dol_print_date($line->date_end, 'day', 0, $outputlangs), '__QUANTITY__' => $line->qty, '__SUBPRICE__' => price($line->subprice), '__AMOUNT__' => price($line->total_ttc), '__AMOUNT_WO_TAX__' => price($line->total_ht), //'__PRODUCT_EXTRAFIELD_FIELD__' Done dinamically just after ); // Create dynamic tags for __PRODUCT_EXTRAFIELD_FIELD__ if (!empty($line->fk_product)) { $extrafields = new ExtraFields($this->db); $extralabels = $extrafields->fetch_name_optionals_label('product', true); $product = new Product($this->db); $product->fetch($line->fk_product, '', '', 1); $product->fetch_optionals($product->id, $extralabels); foreach ($extrafields->attribute_label as $key => $label) { $substit_line['__PRODUCT_EXTRAFIELD_' . strtoupper($key) . '__'] = $product->array_options['options_' . $key]; } } $this->substit_lines[] = $substit_line; } } } /** * Get list of substition keys available. * * @param string $mode 'formemail', 'formemailwithlines', 'formemailforlines', 'emailing', ... * @return void */ static function getAvailableSubstitKey($mode='formemail') { global $conf, $langs; $vars=array(); if ($mode == 'formemail' || $mode == 'formemailwithlines' || $mode == 'formemailforlines') { $vars=array( '__REF__', '__REFCLIENT__', '__REFSUPPLIER__', '__THIRDPARTY_ID__', '__THIRDPARTY_NAME__', '__PROJECT_ID__', '__PROJECT_REF__', '__PROJECT_NAME__', '__CONTACTCIVNAME__', '__AMOUNT__', '__AMOUNT_WO_TAX__', '__AMOUNT_VAT__', '__PERSONALIZED__', // Paypal link will be added here in form mode '__SIGNATURE__', ); if ($mode == 'formwithlines') { $vars[] = '__LINES__'; // Will be set by the get_form function } if ($mode == 'formforlines') { $vars[] = '__QUANTITY__'; // Will be set by the get_form function } } if ($mode == 'emailing') { // For mass emailing, we have different keys $vars=array( '__ID__' => 'IdRecord', '__EMAIL__' => 'EMailRecipient', '__LASTNAME__' => 'Lastname', '__FIRSTNAME__' => 'Firstname', '__MAILTOEMAIL__' => 'TagMailtoEmail', '__OTHER1__' => 'Other1', '__OTHER2__' => 'Other2', '__OTHER3__' => 'Other3', '__OTHER4__' => 'Other4', '__OTHER5__' => 'Other5', '__SIGNATURE__' => 'TagSignature', '__CHECK_READ__' => 'TagCheckMail', '__UNSUBSCRIBE__' => 'TagUnsubscribe' //,'__PERSONALIZED__' => 'Personalized' // Hidden because not used yet in mass emailing ); if (! empty($conf->paypal->enabled) && ! empty($conf->global->PAYPAL_SECURITY_TOKEN)) { $vars['__SECUREKEYPAYPAL__']='SecureKeyPaypal'; if (! empty($conf->global->PAYPAL_SECURITY_TOKEN_UNIQUE)) { if ($conf->adherent->enabled) $vars['__SECUREKEYPAYPAL_MEMBER__']='SecureKeyPaypalUniquePerMember'; if ($conf->facture->enabled) $vars['__SECUREKEYPAYPAL_INVOICE__']='SecureKeyPaypalUniquePerInvoice'; if ($conf->commande->enabled) $vars['__SECUREKEYPAYPAL_ORDER__']='SecureKeyPaypalUniquePerOrder'; if ($conf->contrat->enabled) $vars['__SECUREKEYPAYPAL_CONTRACTLINE__']='SecureKeyPaypalUniquePerContractLine'; } } else { $vars['__SECUREKEYPAYPAL__']=''; $vars['__SECUREKEYPAYPAL_MEMBER__']=''; } } $tmparray=array(); $parameters=array('mode'=>$mode); complete_substitutions_array($tmparray, $langs, null, $parameters); foreach($tmparray as $key => $val) { $vars[$key]=$key; } return $vars; } } /** * ModelMail */ class ModelMail { public $id; public $label; public $topic; public $content; public $content_lines; public $lang; }