Bug 14957: Clean controller
[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 = new CGI;
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 # TODO: order?
65 my $rules = Koha::MarcOverlayRules->search->unblessed;
66
67 if ($op eq 'remove' || $op eq 'doremove') {
68     my @remove_ids = $input->multi_param('batchremove');
69     push @remove_ids, scalar $input->param('id') if $input->param('id');
70     if ($op eq 'remove') {
71         $template->{VARS}->{removeConfirm} = 1;
72         my %remove_ids = map { $_ => undef } @remove_ids;
73         for my $rule (@{$rules}) {
74             $rule->{'removemarked'} = 1 if exists $remove_ids{$rule->{id}};
75         }
76     }
77     elsif ($op eq 'doremove') {
78         my @remove_ids = $input->multi_param('batchremove');
79         push @remove_ids, scalar $input->param('id') if $input->param('id');
80         Koha::MarcOverlayRules->search({ id => { in => \@remove_ids } })->delete();
81     }
82 }
83 elsif ($op eq 'edit') {
84     $template->param( edit => 1 );
85     my $id = $input->param('id');
86     for my $rule(@{$rules}) {
87         if ($rule->{id} == $id) {
88             $rule->{'edit'} = 1;
89             last;
90         }
91     }
92 }
93 elsif ($op eq 'doedit' || $op eq 'add') {
94     my $rule_data = $rule_from_cgi->($input);
95     if (!@{$errors}) {
96         try {
97             Koha::MarcOverlayRules->validate($rule_data);
98         }
99         catch {
100             die $_ unless blessed $_ && $_->can('rethrow');
101
102             if ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidTagRegExp')) {
103                 push @{$errors}, {
104                     type => 'error',
105                     code => 'invalid_tag_regexp',
106                     tag => $rule_data->{tag},
107                 };
108             }
109             elsif ($_->isa('Koha::Exceptions::MarcOverlayRule::InvalidControlFieldActions')) {
110                 push @{$errors}, {
111                     type => 'error',
112                     code => 'invalid_control_field_actions',
113                     tag => $rule_data->{tag},
114                 };
115             }
116             else {
117                 $_->rethrow;
118             }
119         };
120         if (!@{$errors}) {
121             my $rule = Koha::MarcOverlayRules->find_or_create($rule_data);
122             # Need to call set and store here in case we have an update
123             $rule->set($rule_data);
124             $rule->store();
125         }
126     }
127 }
128
129 my $categorycodes = Koha::Patron::Categories->search_with_library_limits({}, {order_by => ['description']});
130 $template->param(
131     rules         => $rules,
132     categorycodes => $categorycodes,
133     messages      => $errors
134 );
135
136 output_html_with_http_headers $input, $cookie, $template->output;