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 return Koha::MarcOverlayRules->search( undef, { order_by => { -asc => 'id' } } )->unblessed;
67 my $rules = $get_rules->();
69 if ($op eq 'remove' || $op eq 'cud-remove') {
70 my @remove_ids = $input->multi_param('batchremove');
71 push @remove_ids, scalar $input->param('id') if $input->param('id');
72 if ($op eq 'remove') {
73 $template->{VARS}->{removeConfirm} = 1;
74 my %remove_ids = map { $_ => undef } @remove_ids;
75 for my $rule (@{$rules}) {
76 $rule->{'removemarked'} = 1 if exists $remove_ids{$rule->{id}};
79 elsif ($op eq 'cud-remove') {
80 my @remove_ids = $input->multi_param('batchremove');
81 push @remove_ids, scalar $input->param('id') if $input->param('id');
82 Koha::MarcOverlayRules->search({ id => { in => \@remove_ids } })->delete();
83 # Update $rules after deletion
84 $rules = $get_rules->();
87 elsif ($op eq 'edit') {
88 $template->param( edit => 1 );
89 my $id = $input->param('id');
90 for my $rule(@{$rules}) {
91 if ($rule->{id} == $id) {
97 elsif ($op eq 'cud-edit' || $op eq 'cud-add') {
98 my $rule_data = $rule_from_cgi->($input);
101 Koha::MarcOverlayRules->validate($rule_data);
104 die $_ unless blessed $_ && $_->can('rethrow');
106 if ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidTagRegExp')) {
109 code => 'invalid_tag_regexp',
110 tag => $rule_data->{tag},
113 elsif ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidControlFieldActions')) {
116 code => 'invalid_control_field_actions',
117 tag => $rule_data->{tag},
125 my $rule = Koha::MarcOverlayRules->find_or_create($rule_data);
126 # Need to call set and store here in case we have an update
127 $rule->set($rule_data);
130 # Update $rules after edit/add
131 $rules = $get_rules->();
135 my $categories = Koha::Patron::Categories->search_with_library_limits( {},
136 { order_by => ['description'], columns => [ 'categorycode', 'description' ] } )->unblessed;
140 categories => $categories,
144 output_html_with_http_headers $input, $cookie, $template->output;