-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIndex.php
More file actions
66 lines (56 loc) · 2.4 KB
/
Copy pathIndex.php
File metadata and controls
66 lines (56 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace BNETDocs\Controllers\User;
use \BNETDocs\Libraries\User\User;
class Index extends \BNETDocs\Controllers\Base
{
public const PAGINATION_LIMIT_DEF = 20; // The default amount of items per page.
public const PAGINATION_LIMIT_MAX = 250; // The most amount of items per page.
public const PAGINATION_LIMIT_MIN = 5; // The least amount of items per page.
/**
* Constructs a Controller, typically to initialize properties.
*/
public function __construct()
{
$this->model = new \BNETDocs\Models\User\Index();
}
/**
* Invoked by the Router class to handle the request.
*
* @param array|null $args The optional route arguments and any captured URI arguments.
* @return boolean Whether the Router should invoke the configured View.
*/
public function invoke(?array $args): bool
{
$q = \BNETDocs\Libraries\Core\Router::query();
$this->model->order = $q['order'] ?? 'registered-desc';
// Translate order value to SQL-compatible properties
switch ($this->model->order)
{
case 'id-asc': $order = ['id', 'ASC']; break;
case 'id-desc': $order = ['id', 'DESC']; break;
case 'username-asc': $order = ['username', 'ASC']; break;
case 'username-desc': $order = ['username', 'DESC']; break;
case 'registered-asc': $order = ['created_datetime', 'ASC']; break;
case 'registered-desc': $order = ['created_datetime', 'DESC']; break;
default: $order = null;
}
// Bounds checking
$this->model->page = (int) ($q['page'] ?? null);
$this->model->limit = (int) ($q['limit'] ?? self::PAGINATION_LIMIT_DEF);
if ($this->model->limit < self::PAGINATION_LIMIT_MIN) $this->model->limit = self::PAGINATION_LIMIT_MIN;
if ($this->model->limit > self::PAGINATION_LIMIT_MAX) $this->model->limit = self::PAGINATION_LIMIT_MAX;
$this->model->pages = ceil(User::getUserCount() / $this->model->limit);
if ($this->model->page < 1) $this->model->page = 1;
if ($this->model->page > $this->model->pages) $this->model->page = $this->model->pages;
// Get all by page
$this->model->users = User::getAllUsers(
$order,
$this->model->limit, // limit per page
$this->model->limit * ($this->model->page - 1) // page offset
);
// Post-filter summary of users
$this->model->sum_users = count($this->model->users);
$this->model->_responseCode = \BNETDocs\Libraries\Core\HttpCode::HTTP_OK;
return true;
}
}