Bug 32482: (follow-up) Add markup comments
[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 qw( get_all_layouts get_all_templates get_output_formats );
28
29 my $cgi = CGI->new;
30 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
31     {
32         template_name   => "patroncards/print.tt",
33         query           => $cgi,
34         type            => "intranet",
35         flagsrequired   => { tools => 'label_creator' },
36     }
37 );
38
39 my $op = $cgi->param('op') || 'none';
40 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
41 my @batch_ids = $cgi->multi_param('batch_id');
42 my $patronlist_id = $cgi->param('patronlist_id') || undef;
43 my $layout_id = $cgi->param('layout_id') || undef;
44 my $layout_back_id = $cgi->param('layout_back_id') || undef;
45 my $template_id = $cgi->param('template_id') || undef;
46 my $start_card = $cgi->param('start_card') || 1;
47 my @borrower_numbers = $cgi->multi_param('borrower_number');
48 my $output_format = $cgi->param('output_format') || 'pdf';
49 my $referer = $cgi->param('referer') || undef;
50
51 my $layouts = undef;
52 my $templates = undef;
53 my $output_formats = undef;
54 my @batches = ();
55 my $multi_batch_count = scalar(@batch_ids);
56 my $card_count = scalar(@label_ids);
57 my $borrower_count = scalar(@borrower_numbers);
58
59 if ($op eq 'export') {
60     if (@label_ids) {
61         my $label_id_param = '&amp;label_id=';
62         $label_id_param .= join ('&amp;label_id=',@label_ids);
63         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
64                          batch_id        => $batch_ids[0],
65                          template_id     => $template_id,
66                          layout_id       => $layout_id,
67                          layout_back_id  => $layout_back_id,
68                          start_card      => $start_card,
69                          label_ids       => $label_id_param,
70                          card_count      => scalar(@label_ids),
71                         });
72         $template->param(
73                         batches     => \@batches,
74                         referer     => $referer,
75                         );
76     }
77     elsif (@borrower_numbers) {
78         my $borrower_number_param = '&amp;borrower_number=';
79         $borrower_number_param .= join ('&amp;borrower_number=',@borrower_numbers);
80         push (@batches, {create_script   => ($output_format eq 'pdf' ? 'create-pdf.pl' : 'create-csv.pl'),
81                          template_id     => $template_id,
82                          layout_id       => $layout_id,
83                          layout_back_id  => $layout_back_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                             layout_back_id  => $layout_back_id,
100                             start_card      => $start_card,
101                             });
102         }
103         $template->param(
104                         batches     => \@batches,
105                         referer     => $referer,
106                         );
107     }
108     elsif ($patronlist_id ) {
109         $template->param(
110                          patronlist_id   => $patronlist_id,
111                          template_id     => $template_id,
112                          layout_id       => $layout_id,
113                          layout_back_id  => $layout_back_id,
114                          start_card      => $start_card,
115                          referer         => $referer,
116                         );
117     }
118 }
119 elsif ($op eq 'none') {
120     # setup select menus for selecting layout and template for this run...
121     $referer = $ENV{'HTTP_REFERER'};
122     $referer =~ s/^.*?:\/\/.*?(\/.*)$/$1/m;
123     @batch_ids = map { {batch_id => $_} } @batch_ids;
124     @label_ids = map { {label_id => $_} } @label_ids;
125     @borrower_numbers = map { {borrower_number => $_} } @borrower_numbers;
126     $templates = get_all_templates( { fields => [qw( template_id template_code ) ], filters => { creator => "Patroncards" } });
127     $layouts = get_all_layouts({ fields => [ qw( layout_id layout_name ) ], filters => { creator => "Patroncards" } });
128     $output_formats = get_output_formats();
129     $template->param(
130                     batch_ids                   => \@batch_ids,
131                     label_ids                   => \@label_ids,
132                     borrower_numbers            => \@borrower_numbers,
133                     patronlist_id               => $patronlist_id,
134                     templates                   => $templates,
135                     layouts                     => $layouts,
136                     output_formats              => $output_formats,
137                     multi_batch_count           => $multi_batch_count,
138                     card_count                  => $card_count,
139                     borrower_count              => $borrower_count,
140                     referer                     => $referer,
141                     );
142 }
143 output_html_with_http_headers $cgi, $cookie, $template->output;