From 36f8de957a18608402a43d5d6dcf1206ebbe45cd Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Tue, 28 Feb 2017 22:03:08 -0500 Subject: [PATCH] Able to parse email templates.... I hope --- .../Emails/EmailTemplateParser.php | 109 +++++++++++++++++- .../Emails/ParsedEmailProperties.php | 33 ++++++ api/BusinessLogic/Tickets/Reply.php | 62 ++++++++++ api/BusinessLogic/Tickets/Ticket.php | 41 ++++++- api/DataAccess/Tickets/TicketGateway.php | 4 +- 5 files changed, 243 insertions(+), 6 deletions(-) create mode 100644 api/BusinessLogic/Emails/ParsedEmailProperties.php create mode 100644 api/BusinessLogic/Tickets/Reply.php diff --git a/api/BusinessLogic/Emails/EmailTemplateParser.php b/api/BusinessLogic/Emails/EmailTemplateParser.php index 8d32bfef..850393fa 100644 --- a/api/BusinessLogic/Emails/EmailTemplateParser.php +++ b/api/BusinessLogic/Emails/EmailTemplateParser.php @@ -46,7 +46,7 @@ class EmailTemplateParser { * @param $language string * @param $ticket Ticket */ - function getFormattedEmailForLanguage($templateName, $language, $ticket, $forStaff, $heskSettings) { + function getFormattedEmailForLanguage($templateName, $language, $ticket, $forStaff, $heskSettings, $modsForHeskSettings) { global $hesklang; $template = self::getFromFileSystem($templateName, $language, false); @@ -54,8 +54,10 @@ class EmailTemplateParser { $subject = ValidEmailTemplates::getValidEmailTemplates()[$templateName]; $subject = $this->parseSubject($subject, $ticket, $language, $heskSettings); - $message = $this->parseMessage($template, $ticket, $language, $forStaff, $heskSettings); - $htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $language, $forStaff, $heskSettings); + $message = $this->parseMessage($template, $ticket, $language, $forStaff, $heskSettings, $modsForHeskSettings, false); + $htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $language, $forStaff, $heskSettings, $modsForHeskSettings, true); + + return new ParsedEmailProperties($subject, $message, $htmlMessage); } /** @@ -144,7 +146,7 @@ class EmailTemplateParser { * @return string * @throws \Exception if common.inc.php isn't loaded */ - private function parseMessage($messageTemplate, $ticket, $language, $admin, $heskSettings) { + private function parseMessage($messageTemplate, $ticket, $language, $admin, $heskSettings, $modsForHeskSettings, $html) { global $hesklang; if (!function_exists('hesk_msgToPlain')) { @@ -205,6 +207,105 @@ class EmailTemplateParser { $msg = str_replace('%%UPDATED%%', $ticket->lastChanged, $msg); $msg = str_replace('%%ID%%', $ticket->id, $msg); + /* All custom fields */ + for ($i=1; $i<=50; $i++) { + $k = 'custom'.$i; + + if (isset($heskSettings['custom_fields'][$k])) { + $v = $heskSettings['custom_fields'][$k]; + + switch ($v['type']) { + case 'checkbox': + $ticket->customFields[$i] = str_replace("
","\n",$ticket->customFields[$i]); + break; + case 'date': + $ticket->customFields[$i] = hesk_custom_date_display_format($ticket->customFields[$i], $v['value']['date_format']); + break; + } + + $msg = str_replace('%%'.strtoupper($k).'%%',stripslashes($ticket->customFields[$i]),$msg); + } else { + $msg = str_replace('%%'.strtoupper($k).'%%','',$msg); + } + } + + // Is message tag in email template? + if (strpos($msg, '%%MESSAGE%%') !== false) { + // Replace message + if ($html) { + $htmlMessage = nl2br($ticket->message); + $msg = str_replace('%%MESSAGE%%', $htmlMessage, $msg); + } else { + $plainTextMessage = $ticket->message; + + $messageHtml = $ticket->usesHtml; + + if (count($ticket->replies) > 0) { + $lastReply = end($ticket->replies); + $messageHtml = $lastReply->usesHtml; + } + + if ($messageHtml) { + if (!function_exists('convert_html_to_text')) { + require(__DIR__ . '/../../../inc/html2text/html2text.php'); + } + $plainTextMessage = convert_html_to_text($plainTextMessage); + $plainTextMessage = fix_newlines($plainTextMessage); + } + $msg = str_replace('%%MESSAGE%%', $plainTextMessage, $msg); + } + + // Add direct links to any attachments at the bottom of the email message + if ($heskSettings['attachments']['use'] && isset($ticket->attachments) && count($ticket->attachments) > 0) { + if (!$modsForHeskSettings['attachments']) { + if ($html) { + $msg .= "


" . $hesklang['fatt']; + } else { + $msg .= "\n\n\n" . $hesklang['fatt']; + } + + foreach ($ticket->attachments as $attachment) { + if ($html) { + $msg .= "

{$attachment->fileName}
"; + } else { + $msg .= "\n\n{$attachment->fileName}\n"; + } + + $msg .= "{$heskSettings['hesk_url']}/download_attachment.php?att_id={$attachment->id}&track={$ticket->trackingId}{$heskSettings['e_param']}"; + } + } + } + + // For customer notifications: if we allow email piping/pop 3 fetching and + // stripping quoted replies add an "reply above this line" tag + if (!$admin && ($heskSettings['email_piping'] || $heskSettings['pop3']) && $heskSettings['strip_quoted']) { + $msg = $hesklang['EMAIL_HR'] . "\n\n" . $msg; + } + } elseif (strpos($msg, '%%MESSAGE_NO_ATTACHMENTS%%') !== false) { + if ($html) { + $htmlMessage = nl2br($ticket->message); + $msg = str_replace('%%MESSAGE_NO_ATTACHMENTS%%', $htmlMessage, $msg); + } else { + $plainTextMessage = $ticket->message; + + $messageHtml = $ticket->usesHtml; + + if (count($ticket->replies) > 0) { + $lastReply = end($ticket->replies); + $messageHtml = $lastReply->usesHtml; + } + + if ($messageHtml) { + if (!function_exists('convert_html_to_text')) { + require(__DIR__ . '/../../../inc/html2text/html2text.php'); + } + $plainTextMessage = convert_html_to_text($plainTextMessage); + $plainTextMessage = fix_newlines($plainTextMessage); + } + $msg = str_replace('%%MESSAGE_NO_ATTACHMENTS%%', $plainTextMessage, $msg); + } + } + return $msg; } } \ No newline at end of file diff --git a/api/BusinessLogic/Emails/ParsedEmailProperties.php b/api/BusinessLogic/Emails/ParsedEmailProperties.php new file mode 100644 index 00000000..5cbe594f --- /dev/null +++ b/api/BusinessLogic/Emails/ParsedEmailProperties.php @@ -0,0 +1,33 @@ +subject = $subject; + $this->message = $message; + $this->htmlMessage = $htmlMessage; + } + + /** + * @var $subject string + */ + public $subject; + + /** + * @var $message string + */ + public $message; + + /** + * @var $htmlMessage string + */ + public $htmlMessage; +} \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/Reply.php b/api/BusinessLogic/Tickets/Reply.php new file mode 100644 index 00000000..77c1d4b9 --- /dev/null +++ b/api/BusinessLogic/Tickets/Reply.php @@ -0,0 +1,62 @@ +id = intval($row['id']); $ticket->trackingId = $row['trackid']; @@ -93,6 +93,40 @@ class Ticket { $ticket->dueDate = $row['due_date']; $ticket->dueDateOverdueEmailSent = $row['overdue_email_sent'] !== null && intval($row['overdue_email_sent']) === 1; + $replies = array(); + while ($replyRow = hesk_dbFetchAssoc($repliesRs)) { + $reply = new Reply(); + $reply->id = $replyRow['id']; + $reply->ticketId = $replyRow['replyto']; + $reply->replierName = $replyRow['name']; + $reply->message = $replyRow['message']; + $reply->dateCreated = $replyRow['dt']; + + if (trim($replyRow['attachments']) !== '') { + $attachments = explode(',', $replyRow['attachments']); + $attachmentArray = array(); + foreach ($attachments as $attachment) { + $attachmentRow = explode('#', $attachment); + $attachmentModel = new Attachment(); + + $attachmentModel->id = $attachmentRow[0]; + $attachmentModel->fileName = $attachmentRow[1]; + $attachmentModel->savedName = $attachmentRow[2]; + + $attachmentArray[] = $attachmentModel; + } + $reply->attachments = $attachmentArray; + } + + $reply->staffId = $replyRow['staffid'] > 0 ? $replyRow['staffid'] : null; + $reply->rating = $replyRow['rating']; + $reply->isRead = $replyRow['read']; + $reply->usesHtml = $replyRow['html']; + + $replies[] = $reply; + } + $ticket->replies = $replies; + return $ticket; } @@ -300,4 +334,9 @@ class Ticket { * @var bool|null */ public $dueDateOverdueEmailSent; + + /** + * @var Reply[] + */ + public $replies; } \ No newline at end of file diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index 4d03b6e2..8b1f29d4 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -25,7 +25,9 @@ class TicketGateway extends CommonDao { $row = hesk_dbFetchAssoc($rs); $linkedTicketsRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `parent` = " . intval($id)); - $ticket = Ticket::fromDatabaseRow($row, $linkedTicketsRs, $heskSettings); + $repliesRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "replies` WHERE `replyto` = " . intval($id) . " ORDER BY `id` ASC"); + + $ticket = Ticket::fromDatabaseRow($row, $linkedTicketsRs, $repliesRs, $heskSettings); $this->close();