DBRev 24.06.00.000: Start of a new release cycle
[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::ILL::Batch::Status;
29 use Koha::ILL::Batch::Statuses;
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::ILL::Batch::Statuses->find( { code => $code } );
48 }
49
50 if ( $op eq 'add_form' ) {
51     if ($status) {
52         $template->param( status => $status );
53     }
54 } elsif ( $op eq 'cud-add_validate' ) {
55     my $name = $input->param('name');
56     my $code = $input->param('code');
57
58     if ( not defined $status ) {
59         $status = Koha::ILL::Batch::Status->new(
60             {
61                 name => $name,
62                 code => $code
63             }
64         );
65     }
66
67     try {
68         if ( $status->id ) {
69             $status->update_and_log( { name => $name } );
70         } else {
71             $status->create_and_log;
72         }
73         push @messages, { type => 'message', code => 'success_on_saving' };
74     } catch {
75         push @messages, { type => 'error', code => 'error_on_saving' };
76     };
77     $op = 'list';
78 } elsif ( $op eq 'cud-delete' ) {
79     try {
80         $status->delete_and_log;
81         push @messages, { code => 'success_on_delete', type => 'message' };
82     } catch {
83         push @messages, { code => 'error_on_delete', type => 'alert' };
84
85     };
86     $op = 'list';
87 }
88 if ( $op eq 'list' ) {
89     my $statuses = Koha::ILL::Batch::Statuses->search();
90     $template->param( statuses => $statuses );
91 }
92
93 $template->param(
94     messages => \@messages,
95     op       => $op,
96 );
97
98 output_html_with_http_headers $input, $cookie, $template->output;