opensupports/server/controllers/ticket/close.php

107 lines
2.8 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/close Close ticket
* @apiVersion 4.0.0
2017-04-17 04:59:11 +02:00
*
* @apiName Close
*
* @apiGroup Ticket
*
* @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
*
* @apiSuccess {Object} data Empty object
2017-04-17 04:59:11 +02:00
*
*/
class CloseController extends Controller {
const PATH = '/close';
const METHOD = 'POST';
private $ticket;
public function validations() {
return [
'permission' => 'user',
'requestData' => [
'ticketNumber' => [
'validation' => DataValidator::validTicketNumber(),
'error' => ERRORS::INVALID_TICKET
]
]
];
}
public function handler() {
$this->ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber'));
if($this->shouldDenyPermission()) {
Response::respondError(ERRORS::NO_PERMISSION);
return;
}
$this->markAsUnread();
$this->addCloseEvent();
$this->ticket->closed = true;
$this->ticket->store();
$this->sendMail();
2017-01-05 22:01:56 +01:00
Log::createLog('CLOSE', $this->ticket->ticketNumber);
2016-12-29 21:25:45 +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()) {
$this->ticket->unread = true;
2016-10-21 21:12:17 +02:00
} else {
$this->ticket->unreadStaff = true;
2016-10-21 21:12:17 +02:00
}
}
private function addCloseEvent() {
$event = Ticketevent::getEvent(Ticketevent::CLOSE);
$event->setProperties(array(
'date' => Date::getCurrentDate()
));
if(Controller::isStaffLogged()) {
$event->authorStaff = Controller::getLoggedUser();
} else {
$event->authorUser = Controller::getLoggedUser();
}
$this->ticket->addEvent($event);
}
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();
}
}