Completed
Push — master ( e1d9a4...42f285 )
by Matt
34:19 queued 03:12
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 10
CRAP Score 3.0067

Importance

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