opensupports/server/controllers/ticket/comment.php

153 lines
4.9 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
* @apiVersion 4.7.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;
2020-04-16 15:07:28 +02:00
private $imagePaths;
public function validations() {
$this->session = Session::getInstance();
return [
'permission' => 'user',
'requestData' => [
'content' => [
'validation' => DataValidator::content(),
'error' => ERRORS::INVALID_CONTENT
],
'ticketNumber' => [
'validation' => DataValidator::validTicketNumber(),
'error' => ERRORS::INVALID_TICKET
]
]
];
}
public function handler() {
$ticketNumber = Controller::request('ticketNumber');
$this->ticket = Ticket::getByTicketNumber($ticketNumber);
$this->content = Controller::request('content', true);
$this->user = Controller::getLoggedUser();
$ticketAuthor = $this->ticket->authorToArray();
$isAuthor = $this->ticket->isAuthor($this->user);
$isOwner = $this->ticket->isOwner($this->user);
$private = Controller::request('private');
if(!$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 && !$private) {
$this->sendMail($ticketAuthor);
}
if($this->ticket->owner && !$isOwner) {
2019-01-19 00:58:30 +01:00
$this->sendMail([
'email' => $this->ticket->owner->email,
'name' => $this->ticket->owner->name,
'staff' => true
]);
}
Log::createLog('COMMENT', $this->ticket->ticketNumber);
Response::respondSuccess();
}
private function storeComment() {
$fileUploader = FileUploader::getInstance();
$fileUploader->setPermission(FileManager::PERMISSION_TICKET, $this->ticket->ticketNumber);
2018-09-20 22:19:47 +02:00
$fileUploader = $this->uploadFile(Controller::isStaffLogged());
2016-09-29 19:34:20 +02:00
$comment = Ticketevent::getEvent(Ticketevent::COMMENT);
$comment->setProperties(array(
2020-04-16 15:52:33 +02:00
'content' => $this->replaceWithImagePaths($this->getImagePaths(), $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 {
$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::isLoginMandatory() && !$isStaff){
$url .= '/check-ticket/' . $this->ticket->ticketNumber;
$url .= '/' . $email;
2018-06-04 23:04:03 +02:00
}
$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,
2020-04-16 15:52:33 +02:00
'content' => $this->replaceWithImagePaths($this->getImagePaths(), $this->content),
2019-07-02 01:38:34 +02:00
'url' => $url
]);
$mailSender->send();
}
2020-04-16 15:52:33 +02:00
private function getImagePaths() {
if(!$this->imagePaths){
$this->imagePaths = $this->uploadImages(Controller::isStaffLogged());
}
return $this->imagePaths;
}
2018-06-04 23:04:03 +02:00
}