Completed
Push — master ( 03fc7e...44f12a )
by Henry
15:26
created

modules/TableSorter/TableSorter.php (1 issue)

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\Modules\TableSorter;
3
4
use Redaxscript\Db;
5
use Redaxscript\Filter;
6
use Redaxscript\Head;
7
use Redaxscript\Header;
8
use Redaxscript\Module;
9
10
/**
11
 * javaScript powered table sorter
12
 *
13
 * @since 4.0.0
14
 *
15
 * @package Redaxscript
16
 * @category Modules
17
 * @author Henry Ruhs
18
 */
19
20
class TableSorter extends Module\Module
21
{
22
	/**
23
	 * array of the module
24
	 *
25
	 * @var array
26
	 */
27
28
	protected static $_moduleArray =
29
	[
30
		'name' => 'Table Sorter',
31
		'alias' => 'TableSorter',
32
		'author' => 'Redaxmedia',
33
		'description' => 'JavaScript powered table sorter',
34
		'version' => '4.0.0'
35
	];
36
37
	/**
38
	 * renderStart
39
	 *
40
	 * @since 4.0.0
41
	 */
42
43
	public function renderStart()
44
	{
45
		if ($this->_registry->get('loggedIn') === $this->_registry->get('token'))
46
		{
47
			/* link */
48
49
			$link = Head\Link::getInstance();
50
			$link
51
				->init()
52
				->appendFile('modules/TableSorter/dist/styles/table-sorter.min.css');
53
54
			/* script */
55
56
			$script = Head\Script::getInstance();
57
			$script
58
				->init('foot')
59
				->appendFile(
60
				[
61
					'https://cdnjs.cloudflare.com/ajax/libs/dragula/3.7.2/dragula.min.js',
62
					'modules/TableSorter/assets/scripts/init.js',
63
					'modules/TableSorter/dist/scripts/table-sorter.min.js'
64
				]);
65
66
			/* handle sort */
67
68
			if ($this->_registry->get('firstParameter') === 'module' && $this->_registry->get('secondParameter') === 'table-sorter' && $this->_registry->get('thirdParameter') === 'sort' && $this->_registry->get('tokenParameter'))
69
			{
70
				$this->_registry->set('renderBreak', true);
0 ignored issues
show
true is of type boolean, but the function expects a string|array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
				echo $this->_sort();
72
			}
73
		}
74
	}
75
76
	/**
77
	 * sort
78
	 *
79
	 * @since 4.0.0
80
	 *
81
	 * @return string|null
82
	 */
83
84
	protected function _sort() : ?string
85
	{
86
		if ($this->_request->getServer('HTTP_X_REQUESTED_WITH') === 'XMLHttpRequest')
87
		{
88
			$postArray = $this->_sanitizePost();
89
			$contents = Db::forTablePrefix($postArray['table'])->whereIn('id', $postArray['rankArray'])->findMany();
90
91
			/* process contents */
92
93
			foreach ($contents as $value)
94
			{
95
				$value->set('rank', array_search($value->id, $postArray['rankArray']));
96
			}
97
98
			/* handle response */
99
100
			if ($contents->save())
101
			{
102
				Header::contentType('application/json');
103
				return json_encode($postArray['rankArray']);
104
			}
105
		}
106
		Header::responseCode(404);
107
		exit;
108
	}
109
110
	/**
111
	 * sanitize the post
112
	 *
113
	 * @since 4.0.0
114
	 *
115
	 * @return array
116
	 */
117
118
	protected function _sanitizePost() : array
119
	{
120
		$specialFilter = new Filter\Special();
121
		$content = file_get_contents('php://input');
122
		$postArray = (array)json_decode($content);
123
124
		/* sanitize post */
125
126
		return
127
		[
128
			'table' => $specialFilter->sanitize($postArray['table']),
129
			'rankArray' => $postArray['rankArray']
130
		];
131
	}
132
}
133