#! /usr/bin/perl

=head1 NAME

project_collect - collect files from a project

=head1 SYNOPSIS

 https://server/schulkonsole/project_collect

=head1 DESCRIPTION

C<project_collect> lets you copy or move files of members of a selected project
to your directory. The HTML template is project_collect.tt.

=head2 Template variables

Additionally to the variables of Schulkonsole::Session C<project_collect>
provides the following variables:

=over

=cut

use strict;
use utf8;
use lib '/usr/share/schulkonsole';
use Schulkonsole::Session;
use Schulkonsole::Info;
use Schulkonsole::Sophomorix;

my $this_file = 'project_collect';


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();

my $projects = Schulkonsole::Info::groups_projects($sk_session->groups());



my $project = $q->param('projects');
if (    $project
    and $$projects{$project}) {
	$sk_session->param('project', $project);
}
$project = $sk_session->param('project');

if (   not $project
    or not $$projects{$project}) {
	my $url = $q->url( -absolute => 1 );
	$url =~ s/$this_file$/project/g;
	$sk_session->param('requested_page',$this_file);
	$sk_session->redirect($url);
}

if (not Schulkonsole::DB::is_project_admin($$projects{$project}{id},
        	$sk_session->userdata('uidnumber'))) {
	$sk_session->set_status_redirect($sk_session->d()->get(
		'Sie sind kein Projektleiter'),
		1);

	my $url = $q->url( -absolute => 1 );
	$url =~ s/$this_file$/project_members/g;
	$sk_session->redirect($url);
}








my $member_userdatas
	= Schulkonsole::DB::project_user_members($$projects{$project}{gidnumber});


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

eval {
	SWITCHCOMMAND: {
		($q->param('collecttop')
		    or $q->param('collectbottom')) and do {
			my @users;
			my $is_all_users = 1;
			foreach my $user (keys %$member_userdatas) {
				if ($q->param("$user;collect")) {
					push @users, $user;
				} elsif ($is_all_users) {
					$is_all_users = 0;
				}
			}
		
			if (@users) {
				eval {
					if (   (    $q->param('collecttop')
					        and $q->param('deletetop'))
					    or (    $q->param('collectbottom')
					        and $q->param('deletebottom'))) {
						Schulkonsole::Sophomorix::collect_from_project_users(
							$id, $password, $project, @users);
					} else {
						Schulkonsole::Sophomorix::collectcopy_from_project_users(
							$id, $password, $project, @users);
					}
		
					if ($is_all_users) {
						$sk_session->set_status(
							$sk_session->d()->get('Eingesammelt'),
							0);
					} else {
						$sk_session->set_status(
							  $sk_session->d()->get('Eingesammelt von')
							. ' ' . join(', ', @users),
							0);
					}
				};
				if ($@) {
					$sk_session->standard_error_handling($this_file, $@);
				}
			} else {
				$sk_session->set_status($sk_session->d()->get(
					'Keine Benutzer ausgewählt'), 1);
			}
			last SWITCHCOMMAND;
		};

		foreach my $param ($q->param) {
			if (my ($file, $action)
			    	= $param =~ /^(.+);(delete|download)$/) {
				utf8::decode($file);
				my $admin_collected_files = Schulkonsole::Sophomorix::ls_collected_project(
						$sk_session->userdata('id'), $sk_session->get_password(), $project);
				
				SWITCHACTION: {
				$action =~ /delete/ and do {
					if(not defined $$admin_collected_files{$file}) {
						$sk_session->set_status(
							$d->get('Datei ') . $file . $d->get(' nicht als eingesammelte Datei vorhanden.'), 1);
						last SWITCHACTION;
					}
					Schulkonsole::Sophomorix::rm_collected_project(
						$sk_session->userdata('id'), $sk_session->get_password(), $project,
						$file, ($$admin_collected_files{$file}?1:0));
					
					$sk_session->set_status($d->get('Datei gelöscht.'),0);
					
					last SWITCHACTION;
				};
				
				$action =~ /download/ and do {
					if(not defined $$admin_collected_files{$file}) {
						$sk_session->set_status(
							$d->get('Datei ') . $file . $d->get(' nicht als eingesammelte Datei vorhanden.'),	1);
						last SWITCHACTION;
					}
					my $tmpfile = "dl_collected_".$sk_session->session_id() . '_' . time;
					my $dlfile = $file;
					
					Schulkonsole::Sophomorix::dl_collected_project(
						$sk_session->userdata('id'), $sk_session->get_password(), $project,
						$file, ($$admin_collected_files{$file}?1:0), $tmpfile);
					if($$admin_collected_files{$file}) {
						$tmpfile .= ".zip";
						$dlfile .= ".zip";
					}
					
					if( -e "$Schulkonsole::Config::_runtimedir/$tmpfile") {
						print $q->header( -type           => "application/x-download",
								          -attachment     => $dlfile,
										  -Content_length => -s "$Schulkonsole::Config::_runtimedir/$tmpfile");
						
						if(! open(FILE, "<$Schulkonsole::Config::_runtimedir/$tmpfile")) {
							$sk_session->set_status($d->get('Die temporäre Datei konnte nicht geöffnet werden.'), 1);
							last SWITCHCOMMAND;
						}
						binmode(FILE);
						binmode(STDOUT);
						print while <FILE>;
						if(!close(FILE)) {
							$sk_session->set_status($d->get('Die temporäre Datei konnte nicht geschlossen werden.'), 1);
							last SWITCHCOMMAND;
						}
						
						system("rm -f $Schulkonsole::Config::_runtimedir/$tmpfile");
						
						$sk_session->set_status($d->get('Die Datei wurde heruntergeladen.'), 0);
						exit 0;
						
					} else {
						$sk_session->set_status($d->get('Es konnte keine temporäre Datei erzeugt werden.'), 1);
						last SWITCHCOMMAND;				
					}
					
					last SWITCHACTION;
				};
				
				} #SWITCHACTION
			}
		} # foreach param

	} #SWITCHCOMMAND
}; #eval
if ($@) {
	$sk_session->standard_error_handling($this_file, $@);
}


eval {
my $admin_collected_files = Schulkonsole::Sophomorix::ls_collected_project(
	$sk_session->userdata('id'),
	$sk_session->get_password(),
	$project);

=item C<admin_collected_files>

Available files as returned by C<Schulkonsole::Sophomorix::create_file_list()>

=cut

$sk_session->set_var('admin_collected_files',
	Schulkonsole::Sophomorix::create_file_list($admin_collected_files));
};
if ($@) {
	if (    ref $@
	    and $@->{code} == Schulkonsole::Error::SophomorixError::WRAPPER_NO_SUCH_DIRECTORY) {
		$sk_session->set_status(
			$sk_session->d()->get(
				'Verzeichnis zum Einsammeln nicht gefunden'),
				1);
	} else {
		$sk_session->standard_error_handling($this_file, $@);
	}
}




eval {
my @login_ids;
foreach my $user (keys %$member_userdatas) {
	push @login_ids, $$member_userdatas{$user}{id};
}
my $user_share_files = Schulkonsole::Sophomorix::ls_collect(
	$id, $password, @login_ids);


my @members;
foreach my $user (sort {
	$$member_userdatas{$a}{surname} cmp $$member_userdatas{$b}{surname} }
	keys %$member_userdatas) {

	my $member = {
		login => $user,
		firstname => $$member_userdatas{$user}{firstname},
		surname => $$member_userdatas{$user}{surname},
		files => Schulkonsole::Sophomorix::create_file_list(
		         	$$user_share_files{$$member_userdatas{$user}{id}}),
	};
	push @members, $member;
}

=item C<members>

An array of hashes with the keys

=over

=item C<login>

The login of a user

=item C<firstname>

The user's first name

=item C<surname>

The user's surname

=item C<files>

A list of the user's collectable files as returned from
C<Schulkonsole::Sophomorix::create_file_list()>.

=back

=cut

$sk_session->set_var('members', \@members);
};
if ($@) {
	$sk_session->standard_error_handling($this_file, $@);
}



my @projects;
foreach my $project (sort {
	$$projects{$a}{displayname} cmp $$projects{$b}{displayname} }
	keys %$projects) {
	push @projects, { gid => $project,
	                  name => $$projects{$project}{displayname},
	                };
}

=item C<projects>

Projects of the current user as an array of hashes with the keys

=over

=item C<gid>

GID of the project

=item C<name>

Name of the project

=back

=cut

$sk_session->set_var('projects', \@projects);


=item C<isadmin>

Indicates that the current user is an administrator of the selected project

=cut

$sk_session->set_var('isadmin', 1);

=item C<project_gid>

The GID of the selected project

=cut

$sk_session->set_var('project_gid', $project);

=item C<project>

The name of the selected project

=cut

$sk_session->set_var('project', $$projects{$project}{displayname});


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


=back

=head2 Form fields

=over

=item C<collecttop>

A submit button indicating that C<deletetop> is to be evaluated

=item C<collectbottom>

A submit button indicating that C<deletebottom> is to be evaluated

=item C<deletetop>/C<deletebottom>

If true the files are moved, copied otherwise

=item C<${members{login}};collect>

Checkboxes created in loop over template variable C<members>. Collecting
is performed with the selected logins.

=back

