AddMembergroup()   F
last analyzed

Complexity

Conditions 48
Paths > 20000

Size

Total Lines 313
Code Lines 163

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 48
eloc 163
nc 4773936
nop 0
dl 0
loc 313
rs 0
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
 * This file is concerned with anything in the Manage Membergroups admin screen.
5
 *
6
 * Simple Machines Forum (SMF)
7
 *
8
 * @package SMF
9
 * @author Simple Machines https://www.simplemachines.org
10
 * @copyright 2022 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1.0
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * Main dispatcher, the entrance point for all 'Manage Membergroup' actions.
21
 * It forwards to a function based on the given subaction, default being subaction 'index', or, without manage_membergroup
22
 * permissions, then 'settings'.
23
 * Called by ?action=admin;area=membergroups.
24
 * Requires the manage_membergroups or the admin_forum permission.
25
 *
26
 * Uses ManageMembergroups template.
27
 * Uses ManageMembers language file.
28
 */
29
function ModifyMembergroups()
30
{
31
	global $context, $txt, $sourcedir;
32
33
	$subActions = array(
34
		'add' => array('AddMembergroup', 'manage_membergroups'),
35
		'delete' => array('DeleteMembergroup', 'manage_membergroups'),
36
		'edit' => array('EditMembergroup', 'manage_membergroups'),
37
		'index' => array('MembergroupIndex', 'manage_membergroups'),
38
		'members' => array('MembergroupMembers', 'manage_membergroups', 'Groups.php'),
39
		'settings' => array('ModifyMembergroupsettings', 'admin_forum'),
40
	);
41
42
	// Default to sub action 'index' or 'settings' depending on permissions.
43
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (allowedTo('manage_membergroups') ? 'index' : 'settings');
44
45
	// Is it elsewhere?
46
	if (isset($subActions[$_REQUEST['sa']][2]))
47
		require_once($sourcedir . '/' . $subActions[$_REQUEST['sa']][2]);
48
49
	// Do the permission check, you might not be allowed her.
50
	isAllowedTo($subActions[$_REQUEST['sa']][1]);
51
52
	// Language and template stuff, the usual.
53
	loadLanguage('ManageMembers');
54
	loadTemplate('ManageMembergroups');
55
56
	// Setup the admin tabs.
57
	$context[$context['admin_menu_name']]['tab_data'] = array(
58
		'title' => $txt['membergroups_title'],
59
		'help' => 'membergroups',
60
		'description' => $txt['membergroups_description'],
61
	);
62
63
	call_integration_hook('integrate_manage_membergroups', array(&$subActions));
64
65
	// Call the right function.
66
	call_helper($subActions[$_REQUEST['sa']][0]);
67
}
68
69
/**
70
 * Shows an overview of the current membergroups.
71
 * Called by ?action=admin;area=membergroups.
72
 * Requires the manage_membergroups permission.
73
 * Splits the membergroups in regular ones and post count based groups.
74
 * It also counts the number of members part of each membergroup.
75
 *
76
 * Uses ManageMembergroups template, main.
77
 */
78
function MembergroupIndex()
79
{
80
	global $txt, $scripturl, $context, $sourcedir;
81
82
	$context['page_title'] = $txt['membergroups_title'];
83
84
	// The first list shows the regular membergroups.
85
	$listOptions = array(
86
		'id' => 'regular_membergroups_list',
87
		'title' => $txt['membergroups_regular'],
88
		'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($_REQUEST['sort2']) ? ';sort2=' . urlencode($_REQUEST['sort2']) : ''),
89
		'default_sort_col' => 'name',
90
		'get_items' => array(
91
			'file' => $sourcedir . '/Subs-Membergroups.php',
92
			'function' => 'list_getMembergroups',
93
			'params' => array(
94
				'regular',
95
			),
96
		),
97
		'columns' => array(
98
			'name' => array(
99
				'header' => array(
100
					'value' => $txt['membergroups_name'],
101
				),
102
				'data' => array(
103
					'function' => function($rowData) use ($scripturl)
104
					{
105
						// Since the moderator group has no explicit members, no link is needed.
106
						if ($rowData['id_group'] == 3)
107
							$group_name = $rowData['group_name'];
108
						else
109
						{
110
							$color_style = empty($rowData['online_color']) ? '' : sprintf(' style="color: %1$s;"', $rowData['online_color']);
111
							$group_name = sprintf('<a href="%1$s?action=admin;area=membergroups;sa=members;group=%2$d"%3$s>%4$s</a>', $scripturl, $rowData['id_group'], $color_style, $rowData['group_name']);
112
						}
113
114
						// Add a help option for moderator and administrator.
115
						if ($rowData['id_group'] == 1)
116
							$group_name .= sprintf(' (<a href="%1$s?action=helpadmin;help=membergroup_administrator" onclick="return reqOverlayDiv(this.href);">?</a>)', $scripturl);
117
						elseif ($rowData['id_group'] == 3)
118
							$group_name .= sprintf(' (<a href="%1$s?action=helpadmin;help=membergroup_moderator" onclick="return reqOverlayDiv(this.href);">?</a>)', $scripturl);
119
120
						return $group_name;
121
					},
122
				),
123
				'sort' => array(
124
					'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, mg.group_name',
125
					'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, mg.group_name DESC',
126
				),
127
			),
128
			'icons' => array(
129
				'header' => array(
130
					'value' => $txt['membergroups_icons'],
131
				),
132
				'data' => array(
133
					'db' => 'icons',
134
				),
135
				'sort' => array(
136
					'default' => 'mg.icons',
137
					'reverse' => 'mg.icons DESC',
138
				)
139
			),
140
			'members' => array(
141
				'header' => array(
142
					'value' => $txt['membergroups_members_top'],
143
					'class' => 'centercol',
144
				),
145
				'data' => array(
146
					'function' => function($rowData) use ($txt)
147
					{
148
						// No explicit members for the moderator group.
149
						return $rowData['id_group'] == 3 ? $txt['membergroups_guests_na'] : comma_format($rowData['num_members']);
150
					},
151
					'class' => 'centercol',
152
				),
153
				'sort' => array(
154
					'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, 1',
155
					'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, 1 DESC',
156
				),
157
			),
158
			'modify' => array(
159
				'header' => array(
160
					'value' => $txt['modify'],
161
					'class' => 'centercol',
162
				),
163
				'data' => array(
164
					'sprintf' => array(
165
						'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
166
						'params' => array(
167
							'id_group' => false,
168
						),
169
					),
170
					'class' => 'centercol',
171
				),
172
			),
173
		),
174
		'additional_rows' => array(
175
			array(
176
				'position' => 'above_table_headers',
177
				'value' => '<a class="button" href="' . $scripturl . '?action=admin;area=membergroups;sa=add;generalgroup">' . $txt['membergroups_add_group'] . '</a>',
178
			),
179
			array(
180
				'position' => 'below_table_data',
181
				'value' => '<a class="button" href="' . $scripturl . '?action=admin;area=membergroups;sa=add;generalgroup">' . $txt['membergroups_add_group'] . '</a>',
182
			),
183
		),
184
	);
185
186
	require_once($sourcedir . '/Subs-List.php');
187
	createList($listOptions);
188
189
	// The second list shows the post count based groups.
190
	$listOptions = array(
191
		'id' => 'post_count_membergroups_list',
192
		'title' => $txt['membergroups_post'],
193
		'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($_REQUEST['sort']) ? ';sort=' . urlencode($_REQUEST['sort']) : ''),
194
		'default_sort_col' => 'required_posts',
195
		'request_vars' => array(
196
			'sort' => 'sort2',
197
			'desc' => 'desc2',
198
		),
199
		'get_items' => array(
200
			'file' => $sourcedir . '/Subs-Membergroups.php',
201
			'function' => 'list_getMembergroups',
202
			'params' => array(
203
				'post_count',
204
			),
205
		),
206
		'columns' => array(
207
			'name' => array(
208
				'header' => array(
209
					'value' => $txt['membergroups_name'],
210
				),
211
				'data' => array(
212
					'function' => function($rowData) use ($scripturl)
213
					{
214
						$colorStyle = empty($rowData['online_color']) ? '' : sprintf(' style="color: %1$s;"', $rowData['online_color']);
215
						return sprintf('<a href="%1$s?action=moderate;area=viewgroups;sa=members;group=%2$d"%3$s>%4$s</a>', $scripturl, $rowData['id_group'], $colorStyle, $rowData['group_name']);
216
					},
217
				),
218
				'sort' => array(
219
					'default' => 'mg.group_name',
220
					'reverse' => 'mg.group_name DESC',
221
				),
222
			),
223
			'icons' => array(
224
				'header' => array(
225
					'value' => $txt['membergroups_icons'],
226
				),
227
				'data' => array(
228
					'db' => 'icons',
229
				),
230
				'sort' => array(
231
					'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, icons',
232
					'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, icons DESC',
233
				)
234
			),
235
			'members' => array(
236
				'header' => array(
237
					'value' => $txt['membergroups_members_top'],
238
					'class' => 'centercol',
239
				),
240
				'data' => array(
241
					'db' => 'num_members',
242
					'class' => 'centercol',
243
				),
244
				'sort' => array(
245
					'default' => '1 DESC',
246
					'reverse' => '1',
247
				),
248
			),
249
			'required_posts' => array(
250
				'header' => array(
251
					'value' => $txt['membergroups_min_posts'],
252
					'class' => 'centercol',
253
				),
254
				'data' => array(
255
					'db' => 'min_posts',
256
					'class' => 'centercol',
257
				),
258
				'sort' => array(
259
					'default' => 'mg.min_posts',
260
					'reverse' => 'mg.min_posts DESC',
261
				),
262
			),
263
			'modify' => array(
264
				'header' => array(
265
					'value' => $txt['modify'],
266
					'class' => 'centercol',
267
				),
268
				'data' => array(
269
					'sprintf' => array(
270
						'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
271
						'params' => array(
272
							'id_group' => false,
273
						),
274
					),
275
					'class' => 'centercol',
276
				),
277
			),
278
		),
279
		'additional_rows' => array(
280
			array(
281
				'position' => 'below_table_data',
282
				'value' => '<a class="button" href="' . $scripturl . '?action=admin;area=membergroups;sa=add;postgroup">' . $txt['membergroups_add_group'] . '</a>',
283
			),
284
		),
285
	);
286
287
	createList($listOptions);
288
}
289
290
/**
291
 * This function handles adding a membergroup and setting some initial properties.
292
 * Called by ?action=admin;area=membergroups;sa=add.
293
 * It requires the manage_membergroups permission.
294
 * Allows to use a predefined permission profile or copy one from another group.
295
 * Redirects to action=admin;area=membergroups;sa=edit;group=x.
296
 *
297
 * @uses template_new_group()
298
 */
299
function AddMembergroup()
300
{
301
	global $context, $txt, $sourcedir, $modSettings, $smcFunc;
302
303
	// A form was submitted, we can start adding.
304
	if (isset($_POST['group_name']) && trim($_POST['group_name']) != '')
305
	{
306
		checkSession();
307
		validateToken('admin-mmg');
308
309
		$postCountBasedGroup = isset($_POST['min_posts']) && (!isset($_POST['postgroup_based']) || !empty($_POST['postgroup_based']));
310
		$_POST['group_type'] = !isset($_POST['group_type']) || $_POST['group_type'] < 0 || $_POST['group_type'] > 3 || ($_POST['group_type'] == 1 && !allowedTo('admin_forum')) ? 0 : (int) $_POST['group_type'];
311
312
		call_integration_hook('integrate_pre_add_membergroup', array());
313
314
		$id_group = $smcFunc['db_insert']('',
315
			'{db_prefix}membergroups',
316
			array(
317
				'description' => 'string', 'group_name' => 'string-80', 'min_posts' => 'int',
318
				'icons' => 'string', 'online_color' => 'string', 'group_type' => 'int',
319
			),
320
			array(
321
				'', $smcFunc['htmlspecialchars']($_POST['group_name'], ENT_QUOTES), ($postCountBasedGroup ? (int) $_POST['min_posts'] : '-1'),
322
				'1#icon.png', '', $_POST['group_type'],
323
			),
324
			array('id_group'),
325
			1
326
		);
327
328
		call_integration_hook('integrate_add_membergroup', array($id_group, $postCountBasedGroup));
329
330
		// Update the post groups now, if this is a post group!
331
		if (isset($_POST['min_posts']))
332
			updateStats('postgroups');
333
334
		// You cannot set permissions for post groups if they are disabled.
335
		if ($postCountBasedGroup && empty($modSettings['permission_enable_postgroups']))
336
			$_POST['perm_type'] = '';
337
338
		if ($_POST['perm_type'] == 'predefined')
339
		{
340
			// Set default permission level.
341
			require_once($sourcedir . '/ManagePermissions.php');
342
			setPermissionLevel($_POST['level'], $id_group, 'null');
343
		}
344
		// Copy or inherit the permissions!
345
		elseif ($_POST['perm_type'] == 'copy' || $_POST['perm_type'] == 'inherit')
346
		{
347
			$copy_id = $_POST['perm_type'] == 'copy' ? (int) $_POST['copyperm'] : (int) $_POST['inheritperm'];
348
349
			// Are you a powerful admin?
350
			if (!allowedTo('admin_forum'))
351
			{
352
				$request = $smcFunc['db_query']('', '
353
					SELECT group_type
354
					FROM {db_prefix}membergroups
355
					WHERE id_group = {int:copy_from}
356
					LIMIT {int:limit}',
357
					array(
358
						'copy_from' => $copy_id,
359
						'limit' => 1,
360
					)
361
				);
362
				list ($copy_type) = $smcFunc['db_fetch_row']($request);
363
				$smcFunc['db_free_result']($request);
364
365
				// Protected groups are... well, protected!
366
				if ($copy_type == 1)
367
					fatal_lang_error('membergroup_does_not_exist');
368
			}
369
370
			// Don't allow copying of a real priviledged person!
371
			require_once($sourcedir . '/ManagePermissions.php');
372
			loadIllegalPermissions();
373
374
			$request = $smcFunc['db_query']('', '
375
				SELECT permission, add_deny
376
				FROM {db_prefix}permissions
377
				WHERE id_group = {int:copy_from}',
378
				array(
379
					'copy_from' => $copy_id,
380
				)
381
			);
382
			$inserts = array();
383
			while ($row = $smcFunc['db_fetch_assoc']($request))
384
			{
385
				if (empty($context['illegal_permissions']) || !in_array($row['permission'], $context['illegal_permissions']))
386
					$inserts[] = array($id_group, $row['permission'], $row['add_deny']);
387
			}
388
			$smcFunc['db_free_result']($request);
389
390
			if (!empty($inserts))
391
				$smcFunc['db_insert']('insert',
392
					'{db_prefix}permissions',
393
					array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
394
					$inserts,
395
					array('id_group', 'permission')
396
				);
397
398
			$request = $smcFunc['db_query']('', '
399
				SELECT id_profile, permission, add_deny
400
				FROM {db_prefix}board_permissions
401
				WHERE id_group = {int:copy_from}',
402
				array(
403
					'copy_from' => $copy_id,
404
				)
405
			);
406
			$inserts = array();
407
			while ($row = $smcFunc['db_fetch_assoc']($request))
408
				$inserts[] = array($id_group, $row['id_profile'], $row['permission'], $row['add_deny']);
409
			$smcFunc['db_free_result']($request);
410
411
			if (!empty($inserts))
412
				$smcFunc['db_insert']('insert',
413
					'{db_prefix}board_permissions',
414
					array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
415
					$inserts,
416
					array('id_group', 'id_profile', 'permission')
417
				);
418
419
			// Also get some membergroup information if we're copying and not copying from guests...
420
			if ($copy_id > 0 && $_POST['perm_type'] == 'copy')
421
			{
422
				$request = $smcFunc['db_query']('', '
423
					SELECT online_color, max_messages, icons
424
					FROM {db_prefix}membergroups
425
					WHERE id_group = {int:copy_from}
426
					LIMIT 1',
427
					array(
428
						'copy_from' => $copy_id,
429
					)
430
				);
431
				$group_info = $smcFunc['db_fetch_assoc']($request);
432
				$smcFunc['db_free_result']($request);
433
434
				// ...and update the new membergroup with it.
435
				$smcFunc['db_query']('', '
436
					UPDATE {db_prefix}membergroups
437
					SET
438
						online_color = {string:online_color},
439
						max_messages = {int:max_messages},
440
						icons = {string:icons}
441
					WHERE id_group = {int:current_group}',
442
					array(
443
						'max_messages' => $group_info['max_messages'],
444
						'current_group' => $id_group,
445
						'online_color' => $group_info['online_color'],
446
						'icons' => $group_info['icons'],
447
					)
448
				);
449
			}
450
			// If inheriting say so...
451
			elseif ($_POST['perm_type'] == 'inherit')
452
			{
453
				$smcFunc['db_query']('', '
454
					UPDATE {db_prefix}membergroups
455
					SET id_parent = {int:copy_from}
456
					WHERE id_group = {int:current_group}',
457
					array(
458
						'copy_from' => $copy_id,
459
						'current_group' => $id_group,
460
					)
461
				);
462
			}
463
		}
464
465
		// Make sure all boards selected are stored in a proper array.
466
		$accesses = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
467
		$changed_boards['allow'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$changed_boards was never initialized. Although not strictly required by PHP, it is generally a good practice to add $changed_boards = array(); before regardless.
Loading history...
468
		$changed_boards['deny'] = array();
469
		$changed_boards['ignore'] = array();
470
		foreach ($accesses as $group_id => $action)
471
			$changed_boards[$action][] = (int) $group_id;
472
473
		foreach (array('allow', 'deny') as $board_action)
474
		{
475
			// Only do this if they have special access requirements.
476
			if (!empty($changed_boards[$board_action]))
477
			{
478
				$smcFunc['db_query']('', '
479
					UPDATE {db_prefix}boards
480
					SET {raw:column} = CASE WHEN {raw:column} = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT({raw:column}, {string:comma_group}) END
481
					WHERE id_board IN ({array_int:board_list})',
482
					array(
483
						'board_list' => $changed_boards[$board_action],
484
						'blank_string' => '',
485
						'group_id_string' => (string) $id_group,
486
						'comma_group' => ',' . $id_group,
487
						'column' => $board_action == 'allow' ? 'member_groups' : 'deny_member_groups',
488
					)
489
				);
490
491
				$smcFunc['db_query']('', '
492
					DELETE FROM {db_prefix}board_permissions_view
493
					WHERE id_board IN ({array_int:board_list})
494
						AND id_group = {int:group_id}
495
						AND deny = {int:deny}',
496
					array(
497
						'board_list' => $changed_boards[$board_action],
498
						'group_id' => $id_group,
499
						'deny' => $board_action == 'allow' ? 0 : 1,
500
					)
501
				);
502
503
				$insert = array();
504
				foreach ($changed_boards[$board_action] as $board_id)
505
					$insert[] = array($id_group, $board_id, $board_action == 'allow' ? 0 : 1);
506
507
				$smcFunc['db_insert']('insert',
508
					'{db_prefix}board_permissions_view',
509
					array('id_group' => 'int', 'id_board' => 'int', 'deny' => 'int'),
510
					$insert,
511
					array('id_group', 'id_board', 'deny')
512
				);
513
			}
514
515
		}
516
517
		// If this is joinable then set it to show group membership in people's profiles.
518
		if (empty($modSettings['show_group_membership']) && $_POST['group_type'] > 1)
519
			updateSettings(array('show_group_membership' => 1));
520
521
		// Rebuild the group cache.
522
		updateSettings(array(
523
			'settings_updated' => time(),
524
		));
525
526
		// We did it.
527
		logAction('add_group', array('group' => $smcFunc['htmlspecialchars']($_POST['group_name'])), 'admin');
528
529
		// Go change some more settings.
530
		redirectexit('action=admin;area=membergroups;sa=edit;group=' . $id_group);
531
	}
532
533
	// Just show the 'add membergroup' screen.
534
	$context['page_title'] = $txt['membergroups_new_group'];
535
	$context['sub_template'] = 'new_group';
536
	$context['post_group'] = isset($_REQUEST['postgroup']);
537
	$context['undefined_group'] = !isset($_REQUEST['postgroup']) && !isset($_REQUEST['generalgroup']);
538
	$context['allow_protected'] = allowedTo('admin_forum');
539
540
	if (!empty($modSettings['deny_boards_access']))
541
		loadLanguage('ManagePermissions');
542
543
	$result = $smcFunc['db_query']('', '
544
		SELECT id_group, group_name
545
		FROM {db_prefix}membergroups
546
		WHERE (id_group > {int:moderator_group} OR id_group = {int:global_mod_group})' . (empty($modSettings['permission_enable_postgroups']) ? '
547
			AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
548
			AND group_type != {int:is_protected}') . '
549
		ORDER BY min_posts, id_group != {int:global_mod_group}, group_name',
550
		array(
551
			'moderator_group' => 3,
552
			'global_mod_group' => 2,
553
			'min_posts' => -1,
554
			'is_protected' => 1,
555
		)
556
	);
557
	$context['groups'] = array();
558
	while ($row = $smcFunc['db_fetch_assoc']($result))
559
		$context['groups'][] = array(
560
			'id' => $row['id_group'],
561
			'name' => $row['group_name']
562
		);
563
	$smcFunc['db_free_result']($result);
564
565
	$request = $smcFunc['db_query']('', '
566
		SELECT b.id_cat, c.name AS cat_name, b.id_board, b.name, b.child_level
567
		FROM {db_prefix}boards AS b
568
			LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
569
		ORDER BY board_order',
570
		array(
571
		)
572
	);
573
	$context['num_boards'] = $smcFunc['db_num_rows']($request);
574
575
	$context['categories'] = array();
576
	while ($row = $smcFunc['db_fetch_assoc']($request))
577
	{
578
		// This category hasn't been set up yet..
579
		if (!isset($context['categories'][$row['id_cat']]))
580
			$context['categories'][$row['id_cat']] = array(
581
				'id' => $row['id_cat'],
582
				'name' => $row['cat_name'],
583
				'boards' => array()
584
			);
585
586
		// Set this board up, and let the template know when it's a child.  (indent them..)
587
		$context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array(
588
			'id' => $row['id_board'],
589
			'name' => $row['name'],
590
			'child_level' => $row['child_level'],
591
			'allow' => false,
592
			'deny' => false
593
		);
594
	}
595
	$smcFunc['db_free_result']($request);
596
597
	// Now, let's sort the list of categories into the boards for templates that like that.
598
	$temp_boards = array();
599
	foreach ($context['categories'] as $category)
600
	{
601
		$temp_boards[] = array(
602
			'name' => $category['name'],
603
			'child_ids' => array_keys($category['boards'])
604
		);
605
		$temp_boards = array_merge($temp_boards, array_values($category['boards']));
606
607
		// Include a list of boards per category for easy toggling.
608
		$context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
609
	}
610
611
	createToken('admin-mmg');
612
}
613
614
/**
615
 * Deleting a membergroup by URL (not implemented).
616
 * Called by ?action=admin;area=membergroups;sa=delete;group=x;session_var=y.
617
 * Requires the manage_membergroups permission.
618
 * Redirects to ?action=admin;area=membergroups.
619
 *
620
 * @todo look at this
621
 */
622
function DeleteMembergroup()
623
{
624
	global $sourcedir;
625
626
	checkSession('get');
627
628
	require_once($sourcedir . '/Subs-Membergroups.php');
629
	$result = deleteMembergroups((int) $_REQUEST['group']);
630
	// Need to throw a warning if it went wrong, but this is the only one we have a message for...
631
	if ($result === 'group_cannot_delete_sub')
632
		fatal_lang_error('membergroups_cannot_delete_paid', false);
633
634
	// Go back to the membergroup index.
635
	redirectexit('action=admin;area=membergroups;');
636
}
637
638
/**
639
 * Editing a membergroup.
640
 * Screen to edit a specific membergroup.
641
 * Called by ?action=admin;area=membergroups;sa=edit;group=x.
642
 * It requires the manage_membergroups permission.
643
 * Also handles the delete button of the edit form.
644
 * Redirects to ?action=admin;area=membergroups.
645
 *
646
 * @uses template_edit_group()
647
 */
648
function EditMembergroup()
649
{
650
	global $context, $txt, $sourcedir, $modSettings, $smcFunc, $settings;
651
652
	$_REQUEST['group'] = isset($_REQUEST['group']) && $_REQUEST['group'] > 0 ? (int) $_REQUEST['group'] : 0;
653
654
	if (!empty($modSettings['deny_boards_access']))
655
		loadLanguage('ManagePermissions');
656
657
	// Make sure this group is editable.
658
	if (!empty($_REQUEST['group']))
659
	{
660
		$request = $smcFunc['db_query']('', '
661
			SELECT id_group
662
			FROM {db_prefix}membergroups
663
			WHERE id_group = {int:current_group}' . (allowedTo('admin_forum') ? '' : '
664
				AND group_type != {int:is_protected}') . '
665
			LIMIT {int:limit}',
666
			array(
667
				'current_group' => $_REQUEST['group'],
668
				'is_protected' => 1,
669
				'limit' => 1,
670
			)
671
		);
672
		list ($_REQUEST['group']) = $smcFunc['db_fetch_row']($request);
673
		$smcFunc['db_free_result']($request);
674
	}
675
676
	// Now, do we have a valid id?
677
	if (empty($_REQUEST['group']))
678
		fatal_lang_error('membergroup_does_not_exist', false);
679
680
	// People who can manage boards are a bit special.
681
	require_once($sourcedir . '/Subs-Members.php');
682
	$board_managers = groupsAllowedTo('manage_boards', null);
683
	$context['can_manage_boards'] = in_array($_REQUEST['group'], $board_managers['allowed']);
684
685
	// Can this group moderate any boards?
686
	$request = $smcFunc['db_query']('', '
687
		SELECT COUNT(*)
688
		FROM {db_prefix}moderator_groups
689
		WHERE id_group = {int:current_group}',
690
		array(
691
			'current_group' => $_REQUEST['group'],
692
		)
693
	);
694
695
	// Why don't we have a $smcFunc['db_result'] function?
696
	$result = $smcFunc['db_fetch_row']($request);
697
	$context['is_moderator_group'] = ($result[0] > 0);
698
	$smcFunc['db_free_result']($request);
699
700
	// The delete this membergroup button was pressed.
701
	if (isset($_POST['delete']))
702
	{
703
		checkSession();
704
		validateToken('admin-mmg');
705
706
		require_once($sourcedir . '/Subs-Membergroups.php');
707
		$result = deleteMembergroups($_REQUEST['group']);
708
		// Need to throw a warning if it went wrong, but this is the only one we have a message for...
709
		if ($result === 'group_cannot_delete_sub')
710
			fatal_lang_error('membergroups_cannot_delete_paid', false);
711
712
		redirectexit('action=admin;area=membergroups;');
713
	}
714
	// A form was submitted with the new membergroup settings.
715
	elseif (isset($_POST['save']))
716
	{
717
		// Validate the session.
718
		checkSession();
719
		validateToken('admin-mmg');
720
721
		// Can they really inherit from this group?
722
		if ($_REQUEST['group'] > 1 && $_REQUEST['group'] != 3 && isset($_POST['group_inherit']) && $_POST['group_inherit'] != -2 && !allowedTo('admin_forum'))
723
		{
724
			$request = $smcFunc['db_query']('', '
725
				SELECT group_type
726
				FROM {db_prefix}membergroups
727
				WHERE id_group = {int:inherit_from}
728
				LIMIT {int:limit}',
729
				array(
730
					'inherit_from' => $_POST['group_inherit'],
731
					'limit' => 1,
732
				)
733
			);
734
			list ($inherit_type) = $smcFunc['db_fetch_row']($request);
735
			$smcFunc['db_free_result']($request);
736
		}
737
738
		// Set variables to their proper value.
739
		$_POST['max_messages'] = isset($_POST['max_messages']) ? (int) $_POST['max_messages'] : 0;
740
		$_POST['min_posts'] = isset($_POST['min_posts']) && isset($_POST['group_type']) && $_POST['group_type'] == -1 && $_REQUEST['group'] > 3 ? abs($_POST['min_posts']) : ($_REQUEST['group'] == 4 ? 0 : -1);
741
		$_POST['icons'] = (empty($_POST['icon_count']) || $_POST['icon_count'] < 0) ? '' : min((int) $_POST['icon_count'], 99) . '#' . $_POST['icon_image'];
742
		$_POST['group_desc'] = isset($_POST['group_desc']) && ($_REQUEST['group'] == 1 || (isset($_POST['group_type']) && $_POST['group_type'] != -1)) ? trim($_POST['group_desc']) : '';
743
		$_POST['group_type'] = !isset($_POST['group_type']) || $_POST['group_type'] < 0 || $_POST['group_type'] > 3 || ($_POST['group_type'] == 1 && !allowedTo('admin_forum')) ? 0 : (int) $_POST['group_type'];
744
		$_POST['group_hidden'] = empty($_POST['group_hidden']) || $_POST['min_posts'] != -1 || $_REQUEST['group'] == 3 ? 0 : (int) $_POST['group_hidden'];
745
		$_POST['group_inherit'] = $_REQUEST['group'] > 1 && $_REQUEST['group'] != 3 && (empty($inherit_type) || $inherit_type != 1) ? (int) $_POST['group_inherit'] : -2;
746
		$_POST['group_tfa_force'] = (empty($modSettings['tfa_mode']) || $modSettings['tfa_mode'] != 2 || empty($_POST['group_tfa_force'])) ? 0 : 1;
747
748
		//@todo Don't set online_color for the Moderators group?
749
750
		// Do the update of the membergroup settings.
751
		$smcFunc['db_query']('', '
752
			UPDATE {db_prefix}membergroups
753
			SET group_name = {string:group_name}, online_color = {string:online_color},
754
				max_messages = {int:max_messages}, min_posts = {int:min_posts}, icons = {string:icons},
755
				description = {string:group_desc}, group_type = {int:group_type}, hidden = {int:group_hidden},
756
				id_parent = {int:group_inherit}, tfa_required = {int:tfa_required}
757
			WHERE id_group = {int:current_group}',
758
			array(
759
				'max_messages' => $_POST['max_messages'],
760
				'min_posts' => $_POST['min_posts'],
761
				'group_type' => $_POST['group_type'],
762
				'group_hidden' => $_POST['group_hidden'],
763
				'group_inherit' => $_POST['group_inherit'],
764
				'current_group' => (int) $_REQUEST['group'],
765
				'group_name' => $smcFunc['htmlspecialchars']($_POST['group_name']),
766
				'online_color' => $_POST['online_color'],
767
				'icons' => $_POST['icons'],
768
				'group_desc' => $smcFunc['normalize']($_POST['group_desc']),
769
				'tfa_required' => $_POST['group_tfa_force'],
770
			)
771
		);
772
773
		call_integration_hook('integrate_save_membergroup', array((int) $_REQUEST['group']));
774
775
		// Time to update the boards this membergroup has access to.
776
		if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
777
		{
778
			$accesses = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
779
780
			$changed_boards['allow'] = array();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$changed_boards was never initialized. Although not strictly required by PHP, it is generally a good practice to add $changed_boards = array(); before regardless.
Loading history...
781
			$changed_boards['deny'] = array();
782
			$changed_boards['ignore'] = array();
783
			foreach ($accesses as $group_id => $action)
784
				$changed_boards[$action][] = (int) $group_id;
785
786
			$smcFunc['db_query']('', '
787
				DELETE FROM {db_prefix}board_permissions_view
788
				WHERE id_group = {int:group_id}',
789
				array(
790
					'group_id' => (int) $_REQUEST['group'],
791
				)
792
			);
793
794
			foreach (array('allow', 'deny') as $board_action)
795
			{
796
				// Find all board this group is in, but shouldn't be in.
797
				$request = $smcFunc['db_query']('', '
798
					SELECT id_board, {raw:column}
799
					FROM {db_prefix}boards
800
					WHERE FIND_IN_SET({string:current_group}, {raw:column}) != 0' . (empty($changed_boards[$board_action]) ? '' : '
801
						AND id_board NOT IN ({array_int:board_access_list})'),
802
					array(
803
						'current_group' => (int) $_REQUEST['group'],
804
						'board_access_list' => $changed_boards[$board_action],
805
						'column' => $board_action == 'allow' ? 'member_groups' : 'deny_member_groups',
806
					)
807
				);
808
				while ($row = $smcFunc['db_fetch_assoc']($request))
809
					$smcFunc['db_query']('', '
810
						UPDATE {db_prefix}boards
811
						SET {raw:column} = {string:member_group_access}
812
						WHERE id_board = {int:current_board}',
813
						array(
814
							'current_board' => $row['id_board'],
815
							'member_group_access' => implode(',', array_diff(explode(',', $row['member_groups']), array($_REQUEST['group']))),
816
							'column' => $board_action == 'allow' ? 'member_groups' : 'deny_member_groups',
817
						)
818
					);
819
				$smcFunc['db_free_result']($request);
820
821
				// Add the membergroup to all boards that hadn't been set yet.
822
				if (!empty($changed_boards[$board_action]))
823
				{
824
					$smcFunc['db_query']('', '
825
						UPDATE {db_prefix}boards
826
						SET {raw:column} = CASE WHEN {raw:column} = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT({raw:column}, {string:comma_group}) END
827
						WHERE id_board IN ({array_int:board_list})
828
							AND FIND_IN_SET({int:current_group}, {raw:column}) = 0',
829
						array(
830
							'board_list' => $changed_boards[$board_action],
831
							'blank_string' => '',
832
							'current_group' => (int) $_REQUEST['group'],
833
							'group_id_string' => (string) (int) $_REQUEST['group'],
834
							'comma_group' => ',' . $_REQUEST['group'],
835
							'column' => $board_action == 'allow' ? 'member_groups' : 'deny_member_groups',
836
						)
837
					);
838
839
					$insert = array();
840
					foreach ($changed_boards[$board_action] as $board_id)
841
						$insert[] = array((int) $_REQUEST['group'], $board_id, $board_action == 'allow' ? 0 : 1);
842
843
					$smcFunc['db_insert']('insert',
844
						'{db_prefix}board_permissions_view',
845
						array('id_group' => 'int', 'id_board' => 'int', 'deny' => 'int'),
846
						$insert,
847
						array('id_group', 'id_board', 'deny')
848
					);
849
				}
850
			}
851
		}
852
853
		// Remove everyone from this group!
854
		if ($_POST['min_posts'] != -1)
855
		{
856
			$smcFunc['db_query']('', '
857
				UPDATE {db_prefix}members
858
				SET id_group = {int:regular_member}
859
				WHERE id_group = {int:current_group}',
860
				array(
861
					'regular_member' => 0,
862
					'current_group' => (int) $_REQUEST['group'],
863
				)
864
			);
865
866
			$request = $smcFunc['db_query']('', '
867
				SELECT id_member, additional_groups
868
				FROM {db_prefix}members
869
				WHERE FIND_IN_SET({string:current_group}, additional_groups) != 0',
870
				array(
871
					'current_group' => (int) $_REQUEST['group'],
872
				)
873
			);
874
			$updates = array();
875
			while ($row = $smcFunc['db_fetch_assoc']($request))
876
				$updates[$row['additional_groups']][] = $row['id_member'];
877
			$smcFunc['db_free_result']($request);
878
879
			foreach ($updates as $additional_groups => $memberArray)
880
				updateMemberData($memberArray, array('additional_groups' => implode(',', array_diff(explode(',', $additional_groups), array((int) $_REQUEST['group'])))));
881
882
			// Sorry, but post groups can't moderate boards
883
			$smcFunc['db_query']('', '
884
				DELETE FROM {db_prefix}moderator_groups
885
				WHERE id_group = {int:current_group}',
886
				array(
887
					'current_group' => (int) $_REQUEST['group'],
888
				)
889
			);
890
		}
891
		elseif ($_REQUEST['group'] != 3)
892
		{
893
			// Making it a hidden group? If so remove everyone with it as primary group (Actually, just make them additional).
894
			if ($_POST['group_hidden'] == 2)
895
			{
896
				$request = $smcFunc['db_query']('', '
897
					SELECT id_member, additional_groups
898
					FROM {db_prefix}members
899
					WHERE id_group = {int:current_group}
900
						AND FIND_IN_SET({int:current_group}, additional_groups) = 0',
901
					array(
902
						'current_group' => (int) $_REQUEST['group'],
903
					)
904
				);
905
				$updates = array();
906
				while ($row = $smcFunc['db_fetch_assoc']($request))
907
					$updates[$row['additional_groups']][] = $row['id_member'];
908
				$smcFunc['db_free_result']($request);
909
910
				foreach ($updates as $additional_groups => $memberArray)
911
				{
912
					$new_groups = (!empty($additional_groups) ? $additional_groups . ',' : '') . $_REQUEST['group']; // We already validated this a while ago.
913
					updateMemberData($memberArray, array('additional_groups' => $new_groups));
914
				}
915
916
				$smcFunc['db_query']('', '
917
					UPDATE {db_prefix}members
918
					SET id_group = {int:regular_member}
919
					WHERE id_group = {int:current_group}',
920
					array(
921
						'regular_member' => 0,
922
						'current_group' => $_REQUEST['group'],
923
					)
924
				);
925
926
				// Hidden groups can't moderate boards
927
				$smcFunc['db_query']('', '
928
					DELETE FROM {db_prefix}moderator_groups
929
					WHERE id_group = {int:current_group}',
930
					array(
931
						'current_group' => $_REQUEST['group'],
932
					)
933
				);
934
			}
935
936
			// Either way, let's check our "show group membership" setting is correct.
937
			$request = $smcFunc['db_query']('', '
938
				SELECT COUNT(*)
939
				FROM {db_prefix}membergroups
940
				WHERE group_type > {int:non_joinable}',
941
				array(
942
					'non_joinable' => 1,
943
				)
944
			);
945
			list ($have_joinable) = $smcFunc['db_fetch_row']($request);
946
			$smcFunc['db_free_result']($request);
947
948
			// Do we need to update the setting?
949
			if ((empty($modSettings['show_group_membership']) && $have_joinable) || (!empty($modSettings['show_group_membership']) && !$have_joinable))
950
				updateSettings(array('show_group_membership' => $have_joinable ? 1 : 0));
951
		}
952
953
		// Do we need to set inherited permissions?
954
		if ($_POST['group_inherit'] != -2 && $_POST['group_inherit'] != $_POST['old_inherit'])
955
		{
956
			require_once($sourcedir . '/ManagePermissions.php');
957
			updateChildPermissions($_POST['group_inherit']);
958
		}
959
960
		// Finally, moderators!
961
		$moderator_string = isset($_POST['group_moderators']) ? trim($_POST['group_moderators']) : '';
962
		$smcFunc['db_query']('', '
963
			DELETE FROM {db_prefix}group_moderators
964
			WHERE id_group = {int:current_group}',
965
			array(
966
				'current_group' => $_REQUEST['group'],
967
			)
968
		);
969
		if ((!empty($moderator_string) || !empty($_POST['moderator_list'])) && $_POST['min_posts'] == -1 && $_REQUEST['group'] != 3)
970
		{
971
			$group_moderators = array();
972
973
			// Get all the usernames from the string
974
			if (!empty($moderator_string))
975
			{
976
				$moderator_string = strtr(preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', $smcFunc['htmlspecialchars']($moderator_string, ENT_QUOTES)), array('&quot;' => '"'));
977
				preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
978
				$moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
979
				for ($k = 0, $n = count($moderators); $k < $n; $k++)
980
				{
981
					$moderators[$k] = trim($moderators[$k]);
982
983
					if (strlen($moderators[$k]) == 0)
984
						unset($moderators[$k]);
985
				}
986
987
				// Find all the id_member's for the member_name's in the list.
988
				if (!empty($moderators))
989
				{
990
					$request = $smcFunc['db_query']('', '
991
						SELECT id_member
992
						FROM {db_prefix}members
993
						WHERE member_name IN ({array_string:moderators}) OR real_name IN ({array_string:moderators})
994
						LIMIT {int:count}',
995
						array(
996
							'moderators' => $moderators,
997
							'count' => count($moderators),
998
						)
999
					);
1000
					while ($row = $smcFunc['db_fetch_assoc']($request))
1001
						$group_moderators[] = $row['id_member'];
1002
					$smcFunc['db_free_result']($request);
1003
				}
1004
			}
1005
1006
			if (!empty($_POST['moderator_list']))
1007
			{
1008
				$moderators = array();
1009
				foreach ($_POST['moderator_list'] as $moderator)
1010
					$moderators[] = (int) $moderator;
1011
1012
				if (!empty($moderators))
1013
				{
1014
					$request = $smcFunc['db_query']('', '
1015
						SELECT id_member
1016
						FROM {db_prefix}members
1017
						WHERE id_member IN ({array_int:moderators})
1018
						LIMIT {int:num_moderators}',
1019
						array(
1020
							'moderators' => $moderators,
1021
							'num_moderators' => count($moderators),
1022
						)
1023
					);
1024
					while ($row = $smcFunc['db_fetch_assoc']($request))
1025
						$group_moderators[] = $row['id_member'];
1026
					$smcFunc['db_free_result']($request);
1027
				}
1028
			}
1029
1030
			// Make sure we don't have any duplicates first...
1031
			$group_moderators = array_unique($group_moderators);
1032
1033
			// Found some?
1034
			if (!empty($group_moderators))
1035
			{
1036
				$mod_insert = array();
1037
				foreach ($group_moderators as $moderator)
1038
					$mod_insert[] = array($_REQUEST['group'], $moderator);
1039
1040
				$smcFunc['db_insert']('insert',
1041
					'{db_prefix}group_moderators',
1042
					array('id_group' => 'int', 'id_member' => 'int'),
1043
					$mod_insert,
1044
					array('id_group', 'id_member')
1045
				);
1046
			}
1047
		}
1048
1049
		// There might have been some post group changes.
1050
		updateStats('postgroups');
1051
		// We've definitely changed some group stuff.
1052
		updateSettings(array(
1053
			'settings_updated' => time(),
1054
		));
1055
1056
		// Log the edit.
1057
		logAction('edited_group', array('group' => $smcFunc['htmlspecialchars']($_POST['group_name'])), 'admin');
1058
1059
		redirectexit('action=admin;area=membergroups');
1060
	}
1061
1062
	// Fetch the current group information.
1063
	$request = $smcFunc['db_query']('', '
1064
		SELECT group_name, description, min_posts, online_color, max_messages, icons, group_type, hidden, id_parent, tfa_required
1065
		FROM {db_prefix}membergroups
1066
		WHERE id_group = {int:current_group}
1067
		LIMIT 1',
1068
		array(
1069
			'current_group' => (int) $_REQUEST['group'],
1070
		)
1071
	);
1072
	if ($smcFunc['db_num_rows']($request) == 0)
1073
		fatal_lang_error('membergroup_does_not_exist', false);
1074
	$row = $smcFunc['db_fetch_assoc']($request);
1075
	$smcFunc['db_free_result']($request);
1076
1077
	$row['icons'] = explode('#', $row['icons']);
1078
1079
	$context['group'] = array(
1080
		'id' => $_REQUEST['group'],
1081
		'name' => $row['group_name'],
1082
		'description' => $smcFunc['htmlspecialchars']($row['description'], ENT_QUOTES),
1083
		'editable_name' => $row['group_name'],
1084
		'color' => $row['online_color'],
1085
		'min_posts' => $row['min_posts'],
1086
		'max_messages' => $row['max_messages'],
1087
		'icon_count' => (int) $row['icons'][0],
1088
		'icon_image' => isset($row['icons'][1]) ? $row['icons'][1] : '',
1089
		'is_post_group' => $row['min_posts'] != -1,
1090
		'type' => $row['min_posts'] != -1 ? 0 : $row['group_type'],
1091
		'hidden' => $row['min_posts'] == -1 ? $row['hidden'] : 0,
1092
		'inherited_from' => $row['id_parent'],
1093
		'allow_post_group' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
1094
		'allow_delete' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
1095
		'allow_protected' => allowedTo('admin_forum'),
1096
		'tfa_required' => $row['tfa_required'],
1097
	);
1098
1099
	// Get any moderators for this group
1100
	$request = $smcFunc['db_query']('', '
1101
		SELECT mem.id_member, mem.real_name
1102
		FROM {db_prefix}group_moderators AS mods
1103
			INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
1104
		WHERE mods.id_group = {int:current_group}',
1105
		array(
1106
			'current_group' => $_REQUEST['group'],
1107
		)
1108
	);
1109
	$context['group']['moderators'] = array();
1110
	while ($row = $smcFunc['db_fetch_assoc']($request))
1111
		$context['group']['moderators'][$row['id_member']] = $row['real_name'];
1112
	$smcFunc['db_free_result']($request);
1113
1114
	$context['group']['moderator_list'] = empty($context['group']['moderators']) ? '' : '&quot;' . implode('&quot;, &quot;', $context['group']['moderators']) . '&quot;';
1115
1116
	if (!empty($context['group']['moderators']))
1117
		list ($context['group']['last_moderator_id']) = array_slice(array_keys($context['group']['moderators']), -1);
1118
1119
	// Get a list of boards this membergroup is allowed to see.
1120
	$context['boards'] = array();
1121
	if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
1122
	{
1123
		$request = $smcFunc['db_query']('', '
1124
			SELECT b.id_cat, c.name as cat_name, b.id_board, b.name, b.child_level,
1125
			FIND_IN_SET({string:current_group}, b.member_groups) != 0 AS can_access, FIND_IN_SET({string:current_group}, b.deny_member_groups) != 0 AS cannot_access
1126
			FROM {db_prefix}boards AS b
1127
				LEFT JOIN {db_prefix}categories AS c ON (c.id_cat = b.id_cat)
1128
			ORDER BY board_order',
1129
			array(
1130
				'current_group' => (int) $_REQUEST['group'],
1131
			)
1132
		);
1133
		$context['categories'] = array();
1134
		while ($row = $smcFunc['db_fetch_assoc']($request))
1135
		{
1136
			// This category hasn't been set up yet..
1137
			if (!isset($context['categories'][$row['id_cat']]))
1138
				$context['categories'][$row['id_cat']] = array(
1139
					'id' => $row['id_cat'],
1140
					'name' => $row['cat_name'],
1141
					'boards' => array()
1142
				);
1143
1144
			// Set this board up, and let the template know when it's a child.  (indent them..)
1145
			$context['categories'][$row['id_cat']]['boards'][$row['id_board']] = array(
1146
				'id' => $row['id_board'],
1147
				'name' => $row['name'],
1148
				'child_level' => $row['child_level'],
1149
				'allow' => !(empty($row['can_access']) || $row['can_access'] == 'f'),
1150
				'deny' => !(empty($row['cannot_access']) || $row['cannot_access'] == 'f'),
1151
			);
1152
		}
1153
		$smcFunc['db_free_result']($request);
1154
1155
		// Now, let's sort the list of categories into the boards for templates that like that.
1156
		$temp_boards = array();
1157
		foreach ($context['categories'] as $category)
1158
		{
1159
			$temp_boards[] = array(
1160
				'name' => $category['name'],
1161
				'child_ids' => array_keys($category['boards'])
1162
			);
1163
			$temp_boards = array_merge($temp_boards, array_values($category['boards']));
1164
1165
			// Include a list of boards per category for easy toggling.
1166
			$context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
1167
		}
1168
	}
1169
1170
	// Get a list of all the image formats we can select.
1171
	$imageExts = array('png', 'jpg', 'jpeg', 'bmp', 'gif');
1172
1173
	// Scan the directory.
1174
	$context['possible_icons'] = array();
1175
	if ($files = scandir($settings['default_theme_dir'] . '/images/membericons'))
1176
	{
1177
		// Loop through every file in the directory.
1178
		foreach ($files as $value)
1179
		{
1180
			// Grab the image extension.
1181
			$ext = pathinfo($settings['default_theme_dir'] . '/images/membericons/' . $value, PATHINFO_EXTENSION);
1182
1183
			// If the extension is not empty, and it is valid
1184
			if (!empty($ext) && in_array($ext, $imageExts))
1185
				$context['possible_icons'][] = $value;
1186
		}
1187
	}
1188
1189
	// Insert our JS, if we have possible icons.
1190
	if (!empty($context['possible_icons']))
1191
		loadJavaScriptFile('icondropdown.js', array('validate' => true, 'minimize' => true), 'smf_icondropdown');
1192
1193
	loadJavaScriptFile('suggest.js', array('defer' => false, 'minimize' => true), 'smf_suggest');
1194
1195
	// Finally, get all the groups this could be inherited off.
1196
	$request = $smcFunc['db_query']('', '
1197
		SELECT id_group, group_name
1198
		FROM {db_prefix}membergroups
1199
		WHERE id_group != {int:current_group}' .
1200
			(empty($modSettings['permission_enable_postgroups']) ? '
1201
			AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
1202
			AND group_type != {int:is_protected}') . '
1203
			AND id_group NOT IN (1, 3)
1204
			AND id_parent = {int:not_inherited}',
1205
		array(
1206
			'current_group' => (int) $_REQUEST['group'],
1207
			'min_posts' => -1,
1208
			'not_inherited' => -2,
1209
			'is_protected' => 1,
1210
		)
1211
	);
1212
	$context['inheritable_groups'] = array();
1213
	while ($row = $smcFunc['db_fetch_assoc']($request))
1214
		$context['inheritable_groups'][$row['id_group']] = $row['group_name'];
1215
	$smcFunc['db_free_result']($request);
1216
1217
	call_integration_hook('integrate_view_membergroup');
1218
1219
	$context['sub_template'] = 'edit_group';
1220
	$context['page_title'] = $txt['membergroups_edit_group'];
1221
1222
	createToken('admin-mmg');
1223
}
1224
1225
/**
1226
 * Set some general membergroup settings and permissions.
1227
 * Called by ?action=admin;area=membergroups;sa=settings
1228
 * Requires the admin_forum permission (and manage_permissions for changing permissions)
1229
 * Redirects to itself.
1230
 *
1231
 * @uses template_show_settings()
1232
 */
1233
function ModifyMembergroupsettings()
1234
{
1235
	global $context, $sourcedir, $scripturl, $txt;
1236
1237
	$context['sub_template'] = 'show_settings';
1238
	$context['page_title'] = $txt['membergroups_settings'];
1239
1240
	// Needed for the settings functions.
1241
	require_once($sourcedir . '/ManageServer.php');
1242
1243
	// Only one thing here!
1244
	$config_vars = array(
1245
		array('permissions', 'manage_membergroups'),
1246
	);
1247
1248
	call_integration_hook('integrate_modify_membergroup_settings', array(&$config_vars));
1249
1250
	if (isset($_REQUEST['save']))
1251
	{
1252
		checkSession();
1253
		call_integration_hook('integrate_save_membergroup_settings');
1254
1255
		// Yeppers, saving this...
1256
		saveDBSettings($config_vars);
1257
		$_SESSION['adm-save'] = true;
1258
		redirectexit('action=admin;area=membergroups;sa=settings');
1259
	}
1260
1261
	// Some simple context.
1262
	$context['post_url'] = $scripturl . '?action=admin;area=membergroups;save;sa=settings';
1263
	$context['settings_title'] = $txt['membergroups_settings'];
1264
1265
	// We need this for the in-line permissions
1266
	createToken('admin-mp');
1267
1268
	prepareDBSettingContext($config_vars);
1269
}
1270
1271
?>