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