Completed
Push — master ( b031c3...d60d34 )
by Matt
06:35
created

acp_manager::install_bbcodes()   C

Complexity

Conditions 7
Paths 16

Size

Total Lines 77
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 7.0035

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 77
ccs 46
cts 48
cp 0.9583
rs 6.5755
cc 7
eloc 42
nc 16
nop 1
crap 7.0035

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
		// return a JSON response if this was an AJAX request
91 7
		if ($this->request->is_ajax())
92 7
		{
93 1
			$json_response = new \phpbb\json_response;
94 1
			$json_response->send(array(
95 1
				'success' => (bool) $this->db->sql_affectedrows(),
96 1
			));
97
		}
98 6
	}
99
100
	/**
101
	 * Update BBCode order fields in the db on drag_drop
102
	 *
103
	 * @return null
104
	 * @access public
105
	 */
106 3
	public function drag_drop()
107
	{
108 3
		if (!$this->request->is_ajax())
109 3
		{
110 1
			return;
111
		}
112
113
		// Get the bbcodes html table's name
114 2
		$tablename = $this->request->variable('tablename', '');
115
116
		// Fetch the posted list
117 2
		$bbcodes_list = $this->request->variable($tablename, array(0 => ''));
118
119 2
		$this->db->sql_transaction('begin');
120
121
		// Run through the list
122 2
		foreach ($bbcodes_list as $order => $bbcode_id)
123
		{
124
			// First one is the header, skip it
125 2
			if ($order == 0)
126 2
			{
127 1
				continue;
128
			}
129
130
			// Update the db
131 2
			$sql = 'UPDATE ' . BBCODES_TABLE . '
132 2
				SET bbcode_order = ' . $order . '
133 2
				WHERE bbcode_id = ' . (int) $bbcode_id;
134 2
			$this->db->sql_query($sql);
135 2
		}
136
137 2
		$this->db->sql_transaction('commit');
138
139
		// Resync bbcode_order
140 2
		$this->resynchronize_bbcode_order();
141
142
		// return an AJAX JSON response
143 2
		$json_response = new \phpbb\json_response;
144 2
		$json_response->send(array(
145 2
			'success' => true,
146 2
		));
147
	}
148
149
	/**
150
	 * Retrieve the maximum value from the bbcode_order field stored in the db
151
	 *
152
	 * @return int The maximum order
153
	 * @access public
154
	 */
155 1
	public function get_max_bbcode_order()
156
	{
157 1
		return $this->get_max_column_value('bbcode_order');
158
	}
159
160
	/**
161
	 * Get the bbcode_group data from the posted form
162
	 *
163
	 * @return string The usergroup id numbers, comma delimited, or empty
164
	 * @access public
165
	 */
166 3
	public function get_bbcode_group_form_data()
167
	{
168 3
		$bbcode_group = $this->request->variable('bbcode_group', array(0));
169 3
		$bbcode_group = (!sizeof($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group);
170
171 3
		return $bbcode_group;
172
	}
173
174
	/**
175
	 * Get the bbcode_group data from the database
176
	 *
177
	 * @param int $bbcode_id Custom BBCode id
178
	 * @return array Custom BBCode user group ids
179
	 * @access public
180
	 */
181 6
	public function get_bbcode_group_data($bbcode_id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
	{
183
		$sql = 'SELECT bbcode_group
184 6
			FROM ' . BBCODES_TABLE . '
185 6
			WHERE bbcode_id = ' . (int) $bbcode_id;
186 6
		$result = $this->db->sql_query($sql);
187 6
		$row = $this->db->sql_fetchrow($result);
188 6
		$this->db->sql_freeresult($result);
189
190 6
		return explode(',', $row['bbcode_group']);
191
	}
192
193
	/**
194
	 * Get the bbcode_group data from the database,
195
	 * for every BBCode that has groups assigned
196
	 *
197
	 * @return array Custom BBCode user group ids for each BBCode, by name
198
	 * @access public
199
	 */
200 1
	public function get_bbcode_groups_data()
201
	{
202
		$sql = 'SELECT bbcode_tag, bbcode_group
203 1
			FROM ' . BBCODES_TABLE . "
204 1
			WHERE bbcode_group > ''";
205 1
		$result = $this->db->sql_query($sql);
206 1
		$groups = array();
207 1
		while ($row = $this->db->sql_fetchrow($result))
208
		{
209 1
			$groups[$row['bbcode_tag']] = $row['bbcode_group'];
210 1
		}
211 1
		$this->db->sql_freeresult($result);
212
213 1
		return $groups;
214
	}
215
216
	/**
217
	 * Generate a select box containing user groups
218
	 *
219
	 * @param mixed $select_id The user groups to mark as selected
220
	 * @return string HTML markup of user groups select box for the form
221
	 * @access public
222
	 */
223 4
	public function bbcode_group_select_options($select_id = false)
224
	{
225
		// Get all groups except bots
226
		$sql = 'SELECT group_id, group_name, group_type
227 4
			FROM ' . GROUPS_TABLE . "
228
			WHERE group_name <> 'BOTS'
229 4
			ORDER BY group_name ASC";
230 4
		$result = $this->db->sql_query($sql);
231
232 4
		$group_options = '';
233 4
		while ($row = $this->db->sql_fetchrow($result))
234
		{
235 4
			$selected = (is_array($select_id)) ? ((in_array($row['group_id'], $select_id)) ? ' selected="selected"' : '') : (($row['group_id'] == $select_id) ? ' selected="selected"' : '');
236 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>';
237 4
		}
238 4
		$this->db->sql_freeresult($result);
239
240 4
		return $group_options;
241
	}
242
243
	/**
244
	 * Resynchronize the Custom BBCodes order field
245
	 * (Based on Custom BBCode Sorting MOD by RMcGirr83)
246
	 *
247
	 * @return null
248
	 * @access public
249
	 */
250 14
	public function resynchronize_bbcode_order()
251
	{
252 14
		$this->db->sql_transaction('begin');
253
254
		// By default, check that order is valid and fix it if necessary
255
		$sql = 'SELECT bbcode_id, bbcode_order
256 14
			FROM ' . BBCODES_TABLE . '
257 14
			ORDER BY bbcode_order, bbcode_id';
258 14
		$result = $this->db->sql_query($sql);
259
260 14
		if ($row = $this->db->sql_fetchrow($result))
261 14
		{
262 14
			$order = 0;
263
			do
264
			{
265
				// pre-increment $order
266 14
				++$order;
267
268 14
				if ($row['bbcode_order'] != $order)
269 14
				{
270 7
					$sql = 'UPDATE ' . BBCODES_TABLE . "
271
						SET bbcode_order = $order
272 7
						WHERE bbcode_id = {$row['bbcode_id']}";
273 7
					$this->db->sql_query($sql);
274 7
				}
275
			}
276 14
			while ($row = $this->db->sql_fetchrow($result));
277 14
		}
278 14
		$this->db->sql_freeresult($result);
279
280 14
		$this->db->sql_transaction('commit');
281 14
	}
282
283
	/**
284
	 * Increment
285
	 *
286
	 * @param string $action The action move_up|move_down
287
	 * @return int Increment amount: Move up -1. Move down +1.
288
	 * @access protected
289
	 */
290 7
	protected function increment($action)
291
	{
292 7
		return ($action == 'move_up') ? -1 : 1;
293
	}
294
295
	/**
296
	 * Retrieve the maximum value in a column from the bbcodes table
297
	 *
298
	 * @param string $column Name of the column (bbcode_id|bbcode_order)
299
	 * @return int The maximum value in the column
300
	 * @access protected
301
	 */
302 3
	protected function get_max_column_value($column)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
	{
304 3
		$sql = 'SELECT MAX(' . $this->db->sql_escape($column) . ') AS maximum
305 3
			FROM ' . BBCODES_TABLE;
306 3
		$result = $this->db->sql_query($sql);
307 3
		$maximum = $this->db->sql_fetchfield('maximum');
308 3
		$this->db->sql_freeresult($result);
309
310 3
		return (int) $maximum;
311
	}
312
}
313