opensupports/server/controllers/ticket/close.php

104 lines
2.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/close Close ticket
* @apiVersion 4.10.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
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
*
*/
class CloseController extends Controller {
const PATH = '/close';
const METHOD = 'POST';
private $ticket;
public function validations() {
$session = Session::getInstance();
return [
'permission' => 'user',
'requestData' => [
'ticketNumber' => [
'validation' => DataValidator::validTicketNumber(),
'error' => ERRORS::INVALID_TICKET
]
]
];
}
public function handler() {
$this->ticket = Ticket::getByTicketNumber(Controller::request('ticketNumber'));
$user = Controller::getLoggedUser();
if(!$user->canManageTicket($this->ticket)){
throw new RequestException(ERRORS::NO_PERMISSION);
2019-07-05 01:22:38 +02:00
}
$this->markAsUnread();
$this->addCloseEvent();
$this->ticket->closed = true;
Add statistics (#904) * Adds first_closed_at and last_closed_at to Ticket * Fixes ticket isClosed function * Adds reopened column to Ticket table * Adds stats path * Adds stats for instant tickets * Adds basic connection with frontend * Creates cards to display ticketData * Adds tooltips with descriptions and i18n * Adds date range filter to backend * Adds DateRange filter on frontend * Documents and better structures code * Makes $dateRange local * Adds departments filter on backend * Adds stats path to menu * Adds first UI for departments filter in stats * Implements departments filter on frontend * Improves styling by adding bootstrap rows * Improves structure of dynamics queries * Adds tags filter on backend * Adding TagFilter for statistics WIP * Adds missing `id` to propTypes TagSelector * Removes console.warns * Adapts form to pass tagnames as value as FormField * Sends tags to API too * Makes tag-selector change form with tagnames only * Fixes tag-selector from ticket-viewer * Removes console.warn * Removes logs * Adds owner filter on backend * Connects owners frontend with backend for stats * Style changes for date-selector * Adds tickets by hours stat to /system/stats path * Adds chart for tickets created on each hour * Adds better wrap for ticketdata cards * Adds getAverageFirstReply to backend stats path * Adds getNumberOfCreatedTicketsByWeekday to backend * Adds created tickets by weekday chart * Disables clicking on the legend to toggle data * Adds base functions for efficiency stats * Adds getAverageFirstClosed to backend stats * Adds getAverageLastClosed to backend stats * Adds table, filters, and groupBy variables to queries * Adds response structure with mocks * Adds totalOwners and totalDepartments * Adds SQL queries to get department/staff hops of a ticket * Changes incorrect name * Rolls back addition of near useless function * Improves tag array management from redux store * Fix bug in autocomplete filters. * Sets default date range to current month. improves date.js. * Adds i18n * wip * Add media query in admin-panel-stats.scss * Updates date handling in search-ticket-utils * Makes tooltip open on hover of the entire block * Fix date range mobile style. * Add Loading * Add submit button and clear button in admin panel stats * Adds tests for stats and comments old ones * Add test for stats after a ticket has been created * Makes default dateRange for stats go to the end of the day * Factors out function to create ticket and adds test * Adds instant ticket test * Adds reopened test * Commit to save technique to test created_by_hour but is prohibitively slow. * Updates test of created_by_hour to be more lightweight * Adds test for created_by_weekday * Fixes default date and renames a function * Fixes hover bug by extracting card-stat to its own component * Fix drawbacks with previous change in style - mobile Co-authored-by: LautaroCesso <lautaro_cesso@hotmail.com> * Set up 0 as a minimum number for bar chart * Moves styles from stats cards to the component * Removes old /system/get-stats path * Changes name from /system/stats to /system/get-stats * Restore getCurrentDate in date transformer Co-authored-by: LautaroCesso <lautaro_cesso@hotmail.com> Co-authored-by: Ivan Diaz <ivan@opensupports.com>
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();
$this->ticket->store();
$this->sendMail();
2017-01-05 22:01:56 +01:00
Log::createLog('CLOSE', $this->ticket->ticketNumber);
2017-06-12 12:22:31 +02:00
Response::respondSuccess();
}
private function markAsUnread() {
$this->ticket->unread = !$this->ticket->isAuthor(Controller::getLoggedUser());
$this->ticket->unreadStaff = !$this->ticket->isOwner(Controller::getLoggedUser());
}
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 = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::TICKET_CLOSED, [
'to' => ($this->ticket->author) ? $this->ticket->author->email : $this->ticket->authorEmail,
'name' => ($this->ticket->author) ? $this->ticket->author->name : $this->ticket->authorName,
'ticketNumber' => $this->ticket->ticketNumber,
'title' => $this->ticket->title,
'url' => Setting::getSetting('url')->getValue()
]);
$mailSender->send();
}
}