Bug 7460: Add template toolkit's FILTER html_line_break on opac details description...
[koha.git] / patroncards / edit-batch.pl
1 #!/usr/bin/perl
2 #
3 # Copyright 2006 Katipo Communications.
4 # Parts Copyright 2009 Foundations Bible College.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23 use vars qw($debug);
24
25 use CGI;
26 use autouse 'Data::Dumper' => qw(Dumper);
27
28 use C4::Auth qw(get_template_and_user);
29 use C4::Output qw(output_html_with_http_headers);
30 use C4::Branch qw(get_branch_code_from_name);
31 use C4::Creators 1.000000;
32 use C4::Patroncards 1.000000;
33
34 my $cgi = new CGI;
35 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36     {
37         template_name   => "patroncards/edit-batch.tmpl",
38         query           => $cgi,
39         type            => "intranet",
40         authnotrequired => 0,
41         flagsrequired   => { catalogue => 1 },
42         debug           => 1,
43     }
44 );
45
46 my $err = 0;
47 my $duplicate_count = undef;
48 my $duplicate_message = undef;
49 my $db_rows = {};
50 my $batch = undef;
51 my $display_columns = [ {_card_number   => {label => 'Card Number', link_field => 0}},
52                         {_summary       => {label => 'Summary', link_field => 0}},
53                         {borrowernumber => {label => 'Borrower Number', link_field => 0}},
54                         {select         => {label => 'Select', value => '_label_id'}},
55                       ];
56 my $op = $cgi->param('op') || 'new';
57 my $batch_id = $cgi->param('element_id') || $cgi->param('batch_id') || undef;
58 my @label_ids = $cgi->param('label_id') if $cgi->param('label_id');
59 my @item_numbers = $cgi->param('item_number') if $cgi->param('item_number');
60 my @borrower_numbers = $cgi->param('borrower_number') if $cgi->param('borrower_number');
61 my $errstr = $cgi->param('error') || '';
62
63 my $branch_code = C4::Context->userenv->{'branch'};
64
65 if ($op eq 'remove') {
66     $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
67     foreach my $label_id (@label_ids) {
68     $err = $batch->remove_item($label_id);
69     }
70     if ($err) {
71         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=403"); # this allows us to avoid problems with the user hitting their refresh button
72         exit;
73     }
74 }
75 elsif ($op eq 'delete') {
76     $err = C4::Creators::Batch::delete(batch_id => $batch_id, branch_code => $branch_code);
77     if ($err) {
78         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=404");
79         exit;
80     }
81 }
82 elsif ($op eq 'add') {
83     $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
84     $batch = C4::Patroncards::Batch->new(branch_code => $branch_code) if $batch == -2;
85     if ($branch_code){
86         foreach my $borrower_number (@borrower_numbers) {
87             $err = $batch->add_item($borrower_number);
88         }
89         if ($err) {
90             print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=401");
91             exit;
92         }
93     }
94     else {
95         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=402");
96         exit;
97     }
98 }
99 elsif ($op eq 'de_duplicate') {
100     $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
101     $duplicate_count = $batch->remove_duplicates();
102     $duplicate_message = 1 if $duplicate_count != -1;
103     if ($duplicate_count == -1) {
104         print $cgi->redirect("edit-batch.pl?op=edit&batch_id=$batch_id&error=405");
105         exit;
106     }
107 }
108 elsif ($op eq 'edit') {
109     $batch = C4::Patroncards::Batch->retrieve(batch_id => $batch_id);
110 }
111 elsif ($op eq 'new') {
112     if ($branch_code eq '') {
113         warn sprintf('Batch edit interface called with an invalid/non-existent branch code: %s',$branch_code ? $branch_code : 'NULL');
114         print $cgi->redirect("manage.pl?card_element=batch&error=203");
115         exit;
116     }
117     $batch = C4::Patroncards::Batch->new(branch_code => $branch_code);
118     $batch_id = $batch->get_attr('batch_id');
119 }
120 else {
121     warn sprintf('Batch edit interface called an unsupported operation: %s',$op);
122     print $cgi->redirect("manage.pl?card_element=batch&error=202");
123     exit;
124 }
125
126 my $items = $batch->get_attr('items');
127 $db_rows = get_card_summary(items => $items, batch_id => $batch_id);
128
129 my $table = html_table($display_columns, $db_rows);
130
131 $template->param(
132                 op                      => $op,
133                 batch_id                => $batch_id,
134                 table_loop              => $table,
135                 duplicate_message       => $duplicate_message,
136                 duplicate_count         => $duplicate_count,
137                 error                   => ($errstr ? 1 : 0),
138                 $errstr                 => 1,
139                 );
140
141 output_html_with_http_headers $cgi, $cookie, $template->output;