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