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>.
20 # standard or CPAN modules used
23 use MARC::File::USMARC;
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
32 use C4::BackgroundJob;
33 use C4::Labels::Batch;
34 use Koha::MarcOverlayRules;
35 use Koha::Patron::Categories;
37 my $script_name = "/cgi-bin/koha/admin/marc-overlay-rules.pl";
40 my $op = $input->param('op') || '';
43 my $rule_from_cgi = sub {
46 my %rule = map { $_ => scalar $cgi->param($_) } (
56 my $id = $cgi->param('id');
64 my ($template, $loggedinuser, $cookie) = get_template_and_user(
66 template_name => "admin/marc-overlay-rules.tt",
70 flagsrequired => { parameters => 'manage_marc_overlay_rules' },
75 $template->param(script_name => $script_name);
77 my %cookies = parse CGI::Cookie($cookie);
78 our $sessionID = $cookies{'CGISESSID'}->value;
82 return [map { { $_->get_columns() } } Koha::MarcOverlayRules->_resultset->all];
86 if ($op eq 'remove' || $op eq 'doremove') {
87 my @remove_ids = $input->multi_param('batchremove');
88 push @remove_ids, scalar $input->param('id') if $input->param('id');
89 if ($op eq 'remove') {
90 $template->{VARS}->{removeConfirm} = 1;
91 my %remove_ids = map { $_ => undef } @remove_ids;
92 $rules = $get_rules->();
93 for my $rule (@{$rules}) {
94 $rule->{'removemarked'} = 1 if exists $remove_ids{$rule->{id}};
97 elsif ($op eq 'doremove') {
98 my @remove_ids = $input->multi_param('batchremove');
99 push @remove_ids, scalar $input->param('id') if $input->param('id');
100 Koha::MarcOverlayRules->search({ id => { in => \@remove_ids } })->delete();
101 $rules = $get_rules->();
104 elsif ($op eq 'edit') {
105 $template->{VARS}->{edit} = 1;
106 my $id = $input->param('id');
107 $rules = $get_rules->();
108 for my $rule(@{$rules}) {
109 if ($rule->{id} == $id) {
115 elsif ($op eq 'doedit' || $op eq 'add') {
116 my $rule_data = $rule_from_cgi->($input);
119 Koha::MarcOverlayRules->validate($rule_data);
122 die $_ unless blessed $_ && $_->can('rethrow');
124 if ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidTagRegExp')) {
127 code => 'invalid_tag_regexp',
128 tag => $rule_data->{tag},
131 elsif ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidControlFieldActions')) {
134 code => 'invalid_control_field_actions',
135 tag => $rule_data->{tag},
143 my $rule = Koha::MarcOverlayRules->find_or_create($rule_data);
144 # Need to call set and store here in case we have an update
145 $rule->set($rule_data);
148 $rules = $get_rules->();
152 $rules = $get_rules->();
155 my $categorycodes = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
156 $template->param( rules => $rules, categorycodes => $categorycodes, messages => $errors );
158 output_html_with_http_headers $input, $cookie, $template->output;