2016-06-30 23:03:17 +02:00
|
|
|
<?php
|
2016-08-04 20:18:29 +02:00
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
DataValidator::with('CustomValidations', true);
|
2016-06-30 23:03:17 +02:00
|
|
|
|
2017-04-17 04:59:11 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +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
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiName Comment ticket
|
2017-04-17 04:59:11 +02:00
|
|
|
*
|
|
|
|
* @apiGroup Ticket
|
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-06-30 23:03:17 +02:00
|
|
|
class CommentController extends Controller {
|
|
|
|
const PATH = '/comment';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2016-06-30 23:03:17 +02:00
|
|
|
|
2016-08-04 20:18:29 +02:00
|
|
|
private $ticket;
|
2016-06-30 23:03:17 +02:00
|
|
|
private $content;
|
|
|
|
|
2016-07-08 09:46:28 +02:00
|
|
|
public function validations() {
|
2017-03-04 01:39:59 +01:00
|
|
|
$session = Session::getInstance();
|
2017-01-21 05:17:28 +01:00
|
|
|
|
2017-03-04 01:39:59 +01:00
|
|
|
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
|
|
|
|
]
|
|
|
|
]
|
2017-01-21 05:17:28 +01:00
|
|
|
];
|
2017-03-04 01:39:59 +01:00
|
|
|
} 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
|
2017-03-04 01:39:59 +01:00
|
|
|
]
|
|
|
|
]
|
2017-01-16 20:07:53 +01:00
|
|
|
];
|
|
|
|
}
|
2016-07-08 09:46:28 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 23:03:17 +02:00
|
|
|
public function handler() {
|
2016-07-04 02:32:34 +02:00
|
|
|
$this->requestData();
|
2018-07-17 06:17:49 +02:00
|
|
|
$ticketAuthor = $this->ticket->authorToArray();
|
|
|
|
$isAuthor = $this->ticket->isAuthor(Controller::getLoggedUser());
|
|
|
|
$isOwner = $this->ticket->isOwner(Controller::getLoggedUser());
|
2016-06-30 23:03:17 +02:00
|
|
|
|
2018-07-17 06:17:49 +02:00
|
|
|
if((Controller::isUserSystemEnabled() || Controller::isStaffLogged()) && !$isOwner && !$isAuthor) {
|
|
|
|
throw new Exception(ERRORS::NO_PERMISSION);
|
|
|
|
}
|
2017-03-17 00:00:21 +01:00
|
|
|
|
2018-07-17 06:17:49 +02:00
|
|
|
$this->storeComment();
|
2018-06-04 23:04:03 +02:00
|
|
|
|
2018-07-17 06:17:49 +02:00
|
|
|
if($isAuthor && $this->ticket->owner) {
|
|
|
|
$this->sendMail([
|
|
|
|
'email' => $this->ticket->owner->email,
|
|
|
|
'name' => $this->ticket->owner->name,
|
|
|
|
'staff' => true
|
|
|
|
]);
|
2016-08-04 20:18:29 +02:00
|
|
|
} else {
|
2018-07-17 06:17:49 +02:00
|
|
|
$this->sendMail($ticketAuthor);
|
2016-08-04 20:18:29 +02:00
|
|
|
}
|
2018-07-17 06:17:49 +02:00
|
|
|
|
|
|
|
Log::createLog('COMMENT', $this->ticket->ticketNumber);
|
|
|
|
|
|
|
|
Response::respondSuccess();
|
2016-06-30 23:03:17 +02:00
|
|
|
}
|
|
|
|
|
2016-07-04 02:32:34 +02:00
|
|
|
private function requestData() {
|
2016-09-09 05:38:58 +02:00
|
|
|
$ticketNumber = Controller::request('ticketNumber');
|
|
|
|
$this->ticket = Ticket::getByTicketNumber($ticketNumber);
|
2017-03-02 06:56:42 +01:00
|
|
|
$this->content = Controller::request('content', true);
|
2016-07-04 02:32:34 +02:00
|
|
|
}
|
2016-06-30 23:03:17 +02:00
|
|
|
|
2016-07-04 02:32:34 +02:00
|
|
|
private function storeComment() {
|
2018-09-14 06:14:15 +02:00
|
|
|
$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());
|
2017-01-16 22:04:26 +01:00
|
|
|
|
2016-09-29 19:34:20 +02:00
|
|
|
$comment = Ticketevent::getEvent(Ticketevent::COMMENT);
|
2016-06-30 23:03:17 +02:00
|
|
|
$comment->setProperties(array(
|
2018-09-14 06:14:15 +02:00
|
|
|
'content' => $this->replaceWithImagePaths($imagePaths, $this->content),
|
2017-01-16 22:04:26 +01:00
|
|
|
'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-06-30 23:03:17 +02:00
|
|
|
));
|
|
|
|
|
2016-09-29 19:34:20 +02:00
|
|
|
if(Controller::isStaffLogged()) {
|
2018-07-17 07:04:00 +02:00
|
|
|
$this->ticket->unread = !$this->ticket->isAuthor(Controller::getLoggedUser());
|
|
|
|
$this->ticket->unreadStaff = !$this->ticket->isOwner(Controller::getLoggedUser());
|
2016-11-20 23:00:39 +01:00
|
|
|
$comment->authorStaff = Controller::getLoggedUser();
|
2017-01-16 20:07:53 +01:00
|
|
|
} else if(Controller::isUserSystemEnabled()) {
|
2016-10-21 21:12:17 +02:00
|
|
|
$this->ticket->unreadStaff = true;
|
2016-11-20 23:00:39 +01:00
|
|
|
$comment->authorUser = Controller::getLoggedUser();
|
2016-09-29 19:34:20 +02:00
|
|
|
}
|
2017-03-04 01:39:59 +01:00
|
|
|
|
2016-09-29 19:46:31 +02:00
|
|
|
$this->ticket->addEvent($comment);
|
2016-08-04 20:18:29 +02:00
|
|
|
$this->ticket->store();
|
2016-06-30 23:03:17 +02:00
|
|
|
}
|
2017-03-17 00:00:21 +01:00
|
|
|
|
2018-07-17 06:17:49 +02:00
|
|
|
private function sendMail($recipient) {
|
2017-06-05 16:41:52 +02:00
|
|
|
$mailSender = MailSender::getInstance();
|
2017-03-17 00:00:21 +01:00
|
|
|
|
2018-07-17 06:17:49 +02:00
|
|
|
$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();
|
|
|
|
|
2018-07-17 06:17:49 +02:00
|
|
|
if(!Controller::isUserSystemEnabled() && !$isStaff) {
|
2018-06-04 23:04:03 +02:00
|
|
|
$url .= '/check-ticket/' . $this->ticket->ticketNumber;
|
|
|
|
$url .= '/' . $email;
|
|
|
|
}
|
|
|
|
|
2017-03-17 00:00:21 +01:00
|
|
|
$mailSender->setTemplate(MailTemplate::TICKET_RESPONDED, [
|
2017-06-25 11:07:14 +02:00
|
|
|
'to' => $email,
|
|
|
|
'name' => $name,
|
2017-03-17 00:00:21 +01:00
|
|
|
'title' => $this->ticket->title,
|
2018-06-04 23:04:03 +02:00
|
|
|
'ticketNumber' => $this->ticket->ticketNumber,
|
|
|
|
'url' => $url
|
2017-03-17 00:00:21 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
$mailSender->send();
|
|
|
|
}
|
2018-06-04 23:04:03 +02:00
|
|
|
}
|