Mods-for-HESK-Netsyms/api/Controllers/Attachments/StaffTicketAttachmentsController.php

70 lines
2.5 KiB
PHP
Raw Normal View History

2017-03-20 22:16:35 -04:00
<?php
namespace Controllers\Attachments;
use BusinessLogic\Attachments\AttachmentHandler;
use BusinessLogic\Attachments\AttachmentRetriever;
2017-03-20 22:16:35 -04:00
use BusinessLogic\Attachments\CreateAttachmentForTicketModel;
use BusinessLogic\Exceptions\ApiFriendlyException;
use BusinessLogic\Helpers;
use BusinessLogic\Security\UserToTicketChecker;
2017-03-20 22:16:35 -04:00
use Controllers\JsonRetriever;
class StaffTicketAttachmentsController {
function get($ticketId, $attachmentId) {
global $hesk_settings, $applicationContext, $userContext;
2017-03-20 22:16:35 -04:00
$this->verifyAttachmentsAreEnabled($hesk_settings);
/* @var $attachmentRetriever AttachmentRetriever */
$attachmentRetriever = $applicationContext->get[AttachmentRetriever::class];
2017-04-11 12:39:09 -04:00
$contents = $attachmentRetriever->getAttachmentContentsForTicket($ticketId, $attachmentId, $userContext, $hesk_settings);
2017-04-30 22:02:34 -04:00
output(array('contents' => $contents));
}
private function verifyAttachmentsAreEnabled($heskSettings) {
if (!$heskSettings['attachments']['use']) {
2017-03-20 22:16:35 -04:00
throw new ApiFriendlyException('Attachments are disabled on this server', 'Attachments Disabled', 404);
}
}
function post($ticketId) {
global $hesk_settings, $applicationContext, $userContext;
$this->verifyAttachmentsAreEnabled($hesk_settings);
2017-03-20 22:16:35 -04:00
/* @var $attachmentHandler AttachmentHandler */
$attachmentHandler = $applicationContext->get[AttachmentHandler::class];
$createAttachmentForTicketModel = $this->createModel(JsonRetriever::getJsonData(), $ticketId);
2017-03-20 22:16:35 -04:00
$createdAttachment = $attachmentHandler->createAttachmentForTicket(
$createAttachmentForTicketModel, $userContext, $hesk_settings);
2017-03-20 22:16:35 -04:00
return output($createdAttachment, 201);
}
private function createModel($json, $ticketId) {
2017-03-20 22:16:35 -04:00
$model = new CreateAttachmentForTicketModel();
$model->attachmentContents = Helpers::safeArrayGet($json, 'data');
$model->displayName = Helpers::safeArrayGet($json, 'displayName');
2017-04-30 22:02:34 -04:00
$model->isEditing = Helpers::safeArrayGet($json, 'isEditing');
$model->ticketId = $ticketId;
2017-03-20 22:16:35 -04:00
return $model;
}
2017-04-12 22:00:56 -04:00
function delete($ticketId, $attachmentId) {
global $applicationContext, $hesk_settings, $userContext;
/* @var $attachmentHandler AttachmentHandler */
$attachmentHandler = $applicationContext->get[AttachmentHandler::class];
$attachmentHandler->deleteAttachmentFromTicket($ticketId, $attachmentId, $userContext, $hesk_settings);
return http_response_code(204);
}
2017-03-20 22:16:35 -04:00
}