dolibarr  20.0.0-alpha
html.formmail.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2005-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
4  * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
5  * Copyright (C) 2015-2017 Marcos García <marcosgdf@gmail.com>
6  * Copyright (C) 2015-2017 Nicolas ZABOURI <info@inovea-conseil.com>
7  * Copyright (C) 2018-2022 Frédéric France <frederic.france@netlogic.fr>
8  * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
9  * Copyright (C) 2023 Anthony Berton <anthony.berton@bb2a.fr>
10  * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
11  *
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program. If not, see <https://www.gnu.org/licenses/>.
25  */
26 
32 require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
33 
34 
41 class FormMail extends Form
42 {
46  public $db;
47 
53  public $withform;
54 
58  public $fromname;
59 
63  public $frommail;
64 
68  public $fromtype;
69 
73  public $fromid;
74 
78  public $fromalsorobot;
79 
83  public $totype;
84 
88  public $toid;
89 
93  public $replytoname;
94 
98  public $replytomail;
99 
103  public $toname;
104 
108  public $tomail;
109 
113  public $trackid;
114 
118  public $inreplyto;
119 
120  public $withsubstit; // Show substitution array
121  public $withfrom;
122 
126  public $withto; // Show recipient emails
127  public $withreplyto;
128 
134  public $withtofree;
135  public $withtocc;
136  public $withtoccc;
137  public $withtopic;
138  public $witherrorsto;
139 
143  public $withfile;
144 
148  public $withlayout;
149 
153  public $withaiprompt;
154 
158  public $withmaindocfile;
159  public $withbody;
160 
161  public $withfromreadonly;
162  public $withreplytoreadonly;
163  public $withtoreadonly;
164  public $withtoccreadonly;
165  public $witherrorstoreadonly;
166  public $withtocccreadonly;
167  public $withtopicreadonly;
168  public $withbodyreadonly;
169  public $withfilereadonly;
170  public $withdeliveryreceipt;
171  public $withcancel;
172  public $withdeliveryreceiptreadonly;
173  public $withfckeditor;
174 
178  public $ckeditortoolbar;
179 
180  public $substit = array();
181  public $substit_lines = array();
185  public $param = array();
186 
187  public $withtouser = array();
188  public $withtoccuser = array();
189 
190  public $lines_model;
191 
192  // -1 suggest the checkbox 'one email per recipient' not checked, 0 = no suggestion, 1 = suggest and checked
193  public $withoptiononeemailperrecipient;
194 
195 
201  public function __construct($db)
202  {
203  $this->db = $db;
204 
205  $this->withform = 1;
206 
207  $this->withfrom = 1;
208  $this->withto = 1;
209  $this->withtofree = 1;
210  $this->withtocc = 1;
211  $this->withtoccc = 0;
212  $this->witherrorsto = 0;
213  $this->withtopic = 1;
214  $this->withfile = 0; // 1=Add section "Attached files". 2=Can add files.
215  $this->withmaindocfile = 0; // 1=Add a checkbox "Attach also main document" for mass actions (checked by default), -1=Add checkbox (not checked by default)
216  $this->withbody = 1;
217 
218  $this->withfromreadonly = 1;
219  $this->withreplytoreadonly = 1;
220  $this->withtoreadonly = 0;
221  $this->withtoccreadonly = 0;
222  $this->withtocccreadonly = 0;
223  $this->witherrorstoreadonly = 0;
224  $this->withtopicreadonly = 0;
225  $this->withfilereadonly = 0;
226  $this->withbodyreadonly = 0;
227  $this->withdeliveryreceiptreadonly = 0;
228  $this->withfckeditor = -1; // -1 = Auto
229  }
230 
231  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
237  public function clear_attached_files()
238  {
239  // phpcs:enable
240  global $conf, $user;
241  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
242 
243  // Set tmp user directory
244  $vardir = $conf->user->dir_output."/".$user->id;
245  $upload_dir = $vardir.'/temp/'; // TODO Add $keytoavoidconflict in upload_dir path
246  if (is_dir($upload_dir)) {
247  dol_delete_dir_recursive($upload_dir);
248  }
249 
250  $keytoavoidconflict = empty($this->trackid) ? '' : '-'.$this->trackid; // this->trackid must be defined
251  unset($_SESSION["listofpaths".$keytoavoidconflict]);
252  unset($_SESSION["listofnames".$keytoavoidconflict]);
253  unset($_SESSION["listofmimes".$keytoavoidconflict]);
254  }
255 
256  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
265  public function add_attached_files($path, $file = '', $type = '')
266  {
267  // phpcs:enable
268  $listofpaths = array();
269  $listofnames = array();
270  $listofmimes = array();
271 
272  if (empty($file)) {
273  $file = basename($path);
274  }
275  if (empty($type)) {
276  $type = dol_mimetype($file);
277  }
278 
279  $keytoavoidconflict = empty($this->trackid) ? '' : '-'.$this->trackid; // this->trackid must be defined
280  if (!empty($_SESSION["listofpaths".$keytoavoidconflict])) {
281  $listofpaths = explode(';', $_SESSION["listofpaths".$keytoavoidconflict]);
282  }
283  if (!empty($_SESSION["listofnames".$keytoavoidconflict])) {
284  $listofnames = explode(';', $_SESSION["listofnames".$keytoavoidconflict]);
285  }
286  if (!empty($_SESSION["listofmimes".$keytoavoidconflict])) {
287  $listofmimes = explode(';', $_SESSION["listofmimes".$keytoavoidconflict]);
288  }
289  if (!in_array($file, $listofnames)) {
290  $listofpaths[] = $path;
291  $listofnames[] = $file;
292  $listofmimes[] = $type;
293  $_SESSION["listofpaths".$keytoavoidconflict] = implode(';', $listofpaths);
294  $_SESSION["listofnames".$keytoavoidconflict] = implode(';', $listofnames);
295  $_SESSION["listofmimes".$keytoavoidconflict] = implode(';', $listofmimes);
296  }
297  }
298 
299  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
306  public function remove_attached_files($keytodelete)
307  {
308  // phpcs:enable
309  $listofpaths = array();
310  $listofnames = array();
311  $listofmimes = array();
312 
313  $keytoavoidconflict = empty($this->trackid) ? '' : '-'.$this->trackid; // this->trackid must be defined
314  if (!empty($_SESSION["listofpaths".$keytoavoidconflict])) {
315  $listofpaths = explode(';', $_SESSION["listofpaths".$keytoavoidconflict]);
316  }
317  if (!empty($_SESSION["listofnames".$keytoavoidconflict])) {
318  $listofnames = explode(';', $_SESSION["listofnames".$keytoavoidconflict]);
319  }
320  if (!empty($_SESSION["listofmimes".$keytoavoidconflict])) {
321  $listofmimes = explode(';', $_SESSION["listofmimes".$keytoavoidconflict]);
322  }
323  if ($keytodelete >= 0) {
324  unset($listofpaths[$keytodelete]);
325  unset($listofnames[$keytodelete]);
326  unset($listofmimes[$keytodelete]);
327  $_SESSION["listofpaths".$keytoavoidconflict] = implode(';', $listofpaths);
328  $_SESSION["listofnames".$keytoavoidconflict] = implode(';', $listofnames);
329  $_SESSION["listofmimes".$keytoavoidconflict] = implode(';', $listofmimes);
330  //var_dump($_SESSION['listofpaths']);
331  }
332  }
333 
334  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
340  public function get_attached_files()
341  {
342  // phpcs:enable
343  $listofpaths = array();
344  $listofnames = array();
345  $listofmimes = array();
346 
347  $keytoavoidconflict = empty($this->trackid) ? '' : '-'.$this->trackid; // this->trackid must be defined
348  if (!empty($_SESSION["listofpaths".$keytoavoidconflict])) {
349  $listofpaths = explode(';', $_SESSION["listofpaths".$keytoavoidconflict]);
350  }
351  if (!empty($_SESSION["listofnames".$keytoavoidconflict])) {
352  $listofnames = explode(';', $_SESSION["listofnames".$keytoavoidconflict]);
353  }
354  if (!empty($_SESSION["listofmimes".$keytoavoidconflict])) {
355  $listofmimes = explode(';', $_SESSION["listofmimes".$keytoavoidconflict]);
356  }
357  return array('paths' => $listofpaths, 'names' => $listofnames, 'mimes' => $listofmimes);
358  }
359 
360  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
371  public function show_form($addfileaction = 'addfile', $removefileaction = 'removefile')
372  {
373  // phpcs:enable
374  print $this->get_form($addfileaction, $removefileaction);
375  }
376 
377  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
388  public function get_form($addfileaction = 'addfile', $removefileaction = 'removefile')
389  {
390  // phpcs:enable
391  global $conf, $langs, $user, $hookmanager, $form;
392 
393  // Required to show preview wof mail attachments
394  require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
395  $formfile = new FormFile($this->db);
396 
397  if (!is_object($form)) {
398  $form = new Form($this->db);
399  }
400 
401  // Load translation files required by the page
402  $langs->loadLangs(array('other', 'mails', 'members'));
403 
404  // Clear temp files. Must be done before call of triggers, at beginning (mode = init), or when we select a new template
405  if (GETPOST('mode', 'alpha') == 'init' || (GETPOST('modelselected') && GETPOST('modelmailselected', 'alpha') && GETPOST('modelmailselected', 'alpha') != '-1')) {
406  $this->clear_attached_files();
407  }
408 
409  // Call hook getFormMail
410  $hookmanager->initHooks(array('formmail'));
411 
412  $parameters = array(
413  'addfileaction' => $addfileaction,
414  'removefileaction' => $removefileaction,
415  'trackid' => $this->trackid
416  );
417  $reshook = $hookmanager->executeHooks('getFormMail', $parameters, $this);
418 
419  if (!empty($reshook)) {
420  return $hookmanager->resPrint;
421  } else {
422  $out = '';
423 
424  $disablebademails = 1;
425 
426  // Define output language
427  $outputlangs = $langs;
428  $newlang = '';
429  if (getDolGlobalInt('MAIN_MULTILANGS') && !empty($this->param['langsmodels'])) {
430  $newlang = $this->param['langsmodels'];
431  }
432  if (!empty($newlang)) {
433  $outputlangs = new Translate("", $conf);
434  $outputlangs->setDefaultLang($newlang);
435  $outputlangs->load('other');
436  }
437 
438  // Get message template for $this->param["models"] into c_email_templates
439  $arraydefaultmessage = -1;
440  if ($this->param['models'] != 'none') {
441  $model_id = 0;
442  if (array_key_exists('models_id', $this->param)) {
443  $model_id = $this->param["models_id"];
444  }
445 
446  $arraydefaultmessage = $this->getEMailTemplate($this->db, $this->param["models"], $user, $outputlangs, $model_id, 1, '', ($model_id > 0 ? -1 : 1)); // If $model_id is empty, preselect the first one
447  }
448 
449  // Define list of attached files
450  $listofpaths = array();
451  $listofnames = array();
452  $listofmimes = array();
453  $keytoavoidconflict = empty($this->trackid) ? '' : '-'.$this->trackid; // this->trackid must be defined
454 
455  if (GETPOST('mode', 'alpha') == 'init' || (GETPOST('modelselected') && GETPOST('modelmailselected', 'alpha') && GETPOST('modelmailselected', 'alpha') != '-1')) {
456  if (!empty($arraydefaultmessage->joinfiles) && !empty($this->param['fileinit']) && is_array($this->param['fileinit'])) {
457  foreach ($this->param['fileinit'] as $path) {
458  if (!empty($path)) {
459  $this->add_attached_files($path);
460  }
461  }
462  }
463  }
464 
465  if (!empty($_SESSION["listofpaths".$keytoavoidconflict])) {
466  $listofpaths = explode(';', $_SESSION["listofpaths".$keytoavoidconflict]);
467  }
468  if (!empty($_SESSION["listofnames".$keytoavoidconflict])) {
469  $listofnames = explode(';', $_SESSION["listofnames".$keytoavoidconflict]);
470  }
471  if (!empty($_SESSION["listofmimes".$keytoavoidconflict])) {
472  $listofmimes = explode(';', $_SESSION["listofmimes".$keytoavoidconflict]);
473  }
474 
475 
476  $out .= "\n".'<!-- Begin form mail type='.$this->param["models"].' --><div id="mailformdiv"></div>'."\n";
477  if ($this->withform == 1) {
478  $out .= '<form method="POST" name="mailform" id="mailform" enctype="multipart/form-data" action="'.$this->param["returnurl"].'#formmail">'."\n";
479 
480  $out .= '<a id="formmail" name="formmail"></a>';
481  $out .= '<input style="display:none" type="submit" id="sendmailhidden" name="sendmail">';
482  $out .= '<input type="hidden" name="token" value="'.newToken().'" />';
483  $out .= '<input type="hidden" name="trackid" value="'.$this->trackid.'" />';
484  $out .= '<input type="hidden" name="inreplyto" value="'.$this->inreplyto.'" />';
485  }
486  if (!empty($this->withfrom)) {
487  if (!empty($this->withfromreadonly)) {
488  $out .= '<input type="hidden" id="fromname" name="fromname" value="'.$this->fromname.'" />';
489  $out .= '<input type="hidden" id="frommail" name="frommail" value="'.$this->frommail.'" />';
490  }
491  }
492  foreach ($this->param as $key => $value) {
493  if (is_array($value)) {
494  $out .= "<!-- param key=".$key." is array, we do not output input field for it -->\n";
495  } else {
496  $out .= '<input type="hidden" id="'.$key.'" name="'.$key.'" value="'.$value.'" />'."\n";
497  }
498  }
499 
500  $modelmail_array = array();
501  if ($this->param['models'] != 'none') {
502  $result = $this->fetchAllEMailTemplate($this->param["models"], $user, $outputlangs);
503  if ($result < 0) {
504  setEventMessages($this->error, $this->errors, 'errors');
505  }
506 
507  foreach ($this->lines_model as $line) {
508  $reg = array();
509  if (preg_match('/\‍((.*)\‍)/', $line->label, $reg)) {
510  $labeltouse = $langs->trans($reg[1]); // langs->trans when label is __(xxx)__
511  } else {
512  $labeltouse = $line->label;
513  }
514 
515  // We escape the $labeltouse to store it into $modelmail_array.
516  $modelmail_array[$line->id] = dol_escape_htmltag($labeltouse);
517  if ($line->lang) {
518  $modelmail_array[$line->id] .= ' '.picto_from_langcode($line->lang);
519  }
520  if ($line->private) {
521  $modelmail_array[$line->id] .= ' - <span class="opacitymedium">'.dol_escape_htmltag($langs->trans("Private")).'</span>';
522  }
523  }
524  }
525 
526  // Zone to select email template
527  if (count($modelmail_array) > 0) {
528  $model_mail_selected_id = GETPOSTISSET('modelmailselected') ? GETPOSTINT('modelmailselected') : ($arraydefaultmessage->id > 0 ? $arraydefaultmessage->id : 0);
529 
530  // If list of template is filled
531  $out .= '<div class="center" style="padding: 0px 0 12px 0">'."\n";
532 
533  $out .= '<span class="opacitymedium">'.$langs->trans('SelectMailModel').':</span> ';
534 
535  $out .= $this->selectarray('modelmailselected', $modelmail_array, $model_mail_selected_id, 1, 0, 0, '', 0, 0, 0, '', 'minwidth100', 1, '', 0, 1);
536  if ($user->admin) {
537  $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFrom", $langs->transnoentitiesnoconv('Setup').' - '.$langs->transnoentitiesnoconv('EMails')), 1);
538  }
539 
540  $out .= ' &nbsp; ';
541  $out .= '<input type="submit" class="button reposition smallpaddingimp" value="'.$langs->trans('Apply').'" name="modelselected" id="modelselected">';
542  $out .= ' &nbsp; ';
543  $out .= '</div>';
544  } elseif (!empty($this->param['models']) && in_array($this->param['models'], array(
545  'propal_send', 'order_send', 'facture_send',
546  'shipping_send', 'fichinter_send', 'supplier_proposal_send', 'order_supplier_send',
547  'invoice_supplier_send', 'thirdparty', 'contract', 'user', 'recruitmentcandidature_send', 'all'
548  ))) {
549  // If list of template is empty
550  $out .= '<div class="center" style="padding: 0px 0 12px 0">'."\n";
551  $out .= '<span class="opacitymedium">'.$langs->trans('SelectMailModel').':</span> ';
552  $out .= '<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.
553  if ($user->admin) {
554  $out .= info_admin($langs->trans("YouCanChangeValuesForThisListFrom", $langs->transnoentitiesnoconv('Setup').' - '.$langs->transnoentitiesnoconv('EMails')), 1);
555  }
556  $out .= ' &nbsp; ';
557  $out .= '<input type="submit" class="button reposition smallpaddingimp" value="'.$langs->trans('Apply').'" name="modelselected" disabled="disabled" id="modelselected">';
558  $out .= ' &nbsp; ';
559  $out .= '</div>';
560  } else {
561  $out .= '<!-- No template available for $this->param["models"] = '.$this->param['models'].' -->';
562  }
563 
564 
565  $out .= '<table class="tableforemailform boxtablenotop centpercent">'."\n";
566 
567  // Substitution array/string
568  $helpforsubstitution = '';
569  if (is_array($this->substit) && count($this->substit)) {
570  $helpforsubstitution .= $langs->trans('AvailableVariables').' :<br><br><span class="small">'."\n";
571  }
572  foreach ($this->substit as $key => $val) {
573  // Do not show deprecated variables into the tooltip help of substitution variables
574  if (in_array($key, array('__NEWREF__', '__REFCLIENT__', '__REFSUPPLIER__', '__SUPPLIER_ORDER_DATE_DELIVERY__', '__SUPPLIER_ORDER_DELAY_DELIVERY__'))) {
575  continue;
576  }
577  $helpforsubstitution .= $key.' -> '.$langs->trans(dol_string_nohtmltag(dolGetFirstLineOfText($val))).'<br>';
578  }
579  if (is_array($this->substit) && count($this->substit)) {
580  $helpforsubstitution .= '</span>';
581  }
582 
583  /*
584  if (!empty($this->withsubstit)) { // Unset or set ->withsubstit=0 to disable this.
585  $out .= '<tr><td colspan="2" class="right">';
586  if (is_numeric($this->withsubstit)) {
587  $out .= $form->textwithpicto($langs->trans("EMailTestSubstitutionReplacedByGenericValues"), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltip'); // Old usage
588  } else {
589  $out .= $form->textwithpicto($langs->trans('AvailableVariables'), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltip'); // New usage
590  }
591  $out .= "</td></tr>\n";
592  }*/
593 
594  // From
595  if (!empty($this->withfrom)) {
596  if (!empty($this->withfromreadonly)) {
597  $out .= '<tr><td class="fieldrequired minwidth200">'.$langs->trans("MailFrom").'</td><td>';
598 
599  // $this->fromtype is the default value to use to select sender
600  if (!($this->fromtype === 'user' && $this->fromid > 0)
601  && !($this->fromtype === 'company')
602  && !($this->fromtype === 'robot')
603  && !preg_match('/user_aliases/', $this->fromtype)
604  && !preg_match('/global_aliases/', $this->fromtype)
605  && !preg_match('/senderprofile/', $this->fromtype)
606  ) {
607  // Use this->fromname and this->frommail or error if not defined
608  $out .= $this->fromname;
609  if ($this->frommail) {
610  $out .= ' &lt;'.$this->frommail.'&gt;';
611  } else {
612  if ($this->fromtype) {
613  $langs->load('errors');
614  $out .= '<span class="warning"> &lt;'.$langs->trans('ErrorNoMailDefinedForThisUser').'&gt; </span>';
615  }
616  }
617  } else {
618  $liste = array();
619 
620  // Add user email
621  if (empty($user->email)) {
622  $langs->load('errors');
623  $s = $user->getFullName($langs).' &lt;'.$langs->trans('ErrorNoMailDefinedForThisUser').'&gt;';
624  } else {
625  $s = $user->getFullName($langs).' &lt;'.$user->email.'&gt;';
626  }
627  $liste['user'] = array('label' => $s, 'data-html' => $s);
628 
629  // Add also company main email
630  if (getDolGlobalString('MAIN_INFO_SOCIETE_MAIL')) {
631  $s = (!getDolGlobalString('MAIN_INFO_SOCIETE_NOM') ? $conf->global->MAIN_INFO_SOCIETE_EMAIL : $conf->global->MAIN_INFO_SOCIETE_NOM).' &lt;' . getDolGlobalString('MAIN_INFO_SOCIETE_MAIL').'&gt;';
632  $liste['company'] = array('label' => $s, 'data-html' => $s);
633  }
634 
635  // Add also email aliases if there is some
636  $listaliases = array(
637  'user_aliases' => (empty($user->email_aliases) ? '' : $user->email_aliases),
638  'global_aliases' => getDolGlobalString('MAIN_INFO_SOCIETE_MAIL_ALIASES'),
639  );
640 
641  if (!empty($arraydefaultmessage->email_from)) {
642  $templatemailfrom = ' &lt;'.$arraydefaultmessage->email_from.'&gt;';
643  $liste['from_template_'.GETPOST('modelmailselected')] = array('label' => $templatemailfrom, 'data-html' => $templatemailfrom);
644  }
645 
646  // Also add robot email
647  if (!empty($this->fromalsorobot)) {
648  if (getDolGlobalString('MAIN_MAIL_EMAIL_FROM') && getDolGlobalString('MAIN_MAIL_EMAIL_FROM') != getDolGlobalString('MAIN_INFO_SOCIETE_MAIL')) {
649  $s = getDolGlobalString('MAIN_MAIL_EMAIL_FROM');
650  if ($this->frommail) {
651  $s .= ' &lt;' . getDolGlobalString('MAIN_MAIL_EMAIL_FROM').'&gt;';
652  }
653  $liste['main_from'] = array('label' => $s, 'data-html' => $s);
654  }
655  }
656 
657  // Add also email aliases from the c_email_senderprofile table
658  $sql = "SELECT rowid, label, email FROM ".$this->db->prefix()."c_email_senderprofile";
659  $sql .= " WHERE active = 1 AND (private = 0 OR private = ".((int) $user->id).")";
660  $sql .= " ORDER BY position";
661  $resql = $this->db->query($sql);
662  if ($resql) {
663  $num = $this->db->num_rows($resql);
664  $i = 0;
665  while ($i < $num) {
666  $obj = $this->db->fetch_object($resql);
667  if ($obj) {
668  $listaliases['senderprofile_'.$obj->rowid] = $obj->label.' <'.$obj->email.'>';
669  }
670  $i++;
671  }
672  } else {
673  dol_print_error($this->db);
674  }
675 
676  foreach ($listaliases as $typealias => $listalias) {
677  $posalias = 0;
678  $listaliasarray = explode(',', $listalias);
679  foreach ($listaliasarray as $listaliasval) {
680  $posalias++;
681  $listaliasval = trim($listaliasval);
682  if ($listaliasval) {
683  $listaliasval = preg_replace('/</', '&lt;', $listaliasval);
684  $listaliasval = preg_replace('/>/', '&gt;', $listaliasval);
685  if (!preg_match('/&lt;/', $listaliasval)) {
686  $listaliasval = '&lt;'.$listaliasval.'&gt;';
687  }
688  $liste[$typealias.'_'.$posalias] = array('label' => $listaliasval, 'data-html' => $listaliasval);
689  }
690  }
691  }
692 
693  // Using ajaxcombo here make the '<email>' no more visible on list because <emailofuser> is not a valid html tag,
694  // so we transform before each record into $liste to be printable with ajaxcombo by replacing <> into ()
695  // $liste['senderprofile_0_0'] = array('label'=>'rrr', 'data-html'=>'rrr &lt;aaaa&gt;');
696  foreach ($liste as $key => $val) {
697  if (!empty($liste[$key]['data-html'])) {
698  $liste[$key]['data-html'] = str_replace(array('&lt;', '<', '&gt;', '>'), array('__LTCHAR__', '__LTCHAR__', '__GTCHAR__', '__GTCHAR__'), $liste[$key]['data-html']);
699  $liste[$key]['data-html'] = str_replace(array('__LTCHAR__', '__GTCHAR__'), array('<span class="opacitymedium">(', ')</span>'), $liste[$key]['data-html']);
700  }
701  }
702  $out .= ' '.$form->selectarray('fromtype', $liste, empty($arraydefaultmessage->email_from) ? $this->fromtype : 'from_template_'.GETPOST('modelmailselected'), 0, 0, 0, '', 0, 0, 0, '', 'fromforsendingprofile maxwidth200onsmartphone', 1, '', $disablebademails);
703  }
704 
705  $out .= "</td></tr>\n";
706  } else {
707  $out .= '<tr><td class="fieldrequired width200">'.$langs->trans("MailFrom")."</td><td>";
708  $out .= $langs->trans("Name").':<input type="text" id="fromname" name="fromname" class="maxwidth200onsmartphone" value="'.$this->fromname.'" />';
709  $out .= '&nbsp; &nbsp; ';
710  $out .= $langs->trans("EMail").':&lt;<input type="text" id="frommail" name="frommail" class="maxwidth200onsmartphone" value="'.$this->frommail.'" />&gt;';
711  $out .= "</td></tr>\n";
712  }
713  }
714 
715  // To
716  if (!empty($this->withto) || is_array($this->withto)) {
717  $out .= $this->getHtmlForTo();
718  }
719 
720  // To User
721  if (!empty($this->withtouser) && is_array($this->withtouser) && getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
722  $out .= '<tr><td>';
723  $out .= $langs->trans("MailToUsers");
724  $out .= '</td><td>';
725 
726  // multiselect array convert html entities into options tags, even if we don't want this, so we encode them a second time
727  $tmparray = $this->withtouser;
728  foreach ($tmparray as $key => $val) {
729  $tmparray[$key] = dol_htmlentities($tmparray[$key], null, 'UTF-8', true);
730  }
731  $withtoselected = GETPOST("receiveruser", 'array'); // Array of selected value
732  if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action', 'aZ09') == 'presend') {
733  $withtoselected = array_keys($tmparray);
734  }
735  $out .= $form->multiselectarray("receiveruser", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', null, "");
736  $out .= "</td></tr>\n";
737  }
738 
739  // With option for one email per recipient
740  if (!empty($this->withoptiononeemailperrecipient)) {
741  if (abs($this->withoptiononeemailperrecipient) == 1) {
742  $out .= '<tr><td class="minwidth200">';
743  $out .= $langs->trans("GroupEmails");
744  $out .= '</td><td>';
745  $out .= ' <input type="checkbox" id="oneemailperrecipient" value="1" name="oneemailperrecipient"'.($this->withoptiononeemailperrecipient > 0 ? ' checked="checked"' : '').'> ';
746  $out .= '<label for="oneemailperrecipient">';
747  $out .= $form->textwithpicto($langs->trans("OneEmailPerRecipient"), $langs->trans("WarningIfYouCheckOneRecipientPerEmail"), 1, 'help');
748  $out .= '</label>';
749  //$out .= '<span class="hideonsmartphone opacitymedium">';
750  //$out .= ' - ';
751  //$out .= $langs->trans("WarningIfYouCheckOneRecipientPerEmail");
752  //$out .= '</span>';
753  if (getDolGlobalString('MASS_ACTION_EMAIL_ON_DIFFERENT_THIRPARTIES_ADD_CUSTOM_EMAIL')) {
754  if (!empty($this->withto) && !is_array($this->withto)) {
755  $out .= ' '.$langs->trans("or").' <input type="email" name="emailto" value="">';
756  }
757  }
758  $out .= '</td></tr>';
759  } else {
760  $out .= '<tr><td><input type="hidden" name="oneemailperrecipient" value="1"></td><td></td></tr>';
761  }
762  }
763 
764  // CC
765  if (!empty($this->withtocc) || is_array($this->withtocc)) {
766  $out .= $this->getHtmlForCc();
767  }
768 
769  // To User cc
770  if (!empty($this->withtoccuser) && is_array($this->withtoccuser) && getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
771  $out .= '<tr><td>';
772  $out .= $langs->trans("MailToCCUsers");
773  $out .= '</td><td>';
774 
775  // multiselect array convert html entities into options tags, even if we don't want this, so we encode them a second time
776  $tmparray = $this->withtoccuser;
777  foreach ($tmparray as $key => $val) {
778  $tmparray[$key] = dol_htmlentities($tmparray[$key], null, 'UTF-8', true);
779  }
780  $withtoselected = GETPOST("receiverccuser", 'array'); // Array of selected value
781  if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action', 'aZ09') == 'presend') {
782  $withtoselected = array_keys($tmparray);
783  }
784  $out .= $form->multiselectarray("receiverccuser", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', null, "");
785  $out .= "</td></tr>\n";
786  }
787 
788  // CCC
789  if (!empty($this->withtoccc) || is_array($this->withtoccc)) {
790  $out .= $this->getHtmlForWithCcc();
791  }
792 
793  // Replyto
794  if (!empty($this->withreplyto)) {
795  if ($this->withreplytoreadonly) {
796  $out .= '<input type="hidden" id="replyname" name="replyname" value="'.$this->replytoname.'" />';
797  $out .= '<input type="hidden" id="replymail" name="replymail" value="'.$this->replytomail.'" />';
798  $out .= "<tr><td>".$langs->trans("MailReply")."</td><td>".$this->replytoname.($this->replytomail ? (" &lt;".$this->replytomail."&gt;") : "");
799  $out .= "</td></tr>\n";
800  }
801  }
802 
803  // Errorsto
804  if (!empty($this->witherrorsto)) {
805  $out .= $this->getHtmlForWithErrorsTo();
806  }
807 
808  // Ask delivery receipt
809  if (!empty($this->withdeliveryreceipt) && getDolGlobalInt('MAIN_EMAIL_SUPPORT_ACK')) {
810  $out .= $this->getHtmlForDeliveryreceipt();
811  }
812 
813  // Topic
814  if (!empty($this->withtopic)) {
815  $out .= $this->getHtmlForTopic($arraydefaultmessage, $helpforsubstitution);
816  }
817 
818  // Attached files
819  if (!empty($this->withfile)) {
820  $out .= '<tr>';
821  $out .= '<td class="tdtop">'.$langs->trans("MailFile").'</td>';
822 
823  $out .= '<td>';
824 
825  if ($this->withmaindocfile) {
826  // withmaindocfile is set to 1 or -1 to show the checkbox (-1 = checked or 1 = not checked)
827  if (GETPOSTISSET('sendmail')) {
828  $this->withmaindocfile = (GETPOST('addmaindocfile', 'alpha') ? -1 : 1);
829  } elseif (is_object($arraydefaultmessage) && $arraydefaultmessage->id > 0) {
830  // If a template was selected, we use setup of template to define if join file checkbox is selected or not.
831  $this->withmaindocfile = ($arraydefaultmessage->joinfiles ? -1 : 1);
832  }
833  }
834 
835  if (!empty($this->withmaindocfile)) {
836  if ($this->withmaindocfile == 1) {
837  $out .= '<input type="checkbox" id="addmaindocfile" name="addmaindocfile" value="1" />';
838  } elseif ($this->withmaindocfile == -1) {
839  $out .= '<input type="checkbox" id="addmaindocfile" name="addmaindocfile" value="1" checked="checked" />';
840  }
841  if (getDolGlobalString('MAIL_MASS_ACTION_ADD_LAST_IF_MAIN_DOC_NOT_FOUND')) {
842  $out .= ' <label for="addmaindocfile">'.$langs->trans("JoinMainDocOrLastGenerated").'.</label><br>';
843  } else {
844  $out .= ' <label for="addmaindocfile">'.$langs->trans("JoinMainDoc").'.</label><br>';
845  }
846  }
847 
848  if (is_numeric($this->withfile)) {
849  // TODO Trick to have param removedfile containing nb of file to delete. But this does not works without javascript
850  $out .= '<input type="hidden" class="removedfilehidden" name="removedfile" value="">'."\n";
851  $out .= '<script nonce="'.getNonce().'" type="text/javascript">';
852  $out .= 'jQuery(document).ready(function () {';
853  $out .= ' jQuery(".removedfile").click(function() {';
854  $out .= ' jQuery(".removedfilehidden").val(jQuery(this).val());';
855  $out .= ' });';
856  $out .= '})';
857  $out .= '</script>'."\n";
858  if (count($listofpaths)) {
859  foreach ($listofpaths as $key => $val) {
860  $relativepathtofile = substr($val, (strlen(DOL_DATA_ROOT) - strlen($val)));
861 
862  if ($conf->entity > 1) {
863  $relativepathtofile = str_replace('/'.$conf->entity.'/', '/', $relativepathtofile);
864  }
865  // Try to extract data from full path
866  $formfile_params = array();
867  preg_match('#^(/)(\w+)(/)(.+)$#', $relativepathtofile, $formfile_params);
868 
869  $out .= '<div id="attachfile_'.$key.'">';
870  // Preview of attachment
871  $out .= img_mime($listofnames[$key]).$listofnames[$key];
872 
873  $out .= ' '.$formfile->showPreview(array(), $formfile_params[2], $formfile_params[4]);
874  if (!$this->withfilereadonly) {
875  $out .= ' <input type="image" style="border: 0px;" src="'.DOL_URL_ROOT.'/theme/'.$conf->theme.'/img/delete.png" value="'.($key + 1).'" class="removedfile input-nobottom" id="removedfile_'.$key.'" name="removedfile_'.$key.'" />';
876  //$out.= ' <a href="'.$_SERVER["PHP_SELF"].'?removedfile='.($key+1).'&id=removedfile_'.$key.'">'.img_delete($langs->trans("Remove"), 'id="removedfile_'.$key.'" name="removedfile_'.$key.'"', 'removedfile input-nobottom').'</a>';
877  }
878  $out .= '<br></div>';
879  }
880  } elseif (empty($this->withmaindocfile)) {
881  //$out .= '<span class="opacitymedium">'.$langs->trans("NoAttachedFiles").'</span><br>';
882  }
883  if ($this->withfile == 2) {
884  $maxfilesizearray = getMaxFileSizeArray();
885  $maxmin = $maxfilesizearray['maxmin'];
886  if ($maxmin > 0) {
887  $out .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.($maxmin * 1024).'">'; // MAX_FILE_SIZE must precede the field type=file
888  }
889  // Can add other files
890  if (!getDolGlobalString('FROM_MAIL_DONT_USE_INPUT_FILE_MULTIPLE')) {
891  $out .= '<input type="file" class="flat" id="addedfile" name="addedfile[]" value="'.$langs->trans("Upload").'" multiple />';
892  } else {
893  $out .= '<input type="file" class="flat" id="addedfile" name="addedfile" value="'.$langs->trans("Upload").'" />';
894  }
895  $out .= ' ';
896  $out .= '<input type="submit" class="button smallpaddingimp" id="'.$addfileaction.'" name="'.$addfileaction.'" value="'.$langs->trans("MailingAddFile").'" />';
897  }
898  } else {
899  $out .= $this->withfile;
900  }
901 
902  $out .= "</td></tr>\n";
903  }
904 
905  // Message (+ Links to choose layout or ai prompt)
906  if (!empty($this->withbody)) {
907  $defaultmessage = GETPOST('message', 'restricthtml');
908  if (!GETPOST('modelselected', 'alpha') || GETPOST('modelmailselected') != '-1') {
909  if ($arraydefaultmessage && $arraydefaultmessage->content) {
910  $defaultmessage = $arraydefaultmessage->content;
911  } elseif (!is_numeric($this->withbody)) {
912  $defaultmessage = $this->withbody;
913  }
914  }
915 
916  // Complete substitution array with the url to make online payment
917  $validpaymentmethod = array();
918  if (empty($this->substit['__REF__'])) {
919  $paymenturl = '';
920  } else {
921  // Set the online payment url link into __ONLINE_PAYMENT_URL__ key
922  require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
923  $langs->loadLangs(array('paypal', 'other'));
924  $typeforonlinepayment = 'free';
925  if ($this->param["models"] == 'order' || $this->param["models"] == 'order_send') {
926  $typeforonlinepayment = 'order'; // TODO use detection on something else than template
927  }
928  if ($this->param["models"] == 'invoice' || $this->param["models"] == 'facture_send') {
929  $typeforonlinepayment = 'invoice'; // TODO use detection on something else than template
930  }
931  if ($this->param["models"] == 'member') {
932  $typeforonlinepayment = 'member'; // TODO use detection on something else than template
933  }
934  $url = getOnlinePaymentUrl(0, $typeforonlinepayment, $this->substit['__REF__']);
935  $paymenturl = $url;
936 
937  $validpaymentmethod = getValidOnlinePaymentMethods('');
938  }
939 
940  if (count($validpaymentmethod) > 0 && $paymenturl) {
941  $langs->load('other');
942  $this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__'] = str_replace('\n', "\n", $langs->transnoentities("PredefinedMailContentLink", $paymenturl));
943  $this->substit['__ONLINE_PAYMENT_URL__'] = $paymenturl;
944  } else {
945  $this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__'] = '';
946  $this->substit['__ONLINE_PAYMENT_URL__'] = '';
947  }
948 
949  $this->substit['__ONLINE_INTERVIEW_SCHEDULER_TEXT_AND_URL__'] = '';
950 
951  // Add lines substitution key from each line
952  $lines = '';
953  $defaultlines = $arraydefaultmessage->content_lines;
954  if (isset($defaultlines)) {
955  foreach ($this->substit_lines as $substit_line) {
956  $lines .= make_substitutions($defaultlines, $substit_line)."\n";
957  }
958  }
959  $this->substit['__LINES__'] = $lines;
960 
961  $defaultmessage = str_replace('\n', "\n", $defaultmessage);
962 
963  // Deal with format differences between message and some substitution variables (text / HTML)
964  $atleastonecomponentishtml = 0;
965  if (strpos($defaultmessage, '__USER_SIGNATURE__') !== false && dol_textishtml($this->substit['__USER_SIGNATURE__'])) {
966  $atleastonecomponentishtml++;
967  }
968  if (strpos($defaultmessage, '__SENDEREMAIL_SIGNATURE__') !== false && dol_textishtml($this->substit['__SENDEREMAIL_SIGNATURE__'])) {
969  $atleastonecomponentishtml++;
970  }
971  if (strpos($defaultmessage, '__ONLINE_PAYMENT_TEXT_AND_URL__') !== false && dol_textishtml($this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__'])) {
972  $atleastonecomponentishtml++;
973  }
974  if (strpos($defaultmessage, '__ONLINE_INTERVIEW_SCHEDULER_TEXT_AND_URL__') !== false && dol_textishtml($this->substit['__ONLINE_INTERVIEW_SCHEDULER_TEXT_AND_URL__'])) {
975  $atleastonecomponentishtml++;
976  }
977  if (dol_textishtml($defaultmessage)) {
978  $atleastonecomponentishtml++;
979  }
980  if ($atleastonecomponentishtml) {
981  if (!dol_textishtml($this->substit['__USER_SIGNATURE__'])) {
982  $this->substit['__USER_SIGNATURE__'] = dol_nl2br($this->substit['__USER_SIGNATURE__']);
983  }
984  if (!dol_textishtml($this->substit['__SENDEREMAIL_SIGNATURE__'])) {
985  $this->substit['__SENDEREMAIL_SIGNATURE__'] = dol_nl2br($this->substit['__SENDEREMAIL_SIGNATURE__']);
986  }
987  if (!dol_textishtml($this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__'])) {
988  $this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__'] = dol_nl2br($this->substit['__ONLINE_PAYMENT_TEXT_AND_URL__']);
989  }
990  if (!dol_textishtml($defaultmessage)) {
991  $defaultmessage = dol_nl2br($defaultmessage);
992  }
993  }
994 
995  if (GETPOSTISSET("message") && !GETPOST('modelselected')) {
996  $defaultmessage = GETPOST("message", "restricthtml");
997  } else {
998  $defaultmessage = make_substitutions($defaultmessage, $this->substit);
999  // Clean first \n and br (to avoid empty line when CONTACTCIVNAME is empty)
1000  $defaultmessage = preg_replace("/^(<br>)+/", "", $defaultmessage);
1001  $defaultmessage = preg_replace("/^\n+/", "", $defaultmessage);
1002  }
1003 
1004  $out .= '<tr>';
1005  $out .= '<td class="tdtop">';
1006  $out .= $form->textwithpicto($langs->trans('MailText'), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltipfrombody');
1007  $out .= '</td>';
1008  $out .= '<td class="tdtop">';
1009 
1010  $formmail = $this;
1011  $showlinktolayout = $formmail->withlayout && $formmail->withfckeditor;
1012  $showlinktolayoutlabel = $langs->trans("FillMessageWithALayout");
1013  $showlinktoai = ($formmail->withaiprompt && isModEnabled('ai')) ? 'textgenerationemail' : '';
1014  $showlinktoailabel = $langs->trans("FillMessageWithAIContent");
1015  $formatforouput = '';
1016  $htmlname = 'message';
1017 
1018  // Fill $out
1019  include DOL_DOCUMENT_ROOT.'/core/tpl/formlayoutai.tpl.php';
1020 
1021  $out .= '</td>';
1022  $out .= '</tr>';
1023 
1024  $out .= '<tr>';
1025  $out .= '<td colspan="2">';
1026  if ($this->withbodyreadonly) {
1027  $out .= nl2br($defaultmessage);
1028  $out .= '<input type="hidden" id="message" name="message" disabled value="'.$defaultmessage.'" />';
1029  } else {
1030  if (!isset($this->ckeditortoolbar)) {
1031  $this->ckeditortoolbar = 'dolibarr_mailings';
1032  }
1033 
1034  // Editor wysiwyg
1035  require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
1036  if ($this->withfckeditor == -1) {
1037  if (getDolGlobalString('FCKEDITOR_ENABLE_MAIL')) {
1038  $this->withfckeditor = 1;
1039  } else {
1040  $this->withfckeditor = 0;
1041  }
1042  }
1043 
1044  $doleditor = new DolEditor('message', $defaultmessage, '', 280, $this->ckeditortoolbar, 'In', true, true, $this->withfckeditor, 8, '95%');
1045  $out .= $doleditor->Create(1);
1046  }
1047  $out .= "</td></tr>\n";
1048  }
1049 
1050  $out .= '</table>'."\n";
1051 
1052  if ($this->withform == 1 || $this->withform == -1) {
1053  $out .= '<div class="center">';
1054  $out .= '<input type="submit" class="button button-add" id="sendmail" name="sendmail" value="'.$langs->trans("SendMail").'"';
1055  // Add a javascript test to avoid to forget to submit file before sending email
1056  if ($this->withfile == 2 && $conf->use_javascript_ajax) {
1057  $out .= ' onClick="if (document.mailform.addedfile.value != \'\') { alert(\''.dol_escape_js($langs->trans("FileWasNotUploaded")).'\'); return false; } else { return true; }"';
1058  }
1059  $out .= ' />';
1060  if ($this->withcancel) {
1061  $out .= '<input class="button button-cancel" type="submit" id="cancel" name="cancel" value="'.$langs->trans("Cancel").'" />';
1062  }
1063  $out .= '</div>'."\n";
1064  }
1065 
1066  if ($this->withform == 1) {
1067  $out .= '</form>'."\n";
1068  }
1069 
1070  // Disable enter key if option MAIN_MAILFORM_DISABLE_ENTERKEY is set
1071  if (getDolGlobalString('MAIN_MAILFORM_DISABLE_ENTERKEY')) {
1072  $out .= '<script nonce="'.getNonce().'" type="text/javascript">';
1073  $out .= 'jQuery(document).ready(function () {';
1074  $out .= ' $(document).on("keypress", \'#mailform\', function (e) { /* Note this is called at every key pressed ! */
1075  var code = e.keyCode || e.which;
1076  if (code == 13) {
1077  console.log("Enter was intercepted and blocked");
1078  e.preventDefault();
1079  return false;
1080  }
1081  });';
1082  $out .= ' })';
1083  $out .= '</script>';
1084  }
1085 
1086  $out .= "<!-- End form mail -->\n";
1087 
1088  return $out;
1089  }
1090  }
1091 
1097  public function getHtmlForTo()
1098  {
1099  global $langs, $form;
1100  $out = '<tr><td class="fieldrequired">';
1101  if ($this->withtofree) {
1102  $out .= $form->textwithpicto($langs->trans("MailTo"), $langs->trans("YouCanUseCommaSeparatorForSeveralRecipients"));
1103  } else {
1104  $out .= $langs->trans("MailTo");
1105  }
1106  $out .= '</td><td>';
1107  if ($this->withtoreadonly) {
1108  if (!empty($this->toname) && !empty($this->tomail)) {
1109  $out .= '<input type="hidden" id="toname" name="toname" value="'.$this->toname.'" />';
1110  $out .= '<input type="hidden" id="tomail" name="tomail" value="'.$this->tomail.'" />';
1111  if ($this->totype == 'thirdparty') {
1112  $soc = new Societe($this->db);
1113  $soc->fetch($this->toid);
1114  $out .= $soc->getNomUrl(1);
1115  } elseif ($this->totype == 'contact') {
1116  $contact = new Contact($this->db);
1117  $contact->fetch($this->toid);
1118  $out .= $contact->getNomUrl(1);
1119  } else {
1120  $out .= $this->toname;
1121  }
1122  $out .= ' &lt;'.$this->tomail.'&gt;';
1123  if ($this->withtofree) {
1124  $out .= '<br>'.$langs->trans("and").' <input class="minwidth200" id="sendto" name="sendto" value="'.(!is_array($this->withto) && !is_numeric($this->withto) ? (GETPOSTISSET("sendto") ? GETPOST("sendto") : $this->withto) : "").'" />';
1125  }
1126  } else {
1127  // Note withto may be a text like 'AllRecipientSelected'
1128  $out .= (!is_array($this->withto) && !is_numeric($this->withto)) ? $this->withto : "";
1129  }
1130  } else {
1131  // The free input of email
1132  if (!empty($this->withtofree)) {
1133  $out .= '<input class="minwidth200" id="sendto" name="sendto" value="'.(($this->withtofree && !is_numeric($this->withtofree)) ? $this->withtofree : (!is_array($this->withto) && !is_numeric($this->withto) ? (GETPOSTISSET("sendto") ? GETPOST("sendto") : $this->withto) : "")).'" />';
1134  }
1135  // The select combo
1136  if (!empty($this->withto) && is_array($this->withto)) {
1137  if (!empty($this->withtofree)) {
1138  $out .= " ".$langs->trans("and")."/".$langs->trans("or")." ";
1139  }
1140 
1141  $tmparray = $this->withto;
1142  foreach ($tmparray as $key => $val) {
1143  if (is_array($val)) {
1144  $label = $val['label'];
1145  } else {
1146  $label = $val;
1147  }
1148 
1149  $tmparray[$key] = array();
1150  $tmparray[$key]['id'] = $key;
1151 
1152  $tmparray[$key]['label'] = $label;
1153  $tmparray[$key]['label'] = str_replace(array('<', '>'), array('(', ')'), $tmparray[$key]['label']);
1154  // multiselect array convert html entities into options tags, even if we don't want this, so we encode them a second time
1155  $tmparray[$key]['label'] = dol_htmlentities($tmparray[$key]['label'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', true);
1156 
1157  $tmparray[$key]['labelhtml'] = $label;
1158  $tmparray[$key]['labelhtml'] = str_replace(array('&lt;', '<', '&gt;', '>'), array('__LTCHAR__', '__LTCHAR__', '__GTCHAR__', '__GTCHAR__'), $tmparray[$key]['labelhtml']);
1159  $tmparray[$key]['labelhtml'] = str_replace(array('__LTCHAR__', '__GTCHAR__'), array('<span class="opacitymedium">(', ')</span>'), $tmparray[$key]['labelhtml']);
1160  }
1161 
1162  $withtoselected = GETPOST("receiver", 'array'); // Array of selected value
1163  if (!getDolGlobalInt('MAIN_MAIL_NO_WITH_TO_SELECTED')) {
1164  if (empty($withtoselected) && count($tmparray) == 1 && GETPOST('action', 'aZ09') == 'presend') {
1165  $withtoselected = array_keys($tmparray);
1166  }
1167  }
1168 
1169  $out .= $form->multiselectarray("receiver", $tmparray, $withtoselected, null, null, 'inline-block minwidth500', 0, 0);
1170  }
1171  }
1172  $out .= "</td></tr>\n";
1173  return $out;
1174  }
1175 
1181  public function getHtmlForCc()
1182  {
1183  global $langs, $form;
1184  $out = '<tr><td>';
1185  $out .= $form->textwithpicto($langs->trans("MailCC"), $langs->trans("YouCanUseCommaSeparatorForSeveralRecipients"));
1186  $out .= '</td><td>';
1187  if ($this->withtoccreadonly) {
1188  $out .= (!is_array($this->withtocc) && !is_numeric($this->withtocc)) ? $this->withtocc : "";
1189  } else {
1190  $out .= '<input class="minwidth200" id="sendtocc" name="sendtocc" value="'.(GETPOST("sendtocc", "alpha") ? GETPOST("sendtocc", "alpha") : ((!is_array($this->withtocc) && !is_numeric($this->withtocc)) ? $this->withtocc : '')).'" />';
1191  if (!empty($this->withtocc) && is_array($this->withtocc)) {
1192  $out .= " ".$langs->trans("and")."/".$langs->trans("or")." ";
1193 
1194  $tmparray = $this->withtocc;
1195  foreach ($tmparray as $key => $val) {
1196  if (is_array($val)) {
1197  $label = $val['label'];
1198  } else {
1199  $label = $val;
1200  }
1201 
1202  $tmparray[$key] = array();
1203  $tmparray[$key]['id'] = $key;
1204 
1205  $tmparray[$key]['label'] = $label;
1206  $tmparray[$key]['label'] = str_replace(array('<', '>'), array('(', ')'), $tmparray[$key]['label']);
1207  // multiselect array convert html entities into options tags, even if we don't want this, so we encode them a second time
1208  $tmparray[$key]['label'] = dol_htmlentities($tmparray[$key]['label'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', true);
1209 
1210  $tmparray[$key]['labelhtml'] = $label;
1211  $tmparray[$key]['labelhtml'] = str_replace(array('&lt;', '<', '&gt;', '>'), array('__LTCHAR__', '__LTCHAR__', '__GTCHAR__', '__GTCHAR__'), $tmparray[$key]['labelhtml']);
1212  $tmparray[$key]['labelhtml'] = str_replace(array('__LTCHAR__', '__GTCHAR__'), array('<span class="opacitymedium">(', ')</span>'), $tmparray[$key]['labelhtml']);
1213  }
1214 
1215  $withtoccselected = GETPOST("receivercc", 'array'); // Array of selected value
1216 
1217  $out .= $form->multiselectarray("receivercc", $tmparray, $withtoccselected, null, null, 'inline-block minwidth500', 0, 0);
1218  }
1219  }
1220  $out .= "</td></tr>\n";
1221  return $out;
1222  }
1223 
1230  public function getHtmlForWithCcc()
1231  {
1232  global $langs, $form;
1233 
1234  $out = '<tr><td>';
1235  $out .= $form->textwithpicto($langs->trans("MailCCC"), $langs->trans("YouCanUseCommaSeparatorForSeveralRecipients"));
1236  $out .= '</td><td>';
1237  if (!empty($this->withtocccreadonly)) {
1238  $out .= (!is_array($this->withtoccc) && !is_numeric($this->withtoccc)) ? $this->withtoccc : "";
1239  } else {
1240  $out .= '<input class="minwidth200" id="sendtoccc" name="sendtoccc" value="'.(GETPOSTISSET("sendtoccc") ? GETPOST("sendtoccc", "alpha") : ((!is_array($this->withtoccc) && !is_numeric($this->withtoccc)) ? $this->withtoccc : '')).'" />';
1241  if (!empty($this->withtoccc) && is_array($this->withtoccc)) {
1242  $out .= " ".$langs->trans("and")."/".$langs->trans("or")." ";
1243 
1244  $tmparray = $this->withtoccc;
1245  foreach ($tmparray as $key => $val) {
1246  if (is_array($val)) {
1247  $label = $val['label'];
1248  } else {
1249  $label = $val;
1250  }
1251  $tmparray[$key] = array();
1252  $tmparray[$key]['id'] = $key;
1253 
1254  $tmparray[$key]['label'] = $label;
1255  $tmparray[$key]['label'] = str_replace(array('<', '>'), array('(', ')'), $tmparray[$key]['label']);
1256  // multiselect array convert html entities into options tags, even if we don't want this, so we encode them a second time
1257  $tmparray[$key]['label'] = dol_htmlentities($tmparray[$key]['label'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8', true);
1258 
1259  $tmparray[$key]['labelhtml'] = $label;
1260  $tmparray[$key]['labelhtml'] = str_replace(array('&lt;', '<', '&gt;', '>'), array('__LTCHAR__', '__LTCHAR__', '__GTCHAR__', '__GTCHAR__'), $tmparray[$key]['labelhtml']);
1261  $tmparray[$key]['labelhtml'] = str_replace(array('__LTCHAR__', '__GTCHAR__'), array('<span class="opacitymedium">(', ')</span>'), $tmparray[$key]['labelhtml']);
1262  }
1263 
1264  $withtocccselected = GETPOST("receiverccc", 'array'); // Array of selected value
1265 
1266  $out .= $form->multiselectarray("receiverccc", $tmparray, $withtocccselected, null, null, 'inline-block minwidth500', 0, 0);
1267  }
1268  }
1269 
1270  $showinfobcc = '';
1271  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_PROPOSAL_TO') && !empty($this->param['models']) && $this->param['models'] == 'propal_send') {
1272  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_PROPOSAL_TO');
1273  }
1274  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_ORDER_TO') && !empty($this->param['models']) && $this->param['models'] == 'order_send') {
1275  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_ORDER_TO');
1276  }
1277  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_INVOICE_TO') && !empty($this->param['models']) && $this->param['models'] == 'facture_send') {
1278  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_INVOICE_TO');
1279  }
1280  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO') && !empty($this->param['models']) && $this->param['models'] == 'supplier_proposal_send') {
1281  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO');
1282  }
1283  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO') && !empty($this->param['models']) && $this->param['models'] == 'order_supplier_send') {
1284  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO');
1285  }
1286  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO') && !empty($this->param['models']) && $this->param['models'] == 'invoice_supplier_send') {
1287  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO');
1288  }
1289  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_PROJECT_TO') && !empty($this->param['models']) && $this->param['models'] == 'project') { // don't know why there is not '_send' at end of this models name.
1290  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_PROJECT_TO');
1291  }
1292  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_SHIPMENT_TO') && !empty($this->param['models']) && $this->param['models'] == 'shipping_send') {
1293  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_SHIPMENT_TO');
1294  }
1295  if (getDolGlobalString('MAIN_MAIL_AUTOCOPY_RECEPTION_TO') && !empty($this->param['models']) && $this->param['models'] == 'reception_send') {
1296  $showinfobcc = getDolGlobalString('MAIN_MAIL_AUTOCOPY_RECEPTION_TO');
1297  }
1298  if ($showinfobcc) {
1299  $out .= ' + '.$showinfobcc;
1300  }
1301  $out .= "</td></tr>\n";
1302  return $out;
1303  }
1304 
1310  public function getHtmlForWithErrorsTo()
1311  {
1312  global $langs;
1313 
1314  //if (! $this->errorstomail) $this->errorstomail=$this->frommail;
1315  $errorstomail = getDolGlobalString('MAIN_MAIL_ERRORS_TO', (!empty($this->errorstomail) ? $this->errorstomail : ''));
1316  if ($this->witherrorstoreadonly) {
1317  $out = '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td>';
1318  $out .= '<input type="hidden" id="errorstomail" name="errorstomail" value="'.$errorstomail.'" />';
1319  $out .= $errorstomail;
1320  $out .= "</td></tr>\n";
1321  } else {
1322  $out = '<tr><td>'.$langs->trans("MailErrorsTo").'</td><td>';
1323  $out .= '<input class="minwidth200" id="errorstomail" name="errorstomail" value="'.$errorstomail.'" />';
1324  $out .= "</td></tr>\n";
1325  }
1326  return $out;
1327  }
1328 
1334  public function getHtmlForDeliveryreceipt()
1335  {
1336  global $langs;
1337 
1338  $out = '<tr><td><label for="deliveryreceipt">'.$langs->trans("DeliveryReceipt").'</label></td><td>';
1339 
1340  if (!empty($this->withdeliveryreceiptreadonly)) {
1341  $out .= yn($this->withdeliveryreceipt);
1342  } else {
1343  $defaultvaluefordeliveryreceipt = 0;
1344  if (getDolGlobalString('MAIL_FORCE_DELIVERY_RECEIPT_PROPAL') && !empty($this->param['models']) && $this->param['models'] == 'propal_send') {
1345  $defaultvaluefordeliveryreceipt = 1;
1346  }
1347  if (getDolGlobalString('MAIL_FORCE_DELIVERY_RECEIPT_SUPPLIER_PROPOSAL') && !empty($this->param['models']) && $this->param['models'] == 'supplier_proposal_send') {
1348  $defaultvaluefordeliveryreceipt = 1;
1349  }
1350  if (getDolGlobalString('MAIL_FORCE_DELIVERY_RECEIPT_ORDER') && !empty($this->param['models']) && $this->param['models'] == 'order_send') {
1351  $defaultvaluefordeliveryreceipt = 1;
1352  }
1353  if (getDolGlobalString('MAIL_FORCE_DELIVERY_RECEIPT_INVOICE') && !empty($this->param['models']) && $this->param['models'] == 'facture_send') {
1354  $defaultvaluefordeliveryreceipt = 1;
1355  }
1356  if (getDolGlobalString('MAIL_FORCE_DELIVERY_RECEIPT_SUPPLIER_ORDER') && !empty($this->param['models']) && $this->param['models'] == 'order_supplier_send') {
1357  $defaultvaluefordeliveryreceipt = 1;
1358  }
1359  //$out .= $form->selectyesno('deliveryreceipt', (GETPOSTISSET("deliveryreceipt") ? GETPOST("deliveryreceipt") : $defaultvaluefordeliveryreceipt), 1);
1360  $out .= '<input type="checkbox" id="deliveryreceipt" name="deliveryreceipt" value="1"'.((GETPOSTISSET("deliveryreceipt") ? GETPOST("deliveryreceipt") : $defaultvaluefordeliveryreceipt) ? ' checked="checked"' : '').'>';
1361  }
1362  $out .= "</td></tr>\n";
1363  return $out;
1364  }
1365 
1373  public function getHtmlForTopic($arraydefaultmessage, $helpforsubstitution)
1374  {
1375  global $langs, $form;
1376 
1377  $defaulttopic = GETPOST('subject', 'restricthtml');
1378 
1379  if (!GETPOST('modelselected', 'alpha') || GETPOST('modelmailselected') != '-1') {
1380  if ($arraydefaultmessage && $arraydefaultmessage->topic) {
1381  $defaulttopic = $arraydefaultmessage->topic;
1382  } elseif (!is_numeric($this->withtopic)) {
1383  $defaulttopic = $this->withtopic;
1384  }
1385  }
1386 
1387  $defaulttopic = make_substitutions($defaulttopic, $this->substit);
1388 
1389  $out = '<tr>';
1390  $out .= '<td class="fieldrequired">';
1391  $out .= $form->textwithpicto($langs->trans('MailTopic'), $helpforsubstitution, 1, 'help', '', 0, 2, 'substittooltipfromtopic');
1392  $out .= '</td>';
1393  $out .= '<td>';
1394  if ($this->withtopicreadonly) {
1395  $out .= $defaulttopic;
1396  $out .= '<input type="hidden" class="quatrevingtpercent" id="subject" name="subject" value="'.$defaulttopic.'" />';
1397  } else {
1398  $out .= '<input type="text" class="quatrevingtpercent" id="subject" name="subject" value="'.((GETPOSTISSET("subject") && !GETPOST('modelselected')) ? GETPOST("subject") : ($defaulttopic ? $defaulttopic : '')).'" />';
1399  }
1400  $out .= "</td></tr>\n";
1401  return $out;
1402  }
1403 
1412  public function getSectionForAIPrompt($function = 'textgeneration', $format = '', $htmlContent = 'message')
1413  {
1414  global $langs;
1415 
1416  $langs->load("other");
1417 
1418  $htmlContent = preg_replace('/[^a-z0-9_]/', '', $htmlContent);
1419 
1420  $out = '<tr id="ai_input" class="hidden">';
1421  $out .= '<td>';
1422  //$out .= $form->textwithpicto($langs->trans('HelpWithAI'), $langs->trans("YouCanMakeSomeInstructionForEmail"));
1423  $out .= '</td>';
1424 
1425  $out .= '<td>';
1426  $out .= '<input type="text" class="quatrevingtpercent" id="ai_instructions" name="instruction" placeholder="'.$langs->trans("EnterYourAIPromptHere").'..." />';
1427  $out .= '<input id="generate_button" type="button" class="button smallpaddingimp" value="'.$langs->trans('Generate').'"/>';
1428  $out .= '<div id="ai_status_message" class="fieldrequired hideobject marginrightonly margintoponly">';
1429  $out .= '<i class="fa fa-spinner fa-spin fa-2x fa-fw valignmiddle marginrightonly"></i>'.$langs->trans("AIProcessingPleaseWait", getDolGlobalString('AI_API_SERVICE', 'chatgpt'));
1430  $out .= '</div>';
1431  $out .= "</td></tr>\n";
1432 
1433  $out .= "<script type='text/javascript'>
1434  $(document).ready(function() {
1435  // for keydown
1436  $('#ai_instructions').keydown(function(event) {
1437  if (event.keyCode === 13) {
1438  event.preventDefault();
1439  $('#generate_button').click();
1440  }
1441  });
1442 
1443  $('#generate_button').click(function() {
1444  console.log('We click on generate ai button');
1445 
1446  var instructions = $('#ai_instructions').val();
1447  var timeoutfinished = 0;
1448  var apicallfinished = 0;
1449 
1450  $('#ai_status_message').show();
1451  $('.icon-container .loader').show();
1452  setTimeout(function() {
1453  timeoutfinished = 1;
1454  if (apicallfinished) {
1455  $('#ai_status_message').hide();
1456  }
1457  }, 2000);
1458 
1459  // set editor in readonly
1460  if (CKEDITOR.instances.".$htmlContent.") {
1461  CKEDITOR.instances.".$htmlContent.".setReadOnly(1);
1462  }
1463 
1464 
1465  $.ajax({
1466  url: '". DOL_URL_ROOT."/ai/ajax/generate_content.php?token=".currentToken()."',
1467  type: 'POST',
1468  contentType: 'application/json',
1469  data: JSON.stringify({
1470  'format': '".dol_escape_js($format)."', /* the format for output */
1471  'function': '".dol_escape_js($function)."', /* the AI feature to call */
1472  'instructions': instructions, /* the prompt string */
1473  }),
1474  success: function(response) {
1475  console.log('Add response into field \'".$htmlContent."\': '+response);
1476 
1477  jQuery('#".$htmlContent."').val(response);
1478  jQuery('#".$htmlContent."preview').val(response);
1479 
1480  if (CKEDITOR.instances) {
1481  var editorInstance = CKEDITOR.instances.".$htmlContent.";
1482  if (editorInstance) {
1483  editorInstance.setReadOnly(0);
1484  editorInstance.setData(response);
1485  }
1486  var editorInstancepreview = CKEDITOR.instances.".$htmlContent."preview;
1487  if (editorInstancepreview) {
1488  editorInstancepreview.setData(response);
1489  }
1490  }
1491 
1492  // remove readonly
1493  $('#ai_instructions').val('');
1494 
1495  apicallfinished = 1;
1496  if (timeoutfinished) {
1497  $('#ai_status_message').hide();
1498  }
1499  },
1500  error: function(xhr, status, error) {
1501  alert(error);
1502  console.error('error ajax', status, error);
1503  $('#ai_status_message').hide();
1504  }
1505 
1506  });
1507  });
1508  });
1509  </script>
1510  ";
1511  return $out;
1512  }
1513 
1520  public function getModelEmailTemplate($htmlContent = 'message')
1521  {
1522  require_once DOL_DOCUMENT_ROOT.'/core/lib/emaillayout.lib.php';
1523 
1524  $out = '<div id="template-selector" class="template-container hidden">';
1525  $templates = array(
1526  'empty' => 'empty',
1527  'basic' => 'basic',
1528  'news' => 'news',
1529  'commerce' => 'commerce',
1530  //'text' => 'text'
1531  );
1532 
1533  foreach ($templates as $template => $templateFunction) {
1534  $contentHtml = getHtmlOfLayout($template);
1535 
1536  $out .= '<div class="template-option" data-template="'.$template.'" data-content="'.htmlentities($contentHtml).'">';
1537  $out .= '<img class="maillayout" alt="'.$template.'" src="'.DOL_URL_ROOT.'/theme/common/maillayout/'.$template.'.png" />';
1538  $out .= '<span class="template-option-text">'.ucfirst($template).'</span>';
1539  $out .= '</div>';
1540  }
1541  $out .= '</div>';
1542 
1543  $out .= '<script type="text/javascript">
1544  $(document).ready(function() {
1545  $(".template-option").click(function() {
1546  console.log("We choose a layout for email");
1547  $(".template-option").removeClass("selected");
1548  $(this).addClass("selected");
1549 
1550  var template = $(this).data("template");
1551  var contentHtml = $(this).data("content");
1552 
1553  jQuery("#'.$htmlContent.'").val(contentHtml);
1554  var editorInstance = CKEDITOR.instances.'.$htmlContent.';
1555  if (editorInstance) {
1556  editorInstance.setData(contentHtml);
1557  }
1558  });
1559  });
1560  </script>';
1561 
1562  return $out;
1563  }
1564 
1582  public function getEMailTemplate($dbs, $type_template, $user, $outputlangs, $id = 0, $active = 1, $label = '', $defaultfortype = -1)
1583  {
1584  global $conf;
1585 
1586  if ($id == -2 && empty($label)) {
1587  $this->error = 'LabelIsMandatoryWhenIdIs-2or-3';
1588  return -1;
1589  }
1590 
1591  $ret = new ModelMail();
1592 
1593  $languagetosearch = (is_object($outputlangs) ? $outputlangs->defaultlang : '');
1594  // Define $languagetosearchmain to fall back on main language (for example to get 'es_ES' for 'es_MX')
1595  $tmparray = explode('_', $languagetosearch);
1596  $languagetosearchmain = $tmparray[0].'_'.strtoupper($tmparray[0]);
1597  if ($languagetosearchmain == $languagetosearch) {
1598  $languagetosearchmain = '';
1599  }
1600 
1601  $sql = "SELECT rowid, module, label, type_template, topic, email_from, joinfiles, content, content_lines, lang, email_from, email_to, email_tocc, email_tobcc";
1602  $sql .= " FROM ".$dbs->prefix().'c_email_templates';
1603  $sql .= " WHERE (type_template = '".$dbs->escape($type_template)."' OR type_template = 'all')";
1604  $sql .= " AND entity IN (".getEntity('c_email_templates').")";
1605  $sql .= " AND (private = 0 OR fk_user = ".((int) $user->id).")"; // Get all public or private owned
1606  if ($active >= 0) {
1607  $sql .= " AND active = ".((int) $active);
1608  }
1609  if ($defaultfortype >= 0) {
1610  $sql .= " AND defaultfortype = ".((int) $defaultfortype);
1611  }
1612  if ($label) {
1613  $sql .= " AND label = '".$dbs->escape($label)."'";
1614  }
1615  if (!($id > 0) && $languagetosearch) {
1616  $sql .= " AND (lang = '".$dbs->escape($languagetosearch)."'".($languagetosearchmain ? " OR lang = '".$dbs->escape($languagetosearchmain)."'" : "")." OR lang IS NULL OR lang = '')";
1617  }
1618  if ($id > 0) {
1619  $sql .= " AND rowid = ".(int) $id;
1620  }
1621  if ($id == -1) {
1622  $sql .= " AND position = 0";
1623  }
1624  if ($languagetosearch) {
1625  $sql .= $dbs->order("position,lang,label", "ASC,DESC,ASC"); // We want line with lang set first, then with lang null or ''
1626  } else {
1627  $sql .= $dbs->order("position,lang,label", "ASC,ASC,ASC"); // If no language provided, we give priority to lang not defined
1628  }
1629  //$sql .= $dbs->plimit(1);
1630  //print $sql;
1631 
1632  $resql = $dbs->query($sql);
1633  if (!$resql) {
1634  dol_print_error($dbs);
1635  return -1;
1636  }
1637 
1638  // Get first found
1639  while (1) {
1640  $obj = $dbs->fetch_object($resql);
1641 
1642  if ($obj) {
1643  // If template is for a module, check module is enabled; if not, take next template
1644  if ($obj->module) {
1645  $tempmodulekey = $obj->module;
1646  if (empty($conf->$tempmodulekey) || !isModEnabled($tempmodulekey)) {
1647  continue;
1648  }
1649  }
1650 
1651  // If a record was found
1652  $ret->id = $obj->rowid;
1653  $ret->module = $obj->module;
1654  $ret->label = $obj->label;
1655  $ret->lang = $obj->lang;
1656  $ret->topic = $obj->topic;
1657  $ret->content = $obj->content;
1658  $ret->content_lines = $obj->content_lines;
1659  $ret->joinfiles = $obj->joinfiles;
1660  $ret->email_from = $obj->email_from;
1661 
1662  break;
1663  } else {
1664  // If no record found
1665  if ($id == -2) {
1666  // Not found with the provided label
1667  return -1;
1668  } else {
1669  // If there is no template at all
1670  $defaultmessage = '';
1671 
1672  if ($type_template == 'body') {
1673  // Special case to use this->withbody as content
1674  $defaultmessage = $this->withbody;
1675  } elseif ($type_template == 'facture_send') {
1676  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendInvoice");
1677  } elseif ($type_template == 'facture_relance') {
1678  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendInvoiceReminder");
1679  } elseif ($type_template == 'propal_send') {
1680  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendProposal");
1681  } elseif ($type_template == 'supplier_proposal_send') {
1682  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendSupplierProposal");
1683  } elseif ($type_template == 'order_send') {
1684  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendOrder");
1685  } elseif ($type_template == 'order_supplier_send') {
1686  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendSupplierOrder");
1687  } elseif ($type_template == 'invoice_supplier_send') {
1688  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendSupplierInvoice");
1689  } elseif ($type_template == 'shipping_send') {
1690  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendShipping");
1691  } elseif ($type_template == 'fichinter_send') {
1692  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendFichInter");
1693  } elseif ($type_template == 'actioncomm_send') {
1694  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentSendActionComm");
1695  } elseif (!empty($type_template)) {
1696  $defaultmessage = $outputlangs->transnoentities("PredefinedMailContentGeneric");
1697  }
1698 
1699  $ret->label = 'default';
1700  $ret->lang = $outputlangs->defaultlang;
1701  $ret->topic = '';
1702  $ret->joinfiles = 1;
1703  $ret->content = $defaultmessage;
1704  $ret->content_lines = '';
1705 
1706  break;
1707  }
1708  }
1709  }
1710 
1711  $dbs->free($resql);
1712 
1713  return $ret;
1714  }
1715 
1725  public function isEMailTemplate($type_template, $user, $outputlangs)
1726  {
1727  $sql = "SELECT label, topic, content, lang";
1728  $sql .= " FROM ".$this->db->prefix().'c_email_templates';
1729  $sql .= " WHERE type_template='".$this->db->escape($type_template)."'";
1730  $sql .= " AND entity IN (".getEntity('c_email_templates').")";
1731  $sql .= " AND (fk_user is NULL or fk_user = 0 or fk_user = ".((int) $user->id).")";
1732  if (is_object($outputlangs)) {
1733  $sql .= " AND (lang = '".$this->db->escape($outputlangs->defaultlang)."' OR lang IS NULL OR lang = '')";
1734  }
1735  $sql .= $this->db->order("lang,label", "ASC");
1736  //print $sql;
1737 
1738  $resql = $this->db->query($sql);
1739  if ($resql) {
1740  $num = $this->db->num_rows($resql);
1741  $this->db->free($resql);
1742  return $num;
1743  } else {
1744  $this->error = get_class($this).' '.__METHOD__.' ERROR:'.$this->db->lasterror();
1745  return -1;
1746  }
1747  }
1748 
1759  public function fetchAllEMailTemplate($type_template, $user, $outputlangs, $active = 1)
1760  {
1761  global $conf;
1762 
1763  $sql = "SELECT rowid, module, label, topic, content, content_lines, lang, fk_user, private, position";
1764  $sql .= " FROM ".$this->db->prefix().'c_email_templates';
1765  $sql .= " WHERE type_template IN ('".$this->db->escape($type_template)."', 'all')";
1766  $sql .= " AND entity IN (".getEntity('c_email_templates').")";
1767  $sql .= " AND (private = 0 OR fk_user = ".((int) $user->id).")"; // See all public templates or templates I own.
1768  if ($active >= 0) {
1769  $sql .= " AND active = ".((int) $active);
1770  }
1771  //if (is_object($outputlangs)) $sql.= " AND (lang = '".$this->db->escape($outputlangs->defaultlang)."' OR lang IS NULL OR lang = '')"; // Return all languages
1772  $sql .= $this->db->order("position,lang,label", "ASC");
1773  //print $sql;
1774 
1775  $resql = $this->db->query($sql);
1776  if ($resql) {
1777  $num = $this->db->num_rows($resql);
1778  $this->lines_model = array();
1779  while ($obj = $this->db->fetch_object($resql)) {
1780  // If template is for a module, check module is enabled.
1781  if ($obj->module) {
1782  $tempmodulekey = $obj->module;
1783  if (empty($conf->$tempmodulekey) || !isModEnabled($tempmodulekey)) {
1784  continue;
1785  }
1786  }
1787 
1788  $line = new ModelMail();
1789  $line->id = $obj->rowid;
1790  $line->label = $obj->label;
1791  $line->lang = $obj->lang;
1792  $line->fk_user = $obj->fk_user;
1793  $line->private = $obj->private;
1794  $line->position = $obj->position;
1795  $line->topic = $obj->topic;
1796  $line->content = $obj->content;
1797  $line->content_lines = $obj->content_lines;
1798 
1799  $this->lines_model[] = $line;
1800  }
1801  $this->db->free($resql);
1802  return $num;
1803  } else {
1804  $this->error = get_class($this).' '.__METHOD__.' ERROR:'.$this->db->lasterror();
1805  return -1;
1806  }
1807  }
1808 
1809 
1810 
1819  public function setSubstitFromObject($object, $outputlangs)
1820  {
1821  global $extrafields;
1822 
1823  $parameters = array();
1824  $tmparray = getCommonSubstitutionArray($outputlangs, 0, null, $object);
1825  complete_substitutions_array($tmparray, $outputlangs, null, $parameters);
1826 
1827  $this->substit = $tmparray;
1828 
1829  // Fill substit_lines with each object lines content
1830  if (is_array($object->lines)) {
1831  foreach ($object->lines as $line) {
1832  $substit_line = array(
1833  '__PRODUCT_REF__' => isset($line->product_ref) ? $line->product_ref : '',
1834  '__PRODUCT_LABEL__' => isset($line->product_label) ? $line->product_label : '',
1835  '__PRODUCT_DESCRIPTION__' => isset($line->product_desc) ? $line->product_desc : '',
1836  '__LABEL__' => isset($line->label) ? $line->label : '',
1837  '__DESCRIPTION__' => isset($line->desc) ? $line->desc : '',
1838  '__DATE_START_YMD__' => dol_print_date($line->date_start, 'day', 0, $outputlangs),
1839  '__DATE_END_YMD__' => dol_print_date($line->date_end, 'day', 0, $outputlangs),
1840  '__QUANTITY__' => $line->qty,
1841  '__SUBPRICE__' => price($line->subprice),
1842  '__AMOUNT__' => price($line->total_ttc),
1843  '__AMOUNT_EXCL_TAX__' => price($line->total_ht)
1844  );
1845 
1846  // Create dynamic tags for __PRODUCT_EXTRAFIELD_FIELD__
1847  if (!empty($line->fk_product)) {
1848  if (!is_object($extrafields)) {
1849  $extrafields = new ExtraFields($this->db);
1850  }
1851  $product = new Product($this->db);
1852  $product->fetch($line->fk_product, '', '', 1);
1853  $product->fetch_optionals();
1854 
1855  $extrafields->fetch_name_optionals_label($product->table_element, true);
1856 
1857  if (!empty($extrafields->attributes[$product->table_element]['label']) && is_array($extrafields->attributes[$product->table_element]['label']) && count($extrafields->attributes[$product->table_element]['label']) > 0) {
1858  foreach ($extrafields->attributes[$product->table_element]['label'] as $key => $label) {
1859  $substit_line['__PRODUCT_EXTRAFIELD_'.strtoupper($key).'__'] = isset($product->array_options['options_'.$key]) ? $product->array_options['options_'.$key] : '';
1860  }
1861  }
1862  }
1863  $this->substit_lines[] = $substit_line;
1864  }
1865  }
1866  }
1867 
1876  public static function getAvailableSubstitKey($mode = 'formemail', $object = null)
1877  {
1878  global $langs;
1879 
1880  $tmparray = array();
1881  if ($mode == 'formemail' || $mode == 'formemailwithlines' || $mode == 'formemailforlines') {
1882  $parameters = array('mode' => $mode);
1883  $tmparray = getCommonSubstitutionArray($langs, 2, null, $object); // Note: On email templated edition, this is null because it is related to all type of objects
1884  complete_substitutions_array($tmparray, $langs, null, $parameters);
1885 
1886  if ($mode == 'formwithlines') {
1887  $tmparray['__LINES__'] = '__LINES__'; // Will be set by the get_form function
1888  }
1889  if ($mode == 'formforlines') {
1890  $tmparray['__QUANTITY__'] = '__QUANTITY__'; // Will be set by the get_form function
1891  }
1892  }
1893 
1894  if ($mode == 'emailing') {
1895  $parameters = array('mode' => $mode);
1896  $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
1897  complete_substitutions_array($tmparray, $langs, null, $parameters);
1898 
1899  // For mass emailing, we have different keys specific to the data into tagerts list
1900  $tmparray['__ID__'] = 'IdRecord';
1901  $tmparray['__THIRDPARTY_CUSTOMER_CODE__'] = 'CustomerCode';
1902  $tmparray['__EMAIL__'] = 'EMailRecipient';
1903  $tmparray['__LASTNAME__'] = 'Lastname';
1904  $tmparray['__FIRSTNAME__'] = 'Firstname';
1905  $tmparray['__MAILTOEMAIL__'] = 'TagMailtoEmail';
1906  $tmparray['__OTHER1__'] = 'Other1';
1907  $tmparray['__OTHER2__'] = 'Other2';
1908  $tmparray['__OTHER3__'] = 'Other3';
1909  $tmparray['__OTHER4__'] = 'Other4';
1910  $tmparray['__OTHER5__'] = 'Other5';
1911  $tmparray['__CHECK_READ__'] = $langs->trans('TagCheckMail');
1912  $tmparray['__UNSUBSCRIBE__'] = $langs->trans('TagUnsubscribe');
1913  $tmparray['__UNSUBSCRIBE_URL__'] = $langs->trans('TagUnsubscribe').' (URL)';
1914 
1915  $onlinepaymentenabled = 0;
1916  if (isModEnabled('paypal')) {
1917  $onlinepaymentenabled++;
1918  }
1919  if (isModEnabled('paybox')) {
1920  $onlinepaymentenabled++;
1921  }
1922  if (isModEnabled('stripe')) {
1923  $onlinepaymentenabled++;
1924  }
1925  if ($onlinepaymentenabled && getDolGlobalString('PAYMENT_SECURITY_TOKEN')) {
1926  $tmparray['__SECUREKEYPAYMENT__'] = getDolGlobalString('PAYMENT_SECURITY_TOKEN');
1927  if (getDolGlobalString('PAYMENT_SECURITY_TOKEN_UNIQUE')) {
1928  if (isModEnabled('member')) {
1929  $tmparray['__SECUREKEYPAYMENT_MEMBER__'] = 'SecureKeyPAYMENTUniquePerMember';
1930  }
1931  if (isModEnabled('don')) {
1932  $tmparray['__SECUREKEYPAYMENT_DONATION__'] = 'SecureKeyPAYMENTUniquePerDonation';
1933  }
1934  if (isModEnabled('invoice')) {
1935  $tmparray['__SECUREKEYPAYMENT_INVOICE__'] = 'SecureKeyPAYMENTUniquePerInvoice';
1936  }
1937  if (isModEnabled('order')) {
1938  $tmparray['__SECUREKEYPAYMENT_ORDER__'] = 'SecureKeyPAYMENTUniquePerOrder';
1939  }
1940  if (isModEnabled('contract')) {
1941  $tmparray['__SECUREKEYPAYMENT_CONTRACTLINE__'] = 'SecureKeyPAYMENTUniquePerContractLine';
1942  }
1943 
1944  //Online payment link
1945  if (isModEnabled('member')) {
1946  $tmparray['__ONLINEPAYMENTLINK_MEMBER__'] = 'OnlinePaymentLinkUniquePerMember';
1947  }
1948  if (isModEnabled('don')) {
1949  $tmparray['__ONLINEPAYMENTLINK_DONATION__'] = 'OnlinePaymentLinkUniquePerDonation';
1950  }
1951  if (isModEnabled('invoice')) {
1952  $tmparray['__ONLINEPAYMENTLINK_INVOICE__'] = 'OnlinePaymentLinkUniquePerInvoice';
1953  }
1954  if (isModEnabled('order')) {
1955  $tmparray['__ONLINEPAYMENTLINK_ORDER__'] = 'OnlinePaymentLinkUniquePerOrder';
1956  }
1957  if (isModEnabled('contract')) {
1958  $tmparray['__ONLINEPAYMENTLINK_CONTRACTLINE__'] = 'OnlinePaymentLinkUniquePerContractLine';
1959  }
1960  }
1961  } else {
1962  /* No need to show into tooltip help, option is not enabled
1963  $vars['__SECUREKEYPAYMENT__']='';
1964  $vars['__SECUREKEYPAYMENT_MEMBER__']='';
1965  $vars['__SECUREKEYPAYMENT_INVOICE__']='';
1966  $vars['__SECUREKEYPAYMENT_ORDER__']='';
1967  $vars['__SECUREKEYPAYMENT_CONTRACTLINE__']='';
1968  */
1969  }
1970  if (getDolGlobalString('MEMBER_ENABLE_PUBLIC')) {
1971  $tmparray['__PUBLICLINK_NEWMEMBERFORM__'] = 'BlankSubscriptionForm';
1972  }
1973  }
1974 
1975  foreach ($tmparray as $key => $val) {
1976  if (empty($val)) {
1977  $tmparray[$key] = $key;
1978  }
1979  }
1980 
1981  return $tmparray;
1982  }
1983 }
1984 
1985 
1991 class ModelMail
1992 {
1996  public $id;
1997 
2001  public $label;
2002 
2006  public $fk_user;
2007 
2011  public $private;
2012 
2016  public $topic;
2017 
2021  public $content;
2022  public $content_lines;
2023  public $lang;
2024  public $joinfiles;
2025 
2026  public $email_from;
2027  public $email_to;
2028  public $email_tocc;
2029  public $email_tobcc;
2030 
2034  public $module;
2035 
2039  public $position;
2040 }
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition: security.php:604
print $object position
Definition: edit.php:195
Class to manage a WYSIWYG editor.
Class to offer components to list and upload files.
Class to manage generation of HTML components Only common components must be here.
static selectarray($htmlname, $array, $id='', $show_empty=0, $key_in_label=0, $value_as_key=0, $moreparam='', $translate=0, $maxlen=0, $disabled=0, $sort='', $morecss='minwidth75', $addjscombo=1, $moreparamonempty='', $disablebademail=0, $nohtmlescape=0)
Return a HTML select string, built from an array of key+value.
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
get_attached_files()
Return list of attached files (stored in SECTION array)
getHtmlForWithErrorsTo()
get Html For WithErrorsTo
getHtmlForTopic($arraydefaultmessage, $helpforsubstitution)
Return Html section for the Topic of message.
clear_attached_files()
Clear list of attached files in send mail form (also stored in session)
fetchAllEMailTemplate($type_template, $user, $outputlangs, $active=1)
Find if template exists and are available for current user, then set them into $this->lines_module.
getHtmlForCc()
get html For CC
getHtmlForTo()
get html For To
add_attached_files($path, $file='', $type='')
Add a file into the list of attached files (stored in SECTION array)
getHtmlForWithCcc()
get html For WithCCC This information is show when MAIN_EMAIL_USECCC is set.
remove_attached_files($keytodelete)
Remove a file from the list of attached files (stored in SECTION array)
__construct($db)
Constructor.
getHtmlForDeliveryreceipt()
get Html For Asking for Delivery Receipt
getEMailTemplate($dbs, $type_template, $user, $outputlangs, $id=0, $active=1, $label='', $defaultfortype=-1)
Return templates of email with type = $type_template or type = 'all'.
show_form($addfileaction='addfile', $removefileaction='removefile')
Show the form to input an email this->withfile: 0=No attaches files, 1=Show attached files,...
get_form($addfileaction='addfile', $removefileaction='removefile')
Get the form to input an email this->withfile: 0=No attaches files, 1=Show attached files,...
Class to manage translations.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:744
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
Definition: files.lib.php:1589
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dolGetFirstLineOfText($text, $nboflines=1, $charset='UTF-8')
Return first line of text.
dol_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto='UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
img_mime($file, $titlealt='', $morecss='')
Show MIME img of a file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_htmlentities($string, $flags=ENT_QUOTES|ENT_SUBSTITUTE, $encoding='UTF-8', $double_encode=false)
Replace htmlentities functions.
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_textishtml($msg, $option=0)
Return if a text is a html content.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information in HTML for admin users or standard users.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
table tableforfield button
0 = Do not include form tag and submit button -1 = Do not include form tag but include submit button
Definition: style.css.php:886
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:122
getMaxFileSizeArray()
Return the max allowed for file upload.