2016-10-15 08:23:12 +02:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
DataValidator::with('CustomValidations', true);
|
|
|
|
|
2017-04-17 04:59:11 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +02:00
|
|
|
* @api {post} /ticket/close Close ticket
|
2021-10-19 03:06:20 +02:00
|
|
|
* @apiVersion 4.10.0
|
2017-04-17 04:59:11 +02:00
|
|
|
*
|
|
|
|
* @apiName Close
|
|
|
|
*
|
|
|
|
* @apiGroup Ticket
|
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiDescription This path closes a ticket.
|
2017-04-17 04:59:11 +02:00
|
|
|
*
|
|
|
|
* @apiPermission user
|
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiParam {Number} ticketNumber The number of a ticket.
|
2017-04-17 04:59:11 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiUse NO_PERMISSION
|
|
|
|
* @apiUse INVALID_TICKET
|
2017-06-12 12:22:31 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiSuccess {Object} data Empty object
|
2017-04-17 04:59:11 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-10-15 08:23:12 +02:00
|
|
|
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() {
|
2017-06-01 12:14:23 +02:00
|
|
|
$session = Session::getInstance();
|
|
|
|
|
|
|
|
return [
|
2020-05-13 00:22:51 +02:00
|
|
|
'permission' => 'user',
|
|
|
|
'requestData' => [
|
|
|
|
'ticketNumber' => [
|
|
|
|
'validation' => DataValidator::validTicketNumber(),
|
|
|
|
'error' => ERRORS::INVALID_TICKET
|
|
|
|
]
|
2017-06-01 12:14:23 +02:00
|
|
|
]
|
|
|
|
];
|
2016-10-15 08:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2016-11-20 23:00:39 +01:00
|
|
|
$this->ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber'));
|
2019-07-12 00:42:09 +02:00
|
|
|
$user = Controller::getLoggedUser();
|
2016-10-15 08:23:12 +02:00
|
|
|
|
2020-05-13 00:22:51 +02:00
|
|
|
if(!$user->canManageTicket($this->ticket)){
|
2019-07-12 00:42:09 +02:00
|
|
|
throw new RequestException(ERRORS::NO_PERMISSION);
|
2019-07-05 01:22:38 +02:00
|
|
|
}
|
|
|
|
|
2016-11-20 23:00:39 +01:00
|
|
|
$this->markAsUnread();
|
|
|
|
$this->addCloseEvent();
|
|
|
|
$this->ticket->closed = true;
|
2020-12-27 20:22:55 +01:00
|
|
|
if (!$this->ticket->first_closed_at) {
|
|
|
|
$this->ticket->first_closed_at = Date::getCurrentDate();
|
|
|
|
}
|
|
|
|
$this->ticket->last_closed_at = Date::getCurrentDate();
|
2016-11-20 23:00:39 +01:00
|
|
|
|
|
|
|
$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);
|
2017-06-12 12:22:31 +02:00
|
|
|
|
2016-11-20 23:00:39 +01:00
|
|
|
Response::respondSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
private function markAsUnread() {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2017-06-05 16:41:52 +02:00
|
|
|
$mailSender = MailSender::getInstance();
|
2017-03-17 00:00:21 +01:00
|
|
|
|
|
|
|
$mailSender->setTemplate(MailTemplate::TICKET_CLOSED, [
|
2017-06-01 12:14:23 +02:00
|
|
|
'to' => ($this->ticket->author) ? $this->ticket->author->email : $this->ticket->authorEmail,
|
|
|
|
'name' => ($this->ticket->author) ? $this->ticket->author->name : $this->ticket->authorName,
|
2017-03-17 00:00:21 +01:00
|
|
|
'ticketNumber' => $this->ticket->ticketNumber,
|
|
|
|
'title' => $this->ticket->title,
|
|
|
|
'url' => Setting::getSetting('url')->getValue()
|
|
|
|
]);
|
|
|
|
|
|
|
|
$mailSender->send();
|
|
|
|
}
|
2017-06-01 12:14:23 +02:00
|
|
|
}
|