dolibarr  9.0.0
card_presend.tpl.php
1 <?php
2 /* Copyright (C) 2017-2018 Laurent Destailleur <eldy@users.sourceforge.net>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  * or see http://www.gnu.org/
17  */
18 
19 /*
20  * Code to ouput content when action is presend
21  *
22  * $trackid must be defined
23  * $modelmail
24  * $defaulttopic
25  * $diroutput
26  * $arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
27  */
28 
29 // Protection to avoid direct call of template
30 if (empty($conf) || ! is_object($conf))
31 {
32  print "Error, template page can't be called as URL";
33  exit;
34 }
35 
36 
37 if ($action == 'presend')
38 {
39  $langs->load("mails");
40 
41  $titreform='SendMail';
42 
43  $object->fetch_projet();
44 
45  if (! in_array($object->element, array('societe', 'user', 'member')))
46  {
47  // TODO get also the main_lastdoc field of $object. If not found, try to guess with following code
48 
49  $ref = dol_sanitizeFileName($object->ref);
50  include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
51  // Special case
52  if ($object->element == 'invoice_supplier')
53  {
54  $fileparams = dol_most_recent_file($diroutput . '/' . get_exdir($object->id,2,0,0,$object,$object->element).$ref, preg_quote($ref,'/').'([^\-])+');
55  }
56  else
57  {
58  $fileparams = dol_most_recent_file($diroutput . '/' . $ref, preg_quote($ref, '/').'[^\-]+');
59  }
60 
61  $file = $fileparams['fullname'];
62  }
63 
64  // Define output language
65  $outputlangs = $langs;
66  $newlang = '';
67  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && ! empty($_REQUEST['lang_id']))
68  {
69  $newlang = $_REQUEST['lang_id'];
70  }
71  if ($conf->global->MAIN_MULTILANGS && empty($newlang))
72  {
73  $newlang = $object->thirdparty->default_lang;
74  }
75 
76  if (!empty($newlang))
77  {
78  $outputlangs = new Translate('', $conf);
79  $outputlangs->setDefaultLang($newlang);
80  // Load traductions files requiredby by page
81  $outputlangs->loadLangs(array('commercial','bills','orders','contracts','members','propal','products','supplier_proposal','interventions'));
82  }
83 
84  $topicmail='';
85  if (empty($object->ref_client)) {
86  $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
87  } else if (! empty($object->ref_client)) {
88  $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REFCLIENT__)');
89  }
90 
91  // Build document if it not exists
92  $forcebuilddoc=true;
93  if (in_array($object->element, array('societe', 'user', 'member'))) $forcebuilddoc=false;
94  if ($object->element == 'invoice_supplier' && empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) $forcebuilddoc=false;
95  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
96  {
97  if ((! $file || ! is_readable($file)) && method_exists($object, 'generateDocument'))
98  {
99  $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
100  if ($result < 0) {
101  dol_print_error($db, $object->error, $object->errors);
102  exit();
103  }
104  if ($object->element == 'invoice_supplier')
105  {
106  $fileparams = dol_most_recent_file($diroutput . '/' . get_exdir($object->id,2,0,0,$object,$object->element).$ref, preg_quote($ref,'/').'([^\-])+');
107  }
108  else
109  {
110  $fileparams = dol_most_recent_file($diroutput . '/' . $ref, preg_quote($ref, '/').'[^\-]+');
111  }
112 
113  $file = $fileparams['fullname'];
114  }
115  }
116 
117  print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
118  print '<div class="clearboth"></div>';
119  print '<br>';
120  print load_fiche_titre($langs->trans($titreform));
121 
122  dol_fiche_head('');
123 
124  // Create form for email
125  include_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
126  $formmail = new FormMail($db);
127 
128  $formmail->param['langsmodels']=(empty($newlang)?$langs->defaultlang:$newlang);
129  $formmail->fromtype = (GETPOST('fromtype')?GETPOST('fromtype'):(!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE)?$conf->global->MAIN_MAIL_DEFAULT_FROMTYPE:'user'));
130 
131  if ($formmail->fromtype === 'user')
132  {
133  $formmail->fromid = $user->id;
134  }
135  $formmail->trackid=$trackid;
136  if (! empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID) && ($conf->global->MAIN_EMAIL_ADD_TRACK_ID & 2)) // If bit 2 is set
137  {
138  include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
139  $formmail->frommail=dolAddEmailTrackId($formmail->frommail, $trackid);
140  }
141  $formmail->withfrom = 1;
142 
143  // Fill list of recipient with email inside <>.
144  $liste = array();
145  if ($object->element == 'expensereport')
146  {
147  $fuser = new User($db);
148  $fuser->fetch($object->fk_user_author);
149  $liste['thirdparty'] = $fuser->getFullName($langs)." <".$fuser->email.">";
150  }
151  elseif ($object->element == 'societe')
152  {
153  foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
154  $liste[$key] = $value;
155  }
156  }
157  elseif ($object->element == 'user' || $object->element == 'member')
158  {
159  $liste['thirdparty'] = $object->getFullName($langs)." <".$object->email.">";
160  }
161  else
162  {
163  if (is_object($object->thirdparty))
164  {
165  foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
166  $liste[$key] = $value;
167  }
168  }
169  }
170  if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
171  $listeuser=array();
172  $fuserdest = new User($db);
173 
174  $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<>\'\''));
175  if ($result>0 && is_array($fuserdest->users) && count($fuserdest->users)>0) {
176  foreach($fuserdest->users as $uuserdest) {
177  $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id,'email');
178  }
179  } elseif ($result<0) {
180  setEventMessages(null, $fuserdest->errors,'errors');
181  }
182  if (count($listeuser)>0) {
183  $formmail->withtouser = $listeuser;
184  $formmail->withtoccuser = $listeuser;
185  }
186  }
187 
188  $formmail->withto = GETPOST('sendto') ? GETPOST('sendto') : $liste;
189  $formmail->withtocc = $liste;
190  $formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
191  $formmail->withtopic = $topicmail;
192  $formmail->withfile = 2;
193  $formmail->withbody = 1;
194  $formmail->withdeliveryreceipt = 1;
195  $formmail->withcancel = 1;
196 
197  //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
198  if (! isset($arrayoffamiliestoexclude)) $arrayoffamiliestoexclude=null;
199 
200  // Make substitution in email content
201  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
202  $substitutionarray['__CHECK_READ__'] = (is_object($object) && is_object($object->thirdparty)) ? '<img src="' . DOL_MAIN_URL_ROOT . '/public/emailing/mailing-read.php?tag=' . $object->thirdparty->tag . '&securitykey=' . urlencode($conf->global->MAILING_EMAIL_UNSUBSCRIBE_KEY) . '" width="1" height="1" style="width:1px;height:1px" border="0"/>' : '';
203  $substitutionarray['__PERSONALIZED__'] = ''; // deprecated
204  $substitutionarray['__CONTACTCIVNAME__'] = '';
205  $parameters = array(
206  'mode' => 'formemail'
207  );
208  complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
209 
210  // Find the good contact adress
211  $custcontact = '';
212  $contactarr = array();
213  $contactarr = $object->liste_contact(- 1, 'external');
214 
215  if (is_array($contactarr) && count($contactarr) > 0) {
216  require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php';
217  $contactstatic = new Contact($db);
218 
219  foreach ($contactarr as $contact) {
220  $contactstatic->fetch($contact['id']);
221  $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($langs, 1);
222  }
223  }
224 
225  // Tableau des substitutions
226  $formmail->substit = $substitutionarray;
227 
228  // Tableau des parametres complementaires
229  $formmail->param['action'] = 'send';
230  $formmail->param['models'] = $modelmail;
231  $formmail->param['models_id']=GETPOST('modelmailselected','int');
232  $formmail->param['id'] = $object->id;
233  $formmail->param['returnurl'] = $_SERVER["PHP_SELF"] . '?id=' . $object->id;
234  $formmail->param['fileinit'] = array($file);
235 
236  // Show form
237  print $formmail->get_form();
238 
239  dol_fiche_end();
240 }
241 
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Classe permettant la generation du formulaire html d&#39;envoi de mail unitaire Usage: $formail = new For...
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
Class to manage contact/addresses.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
get_exdir($num, $level, $alpha, $withoutslash, $object, $modulepart)
Return a path to have a the directory according to object where files are stored. ...
Class to manage Dolibarr users.
Definition: user.class.php:41
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dolAddEmailTrackId($email, $trackingid)
Return an email formatted to include a tracking id For example myemail@example.com becom myemail+trac...
getCommonSubstitutionArray($outputlangs, $onlykey=0, $exclude=null, $object=null)
Return array of possible common substitutions.
dol_fiche_end($notab=0)
Show tab footer of a card.
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:2091
Class to manage translations.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.
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...