3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
27 use Koha::MarcOverlayRules;
28 use Koha::Patron::Categories;
31 my $op = $input->param('op') || '';
34 my $rule_from_cgi = sub {
37 my %rule = map { $_ => scalar $cgi->param($_) } (
47 my $id = $cgi->param('id');
55 my ($template, $loggedinuser, $cookie) = get_template_and_user(
57 template_name => "admin/marc-overlay-rules.tt",
60 flagsrequired => { parameters => 'manage_marc_overlay_rules' },
65 my $rules = Koha::MarcOverlayRules->search->unblessed;
67 if ($op eq 'remove' || $op eq 'doremove') {
68 my @remove_ids = $input->multi_param('batchremove');
69 push @remove_ids, scalar $input->param('id') if $input->param('id');
70 if ($op eq 'remove') {
71 $template->{VARS}->{removeConfirm} = 1;
72 my %remove_ids = map { $_ => undef } @remove_ids;
73 for my $rule (@{$rules}) {
74 $rule->{'removemarked'} = 1 if exists $remove_ids{$rule->{id}};
77 elsif ($op eq 'doremove') {
78 my @remove_ids = $input->multi_param('batchremove');
79 push @remove_ids, scalar $input->param('id') if $input->param('id');
80 Koha::MarcOverlayRules->search({ id => { in => \@remove_ids } })->delete();
83 elsif ($op eq 'edit') {
84 $template->param( edit => 1 );
85 my $id = $input->param('id');
86 for my $rule(@{$rules}) {
87 if ($rule->{id} == $id) {
93 elsif ($op eq 'doedit' || $op eq 'add') {
94 my $rule_data = $rule_from_cgi->($input);
97 Koha::MarcOverlayRules->validate($rule_data);
100 die $_ unless blessed $_ && $_->can('rethrow');
102 if ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidTagRegExp')) {
105 code => 'invalid_tag_regexp',
106 tag => $rule_data->{tag},
109 elsif ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidControlFieldActions')) {
112 code => 'invalid_control_field_actions',
113 tag => $rule_data->{tag},
121 my $rule = Koha::MarcOverlayRules->find_or_create($rule_data);
122 # Need to call set and store here in case we have an update
123 $rule->set($rule_data);
129 my $categorycodes = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
132 categorycodes => $categorycodes,
136 output_html_with_http_headers $input, $cookie, $template->output;