Bug 21520: (QA follow-up): Updatedatabase
[koha.git] / labels / label-create-xml.pl
blobd0369db465961dd0ce2dbb50bbd93ff4a2def24a
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 XML::Simple;
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 $xml_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
45 print $cgi->header(-type => 'text/xml',
46 -encoding => 'utf-8',
47 -attachment => "$xml_file.xml",
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);
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;
62 elsif (@item_numbers) {
63 grep {
64 push(@{$items}, {item_number => $_});
65 } @item_numbers;
67 else {
68 $items = $batch->get_attr('items');
71 my $xml = XML::Simple->new();
72 my $xml_data = {'label' => []};
74 my $item_count = 0;
76 foreach my $item (@$items) {
77 push(@{$xml_data->{'label'}}, {'item_number' => $item->{'item_number'}});
78 my $label = C4::Labels::Label->new(
79 batch_id => $batch_id,
80 item_number => $item->{'item_number'},
81 format_string => $layout->get_attr('format_string'),
83 my $format_string = $layout->get_attr('format_string');
84 my @data_fields = map { $_ eq 'callnumber' ? 'itemcallnumber' : $_ } # see bug 5653
85 split(/, /, $format_string);
86 my $csv_data = $label->csv_data();
87 for (my $i = 0; $i <= (scalar(@data_fields) - 1); $i++) {
88 push(@{$xml_data->{'label'}[$item_count]->{$data_fields[$i]}}, $$csv_data[$i]);
90 $item_count++;
93 #die "XML DATA:\n" . Dumper($xml_data);
95 my $xml_out = $xml->XMLout($xml_data);
96 #die "XML OUT:\n" . Dumper($xml_out);
97 print $xml_out;
99 __END__
101 =head1 NAME
103 labels/label-create-xml.pl - A script for creating a xml export of labels and label batches in Koha
105 =head1 ABSTRACT
107 This script provides the means of producing a xml of labels for items either individually, in groups, or in batches from within Koha. This particular script is provided more as
108 a demonstration of the multitude of formats Koha labels could be exported in based on the current Label Creator API.
110 =head1 AUTHOR
112 Chris Nighswonger <cnighswonger AT foundations DOT edu>
114 =head1 COPYRIGHT
116 Copyright 2009 Foundations Bible College.
118 =head1 LICENSE
120 This file is part of Koha.
122 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
123 Foundation; either version 2 of the License, or (at your option) any later version.
125 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,
126 Fifth Floor, Boston, MA 02110-1301 USA.
128 =head1 DISCLAIMER OF WARRANTY
130 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
131 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
133 =cut