Bug 24879: Add check_cookie_auth when missing
[koha.git] / serials / lateissues-export.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 use CGI qw ( -utf8 );
20 use C4::Auth;
21 use C4::Serials qw( GetLateOrMissingIssues updateClaim );
22 use C4::Output;
23 use C4::Context;
24
25 use Koha::CsvProfiles;
26
27 use Text::CSV_XS;
28
29 my $query = CGI->new;
30 my ($auth_status) =
31     check_cookie_auth( $query->cookie('CGISESSID'), { catalogue => 1 } );
32 if ( $auth_status ne "ok" ) {
33     print $query->header( -type => 'text/plain', -status => '403 Forbidden' );
34     exit 0;
35 }
36
37 my $supplierid = $query->param('supplierid');
38 my @serialids = $query->multi_param('serialid');
39 my $op = $query->param('op') || q{};
40
41 my $csv_profile_id = $query->param('csv_profile');
42 my $csv_profile = Koha::CsvProfiles->find( $csv_profile_id );
43 die "There is no valid csv profile given" unless $csv_profile;
44
45 my $delimiter = $csv_profile->csv_separator;
46 $delimiter = "\t" if $delimiter eq "\\t";
47
48 my $csv = Text::CSV_XS->new({
49     'quote_char'  => '"',
50     'escape_char' => '"',
51     'sep_char'    => $delimiter,
52     'binary'      => 1
53 });
54
55 my $content = $csv_profile->content;
56 my ( @headers, @fields );
57 while ( $content =~ /
58     ([^=\|]+) # header
59     =?
60     ([^\|]*) # fieldname (table.row or row)
61     \|? /gxms
62 ) {
63     my $header = $1;
64     my $field = ($2 eq '') ? $1 : $2;
65
66     $header =~ s/^\s+|\s+$//g; # Trim whitespaces
67     push @headers, $header;
68
69     $field =~ s/[^.]+\.//; # Remove the table name if exists.
70     $field =~ s/^\s+|\s+$//g; # Trim whitespaces
71     push @fields, $field;
72 }
73
74 my @rows;
75 for my $serialid ( @serialids ) {
76     my @missingissues = GetLateOrMissingIssues($supplierid, $serialid);
77     my $issue = $missingissues[0];
78     my @row;
79     for my $field ( @fields ) {
80         push @row, $issue->{$field};
81     }
82     push @rows, \@row;
83
84     # update claim date to let one know they have looked at this missing item
85     updateClaim($serialid);
86 }
87
88 print $query->header(
89     -type       => 'plain/text',
90     -attachment => "serials-claims.csv",
91 );
92
93 print join( $delimiter, @headers ) . "\n";
94
95 for my $row ( @rows ) {
96     $csv->combine(@$row);
97     my $string = $csv->string;
98     print $string, "\n";
99 }