Bug 30477: Add new UNIMARC installer translation files
[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
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 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23
24 use C4::Context;
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Creators::Lib qw(
28     get_all_layouts
29     get_all_templates
30     get_output_formats
31 );
32 use C4::Labels::Batch;
33
34 my $cgi = CGI->new;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "labels/label-print.tt",
38         query           => $cgi,
39         type            => "intranet",
40         flagsrequired   => { catalogue => 1 },
41     }
42 );
43
44 my $op = $cgi->param('op') || 'none';
45 my @label_ids;
46 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');   # this will handle individual label printing
47 my @batch_ids;
48 @batch_ids = $cgi->multi_param('batch_id') if $cgi->param('batch_id');
49 my $layout_id = $cgi->param('layout_id') || undef;
50 my $template_id = $cgi->param('template_id') || undef;
51 my $start_label = $cgi->param('start_label') || 1;
52 my @item_numbers;
53 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
54 my $output_format = $cgi->param('output_format') || 'pdf';
55 my $referer = $cgi->param('referer') || undef;
56
57 my $txt_from = $cgi->param('from') || undef;
58 my $txt_to = $cgi->param('to') || undef;
59 my $from = int($txt_from) || undef;
60 my $to = int($txt_to) || undef;
61 my $barcode_length = length($txt_from) || undef;
62
63 my $layouts = undef;
64 my $templates = undef;
65 my $output_formats = undef;
66 my @batches = ();
67 my $multi_batch_count = scalar(@batch_ids) || ($from && $to) ? 1 : 0;
68 my $label_count = scalar(@label_ids);
69 my $item_count = scalar(@item_numbers);
70
71 if ($op eq 'export') {
72     if (@label_ids) {
73         my $label_id_param = '&amp;label_id=';
74         $label_id_param .= join ('&amp;label_id=',@label_ids);
75         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
76                          batch_id        => $batch_ids[0],
77                          template_id     => $template_id,
78                          layout_id       => $layout_id,
79                          start_label     => $start_label,
80                          label_ids       => $label_id_param,
81                          label_count     => scalar(@label_ids),
82                         });
83         $template->param(
84                         batches     => \@batches,
85                         referer     => $referer,
86                         );
87     }
88     elsif (@item_numbers) {
89         my $item_number_param = '&amp;item_number=';
90         $item_number_param .= join ('&amp;item_number=',@item_numbers);
91         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
92                          template_id     => $template_id,
93                          layout_id       => $layout_id,
94                          start_label     => $start_label,
95                          item_numbers    => $item_number_param,
96                          label_count     => scalar(@item_numbers),
97                         });
98         $template->param(
99                         batches     => \@batches,
100                         referer     => $referer,
101                         );
102     }
103     elsif (@batch_ids) {
104         foreach my $batch_id (@batch_ids) {
105            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'label-create-pdf.pl' : 'label-create-csv.pl'),
106                             batch_id        => $batch_id,
107                             template_id     => $template_id,
108                             layout_id       => $layout_id,
109                             start_label     => $start_label,
110                             });
111         }
112         $template->param(
113                         batches     => \@batches,
114                         referer     => $referer,
115                         );
116     }
117     elsif ($from and $to) {
118         my $dbh = C4::Context->dbh;
119
120         my $sth = $dbh->prepare('SELECT COUNT(*) AS has_barcode FROM creator_layouts WHERE printing_type LIKE("%BAR%") AND layout_id = ?;');
121         $sth->execute($layout_id);
122         if ($sth->fetchrow_hashref->{'has_barcode'} == 0) {
123             $sth = $dbh->prepare('SELECT COUNT(*) AS existing_count FROM items WHERE CAST(barcode AS unsigned) BETWEEN ? AND ?;');
124             $sth->execute($from, $to);
125             if ($sth->fetchrow_hashref->{'existing_count'} < ($to - $from + 1)) {
126                 $template->param( warn_empty_range => 1 )
127             }
128         }
129
130         push (@batches, {create_script   => 'label-create-pdf.pl',
131                  from            => $from,
132                  to              => $to,
133                  barcode_length  => $barcode_length,
134                  template_id     => $template_id,
135                  layout_id       => $layout_id,
136                  start_label     => $start_label,
137                  });
138        $template->param(
139                         batches     => \@batches,
140                         referer     => $referer,
141                         );
142     }
143 }
144 elsif ($op eq 'none') {
145     # setup select menus for selecting layout and template for this run...
146     $referer = $ENV{'HTTP_REFERER'};
147     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
148     @batch_ids = map{{batch_id => $_}} @batch_ids;
149     @label_ids = map{{label_id => $_}} @label_ids;
150     @item_numbers = map{{item_number => $_}} @item_numbers;
151     $templates = get_all_templates( { fields => [ qw( template_id template_code ) ], filters => { creator => "Labels" }, orderby => 'template_code' } );
152     $layouts = get_all_layouts( { fields => [ qw( layout_id layout_name ) ], filters => { creator => "Labels" }, orderby => 'layout_name' } );
153     $output_formats = get_output_formats();
154     $template->param(
155                     batch_ids                   => \@batch_ids,
156                     label_ids                   => \@label_ids,
157                     item_numbers                => \@item_numbers,
158                     templates                   => $templates,
159                     layouts                     => $layouts,
160                     output_formats              => $output_formats,
161                     multi_batch_count           => $multi_batch_count,
162                     label_count                 => $label_count,
163                     item_count                  => $item_count,
164                     referer                     => $referer,
165                     from                        => $from,
166                     to                          => $to,
167                     barcode_length              => $barcode_length,
168                     txt_from                    => $txt_from,
169                     txt_to                      => $txt_to
170                     );
171 }
172 output_html_with_http_headers $cgi, $cookie, $template->output;