Bug 23681: Add Management UI
[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 = new CGI;
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         authnotrequired => 0,
39         flagsrequired   => { parameters => 'manage_patron_restrictions' },
40         debug           => 1,
41     }
42 );
43
44 if ( $op eq 'add_form') {
45     # Get all existing restrictions, so we can do client-side validation
46     $template->param(
47         existing => scalar Koha::RestrictionTypes->search()
48     );
49     if ($code) {
50         $template->param(
51             restriction => scalar Koha::RestrictionTypes->find($code)
52         );
53     }
54 } elsif ( $op eq 'add_validate' ) {
55
56     my $display_text = $input->param('display_text');
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             $restriction->display_text($display_text);
71             $restriction->store;
72         }
73     } else {
74         # Check whether another restriction already has this code
75         my $dupe = Koha::RestrictionTypes->find($code);
76         if ($dupe) {
77             push @messages, {
78                 type => 'error', code => 'duplicate_code'
79             };
80         } else {
81             my $restriction = Koha::RestrictionType->new({
82                 code => $code,
83                 display_text => $display_text
84             });
85             $restriction->store;
86         }
87     }
88     $op = 'list';
89 } elsif ( $op eq 'delete_confirm' ) {
90     $template->param(
91         restriction => scalar Koha::RestrictionTypes->find($code)
92     );
93 } elsif ( $op eq 'delete_confirmed' ) {
94     Koha::RestrictionTypes->find($code)->delete;
95     $op = 'list';
96 }
97
98 $template->param(
99     messages => \@messages,
100     op       => $op
101 );
102
103 if ( $op eq 'list' ) {
104     my $restrictions = Koha::RestrictionTypes->search();
105     $template->param(
106         restrictions => $restrictions,
107     )
108 }
109
110 output_html_with_http_headers $input, $cookie, $template->output;
111
112 exit 0;