Completed
Pull Request — master (#29)
by Matt
06:29
created

acp_manager   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 309
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.52%

Importance

Changes 15
Bugs 1 Features 1
Metric Value
wmc 31
c 15
b 1
f 1
lcom 1
cbo 0
dl 0
loc 309
ccs 118
cts 121
cp 0.9752
rs 9.8

13 Methods

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