Bug 23681: Fix QA issues
[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 $can_be_added_manually = $input->param('can_be_added_manually') || 0;
57     my $is_a_modif = $input->param("is_a_modif");
58
59     if ($is_a_modif) {
60         # Check whether another restriction already has this display text
61         my $dupe = Koha::RestrictionTypes->find({
62             display_text => $display_text
63         });
64         if ($dupe) {
65             push @messages, {
66                 type => 'error', code => 'duplicate_display_text'
67             };
68         } else {
69             my $restriction = Koha::RestrictionTypes->find($code);
70             unless ($restriction->is_system) {
71                 $restriction->display_text($display_text);
72                 $restriction->can_be_added_manually($can_be_added_manually);
73             }
74             $restriction->store;
75         }
76     } else {
77         # Check whether another restriction already has this code
78         my $dupe = Koha::RestrictionTypes->find($code);
79         if ($dupe) {
80             push @messages, {
81                 type => 'error', code => 'duplicate_code'
82             };
83         } else {
84             my $restriction = Koha::RestrictionType->new({
85                 code => $code,
86                 display_text => $display_text,
87                 can_be_added_manually => $can_be_added_manually
88             });
89             $restriction->store;
90         }
91     }
92     $op = 'list';
93 } elsif ( $op eq 'delete_confirm' ) {
94     $template->param(
95         restriction => scalar Koha::RestrictionTypes->find($code)
96     );
97 } elsif ( $op eq 'delete_confirmed' ) {
98     Koha::RestrictionTypes->find($code)->delete;
99     $op = 'list';
100 }
101
102 $template->param(
103     messages => \@messages,
104     op       => $op
105 );
106
107 if ( $op eq 'list' ) {
108     my $restrictions = Koha::RestrictionTypes->search();
109     $template->param(
110         restrictions => $restrictions,
111     )
112 }
113
114 output_html_with_http_headers $input, $cookie, $template->output;
115
116 exit 0;