Merge branch 'bug_8943' into 3.12-master
[koha.git] / labels / label-create-xml.pl
1 #!/usr/bin/perl
2
3 # Copyright Koha development team 2011
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19 #
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use XML::Simple;
26 use Data::Dumper;
27
28 use C4::Debug;
29 use C4::Creators;
30 use C4::Labels;
31
32 my $cgi = new CGI;
33
34 my $batch_id;
35 my @label_ids;
36 my @item_numbers;
37 $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
38 my $template_id = $cgi->param('template_id') || undef;
39 my $layout_id   = $cgi->param('layout_id') || undef;
40 @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
41 @item_numbers  = $cgi->param('item_number') if $cgi->param('item_number');
42
43 my $items = undef;
44
45 my $xml_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
46 print $cgi->header(-type        => 'text/xml',
47                    -encoding    => 'utf-8',
48                    -attachment  => "$xml_file.xml",
49                     );
50
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);
54
55
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;
62 }
63 elsif (@item_numbers) {
64     grep {
65         push(@{$items}, {item_number => $_});
66     } @item_numbers;
67 }
68 else {
69     $items = $batch->get_attr('items');
70 }
71
72 my $xml = XML::Simple->new();
73 my $xml_data = {'label' => []};
74
75 my $item_count = 0;
76
77 foreach my $item (@$items) {
78     push(@{$xml_data->{'label'}}, {'item_number' => $item->{'item_number'}});
79     my $label = C4::Labels::Label->new(
80                                     batch_id            => $batch_id,
81                                     item_number         => $item->{'item_number'},
82                                     format_string       => $layout->get_attr('format_string'),
83                                       );
84     my $format_string = $layout->get_attr('format_string');
85     my @data_fields = map { $_ eq 'callnumber' ? 'itemcallnumber' : $_ } # see bug 5653 
86                       split(/, /, $format_string);
87     my $csv_data = $label->csv_data();
88     for (my $i = 0; $i <= (scalar(@data_fields) - 1); $i++) {
89         push(@{$xml_data->{'label'}[$item_count]->{$data_fields[$i]}}, $$csv_data[$i]);
90     }
91     $item_count++;
92 }
93
94 #die "XML DATA:\n" . Dumper($xml_data);
95
96 my $xml_out = $xml->XMLout($xml_data);
97 #die "XML OUT:\n" . Dumper($xml_out);
98 print $xml_out;
99
100 __END__
101
102 =head1 NAME
103
104 labels/label-create-xml.pl - A script for creating a xml export of labels and label batches in Koha
105
106 =head1 ABSTRACT
107
108 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
109 a demonstration of the multitude of formats Koha labels could be exported in based on the current Label Creator API.
110
111 =head1 AUTHOR
112
113 Chris Nighswonger <cnighswonger AT foundations DOT edu>
114
115 =head1 COPYRIGHT
116
117 Copyright 2009 Foundations Bible College.
118
119 =head1 LICENSE
120
121 This file is part of Koha.
122
123 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
124 Foundation; either version 2 of the License, or (at your option) any later version.
125
126 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,
127 Fifth Floor, Boston, MA 02110-1301 USA.
128
129 =head1 DISCLAIMER OF WARRANTY
130
131 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
132 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
133
134 =cut