[24/40] Adding single/multiple label printing to label export code and interface.
[koha.git] / labels / label-print.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2009 Foundations Bible College.
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 use strict;
21 use warnings;
22
23 use CGI;
24 use HTML::Template::Pro;
25 use Data::Dumper;
26
27 use C4::Auth qw(get_template_and_user);
28 use C4::Output qw(output_html_with_http_headers);
29 use C4::Labels::Lib 1.000000 qw(get_all_templates get_all_layouts get_label_output_formats);
30 use C4::Labels::Batch 1.000000;
31
32 my $cgi = new CGI;
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => "labels/label-print.tmpl",
36         query           => $cgi,
37         type            => "intranet",
38         authnotrequired => 0,
39         flagsrequired   => { catalogue => 1 },
40         debug           => 1,
41     }
42 );
43
44 my $op = $cgi->param('op') || 'none';
45 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');   # this will handle individual label printing
46 my @batch_ids = $cgi->param('batch_id') if $cgi->param('batch_id');
47 my $layout_id = $cgi->param('layout_id') || undef; 
48 my $template_id = $cgi->param('template_id') || undef; 
49 my $start_label = $cgi->param('start_label') || 1;
50 my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
51 my $output_format = $cgi->param('output_format') || 'pdf';
52 my $referer = $cgi->param('referer') || undef;
53
54 my $layouts = undef;
55 my $templates = undef;
56 my $label_output_formats = undef;
57 my @batches = ();
58 my $multi_batch_count = scalar(@batch_ids);
59 my $label_count = scalar(@label_ids);
60 my $item_count = scalar(@item_numbers);
61
62 if ($op eq 'export') {
63     if (@label_ids) {
64         my $label_id_param = '&label_id=';
65         $label_id_param .= join ('&label_id=',@label_ids);
66         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
67                          batch_id        => $batch_ids[0],
68                          template_id     => $template_id,
69                          layout_id       => $layout_id,
70                          start_label     => $start_label,
71                          label_ids       => $label_id_param,
72                          label_count     => scalar(@label_ids),
73                         });
74         $template->param(
75                         batches     => \@batches,
76                         referer     => $referer,
77                         );
78     }
79     elsif (@item_numbers) {
80         my $item_number_param = '&item_number=';
81         $item_number_param .= join ('&item_number=',@item_numbers);
82         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
83                          template_id     => $template_id,
84                          layout_id       => $layout_id,
85                          start_label     => $start_label,
86                          item_numbers    => $item_number_param,
87                          label_count     => scalar(@item_numbers),
88                         });
89         $template->param(
90                         batches     => \@batches,
91                         referer     => $referer,
92                         );
93     }
94     elsif (@batch_ids) {
95         foreach my $batch_id (@batch_ids) {
96            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
97                             batch_id        => $batch_id,
98                             template_id     => $template_id,
99                             layout_id       => $layout_id,
100                             start_label     => $start_label,
101                             });
102         }
103         $template->param(
104                         batches     => \@batches,
105                         referer     => $referer,
106                         );
107     }
108 }
109 elsif ($op eq 'none') {
110     # setup select menus for selecting layout and template for this run...
111     $referer = $ENV{'HTTP_REFERER'};
112     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
113     @batch_ids = grep{$_ = {batch_id => $_}} @batch_ids;
114     @label_ids = grep{$_ = {label_id => $_}} @label_ids;
115     @item_numbers = grep{$_ = {item_number => $_}} @item_numbers;
116     $templates = get_all_templates(field_list => 'template_id, template_code');
117     $layouts = get_all_layouts(field_list => 'layout_id, layout_name');
118     $label_output_formats = get_label_output_formats();
119     $template->param(
120                     batch_ids                   => \@batch_ids,
121                     label_ids                   => \@label_ids,
122                     item_numbers                => \@item_numbers,
123                     templates                   => $templates,
124                     layouts                     => $layouts,
125                     label_output_formats        => $label_output_formats,
126                     multi_batch_count           => $multi_batch_count,
127                     label_count                 => $label_count,
128                     item_count                  => $item_count,
129                     referer                     => $referer,
130                     );
131 }
132 output_html_with_http_headers $cgi, $cookie, $template->output;