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