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