Bug 30719: ILL Batch Statuses
[koha.git] / admin / ill_batch_statuses.pl
1 #! /usr/bin/perl
2
3 # Copyright 2019 Koha Development Team
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 use CGI qw ( -utf8 );
22 use Try::Tiny qw( catch try );
23
24 use C4::Context;
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27
28 use Koha::IllbatchStatus;
29 use Koha::IllbatchStatuses;
30
31 my $input = CGI->new;
32 my $code  = $input->param('code');
33 my $op    = $input->param('op') || 'list';
34 my @messages;
35
36 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
37     {
38         template_name   => "admin/ill_batch_statuses.tt",
39         query           => $input,
40         type            => "intranet",
41         flagsrequired   => { parameters => 'ill' },
42     }
43 );
44
45 my $status;
46 if ($code) {
47     $status = Koha::IllbatchStatuses->find({ code => $code });
48 }
49
50 if ( $op eq 'add_form' ) {
51     if ($status) {
52         $template->param(
53             status => $status
54         );
55     }
56 }
57 elsif ( $op eq 'add_validate' ) {
58     my $name = $input->param('name');
59     my $code = $input->param('code');
60
61     if ( not defined $status ) {
62         $status = Koha::IllbatchStatus->new( {
63             name => $name,
64             code => $code
65         } );
66     }
67
68     try {
69         if ($status->id) {
70             $status->update_and_log({ name => $name });
71         } else {
72             $status->create_and_log;
73         }
74         push @messages, { type => 'message', code => 'success_on_saving' };
75     }
76     catch {
77         push @messages, { type => 'error', code => 'error_on_saving' };
78     };
79     $op = 'list';
80 }
81 elsif ( $op eq 'delete' ) {
82     try {
83         $status->delete_and_log;
84         push @messages, { code => 'success_on_delete', type => 'message' };
85     }
86     catch {
87         push @messages, { code => 'error_on_delete', type => 'alert' };
88
89     };
90     $op = 'list';
91 }
92 if ( $op eq 'list' ) {
93     my $statuses = Koha::IllbatchStatuses->search();
94     $template->param( statuses => $statuses );
95 }
96
97 $template->param(
98     messages => \@messages,
99     op       => $op,
100 );
101
102 output_html_with_http_headers $input, $cookie, $template->output;