#!/usr/local/bin/perl
# edit_group.cgi
# Edit or create a webmin group

require './acl-lib.pl';
&ReadParse();
$access{'groups'} || &error($text{'gedit_ecannot'});
if ($in{'group'}) {
	# Editing an existing group
	&header($text{'gedit_title'}, "");
	foreach $g (&list_groups()) {
		if ($g->{'name'} eq $in{'group'}) {
			%group = %$g;
			}
		}
	}
else {
	# Creating a new group
	&header($text{'gedit_title2'}, "");
	}
print "<hr>\n";

print "<form action=save_group.cgi>\n";
print "<input type=hidden name=old value=\"$in{'group'}\">\n";
print "<table border width=100%>\n";
print "<tr $tb> <td><b>$text{'gedit_rights'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

print "<tr> <td><b>$text{'gedit_group'}</b></td>\n";
print "<td><input name=name size=25 value=\"$group{'name'}\"></td>\n";

@glist = grep { $_->{'name'} ne $group{'name'} } &list_groups();
@mcan = $access{'gassign'} eq '*' ?
		( ( map { $_->{'name'} } @glist ), '_none' ) :
		split(/\s+/, $access{'gassign'});
map { $gcan{$_}++ } @mcan;
if (@glist && %gcan) {
	print "<td><b>$text{'edit_group'}</b></td>\n";
	print "<td><select name=group>\n";
	foreach $g (@glist) {
		local $mem = &indexof($group{'name'}, @{$g->{'members'}}) >= 0;
		next if (!$gcan{$g->{'name'}} && !$mem);
		printf "<option %s>%s\n",
			$mem ?  'selected' : '', $g->{'name'};
		$group = $g if ($mem);
		}
	printf "<option value='' %s>&lt;%s&gt;\n",
		$group ? '' : 'selected', $text{'edit_none'}
			if ($gcan{'_none'});
	print "</select></td>\n";
	}
print "</tr>\n";

@mlist = &list_module_infos();
map { $has{$_}++ } @{$group{'modules'}};
print "<tr> <td valign=top><b>$text{'gedit_modules'}</b></td>\n";
print "<td colspan=3><table width=100% cellpadding=0 cellspacing=0>\n";
foreach $m (@mlist) {
	local $md = $m->{'dir'};
	if (!$sw) { print "<tr>\n"; }
	print "<td width=50%>";
	printf"<input type=checkbox name=mod value=$md %s> %s\n",
	      $has{$md} ? "checked" : "", $m->{'desc'};
	print "</td>";
	if ($sw) { print "<tr>\n"; }
	$sw = !$sw;
	}
print "</table></td> </tr>\n";
print "</table></td> </tr></table>\n";

print "<table width=100%> <tr>\n";
print "<td><input type=submit value='$text{'save'}'></td>\n";
if ($in{'group'}) {
	print "</form><form action=delete_group.cgi>\n";
	print "<input type=hidden name=group value=\"$in{'group'}\">\n";
	print "<td align=right><input type=submit name=delete ",
	      "value='$text{'delete'}'></td></form>\n";
	}
print "</tr></table>\n";

print "<hr>\n";
&footer("", $text{'index_return'});

