Bug 23681: (QA follow-up) Fix license, perlcritic & executable
[koha.git] / admin / restrictions.pl
1 #!/usr/bin/perl
2
3 # Copyright 2019 PTFS Europe
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 C4::Auth qw( get_template_and_user );
24 use C4::Output qw( output_html_with_http_headers );
25 use Koha::RestrictionTypes;
26
27 my $input = CGI->new;
28 my $op    = $input->param('op') // 'list';
29 my $code  = uc $input->param('code');
30 my @messages = ();
31
32
33 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
34     {
35         template_name   => "admin/restrictions.tt",
36         query           => $input,
37         type            => "intranet",
38         flagsrequired   => { parameters => 'manage_patron_restrictions' },
39         debug           => 1,
40     }
41 );
42
43 if ( $op eq 'add_form') {
44     # Get all existing restrictions, so we can do client-side validation
45     $template->param(
46         existing => scalar Koha::RestrictionTypes->search()
47     );
48     if ($code) {
49         $template->param(
50             restriction => scalar Koha::RestrictionTypes->find($code)
51         );
52     }
53 } elsif ( $op eq 'add_validate' ) {
54
55     my $display_text = $input->param('display_text');
56     my $is_a_modif = $input->param("is_a_modif");
57
58     if ($is_a_modif) {
59         # Check whether another restriction already has this display text
60         my $dupe = Koha::RestrictionTypes->find({
61             display_text => $display_text
62         });
63         if ($dupe) {
64             push @messages, {
65                 type => 'error', code => 'duplicate_display_text'
66             };
67         } else {
68             my $restriction = Koha::RestrictionTypes->find($code);
69             $restriction->display_text($display_text);
70             $restriction->store;
71         }
72     } else {
73         # Check whether another restriction already has this code
74         my $dupe = Koha::RestrictionTypes->find($code);
75         if ($dupe) {
76             push @messages, {
77                 type => 'error', code => 'duplicate_code'
78             };
79         } else {
80             my $restriction = Koha::RestrictionType->new({
81                 code => $code,
82                 display_text => $display_text
83             });
84             $restriction->store;
85         }
86     }
87     $op = 'list';
88 } elsif ( $op eq 'delete_confirm' ) {
89     $template->param(
90         restriction => scalar Koha::RestrictionTypes->find($code)
91     );
92 } elsif ( $op eq 'delete_confirmed' ) {
93     Koha::RestrictionTypes->find($code)->delete;
94     $op = 'list';
95 }
96
97 $template->param(
98     messages => \@messages,
99     op       => $op
100 );
101
102 if ( $op eq 'list' ) {
103     my $restrictions = Koha::RestrictionTypes->search();
104     $template->param(
105         restrictions => $restrictions,
106     )
107 }
108
109 output_html_with_http_headers $input, $cookie, $template->output;
110
111 exit 0;