Merge remote branch 'koha-fbc/k_bug_5203' into master
[koha.git] / patroncards / create-pdf.pl
index 04b1684..f063e9b 100755 (executable)
@@ -13,9 +13,9 @@
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# 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, Fifth Floor, Boston, MA 02110-1301 USA.
 
 use strict;
 use warnings;
@@ -29,11 +29,8 @@ use autouse 'Data::Dumper' => qw(Dumper);
 use C4::Debug;
 use C4::Context;
 use autouse 'C4::Members' => qw(GetPatronImage GetMember);
-use C4::Creators::PDF 1.000000;
-use C4::Patroncards::Batch 1.000000;
-use C4::Patroncards::Template 1.000000;
-use C4::Patroncards::Layout 1.000000;
-use C4::Patroncards::Patroncard 1.000000;
+use C4::Creators 1.000000;
+use C4::Patroncards 1.000000;
 
 my $cgi = new CGI;
 
@@ -44,7 +41,8 @@ my $start_label = $cgi->param('start_label') || 1;
 my @label_ids   = $cgi->param('label_id') if $cgi->param('label_id');
 my @borrower_numbers  = $cgi->param('borrower_number') if $cgi->param('borrower_number');
 
-my $items = undef;      # items = cards
+my $items = undef; # items = cards
+my $new_page = 0;
 
 my $pdf_file = (@label_ids || @borrower_numbers ? "card_single_" . scalar(@label_ids || @borrower_numbers) : "card_batch_$batch_id");
 print $cgi->header( -type       => 'application/pdf',
@@ -65,7 +63,7 @@ my $lower_left_y  = 0;
 my $upper_right_x = $template->get_attr('page_width');
 my $upper_right_y = $template->get_attr('page_height');
 
-$pdf->Compress(1);
+$pdf->Compress(1); # comment this out to debug pdf files, but be sure to uncomment it in production or you may be very sorry...
 $pdf->Mbox($lower_left_x, $lower_left_y, $upper_right_x, $upper_right_y);
 
 my ($llx, $lly) = 0,0;
@@ -104,13 +102,12 @@ if ($layout_xml->{'page_side'} eq 'B') { # rearrange items on backside of page t
 
 CARD_ITEMS:
 foreach my $item (@{$items}) {
-    my $new_page = 0; #FIXME: this needs to be implimented or removed
     if ($item) {
         my $borrower_number = $item->{'borrower_number'};
         my $card_number = GetMember(borrowernumber => $borrower_number)->{'cardnumber'};
 
 #       Set barcode data
-        $layout_xml->{'barcode'}->{'data'} = $card_number if $layout_xml->{'barcode'};
+        $layout_xml->{'barcode'}->[0]->{'data'} = $card_number if $layout_xml->{'barcode'};
 
 #       Create a new patroncard object
         my $patron_card = C4::Patroncards::Patroncard->new(
@@ -123,7 +120,7 @@ foreach my $item (@{$items}) {
                 layout                  => $layout_xml,
                 text_wrap_cols          => 30, #FIXME: hardcoded
         );
-        $patron_card->draw_guide_box($pdf);
+        $patron_card->draw_guide_box($pdf) if $layout_xml->{'guide_box'};
         $patron_card->draw_barcode($pdf) if $layout_xml->{'barcode'};
 
 #       Do image foo and place binary image data into layout hash
@@ -144,13 +141,13 @@ foreach my $item (@{$items}) {
                 elsif ($images->{$_}->{'data_source'}->[0]->{'image_source'} eq 'creator_images') {
                     my $dbh = C4::Context->dbh();
                     $dbh->{LongReadLen} = 1000000;      # allows us to read approx 1MB
-                    $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$$layout_xml{'images'}{$_}{'data_source'}{'image_name'}\'");
+                    $image_data = $dbh->selectrow_hashref("SELECT imagefile FROM creator_images WHERE image_name = \'$images->{$_}->{'data_source'}->[0]->{'image_name'}\'");
                     warn sprintf('Database returned the following error: %s.', $error) if $error;
-                    warn sprintf('Image does not exists in db table %s.', $$layout_xml{'images'}{$_}{'data_source'}{'image_source'}) if !$image_data;
+                    warn sprintf('Image does not exists in db table %s.', $images->{$_}->{'data_source'}->[0]->{'image_name'}) if !$image_data;
                     next PROCESS_IMAGES if !$image_data;
                 }
                 else {
-                    warn sprintf('No retrieval method for image source %s.', $$layout_xml{'images'}{$_}{'data_source'}{'image_source'});
+                    warn sprintf('No retrieval method for image source %s.', $images->{$_}->{'data_source'}->[0]->{'image_source'});
                     next PROCESS_IMAGES;
                 }
             }
@@ -196,11 +193,11 @@ foreach my $item (@{$items}) {
         $patron_card->draw_text($pdf);
     }
     ($llx, $lly, $new_page) = $template->get_next_label_pos();
-    #$pdf->Page() if $new_page;
+    $pdf->Page() if $new_page;
 }
 
 $pdf->End();
 
 # FIXME: Possibly do a redirect here if there were error encountered during PDF creation.
 
-exit 0;
+1;