Completed
Push — develop-3.2 ( 433416...b85d23 )
by Matt
17s
created

acp_manager   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 311
Duplicated Lines 6.75 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.54%

Importance

Changes 0
Metric Value
wmc 27
lcom 1
cbo 0
dl 21
loc 311
ccs 119
cts 122
cp 0.9754
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B move() 0 23 4
B move_drag() 0 27 3
A get_max_bbcode_order() 0 4 1
A get_bbcode_group_form_data() 0 7 2
A get_bbcode_group_data() 11 11 1
A get_bbcode_groups_data() 0 15 2
A bbcode_group_select_options() 0 19 3
A resynchronize_bbcode_order() 0 21 3
A get_bbcode_order() 0 11 1
A update_bbcode_orders() 0 16 2
A update_bbcode_order() 0 6 1
A get_max_column_value() 10 10 1
A send_json_response() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 34
	 */
45
	public function __construct(driver_interface $db, helper $group_helper, language $language, request $request)
46 34
	{
47 34
		$this->db = $db;
48 34
		$this->group_helper = $group_helper;
49 34
		$this->language = $language;
50 34
		$this->request = $request;
51
	}
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 10
	 */
59
	public function move($action)
60 10
	{
61
		$bbcode_id = $this->request->variable('id', 0);
62 10
63 10
		if (!check_link_hash($this->request->variable('hash', ''), $action . $bbcode_id))
64 1
		{
65
			trigger_error($this->language->lang('FORM_INVALID'), E_USER_WARNING);
66
		}
67 9
68
		$current_order = $this->get_bbcode_order($bbcode_id);
69
70 9
		// First one can't be moved up
71 9
		if ($current_order <= 1 && $action === ext::MOVE_UP)
72 2
		{
73
			return;
74
		}
75 7
76
		$updated = $this->update_bbcode_orders($current_order, $action);
77 7
78
		$this->resynchronize_bbcode_order();
79 7
80 6
		$this->send_json_response($updated);
81
	}
82
83
	/**
84
	 * Update BBCode order fields in the db on drag-n-drop
85
	 *
86
	 * @access public
87 3
	 */
88
	public function move_drag()
89 3
	{
90 3
		if (!$this->request->is_ajax())
91 1
		{
92
			return;
93
		}
94
95 2
		// Get the bbcodes html table's name
96
		$tablename = $this->request->variable('tablename', '');
97
98 2
		// Fetch the posted list
99
		$bbcodes_list = (array) $this->request->variable($tablename, array(0 => ''));
100
101 2
		// First one is the header, skip it
102
		unset($bbcodes_list[0]);
103 2
104 2
		$this->db->sql_transaction('begin');
105
		foreach ($bbcodes_list as $order => $bbcode_id)
106 2
		{
107 2
			$this->db->sql_query($this->update_bbcode_order($bbcode_id, $order));
108 2
		}
109
		$this->db->sql_transaction('commit');
110 2
111
		$this->resynchronize_bbcode_order();
112 2
113
		$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 1
	 */
122
	public function get_max_bbcode_order()
123 1
	{
124
		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 3
	 */
133
	public function get_bbcode_group_form_data()
134 3
	{
135 3
		$bbcode_group = $this->request->variable('bbcode_group', array(0));
136
		$bbcode_group = (!count($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group);
137 3
138
		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 6
	 */
148 View Code Duplication
	public function get_bbcode_group_data($bbcode_id)
149
	{
150 6
		$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
		$this->db->sql_freeresult($result);
156 6
157
		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 1
	 */
167
	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 1
		$result = $this->db->sql_query($sql);
173 1
		$groups = array();
174
		while ($row = $this->db->sql_fetchrow($result))
175 1
		{
176 1
			$groups[$row['bbcode_tag']] = $row['bbcode_group'];
177 1
		}
178
		$this->db->sql_freeresult($result);
179 1
180
		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 5
	 */
190
	public function bbcode_group_select_options(array $select_id = array())
191
	{
192
		// 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
		$result = $this->db->sql_query($sql);
198 5
199 5
		$group_options = '';
200
		while ($row = $this->db->sql_fetchrow($result))
201 5
		{
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
		$this->db->sql_freeresult($result);
206 5
207
		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 14
	 */
216
	public function resynchronize_bbcode_order()
217 14
	{
218
		$this->db->sql_transaction('begin');
219
220 14
		$sql = 'SELECT bbcode_id, bbcode_order
221 14
			FROM ' . BBCODES_TABLE . '
222 14
			ORDER BY bbcode_order, bbcode_id';
223
		$result = $this->db->sql_query($sql);
224 14
225 14
		$order = 0;
226
		while ($row = $this->db->sql_fetchrow($result))
227 14
		{
228 14
			if (++$order != $row['bbcode_order'])
229 7
			{
230 7
				$this->db->sql_query($this->update_bbcode_order($row['bbcode_id'], $order));
231 14
			}
232 14
		}
233
		$this->db->sql_freeresult($result);
234 14
235 14
		$this->db->sql_transaction('commit');
236
	}
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 9
	 */
245
	protected function get_bbcode_order($bbcode_id)
246
	{
247 9
		$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
		$this->db->sql_freeresult($result);
253 9
254
		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 7
	 */
266
	protected function update_bbcode_orders($bbcode_order, $action)
267 7
	{
268
		$amount = ($action === ext::MOVE_UP) ? -1 : 1;
269 7
270
		$order_total = $bbcode_order * 2 + $amount;
271 7
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
		$this->db->sql_query($sql);
279 7
280
		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 8
	 */
291
	protected function update_bbcode_order($bbcode_id, $bbcode_order)
292 8
	{
293 8
		return 'UPDATE ' . BBCODES_TABLE . '
294 8
			SET bbcode_order = ' . (int) $bbcode_order . '
295
			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 3
	 */
305 View Code Duplication
	protected function get_max_column_value($column)
306 3
	{
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
		$this->db->sql_freeresult($result);
312 3
313
		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 9
	 */
322
	protected function send_json_response($content)
323 9
	{
324 9
		if ($this->request->is_ajax())
325 3
		{
326 3
			$json_response = new \phpbb\json_response;
327 3
			$json_response->send(array(
328 3
				'success' => (bool) $content,
329
			));
330 6
		}
331
	}
332
}
333