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