-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathIndex.php
More file actions
64 lines (52 loc) · 2.29 KB
/
Copy pathIndex.php
File metadata and controls
64 lines (52 loc) · 2.29 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
<?php
namespace BNETDocs\Controllers\EventLog;
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\EventLog\Event;
use \BNETDocs\Models\EventLog\Index as IndexModel;
class Index extends \BNETDocs\Controllers\Base
{
const PAGINATION_LIMIT_DEF = 15; // The default amount of items per page.
const PAGINATION_LIMIT_MIN = 5; // The least amount of items per page.
const PAGINATION_LIMIT_MAX = 250; // The most amount of items per page.
public function __construct()
{
$this->model = new IndexModel();
}
public function invoke(?array $args): bool
{
$this->model->acl_allowed = $this->model->active_user
&& $this->model->active_user->getOption(\BNETDocs\Libraries\User\User::OPTION_ACL_EVENT_LOG_VIEW);
if (!$this->model->acl_allowed)
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
$this->model->error = $this->model->active_user ? IndexModel::ERROR_ACL_NOT_SET : IndexModel::ERROR_NOT_LOGGED_IN;
return true;
}
$q = \BNETDocs\Libraries\Core\Router::query();
$this->model->order = $q['order'] ?? 'datetime-desc';
switch ($this->model->order)
{
case 'id-asc': $order = ['id', false]; break;
case 'id-desc': $order = ['id', true]; break;
case 'datetime-asc': $order = ['event_datetime', false]; break;
case 'datetime-desc': $order = ['event_datetime', true]; break;
default: $order = null;
}
$this->model->page = (isset($q['page']) ? (int) $q['page'] : 0);
$this->model->limit = (isset($q['limit']) ? (int) $q['limit'] : self::PAGINATION_LIMIT_DEF);
if ($this->model->page < 1) $this->model->page = 1;
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(Event::countAll() / $this->model->limit);
if ($this->model->page > $this->model->pages) $this->model->page = $this->model->pages;
$this->model->events = Event::allocateAll(
null,
$order[0], $order[1],
$this->model->limit,
$this->model->limit * ( $this->model->page - 1 )
);
$this->model->sum_events = count($this->model->events);
$this->model->_responseCode = HttpCode::HTTP_OK;
return true;
}
}