Completed
Pull Request — master (#3325)
by Emanuele
11:19
created

ManageMembergroups_Controller::_settings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
dl 0
loc 11
rs 10
c 0
b 0
f 0
nc 1
nop 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
/**
4
 * Handles the administration page for membergroups.
5
 *
6
 * @name      ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause
9
 *
10
 * This file contains code covered by:
11
 * copyright:	2011 Simple Machines (http://www.simplemachines.org)
12
 * license:		BSD, See included LICENSE.TXT for terms and conditions.
13
 *
14
 * @version 1.1
15
 *
16
 */
17
18
/**
19
 * ManageMembergroups controller, administration page for membergroups.
20
 *
21
 * @package Membergroups
22
 */
23
class ManageMembergroups_Controller extends Action_Controller
24
{
25
	/**
26
	 * Main dispatcher, the en\trance point for all 'Manage Membergroup' actions.
27
	 *
28
	 * What it does:
29
	 *
30
	 * - It forwards to a function based on the given subaction, default being subaction 'index', or, without manage_membergroup
31
	 * permissions, then 'settings'.
32
	 * - Called by ?action=admin;area=membergroups.
33
	 * - Requires the manage_membergroups or the admin_forum permission.
34
	 *
35
	 * @event integrate_sa_manage_membergroups Used to add more sub actions
36
	 * @uses ManageMembergroups template.
37
	 * @uses ManageMembers language file.
38
	 * @see Action_Controller::action_index()
39
	 */
40
	public function action_index()
41
	{
42
		global $context, $txt;
43
44
		// Language and template stuff, the usual.
45
		loadLanguage('ManageMembers');
46
		loadTemplate('ManageMembergroups');
47
48
		$subActions = array(
49
			'add' => array(
50
				'controller' => $this,
51
				'function' => 'action_add',
52
				'permission' => 'manage_membergroups'),
53
			'delete' => array(
54
				'controller' => $this,
55
				'function' => 'action_delete',
56
				'permission' => 'manage_membergroups'),
57
			'edit' => array(
58
				'controller' => $this,
59
				'function' => 'action_edit',
60
				'permission' => 'manage_membergroups'),
61
			'index' => array(
62
				'controller' => $this,
63
				'function' => 'action_list',
64
				'permission' => 'manage_membergroups'),
65
			'members' => array(
66
				'controller' => 'Groups_Controller',
67
				'function' => 'action_index',
68
				'permission' => 'manage_membergroups'),
69
			'settings' => array(
70
				'controller' => $this,
71
				'function' => 'action_groupSettings_display',
72
				'permission' => 'admin_forum'),
73
		);
74
75
		$action = new Action('manage_membergroups');
76
77
		// Setup the admin tabs.
78
		$context[$context['admin_menu_name']]['tab_data'] = array(
79
			'title' => $txt['membergroups_title'],
80
			'help' => 'membergroups',
81
			'description' => $txt['membergroups_description'],
82
		);
83
84
		// Default to sub action 'index' or 'settings' depending on permissions.
85
		$subAction = isset($this->_req->query->sa) && isset($subActions[$this->_req->query->sa]) ? $this->_req->query->sa : (allowedTo('manage_membergroups') ? 'index' : 'settings');
86
87
		// Set that subaction, call integrate_sa_manage_membergroups
88
		$subAction = $action->initialize($subActions, $subAction);
89
90
		// Final items for the template
91
		$context['page_title'] = $txt['membergroups_title'];
92
		$context['sub_action'] = $subAction;
93
94
		// Call the right function.
95
		$action->dispatch($subAction);
96
	}
97
98
	/**
99
	 * Shows an overview of the current membergroups.
100
	 *
101
	 * What it does:
102
	 *
103
	 * - Called by ?action=admin;area=membergroups.
104
	 * - Requires the manage_membergroups permission.
105
	 * - Splits the membergroups in regular ones and post count based groups.
106
	 * - It also counts the number of members part of each membergroup.
107
	 *
108
	 * @event integrate_list_regular_membergroups_list
109
	 * @event integrate_list_post_count_membergroups_list
110
	 * @uses ManageMembergroups template, main.
111
	 */
112
	public function action_list()
113
	{
114
		global $txt, $scripturl, $context, $user_info;
115
116
		$context['page_title'] = $txt['membergroups_title'];
117
118
		// The first list shows the regular membergroups.
119
		$listOptions = array(
120
			'id' => 'regular_membergroups_list',
121
			'title' => $txt['membergroups_regular'],
122
			'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($this->_req->query->sort2) ? ';sort2=' . urlencode($this->_req->query->sort2) : ''),
123
			'default_sort_col' => 'name',
124
			'get_items' => array(
125
				'file' => SUBSDIR . '/Membergroups.subs.php',
126
				'function' => 'list_getMembergroups',
127
				'params' => array(
128
					'regular',
129
					$user_info['id'],
130
					allowedTo('manage_membergroups'),
131
					allowedTo('admin_forum'),
132
				),
133
			),
134
			'columns' => array(
135
				'name' => array(
136
					'header' => array(
137
						'value' => $txt['membergroups_name'],
138
					),
139
					'data' => array(
140
						'function' => function ($rowData) {
141
							global $scripturl;
142
143
							// Since the moderator group has no explicit members, no link is needed.
144
							if ($rowData['id_group'] == 3)
145
								$group_name = $rowData['group_name'];
146
							else
147
							{
148
								$group_name = sprintf('<a href="%1$s?action=admin;area=membergroups;sa=members;group=%2$d">%3$s</a>', $scripturl, $rowData['id_group'], $rowData['group_name_color']);
149
							}
150
151
							// Add a help option for moderator and administrator.
152
							if ($rowData['id_group'] == 1)
153
								$group_name .= sprintf(' (<a href="%1$s?action=quickhelp;help=membergroup_administrator" onclick="return reqOverlayDiv(this.href);" class="helpicon i-help"></a>)', $scripturl);
154
							elseif ($rowData['id_group'] == 3)
155
								$group_name .= sprintf(' (<a href="%1$s?action=quickhelp;help=membergroup_moderator" onclick="return reqOverlayDiv(this.href);" class="helpicon i-help"></a>)', $scripturl);
156
157
							return $group_name;
158
						},
159
					),
160
					'sort' => array(
161
						'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, mg.group_name',
162
						'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, mg.group_name DESC',
163
					),
164
				),
165
				'icons' => array(
166
					'header' => array(
167
						'value' => $txt['membergroups_icons'],
168
					),
169
					'data' => array(
170
						'function' => function ($rowData) {
171
							global $settings;
172
173
							if (!empty($rowData['icons'][0]) && !empty($rowData['icons'][1]))
174
								return str_repeat('<img src="' . $settings['images_url'] . '/group_icons/' . $rowData['icons'][1] . '" alt="*" />', $rowData['icons'][0]);
175
							else
176
								return '';
177
						},
178
					),
179
					'sort' => array(
180
						'default' => 'mg.icons',
181
						'reverse' => 'mg.icons DESC',
182
					)
183
				),
184
				'members' => array(
185
					'header' => array(
186
						'value' => $txt['membergroups_members_top'],
187
					),
188
					'data' => array(
189
						'function' => function ($rowData) {
190
							global $txt;
191
192
							// No explicit members for the moderator group.
193
							return $rowData['id_group'] == 3 ? $txt['membergroups_guests_na'] : comma_format($rowData['num_members']);
194
						},
195
					),
196
					'sort' => array(
197
						'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, 1',
198
						'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, 1 DESC',
199
					),
200
				),
201
				'modify' => array(
202
					'header' => array(
203
						'value' => $txt['modify'],
204
					),
205
					'data' => array(
206
						'sprintf' => array(
207
							'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
208
							'params' => array(
209
								'id_group' => false,
210
							),
211
						),
212
					),
213
				),
214
			),
215
			'additional_rows' => array(
216
				array(
217
					'position' => 'below_table_data',
218
					'class' => 'submitbutton',
219
					'value' => '<a class="linkbutton" href="' . $scripturl . '?action=admin;area=membergroups;sa=add;generalgroup">' . $txt['membergroups_add_group'] . '</a>',
220
				),
221
			),
222
		);
223
224
		createList($listOptions);
225
226
		// The second list shows the post count based groups.
227
		$listOptions = array(
228
			'id' => 'post_count_membergroups_list',
229
			'title' => $txt['membergroups_post'],
230
			'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($this->_req->query->sort) ? ';sort=' . urlencode($this->_req->query->sort) : ''),
231
			'default_sort_col' => 'required_posts',
232
			'request_vars' => array(
233
				'sort' => 'sort2',
234
				'desc' => 'desc2',
235
			),
236
			'get_items' => array(
237
				'file' => SUBSDIR . '/Membergroups.subs.php',
238
				'function' => 'list_getMembergroups',
239
				'params' => array(
240
					'post_count',
241
					$user_info['id'],
242
					allowedTo('manage_membergroups'),
243
					allowedTo('admin_forum'),
244
				),
245
			),
246
			'columns' => array(
247
				'name' => array(
248
					'header' => array(
249
						'value' => $txt['membergroups_name'],
250
					),
251
					'data' => array(
252
						'function' => function ($rowData) {
253
							global $scripturl;
254
255
							return sprintf('<a href="%1$s?action=admin;area=membergroups;sa=members;group=%2$d">%3$s</a>', $scripturl, $rowData['id_group'], $rowData['group_name_color']);
256
						},
257
					),
258
					'sort' => array(
259
						'default' => 'mg.group_name',
260
						'reverse' => 'mg.group_name DESC',
261
					),
262
				),
263
				'icons' => array(
264
					'header' => array(
265
						'value' => $txt['membergroups_icons'],
266
					),
267
					'data' => array(
268
						'function' => function ($rowData) {
269
							global $settings;
270
271
							if (!empty($rowData['icons'][0]) && !empty($rowData['icons'][1]))
272
								return str_repeat('<img src="' . $settings['images_url'] . '/group_icons/' . $rowData['icons'][1] . '" alt="*" />', $rowData['icons'][0]);
273
							else
274
								return '';
275
						},
276
					),
277
					'sort' => array(
278
						'default' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, icons',
279
						'reverse' => 'CASE WHEN mg.id_group < 4 THEN mg.id_group ELSE 4 END, icons DESC',
280
					)
281
				),
282
				'members' => array(
283
					'header' => array(
284
						'value' => $txt['membergroups_members_top'],
285
					),
286
					'data' => array(
287
						'db' => 'num_members',
288
					),
289
					'sort' => array(
290
						'default' => '1 DESC',
291
						'reverse' => '1',
292
					),
293
				),
294
				'required_posts' => array(
295
					'header' => array(
296
						'value' => $txt['membergroups_min_posts'],
297
					),
298
					'data' => array(
299
						'db' => 'min_posts',
300
					),
301
					'sort' => array(
302
						'default' => 'mg.min_posts',
303
						'reverse' => 'mg.min_posts DESC',
304
					),
305
				),
306
				'modify' => array(
307
					'header' => array(
308
						'value' => $txt['modify'],
309
					),
310
					'data' => array(
311
						'sprintf' => array(
312
							'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
313
							'params' => array(
314
								'id_group' => false,
315
							),
316
						),
317
					),
318
				),
319
			),
320
			'additional_rows' => array(
321
				array(
322
					'position' => 'below_table_data',
323
					'class' => 'submitbutton',
324
					'value' => '<a class="linkbutton" href="' . $scripturl . '?action=admin;area=membergroups;sa=add;postgroup">' . $txt['membergroups_add_group'] . '</a>',
325
				),
326
			),
327
		);
328
329
		createList($listOptions);
330
	}
331
332
	/**
333
	 * This function handles adding a membergroup and setting some initial properties.
334
	 *
335
	 * What it does:
336
	 *
337
	 * - Called by ?action=admin;area=membergroups;sa=add.
338
	 * - It requires the manage_membergroups permission.
339
	 * - Allows to use a predefined permission profile or copy one from another group.
340
	 * - Redirects to action=admin;area=membergroups;sa=edit;group=x.
341
	 *
342
	 * @event integrate_add_membergroup passed $id_group and $postCountBasedGroup
343
	 * @uses the new_group sub template of ManageMembergroups.
344
	 */
345
	public function action_add()
346
	{
347
		global $context, $txt, $modSettings;
348
349
		require_once(SUBSDIR . '/Membergroups.subs.php');
350
351
		// A form was submitted, we can start adding.
352
		if (isset($this->_req->post->group_name) && trim($this->_req->post->group_name) != '')
353
		{
354
			checkSession();
355
			validateToken('admin-mmg');
356
357
			$postCountBasedGroup = isset($this->_req->post->min_posts) && (!isset($this->_req->post->postgroup_based) || !empty($this->_req->post->postgroup_based));
358
			$group_type = !isset($this->_req->post->group_type) || $this->_req->post->group_type < 0 || $this->_req->post->group_type > 3 || ($this->_req->post->group_type == 1 && !allowedTo('admin_forum')) ? 0 : (int) $this->_req->post->group_type;
359
360
			// @todo Check for members with same name too?
361
362
			// Don't allow copying of a real privileged person!
363
			$permissionsObject = new Permissions;
364
			$illegal_permissions = $permissionsObject->getIllegalPermissions();
365
			$minposts = !empty($this->_req->post->min_posts) ? (int) $this->_req->post->min_posts : '-1';
366
367
			$id_group = createMembergroup($this->_req->post->group_name, $minposts, $group_type);
368
369
			call_integration_hook('integrate_add_membergroup', array($id_group, $postCountBasedGroup));
370
371
			// Update the post groups now, if this is a post group!
372
			if (isset($this->_req->post->min_posts))
373
			{
374
				require_once(SUBSDIR . '/Membergroups.subs.php');
375
				updatePostGroupStats();
376
			}
377
378
			// You cannot set permissions for post groups if they are disabled.
379
			if ($postCountBasedGroup && empty($modSettings['permission_enable_postgroups']))
380
				$this->_req->post->perm_type = '';
381
382
			if ($this->_req->post->perm_type == 'predefined')
383
			{
384
				// Set default permission level.
385
				require_once(SUBSDIR . '/ManagePermissions.subs.php');
386
				setPermissionLevel($this->_req->post->level, $id_group, null);
387
			}
388
			// Copy or inherit the permissions!
389
			elseif ($this->_req->post->perm_type == 'copy' || $this->_req->post->perm_type == 'inherit')
390
			{
391
				$copy_id = $this->_req->post->perm_type == 'copy' ? (int) $this->_req->post->copyperm : (int) $this->_req->post->inheritperm;
392
393
				// Are you a powerful admin?
394
				if (!allowedTo('admin_forum'))
395
				{
396
					$copy_type = membergroupById($copy_id);
397
398
					// Keep protected groups ... well, protected!
399
					if ($copy_type['group_type'] == 1)
400
						throw new Elk_Exception('membergroup_does_not_exist');
401
				}
402
403
				// Don't allow copying of a real privileged person!
404
				copyPermissions($id_group, $copy_id, $illegal_permissions);
405
				copyBoardPermissions($id_group, $copy_id);
406
407
				// Also get some membergroup information if we're copying and not copying from guests...
408
				if ($copy_id > 0 && $this->_req->post->perm_type == 'copy')
409
					updateCopiedGroup($id_group, $copy_id);
410
411
				// If inheriting say so...
412
				elseif ($this->_req->post->perm_type == 'inherit')
413
					updateInheritedGroup($id_group, $copy_id);
414
			}
415
416
			// Make sure all boards selected are stored in a proper array.
417
			$changed_boards = array();
418
			$accesses = empty($this->_req->post->boardaccess) || !is_array($this->_req->post->boardaccess) ? array() : $this->_req->post->boardaccess;
419
			$changed_boards['allow'] = array();
420
			$changed_boards['deny'] = array();
421
			$changed_boards['ignore'] = array();
422
			foreach ($accesses as $group_id => $action)
423
				$changed_boards[$action][] = (int) $group_id;
424
425
			foreach (array('allow', 'deny') as $board_action)
426
			{
427
				// Only do this if they have special access requirements.
428
				if (!empty($changed_boards[$board_action]))
429
					assignGroupToBoards($id_group, $changed_boards, $board_action);
430
			}
431
432
			// If this is joinable then set it to show group membership in people's profiles.
433
			if (empty($modSettings['show_group_membership']) && $group_type > 1)
434
				updateSettings(array('show_group_membership' => 1));
435
436
			// Rebuild the group cache.
437
			updateSettings(array(
438
				'settings_updated' => time(),
439
			));
440
441
			// We did it.
442
			logAction('add_group', array('group' => $this->_req->post->group_name), 'admin');
443
444
			// Go change some more settings.
445
			redirectexit('action=admin;area=membergroups;sa=edit;group=' . $id_group);
446
		}
447
448
		// Just show the 'add membergroup' screen.
449
		$context['page_title'] = $txt['membergroups_new_group'];
450
		$context['sub_template'] = 'new_group';
451
		$context['post_group'] = isset($this->_req->query->postgroup);
452
		$context['undefined_group'] = !isset($this->_req->query->postgroup) && !isset($this->_req->query->generalgroup);
453
		$context['allow_protected'] = allowedTo('admin_forum');
454
455
		if (!empty($modSettings['deny_boards_access']))
456
			loadLanguage('ManagePermissions');
457
458
		$context['groups'] = getBasicMembergroupData(array('globalmod'), array(), 'min_posts, id_group != {int:global_mod_group}, group_name');
459
460
		require_once(SUBSDIR . '/Boards.subs.php');
461
		$context += getBoardList();
462
463
		// Include a list of boards per category for easy toggling.
464
		foreach ($context['categories'] as $category)
465
			$context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
466
467
		createToken('admin-mmg');
468
	}
469
470
	/**
471
	 * Deleting a membergroup by URL (not implemented).
472
	 *
473
	 * What it does:
474
	 *
475
	 * - Called by ?action=admin;area=membergroups;sa=delete;group=x;session_var=y.
476
	 * - Requires the manage_membergroups permission.
477
	 * - Redirects to ?action=admin;area=membergroups.
478
	 *
479
	 * @todo look at this
480
	 */
481
	public function action_delete()
482
	{
483
		checkSession('get');
484
485
		require_once(SUBSDIR . '/Membergroups.subs.php');
486
		deleteMembergroups((int) $this->_req->query->group);
487
488
		// Go back to the membergroup index.
489
		redirectexit('action=admin;area=membergroups;');
490
	}
491
492
	/**
493
	 * Editing a membergroup.
494
	 *
495
	 * What it does:
496
	 *
497
	 * - Screen to edit a specific membergroup.
498
	 * - Called by ?action=admin;area=membergroups;sa=edit;group=x.
499
	 * - It requires the manage_membergroups permission.
500
	 * - Also handles the delete button of the edit form.
501
	 * - Redirects to ?action=admin;area=membergroups.
502
	 *
503
	 * @event integrate_save_membergroup, passed $current_group['id_group']
504
	 * @event integrate_view_membergroup
505
	 * @uses the edit_group sub template of ManageMembergroups.
506
	 */
507
	public function action_edit()
508
	{
509
		global $context, $txt, $modSettings;
510
511
		$current_group_id = isset($this->_req->query->group) ? (int) $this->_req->query->group : 0;
512
		$current_group = array();
513
514
		if (!empty($modSettings['deny_boards_access']))
515
			loadLanguage('ManagePermissions');
516
517
		require_once(SUBSDIR . '/Membergroups.subs.php');
518
519
		// Make sure this group is editable.
520
		if (!empty($current_group_id))
521
			$current_group = membergroupById($current_group_id);
522
523
		// Now, do we have a valid id?
524
		if (!allowedTo('admin_forum') && !empty($current_group_id) && $current_group['group_type'] == 1)
525
			throw new Elk_Exception('membergroup_does_not_exist', false);
526
527
		// The delete this membergroup button was pressed.
528
		if (isset($this->_req->post->delete))
529
		{
530
			checkSession();
531
			validateToken('admin-mmg');
532
533
			if (empty($current_group_id))
534
				throw new Elk_Exception('membergroup_does_not_exist', false);
535
536
			// Let's delete the group
537
			deleteMembergroups($current_group['id_group']);
538
539
			redirectexit('action=admin;area=membergroups;');
540
		}
541
		// A form was submitted with the new membergroup settings.
542
		elseif (isset($this->_req->post->save))
543
		{
544
			// Validate the session.
545
			checkSession();
546
			validateToken('admin-mmg');
547
548
			if (empty($current_group_id))
549
				throw new Elk_Exception('membergroup_does_not_exist', false);
550
551
			// Empty values will be replaced by validator values where they exist
552
			$empty_post = array('max_messages' => null, 'min_posts' => null, 'group_type' => null, 'group_desc' => '',
553
				'group_name' => '', 'group_hidden' => null, 'group_inherit' => null, 'icon_count' => null,
554
				'icon_image' => '', 'online_color' => '', 'boardaccess' => null);
555
556
			$validator = new Data_Validator();
557
558
			// Cleanup the inputs! :D
559
			$validator->sanitation_rules(array(
560
				'max_messages' => 'intval',
561
				'min_posts' => 'intval|abs',
562
				'group_type' => 'intval',
563
				'group_desc' => 'trim|Util::htmlspecialchars',
564
				'group_name' => 'trim|Util::htmlspecialchars',
565
				'group_hidden' => 'intval',
566
				'group_inherit' => 'intval',
567
				'icon_count' => 'intval',
568
				'icon_image' => 'trim|Util::htmlspecialchars',
569
				'online_color' => 'trim|valid_color',
570
			));
571
			$validator->input_processing(array(
572
				'boardaccess' => 'array',
573
			));
574
			$validator->validation_rules(array(
575
				'boardaccess' => 'contains[allow,ignore,deny]',
576
			));
577
			$validator->validate($this->_req->post);
578
579
			// Insert the clean data
580
			$our_post = array_replace((array) $this->_req->post, $empty_post, $validator->validation_data());
581
582
			// Can they really inherit from this group?
583
			$inherit_type = array();
584
			if ($our_post['group_inherit'] != -2 && !allowedTo('admin_forum'))
585
			{
586
				$inherit_type = membergroupById($our_post['group_inherit']);
587
			}
588
589
			$min_posts = $our_post['group_type'] == -1 && $our_post['min_posts'] >= 0 && $current_group['id_group'] > 3 ? $our_post['min_posts'] : ($current_group['id_group'] == 4 ? 0 : -1);
590
			$group_inherit = $current_group['id_group'] > 1 && $current_group['id_group'] != 3 && (empty($inherit_type['group_type']) || $inherit_type['group_type'] != 1) ? $our_post['group_inherit'] : -2;
591
592
			//@todo Don't set online_color for the Moderators group?
593
594
			// Do the update of the membergroup settings.
595
			$properties = array(
596
				'max_messages' => $our_post['max_messages'],
597
				'min_posts' => $min_posts,
598
				'group_type' => $our_post['group_type'] < 0 || $our_post['group_type'] > 3 || ($our_post['group_type'] == 1 && !allowedTo('admin_forum')) ? 0 : $our_post['group_type'],
599
				'hidden' => !$our_post['group_hidden'] || $min_posts != -1 || $current_group['id_group'] == 3 ? 0 : $our_post['group_hidden'],
600
				'id_parent' => $group_inherit,
601
				'current_group' => $current_group['id_group'],
602
				'group_name' => $our_post['group_name'],
603
				'online_color' => $our_post['online_color'],
604
				'icons' => $our_post['icon_count'] <= 0 ? '' : min($our_post['icon_count'], 10) . '#' . $our_post['icon_image'],
605
				// /me wonders why admin is *so* special
606
				'description' => $current_group['id_group'] == 1 || $our_post['group_type'] != -1 ? $our_post['group_desc'] : '',
607
			);
608
			updateMembergroupProperties($properties);
609
610
			call_integration_hook('integrate_save_membergroup', array($current_group['id_group']));
611
612
			// Time to update the boards this membergroup has access to.
613
			if ($current_group['id_group'] == 2 || $current_group['id_group'] > 3)
614
			{
615
				$changed_boards = array();
616
				$changed_boards['allow'] = array();
617
				$changed_boards['deny'] = array();
618
				$changed_boards['ignore'] = array();
619
620
				if ($our_post['boardaccess'])
621
				{
622
					foreach ($our_post['boardaccess'] as $group_id => $action)
623
					{
624
						$changed_boards[$action][] = (int) $group_id;
625
					}
626
				}
627
628
				foreach (array('allow', 'deny') as $board_action)
629
				{
630
					// Find all board this group is in, but shouldn't be in.
631
					detachGroupFromBoards($current_group['id_group'], $changed_boards, $board_action);
632
633
					// Add the membergroup to all boards that hadn't been set yet.
634
					if (!empty($changed_boards[$board_action]))
635
						assignGroupToBoards($current_group['id_group'], $changed_boards, $board_action);
636
				}
637
			}
638
639
			// Remove everyone from this group!
640
			if ($min_posts != -1)
641
				detachDeletedGroupFromMembers($current_group['id_group']);
642
			elseif ($current_group['id_group'] != 3)
643
			{
644
				// Making it a hidden group? If so remove everyone with it as primary group (Actually, just make them additional).
645
				if ($our_post['group_hidden'] == 2)
646
					setGroupToHidden($current_group['id_group']);
647
648
				// Either way, let's check our "show group membership" setting is correct.
649
				validateShowGroupMembership();
650
			}
651
652
			// Do we need to set inherited permissions?
653
			if ($group_inherit != -2 && $group_inherit != $this->_req->post->old_inherit)
654
			{
655
				$permissionsObject = new Permissions;
656
				$permissionsObject->updateChild($group_inherit);
657
			}
658
659
			// Lastly, moderators!
660
			$moderator_string = isset($this->_req->post->group_moderators) ? trim($this->_req->post->group_moderators) : '';
661
			detachGroupModerators($current_group['id_group']);
662
663
			if ((!empty($moderator_string) || !empty($this->_req->post->moderator_list)) && $min_posts == -1 && $current_group['id_group'] != 3)
664
			{
665
				// Get all the usernames from the string
666
				if (!empty($moderator_string))
667
				{
668
					$moderator_string = strtr(preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', htmlspecialchars($moderator_string, ENT_QUOTES, 'UTF-8')), array('&quot;' => '"'));
669
					preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
670
					$moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
671
					for ($k = 0, $n = count($moderators); $k < $n; $k++)
672
					{
673
						$moderators[$k] = trim($moderators[$k]);
674
675
						if (strlen($moderators[$k]) == 0)
676
							unset($moderators[$k]);
677
					}
678
679
					// Find all the id_member's for the member_name's in the list.
680
					if (!empty($moderators))
681
						$group_moderators = getIDMemberFromGroupModerators($moderators);
682
				}
683
				else
684
				{
685
					$moderators = array();
686
					foreach ($this->_req->post->moderator_list as $moderator)
687
						$moderators[] = (int) $moderator;
688
689
					$group_moderators = array();
690
					if (!empty($moderators))
691
					{
692
						require_once(SUBSDIR . '/Members.subs.php');
693
						$members = getBasicMemberData($moderators);
694
						foreach ($members as $member)
695
							$group_moderators[] = $member['id_member'];
696
					}
697
				}
698
699
				// Found some?
700
				if (!empty($group_moderators))
701
					assignGroupModerators($current_group['id_group'], $group_moderators);
702
			}
703
704
			// There might have been some post group changes.
705
			require_once(SUBSDIR . '/Membergroups.subs.php');
706
			updatePostGroupStats();
707
708
			// We've definitely changed some group stuff.
709
			updateSettings(array(
710
				'settings_updated' => time(),
711
			));
712
713
			// Log the edit.
714
			logAction('edited_group', array('group' => $our_post['group_name']), 'admin');
715
716
			redirectexit('action=admin;area=membergroups');
717
		}
718
719
		// Fetch the current group information.
720
		$row = membergroupById($current_group['id_group'], true);
721
722
		if (empty($row) || (!allowedTo('admin_forum') && $row['group_type'] == 1))
723
			throw new Elk_Exception('membergroup_does_not_exist', false);
724
725
		$row['icons'] = explode('#', $row['icons']);
726
727
		$context['group'] = array(
728
			'id' => $row['id_group'],
729
			'name' => $row['group_name'],
730
			'description' => htmlspecialchars($row['description'], ENT_COMPAT, 'UTF-8'),
731
			'editable_name' => $row['group_name'],
732
			'color' => $row['online_color'],
733
			'min_posts' => $row['min_posts'],
734
			'max_messages' => $row['max_messages'],
735
			'icon_count' => (int) $row['icons'][0],
736
			'icon_image' => isset($row['icons'][1]) ? $row['icons'][1] : '',
737
			'is_post_group' => $row['min_posts'] != -1,
738
			'type' => $row['min_posts'] != -1 ? 0 : $row['group_type'],
739
			'hidden' => $row['min_posts'] == -1 ? $row['hidden'] : 0,
740
			'inherited_from' => $row['id_parent'],
741
			'allow_post_group' => $row['id_group'] == 2 || $row['id_group'] > 4,
742
			'allow_delete' => $row['id_group'] == 2 || $row['id_group'] > 4,
743
			'allow_protected' => allowedTo('admin_forum'),
744
		);
745
746
		// Get any moderators for this group
747
		$context['group']['moderators'] = getGroupModerators($row['id_group']);
748
		$context['group']['moderator_list'] = empty($context['group']['moderators']) ? '' : '&quot;' . implode('&quot;, &quot;', $context['group']['moderators']) . '&quot;';
749
750
		if (!empty($context['group']['moderators']))
751
			list ($context['group']['last_moderator_id']) = array_slice(array_keys($context['group']['moderators']), -1);
752
753
		// Get a list of boards this membergroup is allowed to see.
754
		$context['boards'] = array();
755
		if ($row['id_group'] == 2 || $row['id_group'] > 3)
756
		{
757
			require_once(SUBSDIR . '/Boards.subs.php');
758
			$context += getBoardList(array('override_permissions' => true, 'access' => $row['id_group'], 'not_redirection' => true));
759
760
			// Include a list of boards per category for easy toggling.
761
			foreach ($context['categories'] as $category)
762
				$context['categories'][$category['id']]['child_ids'] = array_keys($category['boards']);
763
		}
764
765
		// Finally, get all the groups this could be inherited off.
766
		$context['inheritable_groups'] = getInheritableGroups($row['id_group']);
767
768
		call_integration_hook('integrate_view_membergroup');
769
770
		$context['sub_template'] = 'edit_group';
771
		$context['page_title'] = $txt['membergroups_edit_group'];
772
773
		// Use the autosuggest script when needed
774
		if ($context['group']['id'] != 3 && $context['group']['id'] != 4)
775
			loadJavascriptFile('suggest.js', array('defer' => true));
776
777
		createToken('admin-mmg');
778
	}
779
780
	/**
781
	 * Set some general membergroup settings and permissions.
782
	 *
783
	 * What it does:
784
	 *
785
	 * - Called by ?action=admin;area=membergroups;sa=settings
786
	 * - Requires the admin_forum permission (and manage_permissions for changing permissions)
787
	 * - Redirects to itself.
788
	 *
789
	 * @event integrate_save_membergroup_settings
790
	 * @uses membergroup_settings sub template of ManageMembergroups.
791
	 */
792
	public function action_groupSettings_display()
793
	{
794
		global $context, $scripturl, $txt;
795
796
		$context['sub_template'] = 'show_settings';
797
		$context['page_title'] = $txt['membergroups_settings'];
798
799
		// initialize the form
800
		// Instantiate the form
801
		$settingsForm = new Settings_Form(Settings_Form::DB_ADAPTER);
802
803
		// Initialize it with our settings
804
		$settingsForm->setConfigVars($this->_settings());
805
806
		if (isset($this->_req->query->save))
807
		{
808
			checkSession();
809
			call_integration_hook('integrate_save_membergroup_settings');
810
811
			// Yeppers, saving this...
812
			$settingsForm->setConfigValues((array) $this->_req->post);
813
			$settingsForm->save();
814
			redirectexit('action=admin;area=membergroups;sa=settings');
815
		}
816
817
		// Some simple context.
818
		$context['post_url'] = $scripturl . '?action=admin;area=membergroups;save;sa=settings';
819
		$context['settings_title'] = $txt['membergroups_settings'];
820
821
		$settingsForm->prepare();
822
	}
823
824
	/**
825
	 * Return the configuration settings for membergroups management.
826
	 *
827
	 * @event integrate_modify_membergroup_settings
828
	 */
829 1
	private function _settings()
830
	{
831
		// Only one thing here!
832
		$config_vars = array(
833 1
			array('permissions', 'manage_membergroups'),
834 1
		);
835
836
		// Add new settings with a nice hook, makes them available for admin settings search as well
837 1
		call_integration_hook('integrate_modify_membergroup_settings', array(&$config_vars));
838
839 1
		return $config_vars;
840
	}
841
842
	/**
843
	 * Return the form settings for use in admin search
844
	 */
845 1
	public function settings_search()
846
	{
847 1
		return $this->_settings();
848
	}
849
}
850