Bug 24663: Remove authnotrequired if set to 0
[koha.git] / opac / sci / sci-main.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21
22 use C4::Auth qw(get_template_and_user checkpw);
23 use C4::Circulation;
24 use C4::Output;
25
26 use List::MoreUtils qw( uniq );
27 use Try::Tiny;
28
29 my $cgi = new CGI;
30
31 # 404 if feature is disabled
32 unless ( C4::Context->preference('SelfCheckInModule') ) {
33     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
34     exit;
35 }
36
37 # Let Auth know this is a SCI context
38 $cgi->param( -name => 'sci_user_login', -values => [1] );
39
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
41     {
42         template_name   => "sci/sci-main.tt",
43         flagsrequired   => { self_check => 'self_checkin_module' },
44         query           => $cgi,
45         type            => "opac"
46     }
47 );
48
49 my $op = $cgi->param('op') // '';
50
51 if ( $op eq 'check_in' ) {
52     ## Get the barcodes, perform some basic validation
53     # Remove empty ones
54     my @barcodes = grep { $_ ne '' } $cgi->multi_param('barcode');
55
56     # Remove duplicates
57     @barcodes = uniq @barcodes;
58
59     # Read the library we are logged in from userenv
60     my $library = C4::Context->userenv->{'branch'};
61     my @success;
62     my @errors;
63
64     # Return items
65     foreach my $barcode (@barcodes) {
66         try {
67             my ( $success, $messages, $checkout, $patron ) =
68               AddReturn( $barcode, $library );
69             if ($success) {
70                 push @success,
71                   {
72                     barcode  => $barcode,
73                     messages => $messages,
74                     checkout => $checkout,
75                     patron   => $patron
76                   };
77             }
78             else {
79                 push @errors,
80                   {
81                     barcode  => $barcode,
82                     messages => $messages,
83                     checkout => $checkout,
84                     patron   => $patron
85                   };
86             }
87         }
88         catch {
89             push @errors, { barcode => $barcode, messages => 'unknown_error' };
90         };
91     }
92     $template->param( success => \@success, errors => \@errors, checkins => 1 );
93 }
94
95 # Make sure timeout has a reasonable value
96 my $timeout = C4::Context->preference('SelfCheckInTimeout') || 120;
97 $template->param( refresh_timeout => $timeout );
98
99 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };