Bug 24879: Add check_cookie_auth when missing
[koha.git] / labels / label-create-csv.pl
1 #!/usr/bin/perl
2
3 # Copyright Koha development team 2011
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
21 use Modern::Perl;
22
23 use CGI qw ( -utf8 );
24 use Text::CSV_XS;
25
26 use C4::Labels;
27
28 my $cgi = CGI->new;
29 my ($auth_status) =
30     check_cookie_auth( $cgi->cookie('CGISESSID'), { catalogue => 1 } );
31 if ( $auth_status ne "ok" ) {
32     print $cgi->header( -type => 'text/plain', -status => '403 Forbidden' );
33     exit 0;
34 }
35
36 my $batch_id;
37 my @label_ids;
38 my @item_numbers;
39 $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
40 my $template_id = $cgi->param('template_id') || undef;
41 my $layout_id   = $cgi->param('layout_id') || undef;
42 @label_ids   = $cgi->multi_param('label_id') if $cgi->param('label_id');
43 @item_numbers  = $cgi->multi_param('item_number') if $cgi->param('item_number');
44
45 my $items = undef;
46
47 my $csv_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
48 print $cgi->header(-type        => 'application/vnd.sun.xml.calc',
49                    -encoding    => 'utf-8',
50                    -attachment  => "$csv_file.csv",
51                     );
52
53
54 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
55 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
56 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
57
58
59 if (@label_ids) {
60     my $batch_items = $batch->get_attr('items');
61     grep {
62         my $label_id = $_;
63         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
64     } @label_ids;
65 }
66 elsif (@item_numbers) {
67     grep {
68         push(@{$items}, {item_number => $_});
69     } @item_numbers;
70 }
71 else {
72     $items = $batch->get_attr('items');
73 }
74
75 my $csv = Text::CSV_XS->new();
76
77 foreach my $item (@$items) {
78     my $label = C4::Labels::Label->new(
79                                     batch_id            => $batch_id,
80                                     item_number         => $item->{'item_number'},
81                                     format_string       => $layout->get_attr('format_string'),
82                                       );
83     my $csv_fields = $label->csv_data();
84     if ($csv->combine(@$csv_fields)) {
85         print $csv->string() . "\n";
86     }
87     else {
88         warn sprintf('Text::CSV_XS->combine() returned the following error: %s', $csv->error_input);
89     }
90 }
91
92 __END__
93
94 =head1 NAME
95
96 labels/label-create-csv.pl - A script for creating a csv export of labels and label batches in Koha
97
98 =head1 ABSTRACT
99
100 This script provides the means of producing a csv of labels for items either individually, in groups, or in batches from within Koha.
101
102 =head1 AUTHOR
103
104 Chris Nighswonger <cnighswonger AT foundations DOT edu>
105
106 =head1 COPYRIGHT
107
108 Copyright 2009 Foundations Bible College.
109
110 =head1 LICENSE
111
112 This file is part of Koha.
113
114 Koha is free software; you can redistribute it and/or modify it
115 under the terms of the GNU General Public License as published by
116 the Free Software Foundation; either version 3 of the License, or
117 (at your option) any later version.
118
119 Koha is distributed in the hope that it will be useful, but
120 WITHOUT ANY WARRANTY; without even the implied warranty of
121 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
122 GNU General Public License for more details.
123
124 You should have received a copy of the GNU General Public License
125 along with Koha; if not, see <http://www.gnu.org/licenses>.
126
127 =head1 DISCLAIMER OF WARRANTY
128
129 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
130 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
131
132 =cut