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