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