#! /usr/bin/perl

=head1 NAME

settings_druckquotas - produces page for druckquota settings

=head1 SYNOPSIS

 https://server/schulkonsole/settings_druckquotas

=head1 DESCRIPTION

C<druckquotas> produces the page of schulkonsole druckquota settings.
The HTML template is settings_druckquotas.tt.

=over

=cut

use strict;
use lib '/usr/share/schulkonsole';
use Schulkonsole::Session;
use Schulkonsole::Druckquotas;
use Schulkonsole::DruckquotasConfig;
use Schulkonsole::Files;
use Schulkonsole::Config;

my $this_file = 'settings_druckquotas';
my $textdomain = 'linuxmuster-pk';

my $sk_session = new Schulkonsole::Session($this_file);
if (not $sk_session->get_password()) {
	my $q = new CGI;
	my $url = $q->url( -full => 1 );

	# we send cookies over secure connections only
	if ($url =~ s/^http:/https:/g) {
		$sk_session->redirect($url);
	} else {
		$sk_session->exit_with_login_page($this_file);
	}
}

my $q = $sk_session->query();
my $d = $sk_session->d($textdomain);

my $id = $sk_session->userdata('id');
my $password = $sk_session->get_password();


eval {
COMMANDS: {
my $is_changed = 0;
my @errors;

#############################################################
$q->param('acceptstandards') and do {
	my @processed_params;
	my @lines = Schulkonsole::Druckquotas::read_linuxmuster_pykota_conf_file($id, $password);
	foreach my $param ($q->param) {
		if (my ($gruppe) = $param =~ /^(.+)_gruppenewbalance$/) {
			if ( $gruppe =~ /[\-\w]+/  ) {
				my ($wert) = $q->param($param) =~ /^\s*(.+)\s*$/;
				$wert =~ tr/,/\./;
				if ($wert =~ /\S/) {
				    if ($wert !~ /^[\.\d]+$/) {
					push @errors, sprintf($d->get(
						'%s: Neue Kosten sind falsch angegeben'), $wert);
				    } else {
					my $gefunden = 0;
					my $linecount = 0;
					foreach my $line (@lines) {
						if ($line =~ m/\s*\$balance\{[\"\']?($gruppe)[\"\']?\}/i ) {
							$is_changed++;
							$gefunden = 1;
							push @processed_params, $param;
#							$wert = sprintf ('%.2f', $wert);
#							$wert =~ tr/\,/\./;
#							$lines[$linecount] = '$balance{"'."$gruppe".'"}  =  '.$wert.";\n";
#							$linecount++;
						}
					}
					if ($gefunden == 1) {
							Schulkonsole::Druckquotas::modify_standard($id, $password, $gruppe, $wert);
					} else {
						push @errors, sprintf($d->get(
						'%s: Gruppe existiert nicht.'), $gruppe);
					}
				    }
				}
			} else {
				push @errors, sprintf($d->get(
					'Gruppe %s falsch.'), $gruppe);
			}
		}
	}
#	if ($is_changed) {
#		Schulkosole::Files::_write_linuxmuster_pykota_conf_file($id, $password, \@lines);
#	}
	if (@errors) {
		$sk_session->set_status(join(', ', @errors), 1);
	} elsif ($is_changed) {
		$sk_session->set_status(sprintf($d->get("Änderungen übernommen: %s mal"), $is_changed), 0);
		$q->delete(@processed_params);
	} else {
		$sk_session->set_status($d->get("Keine Änderungen."), 1);
	}
};  # end acceptstandards


$q->param('deletestandards') and do {
	my $gruppe;
	my @lines = Schulkonsole::Druckquotas::read_linuxmuster_pykota_conf_file($id, $password);
	foreach my $param ($q->param) {
		if ( ($gruppe) = $param =~ /^(.+);select$/) {
			if ($gruppe =~ m/^\s*([\-\w]+)\s*$/) {
				$gruppe = $1;
				if ( $gruppe !~ /students|teachers/ ){
					my $gefunden = 0;
					my $linecount = 0;
					foreach my $line (@lines) {
						if ($line =~ m/\s*\$balance\{[\"\']?($gruppe)[\"\']?\}/i ) {
#							splice (@lines, $linecount,1)
							Schulkonsole::Druckquotas::delete_standard($id, $password, $gruppe);
							$is_changed ++;
							$gefunden = 1;
						$linecount++;
						}
					}
					if ($gefunden == 0) {
						push @errors, sprintf($d->get(
						'%s: Gruppe existiert nicht.'), $gruppe);
					}
				} else {
					push @errors, sprintf($d->get(
					'Die Standard-Gruppe %s kann nicht gel&ouml;scht werden.'), $gruppe);
				}
			} else {
				push @errors, sprintf($d->get(
					'Gruppe %s unbekannt'), $gruppe);
			}
		}
	}
#	if ($is_changed) {
#		Schulkosole::Files::_write_linuxmuster_pykota_conf_file($id, $password, \@lines);
#	}
	if (@errors) {
		$sk_session->set_status(join(', ', @errors), 1);
	} elsif ($is_changed) {
		$sk_session->set_status(sprintf($d->get("Gruppe gelöscht: %s mal"), $is_changed), 0);
	} else {
		$sk_session->set_status($d->get('Keine Änderungen.'), 1);
	}
}; # end deletestandards

$q->param('addstandard') and do {
	my @processed_params;
	my $value;
	if ( $q->param('newgroup') =~ /^\s*([\-\w]+)\s*$/) {
		my $gruppe = $1;
		if ( $q->param('newgroupbalance') =~ /^\s*([\.\,\d]+)\s*$/) {
			$value = $1;
			$value =~ tr/\,/\./;
		} else {
			push @errors, sprintf($d->get(
			'%s: Druckquota falsch angegeben.'), $gruppe);
		}
		my @lines = Schulkonsole::Druckquotas::read_linuxmuster_pykota_conf_file($id, $password);
		my $doppelt = 0;
		foreach my $line (@lines) {
			if ($line =~ m/\s*\$balance\{[\"\']?($gruppe)[\"\']?\}/i ) {
				push @errors, sprintf($d->get(
				'%s: Gruppe existiert schon.'), $gruppe);
				$doppelt = 1;
			}
		}
		if (not $doppelt) {
			Schulkonsole::Druckquotas::add_standard($id, $password, $gruppe, $value);
#			my $newline = '$balance{"'."$gruppe".'"}  =  '.$wert.';'."\n";
#			push @lines, $newline;
#			Schulkonsole::Files::_write_linuxmuster_pykota_conf_file($id, $password, \@lines);
			$is_changed = 1;
			push @processed_params, 'newgroup';
			push @processed_params, 'newgroupbalance';
		}
	} else {
		push @errors, sprintf($d->get(
		'Bitte Gruppenname korrigieren.'));
	}
	if (@errors) {
		$sk_session->set_status(join(', ', @errors), 1);
	} elsif ($is_changed) {
		$sk_session->set_status($d->get("Gruppe aufgenommen."), 0);
		$q->delete(@processed_params);
	} else {
		$sk_session->set_status($d->get('Keine &Auml;nderungen.'), 1);
	}
}; # end addstandard


$q->param('setaktiv') and do {
	my @processed_params;
	if ($q->param('newaktiv')=~ /^\s*(\d+)\s*$/) {
		my $nummer = int($1);
		my $count = 0 ;
		my @typ;
		my @position;
		my @text;
		my $linecount;
		my @lines = Schulkonsole::Druckquotas::read_pykota_conf_file($id, $password);
		foreach my $line ( @lines ) {
			if ( $line =~ m/\s*(#*)\s*(askconfirmation\s*\:\s*\/usr\/bin\/pknotify.*)/ ) {
				$count++;
				$typ[$count] = $1;
				$text[$count] = $2;
				$position[$count] = $linecount;
			}
			$linecount++;
		}
		if (($nummer <= $count) and ( $count >0 )){
			for (my $i=1; $i<$count+1; $i++) {
				if ($nummer == $i) {
					if ($typ [$i] =~ m/#+/ ) {
#						$lines[$position[$i]] = $text[$i]."\n";
						$is_changed++;
						push @processed_params, 'newaktiv';
					}
				} else {
					if ($typ [$i] !~ m/#+/ ) {
#						$lines[$position[$i]] = '#'.$text[$i]."\n";
						$is_changed++;
						push @processed_params, 'newaktiv';
					}
				}
			}
			if ($is_changed > 0 ) {
				Schulkonsole::Druckquotas::set_aktiv($id, $password, $nummer);
#				Schulkonsole::Files::_write_pykota_conf_file($id, $password, \@lines);
			}
		} else {
			push @errors, sprintf($d->get(
			"Nummer zu gross."));
		}
	} else {
		push @errors, sprintf($d->get(
		'Nummer falsch.'));
	}
	if (@errors) {
		$sk_session->set_status(join(', ', @errors), 1);
	} elsif ($is_changed) {
		$sk_session->set_status($d->get("Aktivierung ausgef&uuml;hrt."), 0);
		$q->delete(@processed_params);
	} else {
		$sk_session->set_status($d->get('Keine &Auml;nderungen.'), 1);
	}
}

} # end COMMANDS
};

my @standards_array;
my %balance = Schulkonsole::Druckquotas::get_linuxmuster_pykota_conf($id, $password);
foreach my $groupkey (sort keys %balance) {
	my $wert = sprintf('%.2f',$balance{$groupkey});
	$wert =~ tr/\,/\./;
	if ( $groupkey !~ /students|teachers/) {
		push @standards_array, {
			gruppe => $groupkey,
			balance => $wert,
			checkbox => 'show',
		};
	} else {
		push @standards_array, {
			gruppe => $groupkey,
			balance => $wert,
		};
	}
}

my @askconfirmation_array;
my @lines = Schulkonsole::Druckquotas::read_pykota_conf_file($id, $password);
my $typ;
my $num =0;
foreach my $zeile ( @lines ) {
	if ( $zeile =~ m/\s*(#*)\s*askconfirmation\s*\:\s*(\/usr\/bin\/pknotify.*)/ ) {
		$num++;
		my $symbol =$1;
		my $text = $2;
		if ($symbol eq "#" ) {
			$typ = "deaktiviert ";
		} else {
			$typ = "aktiviert ";
		}
		push @askconfirmation_array, {
			typ => $typ,
			text => $text,
			nummer => $num,
		};
	}
}

$sk_session->set_var('standards', \@standards_array);
$sk_session->set_var('askconfirmation', \@askconfirmation_array);

$sk_session->print_page("$this_file.tt", $this_file);




