From b03312f73cd7bd7bbacb323147c3cb58c5c2c3d2 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 23 Apr 2017 22:08:48 -0400 Subject: [PATCH] Fixed a couple issues with deleting tickets --- api/ApplicationContext.php | 9 ++++++--- .../Attachments/AttachmentHandler.php | 2 ++ api/BusinessLogic/Tickets/Ticket.php | 5 +++++ api/BusinessLogic/Tickets/TicketDeleter.php | 6 ++++++ .../Tickets/StaffTicketController.php | 17 +++++++++++++++++ .../Attachments/AttachmentGateway.php | 9 +++++++++ api/DataAccess/Tickets/TicketGateway.php | 4 ++++ api/index.php | 2 +- 8 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 api/Controllers/Tickets/StaffTicketController.php diff --git a/api/ApplicationContext.php b/api/ApplicationContext.php index 602604af..d052b8e6 100644 --- a/api/ApplicationContext.php +++ b/api/ApplicationContext.php @@ -14,6 +14,7 @@ use BusinessLogic\Security\UserContextBuilder; use BusinessLogic\Security\UserToTicketChecker; use BusinessLogic\Settings\ApiChecker; use BusinessLogic\Tickets\Autoassigner; +use BusinessLogic\Tickets\TicketDeleter; use BusinessLogic\Tickets\TicketRetriever; use BusinessLogic\Tickets\TicketCreator; use BusinessLogic\Tickets\NewTicketValidator; @@ -81,6 +82,7 @@ class ApplicationContext { $this->get[MailgunEmailSender::class]); // Tickets + $this->get[UserToTicketChecker::class] = new UserToTicketChecker($this->get[UserGateway::class]); $this->get[TicketGateway::class] = new TicketGateway(); $this->get[TicketRetriever::class] = new TicketRetriever($this->get[TicketGateway::class]); $this->get[TicketValidators::class] = new TicketValidators($this->get[TicketGateway::class]); @@ -98,9 +100,6 @@ class ApplicationContext { $this->get[EmailSenderHelper::class], $this->get[UserGateway::class], $this->get[ModsForHeskSettingsGateway::class]); - - // Attachments - $this->get[UserToTicketChecker::class] = new UserToTicketChecker($this->get[UserGateway::class]); $this->get[FileWriter::class] = new FileWriter(); $this->get[FileReader::class] = new FileReader(); $this->get[FileDeleter::class] = new FileDeleter(); @@ -114,5 +113,9 @@ class ApplicationContext { $this->get[FileReader::class], $this->get[TicketGateway::class], $this->get[UserToTicketChecker::class]); + $this->get[TicketDeleter::class] = + new TicketDeleter($this->get[TicketGateway::class], + $this->get[UserToTicketChecker::class], + $this->get[AttachmentHandler::class]); } } \ No newline at end of file diff --git a/api/BusinessLogic/Attachments/AttachmentHandler.php b/api/BusinessLogic/Attachments/AttachmentHandler.php index d51ce710..862549e9 100644 --- a/api/BusinessLogic/Attachments/AttachmentHandler.php +++ b/api/BusinessLogic/Attachments/AttachmentHandler.php @@ -111,6 +111,7 @@ class AttachmentHandler { if ($attachment->id === $attachmentId) { $indexToRemove = $i; $this->fileDeleter->deleteFile($attachment->savedName, $heskSettings['attach_dir']); + $this->attachmentGateway->deleteAttachment($attachment->id, $heskSettings); } } @@ -122,6 +123,7 @@ class AttachmentHandler { $replyId = $reply->id; $attachmentType = AttachmentType::REPLY; $this->fileDeleter->deleteFile($attachment->savedName, $heskSettings['attach_dir']); + $this->attachmentGateway->deleteAttachment($attachment->id, $heskSettings); } } } diff --git a/api/BusinessLogic/Tickets/Ticket.php b/api/BusinessLogic/Tickets/Ticket.php index ea50b11e..f2e7a59a 100644 --- a/api/BusinessLogic/Tickets/Ticket.php +++ b/api/BusinessLogic/Tickets/Ticket.php @@ -37,6 +37,7 @@ class Ticket { $ticket->lastReplier = $row['replierid'] === null ? null : intval($row['replierid']); $ticket->archived = intval($row['archive']) === 1; $ticket->locked = intval($row['locked']) === 1; + $ticket->attachments = array(); if (trim($row['attachments']) !== '') { $attachments = explode(',', $row['attachments']); @@ -110,6 +111,10 @@ class Ticket { $attachments = explode(',', $replyRow['attachments']); $attachmentArray = array(); foreach ($attachments as $attachment) { + if (trim($attachment) === '') { + continue; + } + $attachmentRow = explode('#', $attachment); $attachmentModel = new Attachment(); diff --git a/api/BusinessLogic/Tickets/TicketDeleter.php b/api/BusinessLogic/Tickets/TicketDeleter.php index 826a4731..cc9f3f8a 100644 --- a/api/BusinessLogic/Tickets/TicketDeleter.php +++ b/api/BusinessLogic/Tickets/TicketDeleter.php @@ -36,6 +36,12 @@ class TicketDeleter { $this->attachmentHandler->deleteAttachmentFromTicket($ticketId, $attachment->id, $userContext, $heskSettings); } + foreach ($ticket->replies as $reply) { + foreach ($reply->attachments as $attachment) { + $this->attachmentHandler->deleteAttachmentFromTicket($ticketId, $attachment->id, $userContext, $heskSettings); + } + } + $this->ticketGateway->deleteReplyDraftsForTicket($ticketId, $heskSettings); $this->ticketGateway->deleteRepliesForTicket($ticketId, $heskSettings); diff --git a/api/Controllers/Tickets/StaffTicketController.php b/api/Controllers/Tickets/StaffTicketController.php new file mode 100644 index 00000000..27283b54 --- /dev/null +++ b/api/Controllers/Tickets/StaffTicketController.php @@ -0,0 +1,17 @@ +get[TicketDeleter::class]; + + $ticketDeleter->deleteTicket($id, $userContext, $hesk_settings); + } +} \ No newline at end of file diff --git a/api/DataAccess/Attachments/AttachmentGateway.php b/api/DataAccess/Attachments/AttachmentGateway.php index ac7403fe..fc9982fc 100644 --- a/api/DataAccess/Attachments/AttachmentGateway.php +++ b/api/DataAccess/Attachments/AttachmentGateway.php @@ -53,4 +53,13 @@ class AttachmentGateway extends CommonDao { return $attachment; } + + function deleteAttachment($attachmentId, $heskSettings) { + $this->init(); + + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "attachments` + WHERE `att_id` = " . intval($attachmentId)); + + $this->close(); + } } \ No newline at end of file diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index a326a537..b6f4bd99 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -279,6 +279,10 @@ class TicketGateway extends CommonDao { * @param $heskSettings array */ function deleteTicket($ticketId, $heskSettings) { + $this->init(); + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `id` = " . intval($ticketId)); + + $this->close(); } } \ No newline at end of file diff --git a/api/index.php b/api/index.php index 22db3b70..c8d333bf 100644 --- a/api/index.php +++ b/api/index.php @@ -153,7 +153,7 @@ Link::all(array( '/v1/tickets/{i}' => \Controllers\Tickets\CustomerTicketController::class, '/v1/tickets' => \Controllers\Tickets\CustomerTicketController::class, // Tickets - Staff - '/v1/staff/tickets/{i}' => null, + '/v1/staff/tickets/{i}' => \Controllers\Tickets\StaffTicketController::class, // Attachments '/v1/staff/tickets/{i}/attachments' => \Controllers\Attachments\StaffTicketAttachmentsController::class, '/v1/staff/tickets/{i}/attachments/{i}' => \Controllers\Attachments\StaffTicketAttachmentsController::class,