diff --git a/api/BusinessLogic/Attachments/AttachmentHandler.php b/api/BusinessLogic/Attachments/AttachmentHandler.php index d56ce05e..60c32cf0 100644 --- a/api/BusinessLogic/Attachments/AttachmentHandler.php +++ b/api/BusinessLogic/Attachments/AttachmentHandler.php @@ -4,6 +4,8 @@ namespace BusinessLogic\Attachments; use BusinessLogic\Exceptions\ValidationException; +use BusinessLogic\Tickets\Attachment; +use BusinessLogic\Tickets\Ticket; use BusinessLogic\ValidationModel; use DataAccess\Attachments\AttachmentGateway; use DataAccess\Files\FileWriter; @@ -52,6 +54,9 @@ class AttachmentHandler { $this->fileWriter->writeToFile($ticketAttachment->savedName, $heskSettings['attach_dir'], $decodedAttachment); $attachmentId = $this->attachmentGateway->createAttachmentForTicket($ticketAttachment, $heskSettings); + + $this->updateAttachmentsOnTicket($ticket, $ticketAttachment, $attachmentId, $heskSettings); + $ticketAttachment->id = $attachmentId; return $ticketAttachment; @@ -366,4 +371,20 @@ class AttachmentHandler { } return true; } + + /** + * @param $ticket Ticket + * @param $ticketAttachment TicketAttachment + * @param $attachmentId int + * @param $heskSettings array + */ + private function updateAttachmentsOnTicket($ticket, $ticketAttachment, $attachmentId, $heskSettings) { + $attachments = $ticket->attachments === null ? array() : $ticket->attachments; + $newAttachment = new Attachment(); + $newAttachment->savedName = $ticketAttachment->savedName; + $newAttachment->fileName = $ticketAttachment->displayName; + $newAttachment->id = $attachmentId; + $attachments[] = $newAttachment; + $this->ticketGateway->updateAttachmentsForTicket($ticket->id, $attachments, $heskSettings); + } } \ No newline at end of file diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index 8b1f29d4..faf0428c 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -3,6 +3,7 @@ namespace DataAccess\Tickets; +use BusinessLogic\Tickets\Attachment; use BusinessLogic\Tickets\Ticket; use BusinessLogic\Tickets\TicketGatewayGeneratedFields; use DataAccess\CommonDao; @@ -205,4 +206,25 @@ class TicketGateway extends CommonDao { return $generatedFields; } + + /** + * @param $ticketId int + * @param $attachments Attachment[] + * @param $heskSettings array + * + * Crappy logic that should just be pulled from the attachments table, but using for backwards compatibility + */ + function updateAttachmentsForTicket($ticketId, $attachments, $heskSettings) { + $this->init(); + + $attachmentStrings = array(); + foreach ($attachments as $attachment) { + $attachmentStrings[] = "{$attachment->id}#{$attachment->fileName}#{$attachment->savedName}"; + } + $attachmentStringToSave = implode(',', $attachmentStrings); + + hesk_dbQuery("UPDATE `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` + SET `attachments` = '" . hesk_dbEscape($attachmentStringToSave) . "' + WHERE `id` = " . intval($ticketId)); + } } \ No newline at end of file diff --git a/api/index.php b/api/index.php index 4382f43c..3a894660 100644 --- a/api/index.php +++ b/api/index.php @@ -147,11 +147,14 @@ Link::before('before'); Link::all(array( // Categories - '/v1/categories' => '\Controllers\Categories\CategoryController::printAllCategories', - '/v1/categories/{i}' => '\Controllers\Categories\CategoryController', + '/v1/categories' => \Controllers\Categories\CategoryController::class . '::printAllCategories', + '/v1/categories/{i}' => \Controllers\Categories\CategoryController::class, // Tickets - '/v1/tickets/{i}' => '\Controllers\Tickets\TicketController', - '/v1/tickets' => '\Controllers\Tickets\TicketController', + '/v1/tickets/{i}' => \Controllers\Tickets\TicketController::class, + '/v1/tickets' => \Controllers\Tickets\TicketController::class, + + // Attachments + '/v1/staff/attachments' => \Controllers\Attachments\StaffTicketAttachmentsController::class, // Any URL that doesn't match goes to the 404 handler '404' => 'handle404'