Fix FSF address in directory t/
[srvgit] / t / db_dependent / Patroncards / t_Patroncards.t
1 #!/usr/bin/perl
2 #
3 # Copyright 2009 Foundations Bible College.
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 ## NOTE: This is really broken at this point and needs updating... -chris_n
21
22 use strict;
23 use warnings;
24
25 #use Test::More;
26 use Graphics::Magick;
27 use XML::Simple;
28 use autouse 'Data::Dumper' => qw(Dumper);
29
30 use C4::Context;
31 use autouse 'C4::Members' => qw(GetPatronImage);
32 use C4::Creators::PDF 1.000000;
33 use C4::Patroncards::Patroncard 1.000000;
34
35 #BEGIN {
36 #    use_ok('C4::Patroncards::Patroncard');
37 #}
38
39 my $borrower_number = 3;
40 my $pdf = C4::Creators::PDF->new(InitVars => 0);
41
42 #   All units are in PostScript points in this script
43 $pdf->Mbox(0, 0, 612, 792);
44
45 # Convert layout to XML and insert into db
46 #my $query = "INSERT INTO creator_layouts (layout_id,layout_xml) VALUES (1,?) ON DUPLICATE KEY UPDATE layout_xml=?;";
47 #my $sth = C4::Context->dbh->prepare($query);
48 my $layout =
49 {
50         barcode                 => {
51                                     type => 'CODE39',
52                                     llx => 26,
53                                     lly => 4.5,
54                                     data => '0002120108',
55                                     text => 1,  # controls text_under_barcode display: 1 => visible, 0 => not visible
56         },
57         text                    => [
58                                     'John Doe' => {
59                                                                 font => 'TB',
60                                                                 font_size => 13,
61                                                                 llx => 63,      # llx & lly are relative to the llx & lly of the card NOT the page
62                                                                 lly => 103.5,
63                                                                 alignment => 'C',
64                                     },
65                                     'Anytown Public Library' => {
66                                                                 font => 'TR',
67                                                                 font_size => 8,
68                                                                 llx => 63,
69                                                                 lly => 85.5,
70                                                                 alignment => 'C',
71                                     },
72                                     'Line 3 Text' => {
73                                                                 font => 'TB',
74                                                                 font_size => 10,
75                                                                 llx => 63,
76                                                                 lly => 76.5,
77                                                                 alignment => 'C',
78                                     },
79         ],
80         images                  => {
81                                     patron_image   => {
82                                                             data_source => {'db' => 'patronimages',
83                                                                             'card_number' => '0002120108',
84                                                             },
85                                                             Sx          => 0,
86                                                             Sy          => 0,
87                                                             Ox          => 0,   # Ox,Oy should be set to 0 unless you want special effects  see http://www.adobe.com/devnet/pdf/pdf_reference.html ISO 32000-1
88                                                             Oy          => 0,
89                                                             Tx          => 4.5,  # Lower left corner of image relative to the lower left corner of the card
90                                                             Ty          => 63,
91                                                             Dx          => 72,  # point height of display image
92                                     },
93                                     image_1        => {
94                                                             data_source => {'db' => 'pcards_images',
95                                                                             'image_id' => 1,
96                                                             },
97                                                             Sx          => 0,
98                                                             Sy          => 0,
99                                                             Ox          => 0,   # Ox,Oy should be set to 0 unless you want special effects  see http://www.adobe.com/devnet/pdf/pdf_reference.html ISO 32000-1
100                                                             Oy          => 0,
101                                                             Tx          => 100,  # Lower left corner of image
102                                                             Ty          => 50,
103                                                             Dx          => 81,
104                                     },
105         },
106 };
107
108 #$sth->execute(XMLout($layout),XMLout($layout));
109
110 # Retrieve XML from database
111 my$query = "SELECT layout_xml FROM creator_layouts WHERE layout_id=20;";
112 my $xml = C4::Context->dbh->selectrow_array($query);
113
114 # Convert back to hash
115 $layout = XMLin($xml);
116
117 # Do image foo and place binary image data into layout hash
118 my $image_data = {};
119 my $alt_image_data = {};
120 my $error = undef;
121 foreach (keys %{$layout->{'images'}}) {
122     warn "Processing key: $_\n";
123     if (grep{m/db/} keys(%{$layout->{'images'}->{$_}->{'data_source'}})) {
124         warn $layout->{'images'}->{$_}->{'data_source'}->{'db'};
125         if ($layout->{'images'}->{$_}->{'data_source'}->{'db'} eq 'patronimages') {
126             warn "Processing patron image data.\n";
127             ($image_data, $error) = GetPatronImage($layout->{'images'}->{$_}->{'data_source'}->{'card_number'});
128             warn sprintf('No image exists for borrower number %s.', $borrower_number) if !$image_data;
129             next if !$image_data;
130         }
131         elsif ($layout->{'images'}->{$_}->{'data_source'}->{'db'} eq 'pcards_images') {
132             warn "Processing pcards image data for image_id " . $$layout{'images'}{$_}{'data_source'}{'image_id'} . ".\n";
133             my $dbh = C4::Context->dbh();
134             $dbh->{LongReadLen} = 1000000;      # allows us to read approx 1MB
135             $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM pcards_images WHERE image_id = \'$$layout{'images'}{$_}{'data_source'}{'image_id'}\'");
136             warn sprintf('Image does not exists in db table %s.', $$layout{'images'}{$_}{'data_source'}{'db'}) if !$image_data;
137             next if !$image_data;
138         #    $string =~ s/(.)/sprintf("%x",ord($1))/eg;
139         }
140         else {
141             warn sprintf('No retrieval method for table %s.', $$layout{'images'}{$_}{'data_source'}{'db'}) if !$image_data;
142         }
143         warn sprintf('Database returned the following error: %s.', $error) if $error;
144     }
145     elsif (grep{m/foo/} keys(%{$layout->{'images'}->{$_}->{'data_source'}})) {
146         # some other image storage/retrieval method
147     }
148
149 my $binary_data = $image_data->{'imagefile'};
150 my $image = Graphics::Magick->new;
151 $image->BlobToImage($binary_data);
152 my $alt_image = Graphics::Magick->new;
153 $alt_image->BlobToImage($binary_data);
154 $alt_image->Set(magick => 'jpg', quality => 100);
155
156 warn "Print Image Dimensions: " . $image->Get('width') . " X " . $image->Get('height') . "\n";
157
158 my $alt_width = $image->Get('width');
159 my $alt_height = $image->Get('height');
160 my $ratio = $alt_width / $alt_height;
161 my $display_height = $layout->{'images'}->{$_}->{'Dx'};
162 my $display_width = $ratio * $display_height;
163 $image->Resize(width => $display_width, height => $display_height);
164 $image->Set(magick => 'jpg', quality => 100);
165
166 warn "Display Image dimensions: " . $image->Get('width') . " X " . $image->Get('height') . "\n";
167
168 #    Write params for alt image...
169     $layout->{'images'}->{$_}->{'alt'}->{'Sx'} = $alt_width;
170     $layout->{'images'}->{$_}->{'alt'}->{'Sy'} = $alt_height;
171     $layout->{'images'}->{$_}->{'alt'}->{'data'} = $alt_image->ImageToBlob();
172 ##    Write params for display image...
173     $layout->{'images'}->{$_}->{'Sx'} = $display_width;
174     $layout->{'images'}->{$_}->{'Sy'} = $display_height;
175     $layout->{'images'}->{$_}->{'data'} = $image->ImageToBlob();
176 }
177 die "BREAKPOINT...";
178 # Create a new patroncard object
179 my $patron_card = C4::Patroncards::Patroncard->new(
180         batch_id                => 1,
181         borrower_number         => $borrower_number,
182         llx                     => 36,#13.5        # lower left corner of the card
183         lly                     => 639,
184         height                  => 139.5,       # of the card
185         width                   => 229.5,
186         layout                  => $layout,
187         text_wrap_cols          => 30,
188 );
189 $patron_card->draw_guide_box($pdf);
190 $patron_card->draw_barcode($pdf);
191 my $err = $patron_card->draw_image($pdf);
192 $patron_card->draw_text($pdf);
193 $pdf->End;
194 exit(1);