Skip to content

Commit

Permalink
Backport fix for send email to assigned user on ticket create
Browse files Browse the repository at this point in the history
  • Loading branch information
IC-William committed Jan 2, 2025
1 parent 4267f5f commit f60f34c
Showing 1 changed file with 137 additions and 33 deletions.
170 changes: 137 additions & 33 deletions htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,45 +77,48 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
if ($res > 0) {
// Send email to notification email
if (empty($conf->global->TICKET_DISABLE_ALL_MAILS)) {
// Init to avoid errors
$filepath = array();
$filename = array();
$mimetype = array();
// Init to avoid errors

Check failure on line 80 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / phpcs

Line indented incorrectly; expected at least 8 tabs, found 0

Check failure on line 80 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $filepath = array();

Check failure on line 81 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / phpcs

Line indented incorrectly; expected at least 8 tabs, found 0

Check failure on line 81 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $filename = array();

Check failure on line 82 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / phpcs

Line indented incorrectly; expected at least 8 tabs, found 0

Check failure on line 82 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $mimetype = array();

Check failure on line 83 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / phpcs

Line indented incorrectly; expected at least 8 tabs, found 0

Check failure on line 83 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0

// Send email to assigned user
$subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketAssignedToYou');
$message = '<p>'.$langs->transnoentities('TicketAssignedEmailBody', $object->track_id, dolGetFirstLastname($user->firstname, $user->lastname))."</p>";
$message .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
$message .= '<li>'.$langs->trans('Type').' : '.$object->type_label.'</li>';
$message .= '<li>'.$langs->trans('Category').' : '.$object->category_label.'</li>';
$message .= '<li>'.$langs->trans('Severity').' : '.$object->severity_label.'</li>';
// Extrafields
if (is_array($object->array_options) && count($object->array_options) > 0) {
foreach ($object->array_options as $key => $value) {
$message .= '<li>'.$langs->trans($key).' : '.$value.'</li>';
}
}

$message .= '</ul>';
$message .= '<p>'.$langs->trans('Message').' : <br>'.$object->message.'</p>';
$message .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'">'.$langs->trans('SeeThisTicketIntomanagementInterface').'</a></p>';
// $subject = '['.$conf->global->MAIN_INFO_SOCIETE_NOM.'] '.$langs->transnoentities('TicketAssignedToYou');

Check failure on line 86 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / phpcs

Line indented incorrectly; expected at least 8 tabs, found 0

Check failure on line 86 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $message = '<p>'.$langs->transnoentities('TicketAssignedEmailBody', $object->track_id, dolGetFirstLastname($user->firstname, $user->lastname))."</p>";

Check failure on line 87 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $message .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';

Check failure on line 88 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $message .= '<li>'.$langs->trans('Type').' : '.$object->type_label.'</li>';

Check failure on line 89 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $message .= '<li>'.$langs->trans('Category').' : '.$object->category_label.'</li>';

Check failure on line 90 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// $message .= '<li>'.$langs->trans('Severity').' : '.$object->severity_label.'</li>';

Check failure on line 91 in htdocs/core/triggers/interface_50_modTicket_TicketEmail.class.php

View workflow job for this annotation

GitHub Actions / pre-commit

Line indented incorrectly; expected at least 8 tabs, found 0
// // Extrafields
// if (is_array($object->array_options) && count($object->array_options) > 0) {
// foreach ($object->array_options as $key => $value) {
// $message .= '<li>'.$langs->trans($key).' : '.$value.'</li>';
// }
// }
//
// $message .= '</ul>';
// $message .= '<p>'.$langs->trans('Message').' : <br>'.$object->message.'</p>';
// $message .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'">'.$langs->trans('SeeThisTicketIntomanagementInterface').'</a></p>';

$sendto = $userstat->email;
$from = dolGetFirstLastname($user->firstname, $user->lastname).'<'.$user->email.'>';

$message = dol_nl2br($message);
$subject_assignee = 'TicketAssignedToYou';
$body_assignee = 'TicketAssignedEmailBody';
$see_ticket_assignee = 'SeeThisTicketIntomanagementInterface';
// $from = dolGetFirstLastname($user->firstname, $user->lastname).'<'.$user->email.'>';
//
// $message = dol_nl2br($message);

if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
$old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
$conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
}
include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
$mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, -1);
if ($mailfile->error) {
setEventMessages($mailfile->error, $mailfile->errors, 'errors');
} else {
$result = $mailfile->sendfile();
}
// include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
// $mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, -1);
// if ($mailfile->error) {
// setEventMessages($mailfile->error, $mailfile->errors, 'errors');
// } else {
// $result = $mailfile->sendfile();
// }
if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
$conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
}
Expand Down Expand Up @@ -173,10 +176,15 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf

$subject_admin = 'TicketNewEmailSubjectAdmin';
$body_admin = 'TicketNewEmailBodyAdmin';

$subject_customer = 'TicketNewEmailSubjectCustomer';
$body_customer = 'TicketNewEmailBodyCustomer';
$see_ticket_customer = 'TicketNewEmailBodyInfosTrackUrlCustomer';

$subject_assignee = 'TicketAssignedToYou';
$body_assignee = 'TicketAssignedEmailBody';
$see_ticket_assignee = 'SeeThisTicketIntomanagementInterface';

// Send email to notification email
if (!empty($conf->global->TICKET_NOTIFICATION_EMAIL_TO) && empty($object->context['disableticketemail'])) {
$sendto = empty($conf->global->TICKET_NOTIFICATION_EMAIL_TO) ? '' : $conf->global->TICKET_NOTIFICATION_EMAIL_TO;
Expand All @@ -185,6 +193,34 @@ public function runTrigger($action, $object, User $user, Translate $langs, Conf
}
}

// Send email to assignee if an assignee was set at creation
if ($object->fk_user_assign > 0 && $object->fk_user_assign != $user->id && empty($object->context['disableticketemail'])) {
$userstat = new User($this->db);
$res = $userstat->fetch($object->fk_user_assign);
if ($res > 0) {
// Send email to notification email
if (!getDolGlobalString('TICKET_DISABLE_ALL_MAILS')) {
// Send email to assigned user
$sendto = $userstat->email;
if (!getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
$old_MAIN_MAIL_AUTOCOPY_TO = $conf->global->MAIN_MAIL_AUTOCOPY_TO;
$conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
}

if (!empty($sendto)) {
$this->composeAndSendAssigneeMessage($sendto, $subject_assignee, $body_assignee, $see_ticket_assignee, $object, $langs);
}

if (!getDolUserString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
$conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
}
}
} else {
$this->error = $userstat->error;
$this->errors = $userstat->errors;
}
}

// Send email to customer
if (empty($conf->global->TICKET_DISABLE_CUSTOMER_MAILS) && empty($object->context['disableticketemail']) && $object->notify_tiers_at_create) {
$sendto = '';
Expand Down Expand Up @@ -428,9 +464,9 @@ private function composeAndSendCustomerMessage($sendto, $base_subject, $body, $s

$trackid = 'tic'.$object->id;

$old_MAIN_MAIL_AUTOCOPY_TO = getDolGlobalString('MAIN_MAIL_AUTOCOPY_TO');

if (!empty($conf->global->TICKET_DISABLE_MAIL_AUTOCOPY_TO)) {
$old_MAIN_MAIL_AUTOCOPY_TO = null;
if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
$old_MAIN_MAIL_AUTOCOPY_TO = getDolGlobalString('MAIN_MAIL_AUTOCOPY_TO');
$conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
}

Expand All @@ -451,4 +487,72 @@ private function composeAndSendCustomerMessage($sendto, $base_subject, $body, $s
$conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
}
}

/**
* Composes and sends a message concerning a ticket, to be sent to user assigned to the ticket
*
* @param string $sendto Addresses to send the mail, format "first@address.net, second@address.net, " etc.
* @param string $base_subject email subject. Non-translated string.
* @param string $body email body (first line). Non-translated string.
* @param string $see_ticket string indicating the ticket public address
* @param Ticket $object the ticket thet the email refers to
* @param Translate $langs the translation object
* @return void
*/
private function composeAndSendAssigneeMessage($sendto, $base_subject, $body, $see_ticket, Ticket $object, Translate $langs)
{
global $conf, $user, $mysoc;

// Init to avoid errors
$filepath = array();
$filename = array();
$mimetype = array();

// Send email to assigned user
$appli = $mysoc->name;

$subject = '['.$appli.'] '.$langs->transnoentities($base_subject);
$message = '<p>'.$langs->transnoentities($body, $object->track_id, dolGetFirstLastname($user->firstname, $user->lastname))."</p>";
$message .= '<ul><li>'.$langs->trans('Title').' : '.$object->subject.'</li>';
$message .= '<li>'.$langs->trans('Type').' : '.$object->type_label.'</li>';
$message .= '<li>'.$langs->trans('Category').' : '.$object->category_label.'</li>';
$message .= '<li>'.$langs->trans('Severity').' : '.$object->severity_label.'</li>';
// Extrafields
if (is_array($object->array_options) && count($object->array_options) > 0) {
foreach ($object->array_options as $key => $value) {
$message .= '<li>'.$langs->trans($key).' : '.$value.'</li>';
}
}

$message .= '</ul>';
$message .= '<p>'.$langs->trans('Message').' : <br>'.$object->message.'</p>';
$message .= '<p><a href="'.dol_buildpath('/ticket/card.php', 2).'?track_id='.$object->track_id.'">'.$langs->trans($see_ticket).'</a></p>';

$from = dolGetFirstLastname($user->firstname, $user->lastname).'<'.$user->email.'>';

$message = dol_nl2br($message);

$old_MAIN_MAIL_AUTOCOPY_TO = null;
if (getDolGlobalString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
$old_MAIN_MAIL_AUTOCOPY_TO = getDolGlobalString('MAIN_MAIL_AUTOCOPY_TO');
$conf->global->MAIN_MAIL_AUTOCOPY_TO = '';
}

include_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
$mailfile = new CMailFile($subject, $sendto, $from, $message, $filepath, $mimetype, $filename, '', '', 0, -1);
if ($mailfile->error) {
setEventMessages($mailfile->error, $mailfile->errors, 'errors');
} else {
$result = $mailfile->sendfile();
if ($result) {
// update last_msg_sent date
$object->fetch($object->id);
$object->date_last_msg_sent = dol_now();
$object->update($user);
}
}
if (!getDolUserString('TICKET_DISABLE_MAIL_AUTOCOPY_TO')) {
$conf->global->MAIN_MAIL_AUTOCOPY_TO = $old_MAIN_MAIL_AUTOCOPY_TO;
}
}
}

0 comments on commit f60f34c

Please sign in to comment.