diff --git a/api/BusinessLogic/Emails/EmailSender.php b/api/BusinessLogic/Emails/EmailSender.php index 4f300529..d1658451 100644 --- a/api/BusinessLogic/Emails/EmailSender.php +++ b/api/BusinessLogic/Emails/EmailSender.php @@ -9,7 +9,7 @@ use PHPMailer; interface EmailSender { /** - * Use to send emails that do NOT include ticket information + * Use to send emails * * @param $emailBuilder EmailBuilder * @param $heskSettings array diff --git a/api/BusinessLogic/Emails/EmailSenderHelper.php b/api/BusinessLogic/Emails/EmailSenderHelper.php new file mode 100644 index 00000000..5558024e --- /dev/null +++ b/api/BusinessLogic/Emails/EmailSenderHelper.php @@ -0,0 +1,35 @@ +emailTemplateParser = $emailTemplateParser; + $this->basicEmailSender = $basicEmailSender; + $this->mailgunEmailSender = $mailgunEmailSender; + } + + function sendEmailForTicket($emailTemplateName, $ticket, $heskSettings, $modsForHeskSettings) { + //-- parse template + + //-- if no mailgun, use basic sender + + //-- otherwise use mailgun sender + } +} \ No newline at end of file diff --git a/api/BusinessLogic/Emails/EmailTemplate.php b/api/BusinessLogic/Emails/EmailTemplate.php new file mode 100644 index 00000000..02884679 --- /dev/null +++ b/api/BusinessLogic/Emails/EmailTemplate.php @@ -0,0 +1,27 @@ +languageKey = $languageKey === null ? $fileName : $languageKey; + $this->fileName = $fileName; + $this->forStaff = $forStaff; + } +} \ No newline at end of file diff --git a/api/BusinessLogic/Emails/EmailTemplateParser.php b/api/BusinessLogic/Emails/EmailTemplateParser.php index 850393fa..c6a3df11 100644 --- a/api/BusinessLogic/Emails/EmailTemplateParser.php +++ b/api/BusinessLogic/Emails/EmailTemplateParser.php @@ -1,10 +1,4 @@ statusGateway = $statusGateway; $this->categoryGateway = $categoryGateway; $this->userGateway = $userGateway; + $this->emailTemplateRetriever = $emailTemplateRetriever; } /** - * @param $templateName string + * @param $templateId int * @param $language string * @param $ticket Ticket + * @param $heskSettings array + * @param $modsForHeskSettings array + * @return ParsedEmailProperties + * @throws InvalidEmailTemplateException */ - function getFormattedEmailForLanguage($templateName, $language, $ticket, $forStaff, $heskSettings, $modsForHeskSettings) { - global $hesklang; + function getFormattedEmailForLanguage($templateId, $language, $ticket, $heskSettings, $modsForHeskSettings) { + $emailTemplate = $this->emailTemplateRetriever->getTemplate($templateId); - $template = self::getFromFileSystem($templateName, $language, false); - $htmlTemplate = self::getFromFileSystem($templateName, $language, true); - $subject = ValidEmailTemplates::getValidEmailTemplates()[$templateName]; + if ($emailTemplate === null) { + throw new InvalidEmailTemplateException($templateId); + } + + $template = self::getFromFileSystem($emailTemplate->fileName, $language, false); + $htmlTemplate = self::getFromFileSystem($emailTemplate->fileName, $language, true); + $subject = $emailTemplate->languageKey; $subject = $this->parseSubject($subject, $ticket, $language, $heskSettings); - $message = $this->parseMessage($template, $ticket, $language, $forStaff, $heskSettings, $modsForHeskSettings, false); - $htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $language, $forStaff, $heskSettings, $modsForHeskSettings, true); + $message = $this->parseMessage($template, $ticket, $language, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, false); + $htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $language, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, true); return new ParsedEmailProperties($subject, $message, $htmlMessage); } @@ -66,13 +74,9 @@ class EmailTemplateParser { * @param $html bool * @return string The template * @throws EmailTemplateNotFoundException If the template was not found in the filesystem for the provided language - * @throws InvalidEmailTemplateException If the $template is not a valid template name */ private function getFromFileSystem($template, $language, $html) { - if (!isset(ValidEmailTemplates::getValidEmailTemplates()[$template])) { - throw new InvalidEmailTemplateException($template); - } $htmlFolder = $html ? 'html/' : ''; /* Get email template */ diff --git a/api/BusinessLogic/Emails/EmailTemplateRetriever.php b/api/BusinessLogic/Emails/EmailTemplateRetriever.php new file mode 100644 index 00000000..6263d8a7 --- /dev/null +++ b/api/BusinessLogic/Emails/EmailTemplateRetriever.php @@ -0,0 +1,65 @@ +validTemplates = array(); + $this->initializeArray(); + } + + const FORGOT_TICKET_ID = 0; + const NEW_REPLY_BY_STAFF = 1; + const NEW_TICKET = 2; + const VERIFY_EMAIL = 3; + const TICKET_CLOSED = 4; + const CATEGORY_MOVED = 5; + const NEW_REPLY_BY_CUSTOMER = 6; + const NEW_TICKET_STAFF = 7; + const TICKET_ASSIGNED_TO_YOU = 8; + const NEW_PM = 9; + const NEW_NOTE = 10; + const RESET_PASSWORD = 11; + const CALENDAR_REMINDER = 12; + const OVERDUE_TICKET = 13; + + function initializeArray() { + if (count($this->validTemplates) > 0) { + //-- Map already built + return; + } + + $this->validTemplates[self::FORGOT_TICKET_ID] = new EmailTemplate(false, 'forgot_ticket_id'); + $this->validTemplates[self::NEW_REPLY_BY_STAFF] = new EmailTemplate(false, 'new_reply_by_staff'); + $this->validTemplates[self::NEW_TICKET] = new EmailTemplate(false, 'new_ticket', 'ticket_received'); + $this->validTemplates[self::VERIFY_EMAIL] = new EmailTemplate(false, 'verify_email'); + $this->validTemplates[self::TICKET_CLOSED] = new EmailTemplate(false, 'ticket_closed'); + $this->validTemplates[self::CATEGORY_MOVED] = new EmailTemplate(true, 'category_moved'); + $this->validTemplates[self::NEW_REPLY_BY_CUSTOMER] = new EmailTemplate(true, 'new_reply_by_customer'); + $this->validTemplates[self::NEW_TICKET_STAFF] = new EmailTemplate(true, 'new_ticket_staff'); + $this->validTemplates[self::TICKET_ASSIGNED_TO_YOU] = new EmailTemplate(true, 'ticket_assigned_to_you'); + $this->validTemplates[self::NEW_PM] = new EmailTemplate(true, 'new_pm'); + $this->validTemplates[self::NEW_NOTE] = new EmailTemplate(true, 'new_note'); + $this->validTemplates[self::RESET_PASSWORD] = new EmailTemplate(true, 'reset_password'); + $this->validTemplates[self::CALENDAR_REMINDER] = new EmailTemplate(true, 'reset_password'); + $this->validTemplates[self::OVERDUE_TICKET] = new EmailTemplate(true, 'overdue_ticket'); + } + + /** + * @param $templateId + * @return EmailTemplate|null + */ + function getTemplate($templateId) { + if (isset($this->validTemplates[$templateId])) { + return $this->validTemplates[$templateId]; + } + + return null; + } +} \ No newline at end of file diff --git a/api/BusinessLogic/Emails/ValidEmailTemplates.php b/api/BusinessLogic/Emails/ValidEmailTemplates.php deleted file mode 100644 index b57d3894..00000000 --- a/api/BusinessLogic/Emails/ValidEmailTemplates.php +++ /dev/null @@ -1,31 +0,0 @@ - 'forgot_ticket_id', - 'new_reply_by_staff' => 'new_reply_by_staff', - 'new_ticket' => 'ticket_received', - 'verify_email' => 'verify_email', - 'ticket_closed' => 'ticket_closed', - 'category_moved' => 'category_moved', - 'new_reply_by_customer' => 'new_reply_by_customer', - 'new_ticket_staff' => 'new_ticket_staff', - 'ticket_assigned_to_you' => 'ticket_assigned_to_you', - 'new_pm' => 'new_pm', - 'new_note' => 'new_note', - 'reset_password' => 'reset_password', - 'calendar_reminder' => 'calendar_reminder', - 'overdue_ticket' => 'overdue_ticket', - ); - } -} \ No newline at end of file diff --git a/api/Tests/BusinessLogic/Emails/EmailSenderHelperTest.php b/api/Tests/BusinessLogic/Emails/EmailSenderHelperTest.php new file mode 100644 index 00000000..4efcb99e --- /dev/null +++ b/api/Tests/BusinessLogic/Emails/EmailSenderHelperTest.php @@ -0,0 +1,41 @@ +emailTemplateParser = $this->createMock(EmailTemplateParser::class); + $this->basicEmailSender = $this->createMock(BasicEmailSender::class); + $this->mailgunEmailSender = $this->createMock(MailgunEmailSender::class); + + $this->emailSenderHelper = new EmailSenderHelper($this->emailTemplateParser, $this->basicEmailSender, + $this->mailgunEmailSender); + } + + function testItParsesTheTemplateForTheTicket() { + + } +}