Bug 24879: Add check_cookie_auth when missing
[koha.git] / cataloguing / value_builder / barcode_manual.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 13437)
4
5 # Copyright 2000-2002 Katipo Communications
6 # Parts copyright 2008-2010 Foundations Bible College
7 #
8 # This file is part of Koha.
9 #
10 # Koha is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # Koha is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22
23 use Modern::Perl;
24
25 use C4::Context;
26 use C4::Barcodes::ValueBuilder;
27 use C4::Biblio qw( GetMarcFromKohaField );
28 use Koha::DateUtils qw( dt_from_string output_pref );
29
30 use CGI qw ( -utf8 );
31 use C4::Auth qw( check_cookie_auth );
32 my $input = CGI->new;
33 my ($auth_status) =
34     check_cookie_auth( $input->cookie('CGISESSID'), { catalogue => 1 } );
35 if ( $auth_status ne "ok" ) {
36     print $input->header( -type => 'text/plain', -status => '403 Forbidden' );
37     exit 0;
38 }
39
40 my $builder = sub {
41     my ( $params ) = @_;
42     my $function_name = $params->{id};
43     my %args;
44
45     my $dbh = $params->{dbh};
46     $args{dbh} = $dbh;
47
48 # find today's date
49     ($args{year}, $args{mon}, $args{day}) = split('-', output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }));
50     ($args{tag},$args{subfield})       =  GetMarcFromKohaField( "items.barcode" );
51
52     my $nextnum;
53     my $scr;
54     my $autoBarcodeType = C4::Context->preference("autoBarcode");
55     if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
56 # don't return a value unless we have the appropriate syspref set
57         return q|<script></script>|;
58     }
59     if ($autoBarcodeType eq 'annual') {
60         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
61     }
62     elsif ($autoBarcodeType eq 'incremental') {
63         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
64     }
65     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
66         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
67     }
68
69 # default js body (if not filled by hbyymmincr)
70     $scr or $scr = <<END_OF_JS;
71     if (\$('#' + id).val() == '') {
72         \$('#' + id).val('$nextnum');
73     }
74 END_OF_JS
75
76     my $js  = <<END_OF_JS;
77     <script>
78
79     function Click$function_name(event) {
80         const id = event.data.id;
81         $scr
82             return false;
83     }
84     </script>
85 END_OF_JS
86     return $js;
87 };
88
89 return { builder => $builder };