Enh 6165: Add OPACResultsSidebar system preference
[koha.git] / labels / label-create-csv.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use CGI;
7 use Text::CSV_XS;
8 use Data::Dumper;
9
10 use C4::Debug;
11 use C4::Creators 1.000000;
12 use C4::Labels 1.000000;
13
14 my $cgi = new CGI;
15
16 my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
17 my $template_id = $cgi->param('template_id') || undef;
18 my $layout_id   = $cgi->param('layout_id') || undef;
19 my @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
20 my @item_numbers  = $cgi->param('item_number') if $cgi->param('item_number');
21
22 my $items = undef;
23
24 my $csv_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
25 print $cgi->header(-type        => 'application/vnd.sun.xml.calc',
26                    -encoding    => 'utf-8',
27                    -attachment  => "$csv_file.csv",
28                     );
29
30
31 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
32 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
33 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
34
35
36 if (@label_ids) {
37     my $batch_items = $batch->get_attr('items');
38     grep {
39         my $label_id = $_;
40         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
41     } @label_ids;
42 }
43 elsif (@item_numbers) {
44     grep {
45         push(@{$items}, {item_number => $_});
46     } @item_numbers;
47 }
48 else {
49     $items = $batch->get_attr('items');
50 }
51
52 my $csv = Text::CSV_XS->new();
53
54 CSV_ITEMS:
55 foreach my $item (@$items) {
56     my $label = C4::Labels::Label->new(
57                                     batch_id            => $batch_id,
58                                     item_number         => $item->{'item_number'},
59                                     format_string       => $layout->get_attr('format_string'),
60                                       );
61     my $csv_fields = $label->csv_data();
62     if ($csv->combine(@$csv_fields)) {
63         print $csv->string() . "\n";
64     }
65     else {
66         warn sprintf('Text::CSV_XS->combine() returned the following error: %s', $csv->error_input);
67         next CSV_ITEMS;
68     }
69 }
70
71 exit(1);
72
73 =head1 NAME
74
75 labels/label-create-csv.pl - A script for creating a csv export of labels and label batches in Koha
76
77 =head1 ABSTRACT
78
79 This script provides the means of producing a csv of labels for items either individually, in groups, or in batches from within Koha.
80
81 =head1 AUTHOR
82
83 Chris Nighswonger <cnighswonger AT foundations DOT edu>
84
85 =head1 COPYRIGHT
86
87 Copyright 2009 Foundations Bible College.
88
89 =head1 LICENSE
90
91 This file is part of Koha.
92
93 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
94 Foundation; either version 2 of the License, or (at your option) any later version.
95
96 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,
97 Fifth Floor, Boston, MA 02110-1301 USA.
98
99 =head1 DISCLAIMER OF WARRANTY
100
101 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
102 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
103