2016-11-30 07:33:46 +01:00
|
|
|
<?php
|
|
|
|
use Respect\Validation\Validator as DataValidator;
|
|
|
|
|
2017-04-16 07:35:04 +02:00
|
|
|
/**
|
2017-05-12 06:58:40 +02:00
|
|
|
* @api {post} /user/get-users Get users list
|
2021-10-19 03:06:20 +02:00
|
|
|
* @apiVersion 4.10.0
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiName Get users list
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
|
|
|
* @apiGroup User
|
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiDescription This path retrieves the list of users by page.
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiPermission staff1
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiParam {Number} page Number of pages of users.
|
|
|
|
* @apiParam {String} orderBy Parameter to order the users by tickets or id.
|
2018-09-28 00:46:30 +02:00
|
|
|
* @apiParam {Boolean} desc Parameter to order the users in an ascending or descending way.
|
2017-05-12 06:58:40 +02:00
|
|
|
* @apiParam {String} search Text query to find users.
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
2017-04-21 08:09:24 +02:00
|
|
|
* @apiUse NO_PERMISSION
|
|
|
|
* @apiUse INVALID_PAGE
|
|
|
|
* @apiUse INVALID_ORDER
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
|
|
|
* @apiSuccess {Object} data
|
2017-04-22 03:33:17 +02:00
|
|
|
* @apiSuccess {[User](#api-Data_Structures-ObjectUser)[]} data.users Array of users found
|
|
|
|
* @apiSuccess {Number} data.pages Number of pages found
|
|
|
|
* @apiSuccess {Number} data.page Number of the page
|
|
|
|
* @apiSuccess {String} data.orderBy Indicates if it's ordered by id or quantity of tickets
|
|
|
|
* @apiSuccess {Boolean} data.desc Indicates if it's ordered in decreasing order
|
|
|
|
* @apiSuccess {String} data.search Query of the search
|
2017-04-16 07:35:04 +02:00
|
|
|
*/
|
|
|
|
|
2016-11-30 07:33:46 +01:00
|
|
|
class GetUsersController extends Controller {
|
|
|
|
const PATH = '/get-users';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2016-11-30 07:33:46 +01:00
|
|
|
|
|
|
|
public function validations() {
|
|
|
|
return[
|
2016-12-14 01:10:49 +01:00
|
|
|
'permission' => 'staff_1',
|
2016-11-30 07:33:46 +01:00
|
|
|
'requestData' => [
|
|
|
|
'page' => [
|
|
|
|
'validation' => DataValidator::numeric(),
|
|
|
|
'error' => ERRORS::INVALID_PAGE
|
|
|
|
],
|
|
|
|
'orderBy' => [
|
|
|
|
'validation' => DataValidator::in(['id','tickets']),
|
|
|
|
'error' => ERRORS::INVALID_ORDER
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2018-09-28 00:46:30 +02:00
|
|
|
|
2016-11-30 07:33:46 +01:00
|
|
|
$userList = $this->getUserList();
|
|
|
|
$userListArray = [];
|
|
|
|
|
|
|
|
foreach ($userList as $user) {
|
|
|
|
$userListArray[] = [
|
|
|
|
'id' => $user->id,
|
|
|
|
'name' => $user->name,
|
2017-01-10 17:19:36 +01:00
|
|
|
'verified' => !$user->verificationToken,
|
2016-11-30 07:33:46 +01:00
|
|
|
'tickets' => $user->tickets,
|
|
|
|
'email' => $user->email,
|
2018-09-28 00:46:30 +02:00
|
|
|
'signupDate' => $user->signupDate,
|
|
|
|
'disabled' => !!$user->disabled
|
2016-11-30 07:33:46 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
Response::respondSuccess([
|
|
|
|
'users' => $userListArray,
|
|
|
|
'pages' => $this->getPagesQuantity(),
|
|
|
|
'page' => Controller::request('page'),
|
|
|
|
'orderBy' => Controller::request('orderBy'),
|
|
|
|
'desc' => Controller::request('desc'),
|
|
|
|
'search' => Controller::request('search')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getUserList() {
|
|
|
|
$query = $this->getSearchQuery();
|
|
|
|
|
|
|
|
return User::find($query, [
|
|
|
|
'%' . Controller::request('search') . '%',
|
|
|
|
'%' . Controller::request('search') . '%',
|
|
|
|
Controller::request('search') . '%',
|
|
|
|
Controller::request('search') . '%'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getPagesQuantity() {
|
|
|
|
$query = '';
|
|
|
|
|
|
|
|
if(Controller::request('search')) {
|
|
|
|
$query .= " (name LIKE ? OR email LIKE ? )";
|
|
|
|
}
|
|
|
|
|
|
|
|
$usersQuantity = User::count($query, [
|
|
|
|
'%' . Controller::request('search') . '%',
|
|
|
|
'%' . Controller::request('search') . '%'
|
|
|
|
]);
|
|
|
|
|
|
|
|
return ceil($usersQuantity / 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSearchQuery() {
|
|
|
|
$query = '';
|
|
|
|
|
|
|
|
if(Controller::request('search')) {
|
|
|
|
$query .= " (name LIKE ? OR email LIKE ? )";
|
|
|
|
$query .= " ORDER BY CASE WHEN (name LIKE ? OR email LIKE ?)";
|
|
|
|
$query .= " THEN 1 ELSE 2 END ASC,";
|
|
|
|
} else {
|
|
|
|
$query .= " ORDER BY ";
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= $this->getOrderAndLimit();
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getOrderAndLimit() {
|
|
|
|
$query = '';
|
|
|
|
|
|
|
|
if(Controller::request('orderBy') === 'tickets') {
|
|
|
|
$query .= 'tickets';
|
|
|
|
} else {
|
|
|
|
$query .= 'id';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Controller::request('desc')) {
|
|
|
|
$query .= ' desc';
|
|
|
|
} else {
|
|
|
|
$query .= ' asc';
|
|
|
|
}
|
|
|
|
$query .= " LIMIT 10 OFFSET ". ((Controller::request('page')-1)*10);
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2018-09-28 00:46:30 +02:00
|
|
|
}
|