Completed
Push — master ( aaa756...6a04f6 )
by Henry
70:00 queued 35:28
created

includes/Admin/View/GroupTable.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\Html;
6
use Redaxscript\Module;
7
8
/**
9
 * children class to create the admin group table
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category View
15
 * @author Henry Ruhs
16
 */
17
18
class GroupTable extends ViewAbstract
19
{
20
	/**
21
	 * render the view
22
	 *
23
	 * @since 4.0.0
24
	 *
25
	 * @return string
26
	 */
27
28 2
	public function render() : string
29
	{
30 2
		$output = Module\Hook::trigger('adminGroupTableStart');
31 2
		$parameterRoute = $this->_registry->get('parameterRoute');
32 2
		$groupsNew = $this->_registry->get('groupsNew');
33
34
		/* html element */
35
36 2
		$element = new Html\Element();
37
		$titleElement = $element
38 2
			->copy()
39 2
			->init('h2',
40
			[
41 2
				'class' => 'rs-admin-title-content',
42
			])
43 2
			->text($this->_language->get('groups'));
44
		$linkElement = $element
45 2
			->copy()
46 2
			->init('a',
47
			[
48 2
				'class' => 'rs-admin-button-default rs-admin-button-create',
49 2
				'href' => $parameterRoute . 'admin/new/groups'
50
			])
51 2
			->text($this->_language->get('group_new'));
52
53
		/* collect output */
54
55 2
		$output .= $titleElement;
56 2
		if ($groupsNew)
57
		{
58 1
			$output .= $linkElement;
59
		}
60 2
		$output .= $this->_renderTable();
61 2
		$output .= Module\Hook::trigger('adminGroupTableEnd');
62 2
		return $output;
63
	}
64
65
	/**
66
	 * render the table
67
	 *
68
	 * @since 4.0.0
69
	 *
70
	 * @return string|null
71
	 */
72
73 2
	protected function _renderTable() : ?string
74
	{
75 2
		$output = null;
76 2
		$outputHead = null;
77 2
		$outputBody = null;
78 2
		$outputFoot = null;
79
		$tableArray =
80
		[
81 2
			'name' => $this->_language->get('name'),
82 2
			'alias' => $this->_language->get('alias'),
83 2
			'description' => $this->_language->get('description')
84
		];
85 2
		$adminControl = new Helper\Control($this->_registry, $this->_language);
86 2
		$userModel = new Admin\Model\Group();
87 2
		$groups = $userModel->getAll();
88 2
		$groupsTotal = $groups->count();
89
90
		/* html element */
91
92 2
		$element = new Html\Element();
93
		$wrapperElement = $element
94 2
			->copy()
95 2
			->init('div',
96
			[
97 2
				'class' => 'rs-admin-wrapper-table'
98
			]);
99
		$tableElement = $element
100 2
			->copy()
101 2
			->init('table',
102
			[
103 2
				'class' => 'rs-admin-table-default'
104
			]);
105 2
		$theadElement = $element->copy()->init('thead');
106 2
		$tbodyElement = $element->copy()->init('tbody');
107 2
		$tfootElement = $element->copy()->init('tfoot');
108 2
		$trElement = $element->copy()->init('tr');
109 2
		$thElement = $element->copy()->init('th');
110 2
		$tdElement = $element->copy()->init('td');
111
112
		/* process table */
113
114 2
		foreach ($tableArray as $key => $value)
115
		{
116 2
			$outputHead .= $thElement->copy()->text($value);
117 2
			$outputFoot .= $tdElement->copy()->text($value);
118
		}
119
120
		/* process categories */
121
122 2
		if ($groupsTotal)
123
		{
124 2
			foreach ($groups as $key => $value)
0 ignored issues
show
The expression $groups 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...
125
			{
126
				$outputBody .= $trElement
127 2
					->copy()
128 2
					->attr('id', 'row-' . $value->id)
129 2
					->addClass(!$value->status ? 'rs-admin-is-disabled' : null)
130 2
					->html(
131 2
						$tdElement->copy()->html($value->name . $adminControl->render('groups', $value->id, $value->alias, $value->status)) .
132 2
						$tdElement->copy()->text($value->alias) .
133 2
						$tdElement->copy()->text($value->description)
134
				);
135
			}
136
		}
137
		else
138
		{
139
			$outputBody .= $trElement
140
				->copy()
141
				->html(
142
					$tdElement
143
						->copy()
144
						->attr('colspan', count($tableArray))
145
						->text($this->_language->get('group_no'))
146
				);
147
		}
148
149
		/* collect output */
150
151 2
		$outputHead = $theadElement->html(
152 2
			$trElement->html($outputHead)
153
		);
154 2
		$outputBody = $tbodyElement->html($outputBody);
155 2
		$outputFoot = $tfootElement->html(
156 2
			$trElement->html($outputFoot)
157
		);
158 2
		$output .= $wrapperElement->copy()->html(
159 2
			$tableElement->html($outputHead . $outputBody . $outputFoot)
160
		);
161 2
		return $output;
162
	}
163
}
164