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