Merge branch 'labels_recon' into to-push
[koha.git] / labels / label-create-xml.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use CGI;
7 use XML::Simple;
8 use Data::Dumper;
9
10 use C4::Debug;
11 use C4::Labels::Batch 1.000000;
12 use C4::Labels::Template 1.000000;
13 use C4::Labels::Layout 1.000000;
14 use C4::Labels::PDF 1.000000;
15 use C4::Labels::Label 1.000000;
16
17 =head
18
19 =cut
20
21 my $cgi = new CGI;
22
23 my $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
24 my $template_id = $cgi->param('template_id') || undef;
25 my $layout_id   = $cgi->param('layout_id') || undef;
26 my @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
27 my @item_numbers  = $cgi->param('item_number') if $cgi->param('item_number');
28
29 my $items = undef;
30
31 my $xml_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
32 print $cgi->header(-type        => 'text/xml',
33                    -encoding    => 'utf-8',
34                    -attachment  => "$xml_file.xml",
35                     );
36
37 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
38 my $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
39 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
40
41
42 if (@label_ids) {
43     my $batch_items = $batch->get_attr('items');
44     grep {
45         my $label_id = $_;
46         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
47     } @label_ids;
48 }
49 elsif (@item_numbers) {
50     grep {
51         push(@{$items}, {item_number => $_});
52     } @item_numbers;
53 }
54 else {
55     $items = $batch->get_attr('items');
56 }
57
58 my $xml = XML::Simple->new();
59 my $xml_data = {'label' => []};
60
61 my $item_count = 0;
62
63 XML_ITEMS:
64 foreach my $item (@$items) {
65     push(@{$xml_data->{'label'}}, {'item_number' => $item->{'item_number'}});
66     my $label = C4::Labels::Label->new(
67                                     batch_id            => $batch_id,
68                                     item_number         => $item->{'item_number'},
69                                     format_string       => $layout->get_attr('format_string'),
70                                       );
71     my $format_string = $layout->get_attr('format_string');
72     my @data_fields = split(/, /, $format_string);
73     my $csv_data = $label->csv_data();
74     for (my $i = 0; $i < (scalar(@data_fields) - 1); $i++) {
75         push(@{$xml_data->{'label'}[$item_count]->{$data_fields[$i]}}, $$csv_data[$i]);
76     }
77     $item_count++;
78 }
79
80 #die "XML DATA:\n" . Dumper($xml_data);
81
82 my $xml_out = $xml->XMLout($xml_data);
83 #die "XML OUT:\n" . Dumper($xml_out);
84 print $xml_out;
85
86 exit(1);
87
88 =head1 NAME
89
90 labels/label-create-xml.pl - A script for creating a xml export of labels and label batches in Koha
91
92 =head1 ABSTRACT
93
94 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
95 a demonstration of the multitude of formats Koha labels could be exported in based on the current Label Creator API.
96
97 =head1 AUTHOR
98
99 Chris Nighswonger <cnighswonger AT foundations DOT edu>
100
101 =head1 COPYRIGHT
102
103 Copyright 2009 Foundations Bible College.
104
105 =head1 LICENSE
106
107 This file is part of Koha.
108        
109 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
110 Foundation; either version 2 of the License, or (at your option) any later version.
111
112 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
113 Suite 330, Boston, MA  02111-1307 USA
114
115 =head1 DISCLAIMER OF WARRANTY
116
117 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
118 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
119