dolibarr 24.0.0-beta
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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 * or see https://www.gnu.org/
22 */
23
47'
48@phan-var-force int<0,1> $diroutput
49@phan-var-force string $defaulttopic
50@phan-var-force string $defaulttopiclang
51@phan-var-force string[] $arrayoffamiliestoexclude
52@phan-var-force string $file
53@phan-var-force string $modelmail
54@phan-var-force CommonObject $object
55';
56
57// Protection to avoid direct call of template
58if (empty($conf) || !is_object($conf)) {
59 print "Error, template page can't be called as URL";
60 exit(1);
61}
62
63$fileparams = array();
64$file = null;
65if (!isset($modelmail)) {
66 $modelmail = '';
67}
68
69if ($action == 'presend') {
70 $langs->load("mails");
71
72 $titreform = 'SendMail';
73
74 $object->fetchProject();
75
76 $ref = dol_sanitizeFileName($object->ref);
77 if (!in_array($object->element, array('user', 'member'))) {
78 //$fileparams['fullname'] can be filled from the card
79 //Get also the main_lastdoc field of $object. If not found, try to guess with following code
80 if (!empty($object->last_main_doc) && is_readable(DOL_DATA_ROOT.'/'.$object->last_main_doc) && is_file(DOL_DATA_ROOT.'/'.$object->last_main_doc)) {
81 $fileparams['fullname'] = DOL_DATA_ROOT.'/'.$object->last_main_doc;
82 } else {
83 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
84 // Special case
85 if ($object->element == 'invoice_supplier') {
86 $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
87 } else {
88 $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
89 }
90 }
91
92 $file = isset($fileparams['fullname']) ? $fileparams['fullname'] : null;
93 }
94
95 // Define output language
96 $outputlangs = $langs;
97 $newlang = '';
98 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
99 if (!is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
100 $object->fetch_thirdparty();
101 }
102 if (is_object($object->thirdparty)) {
103 $newlang = $object->thirdparty->default_lang;
104 }
105 if (GETPOST('lang_id', 'aZ09')) {
106 $newlang = GETPOST('lang_id', 'aZ09');
107 }
108 // When the email form is resubmitted (e.g. Apply button to select a template),
109 // lang_id is not in POST but langsmodels is. Use it to preserve the language selection.
110 if (empty($newlang) && GETPOST('langsmodels', 'aZ09')) {
111 $newlang = GETPOST('langsmodels', 'aZ09');
112 }
113 }
114
115 if (!empty($newlang)) {
116 $outputlangs = new Translate('', $conf);
117 $outputlangs->setDefaultLang($newlang);
118 // Load traductions files required by page
119 $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'main', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions', 'sendings'));
120 if (!empty($defaulttopiclang)) {
121 $outputlangs->loadLangs(array($defaulttopiclang));
122 }
123 }
124
125 $topicmail = '';
126 if (empty($object->ref_client)) {
127 $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
128 } elseif (!empty($object->ref_client)) {
129 $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REF_CLIENT__)');
130 }
131
132 // Build document if it does not exists
133 $forcebuilddoc = true;
134 // except for some cases where it can not exists
135 if (in_array($object->element, array('user', 'member'))) {
136 $forcebuilddoc = false;
137 }
138 if ($object->element == 'invoice_supplier' && !getDolGlobalString('INVOICE_SUPPLIER_ADDON_PDF')) {
139 $forcebuilddoc = false;
140 }
141 if ($object->element == 'project' && !getDolGlobalString('PROJECT_ADDON_PDF')) {
142 $forcebuilddoc = false;
143 }
144 if ($object->element == 'project_task' && !getDolGlobalString('PROJECT_TASK_ADDON_PDF')) {
145 $forcebuilddoc = false;
146 }
147 if ($object->element == 'societe' && !getDolGlobalString('COMPANY_ADDON_PDF')) {
148 $forcebuilddoc = false;
149 }
150
151 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
152 if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument')) {
153 $hidedetails = empty($hidedetails) ? '' : $hidedetails;
154 $hidedesc = empty($hidedesc) ? '' : $hidedesc;
155 $hideref = empty($hideref) ? '' : $hideref;
156
157 $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
158 if ($result < 0) {
159 dol_syslog(__FILE__.' generateDocument failed for '.$object->element.' id='.(empty($object->id) ? 0 : $object->id).' error='.$object->error, LOG_ERR);
160 setEventMessages($object->error, $object->errors, 'errors');
161 }
162 if ($result >= 0) {
163 if ($object->element == 'invoice_supplier') {
164 $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
165 } else {
166 $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
167 }
168
169 $file = isset($fileparams['fullname']) ? $fileparams['fullname'] : null;
170 }
171 }
172 }
173
174 print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
175 print '<div class="clearboth"></div>';
176 print '<br>';
177 print load_fiche_titre($langs->trans($titreform));
178
179 print dol_get_fiche_head([], '', '', -1);
180
181 // Create form for email
182 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
183 $formmail = new FormMail($db);
184
185 $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
186 $formmail->fromtype = (GETPOST('fromtype') ? GETPOST('fromtype') : getDolGlobalString('MAIN_MAIL_DEFAULT_FROMTYPE', 'user'));
187
188 if ($formmail->fromtype === 'user') {
189 $formmail->fromid = $user->id;
190 }
191 if ($object->element == 'salary' && getDolGlobalString('INVOICE_EMAIL_SENDER')) {
192 $formmail->frommail = getDolGlobalString('SINVOICE_EMAIL_SENDER');
193 $formmail->fromname = getDolGlobalString('INVOICE_EMAIL_SENDER_NAME', '');
194 $formmail->fromtype = 'special';
195 }
196 if ($object->element === 'facture' && getDolGlobalString('INVOICE_EMAIL_SENDER')) {
197 $formmail->frommail = getDolGlobalString('INVOICE_EMAIL_SENDER');
198 $formmail->fromname = getDolGlobalString('INVOICE_EMAIL_SENDER_NAME', '');
199 $formmail->fromtype = 'special';
200 }
201 if ($object->element === 'shipping' && getDolGlobalString('SHIPPING_EMAIL_SENDER')) {
202 $formmail->frommail = getDolGlobalString('SHIPPING_EMAIL_SENDER');
203 $formmail->fromname = getDolGlobalString('SHIPPING_EMAIL_SENDER_NAME', '');
204 $formmail->fromtype = 'special';
205 }
206 if ($object->element === 'commande' && getDolGlobalString('COMMANDE_EMAIL_SENDER')) {
207 $formmail->frommail = getDolGlobalString('COMMANDE_EMAIL_SENDER');
208 $formmail->fromname = getDolGlobalString('COMMANDE_EMAIL_SENDER_NAME', '');
209 $formmail->fromtype = 'special';
210 }
211 if ($object->element === 'order_supplier' && getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER')) {
212 $formmail->frommail = getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER');
213 $formmail->fromname = getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER_NAME', '');
214 $formmail->fromtype = 'special';
215 }
216 if ($object->element === 'recruitmentcandidature') {
217 $formmail->frommail = getDolGlobalString('RECRUITMENT_EMAIL_SENDER', (!empty($recruitermail) ? $recruitermail : ''));
218 $formmail->fromname = getDolGlobalString('RECRUITMENT_EMAIL_SENDER_NAME', (!empty($recruitername) ? $recruitername : ''));
219 $formmail->fromtype = 'special';
220 }
221
222 // Set the default "From"
223 $defaultfrom = '';
224 if (GETPOSTISSET('fromtype')) {
225 $defaultfrom = GETPOST('fromtype');
226 } else {
227 $parameters = array();
228 $reshook = $hookmanager->executeHooks('getDefaultFromEmail', $parameters, $formmail);
229 if (empty($reshook)) {
230 $defaultfrom = $formmail->fromtype;
231 }
232 if (!empty($hookmanager->resArray['defaultfrom'])) {
233 $defaultfrom = $hookmanager->resArray['defaultfrom'];
234 }
235 }
236 $formmail->fromtype = $defaultfrom;
237
238 $formmail->trackid = empty($trackid) ? '' : $trackid;
239 $formmail->inreplyto = empty($inreplyto) ? '' : $inreplyto;
240 $formmail->withfrom = 1;
241 $formmail->withlayout = 'email';
242 $formmail->withaiprompt = 'html';
243
244
245 // Define $liste, a list of recipients with email inside <>.
246 $liste = array();
247 if ($object->element == 'expensereport') {
248 '@phan-var-force ExpenseReport $object';
249 $fuser = new User($db);
250 $fuser->fetch($object->fk_user_author);
251 $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
252 } elseif ($object->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
253 '@phan-var-force Partnership $object';
254 $fadherent = new Adherent($db);
255 $fadherent->fetch($object->fk_member);
256 $liste['member'] = $fadherent->getFullName($outputlangs)." <".$fadherent->email.">";
257 } elseif ($object->element == 'societe') {
258 '@phan-var-force Societe $object';
259 foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
260 $liste[$key] = $value;
261 }
262 } elseif ($object->element == 'contact') {
263 '@phan-var-force Contact $object';
264 $liste['contact'] = $object->getFullName($outputlangs)." <".$object->email.">";
265 } elseif ($object->element == 'user' || $object->element == 'member') {
266 '@phan-var-force User|Adherent $object';
267 $liste['thirdparty'] = $object->getFullName($outputlangs)." <".$object->email.">";
268 } elseif ($object->element == 'salary') {
269 '@phan-var-force Salary $object';
270 $fuser = new User($db);
271 $fuser->fetch($object->fk_user);
272 $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
273 } else {
274 // For example if element is project
275 // @phan-suppress-next-line PhanUndeclaredProperty
276 if (property_exists($object, 'socid') && !empty($object->socid) && $object->socid > 0 && !is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
277 $object->fetch_thirdparty();
278 }
279 if (is_object($object->thirdparty)) {
280 foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
281 $liste[$key] = $value;
282 }
283 }
284 }
285
286 if (getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
287 $listeuser = array();
288 $fuserdest = new User($db);
289 $result = $fuserdest->fetchAll('ASC', 't.lastname', 0, 0, "(t.statut:=:1) AND (t.employee:=:1) AND (t.email:isnot:NULL) AND (t.email:!=:'')", 'AND', true);
290 if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
291 foreach ($fuserdest->users as $uuserdest) {
292 $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
293 }
294 } elseif ($result < 0) {
295 setEventMessages(null, $fuserdest->errors, 'errors');
296 }
297 if (count($listeuser) > 0) {
298 $formmail->withtouser = $listeuser;
299 $formmail->withtoccuser = $listeuser;
300 }
301 }
302
303 //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
304 if (!isset($arrayoffamiliestoexclude)) {
305 $arrayoffamiliestoexclude = null;
306 }
307
308 // Make substitution in email content
309 if (!empty($object)) {
310 // First we set ->substit (useless, it will be erased later) and ->substit_lines
311 $formmail->setSubstitFromObject($object, $outputlangs);
312 }
313 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
314
315 $emailsendersignature = null;
316 // Overwrite __SENDEREMAIL_SIGNATURE__ with value select into form
317 if ($formmail->fromtype) {
318 $reg = array();
319 if (preg_match('/user/', $formmail->fromtype, $reg)) {
320 $emailsendersignature = $user->signature;
321 } elseif (preg_match('/company/', $formmail->fromtype, $reg)) {
322 $emailsendersignature = '';
323 } elseif (preg_match('/senderprofile_(\d+)/', $formmail->fromtype, $reg)) {
324 $sql = "SELECT rowid, label, email, signature FROM ".$db->prefix()."c_email_senderprofile";
325 $sql .= " WHERE rowid = ".((int) $reg[1]);
326 $resql = $db->query($sql);
327 if ($resql) {
328 $obj = $db->fetch_object($resql);
329 if ($obj) {
330 $emailsendersignature = $obj->signature;
331 }
332 }
333 }
334 }
335 $substitutionarray['__SENDEREMAIL_SIGNATURE__'] = $emailsendersignature;
336
337 $substitutionarray['__CHECK_READ__'] = "";
338 if (is_object($object) && is_object($object->thirdparty)) {
339 $checkRead = '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php';
340 // @phan-suppress-next-line PhanUndeclaredProperty;
341 $checkRead .= '?tag='.(!empty($object->thirdparty->tag) ? urlencode($object->thirdparty->tag) : "");
342 $checkRead .= '&securitykey='.(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY') ? urlencode(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')) : "");
343 $checkRead .= '" width="1" height="1" style="width:1px;height:1px" border="0"/>';
344 $substitutionarray['__CHECK_READ__'] = $checkRead;
345 }
346 $substitutionarray['__CONTACTCIVNAME__'] = '';
347 $parameters = array(
348 'mode' => 'formemail'
349 );
350 complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
351
352 // Find all external contact addresses
353 $tmpobject = $object;
354 if (($object->element == 'shipping' || $object->element == 'reception')) {
355 $origin = $object->origin;
356 $origin_id = $object->origin_id;
357
358 if (!empty($origin) && !empty($origin_id)) {
359 $element = $subelement = $origin;
360
361 if ($element == 'order_supplier') {
362 $element = 'fourn';
363 $subelement = 'fournisseur.commande';
364 $origin = 'CommandeFournisseur';
365 }
366
367 $regs = array();
368 if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
369 $element = $regs[1];
370 $subelement = $regs[2];
371 }
372 // For compatibility
373 if ($element == 'order') {
374 $element = $subelement = 'commande';
375 }
376 if ($element == 'propal') {
377 $element = 'comm/propal';
378 $subelement = 'propal';
379 }
380 if ($element == 'contract') {
381 $element = $subelement = 'contrat';
382 }
383 if ($element == 'inter') {
384 $element = $subelement = 'fichinter';
385 }
386 if ($element == 'shipping') {
387 $element = $subelement = 'expedition';
388 }
389 if ($element == 'project') {
390 $element = 'projet';
391 }
392
393 dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
394 $classname = ucfirst($origin);
395 $objectsrc = new $classname($db);
396 '@phan-var-force Commande|Facture $objectsrc';
397 $objectsrc->fetch($origin_id);
398
399 $tmpobject = $objectsrc;
400 }
401 }
402
403 $contactarr = array();
404 $contactarr = $tmpobject->liste_contact(-1, 'external', 0, '', 1);
405
406 if (is_array($contactarr) && count($contactarr) > 0) {
407 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
408 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
409 $contactstatic = new Contact($db);
410 $tmpcompany = new Societe($db);
411
412 foreach ($contactarr as $contact) {
413 $contactstatic->fetch($contact['id']);
414 // Complete substitution array
415 $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
416 $substitutionarray['__CONTACT_LASTNAME_'.$contact['code'].'__'] = $contactstatic->lastname;
417 $substitutionarray['__CONTACT_FIRSTNAME_'.$contact['code'].'__'] = $contactstatic->firstname;
418 $substitutionarray['__CONTACT_TITLE_'.$contact['code'].'__'] = $contactstatic->getCivilityLabel();
419
420 // Complete $liste with the $contact
421 if (empty($liste[$contact['id']])) { // If this contact id not already into the $liste
422 $contacttoshow = '';
423 if (isset($object->thirdparty) && is_object($object->thirdparty)) {
424 if ($contactstatic->fk_soc != $object->thirdparty->id) {
425 $tmpcompany->fetch($contactstatic->fk_soc);
426 if ($tmpcompany->id > 0) {
427 $contacttoshow .= $tmpcompany->name.': ';
428 }
429 }
430 }
431 $contacttoshow .= $contactstatic->getFullName($outputlangs, 1);
432 $contacttoshow .= " <".($contactstatic->email ? $contactstatic->email : $langs->transnoentitiesnoconv("NoEMail")) .">";
433 $liste[$contact['id']] = $contacttoshow;
434 }
435 }
436 }
437
438 $formmail->withto = $liste;
439 $formmail->withtofree = (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1');
440 $formmail->withtocc = $liste;
441 $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
442 $formmail->withtopic = $topicmail;
443 $formmail->withfile = 2;
444 $formmail->withbody = 1;
445 $formmail->withdeliveryreceipt = 1;
446 $formmail->withcancel = 1;
447
448 // Array of substitutions
449 $formmail->substit = $substitutionarray;
450
451 // Array of other parameters
452 $formmail->param['action'] = 'send';
453 $formmail->param['models'] = $modelmail;
454 $formmail->param['models_id'] = GETPOSTINT('modelmailselected');
455 $formmail->param['id'] = $object->id;
456 $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
457 $formmail->param['fileinit'] = array($file);
458 $formmail->param['object_entity'] = $object->entity;
459
460 // Show form
461 print $formmail->get_form();
462
463 print dol_get_fiche_end();
464}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage members of a foundation.
Class to manage contact/addresses.
Class to manage a HTML form to send a unitary email Usage: $formail = new FormMail($db) $formmail->pr...
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
Class to manage Dolibarr users.
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_most_recent_file($dir, $regexfilter='', $excludefilter=array('(\.meta|_preview.*\.png) $', '^\.'), $nohook=0, $mode=0)
Return file(s) into a directory (by default most recent)
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
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.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
getDolGlobalString($key, $default='')
Return a 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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.