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