Bug 29407: Make the pickup locations dropdown JS reusable
[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 use CGI qw ( -utf8 );
21 use Try::Tiny;
22
23 use C4::Context;
24 use C4::Auth qw( get_template_and_user );
25 use C4::Output qw( output_html_with_http_headers );
26 use C4::ImportBatch;
27 use Koha::MarcOverlayRules;
28 use Koha::Patron::Categories;
29
30 my $input = CGI->new;
31 my $op = $input->param('op') || '';
32 my $errors = [];
33
34 my $rule_from_cgi = sub {
35     my ($cgi) = @_;
36
37     my %rule = map { $_ => scalar $cgi->param($_) } (
38         'tag',
39         'module',
40         'filter',
41         'add',
42         'append',
43         'remove',
44         'delete'
45     );
46
47     my $id = $cgi->param('id');
48     if ($id) {
49         $rule{id} = $id;
50     }
51
52     return \%rule;
53 };
54
55 my ($template, $loggedinuser, $cookie) = get_template_and_user(
56     {
57         template_name   => "admin/marc-overlay-rules.tt",
58         query           => $input,
59         type            => "intranet",
60         flagsrequired   => { parameters => 'manage_marc_overlay_rules' },
61     }
62 );
63
64 my $get_rules = sub {
65     return Koha::MarcOverlayRules->search( undef, { order_by => { -asc => 'id' } } )->unblessed;
66 };
67 my $rules = $get_rules->();
68
69 if ($op eq 'remove' || $op eq 'doremove') {
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}};
77         }
78     }
79     elsif ($op eq 'doremove') {
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->();
85     }
86 }
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) {
92             $rule->{'edit'} = 1;
93             last;
94         }
95     }
96 }
97 elsif ($op eq 'doedit' || $op eq 'add') {
98     my $rule_data = $rule_from_cgi->($input);
99     if (!@{$errors}) {
100         try {
101             Koha::MarcOverlayRules->validate($rule_data);
102         }
103         catch {
104             die $_ unless blessed $_ && $_->can('rethrow');
105
106             if ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidTagRegExp')) {
107                 push @{$errors}, {
108                     type => 'error',
109                     code => 'invalid_tag_regexp',
110                     tag => $rule_data->{tag},
111                 };
112             }
113             elsif ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidControlFieldActions')) {
114                 push @{$errors}, {
115                     type => 'error',
116                     code => 'invalid_control_field_actions',
117                     tag => $rule_data->{tag},
118                 };
119             }
120             else {
121                 $_->rethrow;
122             }
123         };
124         if (!@{$errors}) {
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);
128             $rule->store();
129         }
130         # Update $rules after edit/add
131         $rules = $get_rules->();
132     }
133 }
134
135 my $categorycodes = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
136 $template->param(
137     rules         => $rules,
138     categorycodes => $categorycodes,
139     messages      => $errors
140 );
141
142 output_html_with_http_headers $input, $cookie, $template->output;