3 # Copyright 2019 PTFS Europe
5 # This file is part of Koha.
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.
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.
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>.
23 use Try::Tiny qw( try catch );
25 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use Koha::Patron::Restriction::Types;
30 my $op = $input->param('op') // 'list';
31 my $code = uc $input->param('code');
34 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
36 template_name => "admin/restrictions.tt",
39 flagsrequired => { parameters => 'manage_patron_restrictions' },
44 if ( $op eq 'add_form') {
45 # Get all existing restrictions, so we can do client-side validation
47 existing => scalar Koha::Patron::Restriction::Types->search()
51 restriction => scalar Koha::Patron::Restriction::Types->find($code)
54 } elsif ( $op eq 'add_validate' ) {
56 my $display_text = $input->param('display_text');
57 my $is_a_modif = $input->param("is_a_modif");
60 # Check whether another restriction already has this display text
61 my $dupe = Koha::Patron::Restriction::Types->search(
63 code => { '!=' => $code },
64 display_text => $display_text,
69 type => 'error', code => 'duplicate_display_text'
72 my $restriction = Koha::Patron::Restriction::Types->find($code);
73 $restriction->display_text($display_text);
75 push @messages, { type => 'message', code => 'update_success' };
78 # Check whether another restriction already has this code
79 my $dupe = Koha::Patron::Restriction::Types->find($code);
82 type => 'error', code => 'duplicate_code'
85 my $restriction = Koha::Patron::Restriction::Type->new({
87 display_text => $display_text
90 push @messages, { type => 'message', code => 'add_success' };
94 } elsif ( $op eq 'make_default' ) {
95 my $restriction = Koha::Patron::Restriction::Types->find($code);
96 $restriction->make_default;
98 } elsif ( $op eq 'delete_confirm' ) {
100 restriction => scalar Koha::Patron::Restriction::Types->find($code)
102 } elsif ( $op eq 'delete_confirmed' ) {
104 Koha::Patron::Restriction::Types->find($code)->delete;
105 push @messages, { type => 'message', code => 'delete_success' };
109 if ( $_->isa('Koha::Exceptions::CannotDeleteDefault') ) {
110 push @messages, { type => 'error', code => 'delete_default' };
112 elsif ( $_->isa('Koha::Exceptions::CannotDeleteSystem') ) {
113 push @messages, { type => 'error', code => 'delete_system' };
121 messages => \@messages,
125 if ( $op eq 'list' ) {
126 my $restrictions = Koha::Patron::Restriction::Types->search();
128 restrictions => $restrictions,
132 output_html_with_http_headers $input, $cookie, $template->output;