Bug 32482: (follow-up) Add markup comments
[koha.git] / opac / opac-privacy.pl
1 #!/usr/bin/perl
2 # This script lets the users change their privacy rules
3 #
4 # copyright 2009, BibLibre, paul.poulain@biblibre.com
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20 use CGI qw ( -utf8 );
21
22 use C4::Auth qw( get_template_and_user );
23 use C4::Context;
24 use C4::Output qw( output_html_with_http_headers );
25 use Koha::Patrons;
26
27 my $query = CGI->new;
28
29 # if OPACPrivacy is disabled, leave immediately
30 if ( ! C4::Context->preference('OPACPrivacy') || ! C4::Context->preference('opacreadinghistory') ) {
31     print $query->redirect("/cgi-bin/koha/errors/404.pl");
32     exit;
33 }
34
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36     {
37         template_name   => "opac-privacy.tt",
38         query           => $query,
39         type            => "opac",
40     }
41 );
42
43 my $op                         = $query->param("op");
44 my $privacy                    = $query->param("privacy");
45 my $privacy_guarantor_checkouts = $query->param("privacy_guarantor_checkouts");
46 my $privacy_guarantor_fines     = $query->param("privacy_guarantor_fines");
47
48 my $patron = Koha::Patrons->find( $borrowernumber );
49
50 if ( $op eq "update_privacy" ) {
51     if ( $patron ) {
52         $patron->set({
53             privacy                    => $privacy,
54             privacy_guarantor_checkouts => defined $privacy_guarantor_checkouts?$privacy_guarantor_checkouts:$patron->privacy_guarantor_checkouts,
55             privacy_guarantor_fines     => defined $privacy_guarantor_fines?$privacy_guarantor_fines:$patron->privacy_guarantor_fines,
56         })->store;
57         $template->param( 'privacy_updated' => 1 );
58     }
59 }
60 elsif ( $op eq "delete_record" ) {
61
62     my $holds     = $query->param('holds');
63     my $checkouts = $query->param('checkouts');
64     my $all       = $query->param('all');
65
66     $template->param( delete_all_quested => 1 )
67       if $all;
68
69     if ( $all or $checkouts ) {
70
71         # delete all reading records for items returned
72         my $rows = eval { $patron->old_checkouts->anonymize + 0 };
73
74         $template->param(
75             (
76                   $@    ? ( error_deleting_checkouts_history => 1 )
77                 : $rows ? ( deleted_checkouts => int($rows) )
78                 :         ( no_checkouts_to_delete => 1 )
79             ),
80             delete_checkouts_requested => 1,
81         );
82     }
83
84     if ( $all or $holds ) {
85
86         my $rows = eval { $patron->old_holds->anonymize + 0 };
87
88         $template->param(
89             (
90                   $@    ? ( error_deleting_holds_history => 1 )
91                 : $rows ? ( deleted_holds => int($rows) )
92                 :         ( no_holds_to_delete => 1 )
93             ),
94             delete_holds_requested => 1,
95         );
96     }
97 }
98
99 $template->param(
100     'Ask_data'                     => 1,
101     'privacy' . $patron->privacy() => 1,
102     'privacyview'                  => 1,
103     'borrower'                     => $patron,
104     'surname'                      => $patron->surname,
105     'firstname'                    => $patron->firstname,
106     'has_guarantor_flag'           => $patron->guarantor_relationships->guarantors->_resultset->count
107 );
108
109 output_html_with_http_headers $query, $cookie, $template->output, undef, { force_no_caching => 1 };