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