Bug 22922: Allow reservedate changes only if AllowHoldDateInFuture is on
[koha.git] / patroncards / create-pdf.pl
blob154377ce0ecdae2dc337e9fbff83661858db17ef
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 = new CGI;
39 my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
40 template_name => "labels/label-home.tt",
41 query => $cgi,
42 type => "intranet",
43 authnotrequired => 0,
44 flagsrequired => { tools => 'label_creator' },
45 debug => 1,
46 });
47 my $batch_id = $cgi->param('batch_id') if $cgi->param('batch_id');
48 my $template_id = $cgi->param('template_id') || undef;
49 my $layout_id = $cgi->param('layout_id') || undef;
50 my $layout_back_id = $cgi->param('layout_back_id') || undef;
51 my $start_card = $cgi->param('start_card') || 1;
52 my @label_ids = $cgi->multi_param('label_id') if $cgi->param('label_id');
53 my @borrower_numbers = $cgi->multi_param('borrower_number') if $cgi->param('borrower_number');
54 my $patronlist_id = $cgi->param('patronlist_id');
56 my $items = undef; # items = cards
57 my $new_page = 0;
59 # Wrap pdf creation part into an eval, some vars need scope outside eval
60 my $pdf_ok;
61 my $pdf;
62 my $pdf_file;
63 my $cardscount = 0;
65 #Note fo bug 14138: Indenting follows in separate patch to ease review
66 eval {
67 $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
69 $pdf = C4::Creators::PDF->new(InitVars => 0);
70 my $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
71 my $pc_template = C4::Patroncards::Template->retrieve(template_id => $template_id, profile_id => 1);
72 my $layout = C4::Patroncards::Layout->retrieve(layout_id => $layout_id);
73 my $layout_back = C4::Patroncards::Layout->retrieve(layout_id => $layout_back_id) if ( $layout_back_id );
75 $| = 1;
77 # set the paper size
78 my $lower_left_x = 0;
79 my $lower_left_y = 0;
80 my $upper_right_x = $pc_template->get_attr('page_width');
81 my $upper_right_y = $pc_template->get_attr('page_height');
83 $pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
84 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
86 my ($llx, $lly) = 0,0;
87 (undef, undef, $llx, $lly) = $pc_template->get_label_position($start_card);
89 if (@label_ids) {
90 my $batch_items = $batch->get_attr('items');
91 grep {
92 my $label_id = $_;
93 push(@{$items}, grep{$_->{'label_id'} == $label_id;} @{$batch_items});
94 } @label_ids;
96 elsif (@borrower_numbers) {
97 grep {
98 push(@{$items}, {borrower_number => $_});
99 } @borrower_numbers;
101 elsif ( $patronlist_id ) {
102 my ($list) = GetPatronLists( { patron_list_id => $patronlist_id } );
103 my @borrowerlist = $list->patron_list_patrons()->search_related('borrowernumber')
104 ->get_column('borrowernumber')->all();
105 grep {
106 push(@{$items}, {borrower_number => $_});
107 } @borrowerlist;
109 else {
110 $items = $batch->get_attr('items');
113 my $layout_xml = XMLin($layout->get_attr('layout_xml'), ForceArray => 1);
114 my $layout_back_xml = XMLin($layout_back->get_attr('layout_xml'), ForceArray => 1) if ( defined $layout_back );
116 if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page to swap columns
117 my $even = 1;
118 my $odd = 0;
119 my @swap_array = ();
120 while ($even <= (scalar(@{$items})+1)) {
121 push (@swap_array, @{$items}[$even]);
122 push (@swap_array, @{$items}[$odd]);
123 $even += 2;
124 $odd += 2;
126 @{$items} = @swap_array;
129 # WARNING: Referential nightmare ahead...
131 CARD_ITEMS:
132 foreach my $item (@{$items}) {
133 if ($item) {
134 my $print_layout_xml = (( ($cardscount % 2 == 1) && ( $layout_back_id ) ) ?
135 dclone($layout_back_xml) : dclone($layout_xml) ); # We must have a true copy of the layout xml hash, otherwise
136 # we modify the original template and very bad things happen.
138 $cardscount ++;
139 my $borrower_number = $item->{'borrower_number'};
140 my $card_number = Koha::Patrons->find( $borrower_number)->cardnumber;
142 # Set barcode data
143 $print_layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $print_layout_xml->{'barcode'};
145 # Create a new patroncard object
146 my $patron_card = C4::Patroncards::Patroncard->new(
147 batch_id => 1,
148 borrower_number => $borrower_number,
149 llx => $llx, # lower left corner of the card
150 lly => $lly,
151 height => $pc_template->get_attr('label_height'), # of the card
152 width => $pc_template->get_attr('label_width'),
153 layout => $print_layout_xml,
154 text_wrap_cols => 30, #FIXME: hardcoded,
157 $patron_card->draw_guide_box($pdf) if $print_layout_xml->{'guide_box'};
158 $patron_card->draw_guide_grid($pdf) if $print_layout_xml->{'guide_grid'};
159 $patron_card->draw_barcode($pdf) if $print_layout_xml->{'barcode'};
161 # Do image foo and place binary image data into layout hash
162 my $image_data = {};
163 my $error = undef;
164 my $images = $print_layout_xml->{'images'};
165 PROCESS_IMAGES:
166 foreach my $card_image (sort(keys %{$images})) {
167 if (grep{m/(source)/} keys(%{$images->{$card_image}->{'data_source'}->[0]})) {
168 if ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'none') {
170 elsif ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'patronimages') {
171 my $patron_image = Koha::Patron::Images->find($borrower_number);
172 if ($patron_image) {
173 $image_data->{'imagefile'} = $patron_image->imagefile;
175 else {
176 warn sprintf('No image exists for borrower number %s.', $borrower_number);
179 elsif ($images->{$card_image}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
180 ## FIXME: The DB stuff here needs to be religated to a Koha::Creator::Images object -chris_n
181 my $dbh = C4::Context->dbh();
182 $dbh->{LongReadLen} = 1000000; # allows us to read approx 1MB
183 $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$card_image}->{'data_source'}->[0]->{'image_name'}\'");
184 warn sprintf('Database returned the following error: %s.', $error) if $error;
185 unless($image_data){
186 warn sprintf('Image does not exists in db table %s.', $images->{$card_image}->{'data_source'}->[0]->{'image_name'});
189 else {
190 warn sprintf('No retrieval method for image source %s.', $images->{$card_image}->{'data_source'}->[0]->{'image_source'});
193 else {
194 warn sprintf("Unrecognized image data source: %s", $images->{$card_image}->{'data_source'});
197 my $binary_data = $image_data->{'imagefile'} || next PROCESS_IMAGES;
199 # invoke the display image object...
200 my $image = Graphics::Magick->new;
201 $image->BlobToImage($binary_data);
203 # invoke the alt (aka print) image object...
204 my $alt_image = Graphics::Magick->new;
205 $alt_image->BlobToImage($binary_data);
206 $alt_image->Set(magick => 'jpg', quality => 100);
208 #To avoid pixelation have the image 5 times bigger and
209 #scale it down in PDF itself
210 my $oversize_factor = 8;
211 my $pdf_scale_factor = 1 / $oversize_factor;
213 my $alt_width = ceil($image->Get('width')); # the rounding up is important: Adobe reader does not handle long decimal numbers well
214 my $alt_height = ceil($image->Get('height'));
215 my $ratio = $alt_width / $alt_height;
216 my $display_height = ceil($images->{$card_image}->{'Dx'});
217 my $display_width = ceil($ratio * $display_height);
220 $image->Resize(width => $oversize_factor * $display_width, height => $oversize_factor * $display_height);
221 $image->Set(magick => 'jpg', quality => 100);
223 # Write param for downsizing in pdf
224 $images->{$card_image}->{'scale'} = $pdf_scale_factor;
226 # Write params for alt image...
227 $images->{$card_image}->{'alt'}->{'Sx'} = $oversize_factor * $alt_width;
228 $images->{$card_image}->{'alt'}->{'Sy'} = $oversize_factor * $alt_height;
229 $images->{$card_image}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
231 # Write params for display image...
232 $images->{$card_image}->{'Sx'} = $oversize_factor * $display_width;
233 $images->{$card_image}->{'Sy'} = $oversize_factor * $display_height;
234 $images->{$card_image}->{'data'} = $image->ImageToBlob();
236 my $err = $patron_card->draw_image($pdf);
237 warn sprintf ("Error encountered while attempting to draw image %s, %s", $card_image, $err) if $err;
238 # Destroy all Graphics::Magick objects and related references
239 # or bad things will happen.
240 undef $image;
241 undef $alt_image;
242 undef $binary_data;
244 $patron_card->draw_text($pdf);
246 ($llx, $lly, $new_page) = $pc_template->get_next_label_pos();
248 if ( ($cardscount % 2 == 1) && ( $layout_back_id ) ) {
249 $pdf->Page();
250 redo; # Use same patron data again for backside in card printer
253 $pdf->Page() if $new_page;
255 # No errors occurred within eval, we can issue the pdf
256 $pdf_ok = 1 if ($cardscount > 0);
257 }; # end of eval block
259 if ($pdf_ok) {
260 #issue the pdf
261 print $cgi->header( -type => 'application/pdf',
262 -encoding => 'utf-8',
263 -attachment => "$pdf_file.pdf",
265 $pdf->End();
267 else {
268 # warn user that pdf is not created
269 my $errparams = '&pdferr=1';
270 $errparams .= "&errba=$batch_id" if $batch_id;
271 $errparams .= "&errpl=$patronlist_id" if $patronlist_id;
272 $errparams = $errparams.'&errpt='.$cgi->param('borrower_number') if $cgi->param('borrower_number');
273 $errparams .= "&errlo=$layout_id" if $layout_id;
274 $errparams .= "&errtpl=$template_id" if $template_id;
275 $errparams .= "&errnocards=1" if !$cardscount;
277 print $cgi->redirect("/cgi-bin/koha/patroncards/manage.pl?card_element=batch$errparams");