Completed
Branch newinternal (104de7)
by Simon
10:16
created

PageLog::main()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 15
Bugs 1 Features 0
Metric Value
c 15
b 1
f 0
dl 0
loc 48
rs 8.7396
cc 4
eloc 27
nc 8
nop 0
1
<?php
2
3
namespace Waca\Pages;
4
5
use Waca\DataObjects\User;
6
use Waca\Helpers\LogHelper;
7
use Waca\Security\SecurityConfiguration;
8
use Waca\Tasks\InternalPageBase;
9
use Waca\WebRequest;
10
11
class PageLog extends InternalPageBase
12
{
13
	/**
14
	 * Main function for this page, when no specific actions are called.
15
	 */
16
	protected function main()
17
	{
18
		$this->setHtmlTitle('Logs');
19
20
		$filterUser = WebRequest::getString('filterUser');
21
		$filterAction = WebRequest::getString('filterAction');
22
23
		$database = $this->getDatabase();
24
25
		$this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) {
26
			return User::getAllUsernames($database);
27
		});
28
29
		$limit = WebRequest::getInt('limit');
30
		if ($limit === null) {
31
			$limit = 100;
32
		}
33
34
		$page = WebRequest::getInt('page');
35
		if ($page === null) {
36
			$page = 1;
37
		}
38
39
		$offset = ($page - 1) * $limit;
40
41
		list($logs, $count) = LogHelper::getLogs($database, $filterUser, $filterAction, null, null, $limit, $offset);
42
43
		if ($logs === false) {
44
			$this->assign('logs', array());
45
			$this->setTemplate('logs/main.tpl');
46
47
			return;
48
		}
49
50
		list($users, $logData) = LogHelper::prepareLogsForTemplate($logs, $database, $this->getSiteConfiguration());
51
52
		$this->setupPageData($page, $limit, $count);
53
54
		$this->assign("logs", $logData);
55
		$this->assign("users", $users);
56
57
		$this->assign("filterUser", $filterUser);
58
		$this->assign("filterAction", $filterAction);
59
60
		$this->assign('allLogActions', LogHelper::getLogActions($this->getDatabase()));
61
62
		$this->setTemplate("logs/main.tpl");
63
	}
64
65
	/**
66
	 * Sets up the security for this page. If certain actions have different permissions, this should be reflected in
67
	 * the return value from this function.
68
	 *
69
	 * If this page even supports actions, you will need to check the route
70
	 *
71
	 * @return SecurityConfiguration
72
	 * @category Security-Critical
73
	 */
74
	protected function getSecurityConfiguration()
75
	{
76
		return $this->getSecurityManager()->configure()->asInternalPage();
77
	}
78
79
	/**
80
	 * @param int $page
81
	 * @param int $limit
82
	 * @param int $count
83
	 */
84
	protected function setupPageData($page, $limit, $count)
85
	{
86
		// The number of pages on the pager to show. Must be odd
87
		$pageLimit = 9;
88
89
		$pageData = array(
90
			// Can the user go to the previous page?
91
			'canprev'   => $page != 1,
92
			// Can the user go to the next page?
93
			'cannext'   => ($page * $limit) < $count,
94
			// Maximum page number
95
			'maxpage'   => ceil($count / $limit),
96
			// Limit to the number of pages to display
97
			'pagelimit' => $pageLimit,
98
		);
99
100
		// number of pages either side of the current to show
101
		$pageMargin = (($pageLimit - 1) / 2);
102
103
		// Calculate the number of pages either side to show - this is for situations like:
104
		//  [1]  [2] [[3]] [4]  [5]  [6]  [7]  [8]  [9] - where you can't just use the page margin calculated
105
		$pageData['lowpage'] = max(1, $page - $pageMargin);
106
		$pageData['hipage'] = min($pageData['maxpage'], $page + $pageMargin);
107
		$pageCount = ($pageData['hipage'] - $pageData['lowpage']) + 1;
108
109
		if ($pageCount < $pageLimit) {
110
			if ($pageData['lowpage'] == 1 && $pageData['hipage'] < $pageData['maxpage']) {
111
				$pageData['hipage'] = min($pageLimit, $pageData['maxpage']);
112
			}
113
			elseif ($pageData['lowpage'] > 1 && $pageData['hipage'] == $pageData['maxpage']) {
114
				$pageData['lowpage'] = max(1, $pageData['maxpage'] - $pageLimit + 1);
115
			}
116
		}
117
118
		// Put the range of pages into the page data
119
		$pageData['pages'] = range($pageData['lowpage'], $pageData['hipage']);
120
121
		$this->assign("pagedata", $pageData);
122
123
		$this->assign("limit", $limit);
124
		$this->assign("page", $page);
125
	}
126
127
128
}