opensupports/server/controllers/ticket/edit-comment.php

106 lines
3.4 KiB
PHP
Raw Normal View History

2019-06-27 03:04:56 +02:00
<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
/**
* @api {post} /ticket/edit-comment Edit a comment
* @apiVersion 4.10.0
2019-06-27 03:04:56 +02:00
*
* @apiName Edit comment
*
* @apiGroup Ticket
*
* @apiDescription This path edits a comment.
2019-06-27 03:04:56 +02:00
*
* @apiPermission user
*
* @apiParam {String} content The new content of the comment.
* @apiParam {Number} ticketEventId The id of the ticket event.
* @apiParam {Number} ticketNumber The number of the ticket.
2019-06-27 03:04:56 +02:00
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_CONTENT
* @apiUse INVALID_TICKET
* @apiUse INVALID_TICKET_EVENT
* @apiUse TICKET_CONTENT_CANNOT_BE_EDITED
2019-06-27 03:04:56 +02:00
*
* @apiSuccess {Object} data Empty object
*
*/
class EditCommentController extends Controller {
const PATH = '/edit-comment';
const METHOD = 'POST';
public function validations() {
return [
'permission' => 'user',
'requestData' => [
'content' => [
'validation' => DataValidator::content(),
'error' => ERRORS::INVALID_CONTENT
],
'ticketNumber' => [
'validation' => DataValidator::oneOf(DataValidator::validTicketNumber(),DataValidator::nullType()),
'error' => ERRORS::INVALID_TICKET
2019-06-27 03:04:56 +02:00
]
]
];
2019-06-27 03:04:56 +02:00
}
public function handler() {
$user = Controller::getLoggedUser();
$newcontent = Controller::request('content', true);
2019-07-05 01:22:38 +02:00
$ticketNumberLog = null;
2019-06-27 03:04:56 +02:00
$ticketevent = Ticketevent::getTicketEvent(Controller::request('ticketEventId'));
if(!$ticketevent->isNull()) {
$ticket = Ticket::getDataStore($ticketevent->ticketId);
} else {
$ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber'));
}
if(!Controller::isStaffLogged() && $user->id !== $ticketevent->authorUserId && $user->id !== $ticket->authorId) {
2019-06-27 03:04:56 +02:00
throw new RequestException(ERRORS::NO_PERMISSION);
}
if (!$ticketevent->isNull()) {
if($user->id !== $ticketevent->authorUserId) {
throw new RequestException(ERRORS::NO_PERMISSION);
}
} else if ($user->id !== $ticket->authorId) {
throw new RequestException(ERRORS::NO_PERMISSION);
}
if(Controller::isStaffLogged() && !$user->canManageTicket($ticket)) {
throw new RequestException(ERRORS::NO_PERMISSION);
}
2019-07-05 01:22:38 +02:00
if(!$ticketevent->isNull()) {
if($ticketevent->type !== "COMMENT" || $ticket->closed || $ticket->getLatestEventOfType("COMMENT")['id'] !== $ticketevent->id) {
throw new RequestException(ERRORS::INVALID_TICKET_EVENT);
}
} else if(sizeof($ticket->getEventsOfType("COMMENT"))) {
throw new RequestException(ERRORS::TICKET_CONTENT_CANNOT_BE_EDITED);
2019-07-05 01:22:38 +02:00
}
2019-06-27 03:04:56 +02:00
if(!$ticketevent->isNull()){
2019-07-05 01:22:38 +02:00
$ticketNumber = Ticket::getTicket($ticketevent->ticketId)->ticketNumber;
2019-06-27 03:04:56 +02:00
$ticketevent->content = $newcontent;
$ticketevent->editedContent = true;
$ticketevent->store();
} else {
2019-07-05 01:22:38 +02:00
$ticketNumber = $ticket->ticketNumber;
2019-06-27 03:04:56 +02:00
$ticket->content = $newcontent;
$ticket->editedContent = true;
$ticket->store();
}
2019-07-05 01:22:38 +02:00
Log::createLog('EDIT_COMMENT', $ticketNumber);
2019-06-27 03:04:56 +02:00
Response::respondSuccess();
}
}