opensupports/server/controllers/ticket/comment.php

164 lines
5.6 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
2018-09-20 22:19:47 +02:00
* @apiVersion 4.3.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;
public function validations() {
$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($session->getTicketNumber()),
'error' => ERRORS::INVALID_TICKET
],
'csrf_token' => [
'validation' => DataValidator::equals($session->getToken()),
2017-06-13 06:03:06 +02:00
'error' => ERRORS::INVALID_TOKEN
]
]
];
}
}
public function handler() {
$this->requestData();
$ticketAuthor = $this->ticket->authorToArray();
$isAuthor = $this->ticket->isAuthor(Controller::getLoggedUser());
$isOwner = $this->ticket->isOwner(Controller::getLoggedUser());
if((Controller::isUserSystemEnabled() || Controller::isStaffLogged()) && !$isOwner && !$isAuthor) {
throw new Exception(ERRORS::NO_PERMISSION);
}
$this->storeComment();
2018-06-04 23:04:03 +02:00
if($isAuthor && $this->ticket->owner) {
$this->sendMail([
'email' => $this->ticket->owner->email,
'name' => $this->ticket->owner->name,
'staff' => true
]);
} else {
$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(Controller::getLoggedUser());
$this->ticket->unreadStaff = !$this->ticket->isOwner(Controller::getLoggedUser());
$comment->authorStaff = Controller::getLoggedUser();
} else if(Controller::isUserSystemEnabled()) {
2016-10-21 21:12:17 +02:00
$this->ticket->unreadStaff = true;
$comment->authorUser = Controller::getLoggedUser();
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, [
2017-06-25 11:07:14 +02:00
'to' => $email,
'name' => $name,
'title' => $this->ticket->title,
2018-06-04 23:04:03 +02:00
'ticketNumber' => $this->ticket->ticketNumber,
'url' => $url
]);
$mailSender->send();
}
2018-06-04 23:04:03 +02:00
}