Merge branch 'bug_8945' into 3.12-master
[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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use CGI;
24 use Data::Dumper;
25
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Creators::Lib qw(get_all_templates get_all_layouts get_output_formats);
29 use C4::Labels::Batch;
30
31 my $cgi = new CGI;
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33     {
34         template_name   => "labels/label-print.tmpl",
35         query           => $cgi,
36         type            => "intranet",
37         authnotrequired => 0,
38         flagsrequired   => { catalogue => 1 },
39         debug           => 1,
40     }
41 );
42
43 my $op = $cgi->param('op') || 'none';
44 my @label_ids;
45 @label_ids = $cgi->param('label_id') if $cgi->param('label_id');   # this will handle individual label printing
46 my @batch_ids;
47 @batch_ids = $cgi->param('batch_id') if $cgi->param('batch_id');
48 my $layout_id = $cgi->param('layout_id') || undef;
49 my $template_id = $cgi->param('template_id') || undef;
50 my $start_label = $cgi->param('start_label') || 1;
51 my @item_numbers;
52 @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
53 my $output_format = $cgi->param('output_format') || 'pdf';
54 my $referer = $cgi->param('referer') || undef;
55
56 my $layouts = undef;
57 my $templates = undef;
58 my $output_formats = undef;
59 my @batches = ();
60 my $multi_batch_count = scalar(@batch_ids);
61 my $label_count = scalar(@label_ids);
62 my $item_count = scalar(@item_numbers);
63
64 if ($op eq 'export') {
65     if (@label_ids) {
66         my $label_id_param = '&label_id=';
67         $label_id_param .= join ('&label_id=',@label_ids);
68         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
69                          batch_id        => $batch_ids[0],
70                          template_id     => $template_id,
71                          layout_id       => $layout_id,
72                          start_label     => $start_label,
73                          label_ids       => $label_id_param,
74                          label_count     => scalar(@label_ids),
75                         });
76         $template->param(
77                         batches     => \@batches,
78                         referer     => $referer,
79                         );
80     }
81     elsif (@item_numbers) {
82         my $item_number_param = '&item_number=';
83         $item_number_param .= join ('&item_number=',@item_numbers);
84         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
85                          template_id     => $template_id,
86                          layout_id       => $layout_id,
87                          start_label     => $start_label,
88                          item_numbers    => $item_number_param,
89                          label_count     => scalar(@item_numbers),
90                         });
91         $template->param(
92                         batches     => \@batches,
93                         referer     => $referer,
94                         );
95     }
96     elsif (@batch_ids) {
97         foreach my $batch_id (@batch_ids) {
98            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
99                             batch_id        => $batch_id,
100                             template_id     => $template_id,
101                             layout_id       => $layout_id,
102                             start_label     => $start_label,
103                             });
104         }
105         $template->param(
106                         batches     => \@batches,
107                         referer     => $referer,
108                         );
109     }
110 }
111 elsif ($op eq 'none') {
112     # setup select menus for selecting layout and template for this run...
113     $referer = $ENV{'HTTP_REFERER'};
114     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
115     @batch_ids = map{{batch_id => $_}} @batch_ids;
116     @label_ids = map{{label_id => $_}} @label_ids;
117     @item_numbers = map{{item_number => $_}} @item_numbers;
118     $templates = get_all_templates(field_list => 'template_id, template_code', filter => 'creator = "Labels"');
119     $layouts = get_all_layouts(field_list => 'layout_id, layout_name', filter => 'creator = "Labels"');
120     $output_formats = get_output_formats();
121     $template->param(
122                     batch_ids                   => \@batch_ids,
123                     label_ids                   => \@label_ids,
124                     item_numbers                => \@item_numbers,
125                     templates                   => $templates,
126                     layouts                     => $layouts,
127                     output_formats              => $output_formats,
128                     multi_batch_count           => $multi_batch_count,
129                     label_count                 => $label_count,
130                     item_count                  => $item_count,
131                     referer                     => $referer,
132                     );
133 }
134 output_html_with_http_headers $cgi, $cookie, $template->output;