Bug 17583: Mock the pref before tests
[koha.git] / labels / label-create-pdf.pl
blobc15c7e74055878f9634567bfe12c81e36f8a1568
1 #!/usr/bin/perl
3 # Copyright Chris Nighswonger 2009
5 # This file is part of Koha.
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.
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.
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>.
21 use strict;
22 use warnings;
24 use CGI qw ( -utf8 );
25 use C4::Auth;
26 use C4::Debug;
27 use C4::Creators;
28 use C4::Labels;
30 my $cgi = new CGI;
32 my ( undef, $loggedinuser, $cookie ) = get_template_and_user({
33 template_name => "labels/label-home.tt",
34 query => $cgi,
35 type => "intranet",
36 authnotrequired => 0,
37 flagsrequired => { tools => 'label_creator' },
38 debug => 1,
39 });
41 my $batch_id;
42 my @label_ids;
43 my @item_numbers;
44 $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id');
45 my $template_id = $cgi->param('template_id') || undef;
46 my $layout_id = $cgi->param('layout_id') || undef;
47 my $start_label = $cgi->param('start_label') || 1;
48 @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
49 @item_numbers = $cgi->multi_param('item_number') if $cgi->param('item_number');
51 my $items = undef;
55 my $pdf_file = (@label_ids || @item_numbers ? "label_single_" . scalar(@label_ids || @item_numbers) : "label_batch_$batch_id");
56 print $cgi->header( -type => 'application/pdf',
57 -encoding => 'utf-8',
58 -attachment => "$pdf_file.pdf",
61 our $pdf = C4::Creators::PDF->new(InitVars => 0);
62 my $batch = C4::Labels::Batch->retrieve(batch_id => $batch_id);
63 our $template = C4::Labels::Template->retrieve(template_id => $template_id, profile_id => 1);
64 my $layout = C4::Labels::Layout->retrieve(layout_id => $layout_id);
66 sub _calc_next_label_pos {
67 my ($row_count, $col_count, $llx, $lly) = @_;
68 if ($col_count < $template->get_attr('cols')) {
69 $llx = ($llx + $template->get_attr('label_width') + $template->get_attr('col_gap'));
70 $col_count++;
72 else {
73 $llx = $template->get_attr('left_margin');
74 if ($row_count == $template->get_attr('rows')) {
75 $pdf->Page();
76 $lly = ($template->get_attr('page_height') - $template->get_attr('top_margin') - $template->get_attr('label_height'));
77 $row_count = 1;
79 else {
80 $lly = ($lly - $template->get_attr('row_gap') - $template->get_attr('label_height'));
81 $row_count++;
83 $col_count = 1;
85 return ($row_count, $col_count, $llx, $lly);
88 sub _print_text {
89 my $label_text = shift;
90 foreach my $text_line (@$label_text) {
91 $pdf->Font($text_line->{'font'});
92 $pdf->FontSize( $text_line->{'font_size'} );
93 $pdf->Text( $text_line->{'text_llx'}, $text_line->{'text_lly'}, $text_line->{'line'} );
97 $| = 1;
99 # set the paper size
100 my $lowerLeftX = 0;
101 my $lowerLeftY = 0;
102 my $upperRightX = $template->get_attr('page_width');
103 my $upperRightY = $template->get_attr('page_height');
105 $pdf->Compress(1);
106 $pdf->Mbox($lowerLeftX, $lowerLeftY, $upperRightX, $upperRightY);
108 my ($row_count, $col_count, $llx, $lly) = $template->get_label_position($start_label);
110 if (@label_ids) {
111 my $batch_items = $batch->get_attr('items');
112 grep {
113 my $label_id = $_;
114 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
115 } @label_ids;
117 elsif (@item_numbers) {
118 grep {
119 push(@{$items}, {item_number => $_});
120 } @item_numbers;
122 else {
123 $items = $batch->get_attr('items');
126 LABEL_ITEMS:
127 foreach my $item (@{$items}) {
128 my ($barcode_llx, $barcode_lly, $barcode_width, $barcode_y_scale_factor) = 0,0,0,0;
129 if ($layout->get_attr('printing_type') eq 'ALT') { # we process the ALT style printing type here because it is not an atomic printing type
130 my $label_a = C4::Labels::Label->new(
131 batch_id => $batch_id,
132 item_number => $item->{'item_number'},
133 llx => $llx,
134 lly => $lly,
135 width => $template->get_attr('label_width'),
136 height => $template->get_attr('label_height'),
137 top_text_margin => $template->get_attr('top_text_margin'),
138 left_text_margin => $template->get_attr('left_text_margin'),
139 barcode_type => $layout->get_attr('barcode_type'),
140 printing_type => 'BIB',
141 guidebox => $layout->get_attr('guidebox'),
142 oblique_title => $layout->get_attr('oblique_title'),
143 font => $layout->get_attr('font'),
144 font_size => $layout->get_attr('font_size'),
145 callnum_split => $layout->get_attr('callnum_split'),
146 justify => $layout->get_attr('text_justify'),
147 format_string => $layout->get_attr('format_string'),
148 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 $pdf->Add($label_a->draw_guide_box) if $layout->get_attr('guidebox');
151 my $label_a_text = $label_a->create_label();
152 _print_text($label_a_text);
153 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
154 my $label_b = C4::Labels::Label->new(
155 batch_id => $batch_id,
156 item_number => $item->{'item_number'},
157 llx => $llx,
158 lly => $lly,
159 width => $template->get_attr('label_width'),
160 height => $template->get_attr('label_height'),
161 top_text_margin => $template->get_attr('top_text_margin'),
162 left_text_margin => $template->get_attr('left_text_margin'),
163 barcode_type => $layout->get_attr('barcode_type'),
164 printing_type => 'BAR',
165 guidebox => $layout->get_attr('guidebox'),
166 oblique_title => $layout->get_attr('oblique_title'),
167 font => $layout->get_attr('font'),
168 font_size => $layout->get_attr('font_size'),
169 callnum_split => $layout->get_attr('callnum_split'),
170 justify => $layout->get_attr('text_justify'),
171 format_string => $layout->get_attr('format_string'),
172 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 $pdf->Add($label_b->draw_guide_box) if $layout->get_attr('guidebox');
175 my $label_b_text = $label_b->create_label();
176 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
177 next LABEL_ITEMS;
179 else {
181 my $label = C4::Labels::Label->new(
182 batch_id => $batch_id,
183 item_number => $item->{'item_number'},
184 llx => $llx,
185 lly => $lly,
186 width => $template->get_attr('label_width'),
187 height => $template->get_attr('label_height'),
188 top_text_margin => $template->get_attr('top_text_margin'),
189 left_text_margin => $template->get_attr('left_text_margin'),
190 barcode_type => $layout->get_attr('barcode_type'),
191 printing_type => $layout->get_attr('printing_type'),
192 guidebox => $layout->get_attr('guidebox'),
193 oblique_title => $layout->get_attr('oblique_title'),
194 font => $layout->get_attr('font'),
195 font_size => $layout->get_attr('font_size'),
196 callnum_split => $layout->get_attr('callnum_split'),
197 justify => $layout->get_attr('text_justify'),
198 format_string => $layout->get_attr('format_string'),
199 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 $pdf->Add($label->draw_guide_box) if $layout->get_attr('guidebox');
202 my $label_text = $label->create_label();
203 _print_text($label_text) if $label_text;
204 ($row_count, $col_count, $llx, $lly) = _calc_next_label_pos($row_count, $col_count, $llx, $lly);
205 next LABEL_ITEMS;
208 $pdf->End();
210 __END__
212 =head1 NAME
214 labels/label-create-pdf.pl - A script for creating a pdf export of labels and label batches in Koha
216 =head1 ABSTRACT
218 This script provides the means of producing a pdf of labels for items either individually, in groups, or in batches from within Koha.
220 =head1 USAGE
222 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
223 parameters and two "multiple" parameters as follows:
225 C<batch_id> A single valid batch id to export.
226 C<template_id> A single valid template id to be applied to the current export. This parameter is manditory.
227 C<layout_id> A single valid layout id to be applied to the current export. This parameter is manditory.
228 C<start_label> The number of the label on which to begin the export. This parameter is optional.
229 C<lable_ids> A single valid label id to export. Multiple label ids may be submitted to export multiple labels.
230 C<item_numbers> A single valid item number to export. Multiple item numbers may be submitted to export multiple items.
232 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.
234 example:
235 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
237 =head1 AUTHOR
239 Chris Nighswonger <cnighswonger AT foundations DOT edu>
241 =head1 COPYRIGHT
243 Copyright 2009 Foundations Bible College.
245 =head1 LICENSE
247 This file is part of Koha.
249 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
250 Foundation; either version 2 of the License, or (at your option) any later version.
252 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
253 Fifth Floor, Boston, MA 02110-1301 USA.
255 =head1 DISCLAIMER OF WARRANTY
257 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
258 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
260 =cut