3 # Copyright 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use C4::Auth qw( get_template_and_user );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Matcher qw/valid_normalization_routines/;
29 my $script_name = "/cgi-bin/koha/admin/matching-rules.pl";
31 our $input = CGI->new;
32 my $op = $input->param('op') || '';
35 my ($template, $loggedinuser, $cookie)
36 = get_template_and_user({template_name => "admin/matching-rules.tt",
39 flagsrequired => { parameters => 'manage_matching_rules' },
42 $template->param(script_name => $script_name);
44 my $matcher_id = $input->param("matcher_id");
46 $template->param( max_matchpoint => 0 );
47 $template->param( max_matchcheck => 0 );
48 my @valid_norms = C4::Matcher::valid_normalization_routines();
49 unshift @valid_norms, 'none';
50 $template->param( valid_norms => \@valid_norms );
53 if ($op eq "edit_matching_rule") {
54 edit_matching_rule_form($template, $matcher_id);
55 } elsif ($op eq "edit_matching_rule_confirmed") {
56 add_update_matching_rule($template, $matcher_id);
58 } elsif ($op eq "add_matching_rule") {
59 add_matching_rule_form($template);
60 } elsif ($op eq "add_matching_rule_confirmed") {
61 add_update_matching_rule($template, $matcher_id);
63 } elsif ($op eq "delete_matching_rule") {
64 delete_matching_rule_form($template, $matcher_id);
65 } elsif ($op eq "delete_matching_rule_confirmed") {
66 delete_matching_rule($template, $matcher_id);
73 matching_rule_list($template);
76 output_html_with_http_headers $input, $cookie, $template->output;
80 sub add_matching_rule_form {
84 matching_rule_form => 1,
85 confirm_op => 'add_matching_rule_confirmed',
92 sub add_update_matching_rule {
94 my $matcher_id = shift;
95 my $record_type = $input->param('record_type') || 'biblio';
98 my $matcher = C4::Matcher->new($record_type, 1000);
99 $matcher->code(scalar $input->param('code'));
100 $matcher->description(scalar $input->param('description'));
101 $matcher->threshold(scalar $input->param('threshold'));
104 my @mp_nums = sort map { /^mp_(\d+)_search_index/ ? int($1): () } $input->multi_param;
105 foreach my $mp_num (@mp_nums) {
106 my $index = $input->param("mp_${mp_num}_search_index");
107 my $score = $input->param("mp_${mp_num}_score");
110 my @comp_nums = sort map { /^mp_${mp_num}_c_(\d+)_tag/ ? int($1): () } $input->multi_param;
111 foreach my $comp_num (@comp_nums) {
113 $component->{'tag'} = $input->param("mp_${mp_num}_c_${comp_num}_tag");
114 $component->{'subfields'} = $input->param("mp_${mp_num}_c_${comp_num}_subfields");
115 $component->{'offset'} = $input->param("mp_${mp_num}_c_${comp_num}_offset");
116 $component->{'length'} = $input->param("mp_${mp_num}_c_${comp_num}_length");
118 $component->{'norms'} = [];
119 my @norm_nums = sort map { /^mp_${mp_num}_c_${comp_num}_n_(\d+)_norm/ ? int($1): () } $input->multi_param;
120 foreach my $norm_num (@norm_nums) {
121 push @{ $component->{'norms'} }, $input->multi_param("mp_${mp_num}_c_${comp_num}_n_${norm_num}_norm");
123 push @$components, $component;
125 $matcher->add_matchpoint($index, $score, $components);
129 my @mc_nums = sort map { /^mc_(\d+)_id/ ? int($1): () } $input->multi_param;
130 foreach my $mc_num (@mc_nums) {
132 my $src_components = [];
133 my @src_comp_nums = sort map { /^mc_${mc_num}_src_c_(\d+)_tag/ ? int($1): () } $input->multi_param;
134 foreach my $comp_num (@src_comp_nums) {
136 $component->{'tag'} = $input->param("mc_${mc_num}_src_c_${comp_num}_tag");
137 $component->{'subfields'} = $input->param("mc_${mc_num}_src_c_${comp_num}_subfields");
138 $component->{'offset'} = $input->param("mc_${mc_num}_src_c_${comp_num}_offset");
139 $component->{'length'} = $input->param("mc_${mc_num}_src_c_${comp_num}_length");
141 $component->{'norms'} = [];
142 my @norm_nums = sort map { /^mc_${mc_num}_src_c_${comp_num}_n_(\d+)_norm/ ? int($1): () } $input->multi_param;
143 foreach my $norm_num (@norm_nums) {
144 push @{ $component->{'norms'} }, $input->multi_param("mc_${mc_num}_src_c_${comp_num}_n_${norm_num}_norm");
146 push @$src_components, $component;
149 my $tgt_components = [];
150 my @tgt_comp_nums = sort map { /^mc_${mc_num}_tgt_c_(\d+)_tag/ ? int($1): () } $input->multi_param;
151 foreach my $comp_num (@tgt_comp_nums) {
153 $component->{'tag'} = $input->param("mc_${mc_num}_tgt_c_${comp_num}_tag");
154 $component->{'subfields'} = $input->param("mc_${mc_num}_tgt_c_${comp_num}_subfields");
155 $component->{'offset'} = $input->param("mc_${mc_num}_tgt_c_${comp_num}_offset");
156 $component->{'length'} = $input->param("mc_${mc_num}_tgt_c_${comp_num}_length");
158 $component->{'norms'} = [];
159 my @norm_nums = sort map { /^mc_${mc_num}_tgt_c_${comp_num}_n_(\d+)_norm/ ? int($1): () } $input->multi_param;
160 foreach my $norm_num (@norm_nums) {
161 push @{ $component->{'norms'} }, $input->multi_param("mc_${mc_num}_tgt_c_${comp_num}_n_${norm_num}_norm");
163 push @$tgt_components, $component;
165 $matcher->add_required_check($src_components, $tgt_components);
168 if (defined $matcher_id and $matcher_id =~ /^\d+/) {
169 $matcher->_id($matcher_id);
170 $template->param(edited_matching_rule => $matcher->code());
172 $template->param(added_matching_rule => $matcher->code());
174 $matcher_id = $matcher->store();
177 sub delete_matching_rule_form {
178 my $template = shift;
179 my $matcher_id = shift;
181 my $matcher = C4::Matcher->fetch($matcher_id);
183 delete_matching_rule_form => 1,
184 confirm_op => "delete_matching_rule_confirmed",
185 matcher_id => $matcher_id,
186 code => $matcher->code(),
187 description => $matcher->description(),
191 sub delete_matching_rule {
192 my $template = shift;
193 my $matcher_id = shift;
195 my $matcher = C4::Matcher->fetch($matcher_id);
196 $template->param(deleted_matching_rule => $matcher->code(),
198 C4::Matcher->delete($matcher_id);
201 sub edit_matching_rule_form {
202 my $template = shift;
203 my $matcher_id = shift;
205 my $matcher = C4::Matcher->fetch($matcher_id);
207 $template->{VARS}->{'matcher_id'} = $matcher_id;
208 $template->{VARS}->{'code'} = $matcher->code();
209 $template->{VARS}->{'description'} = $matcher->description();
210 $template->{VARS}->{'threshold'} = $matcher->threshold();
211 $template->{VARS}->{'record_type'} = $matcher->record_type();
213 my $matcher_info = $matcher->dump();
214 my @matchpoints = ();
216 foreach my $matchpoint (@{ $matcher_info->{'matchpoints'} }) {
218 my @components = _parse_components($matchpoint->{'components'});
221 index => $matchpoint->{'index'},
222 score => $matchpoint->{'score'},
223 components => \@components
226 $template->param(matchpoints => \@matchpoints);
229 my @matchchecks = ();
230 foreach my $matchcheck (@{ $matcher_info->{'matchchecks'} }) {
232 my @src_components = _parse_components($matchcheck->{'source_matchpoint'}->{'components'});
233 my @tgt_components = _parse_components($matchcheck->{'target_matchpoint'}->{'components'});
236 src_components => \@src_components,
237 tgt_components => \@tgt_components
240 $template->param(matchchecks => \@matchchecks);
243 matching_rule_form => 1,
244 edit_matching_rule => 1,
245 confirm_op => 'edit_matching_rule_confirmed',
246 max_matchpoint => $mp_num,
247 max_matchcheck => $mc_num
252 sub _parse_components {
253 my $components_ref = shift;
257 foreach my $component (@{ $components_ref }) {
261 foreach my $norm (@{ $component->{'norms'} }) {
263 push @norms, { norm_num => $norm_num, norm => $norm };
266 comp_num => $comp_num,
267 tag => $component->{'tag'},
268 subfields => join("", sort keys %{ $component->{'subfields'} }),
269 offset => $component->{'offset'},
270 'length' => $component->{'length'},
278 sub matching_rule_list {
279 my $template = shift;
281 my @matching_rules = C4::Matcher::GetMatcherList();
282 $template->param(available_matching_rules => \@matching_rules);
283 $template->param(display_list => 1);