opensupports/server/controllers/ticket/comment.php

171 lines
5.8 KiB
PHP
Raw Normal View History

<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
2017-04-17 04:59:11 +02:00
/**
* @api {post} /ticket/comment Comment ticket
2019-10-08 01:21:43 +02:00
* @apiVersion 4.5.0
2018-06-04 23:04:03 +02:00
*
* @apiName Comment ticket
2017-04-17 04:59:11 +02:00
*
* @apiGroup Ticket
*
* @apiDescription This path comments a ticket.
2017-04-17 04:59:11 +02:00
*
* @apiPermission user
*
2017-04-21 08:09:24 +02:00
* @apiParam {String} content Content of the comment.
* @apiParam {Number} ticketNumber The number of the ticket to comment.
2018-09-28 02:19:34 +02:00
* @apiParam {Boolean} private Indicates if the comment is not shown to users.
2018-09-20 22:19:47 +02:00
* @apiParam {Number} images The number of images in the content
* @apiParam image_i The image file of index `i` (mutiple params accepted)
* @apiParam file The file you with to upload.
2017-04-17 04:59:11 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiUse NO_PERMISSION
* @apiUse INVALID_CONTENT
* @apiUse INVALID_TICKET
2017-06-13 06:03:06 +02:00
* @apiUse INVALID_TOKEN
2018-09-20 22:19:47 +02:00
* @apiUse INVALID_FILE
2017-04-17 04:59:11 +02:00
*
2018-06-04 23:04:03 +02:00
* @apiSuccess {Object} data Empty object
2017-04-17 04:59:11 +02:00
*
*/
class CommentController extends Controller {
const PATH = '/comment';
const METHOD = 'POST';
private $ticket;
private $content;
private $session;
public function validations() {
$this->session = Session::getInstance();
if (Controller::isUserSystemEnabled() || Controller::isStaffLogged()) {
return [
'permission' => 'user',
'requestData' => [
'content' => [
'validation' => DataValidator::length(20, 5000),
'error' => ERRORS::INVALID_CONTENT
],
'ticketNumber' => [
'validation' => DataValidator::validTicketNumber(),
'error' => ERRORS::INVALID_TICKET
]
]
];
} else {
return [
'permission' => 'any',
'requestData' => [
'content' => [
'validation' => DataValidator::length(20, 5000),
'error' => ERRORS::INVALID_CONTENT
],
'ticketNumber' => [
'validation' => DataValidator::equals($this->session->getTicketNumber()),
'error' => ERRORS::INVALID_TICKET
],
'csrf_token' => [
'validation' => DataValidator::equals($this->session->getToken()),
2017-06-13 06:03:06 +02:00
'error' => ERRORS::INVALID_TOKEN
]
]
];
}
}
public function handler() {
$this->requestData();
$this->user = Controller::getLoggedUser();
$ticketAuthor = $this->ticket->authorToArray();
$isAuthor = $this->ticket->isAuthor($this->user) || $this->session->isTicketSession();
$isOwner = $this->ticket->isOwner($this->user);
if(!Controller::isStaffLogged() && Controller::isUserSystemEnabled() && !$isAuthor){
2018-11-20 23:41:00 +01:00
throw new RequestException(ERRORS::NO_PERMISSION);
}
if(!$this->session->isTicketSession() && !$this->user->canManageTicket($this->ticket)) {
2019-09-20 21:58:21 +02:00
throw new RequestException(ERRORS::NO_PERMISSION);
2019-07-05 01:22:38 +02:00
}
$this->storeComment();
2018-06-04 23:04:03 +02:00
if($isAuthor && $this->ticket->owner) {
2019-01-19 00:58:30 +01:00
$this->sendMail([
'email' => $this->ticket->owner->email,
'name' => $this->ticket->owner->name,
'staff' => true
]);
} else if($isOwner && !Controller::request('private')) {
$this->sendMail($ticketAuthor);
}
Log::createLog('COMMENT', $this->ticket->ticketNumber);
Response::respondSuccess();
}
private function requestData() {
$ticketNumber = Controller::request('ticketNumber');
$this->ticket = Ticket::getByTicketNumber($ticketNumber);
$this->content = Controller::request('content', true);
}
private function storeComment() {
$fileUploader = FileUploader::getInstance();
$fileUploader->setPermission(FileManager::PERMISSION_TICKET, $this->ticket->ticketNumber);
2018-09-20 22:19:47 +02:00
$imagePaths = $this->uploadImages(Controller::isStaffLogged());
$fileUploader = $this->uploadFile(Controller::isStaffLogged());
2016-09-29 19:34:20 +02:00
$comment = Ticketevent::getEvent(Ticketevent::COMMENT);
$comment->setProperties(array(
'content' => $this->replaceWithImagePaths($imagePaths, $this->content),
'file' => ($fileUploader instanceof FileUploader) ? $fileUploader->getFileName() : null,
2018-09-28 02:19:34 +02:00
'date' => Date::getCurrentDate(),
'private' => (Controller::isStaffLogged() && Controller::request('private')) ? 1 : 0
));
2016-09-29 19:34:20 +02:00
if(Controller::isStaffLogged()) {
$this->ticket->unread = !$this->ticket->isAuthor($this->user);
$this->ticket->unreadStaff = !$this->ticket->isOwner($this->user);
$comment->authorStaff = $this->user;
} else if(Controller::isUserSystemEnabled()) {
2016-10-21 21:12:17 +02:00
$this->ticket->unreadStaff = true;
$comment->authorUser = $this->user;
2016-09-29 19:34:20 +02:00
}
$this->ticket->addEvent($comment);
$this->ticket->store();
}
private function sendMail($recipient) {
$mailSender = MailSender::getInstance();
$email = $recipient['email'];
$name = $recipient['name'];
2018-10-06 04:54:49 +02:00
$isStaff = array_key_exists('staff', $recipient) && $recipient['staff'];
2017-06-25 11:07:14 +02:00
2018-06-04 23:04:03 +02:00
$url = Setting::getSetting('url')->getValue();
if(!Controller::isUserSystemEnabled() && !$isStaff) {
2018-06-04 23:04:03 +02:00
$url .= '/check-ticket/' . $this->ticket->ticketNumber;
$url .= '/' . $email;
}
$mailSender->setTemplate(MailTemplate::TICKET_RESPONDED, [
2019-07-02 01:38:34 +02:00
'to' => $email,
'name' => $name,
'title' => $this->ticket->title,
'ticketNumber' => $this->ticket->ticketNumber,
'content' => $this->content,
'url' => $url
]);
$mailSender->send();
}
2018-06-04 23:04:03 +02:00
}