Completed
Push — master ( 7de195...5f1ca1 )
by Henry
08:39
created

includes/Admin/Controller/Common.php (1 issue)

Check for loose comparison of integers.

Best Practice Bug Major

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\Controller;
3
4
use Redaxscript\Admin;
5
use function method_exists;
6
7
/**
8
 * children class to handle common
9
 *
10
 * @since 4.0.0
11
 *
12
 * @package Redaxscript
13
 * @category Controller
14
 * @author Henry Ruhs
15
 */
16
17
class Common extends ControllerAbstract
18
{
19
	/**
20
	 * process the class
21
	 *
22
	 * @since 4.0.0
23
	 *
24
	 * @param string $action action to process
25
	 *
26
	 * @return string
27
	 */
28
29
	public function process(string $action = null) : string
30
	{
31
		$table = $this->_registry->get('tableParameter');
32
		$id = $this->_registry->get('idParameter');
33
		$alias = $this->_registry->get('aliasParameter');
34
35
		/* handle publish */
36
37
		if ($action === 'publish')
38
		{
39
			if ($this->_publish($table, $id))
40
			{
41
				return $this->_success(
42
				[
43
					'route' => $this->_getRoute($table, $id),
44
					'timeout' => 0
45
				]);
46
			}
47
		}
48
49
		/* handle unpublish */
50
51
		if ($action === 'unpublish')
52
		{
53
			if ($this->_unpublish($table, $id))
54
			{
55
				return $this->_success(
56
				[
57
					'route' => $this->_getRoute($table, $id),
58
					'timeout' => 0
59
				]);
60
			}
61
		}
62
63
		/* handle enable */
64
65
		if ($action === 'enable')
66
		{
67
			if ($this->_enable($table, $id))
68
			{
69
				return $this->_success(
70
				[
71
					'route' => $this->_getRoute($table, $id),
72
					'timeout' => 0
73
				]);
74
			}
75
		}
76
77
		/* handle disable */
78
79
		if ($action === 'disable')
80
		{
81
			if ($this->_disable($table, $id))
82
			{
83
				return $this->_success(
84
				[
85
					'route' => $this->_getRoute($table, $id),
86
					'timeout' => 0
87
				]);
88
			}
89
		}
90
91
		/* handle install */
92
93
		if ($action === 'install')
94
		{
95
			if ($this->_install($table, $alias))
96
			{
97
				$moduleModel = new Admin\Model\Module();
98
				$moduleId = $moduleModel->getByAlias($alias)->id;
99
				return $this->_success(
100
				[
101
					'route' => $this->_getRoute($table, $moduleId),
102
					'timeout' => 0
103
				]);
104
			}
105
		}
106
107
		/* handle uninstall */
108
109
		if ($action === 'uninstall')
110
		{
111
			if ($this->_uninstall($table, $alias))
112
			{
113
				return $this->_success(
114
				[
115
					'route' => $this->_getRoute($table),
116
					'timeout' => 0
117
				]);
118
			}
119
		}
120
121
		/* handle delete */
122
123
		if ($action === 'delete')
124
		{
125
			if ($this->_delete($table, $id))
126
			{
127
				return $this->_success(
128
				[
129
					'route' => $this->_getRoute($table),
130
					'timeout' => 0
131
				]);
132
			}
133
		}
134
135
		/* handle error */
136
137
		return $this->_error(
138
		[
139
			'route' => $this->_getRoute($table)
140
		]);
141
	}
142
143
	/**
144
	 * publish the item
145
	 *
146
	 * @since 4.0.0
147
	 *
148
	 * @param string $table name of the table
149
	 * @param int $id identifier of the item
150
	 *
151
	 * @return bool
152
	 */
153
154
	protected function _publish(string $table = null, int $id = null) : bool
155
	{
156
		if ($table === 'categories')
157
		{
158
			$categoryModel = new Admin\Model\Category();
159
			$articleModel = new Admin\Model\Article();
160
			$commentModel = new Admin\Model\Comment();
161
			return $categoryModel->publishById($id) && $articleModel->publishByCategory($id) && $commentModel->publishByCategory($id);
162
		}
163
		if ($table === 'articles')
164
		{
165
			$articleModel = new Admin\Model\Article();
166
			$commentModel = new Admin\Model\Comment();
167
			return $articleModel->publishById($id) && $commentModel->publishByArticle($id);
168
		}
169
		if ($table === 'extras')
170
		{
171
			$extraModel = new Admin\Model\Extra();
172
			return $extraModel->publishById($id);
173
		}
174
		if ($table === 'comments')
175
		{
176
			$commentModel = new Admin\Model\Comment();
177
			return $commentModel->publishById($id);
178
		}
179
		return false;
180
	}
181
182
	/**
183
	 * unpublish the item
184
	 *
185
	 * @since 4.0.0
186
	 *
187
	 * @param string $table name of the table
188
	 * @param int $id identifier of the item
189
	 *
190
	 * @return bool
191
	 */
192
193
	protected function _unpublish(string $table = null, int $id = null) : bool
194
	{
195
		if ($table === 'categories')
196
		{
197
			$categoryModel = new Admin\Model\Category();
198
			$articleModel = new Admin\Model\Article();
199
			$commentModel = new Admin\Model\Comment();
200
			return $categoryModel->unpublishById($id) && $articleModel->unpublishByCategory($id) && $commentModel->unpublishByCategory($id);
201
		}
202
		if ($table === 'articles')
203
		{
204
			$articleModel = new Admin\Model\Article();
205
			$commentModel = new Admin\Model\Comment();
206
			return $articleModel->unpublishById($id) && $commentModel->unpublishByArticle($id);
207
		}
208
		if ($table === 'extras')
209
		{
210
			$extraModel = new Admin\Model\Extra();
211
			return $extraModel->unpublishById($id);
212
		}
213
		if ($table === 'comments')
214
		{
215
			$commentModel = new Admin\Model\Comment();
216
			return $commentModel->unpublishById($id);
217
		}
218
		return false;
219
	}
220
221
	/**
222
	 * enable the item
223
	 *
224
	 * @since 4.0.0
225
	 *
226
	 * @param string $table name of the table
227
	 * @param int $id identifier of the item
228
	 *
229
	 * @return bool
230
	 */
231
232
	protected function _enable(string $table = null, int $id = null) : bool
233
	{
234
		if ($table === 'groups')
235
		{
236
			$groupModel = new Admin\Model\Group();
237
			return $groupModel->enableById($id);
238
		}
239
		if ($table === 'users')
240
		{
241
			$userModel = new Admin\Model\User();
242
			return $userModel->enableById($id);
243
		}
244
		if ($table === 'modules')
245
		{
246
			$moduleModel = new Admin\Model\Module();
247
			return $moduleModel->enableById($id);
248
		}
249
		return false;
250
	}
251
252
	/**
253
	 * disable the item
254
	 *
255
	 * @since 4.0.0
256
	 *
257
	 * @param string $table name of the table
258
	 * @param int $id identifier of the item
259
	 *
260
	 * @return bool
261
	 */
262
263
	protected function _disable(string $table = null, int $id = null) : bool
264
	{
265
		if ($table === 'groups')
266
		{
267
			$groupModel = new Admin\Model\Group();
268
			return $groupModel->disableById($id);
269
		}
270
		if ($table === 'users')
271
		{
272
			$userModel = new Admin\Model\User();
273
			return $userModel->disableById($id);
274
		}
275
		if ($table === 'modules')
276
		{
277
			$moduleModel = new Admin\Model\Module();
278
			return $moduleModel->disableById($id);
279
		}
280
		return false;
281
	}
282
283
	/**
284
	 * install the item
285
	 *
286
	 * @since 4.0.0
287
	 *
288
	 * @param string $table name of the table
289
	 * @param string $alias alias of the item
290
	 *
291
	 * @return bool
292
	 */
293
294
	protected function _install(string $table = null, string $alias = null) : bool
295
	{
296
		if ($table === 'modules')
297
		{
298
			$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias;
299
			if (method_exists($moduleClass, 'install'))
300
			{
301
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
302
				return $module->install();
303
			}
304
		}
305
		return false;
306
	}
307
308
	/**
309
	 * uninstall the item
310
	 *
311
	 * @since 4.0.0
312
	 *
313
	 * @param string $table name of the table
314
	 * @param string $alias alias of the item
315
	 *
316
	 * @return bool
317
	 */
318
319
	protected function _uninstall(string $table = null, string $alias = null) : bool
320
	{
321
		if ($table === 'modules')
322
		{
323
			$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias;
324
			if (method_exists($moduleClass, 'uninstall'))
325
			{
326
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
327
				return $module->uninstall();
328
			}
329
		}
330
		return false;
331
	}
332
333
	/**
334
	 * delete the item
335
	 *
336
	 * @since 4.0.0
337
	 *
338
	 * @param string $table name of the table
339
	 * @param int $id identifier of the item
340
	 *
341
	 * @return bool
342
	 */
343
344
	protected function _delete(string $table = null, int $id = null) : bool
345
	{
346
		if ($table === 'categories')
347
		{
348
			$categoryModel = new Admin\Model\Category();
349
			$articleModel = new Admin\Model\Article();
350
			$commentModel = new Admin\Model\Comment();
351
			return $commentModel->deleteByCategory($id) && $articleModel->deleteByCategory($id) && $categoryModel->deleteById($id);
352
		}
353
		if ($table === 'articles')
354
		{
355
			$articleModel = new Admin\Model\Article();
356
			$commentModel = new Admin\Model\Comment();
357
			return $commentModel->deleteByArticle($id) && $articleModel->deleteById($id);
358
		}
359
		if ($table === 'extras')
360
		{
361
			$extraModel = new Admin\Model\Extra();
362
			return $extraModel->deleteById($id);
363
		}
364
		if ($table === 'comments')
365
		{
366
			$commentModel = new Admin\Model\Comment();
367
			return $commentModel->deleteById($id);
368
		}
369
		if ($table === 'groups')
370
		{
371
			$groupModel = new Admin\Model\Group();
372
			return $groupModel->deleteById($id);
373
		}
374
		if ($table === 'users')
375
		{
376
			$userModel = new Admin\Model\User();
377
			return $userModel->deleteById($id);
378
		}
379
		return false;
380
	}
381
382
	/**
383
	 * get the route
384
	 *
385
	 * @since 4.0.0
386
	 *
387
	 * @param string $table name of the table
388
	 * @param int $id identifier of the item
389
	 *
390
	 * @return string
391
	 */
392
393
	protected function _getRoute(string $table = null, int $id = null) : string
394
	{
395
		if ($this->_registry->get($table . 'Edit') && $table)
396
		{
397
			if ($id)
0 ignored issues
show
Bug Best Practice introduced by
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...
398
			{
399
				return 'admin/view/' . $table . '#row-' . $id;
400
			}
401
			return 'admin/view/' . $table;
402
		}
403
		return 'admin';
404
	}
405
}
406