Bug 20248: (QA follow-up) Remove unnecessary stuff
[koha.git] / admin / searchengine / elasticsearch / mappings.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 use CGI;
20 use C4::Koha;
21 use C4::Output;
22 use C4::Auth;
23
24 use Koha::SearchEngine::Elasticsearch;
25 use Koha::SearchMarcMaps;
26 use Koha::SearchFields;
27
28 my $input = new CGI;
29 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
30     {   template_name   => 'admin/searchengine/elasticsearch/mappings.tt',
31         query           => $input,
32         type            => 'intranet',
33         authnotrequired => 0,
34         flagsrequired   => { superlibrarian => 1 },                          # Create a specific permission?
35     }
36 );
37
38 my $index = $input->param('index') || 'biblios';
39 my $op    = $input->param('op')    || 'list';
40 my @messages;
41
42 my $database = Koha::Database->new();
43 my $schema   = $database->schema;
44
45 my $marc_type = lc C4::Context->preference('marcflavour');
46
47 if ( $op eq 'edit' ) {
48
49     $schema->storage->txn_begin;
50
51     my @field_name = $input->param('search_field_name');
52     my @field_label = $input->param('search_field_label');
53     my @field_type = $input->param('search_field_type');
54
55     my @index_name          = $input->param('mapping_index_name');
56     my @search_field_name  = $input->param('mapping_search_field_name');
57     my @mapping_sort        = $input->param('mapping_sort');
58     my @mapping_facet       = $input->param('mapping_facet');
59     my @mapping_suggestible = $input->param('mapping_suggestible');
60     my @mapping_marc_field  = $input->param('mapping_marc_field');
61
62     eval {
63
64         for my $i ( 0 .. scalar(@field_name) - 1 ) {
65             my $field_name = $field_name[$i];
66             my $field_label = $field_label[$i];
67             my $field_type = $field_type[$i];
68             my $search_field = Koha::SearchFields->find( { name => $field_name }, { key => 'name' } );
69             $search_field->label($field_label);
70             $search_field->type($field_type);
71             $search_field->store;
72         }
73
74         Koha::SearchMarcMaps->search( { marc_type => $marc_type, } )->delete;
75
76         for my $i ( 0 .. scalar(@index_name) - 1 ) {
77             my $index_name          = $index_name[$i];
78             my $search_field_name  = $search_field_name[$i];
79             my $mapping_marc_field  = $mapping_marc_field[$i];
80             my $mapping_facet       = $mapping_facet[$i];
81             my $mapping_suggestible = $mapping_suggestible[$i];
82             my $mapping_sort        = $mapping_sort[$i];
83             $mapping_sort = undef if $mapping_sort eq 'undef';
84
85             my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
86             # TODO Check mapping format
87             my $marc_field = Koha::SearchMarcMaps->find_or_create({ index_name => $index_name, marc_type => $marc_type, marc_field => $mapping_marc_field });
88             $search_field->add_to_search_marc_maps($marc_field, { facet => $mapping_facet, suggestible => $mapping_suggestible, sort => $mapping_sort } );
89
90         }
91     };
92     if ($@) {
93         push @messages, { type => 'error', code => 'error_on_update', message => $@, };
94         $schema->storage->txn_rollback;
95     } else {
96         push @messages, { type => 'message', code => 'success_on_update' };
97         $schema->storage->txn_commit;
98     }
99 }
100 elsif( $op eq 'reset_confirmed' ) {
101     Koha::SearchMarcMaps->delete;
102     Koha::SearchFields->delete;
103     Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
104     push @messages, { type => 'message', code => 'success_on_reset' };
105 }
106 elsif( $op eq 'reset_confirm' ) {
107     $template->param( reset_confirm => 1 );
108 }
109
110
111 my @indexes;
112
113 for my $index_name (qw| biblios authorities |) {
114     my $search_fields = Koha::SearchFields->search(
115         { 'search_marc_map.index_name' => $index_name, 'search_marc_map.marc_type' => $marc_type, },
116         {   join => { search_marc_to_fields => 'search_marc_map' },
117             '+select' => [ 'search_marc_to_fields.facet', 'search_marc_to_fields.suggestible', 'search_marc_to_fields.sort', 'search_marc_map.marc_field' ],
118             '+as'     => [ 'facet',                       'suggestible',                       'sort',                       'marc_field' ],
119             order_by => { -asc => [qw/name marc_field/] }
120         }
121     );
122
123     my @mappings;
124     while ( my $s = $search_fields->next ) {
125         push @mappings,
126           { search_field_name  => $s->name,
127             search_field_label => $s->label,
128             search_field_type  => $s->type,
129             marc_field         => $s->get_column('marc_field'),
130             sort               => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
131             suggestible        => $s->get_column('suggestible'),
132             facet              => $s->get_column('facet'),
133           };
134     }
135
136     push @indexes, { index_name => $index_name, mappings => \@mappings };
137 }
138
139 my $search_fields = $schema->resultset('SearchField')->search;
140 my @all_search_fields = $search_fields->search( {}, { order_by => ['name'] } );
141 $template->param(
142     indexes           => \@indexes,
143     all_search_fields => \@all_search_fields,
144     messages          => \@messages,
145 );
146
147 output_html_with_http_headers $input, $cookie, $template->output;