Bug 14957: (QA follow-up) Rename 'merge' => 'overlay'
[koha.git] / admin / marc-overlay-rules.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 # standard or CPAN modules used
21 use CGI qw ( -utf8 );
22 use CGI::Cookie;
23 use MARC::File::USMARC;
24 use Try::Tiny;
25
26 # Koha modules used
27 use C4::Context;
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
30 use C4::ImportBatch;
31 use C4::Matcher;
32 use C4::BackgroundJob;
33 use C4::Labels::Batch;
34 use Koha::MarcOverlayRules;
35 use Koha::Patron::Categories;
36
37 my $script_name = "/cgi-bin/koha/admin/marc-overlay-rules.pl";
38
39 my $input = new CGI;
40 my $op = $input->param('op') || '';
41 my $errors = [];
42
43 my $rule_from_cgi = sub {
44     my ($cgi) = @_;
45
46     my %rule = map { $_ => scalar $cgi->param($_) } (
47         'tag',
48         'module',
49         'filter',
50         'add',
51         'append',
52         'remove',
53         'delete'
54     );
55
56     my $id = $cgi->param('id');
57     if ($id) {
58         $rule{id} = $id;
59     }
60
61     return \%rule;
62 };
63
64 my ($template, $loggedinuser, $cookie) = get_template_and_user(
65     {
66         template_name   => "admin/marc-overlay-rules.tt",
67         query           => $input,
68         type            => "intranet",
69         authnotrequired => 0,
70         flagsrequired   => { parameters => 'manage_marc_overlay_rules' },
71         debug           => 1,
72     }
73 );
74
75 $template->param(script_name => $script_name);
76
77 my %cookies = parse CGI::Cookie($cookie);
78 our $sessionID = $cookies{'CGISESSID'}->value;
79
80 my $get_rules = sub {
81     # TODO: order?
82     return [map { { $_->get_columns() } } Koha::MarcOverlayRules->_resultset->all];
83 };
84 my $rules;
85
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}};
95         }
96     }
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->();
102     }
103 }
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) {
110             $rule->{'edit'} = 1;
111             last;
112         }
113     }
114 }
115 elsif ($op eq 'doedit' || $op eq 'add') {
116     my $rule_data = $rule_from_cgi->($input);
117     if (!@{$errors}) {
118         try {
119             Koha::MarcOverlayRules->validate($rule_data);
120         }
121         catch {
122             die $_ unless blessed $_ && $_->can('rethrow');
123
124             if ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidTagRegExp')) {
125                 push @{$errors}, {
126                     type => 'error',
127                     code => 'invalid_tag_regexp',
128                     tag => $rule_data->{tag},
129                 };
130             }
131             elsif ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidControlFieldActions')) {
132                 push @{$errors}, {
133                     type => 'error',
134                     code => 'invalid_control_field_actions',
135                     tag => $rule_data->{tag},
136                 };
137             }
138             else {
139                 $_->rethrow;
140             }
141         };
142         if (!@{$errors}) {
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);
146             $rule->store();
147         }
148         $rules = $get_rules->();
149     }
150 }
151 else {
152     $rules = $get_rules->();
153 }
154
155 my $categorycodes = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
156 $template->param( rules => $rules, categorycodes => $categorycodes, messages => $errors );
157
158 output_html_with_http_headers $input, $cookie, $template->output;