2016-10-15 08:23:12 +02:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
DataValidator::with('CustomValidations', true);
|
|
|
|
|
|
|
|
class CloseController extends Controller {
|
|
|
|
const PATH = '/close';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2016-10-15 08:23:12 +02:00
|
|
|
|
2016-11-20 23:00:39 +01:00
|
|
|
private $ticket;
|
|
|
|
|
2016-10-15 08:23:12 +02:00
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'user',
|
|
|
|
'requestData' => [
|
|
|
|
'ticketNumber' => [
|
|
|
|
'validation' => DataValidator::validTicketNumber(),
|
|
|
|
'error' => ERRORS::INVALID_TICKET
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2016-11-20 23:00:39 +01:00
|
|
|
$this->ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber'));
|
2016-10-15 08:23:12 +02:00
|
|
|
|
2016-11-20 23:00:39 +01:00
|
|
|
if($this->shouldDenyPermission()) {
|
2016-10-15 08:23:12 +02:00
|
|
|
Response::respondError(ERRORS::NO_PERMISSION);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-20 23:00:39 +01:00
|
|
|
$this->markAsUnread();
|
|
|
|
$this->addCloseEvent();
|
|
|
|
$this->ticket->closed = true;
|
|
|
|
|
|
|
|
$this->ticket->store();
|
2017-03-17 00:00:21 +01:00
|
|
|
|
|
|
|
$this->sendMail();
|
2017-01-05 22:01:56 +01:00
|
|
|
Log::createLog('CLOSE', $this->ticket->ticketNumber);
|
2016-12-29 21:25:45 +01:00
|
|
|
|
2016-11-20 23:00:39 +01:00
|
|
|
Response::respondSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function shouldDenyPermission() {
|
|
|
|
$user = Controller::getLoggedUser();
|
|
|
|
|
|
|
|
return (!Controller::isStaffLogged() && $this->ticket->author->id !== $user->id) ||
|
|
|
|
(Controller::isStaffLogged() && $this->ticket->owner && $this->ticket->owner->id !== $user->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function markAsUnread() {
|
2016-10-21 21:12:17 +02:00
|
|
|
if(Controller::isStaffLogged()) {
|
2016-11-20 23:00:39 +01:00
|
|
|
$this->ticket->unread = true;
|
2016-10-21 21:12:17 +02:00
|
|
|
} else {
|
2016-11-20 23:00:39 +01:00
|
|
|
$this->ticket->unreadStaff = true;
|
2016-10-21 21:12:17 +02:00
|
|
|
}
|
2016-11-20 23:00:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function addCloseEvent() {
|
2016-11-20 02:54:08 +01:00
|
|
|
$event = Ticketevent::getEvent(Ticketevent::CLOSE);
|
|
|
|
$event->setProperties(array(
|
|
|
|
'date' => Date::getCurrentDate()
|
|
|
|
));
|
2016-11-20 23:00:39 +01:00
|
|
|
|
|
|
|
if(Controller::isStaffLogged()) {
|
|
|
|
$event->authorStaff = Controller::getLoggedUser();
|
|
|
|
} else {
|
|
|
|
$event->authorUser = Controller::getLoggedUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->ticket->addEvent($event);
|
2016-10-15 08:23:12 +02:00
|
|
|
}
|
2017-03-17 00:00:21 +01:00
|
|
|
|
|
|
|
private function sendMail() {
|
|
|
|
$mailSender = new MailSender();
|
|
|
|
|
|
|
|
$mailSender->setTemplate(MailTemplate::TICKET_CLOSED, [
|
|
|
|
'to' => $this->ticket->author->email,
|
|
|
|
'name' => $this->ticket->author->name,
|
|
|
|
'ticketNumber' => $this->ticket->ticketNumber,
|
|
|
|
'title' => $this->ticket->title,
|
|
|
|
'url' => Setting::getSetting('url')->getValue()
|
|
|
|
]);
|
|
|
|
|
|
|
|
$mailSender->send();
|
|
|
|
}
|
2016-10-15 08:23:12 +02:00
|
|
|
}
|