acp_manager::resynchronize_bbcode_order()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 0
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 3
rs 9.9332
c 0
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 user-group 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
		return (!count($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group);
135
	}
136 3
137 3
	/**
138
	 * Get the bbcode_group data from the database
139 3
	 *
140
	 * @param int $bbcode_id Custom BBCode id
141
	 * @return array Custom BBCode user group ids
142
	 * @access public
143
	 */
144
	public function get_bbcode_group_data($bbcode_id)
145
	{
146
		$sql = 'SELECT bbcode_group
147
			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...
148
			WHERE bbcode_id = ' . (int) $bbcode_id;
149 6
		$result = $this->db->sql_query($sql);
150
		$row = $this->db->sql_fetchrow($result);
151
		$this->db->sql_freeresult($result);
152 6
153 6
		return !empty($row['bbcode_group']) ? explode(',', $row['bbcode_group']) : [];
154 6
	}
155 6
156 6
	/**
157
	 * Get the bbcode_group data from the database,
158 6
	 * for every BBCode that has groups assigned
159
	 *
160
	 * @return array Custom BBCode user group ids for each BBCode, by name
161
	 * @access public
162
	 */
163
	public function get_bbcode_groups_data()
164
	{
165
		$sql = 'SELECT bbcode_tag, bbcode_group
166
			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...
167
			WHERE bbcode_group > ''";
168 1
		$result = $this->db->sql_query($sql);
169
		$groups = [];
170
		while ($row = $this->db->sql_fetchrow($result))
171 1
		{
172 1
			$groups[$row['bbcode_tag']] = $row['bbcode_group'];
173 1
		}
174 1
		$this->db->sql_freeresult($result);
175 1
176
		return $groups;
177 1
	}
178 1
179 1
	/**
180
	 * Generate a select box containing user groups
181 1
	 *
182
	 * @param array $select_id The user groups to mark as selected
183
	 * @return string HTML markup of user groups select box for the form
184
	 * @access public
185
	 */
186
	public function bbcode_group_select_options(array $select_id = [])
187
	{
188
		// Get all groups except bots
189
		$sql = 'SELECT group_id, group_name, group_type
190
			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...
191 5
			WHERE group_name <> 'BOTS'
192
			ORDER BY group_name ASC";
193
		$result = $this->db->sql_query($sql);
194
195 5
		$group_options = '';
196
		while ($row = $this->db->sql_fetchrow($result))
197 5
		{
198 5
			$selected = in_array($row['group_id'], $select_id) ? ' selected="selected"' : '';
199
			$group_options .= '<option value="' . $row['group_id'] . '"' . $selected . '>' . $this->group_helper->get_name($row['group_name']) . '</option>';
200 5
		}
201 5
		$this->db->sql_freeresult($result);
202
203 5
		return $group_options;
204 5
	}
205 5
206 5
	/**
207
	 * Resynchronize the Custom BBCodes order field
208 5
	 * (Originally based on Custom BBCode Sorting MOD by RMcGirr83)
209
	 *
210
	 * @access public
211
	 */
212
	public function resynchronize_bbcode_order()
213
	{
214
		$this->db->sql_transaction('begin');
215
216
		$sql = 'SELECT bbcode_id, bbcode_order
217 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...
218
			ORDER BY bbcode_order, bbcode_id';
219 14
		$result = $this->db->sql_query($sql);
220
221
		$order = 0;
222 14
		while ($row = $this->db->sql_fetchrow($result))
223 14
		{
224 14
			if (++$order !== (int) $row['bbcode_order'])
225
			{
226 14
				$this->db->sql_query($this->update_bbcode_order($row['bbcode_id'], $order));
227 14
			}
228
		}
229 14
		$this->db->sql_freeresult($result);
230 14
231 7
		$this->db->sql_transaction('commit');
232 7
	}
233 14
234 14
	/**
235
	 * Get the bbcode_order value for a bbcode
236 14
	 *
237 14
	 * @param int $bbcode_id ID of the bbcode
238
	 * @return int The bbcode's order
239
	 * @access protected
240
	 */
241
	protected function get_bbcode_order($bbcode_id)
242
	{
243
		$sql = 'SELECT bbcode_order
244
			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...
245
			WHERE bbcode_id = $bbcode_id";
246 9
		$result = $this->db->sql_query($sql);
247
		$bbcode_order = $this->db->sql_fetchfield('bbcode_order');
248
		$this->db->sql_freeresult($result);
249 9
250 9
		return (int) $bbcode_order;
251 9
	}
252 9
253 9
	/**
254
	 * Update the bbcode orders for bbcodes moved up/down
255 9
	 *
256
	 * @param int    $bbcode_order Value of the bbcode order
257
	 * @param string $action       The action move_up|move_down
258
	 * @return mixed Number of the affected rows by the last query
259
	 *                             false if no query has been run before
260
	 * @access protected
261
	 */
262
	protected function update_bbcode_orders($bbcode_order, $action)
263
	{
264
		$amount = ($action === ext::MOVE_UP) ? -1 : 1;
265
266
		$order_total = $bbcode_order * 2 + $amount;
267 7
268
		$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...
269 7
			SET bbcode_order = ' . $order_total . ' - bbcode_order
270
			WHERE ' . $this->db->sql_in_set('bbcode_order', [
271 7
				$bbcode_order,
272
				$bbcode_order + $amount,
273 7
			]);
274 7
		$this->db->sql_query($sql);
275 7
276 7
		return $this->db->sql_affectedrows();
277 7
	}
278 7
279 7
	/**
280
	 * Build SQL query to update a bbcode order value
281 7
	 *
282
	 * @param int $bbcode_id    ID of the bbcode
283
	 * @param int $bbcode_order Value of the bbcode order
284
	 * @return string The SQL query to run
285
	 * @access protected
286
	 */
287
	protected function update_bbcode_order($bbcode_id, $bbcode_order)
288
	{
289
		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...
290
			SET bbcode_order = ' . (int) $bbcode_order . '
291
			WHERE bbcode_id = ' . (int) $bbcode_id;
292 8
	}
293
294 8
	/**
295 8
	 * Retrieve the maximum value in a column from the bbcodes table
296 8
	 *
297
	 * @param string $column Name of the column (bbcode_id|bbcode_order)
298
	 * @return int The maximum value in the column
299
	 * @access protected
300
	 */
301
	protected function get_max_column_value($column)
302
	{
303
		$sql = 'SELECT MAX(' . $this->db->sql_escape($column) . ') AS maximum
304
			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...
305
		$result = $this->db->sql_query($sql);
306 3
		$maximum = $this->db->sql_fetchfield('maximum');
307
		$this->db->sql_freeresult($result);
308 3
309 3
		return (int) $maximum;
310 3
	}
311 3
312 3
	/**
313
	 * Send a JSON response
314 3
	 *
315
	 * @param bool $content The content of the JSON response (true|false)
316
	 * @access protected
317
	 */
318
	protected function send_json_response($content)
319
	{
320
		if ($this->request->is_ajax())
321
		{
322
			$json_response = new json_response;
323 9
			$json_response->send([
324
				'success' => (bool) $content,
325 9
			]);
326 9
		}
327 3
	}
328
}
329