dolibarr 23.0.3
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
41'
42@phan-var-force int<0,1> $diroutput
43@phan-var-force string $defaulttopic
44@phan-var-force string $defaulttopiclang
45@phan-var-force string[] $arrayoffamiliestoexclude
46@phan-var-force string $file
47@phan-var-force CommonObject $object
48';
49
50// Protection to avoid direct call of template
51if (empty($conf) || !is_object($conf)) {
52 print "Error, template page can't be called as URL";
53 exit(1);
54}
55
56$fileparams = array();
57$file = null;
58
59if ($action == 'presend') {
60 $langs->load("mails");
61
62 $titreform = 'SendMail';
63
64 $object->fetchProject();
65
66 $ref = dol_sanitizeFileName($object->ref);
67 if (!in_array($object->element, array('user', 'member'))) {
68 //$fileparams['fullname'] can be filled from the card
69 //Get also the main_lastdoc field of $object. If not found, try to guess with following code
70 if (!empty($object->last_main_doc) && is_readable(DOL_DATA_ROOT.'/'.$object->last_main_doc) && is_file(DOL_DATA_ROOT.'/'.$object->last_main_doc)) {
71 $fileparams['fullname'] = DOL_DATA_ROOT.'/'.$object->last_main_doc;
72 } else {
73 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
74 // Special case
75 if ($object->element == 'invoice_supplier') {
76 $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
77 } else {
78 $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
79 }
80 }
81
82 $file = isset($fileparams['fullname']) ? $fileparams['fullname'] : null;
83 }
84
85 // Define output language
86 $outputlangs = $langs;
87 $newlang = '';
88 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
89 if (is_object($object->thirdparty)) {
90 $newlang = $object->thirdparty->default_lang;
91 }
92 if (GETPOST('lang_id', 'aZ09')) {
93 $newlang = GETPOST('lang_id', 'aZ09');
94 }
95 }
96
97 if (!empty($newlang)) {
98 $outputlangs = new Translate('', $conf);
99 $outputlangs->setDefaultLang($newlang);
100 // Load traductions files required by page
101 $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'main', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions', 'sendings'));
102 if (!empty($defaulttopiclang)) {
103 $outputlangs->loadLangs(array($defaulttopiclang));
104 }
105 }
106
107 $topicmail = '';
108 if (empty($object->ref_client)) {
109 $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
110 } elseif (!empty($object->ref_client)) {
111 $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REF_CLIENT__)');
112 }
113
114 // Build document if it does not exists
115 $forcebuilddoc = true;
116 // except for some cases where it can not exists
117 if (in_array($object->element, array('user', 'member'))) {
118 $forcebuilddoc = false;
119 }
120 if ($object->element == 'invoice_supplier' && !getDolGlobalString('INVOICE_SUPPLIER_ADDON_PDF')) {
121 $forcebuilddoc = false;
122 }
123 if ($object->element == 'project' && !getDolGlobalString('PROJECT_ADDON_PDF')) {
124 $forcebuilddoc = false;
125 }
126 if ($object->element == 'project_task' && !getDolGlobalString('PROJECT_TASK_ADDON_PDF')) {
127 $forcebuilddoc = false;
128 }
129 if ($object->element == 'societe' && !getDolGlobalString('COMPANY_ADDON_PDF')) {
130 $forcebuilddoc = false;
131 }
132
133 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
134 if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument')) {
135 $hidedetails = $hidedetails?$hidedetails:'';
136 $hidedesc = $hidedetails?$hidedetails:'';
137 $hideref = $hidedetails?$hidedetails:'';
138
139 $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
140 if ($result < 0) {
141 dol_print_error($db, $object->error, $object->errors);
142 exit();
143 }
144 if ($object->element == 'invoice_supplier') {
145 $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
146 } else {
147 $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
148 }
149
150 $file = isset($fileparams['fullname']) ? $fileparams['fullname'] : null;
151 }
152 }
153
154 print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
155 print '<div class="clearboth"></div>';
156 print '<br>';
157 print load_fiche_titre($langs->trans($titreform));
158
159 print dol_get_fiche_head([], '', '', -1);
160
161 // Create form for email
162 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
163 $formmail = new FormMail($db);
164
165 $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
166 $formmail->fromtype = (GETPOST('fromtype') ? GETPOST('fromtype') : getDolGlobalString('MAIN_MAIL_DEFAULT_FROMTYPE', 'user'));
167
168 if ($formmail->fromtype === 'user') {
169 $formmail->fromid = $user->id;
170 }
171 if ($object->element == 'salary' && getDolGlobalString('INVOICE_EMAIL_SENDER')) {
172 $formmail->frommail = getDolGlobalString('SINVOICE_EMAIL_SENDER');
173 $formmail->fromname = getDolGlobalString('INVOICE_EMAIL_SENDER_NAME', '');
174 $formmail->fromtype = 'special';
175 }
176 if ($object->element === 'facture' && getDolGlobalString('INVOICE_EMAIL_SENDER')) {
177 $formmail->frommail = getDolGlobalString('INVOICE_EMAIL_SENDER');
178 $formmail->fromname = getDolGlobalString('INVOICE_EMAIL_SENDER_NAME', '');
179 $formmail->fromtype = 'special';
180 }
181 if ($object->element === 'shipping' && getDolGlobalString('SHIPPING_EMAIL_SENDER')) {
182 $formmail->frommail = getDolGlobalString('SHIPPING_EMAIL_SENDER');
183 $formmail->fromname = getDolGlobalString('SHIPPING_EMAIL_SENDER_NAME', '');
184 $formmail->fromtype = 'special';
185 }
186 if ($object->element === 'commande' && getDolGlobalString('COMMANDE_EMAIL_SENDER')) {
187 $formmail->frommail = getDolGlobalString('COMMANDE_EMAIL_SENDER');
188 $formmail->fromname = getDolGlobalString('COMMANDE_EMAIL_SENDER_NAME', '');
189 $formmail->fromtype = 'special';
190 }
191 if ($object->element === 'order_supplier' && getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER')) {
192 $formmail->frommail = getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER');
193 $formmail->fromname = getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER_NAME', '');
194 $formmail->fromtype = 'special';
195 }
196 if ($object->element === 'recruitmentcandidature') {
197 $formmail->frommail = getDolGlobalString('RECRUITMENT_EMAIL_SENDER', (!empty($recruitermail) ? $recruitermail : ''));
198 $formmail->fromname = getDolGlobalString('RECRUITMENT_EMAIL_SENDER_NAME', (!empty($recruitername) ? $recruitername : ''));
199 $formmail->fromtype = 'special';
200 }
201
202 // Set the default "From"
203 $defaultfrom = '';
204 if (GETPOSTISSET('fromtype')) {
205 $defaultfrom = GETPOST('fromtype');
206 } else {
207 $parameters = array();
208 $reshook = $hookmanager->executeHooks('getDefaultFromEmail', $parameters, $formmail);
209 if (empty($reshook)) {
210 $defaultfrom = $formmail->fromtype;
211 }
212 if (!empty($hookmanager->resArray['defaultfrom'])) {
213 $defaultfrom = $hookmanager->resArray['defaultfrom'];
214 }
215 }
216 $formmail->fromtype = $defaultfrom;
217
218 $formmail->trackid = empty($trackid) ? '' : $trackid;
219 $formmail->inreplyto = empty($inreplyto) ? '' : $inreplyto;
220 $formmail->withfrom = 1;
221 $formmail->withlayout = 'email';
222 $formmail->withaiprompt = 'html';
223
224
225 // Define $liste, a list of recipients with email inside <>.
226 $liste = array();
227 if ($object->element == 'expensereport') {
228 '@phan-var-force ExpenseReport $object';
229 $fuser = new User($db);
230 $fuser->fetch($object->fk_user_author);
231 $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
232 } elseif ($object->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
233 '@phan-var-force Partnership $object';
234 $fadherent = new Adherent($db);
235 $fadherent->fetch($object->fk_member);
236 $liste['member'] = $fadherent->getFullName($outputlangs)." <".$fadherent->email.">";
237 } elseif ($object->element == 'societe') {
238 '@phan-var-force Societe $object';
239 foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
240 $liste[$key] = $value;
241 }
242 } elseif ($object->element == 'contact') {
243 '@phan-var-force Contact $object';
244 $liste['contact'] = $object->getFullName($outputlangs)." <".$object->email.">";
245 } elseif ($object->element == 'user' || $object->element == 'member') {
246 '@phan-var-force User|Adherent $object';
247 $liste['thirdparty'] = $object->getFullName($outputlangs)." <".$object->email.">";
248 } elseif ($object->element == 'salary') {
249 '@phan-var-force Salary $object';
250 $fuser = new User($db);
251 $fuser->fetch($object->fk_user);
252 $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
253 } else {
254 // For example if element is project
255 if (!empty($object->socid) && $object->socid > 0 && !is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
256 $object->fetch_thirdparty();
257 }
258 if (is_object($object->thirdparty)) {
259 foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
260 $liste[$key] = $value;
261 }
262 }
263 }
264
265 if (getDolGlobalString('MAIN_MAIL_ENABLED_USER_DEST_SELECT')) {
266 $listeuser = array();
267 $fuserdest = new User($db);
268 $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);
269 if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
270 foreach ($fuserdest->users as $uuserdest) {
271 $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
272 }
273 } elseif ($result < 0) {
274 setEventMessages(null, $fuserdest->errors, 'errors');
275 }
276 if (count($listeuser) > 0) {
277 $formmail->withtouser = $listeuser;
278 $formmail->withtoccuser = $listeuser;
279 }
280 }
281
282 //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
283 if (!isset($arrayoffamiliestoexclude)) {
284 $arrayoffamiliestoexclude = null;
285 }
286
287 // Make substitution in email content
288 if (!empty($object)) {
289 // First we set ->substit (useless, it will be erased later) and ->substit_lines
290 $formmail->setSubstitFromObject($object, $langs);
291 }
292 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
293
294 $emailsendersignature = null;
295 // Overwrite __SENDEREMAIL_SIGNATURE__ with value select into form
296 if ($formmail->fromtype) {
297 $reg = array();
298 if (preg_match('/user/', $formmail->fromtype, $reg)) {
299 $emailsendersignature = $user->signature;
300 } elseif (preg_match('/company/', $formmail->fromtype, $reg)) {
301 $emailsendersignature = '';
302 } elseif (preg_match('/senderprofile_(\d+)/', $formmail->fromtype, $reg)) {
303 $sql = "SELECT rowid, label, email, signature FROM ".$db->prefix()."c_email_senderprofile";
304 $sql .= " WHERE rowid = ".((int) $reg[1]);
305 $resql = $db->query($sql);
306 if ($resql) {
307 $obj = $db->fetch_object($resql);
308 if ($obj) {
309 $emailsendersignature = $obj->signature;
310 }
311 }
312 }
313 }
314 $substitutionarray['__SENDEREMAIL_SIGNATURE__'] = $emailsendersignature;
315
316 $substitutionarray['__CHECK_READ__'] = "";
317 if (is_object($object) && is_object($object->thirdparty)) {
318 $checkRead = '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php';
319 // @phan-suppress-next-line PhanUndeclaredProperty;
320 $checkRead .= '?tag='.(!empty($object->thirdparty->tag) ? urlencode($object->thirdparty->tag) : "");
321 $checkRead .= '&securitykey='.(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY') ? urlencode(getDolGlobalString('MAILING_EMAIL_UNSUBSCRIBE_KEY')) : "");
322 $checkRead .= '" width="1" height="1" style="width:1px;height:1px" border="0"/>';
323 $substitutionarray['__CHECK_READ__'] = $checkRead;
324 }
325 $substitutionarray['__CONTACTCIVNAME__'] = '';
326 $parameters = array(
327 'mode' => 'formemail'
328 );
329 complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
330
331 // Find all external contact addresses
332 $tmpobject = $object;
333 if (($object->element == 'shipping' || $object->element == 'reception')) {
334 $origin = $object->origin;
335 $origin_id = $object->origin_id;
336
337 if (!empty($origin) && !empty($origin_id)) {
338 $element = $subelement = $origin;
339
340 if ($element == 'order_supplier') {
341 $element = 'fourn';
342 $subelement = 'fournisseur.commande';
343 $origin = 'CommandeFournisseur';
344 }
345
346 $regs = array();
347 if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
348 $element = $regs[1];
349 $subelement = $regs[2];
350 }
351 // For compatibility
352 if ($element == 'order') {
353 $element = $subelement = 'commande';
354 }
355 if ($element == 'propal') {
356 $element = 'comm/propal';
357 $subelement = 'propal';
358 }
359 if ($element == 'contract') {
360 $element = $subelement = 'contrat';
361 }
362 if ($element == 'inter') {
363 $element = $subelement = 'fichinter';
364 }
365 if ($element == 'shipping') {
366 $element = $subelement = 'expedition';
367 }
368 if ($element == 'project') {
369 $element = 'projet';
370 }
371
372 dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
373 $classname = ucfirst($origin);
374 $objectsrc = new $classname($db);
375 '@phan-var-force Commande|Facture $objectsrc';
376 $objectsrc->fetch($origin_id);
377
378 $tmpobject = $objectsrc;
379 }
380 }
381
382 $contactarr = array();
383 $contactarr = $tmpobject->liste_contact(-1, 'external', 0, '', 1);
384
385 if (is_array($contactarr) && count($contactarr) > 0) {
386 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
387 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
388 $contactstatic = new Contact($db);
389 $tmpcompany = new Societe($db);
390
391 foreach ($contactarr as $contact) {
392 $contactstatic->fetch($contact['id']);
393 // Complete substitution array
394 $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
395 $substitutionarray['__CONTACT_LASTNAME_'.$contact['code'].'__'] = $contactstatic->lastname;
396 $substitutionarray['__CONTACT_FIRSTNAME_'.$contact['code'].'__'] = $contactstatic->firstname;
397 $substitutionarray['__CONTACT_TITLE_'.$contact['code'].'__'] = $contactstatic->getCivilityLabel();
398
399 // Complete $liste with the $contact
400 if (empty($liste[$contact['id']])) { // If this contact id not already into the $liste
401 $contacttoshow = '';
402 if (isset($object->thirdparty) && is_object($object->thirdparty)) {
403 if ($contactstatic->fk_soc != $object->thirdparty->id) {
404 $tmpcompany->fetch($contactstatic->fk_soc);
405 if ($tmpcompany->id > 0) {
406 $contacttoshow .= $tmpcompany->name.': ';
407 }
408 }
409 }
410 $contacttoshow .= $contactstatic->getFullName($outputlangs, 1);
411 $contacttoshow .= " <".($contactstatic->email ? $contactstatic->email : $langs->transnoentitiesnoconv("NoEMail")) .">";
412 $liste[$contact['id']] = $contacttoshow;
413 }
414 }
415 }
416
417 $formmail->withto = $liste;
418 $formmail->withtofree = (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1');
419 $formmail->withtocc = $liste;
420 $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
421 $formmail->withtopic = $topicmail;
422 $formmail->withfile = 2;
423 $formmail->withbody = 1;
424 $formmail->withdeliveryreceipt = 1;
425 $formmail->withcancel = 1;
426
427 // Array of substitutions
428 $formmail->substit = $substitutionarray;
429
430 // Array of other parameters
431 $formmail->param['action'] = 'send';
432 $formmail->param['models'] = $modelmail;
433 $formmail->param['models_id'] = GETPOSTINT('modelmailselected');
434 $formmail->param['id'] = $object->id;
435 $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
436 $formmail->param['fileinit'] = array($file);
437 $formmail->param['object_entity'] = $object->entity;
438
439 // Show form
440 print $formmail->get_form();
441
442 print dol_get_fiche_end();
443}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
Class to manage members of a foundation.
Class to manage contact/addresses.
Class permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new Form...
Class to manage third parties objects (customers, suppliers, prospects...)
Class to manage translations.
Class to manage Dolibarr users.
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.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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.