03382824e259220f2e293ed1bad19f193b21b67f
[koha.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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use CGI qw ( -utf8 );
23 use autouse 'Data::Dumper' => qw(Dumper);
24
25 use C4::Auth qw(get_template_and_user);
26 use C4::Output qw(output_html_with_http_headers);
27 use C4::Creators;
28 use C4::Patroncards;
29
30 my $cgi = CGI->new;
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name   => "patroncards/print.tt",
34         query           => $cgi,
35         type            => "intranet",
36         flagsrequired   => { tools => 'label_creator' },
37         debug           => 1,
38     }
39 );
40
41 my $op = $cgi->param('op') || 'none';
42 my @label_ids = $cgi->multi_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
43 my @batch_ids = $cgi->multi_param('batch_id');
44 my $patronlist_id = $cgi->param('patronlist_id') || undef;
45 my $layout_id = $cgi->param('layout_id') || undef;
46 my $layout_back_id = $cgi->param('layout_back_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->multi_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 = '&amp;label_id=';
64         $label_id_param .= join ('&amp;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                          layout_back_id  => $layout_back_id,
70                          start_card      => $start_card,
71                          label_ids       => $label_id_param,
72                          card_count      => scalar(@label_ids),
73                         });
74         $template->param(
75                         batches     => \@batches,
76                         referer     => $referer,
77                         );
78     }
79     elsif (@borrower_numbers) {
80         my $borrower_number_param = '&amp;borrower_number=';
81         $borrower_number_param .= join ('&amp;borrower_number=',@borrower_numbers);
82         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
83                          template_id     => $template_id,
84                          layout_id       => $layout_id,
85                          layout_back_id  => $layout_back_id,
86                          start_card      => $start_card,
87                          borrower_numbers    => $borrower_number_param,
88                          card_count      => scalar(@borrower_numbers),
89                         });
90         $template->param(
91                         batches     => \@batches,
92                         referer     => $referer,
93                         );
94     }
95     elsif (@batch_ids) {
96         foreach my $batch_id (@batch_ids) {
97            push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
98                             batch_id        => $batch_id,
99                             template_id     => $template_id,
100                             layout_id       => $layout_id,
101                             layout_back_id  => $layout_back_id,
102                             start_card      => $start_card,
103                             });
104         }
105         $template->param(
106                         batches     => \@batches,
107                         referer     => $referer,
108                         );
109     }
110     elsif ($patronlist_id ) {
111         $template->param(
112                          patronlist_id   => $patronlist_id,
113                          template_id     => $template_id,
114                          layout_id       => $layout_id,
115                          layout_back_id  => $layout_back_id,
116                          start_card      => $start_card,
117                          referer         => $referer,
118                         );
119     }
120 }
121 elsif ($op eq 'none') {
122     # setup select menus for selecting layout and template for this run...
123     $referer = $ENV{'HTTP_REFERER'};
124     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
125     @batch_ids = map { {batch_id => $_} } @batch_ids;
126     @label_ids = map { {label_id => $_} } @label_ids;
127     @borrower_numbers = map { {borrower_number => $_} } @borrower_numbers;
128     $templates = get_all_templates( { fields => [qw( template_id template_code ) ], filters => { creator => "Patroncards" } });
129     $layouts = get_all_layouts({ fields => [ qw( layout_id layout_name ) ], filters => { creator => "Patroncards" } });
130     $output_formats = get_output_formats();
131     $template->param(
132                     batch_ids                   => \@batch_ids,
133                     label_ids                   => \@label_ids,
134                     borrower_numbers            => \@borrower_numbers,
135                     patronlist_id               => $patronlist_id,
136                     templates                   => $templates,
137                     layouts                     => $layouts,
138                     output_formats              => $output_formats,
139                     multi_batch_count           => $multi_batch_count,
140                     card_count                  => $card_count,
141                     borrower_count              => $borrower_count,
142                     referer                     => $referer,
143                     );
144 }
145 output_html_with_http_headers $cgi, $cookie, $template->output;