2016-11-04 23:10:32 +01:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
|
|
|
|
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() {
|
|
|
|
return[
|
|
|
|
'permission' => 'staff_1',
|
|
|
|
'requestData' => [
|
|
|
|
'page' => [
|
|
|
|
'validation' => DataValidator::numeric(),
|
|
|
|
'error' => ERRORS::INVALID_PAGE
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2017-03-29 23:46:26 +02:00
|
|
|
if (Ticket::isTableEmpty()) {
|
|
|
|
Response::respondSuccess([
|
|
|
|
'tickets' => [],
|
|
|
|
'pages' => 0
|
|
|
|
]);
|
|
|
|
return;
|
|
|
|
}
|
2016-11-04 23:10:32 +01:00
|
|
|
|
|
|
|
Response::respondSuccess([
|
|
|
|
'tickets' => $this->getTicketList()->toArray(),
|
|
|
|
'pages' => $this->getTotalPages()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTicketList() {
|
|
|
|
$page = Controller::request('page');
|
|
|
|
|
|
|
|
$query = $this->getStaffDepartmentsQueryFilter();
|
|
|
|
$query .= 'ORDER BY id DESC LIMIT 10 OFFSET ' . (($page-1)*10);
|
|
|
|
|
|
|
|
return Ticket::find($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTotalPages() {
|
|
|
|
$query = $this->getStaffDepartmentsQueryFilter();
|
|
|
|
|
|
|
|
return ceil(Ticket::count($query) / 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getStaffDepartmentsQueryFilter() {
|
|
|
|
$user = Controller::getLoggedUser();
|
|
|
|
|
|
|
|
$query = ' (';
|
|
|
|
foreach ($user->sharedDepartmentList as $department) {
|
|
|
|
$query .= 'department_id=' . $department->id . ' OR ';
|
|
|
|
}
|
|
|
|
$query = substr($query,0,-3);
|
|
|
|
$query .= ') ';
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
}
|