dolibarr  13.0.2
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 <https://www.gnu.org/licenses/>.
16  * or see https://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  print "Error, template page can't be called as URL";
32  exit;
33 }
34 
35 
36 if ($action == 'presend')
37 {
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  {
46  // TODO get also the main_lastdoc field of $object. If not found, try to guess with following code
47 
48  $ref = dol_sanitizeFileName($object->ref);
49  include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
50  // Special case
51  if ($object->element == 'invoice_supplier')
52  {
53  $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
54  } else {
55  $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
56  }
57 
58  $file = $fileparams['fullname'];
59  }
60 
61  // Define output language
62  $outputlangs = $langs;
63  $newlang = '';
64  if ($conf->global->MAIN_MULTILANGS && empty($newlang) && !empty($_REQUEST['lang_id']))
65  {
66  $newlang = $_REQUEST['lang_id'];
67  }
68  if ($conf->global->MAIN_MULTILANGS && empty($newlang))
69  {
70  $newlang = $object->thirdparty->default_lang;
71  }
72 
73  if (!empty($newlang))
74  {
75  $outputlangs = new Translate('', $conf);
76  $outputlangs->setDefaultLang($newlang);
77  // Load traductions files required by page
78  $outputlangs->loadLangs(array('commercial', 'bills', 'orders', 'contracts', 'members', 'propal', 'products', 'supplier_proposal', 'interventions', 'receptions'));
79  }
80 
81  $topicmail = '';
82  if (empty($object->ref_client)) {
83  $topicmail = $outputlangs->trans($defaulttopic, '__REF__');
84  } elseif (!empty($object->ref_client)) {
85  $topicmail = $outputlangs->trans($defaulttopic, '__REF__ (__REFCLIENT__)');
86  }
87 
88  // Build document if it not exists
89  $forcebuilddoc = true;
90  if (in_array($object->element, array('societe', 'user', 'member'))) $forcebuilddoc = false;
91  if ($object->element == 'invoice_supplier' && empty($conf->global->INVOICE_SUPPLIER_ADDON_PDF)) $forcebuilddoc = false;
92  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
93  {
94  if ((!$file || !is_readable($file)) && method_exists($object, 'generateDocument'))
95  {
96  $result = $object->generateDocument(GETPOST('model') ? GETPOST('model') : $object->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
97  if ($result < 0) {
98  dol_print_error($db, $object->error, $object->errors);
99  exit();
100  }
101  if ($object->element == 'invoice_supplier')
102  {
103  $fileparams = dol_most_recent_file($diroutput.'/'.get_exdir($object->id, 2, 0, 0, $object, $object->element).$ref, preg_quote($ref, '/').'([^\-])+');
104  } else {
105  $fileparams = dol_most_recent_file($diroutput.'/'.$ref, preg_quote($ref, '/').'[^\-]+');
106  }
107 
108  $file = $fileparams['fullname'];
109  }
110  }
111 
112  print '<div id="formmailbeforetitle" name="formmailbeforetitle"></div>';
113  print '<div class="clearboth"></div>';
114  print '<br>';
115  print load_fiche_titre($langs->trans($titreform));
116 
118 
119  // Create form for email
120  include_once DOL_DOCUMENT_ROOT.'/core/class/html.formmail.class.php';
121  $formmail = new FormMail($db);
122 
123  $formmail->param['langsmodels'] = (empty($newlang) ? $langs->defaultlang : $newlang);
124  $formmail->fromtype = (GETPOST('fromtype') ?GETPOST('fromtype') : (!empty($conf->global->MAIN_MAIL_DEFAULT_FROMTYPE) ? $conf->global->MAIN_MAIL_DEFAULT_FROMTYPE : 'user'));
125 
126  if ($formmail->fromtype === 'user')
127  {
128  $formmail->fromid = $user->id;
129  }
130 
131  if ($object->element === 'facture' && !empty($conf->global->INVOICE_EMAIL_SENDER)) {
132  $formmail->frommail = $conf->global->INVOICE_EMAIL_SENDER;
133  $formmail->fromname = '';
134  $formmail->fromtype = 'special';
135  }
136  if ($object->element === 'shipping' && !empty($conf->global->SHIPPING_EMAIL_SENDER)) {
137  $formmail->frommail = $conf->global->SHIPPING_EMAIL_SENDER;
138  $formmail->fromname = '';
139  $formmail->fromtype = 'special';
140  }
141  if ($object->element === 'commande' && !empty($conf->global->COMMANDE_EMAIL_SENDER)) {
142  $formmail->frommail = $conf->global->COMMANDE_EMAIL_SENDER;
143  $formmail->fromname = '';
144  $formmail->fromtype = 'special';
145  }
146 
147  $formmail->trackid = $trackid;
148  if (!empty($conf->global->MAIN_EMAIL_ADD_TRACK_ID) && ($conf->global->MAIN_EMAIL_ADD_TRACK_ID & 2)) // If bit 2 is set
149  {
150  include DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
151  $formmail->frommail = dolAddEmailTrackId($formmail->frommail, $trackid);
152  }
153  $formmail->withfrom = 1;
154 
155  // Fill list of recipient with email inside <>.
156  $liste = array();
157  if ($object->element == 'expensereport') {
158  $fuser = new User($db);
159  $fuser->fetch($object->fk_user_author);
160  $liste['thirdparty'] = $fuser->getFullName($outputlangs)." <".$fuser->email.">";
161  } elseif ($object->element == 'societe') {
162  foreach ($object->thirdparty_and_contact_email_array(1) as $key => $value) {
163  $liste[$key] = $value;
164  }
165  } elseif ($object->element == 'contact') {
166  $liste['contact'] = $object->getFullName($outputlangs)." <".$object->email.">";
167  } elseif ($object->element == 'user' || $object->element == 'member') {
168  $liste['thirdparty'] = $object->getFullName($outputlangs)." <".$object->email.">";
169  } else {
170  if (!empty($object->socid) && $object->socid > 0 && !is_object($object->thirdparty) && method_exists($object, 'fetch_thirdparty')) {
171  $object->fetch_thirdparty();
172  }
173  if (is_object($object->thirdparty))
174  {
175  foreach ($object->thirdparty->thirdparty_and_contact_email_array(1) as $key => $value) {
176  $liste[$key] = $value;
177  }
178  }
179  }
180  if (!empty($conf->global->MAIN_MAIL_ENABLED_USER_DEST_SELECT)) {
181  $listeuser = array();
182  $fuserdest = new User($db);
183 
184  $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);
185  if ($result > 0 && is_array($fuserdest->users) && count($fuserdest->users) > 0) {
186  foreach ($fuserdest->users as $uuserdest) {
187  $listeuser[$uuserdest->id] = $uuserdest->user_get_property($uuserdest->id, 'email');
188  }
189  } elseif ($result < 0) {
190  setEventMessages(null, $fuserdest->errors, 'errors');
191  }
192  if (count($listeuser) > 0) {
193  $formmail->withtouser = $listeuser;
194  $formmail->withtoccuser = $listeuser;
195  }
196  }
197 
198  $formmail->withto = $liste;
199  $formmail->withtofree = (GETPOSTISSET('sendto') ? (GETPOST('sendto', 'alphawithlgt') ? GETPOST('sendto', 'alphawithlgt') : '1') : '1');
200  $formmail->withtocc = $liste;
201  $formmail->withtoccc = $conf->global->MAIN_EMAIL_USECCC;
202  $formmail->withtopic = $topicmail;
203  $formmail->withfile = 2;
204  $formmail->withbody = 1;
205  $formmail->withdeliveryreceipt = 1;
206  $formmail->withcancel = 1;
207 
208  //$arrayoffamiliestoexclude=array('system', 'mycompany', 'object', 'objectamount', 'date', 'user', ...);
209  if (!isset($arrayoffamiliestoexclude)) $arrayoffamiliestoexclude = null;
210 
211  // Make substitution in email content
212  $substitutionarray = getCommonSubstitutionArray($outputlangs, 0, $arrayoffamiliestoexclude, $object);
213  $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"/>' : '';
214  $substitutionarray['__PERSONALIZED__'] = ''; // deprecated
215  $substitutionarray['__CONTACTCIVNAME__'] = '';
216  $parameters = array(
217  'mode' => 'formemail'
218  );
219  complete_substitutions_array($substitutionarray, $outputlangs, $object, $parameters);
220 
221  // Find the good contact address
222  $tmpobject = $object;
223  if (($object->element == 'shipping' || $object->element == 'reception')) {
224  $origin = $object->origin;
225  $origin_id = $object->origin_id;
226 
227  if (!empty($origin) && !empty($origin_id)) {
228  $element = $subelement = $origin;
229  $regs = array();
230  if (preg_match('/^([^_]+)_([^_]+)/i', $origin, $regs)) {
231  $element = $regs[1];
232  $subelement = $regs[2];
233  }
234  // For compatibility
235  if ($element == 'order') {
236  $element = $subelement = 'commande';
237  }
238  if ($element == 'propal') {
239  $element = 'comm/propal';
240  $subelement = 'propal';
241  }
242  if ($element == 'contract') {
243  $element = $subelement = 'contrat';
244  }
245  if ($element == 'inter') {
246  $element = $subelement = 'ficheinter';
247  }
248  if ($element == 'shipping') {
249  $element = $subelement = 'expedition';
250  }
251  if ($element == 'order_supplier') {
252  $element = 'fourn';
253  $subelement = 'fournisseur.commande';
254  }
255  if ($element == 'project') {
256  $element = 'projet';
257  }
258 
259  dol_include_once('/'.$element.'/class/'.$subelement.'.class.php');
260  $classname = ucfirst($origin);
261  $objectsrc = new $classname($db);
262  $objectsrc->fetch($origin_id);
263 
264  $tmpobject = $objectsrc;
265  }
266  }
267 
268  $contactarr = array();
269  $contactarr = $tmpobject->liste_contact(-1, 'external');
270 
271  if (is_array($contactarr) && count($contactarr) > 0) {
272  require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
273  $contactstatic = new Contact($db);
274 
275  foreach ($contactarr as $contact) {
276  $contactstatic->fetch($contact['id']);
277  $substitutionarray['__CONTACT_NAME_'.$contact['code'].'__'] = $contactstatic->getFullName($outputlangs, 1);
278  }
279  }
280 
281  // Array of substitutions
282  $formmail->substit = $substitutionarray;
283 
284  // Array of other parameters
285  $formmail->param['action'] = 'send';
286  $formmail->param['models'] = $modelmail;
287  $formmail->param['models_id'] = GETPOST('modelmailselected', 'int');
288  $formmail->param['id'] = $object->id;
289  $formmail->param['returnurl'] = $_SERVER["PHP_SELF"].'?id='.$object->id;
290  $formmail->param['fileinit'] = array($file);
291 
292  // Show form
293  print $formmail->get_form();
294 
296 }
if(!function_exists('dol_getprefix')) dol_include_once($relpath, $classname= '')
Make an include_once using default root and alternate root if it fails.
GETPOST($paramname, $check= 'alphanohtml', $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...
Class to manage contact/addresses.
Class to manage Dolibarr users.
Definition: user.class.php:44
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.
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
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 translations.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
print $_SERVER["PHP_SELF"]
Edit parameters.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
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:2212
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.
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 &quot;subst...