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