Bug 35619: Fix validation error alignment in change password form
[koha.git] / opac / opac-patron-consent.pl
1 #!/usr/bin/perl
2
3 # Copyright 2018 Rijksmuseum
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use CGI qw/-utf8/;
22
23 use C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25 use Koha::DateUtils qw( dt_from_string );
26 use Koha::Exceptions::Patron;
27 use Koha::Patrons;
28
29 my $query = CGI->new;
30 my $op = $query->param('op') // q{};
31 my $vars = $query->Vars;
32
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user({
34     template_name   => "opac-patron-consent.tt",
35     query           => $query,
36     type            => "opac",
37 });
38
39 my $patron = Koha::Patrons->find($borrowernumber)
40     or Koha::Exceptions::Patron->throw("Patron id $borrowernumber not found");
41
42 # Get consent types and values
43 my @consents;
44 my $consent_types = Koha::Patron::Consents->available_types;
45 foreach my $consent_type ( sort keys %$consent_types) {
46     push @consents, $patron->consent($consent_type);
47 }
48
49 # Handle saves here
50 my $needs_redirect;
51 foreach my $consent ( @consents ) {
52     my $check = $vars->{ "check_".$consent->type };
53     next if !defined($check);    # no choice made
54     $needs_redirect = 1
55         if $consent->type eq q/GDPR_PROCESSING/ && !$check && C4::Context->preference('PrivacyPolicyConsent') eq 'Enforced';
56     next if $consent->given_on && $check || $consent->refused_on && !$check;
57         # No update if no consent change
58     $consent->set({
59         given_on => $check ? dt_from_string() : undef,
60         refused_on => $check ? undef : dt_from_string(),
61     })->store;
62 }
63
64 # If user refused GDPR consent and we enforce GDPR, logout (when saving)
65 if( $needs_redirect ) {
66     print $query->redirect('/cgi-bin/koha/opac-main.pl?logout.x=1');
67     exit;
68 }
69
70 $template->param( patron => $patron, consents => \@consents, consent_types => $consent_types );
71
72 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };