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