Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Admin/View/Helper/Control.php (7 issues)

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\Helper;
3
4
use Redaxscript\Admin\View\ViewAbstract;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use function array_replace_recursive;
8
use function in_array;
9
use function ucfirst;
10
11
/**
12
 * helper class to create the admin control
13
 *
14
 * @since 4.0.0
15
 *
16
 * @package Redaxscript
17
 * @category View
18
 * @author Henry Ruhs
19
 */
20
21
class Control extends ViewAbstract
22
{
23
	/**
24
	 * options of the panel
25
	 *
26
	 * @var array
27
	 */
28
29
	protected $_optionArray =
30
	[
31
		'className' =>
32
		[
33
			'list' => 'rs-admin-list-control',
34
			'item' =>
35
			[
36
				'control' => 'rs-admin-item-control',
37
				'disable' => 'rs-admin-item-disable',
38
				'enable' => 'rs-admin-item-enable',
39
				'future-posting' => 'rs-admin-item-future-posting',
40
				'unpublish' => 'rs-admin-item-unpublish',
41
				'publish' => 'rs-admin-item-publish',
42
				'edit' => 'rs-admin-item-edit',
43
				'delete' => 'rs-admin-item-delete',
44
				'install' => 'rs-admin-item-install',
45
				'uninstall' => 'rs-admin-item-uninstall'
46
			],
47
			'link' =>
48
			[
49
				'delete' => 'rs-admin-js-delete',
50
				'uninstall' => 'rs-admin-js-uninstall'
51
			]
52
		]
53
	];
54
55
	/**
56
	 * init the class
57
	 *
58
	 * @since 4.0.0
59
	 *
60
	 * @param array $optionArray options of the panel
61
	 */
62
63
	public function init(array $optionArray = []) : void
64
	{
65
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
66
	}
67
68
	/**
69
	 * render the view
70
	 *
71
	 * @since 4.0.0
72
	 *
73
	 * @param string $table name of the table
74
	 * @param int $id identifier of the item
75
	 * @param string $alias alias of the item
76
	 * @param int $status status of the item
77
	 *
78
	 * @return string|null
79
	 */
80
81 12
	public function render(string $table = null, int $id = null, string $alias = null, int $status = null) : ?string
82
	{
83 12
		$output = Module\Hook::trigger('adminControlStart');
84 12
		$outputItem = null;
85 12
		$parameterRoute = $this->_registry->get('parameterRoute');
86 12
		$token = $this->_registry->get('token');
87
88
		/* html element */
89
90 12
		$element = new Html\Element();
91
		$listElement = $element
92 12
			->copy()
93 12
			->init('ul',
94
			[
95 12
				'class' => $this->_optionArray['className']['list']
96
			]);
97
		$itemElement = $element
98 12
			->copy()
99 12
			->init('li',
100
			[
101 12
				'class' => $this->_optionArray['className']['item']['control']
102
			]);
103
		$linkElement = $element
104 12
			->copy()
105 12
			->init('a');
106
		$textElement = $element
107 12
			->copy()
108 12
			->init('span');
109
110
		/* collect enable */
111
112 12
		if ($this->_hasPermission($table, 'edit') && $this->_showAction($table, 'enable', $id))
113
		{
114 2
			$enableAction = $status ? 'disable' : 'enable';
115
			$outputItem .= $itemElement
116 2
				->copy()
117 2
				->addClass($enableAction === 'disable' ? $this->_optionArray['className']['item']['disable'] : $this->_optionArray['className']['item']['enable'])
118 2
				->html(
119
					$linkElement
120 2
						->copy()
121 2
						->attr('href', $parameterRoute . 'admin/' . $enableAction . '/' . $table . '/' . $id . '/' . $token)
122 2
						->text($enableAction === 'disable' ? $this->_language->get('disable') : $this->_language->get('enable'))
123
				);
124
		}
125
126
		/* collect publish */
127
128 12
		if ($this->_hasPermission($table, 'edit') && $this->_showAction($table, 'publish', $id))
129
		{
130 3
			if ($status === 2)
131
			{
132
				$outputItem .= $itemElement
133 1
					->copy()
134 1
					->addClass($this->_optionArray['className']['item']['future-posting'])
135 1
					->html(
136
						$textElement
137 1
							->copy()
138 1
							->text($this->_language->get('future_posting'))
139
					);
140
			}
141
			else
142
			{
143 2
				$publishAction = $status ? 'unpublish' : 'publish';
144
				$outputItem .= $itemElement
145 2
					->copy()
146 2
					->addClass($publishAction === 'unpublish' ? $this->_optionArray['className']['item']['unpublish'] : $this->_optionArray['className']['item']['publish'])
147 2
					->html(
148
						$linkElement
149 2
							->copy()
150 2
							->attr('href', $parameterRoute . 'admin/' . $publishAction . '/' . $table . '/' . $id . '/' . $token)
151 2
							->text($publishAction === 'unpublish' ? $this->_language->get('unpublish') : $this->_language->get('publish'))
152
					);
153
			}
154
		}
155
156
		/* collect install */
157
158 12
		if ($this->_hasPermission($table, 'install') && $this->_showAction($table, 'install', $id))
159
		{
160
			$outputItem .= $itemElement
161 1
				->copy()
162 1
				->addClass($this->_optionArray['className']['item']['install'])
163 1
				->html(
164
					$linkElement
165 1
						->copy()
166 1
						->attr('href', $parameterRoute . 'admin/install/' . $table . '/' . $alias . '/' . $token)
167 1
						->text($this->_language->get('install'))
168
				);
169
		}
170
171
		/* collect edit */
172
173 12
		if ($this->_hasPermission($table, 'edit') && $this->_showAction($table, 'edit', $id))
174
		{
175
			$outputItem .= $itemElement
176 6
				->copy()
177 6
				->addClass($this->_optionArray['className']['item']['edit'])
178 6
				->html(
179
					$linkElement
180 6
						->copy()
181 6
						->attr('href', $parameterRoute . 'admin/edit/' . $table . '/' . $id)
182 6
						->text($this->_language->get('edit'))
183
				);
184
		}
185
186
		/* collect delete */
187
188 12
		if ($this->_hasPermission($table, 'delete') && $this->_showAction($table, 'delete', $id))
189
		{
190
			$outputItem .= $itemElement
191 3
				->copy()
192 3
				->addClass($this->_optionArray['className']['item']['delete'])
193 3
				->html(
194
					$linkElement
195 3
						->copy()
196 3
						->addClass($this->_optionArray['className']['link']['delete'])
197 3
						->attr('href', $parameterRoute . 'admin/delete/' . $table . '/' . $id . '/' . $token)
198 3
						->text($this->_language->get('delete'))
199
				);
200
		}
201
202
		/* collect uninstall */
203
204 12
		if ($this->_hasPermission($table, 'uninstall') && $this->_showAction($table, 'uninstall', $id))
205
		{
206
			$outputItem .= $itemElement
207 1
				->copy()
208 1
				->addClass($this->_optionArray['className']['item']['uninstall'])
209 1
				->html(
210
					$linkElement
211 1
						->copy()
212 1
						->addClass($this->_optionArray['className']['link']['uninstall'])
213 1
						->attr('href', $parameterRoute . 'admin/uninstall/' . $table . '/' . $alias . '/' . $token)
214 1
						->text($this->_language->get('uninstall'))
215
				);
216
		}
217
218
		/* collect output */
219
220 12
		if ($outputItem)
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $outputItem of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
221
		{
222 9
			$output .= $listElement->html($outputItem);
223
		}
224 12
		$output .= Module\Hook::trigger('adminControlEnd');
225 12
		return $output;
226
	}
227
228
	/**
229
	 * has the permission
230
	 *
231
	 * @since 4.0.0
232
	 *
233
	 * @param string $table name of the table
234
	 * @param string $type
235
	 *
236
	 * @return bool
237
	 */
238
239 12
	protected function _hasPermission(string $table = null, string $type = null) : bool
240
	{
241 12
		return (bool)$this->_registry->get($table . ucfirst($type));
242
	}
243
244
	/**
245
	 * show the action
246
	 *
247
	 * @since 4.0.0
248
	 *
249
	 * @param string $table name of the table
250
	 * @param string $type
251
	 * @param int $id
252
	 *
253
	 * @return bool
254
	 */
255
256 9
	protected function _showAction(string $table = null, string $type = null, int $id = null) : bool
257
	{
258
		$enableArray =
259
		[
260 9
			'groups',
261
			'users',
262
			'modules'
263
		];
264
		$publishArray =
265
		[
266 9
			'categories',
267
			'articles',
268
			'extras',
269
			'comments'
270
		];
271
		$deleteArray =
272
		[
273 9
			'categories',
274
			'articles',
275
			'extras',
276
			'comments',
277
			'groups',
278
			'users'
279
		];
280 9
		if ($id === 1 && ($type === 'enable' || $type === 'delete') && ($table === 'users' || $table === 'groups'))
281
		{
282 1
			return false;
283
		}
284 9
		return $type === 'enable' && in_array($table, $enableArray) && $id ||
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $id of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
285 9
			$type === 'publish' && in_array($table, $publishArray) && $id ||
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $id of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
286 9
			$type === 'delete' && in_array($table, $deleteArray) && $id ||
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $id of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
287 8
			$type === 'install' && $table === 'modules' && !$id ||
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $id of type null|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
288 7
			$type === 'uninstall' && $table === 'modules' && $id ||
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $id of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
289 9
			$type === 'edit' && $id;
0 ignored issues
show
Bug Best Practice introduced by redaxmedia
The expression $id of type null|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
290
	}
291
}
292