Bug 28591: Don't pass debug to get_template_and_user
[koha.git] / labels / label-create-pdf.pl
1 #!/usr/bin/perl
2
3 # Copyright Chris Nighswonger 2009
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 C4::Auth;
25 use C4::Debug;
26 use C4::Creators;
27 use C4::Labels;
28
29 my $cgi = CGI->new;
30
31 my ( undef, $loggedinuser, $cookie ) = get_template_and_user({
32                                                                      template_name   => "labels/label-home.tt",
33                                                                      query           => $cgi,
34                                                                      type            => "intranet",
35                                                                      flagsrequired   => { tools => 'label_creator' },
36                                                                      });
37
38 my $batch_id;
39 my @label_ids;
40 my @item_numbers;
41 $batch_id    = $cgi->param('batch_id') if $cgi->param('batch_id');
42 my $template_id = $cgi->param('template_id') || undef;
43 my $layout_id   = $cgi->param('layout_id') || undef;
44 my $start_label = $cgi->param('start_label') || 1;
45 @label_ids   = $cgi->multi_param('label_id') if $cgi->param('label_id');
46 @item_numbers  = $cgi->multi_param('item_number') if $cgi->param('item_number');
47 my $from = $cgi->param('from') || undef;
48 my $to = $cgi->param('to') || undef;
49
50 my $items = undef;
51
52 my $pdf_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
53 print $cgi->header( -type       => 'application/pdf',
54                     -encoding   => 'utf-8',
55                     -attachment => "$pdf_file.pdf",
56                   );
57
58 our $pdf = C4::Creators::PDF->new(InitVars => 0);
59 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
60 our $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
61 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
62
63 sub _calc_next_label_pos {
64     my ($row_count, $col_count, $llx, $lly) = @_;
65     if ($col_count < $template->get_attr('cols')) {
66         $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
67         $col_count++;
68     }
69     else {
70         $llx = $template->get_attr('left_margin');
71         if ($row_count == $template->get_attr('rows')) {
72             $pdf->Page();
73             $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
74             $row_count = 1;
75         }
76         else {
77             $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
78             $row_count++;
79         }
80         $col_count = 1;
81     }
82     return ($row_count, $col_count, $llx, $lly);
83 }
84
85 sub _print_text {
86     my $label_text = shift;
87     foreach my $text_line (@$label_text) {
88         $pdf->Font($text_line->{'font'});
89         $pdf->FontSize( $text_line->{'font_size'} );
90         $pdf->Text( $text_line->{'text_llx'}, $text_line->{'text_lly'}, $text_line->{'line'} );
91     }
92 }
93
94 $| = 1;
95
96 # set the paper size
97 my $lowerLeftX  = 0;
98 my $lowerLeftY  = 0;
99 my $upperRightX = $template->get_attr('page_width');
100 my $upperRightY = $template->get_attr('page_height');
101
102 $pdf->Compress(1);
103 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
104
105 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
106
107 if (@label_ids) {
108     my $batch_items = $batch->get_attr('items');
109     grep {
110         my $label_id = $_;
111         push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
112     } @label_ids;
113 }
114 elsif (@item_numbers) {
115     grep {
116         push(@{$items}, {item_number => $_});
117     } @item_numbers;
118 }
119 elsif ($from and $to) {
120     for (my $i = $from; $i <= $to; $i++) {
121         push @{$items}, {'item_number' => $i};
122     }
123 }
124 else {
125     $items = $batch->get_attr('items');
126 }
127
128 LABEL_ITEMS:
129 foreach my $item (@{$items}) {
130     my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
131     if ($layout->get_attr('printing_type') eq 'ALT') {  # we process the ALT style printing type here because it is not an atomic printing type
132         my $label_a = C4::Labels::Label->new(
133                                         batch_id            => $batch_id,
134                                         item_number         => $item->{'item_number'},
135                                         llx                 => $llx,
136                                         lly                 => $lly,
137                                         width               => $template->get_attr('label_width'),
138                                         height              => $template->get_attr('label_height'),
139                                         top_text_margin     => $template->get_attr('top_text_margin'),
140                                         left_text_margin    => $template->get_attr('left_text_margin'),
141                                         barcode_type        => $layout->get_attr('barcode_type'),
142                                         printing_type       => 'BIB',
143                                         guidebox            => $layout->get_attr('guidebox'),
144                                         oblique_title       => $layout->get_attr('oblique_title'),
145                                         font                => $layout->get_attr('font'),
146                                         font_size           => $layout->get_attr('font_size'),
147                                         callnum_split       => $layout->get_attr('callnum_split'),
148                                         justify             => $layout->get_attr('text_justify'),
149                                         format_string       => $layout->get_attr('format_string'),
150                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
151                                           );
152         $pdf->Add($label_a->draw_guide_box) if $layout->get_attr('guidebox');
153         my $label_a_text = $label_a->create_label();
154         _print_text($label_a_text);
155         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
156         my $label_b = C4::Labels::Label->new(
157                                         batch_id            => $batch_id,
158                                         item_number         => $item->{'item_number'},
159                                         llx                 => $llx,
160                                         lly                 => $lly,
161                                         width               => $template->get_attr('label_width'),
162                                         height              => $template->get_attr('label_height'),
163                                         top_text_margin     => $template->get_attr('top_text_margin'),
164                                         left_text_margin    => $template->get_attr('left_text_margin'),
165                                         barcode_type        => $layout->get_attr('barcode_type'),
166                                         printing_type       => 'BAR',
167                                         guidebox            => $layout->get_attr('guidebox'),
168                                         oblique_title       => $layout->get_attr('oblique_title'),
169                                         font                => $layout->get_attr('font'),
170                                         font_size           => $layout->get_attr('font_size'),
171                                         callnum_split       => $layout->get_attr('callnum_split'),
172                                         justify             => $layout->get_attr('text_justify'),
173                                         format_string       => $layout->get_attr('format_string'),
174                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
175                                           );
176         $pdf->Add($label_b->draw_guide_box) if $layout->get_attr('guidebox');
177         my $label_b_text = $label_b->create_label();
178         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
179         next LABEL_ITEMS;
180     }
181     else {
182     }
183         my $label = C4::Labels::Label->new(
184                                         batch_id            => $batch_id,
185                                         item_number         => $item->{'item_number'},
186                                         llx                 => $llx,
187                                         lly                 => $lly,
188                                         width               => $template->get_attr('label_width'),
189                                         height              => $template->get_attr('label_height'),
190                                         top_text_margin     => $template->get_attr('top_text_margin'),
191                                         left_text_margin    => $template->get_attr('left_text_margin'),
192                                         barcode_type        => $layout->get_attr('barcode_type'),
193                                         printing_type       => $layout->get_attr('printing_type'),
194                                         guidebox            => $layout->get_attr('guidebox'),
195                                         oblique_title       => $layout->get_attr('oblique_title'),
196                                         font                => $layout->get_attr('font'),
197                                         font_size           => $layout->get_attr('font_size'),
198                                         callnum_split       => $layout->get_attr('callnum_split'),
199                                         justify             => $layout->get_attr('text_justify'),
200                                         format_string       => $layout->get_attr('format_string'),
201                                         text_wrap_cols      => $layout->get_text_wrap_cols(label_width => $template->get_attr('label_width'), left_text_margin => $template->get_attr('left_text_margin')),
202                                           );
203         $pdf->Add($label->draw_guide_box) if $layout->get_attr('guidebox');
204         $label->{'barcode'} = $item->{'item_number'} if ($from and $to);
205         my $label_text = $label->create_label();
206         _print_text($label_text) if $label_text;
207         ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
208         next LABEL_ITEMS;
209 }
210
211 $pdf->End();
212
213 __END__
214
215 =head1 NAME
216
217 labels/label-create-pdf.pl - A script for creating a pdf export of labels and label batches in Koha
218
219 =head1 ABSTRACT
220
221 This script provides the means of producing a pdf of labels for items either individually, in groups, or in batches from within Koha.
222
223 =head1 USAGE
224
225 This script is intended to be called as a cgi script although it could be easily modified to accept command line parameters. The script accepts four single
226 parameters and two "multiple" parameters as follows:
227
228     C<batch_id>         A single valid batch id to export.
229     C<template_id>      A single valid template id to be applied to the current export. This parameter is manditory.
230     C<layout_id>        A single valid layout id to be applied to the current export. This parameter is manditory.
231     C<start_label>      The number of the label on which to begin the export. This parameter is optional.
232     C<lable_ids>        A single valid label id to export. Multiple label ids may be submitted to export multiple labels.
233     C<item_numbers>     A single valid item number to export. Multiple item numbers may be submitted to export multiple items.
234
235 B<NOTE:> One of the C<batch_id>, C<label_ids>, or C<item_number> parameters is manditory. However, do not pass a combination of them or bad things might result.
236
237     example:
238         http://staff-client.kohadev.org/cgi-bin/koha/labels/label-create-pdf.pl?batch_id=1&template_id=1&layout_id=5&start_label=1
239
240 =head1 AUTHOR
241
242 Chris Nighswonger <cnighswonger AT foundations DOT edu>
243
244 =head1 COPYRIGHT
245
246 Copyright 2009 Foundations Bible College.
247
248 =head1 LICENSE
249
250 This file is part of Koha.
251
252 Koha is free software; you can redistribute it and/or modify it
253 under the terms of the GNU General Public License as published by
254 the Free Software Foundation; either version 3 of the License, or
255 (at your option) any later version.
256
257 Koha is distributed in the hope that it will be useful, but
258 WITHOUT ANY WARRANTY; without even the implied warranty of
259 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
260 GNU General Public License for more details.
261
262 You should have received a copy of the GNU General Public License
263 along with Koha; if not, see <http://www.gnu.org/licenses>.
264
265 =head1 DISCLAIMER OF WARRANTY
266
267 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
268 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
269
270 =cut