From 1b6cca8ddfee05b4a5f6d837304966030e3ca4dd Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Tue, 14 Feb 2017 12:45:02 -0500 Subject: [PATCH] Create ticket endpoint now working, but needs some changes --- api/BusinessLogic/Exceptions/ValidationException.php | 10 ++++------ api/BusinessLogic/Tickets/Ticket.php | 7 +++++-- api/Controllers/JsonRetriever.php | 2 +- api/Controllers/Tickets/TicketController.php | 4 +++- api/DataAccess/Tickets/TicketGateway.php | 10 +++++----- api/index.php | 6 +++--- 6 files changed, 21 insertions(+), 18 deletions(-) diff --git a/api/BusinessLogic/Exceptions/ValidationException.php b/api/BusinessLogic/Exceptions/ValidationException.php index aa382328..6ed8b552 100644 --- a/api/BusinessLogic/Exceptions/ValidationException.php +++ b/api/BusinessLogic/Exceptions/ValidationException.php @@ -5,19 +5,17 @@ namespace BusinessLogic\Exceptions; use BusinessLogic\ValidationModel; use Exception; -class ValidationException extends Exception { - public $validationModel; - +class ValidationException extends ApiFriendlyException { /** * ValidationException constructor. * @param ValidationModel $validationModel The validation model * @throws Exception If the validationModel's errorKeys is empty */ function __construct($validationModel) { - if (count($validationModel->errorKeys) === 0) { - throw new Exception('Tried to throw a ValidationException, but the validation model was valid!'); + if (count($validationModel->errorKeys) === 0 || $validationModel->valid) { + throw new Exception('Tried to throw a ValidationException, but the validation model was valid or had 0 error keys!'); } - $this->validationModel = $validationModel; + parent::__construct(implode(",", $validationModel->errorKeys), "Validation Failed. Error keys are available in the message section.", 400); } } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/Ticket.php b/api/BusinessLogic/Tickets/Ticket.php index 7a8f7b1f..dd480fd6 100644 --- a/api/BusinessLogic/Tickets/Ticket.php +++ b/api/BusinessLogic/Tickets/Ticket.php @@ -238,8 +238,11 @@ class Ticket { function getAttachmentsForDatabase() { $attachmentArray = array(); - foreach ($this->attachments as $attachment) { - $attachmentArray[] = $attachment->id . '#' . $attachment->fileName . '#' . $attachment->savedName; + + if ($this->attachments !== null) { + foreach ($this->attachments as $attachment) { + $attachmentArray[] = $attachment->id . '#' . $attachment->fileName . '#' . $attachment->savedName; + } } return implode(',', $attachmentArray); diff --git a/api/Controllers/JsonRetriever.php b/api/Controllers/JsonRetriever.php index ee590448..74711032 100644 --- a/api/Controllers/JsonRetriever.php +++ b/api/Controllers/JsonRetriever.php @@ -17,6 +17,6 @@ class JsonRetriever { */ static function getJsonData() { $json = file_get_contents('php://input'); - return json_decode($json); + return json_decode($json, true); } } \ No newline at end of file diff --git a/api/Controllers/Tickets/TicketController.php b/api/Controllers/Tickets/TicketController.php index 2d0bdf95..294882c5 100644 --- a/api/Controllers/Tickets/TicketController.php +++ b/api/Controllers/Tickets/TicketController.php @@ -27,7 +27,9 @@ class TicketController { $jsonRequest = JsonRetriever::getJsonData(); - $ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $modsForHeskSettings, $userContext); + $ticket = $ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $modsForHeskSettings, $userContext); + + return output($ticket); } /** diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index 86efcb23..5c6d7cb0 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -124,10 +124,10 @@ class TicketGateway extends CommonDao { $userAgent = $ticket->userAgent !== null ? $ticket->userAgent : ''; $screenResolutionWidth = $ticket->screenResolution !== null && isset($ticket->screenResolution[0]) - && $ticket->screenResolution[0] !== null ? intval($ticket->screenResolution[0]) : ''; + && $ticket->screenResolution[0] !== null ? intval($ticket->screenResolution[0]) : 'NULL'; $screenResolutionHeight = $ticket->screenResolution !== null && isset($ticket->screenResolution[1]) - && $ticket->screenResolution[1] !== null ? intval($ticket->screenResolution[1]) : ''; + && $ticket->screenResolution[1] !== null ? intval($ticket->screenResolution[1]) : 'NULL'; $sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` ( @@ -169,14 +169,14 @@ class TicketGateway extends CommonDao { '" . hesk_dbEscape($ticket->message) . "', NOW(), NOW(), - " . $suggestedArticles . ", + '" . $suggestedArticles . "', '" . hesk_dbEscape($ticket->ipAddress) . "', '" . hesk_dbEscape($language) . "', '" . intval($ticket->openedBy) . "', '" . intval($ticket->ownerId) . "', '" . hesk_dbEscape($ticket->getAttachmentsForDatabase()) . "', '', - '" . intval($ticket->statusId) . "', + " . intval($ticket->statusId) . ", '" . hesk_dbEscape($latitude) . "', '" . hesk_dbEscape($longitude) . "', '" . hesk_dbEscape($ticket->usesHtml) . "', @@ -192,7 +192,7 @@ class TicketGateway extends CommonDao { hesk_dbQuery($sql); $rs = hesk_dbQuery('SELECT `dt`, `lastchange` FROM `' . hesk_dbEscape($heskSettings['db_pfix']) . 'tickets` WHERE `id` = ' . intval(hesk_dbInsertID())); - $row = hesk_dbFetchRow($rs); + $row = hesk_dbFetchAssoc($rs); $generatedFields = new TicketGatewayGeneratedFields(); $generatedFields->dateCreated = $row['dt']; diff --git a/api/index.php b/api/index.php index 07e97f30..d6932a50 100644 --- a/api/index.php +++ b/api/index.php @@ -41,13 +41,13 @@ function errorHandler($errorNumber, $errorMessage, $errorFile, $errorLine) { * @param $exception Exception */ function exceptionHandler($exception) { - if (exceptionIsOfType($exception, 'ApiFriendlyException')) { + if (exceptionIsOfType($exception, \BusinessLogic\Exceptions\ApiFriendlyException::class)) { /* @var $castedException \BusinessLogic\Exceptions\ApiFriendlyException */ $castedException = $exception; print_error($castedException->title, $castedException->getMessage(), $castedException->httpResponseCode); } else { - if (exceptionIsOfType($exception, 'SQLException')) { + if (exceptionIsOfType($exception, \Core\Exceptions\SQLException::class)) { /* @var $castedException \Core\Exceptions\SQLException */ $castedException = $exception; print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $castedException->failingQuery, $exception->getTraceAsString())); @@ -66,7 +66,7 @@ function exceptionHandler($exception) { * @return bool */ function exceptionIsOfType($exception, $class) { - return strpos(get_class($exception), $class) !== false; + return is_a($exception, $class); } function fatalErrorShutdownHandler() {