Bug 26911: (bug 18936 follow-up) Remove invalid data before copying the circ rules
[koha.git] / patroncards / create-pdf.pl
blob1ffaa0002a9d2e736cc0508c2ec6028c782c7258
1 #!/usr/bin/perl
3 # Copyright 2009 Foundations Bible College.
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>.
20 use Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Auth;
23 use Graphics::Magick;
24 use XML::Simple;
25 use POSIX qw(ceil);
26 use Storable qw(dclone);
27 use autouse 'Data::Dumper' => qw(Dumper);
29 use C4::Debug;
30 use C4::Context;
31 use C4::Creators;
32 use C4::Patroncards;
33 use Koha::List::Patron;
34 use Koha::Patrons;
35 use Koha::Patron::Images;
37 my $cgi = CGI->new;
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
40 template_name => "labels/label-home.tt",
41 query => $cgi,
42 type => "intranet",
43 flagsrequired => { tools => 'label_creator' },
44 debug => 1,
45 });
46 my $batch_id = $cgi->param('batch_id') || undef;
47 my $template_id = $cgi->param('template_id') || undef;
48 my $layout_id = $cgi->param('layout_id') || undef;
49 my $layout_back_id = $cgi->param('layout_back_id') || undef;
50 my $start_card = $cgi->param('start_card') || 1;
51 my @label_ids = $cgi->multi_param('label_id');
52 my @borrower_numbers = $cgi->multi_param('borrower_number');
53 my $patronlist_id = $cgi->param('patronlist_id');
55 my $items = undef; # items = cards
56 my $new_page = 0;
58 # Wrap pdf creation part into an eval, some vars need scope outside eval
59 my $pdf_ok;
60 my $pdf;
61 my $pdf_file;
62 my $cardscount = 0;
64 #Note fo bug 14138: Indenting follows in separate patch to ease review
65 eval {
66 $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
68 $pdf = C4::Creators::PDF->new(InitVars => 0);
69 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
70 my $pc_template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
71 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
72 my $layout_back = $layout_back_id ? C4::Patroncards::Layout->retrieve(layout_id => $layout_back_id) : undef;
74 $| = 1;
76 # set the paper size
77 my $lower_left_x = 0;
78 my $lower_left_y = 0;
79 my $upper_right_x = $pc_template->get_attr('page_width');
80 my $upper_right_y = $pc_template->get_attr('page_height');
82 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
83 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
85 my ($llx, $lly) = 0,0;
86 (undef, undef, $llx, $lly) = $pc_template->get_label_position($start_card);
88 if (@label_ids) {
89 my $batch_items = $batch->get_attr('items');
90 grep {
91 my $label_id = $_;
92 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
93 } @label_ids;
95 elsif (@borrower_numbers) {
96 grep {
97 push(@{$items}, {borrower_number => $_});
98 } @borrower_numbers;
100 elsif ( $patronlist_id ) {
101 my ($list) = GetPatronLists( { patron_list_id => $patronlist_id } );
102 my @borrowerlist = $list->patron_list_patrons()->search_related('borrowernumber')
103 ->get_column('borrowernumber')->all();
104 grep {
105 push(@{$items}, {borrower_number => $_});
106 } @borrowerlist;
108 else {
109 $items = $batch->get_attr('items');
112 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
113 my $layout_back_xml = defined $layout_back ? XMLin($layout_back->get_attr('layout_xml'), ForceArray => 1) : undef;
115 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
116 my $even = 1;
117 my $odd = 0;
118 my @swap_array = ();
119 while ($even <= (scalar(@{$items})+1)) {
120 push (@swap_array, @{$items}[$even]);
121 push (@swap_array, @{$items}[$odd]);
122 $even += 2;
123 $odd += 2;
125 @{$items} = @swap_array;
128 # WARNING: Referential nightmare ahead...
130 CARD_ITEMS:
131 foreach my $item (@{$items}) {
132 if ($item) {
133 my $print_layout_xml = (( ($cardscount % 2 == 1) && ( $layout_back_id ) ) ?
134 dclone($layout_back_xml) : dclone($layout_xml) ); # We must have a true copy of the layout xml hash, otherwise
135 # we modify the original template and very bad things happen.
137 $cardscount ++;
138 my $borrower_number = $item->{'borrower_number'};
139 my $card_number = Koha::Patrons->find( $borrower_number)->cardnumber;
141 # Set barcode data
142 $print_layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $print_layout_xml->{'barcode'};
144 # Create a new patroncard object
145 my $patron_card = C4::Patroncards::Patroncard->new(
146 batch_id => 1,
147 borrower_number => $borrower_number,
148 llx => $llx, # lower left corner of the card
149 lly => $lly,
150 height => $pc_template->get_attr('label_height'), # of the card
151 width => $pc_template->get_attr('label_width'),
152 layout => $print_layout_xml,
153 text_wrap_cols => 30, #FIXME: hardcoded,
156 $patron_card->draw_guide_box($pdf) if $print_layout_xml->{'guide_box'};
157 $patron_card->draw_guide_grid($pdf) if $print_layout_xml->{'guide_grid'};
158 $patron_card->draw_barcode($pdf) if $print_layout_xml->{'barcode'};
160 # Do image foo and place binary image data into layout hash
161 my $image_data = {};
162 my $error = undef;
163 my $images = $print_layout_xml->{'images'};
164 PROCESS_IMAGES:
165 foreach my $card_image (sort(keys %{$images})) {
166 if (grep{m/(source)/} keys(%{$images->{$card_image}->{'data_source'}->[0]})) {
167 if ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'none') {
169 elsif ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
170 my $patron_image = Koha::Patron::Images->find($borrower_number);
171 if ($patron_image) {
172 $image_data->{'imagefile'} = $patron_image->imagefile;
174 else {
175 warn sprintf('No image exists for borrower number %s.', $borrower_number);
178 elsif ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
179 ## FIXME: The DB stuff here needs to be religated to a Koha::Creator::Images object -chris_n
180 my $dbh = C4::Context->dbh();
181 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
182 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$card_image}->{'data_source'}->[0]->{'image_name'}\'");
183 warn sprintf('Database returned the following error: %s.', $error) if $error;
184 unless($image_data){
185 warn sprintf('Image does not exists in db table %s.', $images->{$card_image}->{'data_source'}->[0]->{'image_name'});
188 else {
189 warn sprintf('No retrieval method for image source %s.', $images->{$card_image}->{'data_source'}->[0]->{'image_source'});
192 else {
193 warn sprintf("Unrecognized image data source: %s", $images->{$card_image}->{'data_source'});
196 my $binary_data = $image_data->{'imagefile'} || next PROCESS_IMAGES;
198 # invoke the display image object...
199 my $image = Graphics::Magick->new;
200 $image->BlobToImage($binary_data);
202 # invoke the alt (aka print) image object...
203 my $alt_image = Graphics::Magick->new;
204 $alt_image->BlobToImage($binary_data);
205 $alt_image->Set(magick => 'jpg', quality => 100);
207 #To avoid pixelation have the image 5 times bigger and
208 #scale it down in PDF itself
209 my $oversize_factor = 8;
210 my $pdf_scale_factor = 1 / $oversize_factor;
212 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
213 my $alt_height = ceil($image->Get('height'));
214 my $ratio = $alt_width / $alt_height;
215 my $display_height = ceil($images->{$card_image}->{'Dx'});
216 my $display_width = ceil($ratio * $display_height);
219 $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height);
220 $image->Set(magick => 'jpg', quality => 100);
222 # Write param for downsizing in pdf
223 $images->{$card_image}->{'scale'} = $pdf_scale_factor;
225 # Write params for alt image...
226 $images->{$card_image}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width;
227 $images->{$card_image}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height;
228 $images->{$card_image}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
230 # Write params for display image...
231 $images->{$card_image}->{'Sx'} = $oversize_factor * $display_width;
232 $images->{$card_image}->{'Sy'} = $oversize_factor * $display_height;
233 $images->{$card_image}->{'data'} = $image->ImageToBlob();
235 my $err = $patron_card->draw_image($pdf);
236 warn sprintf ("Error encountered while attempting to draw image %s, %s", $card_image, $err) if $err;
237 # Destroy all Graphics::Magick objects and related references
238 # or bad things will happen.
239 undef $image;
240 undef $alt_image;
241 undef $binary_data;
243 $patron_card->draw_text($pdf);
245 ($llx, $lly, $new_page) = $pc_template->get_next_label_pos();
247 if ( ($cardscount % 2 == 1) && ( $layout_back_id ) ) {
248 $pdf->Page();
249 redo; # Use same patron data again for backside in card printer
252 $pdf->Page() if $new_page;
254 # No errors occurred within eval, we can issue the pdf
255 $pdf_ok = 1 if ($cardscount > 0);
256 }; # end of eval block
258 if ($pdf_ok) {
259 #issue the pdf
260 print $cgi->header( -type => 'application/pdf',
261 -encoding => 'utf-8',
262 -attachment => "$pdf_file.pdf",
264 $pdf->End();
266 else {
267 # warn user that pdf is not created
268 my $errparams = '&pdferr=1';
269 $errparams .= "&errba=$batch_id" if $batch_id;
270 $errparams .= "&errpl=$patronlist_id" if $patronlist_id;
271 $errparams = $errparams.'&errpt='.$cgi->param('borrower_number') if $cgi->param('borrower_number');
272 $errparams .= "&errlo=$layout_id" if $layout_id;
273 $errparams .= "&errtpl=$template_id" if $template_id;
274 $errparams .= "&errnocards=1" if !$cardscount;
276 print $cgi->redirect("/cgi-bin/koha/patroncards/manage.pl?card_element=batch$errparams");