dolibarr  19.0.0-dev
card_presend.tpl.php
1 <?php
2 /* Copyright (C) 2017-2018 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2022 Charlene Benke <charlene@patas-monkey.com>
4  * Copyright (C) 2023 Maxime Nicolas <maxime@oarces.com>
5  * Copyright (C) 2023 Benjamin GREMBI <benjamin@oarces.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <https://www.gnu.org/licenses/>.
19  * or see https://www.gnu.org/
20  */
21 
22 /*
23  * Code to ouput content when action is presend
24  *
25  * $trackid must be defined
26  * $modelmail
27  * $defaulttopic
28  * $diroutput
29  * $arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
30  */
31 
32 // Protection to avoid direct call of template
33 if (empty($conf) || !is_object($conf)) {
34  print "Error, template page can't be called as URL";
35  exit;
36 }
37 
38 
39 if ($action == 'presend') {
40  $langs->load("mails");
41 
42  $titreform = 'SendMail';
43 
44  $object->fetch_projet();
45  if (!isset($file)) $file = null;
46  $ref = dol_sanitizeFileName($object->ref);
47  if (!in_array($object->element, array('user', 'member'))) {
48  //$fileparams['fullname'] can be filled from the card
49  //Get also the main_lastdoc field of $object. If not found, try to guess with following code
50  if (!empty($object->last_main_doc) && is_readable(DOL_DATA_ROOT.'/'.$object->last_main_doc) && is_file(DOL_DATA_ROOT.'/'.$object->last_main_doc)) {
51  $fileparams['fullname'] = DOL_DATA_ROOT.'/'.$object->last_main_doc;
52  } else {
53  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
54  // Special case
55  if ($object->element == 'invoice_supplier') {
56  $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
57  } else {
58  $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
59  }
60  }
61 
62  $file = isset($fileparams['fullname'])?$fileparams['fullname']:null;
63  }
64 
65  // Define output language
66  $outputlangs = $langs;
67  $newlang = '';
68  if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
69  $newlang = $object->thirdparty->default_lang;
70  if (GETPOST('lang_id', 'aZ09')) {
71  $newlang = GETPOST('lang_id', 'aZ09');
72  }
73  }
74 
75  if (!empty($newlang)) {
76  $outputlangs = new Translate('', $conf);
77  $outputlangs->setDefaultLang($newlang);
78  // Load traductions files required by page
79  $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions', 'sendings'));
80  }
81 
82  $topicmail = '';
83  if (empty($object->ref_client)) {
84  $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
85  } elseif (!empty($object->ref_client)) {
86  $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REF_CLIENT__)');
87  }
88 
89  // Build document if it not exists
90  $forcebuilddoc = true;
91  if (in_array($object->element, array('user', 'member'))) {
92  $forcebuilddoc = false;
93  }
94  if ($object->element == 'invoice_supplier' && empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) {
95  $forcebuilddoc = false;
96  }
97  if ($object->element == 'societe' && empty($conf->global->COMPANY_ADDON_PDF)) {
98  $forcebuilddoc = false;
99  }
100  if ($forcebuilddoc) { // If there is no default value for supplier invoice, we do not generate file, even if modelpdf was set by a manual generation
101  if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument')) {
102  $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
103  if ($result < 0) {
104  dol_print_error($db, $object->error, $object->errors);
105  exit();
106  }
107  if ($object->element == 'invoice_supplier') {
108  $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
109  } else {
110  $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
111  }
112 
113  $file = isset($fileparams['fullname'])?$fileparams['fullname']:null;
114  }
115  }
116 
117  print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
118  print '<div class="clearboth"></div>';
119  print '<br>';
120  print load_fiche_titre($langs->trans($titreform));
121 
122  print dol_get_fiche_head('', '', '', -1);
123 
124  // Create form for email
125  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
126  $formmail = new FormMail($db);
127 
128  $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
129  $formmail->fromtype = (GETPOST('fromtype') ?GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
130 
131  if ($formmail->fromtype === 'user') {
132  $formmail->fromid = $user->id;
133  }
134  if ($object->element == 'salary' && !empty($conf->global->INVOICE_EMAIL_SENDER)) {
135  $formmail->frommail = $conf->global->SINVOICE_EMAIL_SENDER;
136  $formmail->fromname = (!empty($conf->global->INVOICE_EMAIL_SENDER_NAME) ? $conf->global->INVOICE_EMAIL_SENDER_NAME : '');
137  $formmail->fromtype = 'special';
138  }
139  if ($object->element === 'facture' && !empty($conf->global->INVOICE_EMAIL_SENDER)) {
140  $formmail->frommail = $conf->global->INVOICE_EMAIL_SENDER;
141  $formmail->fromname = (!empty($conf->global->INVOICE_EMAIL_SENDER_NAME) ? $conf->global->INVOICE_EMAIL_SENDER_NAME : '');
142  $formmail->fromtype = 'special';
143  }
144  if ($object->element === 'shipping' && !empty($conf->global->SHIPPING_EMAIL_SENDER)) {
145  $formmail->frommail = $conf->global->SHIPPING_EMAIL_SENDER;
146  $formmail->fromname = (!empty($conf->global->SHIPPING_EMAIL_SENDER_NAME) ? $conf->global->SHIPPING_EMAIL_SENDER_NAME : '');
147  $formmail->fromtype = 'special';
148  }
149  if ($object->element === 'commande' && !empty($conf->global->COMMANDE_EMAIL_SENDER)) {
150  $formmail->frommail = $conf->global->COMMANDE_EMAIL_SENDER;
151  $formmail->fromname = (!empty($conf->global->COMMANDE_EMAIL_SENDER_NAME) ? $conf->global->COMMANDE_EMAIL_SENDER_NAME : '');
152  $formmail->fromtype = 'special';
153  }
154  if ($object->element === 'order_supplier' && !empty($conf->global->ORDER_SUPPLIER_EMAIL_SENDER)) {
155  $formmail->frommail = $conf->global->ORDER_SUPPLIER_EMAIL_SENDER;
156  $formmail->fromname = (!empty($conf->global->ORDER_SUPPLIER_EMAIL_SENDER_NAME) ? $conf->global->ORDER_SUPPLIER_EMAIL_SENDER_NAME : '');
157  $formmail->fromtype = 'special';
158  }
159  if ($object->element === 'recruitmentcandidature' ) {
160  $formmail->frommail = (!empty($conf->global->RECRUITMENT_EMAIL_SENDER) ? $conf->global->RECRUITMENT_EMAIL_SENDER : $recruitermail);
161  $formmail->fromname = (!empty($conf->global->RECRUITMENT_EMAIL_SENDER_NAME) ? $conf->global->RECRUITMENT_EMAIL_SENDER_NAME : (!empty($recruitername) ? $recruitername : ''));
162  $formmail->fromtype = 'special';
163  }
164 
165  // Set the default "From"
166  $defaultfrom = '';
167  if (GETPOSTISSET('fromtype')) {
168  $defaultfrom = GETPOST('fromtype');
169  } else {
170  $parameters = array();
171  $reshook = $hookmanager->executeHooks('getDefaultFromEmail', $parameters, $formmail);
172  if (empty($reshook)) {
173  $defaultfrom = $formmail->fromtype;
174  }
175  if (!empty($hookmanager->resArray['defaultfrom'])) {
176  $defaultfrom = $hookmanager->resArray['defaultfrom'];
177  }
178  }
179  $formmail->fromtype = $defaultfrom;
180 
181  $formmail->trackid = empty($trackid) ? '' : $trackid;
182  $formmail->inreplyto = empty($inreplyto) ? '' : $inreplyto;
183  $formmail->withfrom = 1;
184 
185  // Define $liste, a list of recipients with email inside <>.
186  $liste = array();
187  if ($object->element == 'expensereport') {
188  $fuser = new User($db);
189  $fuser->fetch($object->fk_user_author);
190  $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
191  } elseif ($object->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
192  $fadherent = new Adherent($db);
193  $fadherent->fetch($object->fk_member);
194  $liste['member'] = $fadherent->getFullName($outputlangs)." <".$fadherent->email.">";
195  } elseif ($object->element == 'societe') {
196  foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
197  $liste[$key] = $value;
198  }
199  } elseif ($object->element == 'contact') {
200  $liste['contact'] = $object->getFullName($outputlangs)." <".$object->email.">";
201  } elseif ($object->element == 'user' || $object->element == 'member') {
202  $liste['thirdparty'] = $object->getFullName($outputlangs)." <".$object->email.">";
203  } elseif ($object->element == 'salary') {
204  $fuser = new User($db);
205  $fuser->fetch($object->fk_user);
206  $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
207  } else {
208  if (!empty($object->socid) && $object->socid > 0 && !is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
209  $object->fetch_thirdparty();
210  }
211  if (is_object($object->thirdparty)) {
212  foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
213  $liste[$key] = $value;
214  }
215  }
216  }
217  if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
218  $listeuser = array();
219  $fuserdest = new User($db);
220 
221  $result = $fuserdest->fetchAll('ASC', 't.lastname', 0, 0, array('customsql'=>"t.statut=1 AND t.employee=1 AND t.email IS NOT NULL AND t.email <> ''"), 'AND', true);
222  if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
223  foreach ($fuserdest->users as $uuserdest) {
224  $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
225  }
226  } elseif ($result < 0) {
227  setEventMessages(null, $fuserdest->errors, 'errors');
228  }
229  if (count($listeuser) > 0) {
230  $formmail->withtouser = $listeuser;
231  $formmail->withtoccuser = $listeuser;
232  }
233  }
234 
235  //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
236  if (!isset($arrayoffamiliestoexclude)) {
237  $arrayoffamiliestoexclude = null;
238  }
239 
240  // Make substitution in email content
241  if (!empty($object)) {
242  // First we set ->substit (useless, it will be erased later) and ->substit_lines
243  $formmail->setSubstitFromObject($object, $langs);
244  }
245  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
246 
247  // Overwrite __SENDEREMAIL_SIGNATURE__ with value select into form
248  if ($formmail->fromtype) {
249  $reg = array();
250  if (preg_match('/user/', $formmail->fromtype, $reg)) {
251  $emailsendersignature = $user->signature;
252  } elseif (preg_match('/company/', $formmail->fromtype, $reg)) {
253  $emailsendersignature = '';
254  } elseif (preg_match('/senderprofile_(\d+)/', $formmail->fromtype, $reg)) {
255  $sql = "SELECT rowid, label, email, signature FROM ".$db->prefix()."c_email_senderprofile";
256  $sql .= " WHERE rowid = ".((int) $reg[1]);
257  $resql = $db->query($sql);
258  if ($resql) {
259  $obj = $db->fetch_object($resql);
260  if ($obj) {
261  $emailsendersignature = $obj->signature;
262  }
263  }
264  }
265  }
266  $substitutionarray['__SENDEREMAIL_SIGNATURE__'] = $emailsendersignature;
267 
268  $substitutionarray['__CHECK_READ__'] = "";
269  if (is_object($object) && is_object($object->thirdparty)) {
270  $checkRead= '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php';
271  $checkRead.='?tag='.(!empty($object->thirdparty->tag)?urlencode($object->thirdparty->tag):"");
272  $checkRead.='&securitykey='.(!empty(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY'))?urlencode(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')):"");
273  $checkRead.='" width="1" height="1" style="width:1px;height:1px" border="0"/>';
274  $substitutionarray['__CHECK_READ__'] = $checkRead;
275  }
276  $substitutionarray['__PERSONALIZED__'] = ''; // deprecated
277  $substitutionarray['__CONTACTCIVNAME__'] = '';
278  $parameters = array(
279  'mode' => 'formemail'
280  );
281  complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
282 
283  // Find all external contact addresses
284  $tmpobject = $object;
285  if (($object->element == 'shipping' || $object->element == 'reception')) {
286  $origin = $object->origin;
287  $origin_id = $object->origin_id;
288 
289  if (!empty($origin) && !empty($origin_id)) {
290  $element = $subelement = $origin;
291  $regs = array();
292  if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
293  $element = $regs[1];
294  $subelement = $regs[2];
295  }
296  // For compatibility
297  if ($element == 'order') {
298  $element = $subelement = 'commande';
299  }
300  if ($element == 'propal') {
301  $element = 'comm/propal';
302  $subelement = 'propal';
303  }
304  if ($element == 'contract') {
305  $element = $subelement = 'contrat';
306  }
307  if ($element == 'inter') {
308  $element = $subelement = 'ficheinter';
309  }
310  if ($element == 'shipping') {
311  $element = $subelement = 'expedition';
312  }
313  if ($element == 'order_supplier') {
314  $element = 'fourn';
315  $subelement = 'fournisseur.commande';
316  }
317  if ($element == 'project') {
318  $element = 'projet';
319  }
320 
321  dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
322  $classname = ucfirst($origin);
323  $objectsrc = new $classname($db);
324  $objectsrc->fetch($origin_id);
325 
326  $tmpobject = $objectsrc;
327  }
328  }
329 
330  $contactarr = array();
331  $contactarr = $tmpobject->liste_contact(-1, 'external', 0, '', 1);
332 
333  if (is_array($contactarr) && count($contactarr) > 0) {
334  require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
335  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
336  $contactstatic = new Contact($db);
337  $tmpcompany = new Societe($db);
338 
339  foreach ($contactarr as $contact) {
340  $contactstatic->fetch($contact['id']);
341  // Complete substitution array
342  $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
343  $substitutionarray['__CONTACT_LASTNAME_'.$contact['code'].'__'] = $contactstatic->lastname;
344  $substitutionarray['__CONTACT_FIRSTNAME_'.$contact['code'].'__'] = $contactstatic->firstname;
345  $substitutionarray['__CONTACT_TITLE_'.$contact['code'].'__'] = $contactstatic->getCivilityLabel();
346 
347  // Complete $liste with the $contact
348  if (empty($liste[$contact['id']])) { // If this contact id not already into the $liste
349  $contacttoshow = '';
350  if (isset($object->thirdparty) && is_object($object->thirdparty)) {
351  if ($contactstatic->fk_soc != $object->thirdparty->id) {
352  $tmpcompany->fetch($contactstatic->fk_soc);
353  if ($tmpcompany->id > 0) {
354  $contacttoshow .= $tmpcompany->name.': ';
355  }
356  }
357  }
358  $contacttoshow .= $contactstatic->getFullName($outputlangs, 1);
359  $contacttoshow .= " <".($contactstatic->email ? $contactstatic->email : $langs->transnoentitiesnoconv("NoEMail")) .">";
360  $liste[$contact['id']] = $contacttoshow;
361  }
362  }
363  }
364 
365  $formmail->withto = $liste;
366  $formmail->withtofree = (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1');
367  $formmail->withtocc = $liste;
368  $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
369  $formmail->withtopic = $topicmail;
370  $formmail->withfile = 2;
371  $formmail->withbody = 1;
372  $formmail->withdeliveryreceipt = 1;
373  $formmail->withcancel = 1;
374 
375  // Array of substitutions
376  $formmail->substit = $substitutionarray;
377 
378  // Array of other parameters
379  $formmail->param['action'] = 'send';
380  $formmail->param['models'] = $modelmail;
381  $formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
382  $formmail->param['id'] = $object->id;
383  $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
384  $formmail->param['fileinit'] = array($file);
385 
386  // Show form
387  print $formmail->get_form();
388 
389  print dol_get_fiche_end();
390 }
Class to manage members of a foundation.
Class to manage contact/addresses.
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_most_recent_file($dir, $regexfilter='', $excludefilter=array('(\.meta|_preview.*\.png)$', '^\.'), $nohook=false, $mode='')
Return file(s) into a directory (by default most recent)
Definition: files.lib.php:2524
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname from a POST of a form.
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.