From f5344aeef07647de686601a3d13428e0d85d4ee8 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Fri, 1 Sep 2023 12:40:15 +0000
Subject: [PATCH] Bug 27153: Interface updates

This patch adds a new column to the 'Bibliographic records' tab in
Administration > Search engine configuration (Elasticsearch)

To test:
1 - Confirm the new 'filter' column shows
2 - Update an existing field to set filter to punctuation
3 - Confirm it can be saved
4 - Add a new field
5 - Confirm it saves correctly
6 - Unset filter for a field
7 - Confirm it saves

Signed-off-by: Danielle M Elder <danielle.elder@law.utexas.edu>
---
 admin/searchengine/elasticsearch/mappings.pl  | 29 ++++++++++++-------
 .../searchengine/elasticsearch/mappings.tt    | 26 +++++++++++++++++
 2 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/admin/searchengine/elasticsearch/mappings.pl b/admin/searchengine/elasticsearch/mappings.pl
index e4d5ab10ea..7d404e7797 100755
--- a/admin/searchengine/elasticsearch/mappings.pl
+++ b/admin/searchengine/elasticsearch/mappings.pl
@@ -103,6 +103,7 @@ if ( $op eq 'edit' ) {
     my @mapping_facet       = $input->multi_param('mapping_facet');
     my @mapping_suggestible = $input->multi_param('mapping_suggestible');
     my @mapping_search      = $input->multi_param('mapping_search');
+    my @mapping_filter      = $input->multi_param('mapping_filter');
     my @mapping_marc_field  = $input->multi_param('mapping_marc_field');
     my @faceted_field_names = $input->multi_param('display_facet');
 
@@ -153,6 +154,7 @@ if ( $op eq 'edit' ) {
             my $mapping_suggestible = $mapping_suggestible[$i];
             my $mapping_sort        = $mapping_sort[$i] eq 'undef' ? undef : $mapping_sort[$i];
             my $mapping_search      = $mapping_search[$i];
+            my $mapping_filter      = $mapping_filter[$i];
 
             my $search_field = Koha::SearchFields->find({ name => $search_field_name }, { key => 'name' });
             $mandatory_after++ if $search_field->mandatory && !defined $seen_fields{$search_field_name};
@@ -166,12 +168,16 @@ if ( $op eq 'edit' ) {
                 marc_type => $marc_type,
                 marc_field => $mapping_marc_field
             });
-            $search_field->add_to_search_marc_maps($marc_field, {
-                facet => $mapping_facet,
-                suggestible => $mapping_suggestible,
-                sort => $mapping_sort,
-                search => $mapping_search
-            });
+            $search_field->add_to_search_marc_maps(
+                $marc_field,
+                {
+                    facet       => $mapping_facet,
+                    suggestible => $mapping_suggestible,
+                    sort        => $mapping_sort,
+                    search      => $mapping_search,
+                    filter      => $mapping_filter
+                }
+            );
         }
         push @errors, { type => 'error', code => 'missing_mandatory_fields' } if $mandatory_after < $mandatory_before;
     };
@@ -227,15 +233,16 @@ for my $index_name (@index_names) {
     my $search_fields = Koha::SearchFields->search(
         {
             'search_marc_map.index_name' => $index_name,
-            'search_marc_map.marc_type' => $marc_type,
+            'search_marc_map.marc_type'  => $marc_type,
         },
         {
-            join => { search_marc_to_fields => 'search_marc_map' },
+            join      => { search_marc_to_fields => 'search_marc_map' },
             '+select' => [
                 'search_marc_to_fields.facet',
                 'search_marc_to_fields.suggestible',
                 'search_marc_to_fields.sort',
                 'search_marc_to_fields.search',
+                'search_marc_to_fields.filter',
                 'search_marc_map.marc_field'
             ],
             '+as' => [
@@ -243,11 +250,12 @@ for my $index_name (@index_names) {
                 'suggestible',
                 'sort',
                 'search',
+                'filter',
                 'marc_field'
             ],
             order_by => { -asc => [qw/name marc_field/] }
-         }
-     );
+        }
+    );
 
     my @mappings;
     my @facetable_field_names = map { $_->name } @facetable_fields;
@@ -263,6 +271,7 @@ for my $index_name (@index_names) {
             sort               => $s->get_column('sort') // 'undef', # To avoid warnings "Use of uninitialized value in lc"
             suggestible        => $s->get_column('suggestible'),
             search             => $s->get_column('search'),
+            filter             => $s->get_column('filter'),
             facet              => $s->get_column('facet'),
             is_facetable       => ( grep { $_ eq $name } @facetable_field_names ) ? 1 : 0,
         };
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
index 603a9b7d0b..840b907ca0 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/searchengine/elasticsearch/mappings.tt
@@ -267,6 +267,7 @@ a.add, a.delete {
                                 <th>Facetable</th>
                                 <th>Suggestible</th>
                                 <th>Searchable</th>
+                                <th>Filter</th>
                                 <th>Mapping</th>
                                 <th>&nbsp;</th>
                                 </tr>
@@ -297,6 +298,9 @@ a.add, a.delete {
                                             <td>
                                                 <input type="hidden" name="mapping_search" value="[% mapping.search | html %]" readonly />[% IF mapping.search == 1 %]Yes[% ELSE %]No[% END %]
                                             </td>
+                                            <td>
+                                                <input type="hidden" name="mapping_filter" value="[% mapping.filter | html %]" readonly />[% IF mapping.filter == 'punctuation' %]Punctuation[% END %]
+                                            </td>
                                             <td data-search="[% mapping.marc_field | html %]">
                                                 <input name="mapping_marc_field" type="text" value="[% mapping.marc_field | html %]" />
                                             </td>
@@ -359,6 +363,17 @@ a.add, a.delete {
                                                 [% END %]
                                             </select>
                                             </td>
+                                            <td>
+                                                <select name="mapping_filter">
+                                                    [% IF mapping.filter == 'punctuation' %]
+                                                        <option value="">None</option>
+                                                        <option value="punctuation" selected="selected">Punctuation</option>
+                                                    [% ELSE %]
+                                                        <option value="" selected="selected">None</option>
+                                                        <option value="punctuation">Punctuation</option>
+                                                    [% END %]
+                                                </select>
+                                            </td>
                                             <td data-search="[% mapping.marc_field | html %]">
                                                 <input name="mapping_marc_field" type="text" value="[% mapping.marc_field | html %]" />
                                             </td>
@@ -421,6 +436,17 @@ a.add, a.delete {
                                             [% END %]
                                         </select>
                                     </td>
+                                    <td>
+                                        <select name="mapping_filter">
+                                            [% IF mapping.filter == 'punctuation' %]
+                                                <option value="">None</option>
+                                                <option value="punctuation" selected="selected">Punctuation</option>
+                                            [% ELSE %]
+                                                <option value="" selected="selected">None</option>
+                                                <option value="punctuation">Punctuation</option>
+                                            [% END %]
+                                        </select>
+                                    </td>
                                     <td><input data-id="mapping_marc_field" type="text" /></td>
                                     <td><a class="btn btn-default btn-xs add"><i class="fa fa-plus"></i> Add</a></td>
                                 </tr>
-- 
2.30.2