2016-11-04 23:10:32 +01:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
|
2017-04-18 06:39:55 +02:00
|
|
|
/**
|
|
|
|
* @api {post} /staff/search-tickets Search some tickets.
|
|
|
|
*
|
|
|
|
* @apiName Search tickets
|
|
|
|
*
|
|
|
|
* @apiGroup staff
|
|
|
|
*
|
|
|
|
* @apiDescription This path search some tickets.
|
|
|
|
*
|
|
|
|
* @apiPermission Staff level 1
|
|
|
|
*
|
|
|
|
* @apiParam {String} query key to search.
|
|
|
|
*
|
|
|
|
* @apiParam {number} page The number of page.
|
|
|
|
*
|
|
|
|
* @apiError {String} message
|
|
|
|
*
|
|
|
|
* @apiSuccess {Object} data
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-11-04 23:10:32 +01:00
|
|
|
class SearchTicketStaffController extends Controller {
|
|
|
|
const PATH = '/search-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' => [
|
|
|
|
'query' => [
|
2017-03-02 04:00:45 +01:00
|
|
|
'validation' => DataValidator::length(1),
|
2016-11-04 23:10:32 +01:00
|
|
|
'error' => ERRORS::INVALID_QUERY
|
|
|
|
],
|
|
|
|
'page' => [
|
|
|
|
'validation' => DataValidator::numeric(),
|
|
|
|
'error' => ERRORS::INVALID_PAGE
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
|
|
|
Response::respondSuccess([
|
|
|
|
'tickets' => $this->getTicketList()->toArray(),
|
|
|
|
'pages' => $this->getTotalPages()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTicketList() {
|
|
|
|
$query = $this->getSearchQuery();
|
|
|
|
|
|
|
|
return Ticket::find($query, [
|
|
|
|
Controller::request('query') . '%',
|
|
|
|
'%' . Controller::request('query') . '%',
|
|
|
|
Controller::request('query') . '%'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSearchQuery() {
|
|
|
|
$page = Controller::request('page');
|
|
|
|
|
|
|
|
$query = " (title LIKE ? OR title LIKE ?) AND ";
|
|
|
|
$query .= $this->getStaffDepartmentsQueryFilter();
|
|
|
|
$query .= "ORDER BY CASE WHEN (title LIKE ?) THEN 1 ELSE 2 END ASC LIMIT 10 OFFSET " . (($page-1)*10);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTotalPages() {
|
|
|
|
$query = " (title LIKE ? OR title LIKE ?) AND ";
|
|
|
|
$query .= $this->getStaffDepartmentsQueryFilter();
|
|
|
|
|
|
|
|
$ticketQuantity = Ticket::count($query, [
|
|
|
|
Controller::request('query') . '%',
|
|
|
|
'%' . Controller::request('query') . '%'
|
|
|
|
]);
|
|
|
|
|
|
|
|
return ceil($ticketQuantity / 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;
|
|
|
|
}
|
|
|
|
}
|