Completed
Push — master ( bb96c5...c70d21 )
by Matt
04:13 queued 44s
created

acp_manager::send_json_response()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

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