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
|
|
|
|
|
|
|
class CommentController extends Controller {
|
|
|
|
const PATH = '/comment';
|
|
|
|
|
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() {
|
|
|
|
return [
|
2016-08-04 20:18:29 +02:00
|
|
|
'permission' => 'user',
|
|
|
|
'requestData' => [
|
|
|
|
'content' => [
|
2016-12-21 21:07:34 +01:00
|
|
|
'validation' => DataValidator::length(20, 5000),
|
2016-08-04 20:18:29 +02:00
|
|
|
'error' => ERRORS::INVALID_CONTENT
|
|
|
|
],
|
2016-09-09 05:38:58 +02:00
|
|
|
'ticketNumber' => [
|
|
|
|
'validation' => DataValidator::validTicketNumber(),
|
2016-08-04 20:18:29 +02:00
|
|
|
'error' => ERRORS::INVALID_TICKET
|
|
|
|
]
|
|
|
|
]
|
2016-07-08 09:46:28 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:03:17 +02:00
|
|
|
public function handler() {
|
2016-08-04 20:18:29 +02:00
|
|
|
$session = Session::getInstance();
|
2016-07-04 02:32:34 +02:00
|
|
|
$this->requestData();
|
2016-06-30 23:03:17 +02:00
|
|
|
|
2016-08-04 20:18:29 +02:00
|
|
|
if ($session->isLoggedWithId($this->ticket->author->id) || Controller::isStaffLogged()) {
|
|
|
|
$this->storeComment();
|
2016-12-29 21:25:45 +01:00
|
|
|
|
|
|
|
Log::createLog('COMMENT_TICKET', $this->ticket);
|
|
|
|
|
2016-08-04 20:18:29 +02:00
|
|
|
Response::respondSuccess();
|
|
|
|
} else {
|
|
|
|
Response::respondError(ERRORS::NO_PERMISSION);
|
|
|
|
}
|
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');
|
2016-08-04 20:18:29 +02:00
|
|
|
|
2016-09-09 05:38:58 +02:00
|
|
|
$this->ticket = Ticket::getByTicketNumber($ticketNumber);
|
2016-06-30 23:03:17 +02:00
|
|
|
$this->content = Controller::request('content');
|
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() {
|
2016-09-29 19:34:20 +02:00
|
|
|
$comment = Ticketevent::getEvent(Ticketevent::COMMENT);
|
2016-06-30 23:03:17 +02:00
|
|
|
$comment->setProperties(array(
|
2016-08-04 20:18:29 +02:00
|
|
|
'content' => $this->content,
|
|
|
|
'date' => Date::getCurrentDate()
|
2016-06-30 23:03:17 +02:00
|
|
|
));
|
|
|
|
|
2016-09-29 19:34:20 +02:00
|
|
|
if(Controller::isStaffLogged()) {
|
2016-10-21 21:12:17 +02:00
|
|
|
$this->ticket->unread = true;
|
2016-11-20 23:00:39 +01:00
|
|
|
$comment->authorStaff = Controller::getLoggedUser();
|
2016-09-29 19:34:20 +02:00
|
|
|
} else {
|
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
|
|
|
}
|
2016-11-20 23:00:39 +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
|
|
|
}
|
|
|
|
}
|