Completed
Pull Request — master (#28)
by Matt
08:09
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 View Code Duplication
	public function get_max_bbcode_order()
0 ignored issues
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...
156
	{
157
		$sql = 'SELECT MAX(bbcode_order) AS max_bbcode_order
158 1
			FROM ' . BBCODES_TABLE;
159 1
		$result = $this->db->sql_query($sql);
160 1
		$max_order = (int) $this->db->sql_fetchfield('max_bbcode_order');
161 1
		$this->db->sql_freeresult($result);
162
163 1
		return $max_order;
164
	}
165
166
	/**
167
	 * Get the bbcode_group data from the posted form
168
	 *
169
	 * @return string The usergroup id numbers, comma delimited, or empty
170
	 * @access public
171
	 */
172 3
	public function get_bbcode_group_form_data()
173
	{
174 3
		$bbcode_group = $this->request->variable('bbcode_group', array(0));
175 3
		$bbcode_group = (!sizeof($bbcode_group)) ? $this->request->variable('bbcode_group', '') : implode(',', $bbcode_group);
176
177 3
		return $bbcode_group;
178
	}
179
180
	/**
181
	 * Get the bbcode_group data from the database
182
	 *
183
	 * @param int $bbcode_id Custom BBCode id
184
	 * @return array Custom BBCode user group ids
185
	 * @access public
186
	 */
187 6
	public function get_bbcode_group_data($bbcode_id)
188
	{
189
		$sql = 'SELECT bbcode_group
190 6
			FROM ' . BBCODES_TABLE . '
191 6
			WHERE bbcode_id = ' . (int) $bbcode_id;
192 6
		$result = $this->db->sql_query($sql);
193 6
		$row = $this->db->sql_fetchrow($result);
194 6
		$this->db->sql_freeresult($result);
195
196 6
		return explode(',', $row['bbcode_group']);
197
	}
198
199
	/**
200
	 * Get the bbcode_group data from the database,
201
	 * for every BBCode that has groups assigned
202
	 *
203
	 * @return array Custom BBCode user group ids for each BBCode, by name
204
	 * @access public
205
	 */
206 1
	public function get_bbcode_groups_data()
207
	{
208
		$sql = 'SELECT bbcode_tag, bbcode_group
209 1
			FROM ' . BBCODES_TABLE . "
210 1
			WHERE bbcode_group > ''";
211 1
		$result = $this->db->sql_query($sql);
212 1
		$groups = array();
213 1
		while ($row = $this->db->sql_fetchrow($result))
214
		{
215 1
			$groups[$row['bbcode_tag']] = $row['bbcode_group'];
216 1
		}
217 1
		$this->db->sql_freeresult($result);
218
219 1
		return $groups;
220
	}
221
222
	/**
223
	 * Generate a select box containing user groups
224
	 *
225
	 * @param mixed $select_id The user groups to mark as selected
226
	 * @return string HTML markup of user groups select box for the form
227
	 * @access public
228
	 */
229 4
	public function bbcode_group_select_options($select_id = false)
230
	{
231
		// Get all groups except bots
232
		$sql = 'SELECT group_id, group_name, group_type
233 4
			FROM ' . GROUPS_TABLE . "
234
			WHERE group_name <> 'BOTS'
235 4
			ORDER BY group_name ASC";
236 4
		$result = $this->db->sql_query($sql);
237
238 4
		$group_options = '';
239 4
		while ($row = $this->db->sql_fetchrow($result))
240
		{
241 4
			$selected = (is_array($select_id)) ? ((in_array($row['group_id'], $select_id)) ? ' selected="selected"' : '') : (($row['group_id'] == $select_id) ? ' selected="selected"' : '');
242 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>';
243 4
		}
244 4
		$this->db->sql_freeresult($result);
245
246 4
		return $group_options;
247
	}
248
249
	/**
250
	 * Resynchronize the Custom BBCodes order field
251
	 * (Based on Custom BBCode Sorting MOD by RMcGirr83)
252
	 *
253
	 * @return null
254
	 * @access public
255
	 */
256 14
	public function resynchronize_bbcode_order()
257
	{
258 14
		$this->db->sql_transaction('begin');
259
260
		// By default, check that order is valid and fix it if necessary
261
		$sql = 'SELECT bbcode_id, bbcode_order
262 14
			FROM ' . BBCODES_TABLE . '
263 14
			ORDER BY bbcode_order, bbcode_id';
264 14
		$result = $this->db->sql_query($sql);
265
266 14
		if ($row = $this->db->sql_fetchrow($result))
267 14
		{
268 14
			$order = 0;
269
			do
270
			{
271
				// pre-increment $order
272 14
				++$order;
273
274 14
				if ($row['bbcode_order'] != $order)
275 14
				{
276 7
					$sql = 'UPDATE ' . BBCODES_TABLE . "
277
						SET bbcode_order = $order
278 7
						WHERE bbcode_id = {$row['bbcode_id']}";
279 7
					$this->db->sql_query($sql);
280 7
				}
281
			}
282 14
			while ($row = $this->db->sql_fetchrow($result));
283 14
		}
284 14
		$this->db->sql_freeresult($result);
285
286 14
		$this->db->sql_transaction('commit');
287 14
	}
288
289
	/**
290
	 * Increment
291
	 *
292
	 * @param string $action The action move_up|move_down
293
	 * @return int Increment amount: Move up -1. Move down +1.
294
	 * @access protected
295
	 */
296 7
	protected function increment($action)
297
	{
298 7
		return ($action == 'move_up') ? -1 : 1;
299
	}
300
}
301