# FILE: adm-grp.pl
# DESCRIPTION: Group Manager Interfaces
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $DCONF $PARAMS);

###
### GROUP_admin
###
### Controls Group Manager and all of its functions
###

sub GROUP_admin {
	my ($FORMref) = @_;
	my $result = check_password($FORMref->{username}, undef, { type_required => 'moderator' }, $FORMref->{'COOKIE'});
	bad_login( { bad_username => 1 } ) if scalar(@{ $result }) == 0;
	bad_login( { superuser_required => 1 } ) if $result->[0]->{user} ne $DCONF->{superuser};
	group_manager($FORMref, $result->[0]->{user}) if $FORMref->{action} eq "grp_mgr";
	group_add($FORMref, $result->[0]->{user}) if $FORMref->{action} eq "grp_add";
	if ($FORMref->{'action'} eq "grp_mgmt") {
		my $sa = $FORMref->{SELECTION_action};
		my $sw = $FORMref->{SELECTION_who};
		if ($sa eq 'd') {
			my $count = group_delete($sw, $FORMref->{MARK});
			group_manager($FORMref, $result->[0]->{user}, { what => 2, count => $count });
		} elsif ($sa eq 'e') {
			group_edit_screen(($sw eq "*" ? $FORMref->{MARK} : $sw), $result->[0]->{user});
		} else {
			error_message("Group Management Error", "Error code GM001: Invalid Query", 0, 1);
		}
	}
	if ($FORMref->{action} eq "grp_mod") {
		group_edit_save($FORMref->{'affect'}, $FORMref->{'mod'});
		group_edit_screen($FORMref->{'affect'}, $result->[0]->{user}, 2);
	}
	if ($FORMref->{action} eq "grp_notify") {
		dreq("fcn-user-PRO");
		my @m = ();
		my %z = map { $_, 1 } split(/,/, $FORMref->{mod});
		foreach my $u (split(/,/, $FORMref->{mod_choice})) {
			my $emhash = {};
			$emhash->{username} = $u;
			$emhash->{action} = $z{$u} == 1 ? 'add_str' : 'rem_str';
			$emhash->{new_str} = $FORMref->{affect};
			push @m, $emhash;
		}
		new_registrations_email_moderators_config_write(\@m);
		group_edit_screen($FORMref->{affect}, $result->[0]->{user}, 1);
	}
}

###
### group_edit_save
###
### Saves group editing privileges of moderators
###

sub group_edit_save {
	my ($groups, $moderators) = @_;
	dreq("fcn-grp");
	my @actions = ({ group => $groups, moderator => $moderators, action => 'set_equal' });
	write_group_file(\@actions);
}

###
### group_edit_screen
###
### Screen where you select what moderators will belong to a group
###

sub group_edit_screen {
	my ($groups, $username, $saved_flag) = @_;
	dreq("fcn-grp");
	my $group_cache = read_group_file();
	my $mod = read_account_file("passwd", undef, { returnformat => 'single_array' });
	my @mod = ();
	my @gs = split(/,/, $groups);
	foreach my $x (@{ $mod }) {
		my $i = {};
		$i->{user} = $x->{user};
		$i->{name} = $x->{fullname};
		my $c = 0;
		foreach my $g (@gs) {
			$c += check_group_authorization($x->{user}, $g, $group_cache);
		}
		$i->{checked} = ($c >= scalar(@gs) ? 1 : 0);
		push (@mod, $i);
	}
	@mod = sort {
		return 1 if $b->{user} eq $DCONF->{superuser};
		return -1 if $a->{user} eq $DCONF->{superuser};
		return lc($a->{user}) cmp lc($b->{user});
	} @mod;
	my $subst = {};
	if ($DCONF->{pro} && scalar @gs == 1) {
		dreq("selfreg-PRO", "fcn-user-PRO");
		my $x = read_selfreg_file();
		my $param = $x->{param}->{$gs[0]}->{param};
		if ($param == 2) {
			my $hash = new_registrations_email_moderators_config_read(0, $group_cache);
			my $l = {};
			foreach my $k (keys %{$hash}) {
				my $grps = $hash->{$k}->{groups};
				next if ! defined $grps->{$gs[0]};
				$l->{$k} = 1;
			}
			foreach my $z (@mod) {
				$z->{notify_checked} = 0 + $l->{$z->{user}};
			}
			$subst->{general}->{groupmail} = 1;
		}
	}
	$subst->{moderators} = \@mod;
	$subst->{general}->{mods} = scalar(split(/,/, $groups));
	$subst->{general}->{group} = $groups;
	$subst->{general}->{username} = $username;
	$subst->{general}->{affect} = $groups;
	$subst->{general}->{saved_flag} = $saved_flag;
	screen_out("grpmodas", $subst);
}

###
### group_manager
###
### Main interface of the Group Manager screen
###

sub group_manager {
	my ($FORMref, $username, $stuff) = @_;
	dreq("fcn-grp");
	my $group_file = read_group_file(1);
	my @groups = ();
	foreach my $g (@{ $group_file->{group_list} }) {
		next if $g eq "#" || $g !~ m|\S|;
		my $i = {};
		$i->{group_name} = $g;
		$i->{mod_count} = scalar(keys(%{ $group_file->{by_grp}->{$g} }));
		push (@groups, $i);
	}
	@groups = sort { lc($a->{group_name}) cmp lc($b->{group_name}) } @groups;
	my $subst = {};
	$subst->{'general'}->{'url'} = "$PARAMS->{cgiurl}?action=grp_mgr&username=$username";
	$subst->{'general'}->{'menu'} = $FORMref->{'menu'};
	$subst->{'general'}->{'username'} = $username;
	$subst->{'groups'} = \@groups;
	$subst->{'stuff'} = $stuff;
	screen_out("groupmgr", $subst);
}

###
### group_add
###
### Adds a group
###

sub group_add {
	my ($FORMref, $username) = @_;
	undef my $subst;
	$subst->{'general'}->{'url'} = "$PARAMS->{cgiurl}?action=grp_mgr&username=$username";
	$subst->{'general'}->{'menu'} = 1;
	$subst->{'general'}->{'username'} = $username;
	my $y = prepare_userpass($FORMref->{group});
	$y =~ s/\W//g;
	if (length($y) < 1 || length($y) > 15) {
		error_message("Add Group Error", "Group name must be 1-15 characters long.  Only alphanumeric characters (A-Z, 0-9, underscore) are allowed.", 0, 1);
	}
	dreq("fcn-grp");
	my $ctr = write_group_file([ { group => $y, action => "add_group" } ]);
	if ($ctr == 0) {
		error_message("Add Group Error", "New group could not be added.  Make sure group name does not already exist.", 0, 1);
	}
	$subst->{stuff}->{what} = 1;
	$subst->{stuff}->{group_added} = $y;
	header();
	print templ_int("groupmgr", $subst);
	program_exit(0);
}

###
### group_delete
###
### Deletes a group
###

sub group_delete {
	my ($delete, $mark) = @_;
	dreq("fcn-grp");
	my @del = ();
	if ($delete eq "*") {
		@del = split(/,/, $mark);
	} else {
		@del = ($delete);
	}
	if ($DCONF->{pro}) {
		dreq("selfreg-PRO");
		selfreg_delete_groups(\@del);
	}
	my @d = ();
	foreach my $i (@del) {
		undef my $u;
		$u->{group} = $i;
		$u->{action} = "del_group";
		push (@d, $u);
	}
	return write_group_file(\@d);
}

1;
