Completed
Pull Request — development (#2979)
by Stephen
08:55
created

InlinePermissions::prepare()   C

Complexity

Conditions 10
Paths 53

Size

Total Lines 82
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 52
CRAP Score 10.0006

Importance

Changes 0
Metric Value
cc 10
eloc 43
nc 53
nop 0
dl 0
loc 82
ccs 52
cts 53
cp 0.9811
crap 10.0006
rs 5.4646
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
 * Functions to support the permissions controller
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * @version 1.1 Release Candidate 1
11
 */
12
13
namespace ElkArte\sources\subs\SettingsFormAdapter;
14
15
/**
16
 * Class to initialize inline permissions sub-form and save its settings
17
 */
18
class InlinePermissions extends Adapter
19
{
20
	/**
21
	 * @var array
22
	 */
23
	private $permissions = array();
24
25
	/**
26
	 * @var string[]
27
	 */
28
	private $permissionList = array();
29
30
	/**
31
	 * @var string[]
32
	 */
33
	private $illegal_permissions = array();
34
35
	/**
36
	 * @var string[]
37
	 */
38
	private $illegal_guest_permissions = array();
39
40
	/**
41
	 * @var int[]
42
	 */
43
	private $excluded_groups = array();
44
45
	private $permissionsObject;
46
	private $db;
47
48
	/**
49
	 * @return string[]
50
	 */
51
	public function getPermissions()
52
	{
53
		return $this->permissions;
54
	}
55
56
	/**
57
	 * @param string[] $permissions
58
	 */
59 4
	public function setPermissions($permissions)
60
	{
61 4
		$this->permissions = $permissions;
62
63
		// Load the permission list
64 4
		$this->permissionList = array_map(
65
			function ($permission)
66
			{
67 4
				return $permission[1];
68 4
			}, $this->permissions
69 4
		);
70
71
		// Load the permission settings that guests cannot have
72 4
		$this->illegal_guest_permissions = array_intersect(
73 4
			array_map(
74 4
				function ($permission)
75
				{
76 4
					return str_replace(array('_any', '_own'), '', $permission[1]);
77 4
				}, $this->permissions
78 4
			), $this->permissionsObject->getIllegalGuestPermissions()
79 4
		);
80 4
	}
81
82
	/**
83
	 * @return int[]
84
	 */
85
	public function getExcludedGroups()
86
	{
87
		return $this->excluded_groups;
88
	}
89
90
	/**
91
	 * @param int[] $excluded_groups
92
	 */
93 2
	public function setExcludedGroups($excluded_groups)
94
	{
95 2
		$this->excluded_groups = $excluded_groups;
96 2
	}
97
98
	/**
99
	 * InlinePermissions constructor.
100
	 */
101 4
	public function __construct()
102
	{
103 4
		$this->db = database();
104
105
		// Make sure they can't do certain things,
106
		// unless they have the right permissions.
107 4
		$this->permissionsObject = new \Permissions;
108 4
		$this->illegal_permissions = $this->permissionsObject->getIllegalPermissions();
109 4
	}
110
111
	/**
112
	 * Save the permissions of a form containing inline permissions.
113
	 */
114 2
	public function save()
115
	{
116
		// No permissions? Not a great deal to do here.
117 2
		if (!allowedTo('manage_permissions'))
118 2
		{
119
			return;
120
		}
121 2
		$insertRows = array();
122 2
		foreach ($this->permissionList as $permission)
123
		{
124 2
			if (!isset($_POST[$permission]))
125 2
			{
126 1
				continue;
127
			}
128
129 2
			foreach ($_POST[$permission] as $id_group => $value)
130
			{
131 2
				if (in_array($value, array('on', 'deny')) && !in_array($permission, $this->illegal_permissions))
132 2
				{
133 2
					$insertRows[] = array($permission, (int) $id_group, $value == 'on' ? 1 : 0);
134 2
				}
135 2
			}
136 2
		}
137
138
		// Remove the old permissions...
139 2
		$this->permissionsObject->deletePermissions($this->permissionList);
140
141
		// ...and replace them with new ones.
142 2
		require_once(SUBSDIR . '/ManagePermissions.subs.php');
143 2
		replacePermission($insertRows);
144
145
		// Do a full child update.
146 2
		$this->permissionsObject->updateChild(array(), -1);
147
148
		// Just in case we cached this.
149 2
		updateSettings(array('settings_updated' => time()));
150 2
	}
151
152
	/**
153
	 * Initialize a form with inline permissions settings.
154
	 * It loads a context variables for each permission.
155
	 * This function is used by several settings screens to set specific permissions.
156
	 *
157
	 * @uses ManagePermissions language
158
	 * @uses ManagePermissions template
159
	 */
160 4
	public function prepare()
161
	{
162 4
		global $modSettings;
163
164
		// No permissions? Not a great deal to do here.
165 4
		if (!allowedTo('manage_permissions'))
166 4
		{
167
			return;
168
		}
169
170
		// Load the permission settings for guests
171 4
		foreach ($this->permissions as $permission)
172
		{
173 4
			$this->context[$permission[1]] = array(
174
				-1 => array(
175 4
					'id' => -1,
176 4
					'is_postgroup' => false,
177 4
					'status' => 'off',
178 4
				),
179
				0 => array(
180 4
					'id' => 0,
181 4
					'is_postgroup' => false,
182 4
					'status' => 'off',
183 4
				),
184
			);
185 4
		}
186
187 4
		$request = $this->db->query('', '
188
			SELECT id_group, CASE WHEN add_deny = {int:denied} THEN {string:deny} ELSE {string:on} END AS status, permission
189
			FROM {db_prefix}permissions
190
			WHERE id_group IN (-1, 0)
191 4
				AND permission IN ({array_string:permissions})',
192
			array(
193 4
				'denied' => 0,
194 4
				'permissions' => $this->permissionList,
195 4
				'deny' => 'deny',
196 4
				'on' => 'on',
197
			)
198 4
		);
199 4
		while ($row = $this->db->fetch_assoc($request))
200
		{
201 2
			$this->context[$row['permission']][$row['id_group']]['status'] = $row['status'];
202 2
		}
203 4
		$this->db->free_result($request);
204
205 4
		$request = $this->db->query('', '
206
			SELECT mg.id_group, mg.group_name, mg.min_posts, COALESCE(p.add_deny, -1) AS status, p.permission
207
			FROM {db_prefix}membergroups AS mg
208
				LEFT JOIN {db_prefix}permissions AS p ON (p.id_group = mg.id_group AND p.permission IN ({array_string:permissions}))
209
			WHERE mg.id_group NOT IN (1, 3)
210 4
				AND mg.id_parent = {int:not_inherited}' . (empty($modSettings['permission_enable_postgroups']) ? '
211 4
				AND mg.min_posts = {int:min_posts}' : '') . '
212 4
			ORDER BY mg.min_posts, CASE WHEN mg.id_group < {int:newbie_group} THEN mg.id_group ELSE 4 END, mg.group_name',
213
			array(
214 4
				'not_inherited' => -2,
215 4
				'min_posts' => -1,
216 4
				'newbie_group' => 4,
217 4
				'permissions' => $this->permissionList,
218
			)
219 4
		);
220 4
		while ($row = $this->db->fetch_assoc($request))
221
		{
222
			// Initialize each permission as being 'off' until proven otherwise.
223 4
			foreach ($this->permissions as $permission)
224
			{
225 4
				if (!isset($this->context[$permission[1]][$row['id_group']]))
226 4
				{
227 4
					$this->context[$permission[1]][$row['id_group']] = array(
228 4
						'id' => $row['id_group'],
229 4
						'name' => $row['group_name'],
230 4
						'is_postgroup' => $row['min_posts'] != -1,
231 4
						'status' => 'off',
232
					);
233 4
				}
234 4
			}
235
236 4
			$this->context[$row['permission']][$row['id_group']]['status'] = empty($row['status']) ? 'deny' : ($row['status'] == 1 ? 'on' : 'off');
237 4
		}
238 4
		$this->db->free_result($request);
239 4
		$this->filterIllegalPermissions();
240 4
		$this->prepareContext();
241 4
	}
242
243
	/**
244
	 * Prepare the template by loading context
245
	 * variables for each permission.
246
	 *
247
	 * @uses ManagePermissions template
248
	 * @uses ManagePermissions languuge
249
	 */
250 4
	protected function prepareContext()
251
	{
252 4
		global $context, $txt;
253
254 4
		loadTemplate('ManagePermissions');
255 4
		loadLanguage('ManagePermissions');
256
257
		// Load the names for guests
258 4
		foreach ($this->permissions as $permission)
259
		{
260 4 View Code Duplication
			if (isset($this->context[$permission[1]][-1]))
261 4
			{
262 4
				$this->context[$permission[1]][-1]['name'] = $txt['membergroups_guests'];
263 4
			}
264 4 View Code Duplication
			if (isset($this->context[$permission[1]][0]))
265 4
			{
266 4
				$this->context[$permission[1]][0]['name'] = $txt['membergroups_members'];
267 4
			}
268 4
		}
269
270 4
		$context['permissions'] = $this->context;
271 4
	}
272
273
	/**
274
	 * Some permissions cannot be given to certain groups. Remove them.
275
	 */
276 4
	private function filterIllegalPermissions()
277
	{
278 4
		foreach ($this->permissions as $permission)
279
		{
280 4
			$excluded_groups = $this->excluded_groups;
281 4
			if (isset($permission['excluded_groups']))
282 4
			{
283 2
				$excluded_groups += $permission['excluded_groups'];
284 2
			}
285 4
			foreach ($excluded_groups as $group)
286
			{
287 2
				if (isset($this->context[$permission[1]][$group]))
288 2
				{
289 2
					unset($this->context[$permission[1]][$group]);
290 2
				}
291 4
			}
292
			// Is this permission one that guests can't have?
293 4
			if (in_array($permission[1], $this->illegal_guest_permissions))
294 4
			{
295 2
				unset($this->context[$permission[1]][-1]);
296 2
			}
297
298
			// Is this permission outright disabled?
299 4
			if (in_array($permission[1], $this->illegal_permissions))
300 4
			{
301 2
				unset($this->context[$permission[1]]);
302 2
			}
303 4
		}
304 4
	}
305
}
306