opensupports/server/controllers/user/get-users.php

138 lines
3.8 KiB
PHP
Raw Normal View History

2016-11-30 07:33:46 +01:00
<?php
use Respect\Validation\Validator as DataValidator;
2017-04-16 07:35:04 +02:00
/**
2017-04-21 08:09:24 +02:00
* @api {post} /user/get-users Retrieve the information of a list of users.
2017-04-16 07:35:04 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiName Get-users
2017-04-16 07:35:04 +02:00
*
* @apiGroup User
*
2017-04-21 08:09:24 +02:00
* @apiDescription This path retrieves information about a list of users.
2017-04-16 07:35:04 +02:00
*
* @apiPermission Staff level 1
*
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.
* @apiParam {Boolean} desc Parameter to order the user ascending or descending way.
* @apiParam {String} search Key to find some specific 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
* @apiUse USER_SYSTEM_DISABLED
2017-04-16 07:35:04 +02:00
*
* @apiSuccess {Object} data
*
*/
2016-11-30 07:33:46 +01:00
class GetUsersController extends Controller {
const PATH = '/get-users';
const METHOD = 'POST';
2016-11-30 07:33:46 +01:00
public function validations() {
return[
'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() {
if(!Controller::isUserSystemEnabled()) {
throw new Exception(ERRORS::USER_SYSTEM_DISABLED);
}
2016-11-30 07:33:46 +01:00
$userList = $this->getUserList();
$userListArray = [];
foreach ($userList as $user) {
$userListArray[] = [
'id' => $user->id,
'name' => $user->name,
'verified' => !$user->verificationToken,
2016-11-30 07:33:46 +01:00
'tickets' => $user->tickets,
'email' => $user->email,
'signupDate' => $user->signupDate
];
}
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;
}
}