Completed
Push — develop-3.2.x ( b3fe85...938ab2 )
by Matt
08:07
created

acp_manager::move()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0058

Importance

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