Completed
Pull Request — develop-3.2 (#38)
by Matt
34:37
created

acp_manager::move_drag()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 8
cts 9
cp 0.8889
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 0
crap 3.0123
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;
14
use phpbb\group\helper;
15
use phpbb\language\language;
16
use phpbb\request\request;
17
use vse\abbc3\ext;
18
19
/**
20
 * ABBC3 ACP manager class
21
 */
22
class acp_manager
23
{
24
	/** @var driver_interface */
25
	protected $db;
26
27
	/** @var helper */
28
	protected $group_helper;
29
30
	/** @var language */
31
	protected $language;
32
33
	/** @var request */
34
	protected $request;
35
36
	/**
37
	 * Constructor
38
	 *
39
	 * @param driver_interface $db
40
	 * @param helper           $group_helper
41
	 * @param language         $language
42
	 * @param request          $request
43
	 * @access public
44 34
	 */
45
	public function __construct(driver_interface $db, helper $group_helper, language $language, request $request)
46 34
	{
47 34
		$this->db = $db;
48 34
		$this->group_helper = $group_helper;
49 34
		$this->language = $language;
50 34
		$this->request = $request;
51
	}
52
53
	/**
54
	 * Move a BBCode up in order
55
	 */
56
	public function move_up()
57
	{
58 10
		$this->move(ext::MOVE_UP);
59
	}
60 10
61
	/**
62 10
	 * Move a BBCode down in order
63 10
	 */
64 1
	public function move_down()
65
	{
66
		$this->move(ext::MOVE_DOWN);
67 9
	}
68
69
	/**
70 9
	 * Update BBCode order fields in the db on move up/down
71 9
	 *
72 2
	 * @param string $action The action move_up|move_down
73
	 * @access public
74
	 */
75 7
	public function move($action)
76
	{
77 7
		$bbcode_id = $this->request->variable('id', 0);
78
79 7
		if (!check_link_hash($this->request->variable('hash', ''), $action . $bbcode_id))
80 6
		{
81
			trigger_error($this->language->lang('FORM_INVALID'), E_USER_WARNING);
82
		}
83
84
		$current_order = $this->get_bbcode_order($bbcode_id);
85
86
		// First one can't be moved up
87 3
		if ($current_order <= 1 && $action === ext::MOVE_UP)
88
		{
89 3
			return;
90 3
		}
91 1
92
		$updated = $this->update_bbcode_orders($current_order, $action);
93
94
		$this->resynchronize_bbcode_order();
95 2
96
		$this->send_json_response($updated);
97
	}
98 2
99
	/**
100
	 * Update BBCode order fields in the db on drag-n-drop
101 2
	 *
102
	 * @access public
103 2
	 */
104 2
	public function move_drag()
105
	{
106 2
		if (!$this->request->is_ajax())
107 2
		{
108 2
			return;
109
		}
110 2
111
		// Get the bbcodes html table's name
112 2
		$tablename = $this->request->variable('tablename', '');
113
114
		// Fetch the posted list
115
		$bbcodes_list = (array) $this->request->variable($tablename, array(0 => ''));
116
117
		// First one is the header, skip it
118
		unset($bbcodes_list[0]);
119
120
		$this->db->sql_transaction('begin');
121 1
		foreach ($bbcodes_list as $order => $bbcode_id)
122
		{
123 1
			$this->db->sql_query($this->update_bbcode_order($bbcode_id, $order));
124
		}
125
		$this->db->sql_transaction('commit');
126
127
		$this->resynchronize_bbcode_order();
128
129
		$this->send_json_response(true);
130
	}
131
132 3
	/**
133
	 * Retrieve the maximum value from the bbcode_order field stored in the db
134 3
	 *
135 3
	 * @return int The maximum order
136
	 * @access public
137 3
	 */
138
	public function get_max_bbcode_order()
139
	{
140
		return $this->get_max_column_value('bbcode_order');
141
	}
142
143
	/**
144
	 * Get the bbcode_group data from the posted form
145
	 *
146
	 * @return string The usergroup id numbers, comma delimited, or empty
147 6
	 * @access public
148
	 */
149
	public function get_bbcode_group_form_data()
150 6
	{
151 6
		$bbcode_group = $this->request->variable('bbcode_group', array(0));
152 6
		$bbcode_group = (!count($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group);
153 6
154 6
		return $bbcode_group;
155
	}
156 6
157
	/**
158
	 * Get the bbcode_group data from the database
159
	 *
160
	 * @param int $bbcode_id Custom BBCode id
161
	 * @return array Custom BBCode user group ids
162
	 * @access public
163
	 */
164 View Code Duplication
	public function get_bbcode_group_data($bbcode_id)
165
	{
166 1
		$sql = 'SELECT bbcode_group
167
			FROM ' . BBCODES_TABLE . '
168
			WHERE bbcode_id = ' . (int) $bbcode_id;
169 1
		$result = $this->db->sql_query($sql);
170 1
		$row = $this->db->sql_fetchrow($result);
171 1
		$this->db->sql_freeresult($result);
172 1
173 1
		return explode(',', $row['bbcode_group']);
174
	}
175 1
176 1
	/**
177 1
	 * Get the bbcode_group data from the database,
178
	 * for every BBCode that has groups assigned
179 1
	 *
180
	 * @return array Custom BBCode user group ids for each BBCode, by name
181
	 * @access public
182
	 */
183
	public function get_bbcode_groups_data()
184
	{
185
		$sql = 'SELECT bbcode_tag, bbcode_group
186
			FROM ' . BBCODES_TABLE . "
187
			WHERE bbcode_group > ''";
188
		$result = $this->db->sql_query($sql);
189 5
		$groups = array();
190
		while ($row = $this->db->sql_fetchrow($result))
191
		{
192
			$groups[$row['bbcode_tag']] = $row['bbcode_group'];
193 5
		}
194
		$this->db->sql_freeresult($result);
195 5
196 5
		return $groups;
197
	}
198 5
199 5
	/**
200
	 * Generate a select box containing user groups
201 5
	 *
202 5
	 * @param array $select_id The user groups to mark as selected
203 5
	 * @return string HTML markup of user groups select box for the form
204 5
	 * @access public
205
	 */
206 5
	public function bbcode_group_select_options(array $select_id = array())
207
	{
208
		// Get all groups except bots
209
		$sql = 'SELECT group_id, group_name, group_type
210
			FROM ' . GROUPS_TABLE . "
211
			WHERE group_name <> 'BOTS'
212
			ORDER BY group_name ASC";
213
		$result = $this->db->sql_query($sql);
214
215 14
		$group_options = '';
216
		while ($row = $this->db->sql_fetchrow($result))
217 14
		{
218
			$selected = in_array($row['group_id'], $select_id) ? ' selected="selected"' : '';
219
			$group_options .= '<option value="' . $row['group_id'] . '"' . $selected . '>' . $this->group_helper->get_name($row['group_name']) . '</option>';
220 14
		}
221 14
		$this->db->sql_freeresult($result);
222 14
223
		return $group_options;
224 14
	}
225 14
226
	/**
227 14
	 * Resynchronize the Custom BBCodes order field
228 14
	 * (Originally based on Custom BBCode Sorting MOD by RMcGirr83)
229 7
	 *
230 7
	 * @access public
231 14
	 */
232 14
	public function resynchronize_bbcode_order()
233
	{
234 14
		$this->db->sql_transaction('begin');
235 14
236
		$sql = 'SELECT bbcode_id, bbcode_order
237
			FROM ' . BBCODES_TABLE . '
238
			ORDER BY bbcode_order, bbcode_id';
239
		$result = $this->db->sql_query($sql);
240
241
		$order = 0;
242
		while ($row = $this->db->sql_fetchrow($result))
243
		{
244 9
			if (++$order != $row['bbcode_order'])
245
			{
246
				$this->db->sql_query($this->update_bbcode_order($row['bbcode_id'], $order));
247 9
			}
248 9
		}
249 9
		$this->db->sql_freeresult($result);
250 9
251 9
		$this->db->sql_transaction('commit');
252
	}
253 9
254
	/**
255
	 * Get the bbcode_order value for a bbcode
256
	 *
257
	 * @param int $bbcode_id ID of the bbcode
258
	 * @return int The bbcode's order
259
	 * @access protected
260
	 */
261
	protected function get_bbcode_order($bbcode_id)
262
	{
263
		$sql = 'SELECT bbcode_order
264
			FROM ' . BBCODES_TABLE . "
265 7
			WHERE bbcode_id = $bbcode_id";
266
		$result = $this->db->sql_query($sql);
267 7
		$bbcode_order = $this->db->sql_fetchfield('bbcode_order');
268
		$this->db->sql_freeresult($result);
269 7
270
		return (int) $bbcode_order;
271 7
	}
272 7
273 7
	/**
274 7
	 * Update the bbcode orders for bbcodes moved up/down
275 7
	 *
276 7
	 * @param int    $bbcode_order Value of the bbcode order
277 7
	 * @param string $action       The action move_up|move_down
278
	 * @return mixed Number of the affected rows by the last query
279 7
	 *                             false if no query has been run before
280
	 * @access protected
281
	 */
282
	protected function update_bbcode_orders($bbcode_order, $action)
283
	{
284
		$amount = ($action === ext::MOVE_UP) ? -1 : 1;
285
286
		$order_total = $bbcode_order * 2 + $amount;
287
288
		$sql = 'UPDATE ' . BBCODES_TABLE . '
289
			SET bbcode_order = ' . $order_total . ' - bbcode_order
290 8
			WHERE ' . $this->db->sql_in_set('bbcode_order', array(
291
				$bbcode_order,
292 8
				$bbcode_order + $amount,
293 8
			));
294 8
		$this->db->sql_query($sql);
295
296
		return $this->db->sql_affectedrows();
297
	}
298
299
	/**
300
	 * Build SQL query to update a bbcode order value
301
	 *
302
	 * @param int $bbcode_id    ID of the bbcode
303
	 * @param int $bbcode_order Value of the bbcode order
304 3
	 * @return string The SQL query to run
305
	 * @access protected
306 3
	 */
307 3
	protected function update_bbcode_order($bbcode_id, $bbcode_order)
308 3
	{
309 3
		return 'UPDATE ' . BBCODES_TABLE . '
310 3
			SET bbcode_order = ' . (int) $bbcode_order . '
311
			WHERE bbcode_id = ' . (int) $bbcode_id;
312 3
	}
313
314
	/**
315
	 * Retrieve the maximum value in a column from the bbcodes table
316
	 *
317
	 * @param string $column Name of the column (bbcode_id|bbcode_order)
318
	 * @return int The maximum value in the column
319
	 * @access protected
320
	 */
321 9 View Code Duplication
	protected function get_max_column_value($column)
322
	{
323 9
		$sql = 'SELECT MAX(' . $this->db->sql_escape($column) . ') AS maximum
324 9
			FROM ' . BBCODES_TABLE;
325 3
		$result = $this->db->sql_query($sql);
326 3
		$maximum = $this->db->sql_fetchfield('maximum');
327 3
		$this->db->sql_freeresult($result);
328 3
329
		return (int) $maximum;
330 6
	}
331
332
	/**
333
	 * Send a JSON response
334
	 *
335
	 * @param bool $content The content of the JSON response (true|false)
336
	 * @access protected
337
	 */
338
	protected function send_json_response($content)
339
	{
340
		if ($this->request->is_ajax())
341
		{
342
			$json_response = new \phpbb\json_response;
343
			$json_response->send(array(
344
				'success' => (bool) $content,
345
			));
346
		}
347
	}
348
}
349