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