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