12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424 |
- <?php
- /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
- * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
- * Copyright (C) 2015-2017 Marcos García <marcosgdf@gmail.com>
- * Copyright (C) 2015-2017 Nicolas ZABOURI <info@inovea-conseil.com>
- *
- * 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 <http://www.gnu.org/licenses/>.
- */
- /**
- * \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 DoliDB Database handler.
- */
- public $db;
- public $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
- public $fromname;
- public $frommail;
- public $replytoname;
- public $replytomail;
- public $toname;
- public $tomail;
- public $trackid;
- public $withsubstit; // Show substitution array
- public $withfrom;
- /**
- * @var int
- * @deprecated Fill withto with array before calling method.
- * @see withto
- */
- public $withtosocid;
- /**
- * @var int|int[]
- */
- public $withto; // Show recipient emails
- public $withtofree; // Show free text for recipient emails
- public $withtocc;
- public $withtoccc;
- public $withtopic;
- public $withfile; // 0=No attaches files, 1=Show attached files, 2=Can add new attached files
- public $withmaindocfile; // 1=Add a checkbox "Attach also main document" for mass actions (checked by default), -1=Add checkbox (not checked by default)
- public $withbody;
- public $withfromreadonly;
- public $withreplytoreadonly;
- public $withtoreadonly;
- public $withtoccreadonly;
- public $withtocccreadonly;
- public $withtopicreadonly;
- public $withfilereadonly;
- public $withdeliveryreceipt;
- public $withcancel;
- public $withfckeditor;
- public $substit=array();
- public $substit_lines=array();
- public $param=array();
- public $withtouser=array();
- public $withtoccuser=array();
- /**
- * @var string Error code (or message)
- */
- public $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; // 1=Add section "Attached files". 2=Can add files.
- $this->withmaindocfile=0; // 1=Add a checkbox "Attach also main document" for mass actions (checked by default), -1=Add checkbox (not checked by default)
- $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
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
- /**
- * Clear list of attached files in send mail form (also stored in session)
- *
- * @return void
- */
- function clear_attached_files()
- {
- // phpcs:enable
- 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]);
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
- /**
- * 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 (can be basename($path))
- * @param string $type Mime type (can be dol_mimetype($file))
- * @return void
- */
- function add_attached_files($path, $file='', $type='')
- {
- // phpcs:enable
- $listofpaths=array();
- $listofnames=array();
- $listofmimes=array();
- if (empty($file)) $file=basename($path);
- if (empty($type)) $type=dol_mimetype($file);
- $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);
- }
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
- /**
- * 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)
- {
- // phpcs:enable
- $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']);
- }
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
- /**
- * Return list of attached files (stored in SECTION array)
- *
- * @return array array('paths'=> ,'names'=>, 'mimes'=> )
- */
- function get_attached_files()
- {
- // phpcs:enable
- $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);
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
- /**
- * Show the form to input an email
- * this->withfile: 0=No attaches files, 1=Show attached files, 2=Can add new attached files
- * this->withmaindocfile
- *
- * @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')
- {
- // phpcs:enable
- print $this->get_form($addfileaction,$removefileaction);
- }
- // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
- /**
- * Get the form to input an email
- * this->withfile: 0=No attaches files, 1=Show attached files, 2=Can add new attached files
- * this->withfile
- * this->param: Contains more parameters 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')
- {
- // phpcs:enable
- global $conf, $langs, $user, $hookmanager, $form;
- if (! is_object($form)) $form=new Form($this->db);
- // Load translation files required by the page
- $langs->loadLangs(array('other', 'mails'));
- // Clear temp files. Must be done at beginning, before call of triggers
- if (GETPOST('mode','alpha') == 'init' || (GETPOST('modelmailselected','alpha') && GETPOST('modelmailselected','alpha') != '-1'))
- {
- $this->clear_attached_files();
- }
- // Call hook getFormMail
- $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 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 for $this->param["models"] into c_email_templates
- $arraydefaultmessage = -1;
- if ($this->param['models'] != 'none')
- {
- $model_id=0;
- if (array_key_exists('models_id',$this->param))
- {
- $model_id=$this->param["models_id"];
- }
- // 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();
- $listofnames=array();
- $listofmimes=array();
- $keytoavoidconflict = empty($this->trackid)?'':'-'.$this->trackid; // this->trackid must be defined
- if (GETPOST('mode','alpha') == 'init' || (GETPOST('modelmailselected','alpha') && GETPOST('modelmailselected','alpha') != '-1'))
- {
- if (! empty($arraydefaultmessage->joinfiles) && is_array($this->param['fileinit']))
- {
- foreach($this->param['fileinit'] as $file)
- {
- $this->add_attached_files($file, basename($file), dol_mimetype($file));
- }
- }
- }
- 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]);
- $out.= "\n".'<!-- Begin form mail type='.$this->param["models"].' --><div id="mailformdiv"></div>'."\n";
- if ($this->withform == 1)
- {
- $out.= '<form method="POST" name="mailform" id="mailform" enctype="multipart/form-data" action="'.$this->param["returnurl"].'#formmail">'."\n";
- $out.= '<a id="formmail" name="formmail"></a>';
- $out.= '<input style="display:none" type="submit" id="sendmail" name="sendmail">';
- $out.= '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'" />';
- $out.= '<input type="hidden" name="trackid" value="'.$this->trackid.'" />';
- }
- if (! empty($this->withfrom))
- {
- if (! empty($this->withfromreadonly))
- {
- $out.= '<input type="hidden" id="fromname" name="fromname" value="'.$this->fromname.'" />';
- $out.= '<input type="hidden" id="frommail" name="frommail" value="'.$this->frommail.'" />';
- }
- }
- foreach ($this->param as $key=>$value)
- {
- $out.= '<input type="hidden" id="'.$key.'" name="'.$key.'" value="'.$value.'" />'."\n";
- }
- $modelmail_array=array();
- if ($this->param['models'] != 'none')
- {
- $result = $this->fetchAllEMailTemplate($this->param["models"], $user, $outputlangs);
- if ($result < 0)
- {
- setEventMessages($this->error, $this->errors, 'errors');
- }
- foreach($this->lines_model as $line)
- {
- $langs->trans("members");
- if (preg_match('/\((.*)\)/', $line->label, $reg))
- {
- $modelmail_array[$line->id]=$langs->trans($reg[1]); // langs->trans when label is __(xxx)__
- }
- else
- {
- $modelmail_array[$line->id]=$line->label;
- }
- if ($line->lang) $modelmail_array[$line->id].=' ('.$line->lang.')';
- if ($line->private) $modelmail_array[$line->id].=' - '.$langs->trans("Private");
- //if ($line->fk_user != $user->id) $modelmail_array[$line->id].=' - '.$langs->trans("By").' ';
- }
- }
- // Zone to select email template
- if (count($modelmail_array)>0)
- {
- // If list of template is filled
- $out.= '<div class="center" style="padding: 0px 0 12px 0">'."\n";
- $out.= '<span class="opacitymedium">'.$langs->trans('SelectMailModel').':</span> '.$this->selectarray('modelmailselected', $modelmail_array, 0, 1, 0, 0, '', 0, 0, 0, '', 'minwidth100');
- if ($user->admin) $out.= info_admin($langs->trans("YouCanChangeValuesForThisListFrom", $langs->transnoentitiesnoconv('Setup').' - '.$langs->transnoentitiesnoconv('EMails')),1);
- $out.= ' ';
- $out.= '<input class="button" type="submit" value="'.$langs->trans('Apply').'" name="modelselected" id="modelselected">';
- $out.= ' ';
- $out.= '</div>';
- }
- 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','contract','user','all'
- )))
- {
- // If list of template is empty
- $out.= '<div class="center" style="padding: 0px 0 12px 0">'."\n";
- $out.= $langs->trans('SelectMailModel').': <select name="modelmailselected" disabled="disabled"><option value="none">'.$langs->trans("NoTemplateDefined").'</option></select>'; // Do not put 'disabled' on 'option' tag, 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.= '<input class="button" type="submit" value="'.$langs->trans('Apply').'" name="modelselected" disabled="disabled" id="modelselected">';
- $out.= ' ';
- $out.= '</div>';
- }
- $out.= '<table class="tableforemailform boxtablenotop" width="100%">'."\n";
- // Substitution array/string
- $helpforsubstitution='';
- if (is_array($this->substit) && count($this->substit)) $helpforsubstitution.=$langs->trans('AvailableVariables').' :<br>'."\n";
- foreach($this->substit as $key => $val)
- {
- $helpforsubstitution.=$key.' -> '.$langs->trans(dol_string_nohtmltag($val)).'<br>';
- }
- if (! empty($this->withsubstit)) // Unset or set ->withsubstit=0 to disable this.
- {
- $out.= '<tr><td colspan="2" align="right">';
- //$out.='<div class="floatright">';
- if (is_numeric($this->withsubstit)) $out.= $form->textwithpicto($langs->trans("EMailTestSubstitutionReplacedByGenericValues"), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltip'); // Old usage
- else $out.= $form->textwithpicto($langs->trans('AvailableVariables'), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltip'); // New usage
- $out.= "</td></tr>\n";
- //$out.='</div>';
- }
- /*var_dump(! empty($this->withfromreadonly));
- var_dump($this->withfrom);
- var_dump($this->fromtype);
- var_dump($this->fromname);*/
- // From
- if (! empty($this->withfrom))
- {
- if (! empty($this->withfromreadonly))
- {
- $out.= '<tr><td class="fieldrequired minwidth200">'.$langs->trans("MailFrom").'</td><td>';
- // $this->fromtype is the default value to use to select sender
- if (! ($this->fromtype === 'user' && $this->fromid > 0)
- && ! ($this->fromtype === 'company')
- && ! ($this->fromtype === 'robot')
- && ! preg_match('/user_aliases/', $this->fromtype)
- && ! preg_match('/global_aliases/', $this->fromtype)
- && ! preg_match('/senderprofile/', $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.= '<span class="warning"> <'.$langs->trans('ErrorNoMailDefinedForThisUser').'> </span>';
- }
- }
- } else {
- $liste = array();
- // Add user email
- if (empty($user->email))
- {
- $langs->load('errors');
- $liste['user'] = $user->getFullName($langs) . ' <'.$langs->trans('ErrorNoMailDefinedForThisUser').'>';
- }
- else
- {
- $liste['user'] = $user->getFullName($langs) .' <'.$user->email.'>';
- }
- // Add also company main email
- $liste['company'] = $conf->global->MAIN_INFO_SOCIETE_NOM .' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
- // Add also email aliases if there is some
- $listaliases=array('user_aliases'=>$user->email_aliases, 'global_aliases'=>$conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES);
- // Also add robot email
- if (! empty($this->fromalsorobot))
- {
- if (! empty($conf->global->MAIN_MAIL_EMAIL_FROM) && $conf->global->MAIN_MAIL_EMAIL_FROM != $conf->global->MAIN_INFO_SOCIETE_MAIL)
- {
- $liste['robot'] = $conf->global->MAIN_MAIL_EMAIL_FROM;
- if ($this->frommail)
- {
- $liste['robot'] .= ' <'.$conf->global->MAIN_MAIL_EMAIL_FROM.'>';
- }
- }
- }
- // Add also email aliases from the c_email_senderprofile table
- $sql='SELECT rowid, label, email FROM '.MAIN_DB_PREFIX.'c_email_senderprofile WHERE active = 1 ORDER BY position';
- $resql = $this->db->query($sql);
- if ($resql)
- {
- $num = $this->db->num_rows($resql);
- $i=0;
- while($i < $num)
- {
- $obj = $this->db->fetch_object($resql);
- if ($obj)
- {
- $listaliases['senderprofile_'.$obj->rowid] = $obj->label.' <'.$obj->email.'>';
- }
- $i++;
- }
- }
- else dol_print_error($this->db);
- foreach($listaliases as $typealias => $listalias)
- {
- $posalias=0;
- $listaliasarray=explode(',', $listalias);
- foreach ($listaliasarray as $listaliasval)
- {
- $posalias++;
- $listaliasval=trim($listaliasval);
- if ($listaliasval)
- {
- $listaliasval=preg_replace('/</', '<', $listaliasval);
- $listaliasval=preg_replace('/>/', '>', $listaliasval);
- if (! preg_match('/</', $listaliasval)) $listaliasval='<'.$listaliasval.'>';
- $liste[$typealias.'_'.$posalias]=$listaliasval;
- }
- }
- }
- // Set the default "From"
- $defaultfrom='';
- $reshook=$hookmanager->executeHooks('getDefaultFromEmail', $parameters, $this);
- if (empty($reshook))
- {
- $defaultfrom = $this->fromtype;
- }
- if (! empty($hookmanager->resArray['defaultfrom'])) $defaultfrom=$hookmanager->resArray['defaultfrom'];
- // Using combo here make the '<email>' no more visible on list.
- //$out.= ' '.$form->selectarray('fromtype', $liste, $this->fromtype, 0, 0, 0, '', 0, 0, 0, '', 'fromforsendingprofile maxwidth200onsmartphone', 1, '', $disablebademails);
- $out.= ' '.$form->selectarray('fromtype', $liste, $defaultfrom, 0, 0, 0, '', 0, 0, 0, '', 'fromforsendingprofile maxwidth200onsmartphone', 0, '', $disablebademails);
- }
- $out.= "</td></tr>\n";
- }
- else
- {
- $out.= '<tr><td class="fieldrequired width200">'.$langs->trans("MailFrom")."</td><td>";
- $out.= $langs->trans("Name").':<input type="text" id="fromname" name="fromname" class="maxwidth200onsmartphone" value="'.$this->fromname.'" />';
- $out.= ' ';
- $out.= $langs->trans("EMail").':<<input type="text" id="frommail" name="frommail" class="maxwidth200onsmartphone" value="'.$this->frommail.'" />>';
- $out.= "</td></tr>\n";
- }
- }
- // To
- if (! empty($this->withto) || is_array($this->withto))
- {
- $out.= '<tr><td class="fieldrequired">';
- if ($this->withtofree) $out.= $form->textwithpicto($langs->trans("MailTo"),$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients"));
- else $out.= $langs->trans("MailTo");
- $out.= '</td><td>';
- if ($this->withtoreadonly)
- {
- if (! empty($this->toname) && ! empty($this->tomail))
- {
- $out.= '<input type="hidden" id="toname" name="toname" value="'.$this->toname.'" />';
- $out.= '<input type="hidden" id="tomail" name="tomail" value="'.$this->tomail.'" />';
- 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.= '<br>'.$langs->trans("and").' <input class="minwidth200" id="sendto" name="sendto" value="'.(! is_array($this->withto) && ! is_numeric($this->withto)? (isset($_REQUEST["sendto"])?$_REQUEST["sendto"]:$this->withto) :"").'" />';
- }
- }
- else
- {
- // Note withto may be a text like 'AllRecipientSelected'
- $out.= (! is_array($this->withto) && ! is_numeric($this->withto))?$this->withto:"";
- }
- }
- else
- {
- if (! empty($this->withtofree))
- {
- $out.= '<input class="minwidth200" id="sendto" name="sendto" value="'.(! is_array($this->withto) && ! is_numeric($this->withto)? (isset($_REQUEST["sendto"])?$_REQUEST["sendto"]:$this->withto) :"").'" />';
- }
- if (! empty($this->withto) && is_array($this->withto))
- {
- if (! empty($this->withtofree)) $out.= " ".$langs->trans("and")."/".$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);
- }
- $withtoselected=GETPOST("receiver",'none'); // Array of selected value
- if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action','aZ09') == 'presend')
- {
- $withtoselected = array_keys($tmparray);
- }
- $out.= $form->multiselectarray("receiver", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', null, "");
- }
- }
- $out.= "</td></tr>\n";
- }
- // To User
- if (! empty($this->withtouser) && is_array($this->withtouser) && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT))
- {
- $out.= '<tr><td>';
- $out.= $langs->trans("MailToUsers");
- $out.= '</td><td>';
- // multiselect array convert html entities into options tags, even if we dont want this, so we encode them a second time
- $tmparray = $this->withtouser;
- foreach($tmparray as $key => $val)
- {
- $tmparray[$key]=dol_htmlentities($tmparray[$key], null, 'UTF-8', true);
- }
- $withtoselected=GETPOST("receiveruser",'none'); // Array of selected value
- if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action','aZ09') == 'presend')
- {
- $withtoselected = array_keys($tmparray);
- }
- $out.= $form->multiselectarray("receiveruser", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', null, "");
- $out.= "</td></tr>\n";
- }
- // withoptiononeemailperrecipient
- if (! empty($this->withoptiononeemailperrecipient))
- {
- $out.= '<tr><td class="minwidth200">';
- $out.= $langs->trans("GroupEmails");
- $out.= '</td><td>';
- $out.=' <input type="checkbox" name="oneemailperrecipient"'.($this->withoptiononeemailperrecipient > 0?' checked="checked"':'').'> ';
- $out.= $langs->trans("OneEmailPerRecipient");
- $out.='<span class="hideonsmartphone">';
- $out.=' - ';
- $out.= $langs->trans("WarningIfYouCheckOneRecipientPerEmail");
- $out.='</span>';
- $out.= '</td></tr>';
- }
- // CC
- if (! empty($this->withtocc) || is_array($this->withtocc))
- {
- $out.= '<tr><td>';
- $out.= $form->textwithpicto($langs->trans("MailCC"),$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients"));
- $out.= '</td><td>';
- if ($this->withtoccreadonly)
- {
- $out.= (! is_array($this->withtocc) && ! is_numeric($this->withtocc))?$this->withtocc:"";
- }
- else
- {
- $out.= '<input class="minwidth200" id="sendtocc" name="sendtocc" value="'.((! is_array($this->withtocc) && ! is_numeric($this->withtocc))? (isset($_POST["sendtocc"])?$_POST["sendtocc"]:$this->withtocc) : (isset($_POST["sendtocc"])?$_POST["sendtocc"]:"") ).'" />';
- if (! empty($this->withtocc) && is_array($this->withtocc))
- {
- $out.= " ".$langs->trans("and")."/".$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);
- }
- $withtoccselected=GETPOST("receivercc"); // Array of selected value
- $out.= $form->multiselectarray("receivercc", $tmparray, $withtoccselected, null, null, 'inline-block minwidth500',null, "");
- }
- }
- $out.= "</td></tr>\n";
- }
- // To User cc
- if (! empty($this->withtoccuser) && is_array($this->withtoccuser) && !empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT))
- {
- $out.= '<tr><td>';
- $out.= $langs->trans("MailToCCUsers");
- $out.= '</td><td>';
- // multiselect array convert html entities into options tags, even if we dont want this, so we encode them a second time
- $tmparray = $this->withtoccuser;
- foreach($tmparray as $key => $val)
- {
- $tmparray[$key]=dol_htmlentities($tmparray[$key], null, 'UTF-8', true);
- }
- $withtoselected=GETPOST("receiverccuser",'none'); // Array of selected value
- if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action','aZ09') == 'presend')
- {
- $withtoselected = array_keys($tmparray);
- }
- $out.= $form->multiselectarray("receiverccuser", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', null, "");
- $out.= "</td></tr>\n";
- }
- // CCC
- if (! empty($this->withtoccc) || is_array($this->withtoccc))
- {
- $out.= '<tr><td>';
- $out.= $form->textwithpicto($langs->trans("MailCCC"),$langs->trans("YouCanUseCommaSeparatorForSeveralRecipients"));
- $out.= '</td><td>';
- if (! empty($this->withtocccreadonly))
- {
- $out.= (! is_array($this->withtoccc) && ! is_numeric($this->withtoccc))?$this->withtoccc:"";
- }
- else
- {
- $out.= '<input class="minwidth200" id="sendtoccc" name="sendtoccc" value="'.((! is_array($this->withtoccc) && ! is_numeric($this->withtoccc))? (isset($_POST["sendtoccc"])?$_POST["sendtoccc"]:$this->withtoccc) : (isset($_POST["sendtoccc"])?$_POST["sendtoccc"]:"") ).'" />';
- if (! empty($this->withtoccc) && is_array($this->withtoccc))
- {
- $out.= " ".$langs->trans("and")."/".$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);
- }
- $withtocccselected=GETPOST("receiverccc"); // Array of selected value
- $out.= $form->multiselectarray("receiverccc", $tmparray, $withtocccselected, 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.= "</td></tr>\n";
- }
- // Replyto
- if (! empty($this->withreplyto))
- {
- if ($this->withreplytoreadonly)
- {
- $out.= '<input type="hidden" id="replyname" name="replyname" value="'.$this->replytoname.'" />';
- $out.= '<input type="hidden" id="replymail" name="replymail" value="'.$this->replytomail.'" />';
- $out.= "<tr><td>".$langs->trans("MailReply")."</td><td>".$this->replytoname.($this->replytomail?(" <".$this->replytomail.">"):"");
- $out.= "</td></tr>\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.= '<input type="hidden" id="errorstomail" name="errorstomail" value="'.$errorstomail.'" />';
- $out.= '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td>';
- $out.= $errorstomail;
- $out.= "</td></tr>\n";
- }
- else
- {
- $out.= '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td>';
- $out.= '<input size="30" id="errorstomail" name="errorstomail" value="'.$errorstomail.'" />';
- $out.= "</td></tr>\n";
- }
- }
- // Ask delivery receipt
- if (! empty($this->withdeliveryreceipt))
- {
- $out.= '<tr><td>'.$langs->trans("DeliveryReceipt").'</td><td>';
- 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.= "</td></tr>\n";
- }
- // Topic
- if (! empty($this->withtopic))
- {
- $defaulttopic=GETPOST('subject','none');
- if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1')
- {
- if ($arraydefaultmessage && $arraydefaultmessage->topic) {
- $defaulttopic = $arraydefaultmessage->topic;
- } elseif (! is_numeric($this->withtopic)) {
- $defaulttopic = $this->withtopic;
- }
- }
- $defaulttopic=make_substitutions($defaulttopic,$this->substit);
- $out.= '<tr>';
- $out.= '<td class="fieldrequired">';
- $out.=$form->textwithpicto($langs->trans('MailTopic'), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltipfromtopic');
- $out.='</td>';
- $out.= '<td>';
- if ($this->withtopicreadonly)
- {
- $out.= $defaulttopic;
- $out.= '<input type="hidden" class="quatrevingtpercent" id="subject" name="subject" value="'.$defaulttopic.'" />';
- }
- else
- {
- $out.= '<input type="text" class="quatrevingtpercent" id="subject" name="subject" value="'. ((isset($_POST["subject"]) && ! $_POST['modelselected'])?$_POST["subject"]:($defaulttopic?$defaulttopic:'')) .'" />';
- }
- $out.= "</td></tr>\n";
- }
- // Attached files
- if (! empty($this->withfile))
- {
- $out.= '<tr>';
- $out.= '<td>'.$langs->trans("MailFile").'</td>';
- $out.= '<td>';
- if (! empty($this->withmaindocfile))
- {
- if ($this->withmaindocfile == 1)
- {
- $out.='<input type="checkbox" name="addmaindocfile" value="1" />';
- }
- if ($this->withmaindocfile == -1)
- {
- $out.='<input type="checkbox" name="addmaindocfile" checked="checked" />';
- }
- $out.=' '.$langs->trans("JoinMainDoc").'.<br>';
- }
- if (is_numeric($this->withfile))
- {
- // TODO Trick to have param removedfile containing nb of file to delete. But this does not works without javascript
- $out.= '<input type="hidden" class="removedfilehidden" name="removedfile" value="">'."\n";
- $out.= '<script type="text/javascript" language="javascript">';
- $out.= 'jQuery(document).ready(function () {';
- $out.= ' jQuery(".removedfile").click(function() {';
- $out.= ' jQuery(".removedfilehidden").val(jQuery(this).val());';
- $out.= ' });';
- $out.= '})';
- $out.= '</script>'."\n";
- if (count($listofpaths))
- {
- foreach($listofpaths as $key => $val)
- {
- $out.= '<div id="attachfile_'.$key.'">';
- $out.= img_mime($listofnames[$key]).' '.$listofnames[$key];
- if (! $this->withfilereadonly)
- {
- $out.= ' <input type="image" style="border: 0px;" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/delete.png" value="'.($key+1).'" class="removedfile" id="removedfile_'.$key.'" name="removedfile_'.$key.'" />';
- //$out.= ' <a href="'.$_SERVER["PHP_SELF"].'?removedfile='.($key+1).' id="removedfile_'.$key.'">'.img_delete($langs->trans("Delete").'</a>';
- }
- $out.= '<br></div>';
- }
- }
- else if (empty($this->withmaindocfile)) // Do not show message if we asked to show the checkbox
- {
- $out.= $langs->trans("NoAttachedFiles").'<br>';
- }
- if ($this->withfile == 2) // Can add other files
- {
- if (!empty($conf->global->FROM_MAIL_USE_INPUT_FILE_MULTIPLE)) $out.= '<input type="file" class="flat" id="addedfile" name="addedfile[]" value="'.$langs->trans("Upload").'" multiple />';
- else $out.= '<input type="file" class="flat" id="addedfile" name="addedfile" value="'.$langs->trans("Upload").'" />';
- $out.= ' ';
- $out.= '<input class="button" type="submit" id="'.$addfileaction.'" name="'.$addfileaction.'" value="'.$langs->trans("MailingAddFile").'" />';
- }
- }
- else
- {
- $out.=$this->withfile;
- }
- $out.= "</td></tr>\n";
- }
- // Message
- if (! empty($this->withbody))
- {
- $defaultmessage=GETPOST('message','none');
- if (! GETPOST('modelselected','alpha') || GETPOST('modelmailselected') != '-1')
- {
- if ($arraydefaultmessage && $arraydefaultmessage->content) {
- $defaultmessage = $arraydefaultmessage->content;
- } elseif (! is_numeric($this->withbody)) {
- $defaultmessage = $this->withbody;
- }
- }
- // Complete substitution array with the url to make online payment
- $paymenturl=''; $validpaymentmethod=array();
- if (empty($this->substit['__REF__']))
- {
- $paymenturl='';
- }
- else
- {
- // Set the online payment url link into __ONLINE_PAYMENT_URL__ key
- require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
- $langs->loadLangs(array('paypal','other'));
- $typeforonlinepayment='free';
- if ($this->param["models"]=='order' || $this->param["models"]=='order_send') $typeforonlinepayment='order'; // TODO use detection on something else than template
- if ($this->param["models"]=='invoice' || $this->param["models"]=='facture_send') $typeforonlinepayment='invoice'; // TODO use detection on something else than template
- if ($this->param["models"]=='member') $typeforonlinepayment='member'; // TODO use detection on something else than template
- $url=getOnlinePaymentUrl(0, $typeforonlinepayment, $this->substit['__REF__']);
- $paymenturl=$url;
- $validpaymentmethod = getValidOnlinePaymentMethods('');
- }
- if (count($validpaymentmethod) > 0 && $paymenturl)
- {
- $langs->load('other');
- $this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__']=str_replace('\n',"\n",$langs->transnoentities("PredefinedMailContentLink", $paymenturl));
- $this->substit['__ONLINE_PAYMENT_URL__']=$paymenturl;
- }
- else
- {
- $this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__']='';
- $this->substit['__ONLINE_PAYMENT_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['__USER_SIGNATURE__'])) {
- $this->substit['__USER_SIGNATURE__'] = dol_nl2br($this->substit['__USER_SIGNATURE__']);
- } else if(!dol_textishtml($defaultmessage) && dol_textishtml($this->substit['__USER_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("/^(<br>)+/","",$defaultmessage);
- $defaultmessage=preg_replace("/^\n+/","",$defaultmessage);
- }
- $out.= '<tr>';
- $out.= '<td valign="top">';
- $out.=$form->textwithpicto($langs->trans('MailText'), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltipfrombody');
- $out.='</td>';
- $out.= '<td>';
- if ($this->withbodyreadonly)
- {
- $out.= nl2br($defaultmessage);
- $out.= '<input type="hidden" id="message" name="message" value="'.$defaultmessage.'" />';
- }
- 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.= "</td></tr>\n";
- }
- $out.= '</table>'."\n";
- if ($this->withform == 1 || $this->withform == -1)
- {
- $out.= '<br><div class="center">';
- $out.= '<input class="button" type="submit" id="sendmail" name="sendmail" value="'.$langs->trans("SendMail").'"';
- // Add a javascript test to avoid to forget to submit file before sending email
- if ($this->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.= '<input class="button" type="submit" id="cancel" name="cancel" value="'.$langs->trans("Cancel").'" />';
- }
- $out.= '</div>'."\n";
- }
- if ($this->withform == 1) $out.= '</form>'."\n";
- // Disable enter key if option MAIN_MAILFORM_DISABLE_ENTERKEY is set
- if (! empty($conf->global->MAIN_MAILFORM_DISABLE_ENTERKEY))
- {
- $out.= '<script type="text/javascript" language="javascript">';
- $out.= 'jQuery(document).ready(function () {';
- $out.= ' $(document).on("keypress", \'#mailform\', function (e) { /* Note this is called at every key pressed ! */
- var code = e.keyCode || e.which;
- if (code == 13) {
- e.preventDefault();
- return false;
- }
- });';
- $out.=' })';
- $out.= '</script>';
- }
- $out.= "<!-- End form mail -->\n";
- return $out;
- }
- }
- /**
- * Return templates of email with type = $type_template or type = 'all'.
- * This search into table c_email_templates. Used by the get_form function.
- *
- * @param DoliDB $db Database handler
- * @param string $type_template Get message for model/type=$type_template, type='all' also included.
- * @param string $user Get template public or limited to this user
- * @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 first found whatever is position (priority order depends on lang provided or not) or -2 for exact match with label (no answer if not found)
- * @param int $active 1=Only active template, 0=Only disabled, -1=All
- * @param string $label Label of template
- * @return ModelMail One instance of ModelMail
- */
- public function getEMailTemplate($db, $type_template, $user, $outputlangs, $id=0, $active=1, $label='')
- {
- $ret = new ModelMail();
- if ($id == -2 && empty($label))
- {
- $this->error = 'LabelIsMandatoryWhenIdIs-2';
- return -1;
- }
- $sql = "SELECT rowid, label, topic, joinfiles, content, content_lines, lang";
- $sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates';
- $sql.= " WHERE (type_template='".$db->escape($type_template)."' OR type_template='all')";
- $sql.= " AND entity IN (".getEntity('c_email_templates').")";
- $sql.= " AND (private = 0 OR fk_user = ".$user->id.")"; // Get all public or private owned
- if ($active >= 0) $sql.=" AND active = ".$active;
- if ($label) $sql.=" AND label ='".$db->escape($label)."'";
- if (! ($id > 0) && is_object($outputlangs)) $sql.= " AND (lang = '".$db->escape($outputlangs->defaultlang)."' OR lang IS NULL OR lang = '')";
- if ($id > 0) $sql.= " AND rowid=".$id;
- if ($id == -1) $sql.= " AND position=0";
- if (is_object($outputlangs)) $sql.= $db->order("position,lang,label","ASC,DESC,ASC"); // We want line with lang set first, then with lang null or ''
- else $sql.= $db->order("position,lang,label","ASC,ASC,ASC"); // If no language provided, we give priority to lang not defined
- $sql.= $db->plimit(1);
- //print $sql;
- $resql = $db->query($sql);
- if ($resql)
- {
- // Get first found
- $obj = $db->fetch_object($resql);
- if ($obj) {
- $ret->id = $obj->rowid;
- $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;
- }
- elseif($id == -2) {
- // Not found with the provided label
- return -1;
- }
- else { // If there is no template at all
- $defaultmessage='';
- if ($type_template=='body') { $defaultmessage=$this->withbody; } // Special case to use this->withbody as content
- elseif ($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"); }
- elseif ($type_template=='user') { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContentUser"); }
- elseif (!empty($type_template)) { $defaultmessage=$outputlangs->transnoentities("PredefinedMailContent".ucfirst($type_template)); }
- $ret->label = 'default';
- $ret->lang = $outputlangs->defaultlang;
- $ret->topic = '';
- $ret->joinfiles = 1;
- $ret->content = $defaultmessage;
- $ret->content_lines ='';
- }
- $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 and are available for current user, then set them into $this->lines_module.
- * 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, fk_user, private, position";
- $sql.= " FROM ".MAIN_DB_PREFIX.'c_email_templates';
- $sql.= " WHERE type_template IN ('".$this->db->escape($type_template)."', 'all')";
- $sql.= " AND entity IN (".getEntity('c_email_templates').")";
- $sql.= " AND (private = 0 OR fk_user = ".$user->id.")"; // See all public templates or templates I own.
- if ($active >= 0) $sql.=" AND active = ".$active;
- //if (is_object($outputlangs)) $sql.= " AND (lang = '".$outputlangs->defaultlang."' OR lang IS NULL OR lang = '')"; // Return all languages
- $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->lang=$obj->lang;
- $line->fk_user=$obj->fk_user;
- $line->private=$obj->private;
- $line->position=$obj->position;
- $line->topic=$obj->topic;
- $line->content=$obj->content;
- $line->content_lines=$obj->content_lines;
- $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. This is call when suggesting the email template into forms before sending email.
- *
- * @param CommonObject $object Object to use
- * @param Translate $outputlangs Object lang
- * @return void
- * @see getCommonSubstitutionArray
- */
- function setSubstitFromObject($object, $outputlangs)
- {
- global $conf, $user;
- $parameters=array();
- $tmparray=getCommonSubstitutionArray($outputlangs, 0, null, $object);
- complete_substitutions_array($tmparray, $outputlangs, null, $parameters);
- $this->substit=$tmparray;
- // 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_EXCL_TAX__' => price($line->total_ht)
- );
- // Create dynamic tags for __PRODUCT_EXTRAFIELD_FIELD__
- if (!empty($line->fk_product))
- {
- if (! is_object($extrafields)) $extrafields = new ExtraFields($this->db);
- $extrafields->fetch_name_optionals_label('product', true);
- $product = new Product($this->db);
- $product->fetch($line->fk_product, '', '', 1);
- $product->fetch_optionals();
- if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label']) > 0)
- {
- foreach ($extrafields->attributes[$product->table_element]['label'] as $key => $label) {
- $substit_line['__PRODUCT_EXTRAFIELD_' . strtoupper($key) . '__'] = $product->array_options['options_' . $key];
- }
- }
- }
- $this->substit_lines[] = $substit_line;
- }
- }
- }
- /**
- * Get list of substitution keys available for emails. This is used for tooltips help.
- * This include the complete_substitutions_array.
- *
- * @param string $mode 'formemail', 'formemailwithlines', 'formemailforlines', 'emailing', ...
- * @param Object $object Object if applicable
- * @return array Array of substitution values for emails.
- */
- static function getAvailableSubstitKey($mode='formemail', $object=null)
- {
- global $conf, $langs;
- $tmparray=array();
- if ($mode == 'formemail' || $mode == 'formemailwithlines' || $mode == 'formemailforlines')
- {
- $parameters=array('mode'=>$mode);
- $tmparray=getCommonSubstitutionArray($langs, 2, null, $object); // Note: On email templated edition, this is null because it is related to all type of objects
- complete_substitutions_array($tmparray, $langs, null, $parameters);
- if ($mode == 'formwithlines')
- {
- $tmparray['__LINES__'] = '__LINES__'; // Will be set by the get_form function
- }
- if ($mode == 'formforlines')
- {
- $tmparray['__QUANTITY__'] = '__QUANTITY__'; // Will be set by the get_form function
- }
- }
- if ($mode == 'emailing')
- {
- $parameters=array('mode'=>$mode);
- $tmparray=getCommonSubstitutionArray($langs, 2, array('object','objectamount'), $object); // Note: On email templated edition, this is null because it is related to all type of objects
- complete_substitutions_array($tmparray, $langs, null, $parameters);
- // For mass emailing, we have different keys
- $tmparray['__ID__'] = 'IdRecord';
- $tmparray['__EMAIL__'] = 'EMailRecipient';
- $tmparray['__LASTNAME__'] = 'Lastname';
- $tmparray['__FIRSTNAME__'] = 'Firstname';
- $tmparray['__MAILTOEMAIL__'] = 'TagMailtoEmail';
- $tmparray['__OTHER1__'] = 'Other1';
- $tmparray['__OTHER2__'] = 'Other2';
- $tmparray['__OTHER3__'] = 'Other3';
- $tmparray['__OTHER4__'] = 'Other4';
- $tmparray['__OTHER5__'] = 'Other5';
- $tmparray['__USER_SIGNATURE__'] = 'TagSignature';
- $tmparray['__CHECK_READ__'] = 'TagCheckMail';
- $tmparray['__UNSUBSCRIBE__'] = 'TagUnsubscribe';
- //,'__PERSONALIZED__' => 'Personalized' // Hidden because not used yet in mass emailing
- $onlinepaymentenabled = 0;
- if (! empty($conf->paypal->enabled)) $onlinepaymentenabled++;
- if (! empty($conf->paybox->enabled)) $onlinepaymentenabled++;
- if (! empty($conf->stripe->enabled)) $onlinepaymentenabled++;
- if ($onlinepaymentenabled && ! empty($conf->global->PAYMENT_SECURITY_TOKEN))
- {
- $tmparray['__SECUREKEYPAYMENT__']=$conf->global->PAYMENT_SECURITY_TOKEN;
- if (! empty($conf->global->PAYMENT_SECURITY_TOKEN_UNIQUE))
- {
- if ($conf->adherent->enabled) $tmparray['__SECUREKEYPAYMENT_MEMBER__']='SecureKeyPAYMENTUniquePerMember';
- if ($conf->facture->enabled) $tmparray['__SECUREKEYPAYMENT_INVOICE__']='SecureKeyPAYMENTUniquePerInvoice';
- if ($conf->commande->enabled) $tmparray['__SECUREKEYPAYMENT_ORDER__']='SecureKeyPAYMENTUniquePerOrder';
- if ($conf->contrat->enabled) $tmparray['__SECUREKEYPAYMENT_CONTRACTLINE__']='SecureKeyPAYMENTUniquePerContractLine';
- }
- }
- else
- {
- /* No need to show into tooltip help, option is not enabled
- $vars['__SECUREKEYPAYMENT__']='';
- $vars['__SECUREKEYPAYMENT_MEMBER__']='';
- $vars['__SECUREKEYPAYMENT_INVOICE__']='';
- $vars['__SECUREKEYPAYMENT_ORDER__']='';
- $vars['__SECUREKEYPAYMENT_CONTRACTLINE__']='';
- */
- }
- }
- $tmparray['__(AnyTranslationKey)__']="Translation";
- foreach($tmparray as $key => $val)
- {
- if (empty($val)) $tmparray[$key]=$key;
- }
- return $tmparray;
- }
- }
- /**
- * ModelMail
- */
- class ModelMail
- {
- /**
- * @var int ID
- */
- public $id;
- /**
- * @var string Model mail label
- */
- public $label;
- public $topic;
- public $content;
- public $content_lines;
- public $lang;
- public $joinfiles;
- }
|