Passed
Branch master (e751cb)
by Matt
02:48
created

acp_manager::move()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4.0072

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 4
nop 1
dl 0
loc 22
ccs 12
cts 13
cp 0.9231
crap 4.0072
rs 9.9666
c 3
b 0
f 0
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\core;
12
13
use phpbb\db\driver\driver_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\group\helper;
0 ignored issues
show
Bug introduced by
The type phpbb\group\helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\json_response;
0 ignored issues
show
Bug introduced by
The type phpbb\json_response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\language\language;
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use phpbb\request\request;
0 ignored issues
show
Bug introduced by
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use vse\abbc3\ext;
19
20
/**
21
 * ABBC3 ACP manager class
22
 */
23
class acp_manager
24
{
25
	/** @var driver_interface */
26
	protected $db;
27
28
	/** @var helper */
29
	protected $group_helper;
30
31
	/** @var language */
32
	protected $language;
33
34
	/** @var request */
35
	protected $request;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param driver_interface $db
41
	 * @param helper           $group_helper
42
	 * @param language         $language
43
	 * @param request          $request
44
	 * @access public
45
	 */
46 34
	public function __construct(driver_interface $db, helper $group_helper, language $language, request $request)
47
	{
48 34
		$this->db = $db;
49 34
		$this->group_helper = $group_helper;
50 34
		$this->language = $language;
51 34
		$this->request = $request;
52 34
	}
53
54
	/**
55
	 * Update BBCode order fields in the db on move up/down
56
	 *
57
	 * @param string $action The action move_up|move_down
58
	 * @access public
59
	 */
60 10
	public function move($action)
61
	{
62 10
		$bbcode_id = $this->request->variable('id', 0);
63
64 10
		if (!check_link_hash($this->request->variable('hash', ''), $action . $bbcode_id))
0 ignored issues
show
Bug introduced by
The function check_link_hash was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
		if (!/** @scrutinizer ignore-call */ check_link_hash($this->request->variable('hash', ''), $action . $bbcode_id))
Loading history...
65 10
		{
66 1
			trigger_error($this->language->lang('FORM_INVALID'), E_USER_WARNING);
67
		}
68
69 9
		$current_order = $this->get_bbcode_order($bbcode_id);
70
71
		// First one can't be moved up
72 9
		if ($current_order <= 1 && $action === ext::MOVE_UP)
73 9
		{
74 2
			return;
75
		}
76
77 7
		$updated = $this->update_bbcode_orders($current_order, $action);
78
79 7
		$this->resynchronize_bbcode_order();
80
81 7
		$this->send_json_response($updated);
82 6
	}
83
84
	/**
85
	 * Update BBCode order fields in the db on drag-n-drop
86
	 *
87
	 * @access public
88
	 */
89 3
	public function move_drag()
90
	{
91 3
		if (!$this->request->is_ajax())
92 3
		{
93 1
			return;
94
		}
95
96
		// Get the bbcodes html table's name
97 2
		$tablename = $this->request->variable('tablename', '');
98
99
		// Fetch the posted list
100 2
		$bbcodes_list = (array) $this->request->variable($tablename, [0 => '']);
101
102
		$this->db->sql_transaction('begin');
103 2
		foreach ($bbcodes_list as $order => $bbcode_id)
104
		{
105 2
			$this->db->sql_query($this->update_bbcode_order($bbcode_id, $order));
106 2
		}
107
		$this->db->sql_transaction('commit');
108 2
109 2
		$this->resynchronize_bbcode_order();
110 2
111
		$this->send_json_response(true);
112 2
	}
113
114 2
	/**
115
	 * Retrieve the maximum value from the bbcode_order field stored in the db
116
	 *
117
	 * @return int The maximum order
118
	 * @access public
119
	 */
120
	public function get_max_bbcode_order()
121
	{
122
		return $this->get_max_column_value('bbcode_order');
123 1
	}
124
125 1
	/**
126
	 * Get the bbcode_group data from the posted form
127
	 *
128
	 * @return string The usergroup id numbers, comma delimited, or empty
129
	 * @access public
130
	 */
131
	public function get_bbcode_group_form_data()
132
	{
133
		$bbcode_group = $this->request->variable('bbcode_group', [0]);
134 3
		$bbcode_group = (!count($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group);
135
136 3
		return $bbcode_group;
137 3
	}
138
139 3
	/**
140
	 * Get the bbcode_group data from the database
141
	 *
142
	 * @param int $bbcode_id Custom BBCode id
143
	 * @return array Custom BBCode user group ids
144
	 * @access public
145
	 */
146
	public function get_bbcode_group_data($bbcode_id)
147
	{
148
		$sql = 'SELECT bbcode_group
149 6
			FROM ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
150
			WHERE bbcode_id = ' . (int) $bbcode_id;
151
		$result = $this->db->sql_query($sql);
152 6
		$row = $this->db->sql_fetchrow($result);
153 6
		$this->db->sql_freeresult($result);
154 6
155 6
		return !empty($row['bbcode_group']) ? explode(',', $row['bbcode_group']) : [];
156 6
	}
157
158 6
	/**
159
	 * Get the bbcode_group data from the database,
160
	 * for every BBCode that has groups assigned
161
	 *
162
	 * @return array Custom BBCode user group ids for each BBCode, by name
163
	 * @access public
164
	 */
165
	public function get_bbcode_groups_data()
166
	{
167
		$sql = 'SELECT bbcode_tag, bbcode_group
168 1
			FROM ' . BBCODES_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
169
			WHERE bbcode_group > ''";
170
		$result = $this->db->sql_query($sql);
171 1
		$groups = [];
172 1
		while ($row = $this->db->sql_fetchrow($result))
173 1
		{
174 1
			$groups[$row['bbcode_tag']] = $row['bbcode_group'];
175 1
		}
176
		$this->db->sql_freeresult($result);
177 1
178 1
		return $groups;
179 1
	}
180
181 1
	/**
182
	 * Generate a select box containing user groups
183
	 *
184
	 * @param array $select_id The user groups to mark as selected
185
	 * @return string HTML markup of user groups select box for the form
186
	 * @access public
187
	 */
188
	public function bbcode_group_select_options(array $select_id = [])
189
	{
190
		// Get all groups except bots
191 5
		$sql = 'SELECT group_id, group_name, group_type
192
			FROM ' . GROUPS_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\GROUPS_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
193
			WHERE group_name <> 'BOTS'
194
			ORDER BY group_name ASC";
195 5
		$result = $this->db->sql_query($sql);
196
197 5
		$group_options = '';
198 5
		while ($row = $this->db->sql_fetchrow($result))
199
		{
200 5
			$selected = in_array($row['group_id'], $select_id) ? ' selected="selected"' : '';
201 5
			$group_options .= '<option value="' . $row['group_id'] . '"' . $selected . '>' . $this->group_helper->get_name($row['group_name']) . '</option>';
202
		}
203 5
		$this->db->sql_freeresult($result);
204 5
205 5
		return $group_options;
206 5
	}
207
208 5
	/**
209
	 * Resynchronize the Custom BBCodes order field
210
	 * (Originally based on Custom BBCode Sorting MOD by RMcGirr83)
211
	 *
212
	 * @access public
213
	 */
214
	public function resynchronize_bbcode_order()
215
	{
216
		$this->db->sql_transaction('begin');
217 14
218
		$sql = 'SELECT bbcode_id, bbcode_order
219 14
			FROM ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
220
			ORDER BY bbcode_order, bbcode_id';
221
		$result = $this->db->sql_query($sql);
222 14
223 14
		$order = 0;
224 14
		while ($row = $this->db->sql_fetchrow($result))
225
		{
226 14
			if (++$order !== (int) $row['bbcode_order'])
227 14
			{
228
				$this->db->sql_query($this->update_bbcode_order($row['bbcode_id'], $order));
229 14
			}
230 14
		}
231 7
		$this->db->sql_freeresult($result);
232 7
233 14
		$this->db->sql_transaction('commit');
234 14
	}
235
236 14
	/**
237 14
	 * Get the bbcode_order value for a bbcode
238
	 *
239
	 * @param int $bbcode_id ID of the bbcode
240
	 * @return int The bbcode's order
241
	 * @access protected
242
	 */
243
	protected function get_bbcode_order($bbcode_id)
244
	{
245
		$sql = 'SELECT bbcode_order
246 9
			FROM ' . BBCODES_TABLE . "
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
247
			WHERE bbcode_id = $bbcode_id";
248
		$result = $this->db->sql_query($sql);
249 9
		$bbcode_order = $this->db->sql_fetchfield('bbcode_order');
250 9
		$this->db->sql_freeresult($result);
251 9
252 9
		return (int) $bbcode_order;
253 9
	}
254
255 9
	/**
256
	 * Update the bbcode orders for bbcodes moved up/down
257
	 *
258
	 * @param int    $bbcode_order Value of the bbcode order
259
	 * @param string $action       The action move_up|move_down
260
	 * @return mixed Number of the affected rows by the last query
261
	 *                             false if no query has been run before
262
	 * @access protected
263
	 */
264
	protected function update_bbcode_orders($bbcode_order, $action)
265
	{
266
		$amount = ($action === ext::MOVE_UP) ? -1 : 1;
267 7
268
		$order_total = $bbcode_order * 2 + $amount;
269 7
270
		$sql = 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
271 7
			SET bbcode_order = ' . $order_total . ' - bbcode_order
272
			WHERE ' . $this->db->sql_in_set('bbcode_order', [
273 7
				$bbcode_order,
274 7
				$bbcode_order + $amount,
275 7
			]);
276 7
		$this->db->sql_query($sql);
277 7
278 7
		return $this->db->sql_affectedrows();
279 7
	}
280
281 7
	/**
282
	 * Build SQL query to update a bbcode order value
283
	 *
284
	 * @param int $bbcode_id    ID of the bbcode
285
	 * @param int $bbcode_order Value of the bbcode order
286
	 * @return string The SQL query to run
287
	 * @access protected
288
	 */
289
	protected function update_bbcode_order($bbcode_id, $bbcode_order)
290
	{
291
		return 'UPDATE ' . BBCODES_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
292 8
			SET bbcode_order = ' . (int) $bbcode_order . '
293
			WHERE bbcode_id = ' . (int) $bbcode_id;
294 8
	}
295 8
296 8
	/**
297
	 * Retrieve the maximum value in a column from the bbcodes table
298
	 *
299
	 * @param string $column Name of the column (bbcode_id|bbcode_order)
300
	 * @return int The maximum value in the column
301
	 * @access protected
302
	 */
303
	protected function get_max_column_value($column)
304
	{
305
		$sql = 'SELECT MAX(' . $this->db->sql_escape($column) . ') AS maximum
306 3
			FROM ' . BBCODES_TABLE;
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\BBCODES_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
307
		$result = $this->db->sql_query($sql);
308 3
		$maximum = $this->db->sql_fetchfield('maximum');
309 3
		$this->db->sql_freeresult($result);
310 3
311 3
		return (int) $maximum;
312 3
	}
313
314 3
	/**
315
	 * Send a JSON response
316
	 *
317
	 * @param bool $content The content of the JSON response (true|false)
318
	 * @access protected
319
	 */
320
	protected function send_json_response($content)
321
	{
322
		if ($this->request->is_ajax())
323 9
		{
324
			$json_response = new json_response;
325 9
			$json_response->send([
326 9
				'success' => (bool) $content,
327 3
			]);
328 3
		}
329 3
	}
330
}
331