* Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2010-2011 Juanjo Menent * * 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 { 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 $withsubstit; // Show substitution array var $withfrom; var $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 $param=array(); var $error; /** * 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 (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/'; if (is_dir($upload_dir)) dol_delete_dir_recursive($upload_dir); unset($_SESSION["listofpaths"]); unset($_SESSION["listofnames"]); unset($_SESSION["listofmimes"]); } /** * 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(); if (! empty($_SESSION["listofpaths"])) $listofpaths=explode(';',$_SESSION["listofpaths"]); if (! empty($_SESSION["listofnames"])) $listofnames=explode(';',$_SESSION["listofnames"]); if (! empty($_SESSION["listofmimes"])) $listofmimes=explode(';',$_SESSION["listofmimes"]); if (! in_array($file,$listofnames)) { $listofpaths[]=$path; $listofnames[]=$file; $listofmimes[]=$type; $_SESSION["listofpaths"]=join(';',$listofpaths); $_SESSION["listofnames"]=join(';',$listofnames); $_SESSION["listofmimes"]=join(';',$listofmimes); } } /** * Remove a file from the list of attached files (stored in SECTION array) * * @param string $keytodelete Key in file array * @return void */ function remove_attached_files($keytodelete) { $listofpaths=array(); $listofnames=array(); $listofmimes=array(); if (! empty($_SESSION["listofpaths"])) $listofpaths=explode(';',$_SESSION["listofpaths"]); if (! empty($_SESSION["listofnames"])) $listofnames=explode(';',$_SESSION["listofnames"]); if (! empty($_SESSION["listofmimes"])) $listofmimes=explode(';',$_SESSION["listofmimes"]); if ($keytodelete >= 0) { unset ($listofpaths[$keytodelete]); unset ($listofnames[$keytodelete]); unset ($listofmimes[$keytodelete]); $_SESSION["listofpaths"]=join(';',$listofpaths); $_SESSION["listofnames"]=join(';',$listofnames); $_SESSION["listofmimes"]=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(); if (! empty($_SESSION["listofpaths"])) $listofpaths=explode(';',$_SESSION["listofpaths"]); if (! empty($_SESSION["listofnames"])) $listofnames=explode(';',$_SESSION["listofnames"]); if (! empty($_SESSION["listofmimes"])) $listofmimes=explode(';',$_SESSION["listofmimes"]); 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 * * @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 ); $reshook=$hookmanager->executeHooks('getFormMail', $parameters, $this); if (!empty($reshook)) { return $hookmanager->resPrint; } else { $out=''; // Define list of attached files $listofpaths=array(); $listofnames=array(); $listofmimes=array(); if (! empty($_SESSION["listofpaths"])) $listofpaths=explode(';',$_SESSION["listofpaths"]); if (! empty($_SESSION["listofnames"])) $listofnames=explode(';',$_SESSION["listofnames"]); if (! empty($_SESSION["listofmimes"])) $listofmimes=explode(';',$_SESSION["listofmimes"]); $out.= "\n\n"; if ($this->withform == 1) { $out.= '
'."\n"; $out.= ''; } foreach ($this->param as $key=>$value) { $out.= ''."\n"; } $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)) { $this->withtopic=make_substitutions($this->withtopic,$this->substit); $out.= ''; $out.= ''; $out.= '\n"; } // Attached files if (! empty($this->withfile)) { $out.= ''; $out.= ''; $out.= '\n"; } // Message if (! empty($this->withbody)) { $defaultmessage=""; // TODO A partir du type, proposer liste de messages dans table llx_c_email_template if ($this->param["models"]=='facture_send') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendInvoice"); } elseif ($this->param["models"]=='facture_relance') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendInvoiceReminder"); } elseif ($this->param["models"]=='propal_send') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendProposal"); } elseif ($this->param["models"]=='order_send') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendOrder"); } elseif ($this->param["models"]=='order_supplier_send') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendSupplierOrder"); } elseif ($this->param["models"]=='invoice_supplier_send') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendSupplierInvoice"); } elseif ($this->param["models"]=='shipping_send') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendShipping"); } elseif ($this->param["models"]=='fichinter_send') { $defaultmessage=$langs->transnoentities("PredefinedMailContentSendFichInter"); } elseif ($this->param["models"]=='thirdparty') { $defaultmessage=$langs->transnoentities("PredefinedMailContentThirdparty"); } 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'); if ($this->param["models"]=='order_send') { $url=getPaypalPaymentUrl(0,'order',$this->substit['__ORDERREF__']); $this->substit['__PERSONALIZED__']=str_replace('\n',"\n",$langs->transnoentitiesnoconv("PredefinedMailContentLink",$url)); } if ($this->param["models"]=='facture_send') { $url=getPaypalPaymentUrl(0,'invoice',$this->substit['__FACREF__']); $this->substit['__PERSONALIZED__']=str_replace('\n',"\n",$langs->transnoentitiesnoconv("PredefinedMailContentLink",$url)); } } $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"])) $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"; } if ($this->withform == 1 || $this->withform == -1) { $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) { $langs->load("users"); $fuser=new User($this->db); $fuser->fetch($this->fromid); $out.= $fuser->getNomUrl(1); } else { $out.= $this->fromname; } if ($this->frommail) { $out.= " <".$this->frommail.">"; } else { if ($this->fromtype) { $langs->load("errors"); $out.= ' <'.$langs->trans("ErrorNoMailDefinedForThisUser").'> '; } } $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")." "; $out.= $form->selectarray("receiver", $this->withto, GETPOST("receiver"), 1); } if (isset($this->withtosocid) && $this->withtosocid > 0) // deprecated. TODO Remove this. Instead, fill withto with array before calling method. { $liste=array(); $soc=new Societe($this->db); $soc->fetch($this->withtosocid); foreach ($soc->thirdparty_and_contact_email_array(1) as $key=>$value) { $liste[$key]=$value; } if ($this->withtofree) $out.= " ".$langs->trans("or")." "; $out.= $form->selectarray("receiver", $liste, GETPOST("receiver"), 1); } } $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")." "; $out.= $form->selectarray("receivercc", $this->withtocc, GETPOST("receivercc"), 1); } } $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")." "; $out.= $form->selectarray("receiverccc", $this->withtoccc, GETPOST("receiverccc"), 1); } } //if (! empty($conf->global->MAIN_MAIL_AUTOCOPY_TO)) print ' '.info_admin("+ ".$conf->global->MAIN_MAIL_AUTOCOPY_TO,1); $out.= "
'.$langs->trans("DeliveryReceipt").''; if (! empty($this->withdeliveryreceiptreadonly)) { $out.= yn($this->withdeliveryreceipt); } else { $out.= $form->selectyesno('deliveryreceipt', (isset($_POST["deliveryreceipt"])?$_POST["deliveryreceipt"]:0), 1); } $out.= "
'.$langs->trans("MailTopic").''; if ($this->withtopicreadonly) { $out.= $this->withtopic; $out.= ''; } else { $out.= 'withtopic)?'':$this->withtopic)) .'" />'; } $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 { $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,72); $out.= $doleditor->Create(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"; $out.= "\n"; return $out; } } }