Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
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
				return $this->_success(
98
				[
99
					'route' => $this->_getRoute($table),
100
					'timeout' => 0
101
				]);
102
			}
103
		}
104
105
		/* handle uninstall */
106
107
		if ($action === 'uninstall')
108
		{
109
			if ($this->_uninstall($table, $alias))
110
			{
111
				return $this->_success(
112
				[
113
					'route' => $this->_getRoute($table),
114
					'timeout' => 0
115
				]);
116
			}
117
		}
118
119
		/* handle delete */
120
121
		if ($action === 'delete')
122
		{
123
			if ($this->_delete($table, $id))
124
			{
125
				return $this->_success(
126
				[
127
					'route' => $this->_getRoute($table, $id),
128
					'timeout' => 0
129
				]);
130
			}
131
		}
132
133
		/* handle error */
134
135
		return $this->_error(
136
		[
137
			'route' => $this->_getRoute($table, $id)
138
		]);
139
	}
140
141
	/**
142
	 * publish the item
143
	 *
144
	 * @since 4.0.0
145
	 *
146
	 * @param string $table name of the table
147
	 * @param int $id identifier of the item
148
	 *
149
	 * @return bool
150
	 */
151
152
	protected function _publish(string $table = null, int $id = null) : bool
153
	{
154
		if ($table === 'categories')
155
		{
156
			$categoryModel = new Admin\Model\Category();
157
			$articleModel = new Admin\Model\Article();
158
			$commentModel = new Admin\Model\Comment();
159
			return $categoryModel->publishById($id) && $articleModel->publishByCategory($id) && $commentModel->publishByCategory($id);
160
		}
161
		if ($table === 'articles')
162
		{
163
			$articleModel = new Admin\Model\Article();
164
			$commentModel = new Admin\Model\Comment();
165
			return $articleModel->publishById($id) && $commentModel->publishByArticle($id);
166
		}
167
		if ($table === 'extras')
168
		{
169
			$extraModel = new Admin\Model\Extra();
170
			return $extraModel->publishById($id);
171
		}
172
		if ($table === 'comments')
173
		{
174
			$commentModel = new Admin\Model\Comment();
175
			return $commentModel->publishById($id);
176
		}
177
		return false;
178
	}
179
180
	/**
181
	 * unpublish the item
182
	 *
183
	 * @since 4.0.0
184
	 *
185
	 * @param string $table name of the table
186
	 * @param int $id identifier of the item
187
	 *
188
	 * @return bool
189
	 */
190
191
	protected function _unpublish(string $table = null, int $id = null) : bool
192
	{
193
		if ($table === 'categories')
194
		{
195
			$categoryModel = new Admin\Model\Category();
196
			$articleModel = new Admin\Model\Article();
197
			$commentModel = new Admin\Model\Comment();
198
			return $categoryModel->unpublishById($id) && $articleModel->unpublishByCategory($id) && $commentModel->unpublishByCategory($id);
199
		}
200
		if ($table === 'articles')
201
		{
202
			$articleModel = new Admin\Model\Article();
203
			$commentModel = new Admin\Model\Comment();
204
			return $articleModel->unpublishById($id) && $commentModel->unpublishByArticle($id);
205
		}
206
		if ($table === 'extras')
207
		{
208
			$extraModel = new Admin\Model\Extra();
209
			return $extraModel->unpublishById($id);
210
		}
211
		if ($table === 'comments')
212
		{
213
			$commentModel = new Admin\Model\Comment();
214
			return $commentModel->unpublishById($id);
215
		}
216
		return false;
217
	}
218
219
	/**
220
	 * enable the item
221
	 *
222
	 * @since 4.0.0
223
	 *
224
	 * @param string $table name of the table
225
	 * @param int $id identifier of the item
226
	 *
227
	 * @return bool
228
	 */
229
230
	protected function _enable(string $table = null, int $id = null) : bool
231
	{
232
		if ($table === 'groups')
233
		{
234
			$groupModel = new Admin\Model\Group();
235
			return $groupModel->enableById($id);
236
		}
237
		if ($table === 'users')
238
		{
239
			$userModel = new Admin\Model\User();
240
			return $userModel->enableById($id);
241
		}
242
		if ($table === 'modules')
243
		{
244
			$moduleModel = new Admin\Model\Module();
245
			return $moduleModel->enableById($id);
246
		}
247
		return false;
248
	}
249
250
	/**
251
	 * disable the item
252
	 *
253
	 * @since 4.0.0
254
	 *
255
	 * @param string $table name of the table
256
	 * @param int $id identifier of the item
257
	 *
258
	 * @return bool
259
	 */
260
261
	protected function _disable(string $table = null, int $id = null) : bool
262
	{
263
		if ($table === 'groups')
264
		{
265
			$groupModel = new Admin\Model\Group();
266
			return $groupModel->disableById($id);
267
		}
268
		if ($table === 'users')
269
		{
270
			$userModel = new Admin\Model\User();
271
			return $userModel->disableById($id);
272
		}
273
		if ($table === 'modules')
274
		{
275
			$moduleModel = new Admin\Model\Module();
276
			return $moduleModel->disableById($id);
277
		}
278
		return false;
279
	}
280
281
	/**
282
	 * install the item
283
	 *
284
	 * @since 4.0.0
285
	 *
286
	 * @param string $table name of the table
287
	 * @param string $alias alias of the item
288
	 *
289
	 * @return bool
290
	 */
291
292
	protected function _install(string $table = null, string $alias = null) : bool
293
	{
294
		if ($table === 'modules')
295
		{
296
			$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias;
297
			if (method_exists($moduleClass, 'install'))
298
			{
299
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
300
				return $module->install();
301
			}
302
		}
303
		return false;
304
	}
305
306
	/**
307
	 * uninstall the item
308
	 *
309
	 * @since 4.0.0
310
	 *
311
	 * @param string $table name of the table
312
	 * @param string $alias alias of the item
313
	 *
314
	 * @return bool
315
	 */
316
317
	protected function _uninstall(string $table = null, string $alias = null) : bool
318
	{
319
		if ($table === 'modules')
320
		{
321
			$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias;
322
			if (method_exists($moduleClass, 'uninstall'))
323
			{
324
				$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
325
				return $module->uninstall();
326
			}
327
		}
328
		return false;
329
	}
330
331
	/**
332
	 * delete the item
333
	 *
334
	 * @since 4.0.0
335
	 *
336
	 * @param string $table name of the table
337
	 * @param int $id identifier of the item
338
	 *
339
	 * @return bool
340
	 */
341
342
	protected function _delete(string $table = null, int $id = null) : bool
343
	{
344
		if ($table === 'categories')
345
		{
346
			$categoryModel = new Admin\Model\Category();
347
			$articleModel = new Admin\Model\Article();
348
			$commentModel = new Admin\Model\Comment();
349
			return $commentModel->deleteByCategory($id) && $articleModel->deleteByCategory($id) && $categoryModel->deleteById($id);
350
		}
351
		if ($table === 'articles')
352
		{
353
			$articleModel = new Admin\Model\Article();
354
			$commentModel = new Admin\Model\Comment();
355
			return $commentModel->deleteByArticle($id) && $articleModel->deleteById($id);
356
		}
357
		if ($table === 'extras')
358
		{
359
			$extraModel = new Admin\Model\Extra();
360
			return $extraModel->deleteById($id);
361
		}
362
		if ($table === 'comments')
363
		{
364
			$commentModel = new Admin\Model\Comment();
365
			return $commentModel->deleteById($id);
366
		}
367
		if ($table === 'groups')
368
		{
369
			$groupModel = new Admin\Model\Group();
370
			return $groupModel->deleteById($id);
371
		}
372
		if ($table === 'users')
373
		{
374
			$userModel = new Admin\Model\User();
375
			return $userModel->deleteById($id);
376
		}
377
		return false;
378
	}
379
380
	/**
381
	 * get the route
382
	 *
383
	 * @since 4.0.0
384
	 *
385
	 * @param string $table name of the table
386
	 * @param int $id identifier of the item
387
	 *
388
	 * @return string
389
	 */
390
391
	protected function _getRoute(string $table = null, int $id = null) : string
392
	{
393
		if ($this->_registry->get($table . 'Edit'))
394
		{
395
			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...
396
			{
397
				return 'admin/view/' . $table . '#row-' . $id;
398
			}
399
			if ($table)
400
			{
401
				return 'admin/view/' . $table;
402
			}
403
		}
404
		return 'admin';
405
	}
406
}
407