Bug 8233 : SearchEngine: Add a Koha::SearchEngine module
[koha.git] / admin / searchengine / solr / indexes.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 BibLibre SARL
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use CGI;
22 use C4::Koha;
23 use C4::Output;
24 use C4::Auth;
25 use Koha::SearchEngine;
26
27 my $input = new CGI;
28 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
29     {
30         template_name   => 'admin/searchengine/solr/indexes.tt',
31         query           => $input,
32         type            => 'intranet',
33 #        authnotrequired => 0,
34 #        flagsrequired   => { reserveforothers => "place_holds" }, #TODO
35     }
36 );
37
38 my $ressource_type = $input->param('ressource_type') || 'biblio';
39 my $se = Koha::SearchEngine->new;
40 my $se_config = $se->config;
41
42 my $indexes;
43 if ( $input->param('op') and $input->param('op') eq 'edit' ) {
44     my @code            = $input->param('code');
45     my @label           = $input->param('label');
46     my @type            = $input->param('type');
47     my @sortable        = $input->param('sortable');
48     my @facetable       = $input->param('facetable');
49     my @mandatory       = $input->param('mandatory');
50     my @ressource_type  = $input->param('ressource_type');
51     my @mappings        = $input->param('mappings');
52     my @indexes;
53     my @errors;
54     for ( 0 .. @code-1 ) {
55         my $icode = $code[$_];
56         my @current_mappings = split /\r\n/, $mappings[$_];
57         if ( not @current_mappings ) {
58             @current_mappings = split /\n/, $mappings[$_];
59         }
60         if ( not @current_mappings ) {
61             push @errors, { type => 'no_mapping', value => $icode};
62         }
63
64         push @indexes, {
65             code           => $icode,
66             label          => $label[$_],
67             type           => $type[$_],
68             sortable       => scalar(grep(/^$icode$/, @sortable)),
69             facetable      => scalar(grep(/^$icode$/, @facetable)),
70             mandatory      => $mandatory[$_] eq '1' ? '1' : '0',
71             ressource_type => $ressource_type[$_],
72             mappings       => \@current_mappings,
73         };
74         for my $m ( @current_mappings ) {
75             push @errors, {type => 'malformed_mapping', value => $m}
76                 if not $m =~ /^\d(\d|\*|\.){2}\$.$/;
77         }
78     }
79     $indexes = \@indexes if @errors;
80     $template->param( errors => \@errors );
81
82     $se_config->indexes(\@indexes) if not @errors;
83 }
84
85 my $ressource_types = $se_config->ressource_types;
86 $indexes //= $se_config->indexes;
87
88 my $indexloop;
89 for my $rt ( @$ressource_types ) {
90     my @indexes = map {
91         $_->{ressource_type} eq $rt ? $_ : ();
92     } @$indexes;
93     push @$indexloop, {
94         ressource_type => $rt,
95         indexes => \@indexes,
96     }
97 }
98
99 $template->param(
100     indexloop       => $indexloop,
101 );
102
103 output_html_with_http_headers $input, $cookie, $template->output;