From 41123e987b0614c907343daef5a23712ff6cb7be Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 26 Feb 2017 21:52:12 -0500 Subject: [PATCH] Some more API changes --- .../Emails/EmailTemplateParser.php | 116 ++++++++++++++++-- api/DataAccess/Security/UserGateway.php | 15 +++ api/autoload.php | 1 + api/composer.json | 3 +- inc/common.inc.php | 1 - 5 files changed, 124 insertions(+), 12 deletions(-) diff --git a/api/BusinessLogic/Emails/EmailTemplateParser.php b/api/BusinessLogic/Emails/EmailTemplateParser.php index ac77fe0d..8f0e9f4d 100644 --- a/api/BusinessLogic/Emails/EmailTemplateParser.php +++ b/api/BusinessLogic/Emails/EmailTemplateParser.php @@ -15,6 +15,7 @@ use BusinessLogic\Statuses\DefaultStatusForAction; use BusinessLogic\Tickets\Ticket; use Core\Constants\Priority; use DataAccess\Categories\CategoryGateway; +use DataAccess\Security\UserGateway; use DataAccess\Statuses\StatusGateway; class EmailTemplateParser { @@ -29,9 +30,15 @@ class EmailTemplateParser { */ private $categoryGateway; - function __construct($statusGateway, $categoryGateway) { + /** + * @var $userGateway UserGateway + */ + private $userGateway; + + function __construct($statusGateway, $categoryGateway, $userGateway) { $this->statusGateway = $statusGateway; $this->categoryGateway = $categoryGateway; + $this->userGateway = $userGateway; } /** @@ -42,20 +49,31 @@ class EmailTemplateParser { function getFormattedEmailForLanguage($templateName, $language, $ticket, $heskSettings) { global $hesklang; - $template = self::getFromFileSystem($templateName, $language); + $template = self::getFromFileSystem($templateName, $language, false); + $htmlTemplate = self::getFromFileSystem($templateName, $language, true); $subject = ValidEmailTemplates::getValidEmailTemplates()[$templateName]; $subject = $this->parseSubject($subject, $ticket, $language, $heskSettings); + $message = $this->parseMessage($subject, $ticket, $language, $heskSettings); } - private function getFromFileSystem($template, $language) + /** + * @param $template string + * @param $language string + * @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 */ - $file = 'language/' . $language . '/emails/' . $template . '.txt'; + $file = "language/{$language}/emails/{$htmlFolder}{$template}.txt"; $absoluteFilePath = __DIR__ . '/../../../' . $file; if (file_exists($absoluteFilePath)) { @@ -71,10 +89,15 @@ class EmailTemplateParser { * @param $language string * @param $heskSettings array * @return string + * @throws \Exception if common.inc.php isn't loaded */ private function parseSubject($subjectTemplate, $ticket, $language, $heskSettings) { global $hesklang; + if (!function_exists('hesk_msgToPlain')) { + throw new \Exception("common.inc.php not loaded!"); + } + if ($ticket === null) { return $subjectTemplate; } @@ -84,7 +107,6 @@ class EmailTemplateParser { $statusName = $defaultStatus->localizedNames[$language]->text; $category = $this->categoryGateway->getAllCategories($heskSettings)[$ticket->categoryId]; - $priority = ''; switch ($ticket->priorityId) { case Priority::CRITICAL: $priority = $hesklang['critical']; @@ -104,10 +126,84 @@ class EmailTemplateParser { } // Special tags - $msg = str_replace('%%SUBJECT%%', $ticket->subject, $subjectTemplate); - $msg = str_replace('%%TRACK_ID%%', $ticket->trackingId, $msg); - $msg = str_replace('%%CATEGORY%%', $category->id, $msg); - $msg = str_replace('%%PRIORITY%%', $priority, $msg); - $msg = str_replace('%%STATUS%%', $statusName, $msg); + $subject = str_replace('%%SUBJECT%%', $ticket->subject, $subjectTemplate); + $subject = str_replace('%%TRACK_ID%%', $ticket->trackingId, $subject); + $subject = str_replace('%%CATEGORY%%', $category->id, $subject); + $subject = str_replace('%%PRIORITY%%', $priority, $subject); + $subject = str_replace('%%STATUS%%', $statusName, $subject); + + return $subject; + } + + /** + * @param $messageTemplate string + * @param $ticket Ticket + * @param $language string + * @param $heskSettings array + * @return string + * @throws \Exception if common.inc.php isn't loaded + */ + private function parseMessage($messageTemplate, $ticket, $language, $admin, $heskSettings) { + global $hesklang; + + if (!function_exists('hesk_msgToPlain')) { + throw new \Exception("common.inc.php not loaded!"); + } + + if ($ticket === null) { + return $messageTemplate; + } + + $heskSettings['site_title'] = hesk_msgToPlain($heskSettings['site_title'], 1); + + // Is email required to view ticket (for customers only)? + $heskSettings['e_param'] = $heskSettings['email_view_ticket'] ? '&e=' . rawurlencode($ticket->email) : ''; + + /* Generate the ticket URLs */ + $trackingURL = $heskSettings['hesk_url']; + $trackingURL .= $admin ? '/' . $heskSettings['admin_dir'] . '/admin_ticket.php' : '/ticket.php'; + $trackingURL .= '?track=' . $ticket['trackid'] . ($admin ? '' : $heskSettings['e_param']) . '&Refresh=' . rand(10000, 99999); + + // Status name and category name + $defaultStatus = $this->statusGateway->getStatusForDefaultAction(DefaultStatusForAction::NEW_TICKET, $heskSettings); + $statusName = hesk_msgToPlain($defaultStatus->localizedNames[$language]->text); + $category = hesk_msgToPlain($this->categoryGateway->getAllCategories($heskSettings)[$ticket->categoryId]); + $owner = hesk_msgToPlain($this->userGateway->getNameForId($ticket->ownerId, $heskSettings)); + + switch ($ticket->priorityId) { + case Priority::CRITICAL: + $priority = $hesklang['critical']; + break; + case Priority::HIGH: + $priority = $hesklang['high']; + break; + case Priority::MEDIUM: + $priority = $hesklang['medium']; + break; + case Priority::LOW: + $priority = $hesklang['low']; + break; + default: + $priority = 'PRIORITY NOT FOUND'; + break; + } + + // Special tags + $msg = str_replace('%%NAME%%', $ticket->name, $messageTemplate); + $msg = str_replace('%%SUBJECT%%', $ticket['subject'], $msg); + $msg = str_replace('%%TRACK_ID%%', $ticket['trackid'], $msg); + $msg = str_replace('%%TRACK_URL%%', $trackingURL, $msg); + $msg = str_replace('%%SITE_TITLE%%', $hesk_settings['site_title'], $msg); + $msg = str_replace('%%SITE_URL%%', $hesk_settings['site_url'], $msg); + $msg = str_replace('%%CATEGORY%%', $ticket['category'], $msg); + $msg = str_replace('%%PRIORITY%%', $ticket['priority'], $msg); + $msg = str_replace('%%OWNER%%', $ticket['owner'], $msg); + $msg = str_replace('%%STATUS%%', $ticket['status'], $msg); + $msg = str_replace('%%EMAIL%%', $ticket['email'], $msg); + $msg = str_replace('%%CREATED%%', $ticket['dt'], $msg); + $msg = str_replace('%%UPDATED%%', $ticket['lastchange'], $msg); + $msg = str_replace('%%ID%%', $ticket['id'], $msg); + + return $subject; } } \ No newline at end of file diff --git a/api/DataAccess/Security/UserGateway.php b/api/DataAccess/Security/UserGateway.php index 1962f3ff..b44be8b5 100644 --- a/api/DataAccess/Security/UserGateway.php +++ b/api/DataAccess/Security/UserGateway.php @@ -31,4 +31,19 @@ class UserGateway extends CommonDao { return $row; } + + // TODO Replace this with a basic User retrieval + function getNameForId($id, $heskSettings) { + $this->init(); + + $rs = hesk_dbQuery("SELECT `name` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "users` WHERE `id` = " . intval($id)); + + if (hesk_dbNumRows($rs) === 0) { + return null; + } + + $row = hesk_dbFetchAssoc($rs); + + return $row['name']; + } } \ No newline at end of file diff --git a/api/autoload.php b/api/autoload.php index 1a2db8c2..5ffee38e 100644 --- a/api/autoload.php +++ b/api/autoload.php @@ -4,6 +4,7 @@ // Core requirements define('IN_SCRIPT', 1); define('HESK_PATH', '../'); +require_once(__DIR__ . 'vendor/autoload.php'); require_once(__DIR__ . '/bootstrap.php'); require_once(__DIR__ . '/../hesk_settings.inc.php'); require_once(__DIR__ . '/../inc/common.inc.php'); diff --git a/api/composer.json b/api/composer.json index 8b3f0002..f79dd05d 100644 --- a/api/composer.json +++ b/api/composer.json @@ -16,6 +16,7 @@ "php-http/guzzle6-adapter": "^1.1", "php-http/message": "^1.5", "php-http/curl-client": "^1.7", - "guzzlehttp/psr7": "^1.3" + "guzzlehttp/psr7": "^1.3", + "doctrine/orm": "*" } } diff --git a/inc/common.inc.php b/inc/common.inc.php index cf54fa9e..72772a16 100644 --- a/inc/common.inc.php +++ b/inc/common.inc.php @@ -989,7 +989,6 @@ function hesk_ticketToPlain($ticket, $specialchars = 0, $strip = 1) } } // END hesk_ticketToPlain() - function hesk_msgToPlain($msg, $specialchars = 0, $strip = 1) { $msg = preg_replace('/\/i', "$2", $msg);