Completed
Push — develop-3.2.x ( 26b46b...e41dd4 )
by Matt
06:51
created

acp_manager::move()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4.0009

Importance

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