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