dolibarr 19.0.3
interface_50_modTicket_TicketEmail.class.php
Go to the documentation of this file.
1<?php
2/*
3 * Copyright (C) 2014-2016 Jean-François Ferry <hello@librethic.io>
4 * 2016 Christophe Battarel <christophe@altairis.fr>
5 * Copyright (C) 2023 Benjamin Falière <benjamin.faliere@altairis.fr>
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 */
20
26require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
27
28
33{
39 public function __construct($db)
40 {
41 $this->db = $db;
42
43 $this->name = preg_replace('/^Interface/i', '', get_class($this));
44 $this->family = "ticket";
45 $this->description = "Triggers of the module ticket to send notifications to internal users and to third-parties";
46 $this->version = self::VERSION_DOLIBARR; // 'development', 'experimental', 'dolibarr' or version
47 $this->picto = 'ticket';
48 }
49
61 public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
62 {
63 global $mysoc;
64
65 $ok = 0;
66
67 if (empty($conf->ticket) || !isModEnabled('ticket')) {
68 return 0; // Module not active, we do nothing
69 }
70
71 switch ($action) {
72 case 'TICKET_ASSIGNED':
73 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
74
75 if ($object->fk_user_assign > 0) {
76 if ($object->fk_user_assign != $user->id) {
77 $userstat = new User($this->db);
78 $res = $userstat->fetch($object->fk_user_assign);
79 if ($res > 0) {
80 // Send email to notification email
81 if (!getDolGlobalString('TICKET_DISABLE_ALL_MAILS')) {
82 // Init to avoid errors
83 $filepath = array();
84 $filename = array();
85 $mimetype = array();
86
87 $appli = $mysoc->name;
88
89 // Send email to assigned user
90 $subject = '['.$appli.'] '.$langs->transnoentities('TicketAssignedToYou');
91 $message = '<p>'.$langs->transnoentities('TicketAssignedEmailBody', $object->track_id, dolGetFirstLastname($user->firstname, $user->lastname))."</p>";
92 $message .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
93 $message .= '<li>'.$langs->trans('Type').' : '.$object->type_label.'</li>';
94 $message .= '<li>'.$langs->trans('Category').' : '.$object->category_label.'</li>';
95 $message .= '<li>'.$langs->trans('Severity').' : '.$object->severity_label.'</li>';
96 // Extrafields
97 if (is_array($object->array_options) && count($object->array_options) > 0) {
98 foreach ($object->array_options as $key => $value) {
99 $message .= '<li>'.$langs->trans($key).' : '.$value.'</li>';
100 }
101 }
102
103 $message .= '</ul>';
104 $message .= '<p>'.$langs->trans('Message').' : <br>'.$object->message.'</p>';
105 $message .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'">'.$langs->trans('SeeThisTicketIntomanagementInterface').'</a></p>';
106
107 $sendto = $userstat->email;
108 $from = dolGetFirstLastname($user->firstname, $user->lastname).'<'.$user->email.'>';
109
110 $message = dol_nl2br($message);
111
112 if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
113 $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
114 $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
115 }
116 include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
117 $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, -1);
118 if ($mailfile->error) {
119 setEventMessages($mailfile->error, $mailfile->errors, 'errors');
120 } else {
121 $result = $mailfile->sendfile();
122 }
123 if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
124 $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
125 }
126 }
127 } else {
128 $this->error = $userstat->error;
129 $this->errors = $userstat->errors;
130 }
131 }
132
133 // Send an email to the Customer to inform him that his ticket has been taken in charge.
134 if (getDolGlobalString('TICKET_NOTIFY_CUSTOMER_TICKET_ASSIGNED') && empty($object->oldcopy->fk_user_assign)) {
135 $langs->load('ticket');
136
137 $subject_customer = 'TicketAssignedCustomerEmail';
138 $body_customer = 'TicketAssignedCustomerBody';
139 $see_ticket_customer = 'TicketNewEmailBodyInfosTrackUrlCustomer';
140
141 // Get all external contacts linked to the ticket
142 $linked_contacts = $object->listeContact(-1, 'thirdparty');
143
144 // Initialize and fill recipient addresses at least with origin_email
145 $sendto = '';
146 $temp_emails = [];
147 if ($object->origin_email) {
148 $temp_emails[] = $object->origin_email;
149 }
150
151 if (!empty($linked_contacts)) {
152 foreach ($linked_contacts as $contact) {
153 // Avoid the email from being sent twice in case of duplicated contact
154 if (!in_array($contact['email'], $temp_emails)) {
155 $temp_emails[] = $contact['email'];
156 }
157 }
158 }
159
160 $sendto = implode(", ", $temp_emails);
161 unset($temp_emails);
162 unset($linked_contacts);
163
164 // If recipients, we send the email
165 if ($sendto) {
166 $this->composeAndSendCustomerMessage($sendto, $subject_customer, $body_customer, $see_ticket_customer, $object, $langs);
167 }
168 }
169 $ok = 1;
170 }
171 break;
172
173 case 'TICKET_CREATE':
174 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
175
176 $langs->load('ticket');
177
178 $subject_admin = 'TicketNewEmailSubjectAdmin';
179 $body_admin = 'TicketNewEmailBodyAdmin';
180 $subject_customer = 'TicketNewEmailSubjectCustomer';
181 $body_customer = 'TicketNewEmailBodyCustomer';
182 $see_ticket_customer = 'TicketNewEmailBodyInfosTrackUrlCustomer';
183
184 // Send email to notification email
185 if (getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO') && empty($object->context['disableticketemail'])) {
186 $sendto = !getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO') ? '' : $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
187 if ($sendto) {
188 $this->composeAndSendAdminMessage($sendto, $subject_admin, $body_admin, $object, $langs);
189 }
190 }
191
192 // Send email to customer
193 if (!getDolGlobalString('TICKET_DISABLE_CUSTOMER_MAILS') && empty($object->context['disableticketemail']) && $object->notify_tiers_at_create) {
194 $sendto = '';
195
196 //if contact selected send to email's contact else send to email's thirdparty
197
198 $contactid = GETPOST('contactid', 'alpha');
199 $res = 0;
200
201 if (!empty($contactid)) {
202 $contact = new Contact($this->db);
203 $res = $contact->fetch($contactid);
204 }
205
206 if ($res > 0 && !empty($contact->email) && !empty($contact->statut)) {
207 $sendto = $contact->email;
208 } elseif (!empty($object->fk_soc)) {
209 $object->fetch_thirdparty();
210 $sendto = $object->thirdparty->email;
211 }
212
213 if ($sendto) {
214 $this->composeAndSendCustomerMessage($sendto, $subject_customer, $body_customer, $see_ticket_customer, $object, $langs);
215 }
216 }
217
218 $ok = 1;
219 break;
220
221 case 'TICKET_DELETE':
222 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
223 break;
224
225 case 'TICKET_MODIFY':
226 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
227 break;
228
229 case 'TICKET_CLOSE':
230 dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
231 $langs->load('ticket');
232
233 $subject_admin = 'TicketCloseEmailSubjectAdmin';
234 $body_admin = 'TicketCloseEmailBodyAdmin';
235 $subject_customer = 'TicketCloseEmailSubjectCustomer';
236 $body_customer = 'TicketCloseEmailBodyCustomer';
237 $see_ticket_customer = 'TicketCloseEmailBodyInfosTrackUrlCustomer';
238
239 // Send email to notification email
240 if (getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO') && empty($object->context['disableticketemail'])) {
241 $sendto = !getDolGlobalString('TICKET_NOTIFICATION_EMAIL_TO') ? '' : $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
242 if ($sendto) {
243 $this->composeAndSendAdminMessage($sendto, $subject_admin, $body_admin, $object, $langs);
244 }
245 }
246
247 // Send email to customer.
248 if (!getDolGlobalString('TICKET_DISABLE_CUSTOMER_MAILS') && empty($object->context['disableticketemail'])) {
249 $linked_contacts = $object->listeContact(-1, 'thirdparty');
250 $linked_contacts = array_merge($linked_contacts, $object->listeContact(-1, 'internal'));
251 if (empty($linked_contacts) && getDolGlobalString('TICKET_NOTIFY_AT_CLOSING') && !empty($object->fk_soc)) {
252 $object->fetch_thirdparty();
253 $linked_contacts[]['email'] = $object->thirdparty->email;
254 }
255
256 $contactid = GETPOST('contactid', 'int');
257 $res = 0;
258
259 if ($contactid > 0) {
260 // TODO This security test has no sens. We must check that $contactid is inside $linked_contacts[]['id'] when $linked_contacts[]['source'] = 'external' or 'thirdparty'
261 // Refuse email if not
262 $contact = new Contact($this->db);
263 $res = $contact->fetch($contactid);
264 if (! in_array($contact, $linked_contacts)) {
265 $error_msg = $langs->trans('Error'). ': ';
266 $error_msg .= $langs->transnoentities('TicketWrongContact');
267 setEventMessages($error_msg, [], 'errors');
268 $ok = 0;
269 break;
270 }
271 }
272
273 $sendto = '';
274 if ($res > 0 && !empty($contact->email) && !empty($contact->statut)) {
275 $sendto = $contact->email;
276 } elseif (!empty($linked_contacts) && ($contactid == -2 || (GETPOST('massaction', 'alpha') == 'close' && GETPOST('confirm', 'alpha') == 'yes'))) {
277 // if sending to all contacts or sending to contacts while mass closing
278 $temp_emails = [];
279 foreach ($linked_contacts as $contact) {
280 $temp_emails[] = $contact['email'];
281 }
282 $sendto = implode(", ", $temp_emails);
283 unset($temp_emails);
284 unset($linked_contacts);
285 }
286 if ($sendto) {
287 $this->composeAndSendCustomerMessage($sendto, $subject_customer, $body_customer, $see_ticket_customer, $object, $langs);
288 }
289 }
290 $ok = 1;
291 break;
292 }
293
294 return $ok;
295 }
296
307 private function composeAndSendAdminMessage($sendto, $base_subject, $body, Ticket $object, Translate $langs)
308 {
309 global $conf, $mysoc;
310
311 // Init to avoid errors
312 $filepath = array();
313 $filename = array();
314 $mimetype = array();
315
316 $appli = $mysoc->name;
317
318 /* Send email to admin */
319 $subject = '['.$appli.'] '.$langs->transnoentities($base_subject, $object->ref, $object->track_id);
320 $message_admin = $langs->transnoentities($body, $object->track_id).'<br>';
321 $message_admin .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
322 $message_admin .= '<li>'.$langs->trans('Type').' : '.$langs->getLabelFromKey($this->db, 'TicketTypeShort'.$object->type_code, 'c_ticket_type', 'code', 'label', $object->type_code).'</li>';
323 $message_admin .= '<li>'.$langs->trans('TicketCategory').' : '.$langs->getLabelFromKey($this->db, 'TicketCategoryShort'.$object->category_code, 'c_ticket_category', 'code', 'label', $object->category_code).'</li>';
324 $message_admin .= '<li>'.$langs->trans('Severity').' : '.$langs->getLabelFromKey($this->db, 'TicketSeverityShort'.$object->severity_code, 'c_ticket_severity', 'code', 'label', $object->severity_code).'</li>';
325 $message_admin .= '<li>'.$langs->trans('From').' : '.($object->email_from ? $object->email_from : ($object->fk_user_create > 0 ? $langs->trans('Internal') : '')).'</li>';
326 // Extrafields
327 $extraFields = new ExtraFields($this->db);
328 $extraFields->fetch_name_optionals_label($object->table_element);
329 if (is_array($object->array_options) && count($object->array_options) > 0) {
330 foreach ($object->array_options as $key => $value) {
331 $key = substr($key, 8); // remove "options_"
332 $message_admin .= '<li>'.$langs->trans($extraFields->attributes[$object->element]['label'][$key]).' : '.$extraFields->showOutputField($key, $value, '', $object->table_element).'</li>';
333 }
334 }
335 if ($object->fk_soc > 0) {
336 $object->fetch_thirdparty();
337 $message_admin .= '<li>'.$langs->trans('Company').' : '.$object->thirdparty->name.'</li>';
338 }
339 $message_admin .= '</ul>';
340
341 $message = $object->message;
342 if (!dol_textishtml($message)) {
343 $message = dol_nl2br($message);
344 }
345 $message_admin .= '<p>'.$langs->trans('Message').' : <br><br>'.$message.'</p><br>';
346 $message_admin .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'">'.$langs->trans('SeeThisTicketIntomanagementInterface').'</a></p>';
347
348 $from = getDolGlobalString('MAIN_INFO_SOCIETE_NOM') . '<' . getDolGlobalString('TICKET_NOTIFICATION_EMAIL_FROM').'>';
349
350 $trackid = 'tic'.$object->id;
351
352 if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
353 $old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
354 $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
355 }
356 include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
357 $mailfile = new CMailFile($subject, $sendto, $from, $message_admin, $filepath, $mimetype, $filename, '', '', 0, -1, '', '', $trackid, '', 'ticket');
358 if ($mailfile->error) {
359 dol_syslog($mailfile->error, LOG_DEBUG);
360 } else {
361 $result = $mailfile->sendfile();
362 }
363 if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
364 $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
365 }
366 }
367
379 private function composeAndSendCustomerMessage($sendto, $base_subject, $body, $see_ticket, Ticket $object, Translate $langs)
380 {
381 global $conf, $extrafields, $mysoc, $user;
382
383 // Init to avoid errors
384 $filepath = array();
385 $filename = array();
386 $mimetype = array();
387
388 $appli = $mysoc->name;
389
390 $subject = '['.$appli.'] '.$langs->transnoentities($base_subject);
391 $message_customer = $langs->transnoentities($body, $object->track_id).'<br>';
392 $message_customer .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
393 $message_customer .= '<li>'.$langs->trans('Type').' : '.$langs->getLabelFromKey($this->db, 'TicketTypeShort'.$object->type_code, 'c_ticket_type', 'code', 'label', $object->type_code).'</li>';
394 $message_customer .= '<li>'.$langs->trans('TicketCategory').' : '.$langs->getLabelFromKey($this->db, 'TicketCategoryShort'.$object->category_code, 'c_ticket_category', 'code', 'label', $object->category_code).'</li>';
395 $message_customer .= '<li>'.$langs->trans('Severity').' : '.$langs->getLabelFromKey($this->db, 'TicketSeverityShort'.$object->severity_code, 'c_ticket_severity', 'code', 'label', $object->severity_code).'</li>';
396
397 // Extrafields
398 if (is_array($extrafields->attributes[$object->table_element]['label'])) {
399 foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $value) {
400 $enabled = 1;
401 if ($enabled && isset($extrafields->attributes[$object->table_element]['list'][$key])) {
402 $enabled = dol_eval($extrafields->attributes[$object->table_element]['list'][$key], 1);
403 }
404 $perms = 1;
405 if ($perms && isset($extrafields->attributes[$object->table_element]['perms'][$key])) {
406 $perms = dol_eval($extrafields->attributes[$object->table_element]['perms'][$key], 1);
407 }
408
409 $qualified = true;
410 if (empty($enabled)) {
411 $qualified = false;
412 }
413 if (empty($perms)) {
414 $qualified = false;
415 }
416
417 if ($qualified) {
418 $message_customer .= '<li>' . $langs->trans($key) . ' : ' . $value . '</li>';
419 }
420 }
421 }
422
423 $message_customer .= '</ul>';
424
425 $message = $object->message;
426 if (!dol_textishtml($message)) {
427 $message = dol_nl2br($message);
428 }
429 $message_customer .= '<p>'.$langs->trans('Message').' : <br><br>'.$message.'</p><br>';
430
431 $url_public_ticket = getDolGlobalString('TICKET_URL_PUBLIC_INTERFACE', dol_buildpath('/public/ticket/', 2)).'view.php?track_id='.$object->track_id;
432
433 $message_customer .= '<p>'.$langs->trans($see_ticket).' : <a href="'.$url_public_ticket.'">'.$url_public_ticket.'</a></p>';
434 $message_customer .= '<p>'.$langs->trans('TicketEmailPleaseDoNotReplyToThisEmail').'</p>';
435
436 $from = (!getDolGlobalString('MAIN_INFO_SOCIETE_NOM') ? '' : getDolGlobalString('MAIN_INFO_SOCIETE_NOM') . ' ').'<' . getDolGlobalString('TICKET_NOTIFICATION_EMAIL_FROM').'>';
437
438 $trackid = 'tic'.$object->id;
439
440 $old_MAIN_MAIL_AUTOCOPY_TO = getDolGlobalString('MAIN_MAIL_AUTOCOPY_TO');
441
442 if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
443 $conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
444 }
445
446 include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
447 $mailfile = new CMailFile($subject, $sendto, $from, $message_customer, $filepath, $mimetype, $filename, '', '', 0, -1, '', '', $trackid, '', 'ticket');
448 if ($mailfile->error) {
449 dol_syslog($mailfile->error, LOG_DEBUG);
450 } else {
451 $result = $mailfile->sendfile();
452 if ($result) {
453 // update last_msg_sent date
454 $object->fetch($object->id);
455 $object->date_last_msg_sent = dol_now();
456 $object->update($user);
457 }
458 }
459 if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
460 $conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
461 }
462 }
463}
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Class to stock current configuration.
Class that all the triggers must extend.
Class to manage standard extra fields.
Class of triggers for ticket module.
composeAndSendCustomerMessage($sendto, $base_subject, $body, $see_ticket, Ticket $object, Translate $langs)
Composes and sends a message concerning a ticket, to be sent to customer addresses.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
composeAndSendAdminMessage($sendto, $base_subject, $body, Ticket $object, Translate $langs)
Composes and sends a message concerning a ticket, to be sent to admin address.
Class to manage translations.
Class to manage Dolibarr users.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_nl2br($stringtoencode, $nl2brmode=0, $forxml=false)
Replace CRLF in string with a HTML BR tag.
dol_now($mode='auto')
Return date for now.
dol_eval($s, $returnvalue=0, $hideerrors=1, $onlysimplestring='1')
Replace eval function to add more security.
dolGetFirstLastname($firstname, $lastname, $nameorder=-1)
Return firstname and lastname in correct order.
dol_textishtml($msg, $option=0)
Return if a text is a html content.
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_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Class to generate the form for creating a new ticket.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition repair.php:124
Contact()
Old copy.
Definition index.php:572