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