Completed
Push — master ( 16ddfb...0ea243 )
by Henry
09:18
created

includes/Admin/View/UserTable.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
use function count;
9
use function json_decode;
10
11
/**
12
 * children class to create the admin user table
13
 *
14
 * @since 4.0.0
15
 *
16
 * @package Redaxscript
17
 * @category View
18
 * @author Henry Ruhs
19
 */
20
21
class UserTable extends ViewAbstract
22
{
23
	/**
24
	 * render the view
25
	 *
26
	 * @since 4.0.0
27
	 *
28
	 * @return string
29
	 */
30
31 4
	public function render() : string
32
	{
33 4
		$output = Module\Hook::trigger('adminUserTableStart');
34 4
		$parameterRoute = $this->_registry->get('parameterRoute');
35 4
		$usersNew = $this->_registry->get('usersNew');
36
37
		/* html element */
38
39 4
		$element = new Html\Element();
40
		$titleElement = $element
41 4
			->copy()
42 4
			->init('h2',
43
			[
44 4
				'class' => 'rs-admin-title-content',
45
			])
46 4
			->text($this->_language->get('users'));
47
		$linkElement = $element
48 4
			->copy()
49 4
			->init('a',
50
			[
51 4
				'class' => 'rs-admin-button-default rs-admin-button-create',
52 4
				'href' => $parameterRoute . 'admin/new/users'
53
			])
54 4
			->text($this->_language->get('user_new'));
55
56
		/* collect output */
57
58 4
		$output .= $titleElement;
59 4
		if ($usersNew)
60
		{
61 1
			$output .= $linkElement;
62
		}
63 4
		$output .= $this->_renderTable();
64 4
		$output .= Module\Hook::trigger('adminUserTableEnd');
65 4
		return $output;
66
	}
67
68
	/**
69
	 * render the table
70
	 *
71
	 * @since 4.0.0
72
	 *
73
	 * @return string|null
74
	 */
75
76 4
	protected function _renderTable() : ?string
77
	{
78 4
		$output = null;
79 4
		$outputHead = null;
80 4
		$outputBody = null;
81 4
		$outputFoot = null;
82
		$tableArray =
83
		[
84 4
			'name' => $this->_language->get('name'),
85 4
			'user' => $this->_language->get('user'),
86 4
			'session' => $this->_language->get('session'),
87 4
			'groups' => $this->_language->get('groups')
88
		];
89 4
		$adminControl = new Helper\Control($this->_registry, $this->_language);
90 4
		$adminControl->init();
91 4
		$userModel = new Admin\Model\User();
92 4
		$users = $userModel->getAll();
93 4
		$usersTotal = $users->count();
94
95
		/* html element */
96
97 4
		$element = new Html\Element();
98
		$wrapperElement = $element
99 4
			->copy()
100 4
			->init('div',
101
			[
102 4
				'class' => 'rs-admin-wrapper-table'
103
			]);
104
		$tableElement = $element
105 4
			->copy()
106 4
			->init('table',
107
			[
108 4
				'class' => 'rs-admin-table-default'
109
			]);
110 4
		$theadElement = $element->copy()->init('thead');
111 4
		$tbodyElement = $element->copy()->init('tbody');
112 4
		$tfootElement = $element->copy()->init('tfoot');
113 4
		$trElement = $element->copy()->init('tr');
114 4
		$thElement = $element->copy()->init('th');
115 4
		$tdElement = $element->copy()->init('td');
116
117
		/* process table */
118
119 4
		foreach ($tableArray as $key => $value)
120
		{
121 4
			$outputHead .= $thElement->copy()->text($value);
122 4
			$outputFoot .= $tdElement->copy()->text($value);
123
		}
124
125
		/* process categories */
126
127 4
		if ($usersTotal)
128
		{
129 4
			foreach ($users as $key => $value)
0 ignored issues
show
The expression $users of type array|object<IdiormResultSet>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
130
			{
131
				$outputBody .= $trElement
132 4
					->copy()
133 4
					->attr('id', 'row-' . $value->id)
134 4
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
135 4
					->html(
136 4
						$tdElement->copy()->html($value->name . $adminControl->render('users', $value->id, $value->alias, $value->status)) .
137 4
						$tdElement->copy()->text($value->user) .
138 4
						$tdElement->copy()->text($this->_renderSession($value->last)) .
139 4
						$tdElement->copy()->html($this->_renderGroup($value->groups))
140
					);
141
			}
142
		}
143
		else
144
		{
145
			$outputBody .= $trElement
146
				->copy()
147
				->html(
148
					$tdElement
149
						->copy()
150
						->attr('colspan', count($tableArray))
151
						->text($this->_language->get('user_no'))
152
				);
153
		}
154
155
		/* collect output */
156
157 4
		$outputHead = $theadElement->html(
158 4
			$trElement->html($outputHead)
159
		);
160 4
		$outputBody = $tbodyElement->html($outputBody);
161 4
		$outputFoot = $tfootElement->html(
162 4
			$trElement->html($outputFoot)
163
		);
164 4
		$output .= $wrapperElement->copy()->html(
165 4
			$tableElement->html($outputHead . $outputBody . $outputFoot)
166
		);
167 4
		return $output;
168
	}
169
170
	/**
171
	 * render the session
172
	 *
173
	 * @since 4.0.0
174
	 *
175
	 * @param string $last
176
	 *
177
	 * @return string
178
	 */
179
180 4
	protected function _renderSession(string $last = null) : string
181
	{
182 4
		$daterLast = new Dater();
183 4
		$daterLast->init($last);
184 4
		$daterNow = new Dater();
185 4
		$daterNow->init($this->_registry->get('now'));
186
187
		/* handle session */
188
189 4
		if ($daterLast->getDateTime() > $daterNow->getDateTime()->modify('-1 minute'))
190
		{
191 2
			return $this->_language->get('online');
192
		}
193 2
		if ($daterLast->getDateTime() > $daterNow->getDateTime()->modify('+1 minute -1 day'))
194
		{
195 1
			return $this->_language->get('today') . ' ' . $this->_language->get('at') . ' ' . $daterLast->formatTime();
196
		}
197 1
		return $daterLast->formatDate();
198
	}
199
200
	/**
201
	 * render the group
202
	 *
203
	 * @since 4.0.0
204
	 *
205
	 * @param string groups
206
	 *
207
	 * @return string|null
208
	 */
209
210 4
	protected function _renderGroup(string $groups = null) : ?string
211
	{
212 4
		$output = null;
213 4
		$groupModel = new Admin\Model\Group();
214 4
		$groupArray = (array)json_decode($groups);
215
216
		/* html element */
217
218 4
		$linkElement = new Html\Element();
219 4
		$linkElement->init('a',
220
		[
221 4
			'class' => 'rs-admin-link-default'
222
		]);
223
224
		/* process groups */
225
226 4
		if ($groupArray)
227
		{
228 4
			foreach ($groupArray as $groupId)
229
			{
230
				$output .= $linkElement
231 4
					->copy()
232 4
					->attr('href', $this->_registry->get('parameterRoute') . 'admin/edit/groups/' . $groupId)
233 4
					->text($groupModel->getById($groupId)->name);
234
			}
235
		}
236
		else
237
		{
238
			$output = $this->_language->get('none');
239
		}
240 4
		return $output;
241
	}
242
}
243