Completed
Push — master ( 0947c3...b8ee92 )
by Henry
11:06 queued 03:27
created

UserTable::_renderSession()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 9
cp 1
rs 9.6333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
namespace Redaxscript\Admin\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Dater;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
9
/**
10
 * children class to create the admin user table
11
 *
12
 * @since 4.0.0
13
 *
14
 * @package Redaxscript
15
 * @category View
16
 * @author Henry Ruhs
17
 */
18
19
class UserTable extends ViewAbstract
20
{
21
	/**
22
	 * render the view
23
	 *
24
	 * @since 4.0.0
25
	 *
26
	 * @return string
27
	 */
28 2
29
	public function render() : string
30 2
	{
31 2
		$output = Module\Hook::trigger('adminUserTableStart');
32 2
		$parameterRoute = $this->_registry->get('parameterRoute');
33
		$usersNew = $this->_registry->get('usersNew');
34
35
		/* html element */
36 2
37
		$element = new Html\Element();
38 2
		$titleElement = $element
39 2
			->copy()
40
			->init('h2',
41 2
			[
42
				'class' => 'rs-admin-title-content',
43 2
			])
44
			->text($this->_language->get('users'));
45 2
		$linkElement = $element
46 2
			->copy()
47
			->init('a',
48 2
			[
49 2
				'class' => 'rs-admin-button-default rs-admin-button-create',
50
				'href' => $parameterRoute . 'admin/new/users'
51 2
			])
52
			->text($this->_language->get('user_new'));
53
54
		/* collect output */
55 2
56 2
		$output .= $titleElement;
57
		if ($usersNew)
58 1
		{
59
			$output .= $linkElement;
60 2
		}
61 2
		$output .= $this->_renderTable();
62 2
		$output .= Module\Hook::trigger('adminUserTableEnd');
63
		return $output;
64
	}
65
66
	/**
67
	 * render the table
68
	 *
69
	 * @since 4.0.0
70
	 *
71
	 * @return string
72
	 */
73 2
74
	protected function _renderTable() : string
75 2
	{
76 2
		$output = null;
77 2
		$outputHead = null;
78 2
		$outputBody = null;
79
		$outputFoot = null;
80
		$tableArray =
81 2
		[
82 2
			'name' => $this->_language->get('name'),
83 2
			'user' => $this->_language->get('user'),
84
			'session' => $this->_language->get('session'),
85 2
			'groups' => $this->_language->get('groups')
86 2
		];
87 2
		$adminControl = new Helper\Control($this->_registry, $this->_language);
88 2
		$userModel = new Admin\Model\User();
89
		$users = $userModel->getAll();
90
		$usersTotal = $users->count();
91
92 2
		/* html element */
93
94 2
		$element = new Html\Element();
95 2
		$wrapperElement = $element
96
			->copy()
97 2
			->init('div',
98
			[
99
				'class' => 'rs-admin-wrapper-table'
100 2
			]);
101 2
		$tableElement = $element
102
			->copy()
103 2
			->init('table',
104
			[
105 2
				'class' => 'rs-admin-table-default'
106 2
			]);
107 2
		$theadElement = $element->copy()->init('thead');
108 2
		$tbodyElement = $element->copy()->init('tbody');
109 2
		$tfootElement = $element->copy()->init('tfoot');
110 2
		$trElement = $element->copy()->init('tr');
111
		$thElement = $element->copy()->init('th');
112
		$tdElement = $element->copy()->init('td');
113
114 2
		/* process table */
115
116 2
		foreach ($tableArray as $key => $value)
117 2
		{
118
			$outputHead .= $thElement->copy()->text($value);
119
			$outputFoot .= $tdElement->copy()->text($value);
120
		}
121
122 2
		/* process categories */
123
124 2
		if ($usersTotal)
125
		{
126 2
			foreach ($users as $key => $value)
127
			{
128 2
				$outputBody .= $trElement
129 2
					->copy()
130 2
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
131 2
					->html(
132 2
						$tdElement->copy()->html($value->name . $adminControl->render('users', $value->id, $value->alias, $value->status)) .
133 2
						$tdElement->copy()->text($value->user) .
134
						$tdElement->copy()->text($this->_renderSession($value->last)) .
135
						$tdElement->copy()->html($this->_renderGroup($value->groups))
136
					);
137
			}
138
		}
139
		else
140
		{
141
			$outputBody .= $trElement
142
				->copy()
143
				->html(
144
					$tdElement
145
						->copy()
146
						->attr('colspan', count($tableArray))
147
						->text($this->_language->get('user_no'))
148
				);
149
		}
150
151 2
		/* collect output */
152 2
153
		$outputHead = $theadElement->html(
154 2
			$trElement->html($outputHead)
155 2
		);
156 2
		$outputBody = $tbodyElement->html($outputBody);
157
		$outputFoot = $tfootElement->html(
158 2
			$trElement->html($outputFoot)
159 2
		);
160
		$output .= $wrapperElement->copy()->html(
161 2
			$tableElement->html($outputHead . $outputBody . $outputFoot)
162
		);
163
		return $output;
164
	}
165
166
	/**
167
	 * render the session
168
	 *
169
	 * @since 4.0.0
170
	 *
171
	 * @param string $last
172
	 *
173
	 * @return string
174 2
	 */
175
176 2
	protected function _renderSession(string $last = null) : string
177 2
	{
178
		$daterLast = new Dater();
179
		$daterLast->init($last);
180
		$daterNow = new Dater();
181 2
		$daterNow->init($this->_registry->get('now'));
182 2
183
		/* handle session */
184 2
185
		if ($daterLast->getDateTime() > $daterNow->getDateTime()->modify('-1 minute'))
186
		{
187
			return $this->_language->get('online');
188
		}
189 2
		if ($daterLast->getDateTime() > $daterNow->getDateTime()->modify('+1 minute -1 day'))
190
		{
191
			return $this->_language->get('today') . ' ' . $this->_language->get('at') . ' ' . $daterLast->formatTime();
192 2
		}
193 2
		return $daterLast->formatDate();
194 2
	}
195
196 2
	/**
197
	 * render the group
198
	 *
199
	 * @since 4.0.0
200
	 *
201
	 * @param string groups
202
	 *
203
	 * @return string
204
	 */
205
206
	protected function _renderGroup(string $groups = null) : string
207
	{
208
		$output = null;
209
		$groupModel = new Admin\Model\Group();
210
		$groupArray = array_map('intval', explode(',', $groups));
211
212
		/* html element */
213
214
		$linkElement = new Html\Element();
215
		$linkElement->init('a',
216
		[
217
			'class' => 'rs-admin-link-default'
218
		]);
219
220
		/* process groups */
221
222
		foreach ($groupArray as $groupId)
223
		{
224
			$output .= $linkElement
225
				->copy()
226
				->attr('href', $this->_registry->get('parameterRoute') . 'admin/edit/groups/' . $groupId)
227
				->text($groupModel->getById($groupId)->name);
228
		}
229
		return $output;
230
	}
231
}
232