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