Bug 14957: (QA follow-up) Fix script name
[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 $input = new CGI;
38 my $op = $input->param('op') || '';
39 my $errors = [];
40
41 my $rule_from_cgi = sub {
42     my ($cgi) = @_;
43
44     my %rule = map { $_ => scalar $cgi->param($_) } (
45         'tag',
46         'module',
47         'filter',
48         'add',
49         'append',
50         'remove',
51         'delete'
52     );
53
54     my $id = $cgi->param('id');
55     if ($id) {
56         $rule{id} = $id;
57     }
58
59     return \%rule;
60 };
61
62 my ($template, $loggedinuser, $cookie) = get_template_and_user(
63     {
64         template_name   => "admin/marc-overlay-rules.tt",
65         query           => $input,
66         type            => "intranet",
67         flagsrequired   => { parameters => 'manage_marc_overlay_rules' },
68     }
69 );
70
71 my %cookies = parse CGI::Cookie($cookie);
72 our $sessionID = $cookies{'CGISESSID'}->value;
73
74 my $get_rules = sub {
75     # TODO: order?
76     return [map { { $_->get_columns() } } Koha::MarcOverlayRules->_resultset->all];
77 };
78 my $rules;
79
80 if ($op eq 'remove' || $op eq 'doremove') {
81     my @remove_ids = $input->multi_param('batchremove');
82     push @remove_ids, scalar $input->param('id') if $input->param('id');
83     if ($op eq 'remove') {
84         $template->{VARS}->{removeConfirm} = 1;
85         my %remove_ids = map { $_ => undef } @remove_ids;
86         $rules = $get_rules->();
87         for my $rule (@{$rules}) {
88             $rule->{'removemarked'} = 1 if exists $remove_ids{$rule->{id}};
89         }
90     }
91     elsif ($op eq 'doremove') {
92         my @remove_ids = $input->multi_param('batchremove');
93         push @remove_ids, scalar $input->param('id') if $input->param('id');
94         Koha::MarcOverlayRules->search({ id => { in => \@remove_ids } })->delete();
95         $rules = $get_rules->();
96     }
97 }
98 elsif ($op eq 'edit') {
99     $template->{VARS}->{edit} = 1;
100     my $id = $input->param('id');
101     $rules = $get_rules->();
102     for my $rule(@{$rules}) {
103         if ($rule->{id} == $id) {
104             $rule->{'edit'} = 1;
105             last;
106         }
107     }
108 }
109 elsif ($op eq 'doedit' || $op eq 'add') {
110     my $rule_data = $rule_from_cgi->($input);
111     if (!@{$errors}) {
112         try {
113             Koha::MarcOverlayRules->validate($rule_data);
114         }
115         catch {
116             die $_ unless blessed $_ && $_->can('rethrow');
117
118             if ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidTagRegExp')) {
119                 push @{$errors}, {
120                     type => 'error',
121                     code => 'invalid_tag_regexp',
122                     tag => $rule_data->{tag},
123                 };
124             }
125             elsif ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidControlFieldActions')) {
126                 push @{$errors}, {
127                     type => 'error',
128                     code => 'invalid_control_field_actions',
129                     tag => $rule_data->{tag},
130                 };
131             }
132             else {
133                 $_->rethrow;
134             }
135         };
136         if (!@{$errors}) {
137             my $rule = Koha::MarcOverlayRules->find_or_create($rule_data);
138             # Need to call set and store here in case we have an update
139             $rule->set($rule_data);
140             $rule->store();
141         }
142         $rules = $get_rules->();
143     }
144 }
145 else {
146     $rules = $get_rules->();
147 }
148
149 my $categorycodes = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
150 $template->param( rules => $rules, categorycodes => $categorycodes, messages => $errors );
151
152 output_html_with_http_headers $input, $cookie, $template->output;