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