dolibarr 21.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 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
24/*
25 * Code to output content when action is presend
26 *
27 * $trackid must be defined
28 * $modelmail
29 * $defaulttopic and $defaulttopiclang
30 * $diroutput
31 * $arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
32 * $file
33 */
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 CommonObject $object
54';
55
56// Protection to avoid direct call of template
57if (empty($conf) || !is_object($conf)) {
58 print "Error, template page can't be called as URL";
59 exit(1);
60}
61
62$fileparams = array();
63
64if ($action == 'presend') {
65 $langs->load("mails");
66
67 $titreform = 'SendMail';
68
69 $object->fetchProject();
70
71 $ref = dol_sanitizeFileName($object->ref);
72 if (!in_array($object->element, array('user', 'member'))) {
73 //$fileparams['fullname'] can be filled from the card
74 //Get also the main_lastdoc field of $object. If not found, try to guess with following code
75 if (!empty($object->last_main_doc) && is_readable(DOL_DATA_ROOT.'/'.$object->last_main_doc) && is_file(DOL_DATA_ROOT.'/'.$object->last_main_doc)) {
76 $fileparams['fullname'] = DOL_DATA_ROOT.'/'.$object->last_main_doc;
77 } else {
78 include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
79 // Special case
80 if ($object->element == 'invoice_supplier') {
81 $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
82 } else {
83 $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
84 }
85 }
86
87 $file = isset($fileparams['fullname']) ? $fileparams['fullname'] : null;
88 }
89
90 // Define output language
91 $outputlangs = $langs;
92 $newlang = '';
93 if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang)) {
94 if (is_object($object->thirdparty)) {
95 $newlang = $object->thirdparty->default_lang;
96 }
97 if (GETPOST('lang_id', 'aZ09')) {
98 $newlang = GETPOST('lang_id', 'aZ09');
99 }
100 }
101
102 if (!empty($newlang)) {
103 $outputlangs = new Translate('', $conf);
104 $outputlangs->setDefaultLang($newlang);
105 // Load traductions files required by page
106 $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'main', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions', 'sendings'));
107 if (!empty($defaulttopiclang)) {
108 $outputlangs->loadLangs(array($defaulttopiclang));
109 }
110 }
111
112 $topicmail = '';
113 if (empty($object->ref_client)) {
114 $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
115 } elseif (!empty($object->ref_client)) {
116 $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REF_CLIENT__)');
117 }
118
119 // Build document if it does not exists
120 $forcebuilddoc = true;
121 // except for some cases where it can not exists
122 if (in_array($object->element, array('user', 'member'))) {
123 $forcebuilddoc = false;
124 }
125 if ($object->element == 'invoice_supplier' && !getDolGlobalString('INVOICE_SUPPLIER_ADDON_PDF')) {
126 $forcebuilddoc = false;
127 }
128 if ($object->element == 'project' && !getDolGlobalString('PROJECT_ADDON_PDF')) {
129 $forcebuilddoc = false;
130 }
131 if ($object->element == 'project_task' && !getDolGlobalString('PROJECT_TASK_ADDON_PDF')) {
132 $forcebuilddoc = false;
133 }
134 if ($object->element == 'societe' && !getDolGlobalString('COMPANY_ADDON_PDF')) {
135 $forcebuilddoc = false;
136 }
137
138 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
139 if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument')) {
140 $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
141 if ($result < 0) {
142 dol_print_error($db, $object->error, $object->errors);
143 exit();
144 }
145 if ($object->element == 'invoice_supplier') {
146 $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
147 } else {
148 $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
149 }
150
151 $file = isset($fileparams['fullname']) ? $fileparams['fullname'] : null;
152 }
153 }
154
155 print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
156 print '<div class="clearboth"></div>';
157 print '<br>';
158 print load_fiche_titre($langs->trans($titreform));
159
160 print dol_get_fiche_head([], '', '', -1);
161
162 // Create form for email
163 include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
164 $formmail = new FormMail($db);
165
166 $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
167 $formmail->fromtype = (GETPOST('fromtype') ? GETPOST('fromtype') : getDolGlobalString('MAIN_MAIL_DEFAULT_FROMTYPE', 'user'));
168
169 if ($formmail->fromtype === 'user') {
170 $formmail->fromid = $user->id;
171 }
172 if ($object->element == 'salary' && getDolGlobalString('INVOICE_EMAIL_SENDER')) {
173 $formmail->frommail = getDolGlobalString('SINVOICE_EMAIL_SENDER');
174 $formmail->fromname = getDolGlobalString('INVOICE_EMAIL_SENDER_NAME', '');
175 $formmail->fromtype = 'special';
176 }
177 if ($object->element === 'facture' && getDolGlobalString('INVOICE_EMAIL_SENDER')) {
178 $formmail->frommail = getDolGlobalString('INVOICE_EMAIL_SENDER');
179 $formmail->fromname = getDolGlobalString('INVOICE_EMAIL_SENDER_NAME', '');
180 $formmail->fromtype = 'special';
181 }
182 if ($object->element === 'shipping' && getDolGlobalString('SHIPPING_EMAIL_SENDER')) {
183 $formmail->frommail = getDolGlobalString('SHIPPING_EMAIL_SENDER');
184 $formmail->fromname = getDolGlobalString('SHIPPING_EMAIL_SENDER_NAME', '');
185 $formmail->fromtype = 'special';
186 }
187 if ($object->element === 'commande' && getDolGlobalString('COMMANDE_EMAIL_SENDER')) {
188 $formmail->frommail = getDolGlobalString('COMMANDE_EMAIL_SENDER');
189 $formmail->fromname = getDolGlobalString('COMMANDE_EMAIL_SENDER_NAME', '');
190 $formmail->fromtype = 'special';
191 }
192 if ($object->element === 'order_supplier' && getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER')) {
193 $formmail->frommail = getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER');
194 $formmail->fromname = getDolGlobalString('ORDER_SUPPLIER_EMAIL_SENDER_NAME', '');
195 $formmail->fromtype = 'special';
196 }
197 if ($object->element === 'recruitmentcandidature') {
198 $formmail->frommail = getDolGlobalString('RECRUITMENT_EMAIL_SENDER', (!empty($recruitermail) ? $recruitermail : ''));
199 $formmail->fromname = getDolGlobalString('RECRUITMENT_EMAIL_SENDER_NAME', (!empty($recruitername) ? $recruitername : ''));
200 $formmail->fromtype = 'special';
201 }
202
203 // Set the default "From"
204 $defaultfrom = '';
205 if (GETPOSTISSET('fromtype')) {
206 $defaultfrom = GETPOST('fromtype');
207 } else {
208 $parameters = array();
209 $reshook = $hookmanager->executeHooks('getDefaultFromEmail', $parameters, $formmail);
210 if (empty($reshook)) {
211 $defaultfrom = $formmail->fromtype;
212 }
213 if (!empty($hookmanager->resArray['defaultfrom'])) {
214 $defaultfrom = $hookmanager->resArray['defaultfrom'];
215 }
216 }
217 $formmail->fromtype = $defaultfrom;
218
219 $formmail->trackid = empty($trackid) ? '' : $trackid;
220 $formmail->inreplyto = empty($inreplyto) ? '' : $inreplyto;
221 $formmail->withfrom = 1;
222 $formmail->withlayout = 'email';
223 $formmail->withaiprompt = 'html';
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 $regs = array();
340 if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
341 $element = $regs[1];
342 $subelement = $regs[2];
343 }
344 // For compatibility
345 if ($element == 'order') {
346 $element = $subelement = 'commande';
347 }
348 if ($element == 'propal') {
349 $element = 'comm/propal';
350 $subelement = 'propal';
351 }
352 if ($element == 'contract') {
353 $element = $subelement = 'contrat';
354 }
355 if ($element == 'inter') {
356 $element = $subelement = 'fichinter';
357 }
358 if ($element == 'shipping') {
359 $element = $subelement = 'expedition';
360 }
361 if ($element == 'order_supplier') {
362 $element = 'fourn';
363 $subelement = 'fournisseur.commande';
364 }
365 if ($element == 'project') {
366 $element = 'projet';
367 }
368
369 dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
370 $classname = ucfirst($origin);
371 $objectsrc = new $classname($db);
372 '@phan-var-force Commande|Facture $objectsrc';
373 $objectsrc->fetch($origin_id);
374
375 $tmpobject = $objectsrc;
376 }
377 }
378
379 $contactarr = array();
380 $contactarr = $tmpobject->liste_contact(-1, 'external', 0, '', 1);
381
382 if (is_array($contactarr) && count($contactarr) > 0) {
383 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
384 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
385 $contactstatic = new Contact($db);
386 $tmpcompany = new Societe($db);
387
388 foreach ($contactarr as $contact) {
389 $contactstatic->fetch($contact['id']);
390 // Complete substitution array
391 $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
392 $substitutionarray['__CONTACT_LASTNAME_'.$contact['code'].'__'] = $contactstatic->lastname;
393 $substitutionarray['__CONTACT_FIRSTNAME_'.$contact['code'].'__'] = $contactstatic->firstname;
394 $substitutionarray['__CONTACT_TITLE_'.$contact['code'].'__'] = $contactstatic->getCivilityLabel();
395
396 // Complete $liste with the $contact
397 if (empty($liste[$contact['id']])) { // If this contact id not already into the $liste
398 $contacttoshow = '';
399 if (isset($object->thirdparty) && is_object($object->thirdparty)) {
400 if ($contactstatic->fk_soc != $object->thirdparty->id) {
401 $tmpcompany->fetch($contactstatic->fk_soc);
402 if ($tmpcompany->id > 0) {
403 $contacttoshow .= $tmpcompany->name.': ';
404 }
405 }
406 }
407 $contacttoshow .= $contactstatic->getFullName($outputlangs, 1);
408 $contacttoshow .= " <".($contactstatic->email ? $contactstatic->email : $langs->transnoentitiesnoconv("NoEMail")) .">";
409 $liste[$contact['id']] = $contacttoshow;
410 }
411 }
412 }
413
414 $formmail->withto = $liste;
415 $formmail->withtofree = (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1');
416 $formmail->withtocc = $liste;
417 $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
418 $formmail->withtopic = $topicmail;
419 $formmail->withfile = 2;
420 $formmail->withbody = 1;
421 $formmail->withdeliveryreceipt = 1;
422 $formmail->withcancel = 1;
423
424 // Array of substitutions
425 $formmail->substit = $substitutionarray;
426
427 // Array of other parameters
428 $formmail->param['action'] = 'send';
429 $formmail->param['models'] = $modelmail;
430 $formmail->param['models_id'] = GETPOSTINT('modelmailselected');
431 $formmail->param['id'] = $object->id;
432 $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
433 $formmail->param['fileinit'] = array($file);
434 $formmail->param['object_entity'] = $object->entity;
435
436 // Show form
437 print $formmail->get_form();
438
439 print dol_get_fiche_end();
440}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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)
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...
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null, $include=null)
Return array of possible common substitutions.
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