Bug 11944: use CGI( -utf8 ) everywhere
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 #
20
21 use strict;
22 use warnings;
23
24 use CGI qw ( -utf8 );
25 use Text::CSV_XS;
26 use Data::Dumper;
27
28 use C4::Debug;
29 use C4::Creators;
30 use C4::Labels;
31
32 my $cgi = new CGI;
33
34 my $batch_id;
35 my @label_ids;
36 my @item_numbers;
37 $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
38 my $template_id = $cgi->param('template_id') || undef;
39 my $layout_id   = $cgi->param('layout_id') || undef;
40 @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
41 @item_numbers  = $cgi->param('item_number') if $cgi->param('item_number');
42
43 my $items = undef;
44
45 my $csv_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
46 print $cgi->header(-type        => 'application/vnd.sun.xml.calc',
47                    -encoding    => 'utf-8',
48                    -attachment  => "$csv_file.csv",
49                     );
50
51
52 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
53 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
54 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
55
56
57 if (@label_ids) {
58     my $batch_items = $batch->get_attr('items');
59     grep {
60         my $label_id = $_;
61         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
62     } @label_ids;
63 }
64 elsif (@item_numbers) {
65     grep {
66         push(@{$items}, {item_number => $_});
67     } @item_numbers;
68 }
69 else {
70     $items = $batch->get_attr('items');
71 }
72
73 my $csv = Text::CSV_XS->new();
74
75 foreach my $item (@$items) {
76     my $label = C4::Labels::Label->new(
77                                     batch_id            => $batch_id,
78                                     item_number         => $item->{'item_number'},
79                                     format_string       => $layout->get_attr('format_string'),
80                                       );
81     my $csv_fields = $label->csv_data();
82     if ($csv->combine(@$csv_fields)) {
83         print $csv->string() . "\n";
84     }
85     else {
86         warn sprintf('Text::CSV_XS->combine() returned the following error: %s', $csv->error_input);
87     }
88 }
89
90 __END__
91
92 =head1 NAME
93
94 labels/label-create-csv.pl - A script for creating a csv export of labels and label batches in Koha
95
96 =head1 ABSTRACT
97
98 This script provides the means of producing a csv of labels for items either individually, in groups, or in batches from within Koha.
99
100 =head1 AUTHOR
101
102 Chris Nighswonger <cnighswonger AT foundations DOT edu>
103
104 =head1 COPYRIGHT
105
106 Copyright 2009 Foundations Bible College.
107
108 =head1 LICENSE
109
110 This file is part of Koha.
111
112 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
113 Foundation; either version 2 of the License, or (at your option) any later version.
114
115 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
116 Fifth Floor, Boston, MA 02110-1301 USA.
117
118 =head1 DISCLAIMER OF WARRANTY
119
120 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
121 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
122
123 =cut