#!/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'}, "");
	foreach $g (&list_groups()) {
		if ($g->{'name'} eq $in{'clone'}) {
			$group{'modules'} = $g->{'modules'};
			}
		}
	}
print "<hr>\n";

print "<form action=save_group.cgi method=post>\n";
print "<input type=hidden name=old value=\"$in{'group'}\">\n";
if ($in{'clone'}) {
	print "<input type=hidden name=clone value=\"$in{'clone'}\">\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>\n";
print "<a href='' onClick='document.forms[0].mod.checked = true; for(i=0; i<document.forms[0].mod.length; i++) { document.forms[0].mod[i].checked = true; } return false'>$text{'edit_selall'}</a>&nbsp;\n";
print "<a href='' onClick='document.forms[0].mod.checked = !document.forms[0].mod.checked; for(i=0; i<document.forms[0].mod.length; i++) { document.forms[0].mod[i].checked = !document.forms[0].mod[i].checked; } return false'>$text{'edit_invert'}</a><br>\n";
print "<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>\n";
print "<a href='' onClick='document.forms[0].mod.checked = true; for(i=0; i<document.forms[0].mod.length; i++) { document.forms[0].mod[i].checked = true; } return false'>$text{'edit_selall'}</a>&nbsp;\n";
print "<a href='' onClick='document.forms[0].mod.checked = !document.forms[0].mod.checked; for(i=0; i<document.forms[0].mod.length; i++) { document.forms[0].mod[i].checked = !document.forms[0].mod[i].checked; } return false'>$text{'edit_invert'}</a>\n";
print "</td> </tr>\n";
print "</table></td> </tr></table>\n";

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

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

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

