From ef9cbf98b07fd7206f6456921da309948396b468 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Mon, 1 May 2017 22:05:21 -0400 Subject: [PATCH] Improve POST ticket --- .../Emails/EmailTemplateParser.php | 25 ++++++++++++++----- api/BusinessLogic/Statuses/Status.php | 2 +- api/BusinessLogic/Tickets/Ticket.php | 10 ++++++-- api/BusinessLogic/Tickets/TicketCreator.php | 2 +- api/BusinessLogic/Tickets/TicketRetriever.php | 2 +- .../Tickets/TrackingIdGenerator.php | 8 +++--- .../Tickets/CustomerTicketController.php | 2 +- api/DataAccess/Tickets/TicketGateway.php | 18 +++++++++++++ api/index.php | 1 - 9 files changed, 53 insertions(+), 17 deletions(-) diff --git a/api/BusinessLogic/Emails/EmailTemplateParser.php b/api/BusinessLogic/Emails/EmailTemplateParser.php index 1bcd84a6..927a0d00 100644 --- a/api/BusinessLogic/Emails/EmailTemplateParser.php +++ b/api/BusinessLogic/Emails/EmailTemplateParser.php @@ -3,6 +3,7 @@ namespace BusinessLogic\Emails; +use BusinessLogic\Exceptions\ApiFriendlyException; use BusinessLogic\Exceptions\EmailTemplateNotFoundException; use BusinessLogic\Exceptions\InvalidEmailTemplateException; use BusinessLogic\Statuses\DefaultStatusForAction; @@ -63,9 +64,21 @@ class EmailTemplateParser { $htmlTemplate = self::getFromFileSystem($emailTemplate->fileName, $languageCode, true); $subject = $hesklang[$emailTemplate->languageKey]; - $subject = $this->parseSubject($subject, $ticket, $languageCode, $heskSettings); - $message = $this->parseMessage($template, $ticket, $languageCode, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, false); - $htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $languageCode, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, true); + $fullLanguageName = null; + foreach ($heskSettings['languages'] as $key => $value) { + if ($value['folder'] === $languageCode) { + $fullLanguageName = $key; + break; + } + } + + if ($fullLanguageName === null) { + throw new \Exception("Language code {$languageCode} did not return any valid HESK languages!"); + } + + $subject = $this->parseSubject($subject, $ticket, $fullLanguageName, $heskSettings); + $message = $this->parseMessage($template, $ticket, $fullLanguageName, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, false); + $htmlMessage = $this->parseMessage($htmlTemplate, $ticket, $fullLanguageName, $emailTemplate->forStaff, $heskSettings, $modsForHeskSettings, true); return new ParsedEmailProperties($subject, $message, $htmlMessage); } @@ -113,7 +126,7 @@ class EmailTemplateParser { // Status name and category name $defaultStatus = $this->statusGateway->getStatusForDefaultAction(DefaultStatusForAction::NEW_TICKET, $heskSettings); - $statusName = $defaultStatus->localizedNames[$language]->text; + $statusName = $defaultStatus->localizedNames[$language]; $category = $this->categoryGateway->getAllCategories($heskSettings)[$ticket->categoryId]; switch ($ticket->priorityId) { @@ -175,7 +188,7 @@ class EmailTemplateParser { // Status name and category name $defaultStatus = $this->statusGateway->getStatusForDefaultAction(DefaultStatusForAction::NEW_TICKET, $heskSettings); - $statusName = hesk_msgToPlain($defaultStatus->localizedNames[$language]->text); + $statusName = hesk_msgToPlain($defaultStatus->localizedNames[$language]); $category = hesk_msgToPlain($this->categoryGateway->getAllCategories($heskSettings)[$ticket->categoryId]->name); $owner = hesk_msgToPlain($this->userGateway->getUserById($ticket->ownerId, $heskSettings)->name); @@ -217,7 +230,7 @@ class EmailTemplateParser { for ($i=1; $i<=50; $i++) { $k = 'custom'.$i; - if (isset($heskSettings['custom_fields'][$k])) { + if (isset($heskSettings['custom_fields'][$k]) && isset($ticket->customFields[$i])) { $v = $heskSettings['custom_fields'][$k]; switch ($v['type']) { diff --git a/api/BusinessLogic/Statuses/Status.php b/api/BusinessLogic/Statuses/Status.php index 8e58345f..c5ef3242 100644 --- a/api/BusinessLogic/Statuses/Status.php +++ b/api/BusinessLogic/Statuses/Status.php @@ -66,7 +66,7 @@ class Status { public $sort; /** - * @var $name StatusLanguage[] + * @var $name string[] */ public $localizedNames; } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/Ticket.php b/api/BusinessLogic/Tickets/Ticket.php index 029dcff4..42eda13c 100644 --- a/api/BusinessLogic/Tickets/Ticket.php +++ b/api/BusinessLogic/Tickets/Ticket.php @@ -24,7 +24,13 @@ class Ticket { $ticket->closedDate = $row['closedat']; if (trim($row['articles']) !== '') { - $ticket->suggestedArticles = explode(',', $row['articles']); + $suggestedArticles = explode(',', $row['articles']); + + $articlesAsInts = array(); + foreach ($suggestedArticles as $article) { + $articlesAsInts[] = intval($article); + } + $ticket->suggestedArticles = $articlesAsInts; } $ticket->ipAddress = $row['ip']; @@ -202,7 +208,7 @@ class Ticket { public $closedDate; /** - * @var string[] + * @var int[] */ public $suggestedArticles = array(); diff --git a/api/BusinessLogic/Tickets/TicketCreator.php b/api/BusinessLogic/Tickets/TicketCreator.php index 1c0c0818..245da2a1 100644 --- a/api/BusinessLogic/Tickets/TicketCreator.php +++ b/api/BusinessLogic/Tickets/TicketCreator.php @@ -145,7 +145,7 @@ class TicketCreator { if ($ticketRequest->sendEmailToCustomer && $emailVerified) { $this->emailSenderHelper->sendEmailForTicket(EmailTemplateRetriever::NEW_TICKET, $ticketRequest->language, $addressees, $ticket, $heskSettings, $modsForHeskSettings); - } else if ($modsForHeskSettings[''] && !$emailVerified) { + } else if ($modsForHeskSettings['customer_email_verification_required'] && !$emailVerified) { $this->emailSenderHelper->sendEmailForTicket(EmailTemplateRetriever::VERIFY_EMAIL, $ticketRequest->language, $addressees, $ticket, $heskSettings, $modsForHeskSettings); } diff --git a/api/BusinessLogic/Tickets/TicketRetriever.php b/api/BusinessLogic/Tickets/TicketRetriever.php index b63bb8e0..5a237a98 100644 --- a/api/BusinessLogic/Tickets/TicketRetriever.php +++ b/api/BusinessLogic/Tickets/TicketRetriever.php @@ -28,7 +28,7 @@ class TicketRetriever { function getTicketById($id, $heskSettings, $userContext) { $ticket = $this->ticketGateway->getTicketById($id, $heskSettings); - if ($ticket !== null) { + if ($ticket === null) { throw new ApiFriendlyException("Ticket {$id} not found!", "Ticket Not Found", 404); } diff --git a/api/BusinessLogic/Tickets/TrackingIdGenerator.php b/api/BusinessLogic/Tickets/TrackingIdGenerator.php index 25cbe010..623ab1fe 100644 --- a/api/BusinessLogic/Tickets/TrackingIdGenerator.php +++ b/api/BusinessLogic/Tickets/TrackingIdGenerator.php @@ -36,9 +36,9 @@ class TrackingIdGenerator { $trackingId = $this->formatTrackingId($trackingId); /* Check for duplicate IDs */ - $ticket = $this->ticketGateway->getTicketByTrackingId($trackingId, $heskSettings); + $goodId = !$this->ticketGateway->doesTicketExist($trackingId, $heskSettings); - if ($ticket === null) { + if ($goodId) { return $trackingId; } @@ -57,9 +57,9 @@ class TrackingIdGenerator { /* Format the ID to the correct shape and check wording */ $trackingId = $this->formatTrackingId($trackingId); - $ticket = $this->ticketGateway->getTicketByTrackingId($trackingId, $heskSettings); + $goodId = !$this->ticketGateway->doesTicketExist($trackingId, $heskSettings); - if ($ticket === null) { + if ($goodId) { return $trackingId; } diff --git a/api/Controllers/Tickets/CustomerTicketController.php b/api/Controllers/Tickets/CustomerTicketController.php index 7b9d455e..cd8e9481 100644 --- a/api/Controllers/Tickets/CustomerTicketController.php +++ b/api/Controllers/Tickets/CustomerTicketController.php @@ -55,7 +55,7 @@ class CustomerTicketController { $ticketRequest->screenResolution = Helpers::safeArrayGet($json, 'screenResolution'); $ticketRequest->ipAddress = Helpers::safeArrayGet($json, 'ip'); $ticketRequest->language = Helpers::safeArrayGet($json, 'language'); - $ticketRequest->sendEmailToCustomer = Helpers::safeArrayGet($json, 'sendEmailToCustomer'); + $ticketRequest->sendEmailToCustomer = true; $ticketRequest->customFields = array(); $jsonCustomFields = Helpers::safeArrayGet($json, 'customFields'); diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index 34f66e11..1645618b 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -66,6 +66,24 @@ class TicketGateway extends CommonDao { return $tickets; } + /** + * @param $trackingId string + * @param $heskSettings array + * @return bool + */ + function doesTicketExist($trackingId, $heskSettings) { + $this->init(); + + $rs = hesk_dbQuery("SELECT 1 FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` + WHERE `trackid` = '" . hesk_dbEscape($trackingId) . "'"); + + $ticketExists = hesk_dbNumRows($rs) > 0; + + $this->close(); + + return $ticketExists; + } + /** * @param $trackingId string * @param $heskSettings array diff --git a/api/index.php b/api/index.php index 991160c5..079fd851 100644 --- a/api/index.php +++ b/api/index.php @@ -150,7 +150,6 @@ Link::all(array( '/v1/categories' => \Controllers\Categories\CategoryController::class . '::printAllCategories', '/v1/categories/{i}' => \Controllers\Categories\CategoryController::class, // Tickets - '/v1/tickets/{i}' => \Controllers\Tickets\CustomerTicketController::class, '/v1/tickets' => \Controllers\Tickets\CustomerTicketController::class, // Tickets - Staff '/v1/staff/tickets/{i}' => \Controllers\Tickets\StaffTicketController::class,