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