dolibarr  16.0.5
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  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <https://www.gnu.org/licenses/>.
17  * or see https://www.gnu.org/
18  */
19 
20 /*
21  * Code to ouput content when action is presend
22  *
23  * $trackid must be defined
24  * $modelmail
25  * $defaulttopic
26  * $diroutput
27  * $arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
28  */
29 
30 // Protection to avoid direct call of template
31 if (empty($conf) || !is_object($conf)) {
32  print "Error, template page can't be called as URL";
33  exit;
34 }
35 
36 
37 if ($action == 'presend') {
38  $langs->load("mails");
39 
40  $titreform = 'SendMail';
41 
42  $object->fetch_projet();
43 
44  if (!in_array($object->element, array('societe', 'user', 'member'))) {
45  // TODO get also the main_lastdoc field of $object. If not found, try to guess with following code
46 
47  $ref = dol_sanitizeFileName($object->ref);
48  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
49  // Special case
50  if ($object->element == 'invoice_supplier') {
51  $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
52  } else {
53  $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
54  }
55 
56  $file = $fileparams['fullname'];
57  }
58 
59  // Define output language
60  $outputlangs = $langs;
61  $newlang = '';
62  if (!empty($conf->global->MAIN_MULTILANGS) && empty($newlang)) {
63  $newlang = $object->thirdparty->default_lang;
64  if (GETPOST('lang_id', 'aZ09')) {
65  $newlang = GETPOST('lang_id', 'aZ09');
66  }
67  }
68 
69  if (!empty($newlang)) {
70  $outputlangs = new Translate('', $conf);
71  $outputlangs->setDefaultLang($newlang);
72  // Load traductions files required by page
73  $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions', 'sendings'));
74  }
75 
76  $topicmail = '';
77  if (empty($object->ref_client)) {
78  $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
79  } elseif (!empty($object->ref_client)) {
80  $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REF_CLIENT__)');
81  }
82 
83  // Build document if it not exists
84  $forcebuilddoc = true;
85  if (in_array($object->element, array('societe', 'user', 'member'))) {
86  $forcebuilddoc = false;
87  }
88  if ($object->element == 'invoice_supplier' && empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) {
89  $forcebuilddoc = false;
90  }
91  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
92  if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument')) {
93  $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
94  if ($result < 0) {
95  dol_print_error($db, $object->error, $object->errors);
96  exit();
97  }
98  if ($object->element == 'invoice_supplier') {
99  $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
100  } else {
101  $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
102  }
103 
104  $file = $fileparams['fullname'];
105  }
106  }
107 
108  print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
109  print '<div class="clearboth"></div>';
110  print '<br>';
111  print load_fiche_titre($langs->trans($titreform));
112 
113  print dol_get_fiche_head('');
114 
115  // Create form for email
116  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
117  $formmail = new FormMail($db);
118 
119  $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
120  $formmail->fromtype = (GETPOST('fromtype') ?GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
121 
122  if ($formmail->fromtype === 'user') {
123  $formmail->fromid = $user->id;
124  }
125 
126  if ($object->element === 'facture' && !empty($conf->global->INVOICE_EMAIL_SENDER)) {
127  $formmail->frommail = $conf->global->INVOICE_EMAIL_SENDER;
128  $formmail->fromname = (!empty($conf->global->INVOICE_EMAIL_SENDER_NAME) ? $conf->global->INVOICE_EMAIL_SENDER_NAME : '');
129  $formmail->fromtype = 'special';
130  }
131  if ($object->element === 'shipping' && !empty($conf->global->SHIPPING_EMAIL_SENDER)) {
132  $formmail->frommail = $conf->global->SHIPPING_EMAIL_SENDER;
133  $formmail->fromname = (!empty($conf->global->SHIPPING_EMAIL_SENDER_NAME) ? $conf->global->SHIPPING_EMAIL_SENDER_NAME : '');
134  $formmail->fromtype = 'special';
135  }
136  if ($object->element === 'commande' && !empty($conf->global->COMMANDE_EMAIL_SENDER)) {
137  $formmail->frommail = $conf->global->COMMANDE_EMAIL_SENDER;
138  $formmail->fromname = (!empty($conf->global->COMMANDE_EMAIL_SENDER_NAME) ? $conf->global->COMMANDE_EMAIL_SENDER_NAME : '');
139  $formmail->fromtype = 'special';
140  }
141  if ($object->element === 'order_supplier' && !empty($conf->global->ORDER_SUPPLIER_EMAIL_SENDER)) {
142  $formmail->frommail = $conf->global->ORDER_SUPPLIER_EMAIL_SENDER;
143  $formmail->fromname = (!empty($conf->global->ORDER_SUPPLIER_EMAIL_SENDER_NAME) ? $conf->global->ORDER_SUPPLIER_EMAIL_SENDER_NAME : '');
144  $formmail->fromtype = 'special';
145  }
146 
147  $formmail->trackid = $trackid;
148  $formmail->withfrom = 1;
149 
150  // Define $liste, a list of recipients with email inside <>.
151  $liste = array();
152  if ($object->element == 'expensereport') {
153  $fuser = new User($db);
154  $fuser->fetch($object->fk_user_author);
155  $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
156  } elseif ($object->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
157  $fadherent = new Adherent($db);
158  $fadherent->fetch($object->fk_member);
159  $liste['member'] = $fadherent->getFullName($outputlangs)." <".$fadherent->email.">";
160  } elseif ($object->element == 'societe') {
161  foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
162  $liste[$key] = $value;
163  }
164  } elseif ($object->element == 'contact') {
165  $liste['contact'] = $object->getFullName($outputlangs)." <".$object->email.">";
166  } elseif ($object->element == 'user' || $object->element == 'member') {
167  $liste['thirdparty'] = $object->getFullName($outputlangs)." <".$object->email.">";
168  } else {
169  if (!empty($object->socid) && $object->socid > 0 && !is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
170  $object->fetch_thirdparty();
171  }
172  if (is_object($object->thirdparty)) {
173  foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
174  $liste[$key] = $value;
175  }
176  }
177  }
178  if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
179  $listeuser = array();
180  $fuserdest = new User($db);
181 
182  $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);
183  if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
184  foreach ($fuserdest->users as $uuserdest) {
185  $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
186  }
187  } elseif ($result < 0) {
188  setEventMessages(null, $fuserdest->errors, 'errors');
189  }
190  if (count($listeuser) > 0) {
191  $formmail->withtouser = $listeuser;
192  $formmail->withtoccuser = $listeuser;
193  }
194  }
195 
196  //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
197  if (!isset($arrayoffamiliestoexclude)) {
198  $arrayoffamiliestoexclude = null;
199  }
200 
201  // Make substitution in email content
202  if ($object) {
203  // First we set ->substit (useless, it will be erased later) and ->substit_lines
204  $formmail->setSubstitFromObject($object, $langs);
205  }
206  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
207  $substitutionarray['__CHECK_READ__'] = "";
208  if (is_object($object) && is_object($object->thirdparty)) {
209  $checkRead= '<img src="'.DOL_MAIN_URL_ROOT.'/public/emailing/mailing-read.php';
210  $checkRead.='?tag='.(!empty($object->thirdparty->tag)?urlencode($object->thirdparty->tag):"");
211  $checkRead.='&securitykey='.(!empty($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY)?urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY):"");
212  $checkRead.='" width="1" height="1" style="width:1px;height:1px" border="0"/>';
213  $substitutionarray['__CHECK_READ__'] = $checkRead;
214  }
215  $substitutionarray['__PERSONALIZED__'] = ''; // deprecated
216  $substitutionarray['__CONTACTCIVNAME__'] = '';
217  $parameters = array(
218  'mode' => 'formemail'
219  );
220  complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
221 
222  // Find all external contact addresses
223  $tmpobject = $object;
224  if (($object->element == 'shipping' || $object->element == 'reception')) {
225  $origin = $object->origin;
226  $origin_id = $object->origin_id;
227 
228  if (!empty($origin) && !empty($origin_id)) {
229  $element = $subelement = $origin;
230  $regs = array();
231  if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
232  $element = $regs[1];
233  $subelement = $regs[2];
234  }
235  // For compatibility
236  if ($element == 'order') {
237  $element = $subelement = 'commande';
238  }
239  if ($element == 'propal') {
240  $element = 'comm/propal';
241  $subelement = 'propal';
242  }
243  if ($element == 'contract') {
244  $element = $subelement = 'contrat';
245  }
246  if ($element == 'inter') {
247  $element = $subelement = 'ficheinter';
248  }
249  if ($element == 'shipping') {
250  $element = $subelement = 'expedition';
251  }
252  if ($element == 'order_supplier') {
253  $element = 'fourn';
254  $subelement = 'fournisseur.commande';
255  }
256  if ($element == 'project') {
257  $element = 'projet';
258  }
259 
260  dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
261  $classname = ucfirst($origin);
262  $objectsrc = new $classname($db);
263  $objectsrc->fetch($origin_id);
264 
265  $tmpobject = $objectsrc;
266  }
267  }
268 
269  $contactarr = array();
270  $contactarr = $tmpobject->liste_contact(-1, 'external');
271 
272  if (is_array($contactarr) && count($contactarr) > 0) {
273  require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
274  require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
275  $contactstatic = new Contact($db);
276  $tmpcompany = new Societe($db);
277 
278  foreach ($contactarr as $contact) {
279  $contactstatic->fetch($contact['id']);
280  // Complete substitution array
281  $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
282  $substitutionarray['__CONTACT_LASTNAME_'.$contact['code'].'__'] = $contactstatic->lastname;
283  $substitutionarray['__CONTACT_FIRSTNAME_'.$contact['code'].'__'] = $contactstatic->firstname;
284  $substitutionarray['__CONTACT_TITLE_'.$contact['code'].'__'] = $contactstatic->getCivilityLabel();
285 
286  // Complete $liste with the $contact
287  if (empty($liste[$contact['id']])) { // If this contact id not already into the $liste
288  $contacttoshow = '';
289  if (isset($object->thirdparty) && is_object($object->thirdparty)) {
290  if ($contactstatic->fk_soc != $object->thirdparty->id) {
291  $tmpcompany->fetch($contactstatic->fk_soc);
292  if ($tmpcompany->id > 0) {
293  $contacttoshow .= $tmpcompany->name.': ';
294  }
295  }
296  }
297  $contacttoshow .= $contactstatic->getFullName($outputlangs, 1);
298  $contacttoshow .= " <".($contactstatic->email ? $contactstatic->email : $langs->transnoentitiesnoconv("NoEMail")) .">";
299  $liste[$contact['id']] = $contacttoshow;
300  }
301  }
302  }
303 
304  $formmail->withto = $liste;
305  $formmail->withtofree = (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1');
306  $formmail->withtocc = $liste;
307  $formmail->withtoccc = getDolGlobalString('MAIN_EMAIL_USECCC');
308  $formmail->withtopic = $topicmail;
309  $formmail->withfile = 2;
310  $formmail->withbody = 1;
311  $formmail->withdeliveryreceipt = 1;
312  $formmail->withcancel = 1;
313 
314  // Array of substitutions
315  $formmail->substit = $substitutionarray;
316 
317  // Array of other parameters
318  $formmail->param['action'] = 'send';
319  $formmail->param['models'] = $modelmail;
320  $formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
321  $formmail->param['id'] = $object->id;
322  $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
323  $formmail->param['fileinit'] = array($file);
324 
325  // Show form
326  print $formmail->get_form();
327 
328  print dol_get_fiche_end();
329 }
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
dol_include_once
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
Definition: functions.lib.php:1033
Translate
Class to manage translations.
Definition: translate.class.php:30
dol_most_recent_file
dol_most_recent_file($dir, $regexfilter='', $excludefilter=array('(\.meta|_preview.*\.png)$', '^\.'), $nohook=false, $mode='')
Return file(s) into a directory (by default most recent)
Definition: files.lib.php:2386
get_exdir
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart='')
Return a path to have a the directory according to object where files are stored.
Definition: functions.lib.php:6549
getCommonSubstitutionArray
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
Return array of possible common substitutions.
Definition: functions.lib.php:7275
Contact
Class to manage contact/addresses.
Definition: contact.class.php:40
Adherent
Class to manage members of a foundation.
Definition: adherent.class.php:46
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:80
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
User
Class to manage Dolibarr users.
Definition: user.class.php:44
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
FormMail
Classe permettant la generation du formulaire html d'envoi de mail unitaire Usage: $formail = new For...
Definition: html.formmail.class.php:38
complete_substitutions_array
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...
Definition: functions.lib.php:7961