Merge branch 'master' of git://git.koha-community.org/koha into MSWin32
[wip/koha-chris_n.git] / patroncards / print.pl
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 use strict;
21 use warnings;
22
23 use CGI;
24 use autouse 'Data::Dumper' => qw(Dumper);
25
26 use C4::Auth qw(get_template_and_user);
27 use C4::Output qw(output_html_with_http_headers);
28 use C4::Creators 1.000000;
29 use C4::Patroncards 1.000000;
30
31 my $cgi = new CGI;
32 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33     {
34         template_name   => "patroncards/print.tmpl",
35         query           => $cgi,
36         type            => "intranet",
37         authnotrequired => 0,
38         flagsrequired   => { catalogue => 1 },
39         debug           => 1,
40     }
41 );
42
43 my $op = $cgi->param('op') || 'none';
44 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');   # this will handle individual card printing; we use label_id to maintain consistency with the column names in the creator_batches table
45 my @batch_ids = $cgi->param('batch_id') if $cgi->param('batch_id');
46 my $layout_id = $cgi->param('layout_id') || undef;
47 my $template_id = $cgi->param('template_id') || undef;
48 my $start_card = $cgi->param('start_card') || 1;
49 my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number');
50 my $output_format = $cgi->param('output_format') || 'pdf';
51 my $referer = $cgi->param('referer') || undef;
52
53 my $layouts = undef;
54 my $templates = undef;
55 my $output_formats = undef;
56 my @batches = ();
57 my $multi_batch_count = scalar(@batch_ids);
58 my $card_count = scalar(@label_ids);
59 my $borrower_count = scalar(@borrower_numbers);
60
61 if ($op eq 'export') {
62     if (@label_ids) {
63         my $label_id_param = '&label_id=';
64         $label_id_param .= join ('&label_id=',@label_ids);
65         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
66                          batch_id        => $batch_ids[0],
67                          template_id     => $template_id,
68                          layout_id       => $layout_id,
69                          start_card      => $start_card,
70                          label_ids       => $label_id_param,
71                          card_count      => scalar(@label_ids),
72                         });
73         $template->param(
74                         batches     => \@batches,
75                         referer     => $referer,
76                         );
77     }
78     elsif (@borrower_numbers) {
79         my $borrower_number_param = '&borrower_number=';
80         $borrower_number_param .= join ('&borrower_number=',@borrower_numbers);
81         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
82                          template_id     => $template_id,
83                          layout_id       => $layout_id,
84                          start_card      => $start_card,
85                          borrower_numbers    => $borrower_number_param,
86                          card_count      => scalar(@borrower_numbers),
87                         });
88         $template->param(
89                         batches     => \@batches,
90                         referer     => $referer,
91                         );
92     }
93     elsif (@batch_ids) {
94         foreach my $batch_id (@batch_ids) {
95            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
96                             batch_id        => $batch_id,
97                             template_id     => $template_id,
98                             layout_id       => $layout_id,
99                             start_card      => $start_card,
100                             });
101         }
102         $template->param(
103                         batches     => \@batches,
104                         referer     => $referer,
105                         );
106     }
107 }
108 elsif ($op eq 'none') {
109     # setup select menus for selecting layout and template for this run...
110     $referer = $ENV{'HTTP_REFERER'};
111     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
112     @batch_ids = grep{$_ = {batch_id => $_}} @batch_ids;
113     @label_ids = grep{$_ = {label_id => $_}} @label_ids;
114     @borrower_numbers = grep{$_ = {borrower_number => $_}} @borrower_numbers;
115     $templates = get_all_templates(field_list => 'template_id, template_code', filter => 'creator = "Patroncards"');
116     $layouts = get_all_layouts(field_list => 'layout_id, layout_name', filter => 'creator = "Patroncards"');
117     $output_formats = get_output_formats();
118     $template->param(
119                     batch_ids                   => \@batch_ids,
120                     label_ids                   => \@label_ids,
121                     borrower_numbers            => \@borrower_numbers,
122                     templates                   => $templates,
123                     layouts                     => $layouts,
124                     output_formats              => $output_formats,
125                     multi_batch_count           => $multi_batch_count,
126                     card_count                  => $card_count,
127                     borrower_count              => $borrower_count,
128                     referer                     => $referer,
129                     );
130 }
131 output_html_with_http_headers $cgi, $cookie, $template->output;