2016-11-04 23:10:32 +01:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
|
2017-04-18 06:39:55 +02:00
|
|
|
/**
|
2018-10-27 22:42:45 +02:00
|
|
|
* @api {post} /staff/get-all-tickets Get all tickets according to search
|
2020-06-17 02:26:04 +02:00
|
|
|
* @apiVersion 4.7.0
|
2017-04-18 06:39:55 +02:00
|
|
|
*
|
|
|
|
* @apiName Get all tickets
|
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiGroup Staff
|
2017-04-18 06:39:55 +02:00
|
|
|
*
|
2018-10-27 22:42:45 +02:00
|
|
|
* @apiDescription This path retrieves all tickets according to search and opened/closed filters.
|
2017-04-18 06:39:55 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiPermission staff1
|
2017-04-18 06:39:55 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiParam {Number} page The page number.
|
2018-10-27 22:42:45 +02:00
|
|
|
* @apiParam {String} query Query string to search.
|
2018-10-18 17:02:39 +02:00
|
|
|
* @apiParam {Boolean} closed Include closed tickets.
|
2017-04-18 06:39:55 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiUse NO_PERMISSION
|
|
|
|
* @apiUse INVALID_PAGE
|
2017-04-18 06:39:55 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiSuccess {Object} data Information about a tickets and quantity of pages.
|
|
|
|
* @apiSuccess {[Ticket](#api-Data_Structures-ObjectTicket)[]} data.tickets Array of tickets of the current page.
|
|
|
|
* @apiSuccess {Number} data.pages Quantity of pages.
|
2017-04-18 06:39:55 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-11-04 23:10:32 +01:00
|
|
|
class GetAllTicketsStaffController extends Controller {
|
|
|
|
const PATH = '/get-all-tickets';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2016-11-04 23:10:32 +01:00
|
|
|
|
|
|
|
public function validations() {
|
2018-11-16 19:12:15 +01:00
|
|
|
return [
|
2016-11-04 23:10:32 +01:00
|
|
|
'permission' => 'staff_1',
|
|
|
|
'requestData' => [
|
|
|
|
'page' => [
|
|
|
|
'validation' => DataValidator::numeric(),
|
|
|
|
'error' => ERRORS::INVALID_PAGE
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2018-07-27 02:35:19 +02:00
|
|
|
|
2016-11-04 23:10:32 +01:00
|
|
|
public function handler() {
|
2017-03-29 23:46:26 +02:00
|
|
|
if (Ticket::isTableEmpty()) {
|
|
|
|
Response::respondSuccess([
|
|
|
|
'tickets' => [],
|
|
|
|
'pages' => 0
|
|
|
|
]);
|
|
|
|
return;
|
|
|
|
}
|
2019-12-17 12:35:04 +01:00
|
|
|
|
2016-11-04 23:10:32 +01:00
|
|
|
Response::respondSuccess([
|
2018-11-16 00:33:08 +01:00
|
|
|
'tickets' => $this->getTicketList()->toArray(true),
|
2016-11-04 23:10:32 +01:00
|
|
|
'pages' => $this->getTotalPages()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTicketList() {
|
|
|
|
$page = Controller::request('page');
|
|
|
|
|
2018-10-27 22:42:45 +02:00
|
|
|
$query = $this->getSearchQuery();
|
|
|
|
$query .= $this->getStaffDepartmentsQueryFilter();
|
2018-10-18 17:02:39 +02:00
|
|
|
$query .= $this->getClosedFilter();
|
2018-11-07 14:34:14 +01:00
|
|
|
$query .= "ORDER BY CASE WHEN (title LIKE ?) THEN 1 ELSE 2 END ASC, id DESC LIMIT 10 OFFSET " . (($page-1)*10);
|
2020-02-04 20:22:08 +01:00
|
|
|
|
2018-10-27 22:42:45 +02:00
|
|
|
return Ticket::find($query, [
|
|
|
|
Controller::request('query') . '%',
|
|
|
|
'%' . Controller::request('query') . '%',
|
|
|
|
Controller::request('query') . '%'
|
|
|
|
]);
|
|
|
|
}
|
2016-11-04 23:10:32 +01:00
|
|
|
|
2018-10-27 22:42:45 +02:00
|
|
|
private function getSearchQuery() {
|
|
|
|
$page = Controller::request('page');
|
|
|
|
|
|
|
|
$query = " (title LIKE ? OR title LIKE ?) AND ";
|
|
|
|
|
|
|
|
return $query;
|
2016-11-04 23:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getTotalPages() {
|
2018-11-16 00:33:08 +01:00
|
|
|
$query = $this->getSearchQuery();
|
|
|
|
$query .= $this->getStaffDepartmentsQueryFilter();
|
|
|
|
$query .= $this->getClosedFilter();
|
2016-11-04 23:10:32 +01:00
|
|
|
|
2018-11-16 00:33:08 +01:00
|
|
|
return ceil(Ticket::count($query, [
|
|
|
|
Controller::request('query') . '%',
|
|
|
|
'%' . Controller::request('query') . '%'
|
|
|
|
]) / 10);
|
2016-11-04 23:10:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getStaffDepartmentsQueryFilter() {
|
|
|
|
$user = Controller::getLoggedUser();
|
|
|
|
|
|
|
|
$query = ' (';
|
|
|
|
foreach ($user->sharedDepartmentList as $department) {
|
|
|
|
$query .= 'department_id=' . $department->id . ' OR ';
|
|
|
|
}
|
2018-07-27 02:35:19 +02:00
|
|
|
$query .= 'FALSE) ';
|
2016-11-04 23:10:32 +01:00
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2018-10-18 17:02:39 +02:00
|
|
|
|
|
|
|
private function getClosedFilter() {
|
|
|
|
$closed = Controller::request('closed')*1;
|
|
|
|
if ($closed) {
|
|
|
|
return '';
|
|
|
|
} else {
|
|
|
|
return " AND (closed = '0')";
|
|
|
|
}
|
|
|
|
}
|
2018-07-27 02:35:19 +02:00
|
|
|
}
|