[% USE raw %]
[% USE Asset %]
[% SET footerjs = 1 %]
[% PROCESS 'i18n.inc' %]
[% INCLUDE 'doc-head-open.inc' %]
<title>[% FILTER collapse %]
    [% t("Search engine configuration (Elasticsearch)") | html %] &rsaquo;
    [% t("Administration") | html %] &rsaquo;
    [% t("Koha") | html %]
[% END %]</title>
[% INCLUDE 'doc-head-close.inc' %]
<style>
a.add, a.delete {
    cursor: pointer;
}
</style>
</head>

<body id="admin_searchengine_mappings" class="admin">
[% WRAPPER 'header.inc' %]
    [% INCLUDE 'prefs-admin-search.inc' %]
[% END %]

[% WRAPPER 'sub-header.inc' %]
    [% WRAPPER breadcrumbs %]
        [% WRAPPER breadcrumb_item %]
            <a href="/cgi-bin/koha/admin/admin-home.pl">Administration</a>
        [% END %]
        [% WRAPPER breadcrumb_item bc_active= 1 %]
            <span>Search engine configuration (Elasticsearch)</span>
        [% END %]
    [% END #/ WRAPPER breadcrumbs %]
[% END #/ WRAPPER sub-header.inc %]

<div class="main container-fluid">
    <div class="row">
        <div class="col-sm-10 col-sm-push-2">
            <main>
                [% INCLUDE 'messages.inc' %]

    [% FOR m IN messages %]
      <div class="dialog [% m.type | html %]">
        [% SWITCH m.code %]
        [% CASE 'error_on_update' %]
          [% tx("An error occurred when updating mappings: {message}.", { message = m.message }) | html %]
        [% CASE 'error_on_delete' %]
          [% t("An error occurred when deleting the existing mappings. Nothing has been changed!") | $raw %]
          [% tx("(search field {field_name} with mapping {marc_field}.)", { field_name = m.values.field_name, marc_field = m.values.marc_field }) | html %]
        [% CASE 'invalid_field_weight' %]
          [% tx("Invalid field weight '{weight}', must be a positive decimal number.", { weight = m.weight }) | html %]
        [% CASE 'missing_mandatory_fields' %]
          [% t("You attempted to delete all mappings for a required index, you must leave at least one mapping") | $raw %]
        [% CASE 'error_on_update_es_mappings' %]
          [% tx("An error occurred when updating Elasticsearch index mappings: {message}.", { message = m.message }) | html %]
        [% CASE 'reindex_required' %]
          [% tx("Index '{index}' needs to be reindexed.", { index = m.index }) | html %]
        [% CASE 'recreate_required' %]
          [% tx("Index '{index}' needs to be recreated.", { index = m.index }) | html %]
        [% CASE 'success_on_update' %]
          [% t("Mappings updated successfully.") | $raw %]
        [% CASE 'success_on_reset' %]
          [% t("Mappings have been reset successfully.") | $raw %]
        [% CASE 'elasticsearch_disabled' %]
          [% t("Elasticsearch is currently disabled.") | $raw %]
        [% CASE %]
          [% m.code | html %]
        [% END %]
      </div>
    [% END %]

    <h1>Search engine configuration (Elasticsearch)</h1>

    [% IF errors %]
        <div class="dialog alert">
        Changes have not been applied. Please check the following values:
          <ul>
            [% FOREACH e IN errors %]
                <li>
                    [% IF ( e.type == "malformed_mapping" ) %]
                        <span>The value "[% e.value | html %]" is not supported for mappings</span>
                    [% ELSIF ( e.type == "no_mapping" ) %]
                        <span>There is no mapping for the index [% e.value | html %]</span>
                    [% END %]
                </li>
            [% END %]
          </ul>
        </div>
    [% END %]

    [% IF reset_confirm %]
        <div class="dialog alert">
            <h3>The current mappings you see on the screen will be erased and replaced by the mappings in the mappings.yaml file.</h3>
            <form method="post">
                <input type="hidden" name="op" value="reset_confirmed" />
                <button type="submit" class="approve"><i class="fa fa-fw fa-check"></i> Yes, reset mappings</button>
            </form>

            <form method="post">
                <button type="submit" class="deny"><i class="fa fa-fw fa-times"></i> No, do not reset mappings</button>
            </form>
        </div>
    [% END %]

    <div class="note">
        <i class="fa fa-exclamation"></i>
        <strong>Warning:</strong> Any changes to the configuration will only take effect after a full reindex. Until then searching may not work correctly.

        <p><strong>Weight:</strong> define weight as a positive number. Higher numbers indicate increased relevancy.
        <strong>Note that fields weighting works only for simple search.</strong></p>
        <ol>
          <li>Only search fields mapped with biblios can be weighted</li>
          <li>Search will boost/increase weighted field(s) relevancy</li>
        </ol>
    </div>

    <form id="es_mappings" method="post">

        [% WRAPPER tabs id= "tabs" %]

            [% WRAPPER tabs_nav %]
                [% WRAPPER tab_item tabname= "search_fields" bt_active= 1 %] Search fields [% END %]
                [% FOREACH index IN indexes %]
                    [% SWITCH index.index_name %]
                        [% CASE 'biblios' %]
                            [% WRAPPER tab_item tabname= "mapping_biblios" %] <span>Bibliographic records</span> [% END %]
                        [% CASE 'authorities' %]
                            [% WRAPPER tab_item tabname= "mapping_authorities" %] <span>Authorities</span> [% END %]
                    [% END %]
                [% END %]
            [% END # /WRAPPER tabs_nav %]

            [% WRAPPER tab_panels %]
                [% WRAPPER tab_panel tabname="search_fields" bt_active= 1 %]
                    <table class="search_fields" id="search_fields_table">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Aliases</th>
                                <th>Label</th>
                                <th>Type</th>
                                <th>Weight</th>
                                <th colspan="2">Searchable</th>
                            </tr>
                            <tr>
                                <th colspan="5" class="NoSort">&nbsp;</th>
                                <th class="NoSort">Staff interface</th>
                                <th class="NoSort">OPAC</th>
                            </tr>
                        </thead>
                        <tbody>
                            [% FOREACH search_field IN all_search_fields %]
                                <tr>
                                    <td data-order="[% search_field.name | html %]">
                                        [% IF search_field.mandatory %]
                                            <input type="text" name="search_field_name" value="[% search_field.name | html %]" readonly />
                                        [% ELSE %]
                                            <input type="text" name="search_field_name" value="[% search_field.name | html %]" />
                                        [% END %]
                                    </td>
                                    <td>
                                        [% search_field.aliases.join(', ') | html %]
                                    </td>
                                    <td data-order="[% search_field.label | html %]">
                                        [% IF search_field.mandatory %]
                                            <input type="text" name="search_field_label" value="[% search_field.label | html %]" readonly />
                                        [% ELSE %]
                                            <input type="text" name="search_field_label" value="[% search_field.label | html %]" />
                                        [% END %]
                                    </td>
                                    <td data-order="[% search_field.type | html %]">
                                        [% IF search_field.mandatory %]
                                            <input type="hidden" name="search_field_type" value="[% search_field.type | html %]" />
                                            <select name="search_field_type" disabled>
                                        [% ELSE %]
                                            <select name="search_field_type">
                                        [% END %]
                                            <option value=""></option>
                                            [% IF search_field.type == "string" %]
                                                <option value="string" selected="selected">String</option>
                                            [% ELSE %]
                                                <option value="string">String</option>
                                            [% END %]
                                            [% IF search_field.type == "date" %]
                                                <option value="date" selected="selected">Date</option>
                                            [% ELSE %]
                                                <option value="date">Date</option>
                                            [% END %]
                                            [% IF search_field.type == "year" %]
                                                <option value="year" selected="selected">Year</option>
                                            [% ELSE %]
                                                <option value="year">Year</option>
                                            [% END %]
                                            [% IF search_field.type == "number" %]
                                                <option value="number" selected="selected">Number</option>
                                            [% ELSE %]
                                                <option value="number">Number</option>
                                            [% END %]
                                            [% IF search_field.type == "boolean" %]
                                                <option value="boolean" selected="selected">Boolean</option>
                                            [% ELSE %]
                                                <option value="boolean">Boolean</option>
                                            [% END %]
                                            [% IF search_field.type == "sum" %]
                                                <option value="sum" selected="selected">Sum</option>
                                            [% ELSE %]
                                                <option value="sum">Sum</option>
                                            [% END %]
                                            [% IF search_field.type == "isbn" %]
                                                <option value="isbn" selected="selected">ISBN</option>
                                            [% ELSE %]
                                                <option value="isbn">ISBN</option>
                                            [% END %]
                                            [% IF search_field.type == "stdno" %]
                                                <option value="stdno" selected="selected">Std. Number</option>
                                            [% ELSE %]
                                                <option value="stdno">Std. Number</option>
                                            [% END %]
                                            [% IF search_field.type == "callnumber" %]
                                                <option value="callnumber" selected="selected">Call Number</option>
                                            [% ELSE %]
                                                <option value="callnumber">Call Number</option>
                                            [% END %]
                                        </select>
                                    </td>
                                        <td data-order="[% search_field.weight | html %]">
                                            [% IF search_field.mapped_biblios %]
                                                <input type="text" inputmode="decimal" pattern="[0-9\.]*" name="search_field_weight" value="[% search_field.weight | html %]" />
                                            [% ELSE %]
                                                <input type="text" name="search_field_weight" value="">
                                            [% END %]
                                        </td>
                                    <td>
                                        <select name="search_field_staff_client">
                                            [% IF search_field.staff_client %]
                                                <option value="1" selected="selected">Yes</option>
                                                <option value="0">No</option>
                                            [% ELSE %]
                                                <option value="1">Yes</option>
                                                <option value="0" selected="selected">No</option>
                                            [% END %]
                                        </select>
                                    </td>
                                    <td>
                                        <select name="search_field_opac">
                                            [% IF search_field.opac %]
                                                <option value="1" selected="selected">Yes</option>
                                                <option value="0">No</option>
                                            [% ELSE %]
                                                <option value="1">Yes</option>
                                                <option value="0" selected="selected">No</option>
                                            [% END %]
                                        </select>
                                    </td>
                                </tr>
                            [% END %]
                        </tbody>
                    </table>
                [% END %]
                [% FOREACH index IN indexes %]
                    [% WRAPPER tab_panel tabname="mapping_${ index.index_name }" %]
                        <table class="mappings" data-index_name="[% index.index_name | html %]" data-ordering="false" id="mapping_[% index.index_name | html %]_table">
                            <thead>
                                <tr>
                                <th>Search field</th>
                                <th>Sortable</th>
                                <th>Facetable</th>
                                <th>Suggestible</th>
                                <th>Searchable</th>
                                <th>Filter</th>
                                <th>Mapping</th>
                                <th>&nbsp;</th>
                                </tr>
                            </thead>
                            <tbody>
                                [% FOREACH mapping IN index.mappings %]
                                    [% IF mapping.search_field_mandatory && mapping.search_field_label != loop.next.search_field_label && mapping.search_field_label != loop.prev.search_field_label %]
                                        <tr>
                                            <td>
                                                <input type="hidden" name="mapping_index_name" value="[% index.index_name | html %]" />
                                                <input type="hidden" name="mapping_search_field_name" value="[% mapping.search_field_name | html %]">
                                                [% mapping.search_field_name | html %]
                                                [% IF mapping.search_field_label != mapping.search_field_name %] ([% mapping.search_field_label | html %])[% END %]
                                            </td>
                                            <td>
                                                [% IF mapping.sort == 0 %]
                                                    <input type="hidden" name="mapping_sort" value="0" readonly />No
                                                [% ELSE %]
                                                    <input type="hidden" name="mapping_sort" value="1" readonly />Yes
                                                [% END %]
                                            </td>
                                            <td>
                                                <input type="hidden" name="mapping_facet" value="[% mapping.facet | html %]" readonly />[% IF mapping.facet == 1 %]Yes[% ELSE %]No[% END %]
                                            </td>
                                            <td>
                                                <input type="hidden" name="mapping_suggestible" value="[% mapping.suggestible | html %]" readonly />[% IF mapping.suggestible == 1 %]Yes[% ELSE %]No[% END %]
                                            </td>
                                            <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>
                                            <td>&nbsp;</td>
                                        </tr>
                                    [% ELSE %]
                                        <tr>
                                            <td>
                                                <input type="hidden" name="mapping_index_name" value="[% index.index_name | html %]" />
                                                <input type="hidden" name="mapping_search_field_name" value="[% mapping.search_field_name | html %]">
                                                [% mapping.search_field_name | html %]
                                                [% IF mapping.search_field_label != mapping.search_field_name %] ([% mapping.search_field_label | html %])[% END %]
                                            </td>
                                            <td>
                                                <select name="mapping_sort">
                                                    [% IF mapping.sort == 0 %]
                                                        <option value="0" selected="selected">No</option>
                                                        <option value="1">Yes</option>
                                                    [% ELSE %]
                                                        <option value="0">No</option>
                                                        <option value="1" selected="selected">Yes</option>
                                                    [% END %]
                                                </select>
                                            </td>
                                            <td>
                                                [% IF mapping.is_facetable %]
                                                    <select name="mapping_facet">
                                                        [% IF mapping.facet %]
                                                            <option value="0">No</option>
                                                            <option value="1" selected="selected">Yes</option>
                                                        [% ELSE %]
                                                            <option value="0" selected="selected">No</option>
                                                            <option value="1">Yes</option>
                                                        [% END %]
                                                    </select>
                                                [% ELSE %]
                                                    <input type="hidden" name="mapping_facet" value="0" />
                                                    No
                                                [% END %]
                                            </td>
                                            <td>
                                                <select name="mapping_suggestible">
                                                    [% IF mapping.suggestible %]
                                                        <option value="0">No</option>
                                                        <option value="1" selected="selected">Yes</option>
                                                    [% ELSE %]
                                                        <option value="0" selected="selected">No</option>
                                                        <option value="1">Yes</option>
                                                    [% END %]
                                                </select>
                                            </td>
                                            <td>
                                            <select name="mapping_search">
                                                [% IF mapping.search %]
                                                    <option value="0">No</option>
                                                    <option value="1" selected="selected">Yes</option>
                                                [% ELSE %]
                                                    <option value="0" selected="selected">No</option>
                                                    <option value="1">Yes</option>
                                                [% 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>
                                            [% IF mapping.search_field_mandatory %]
                                                <td><a class="btn btn-default btn-xs delete mandatory" data-field_name="[% mapping.search_field_name | html %]" style="cursor: pointer;"><i class="fa fa-trash"></i> Delete</a></td>
                                            [% ELSE %]
                                                <td><a class="btn btn-default btn-xs delete" style="cursor: pointer;"><i class="fa fa-trash"></i> Delete</a></td>
                                            [% END %]
                                        </tr>
                                    [% END %]
                                [% END %]
                            </tbody>
                            <tfoot>
                                <tr>
                                    <td>
                                        <input data-id="mapping_index_name" type="hidden" value="[% index.index_name | html %]" />
                                        <select data-id="mapping_search_field_name">
                                            [% FOREACH f IN all_search_fields %]
                                                <option value="[% f.name | html %]">[% f.name | html %][% IF f.label != f.name %] ([% f.label | html %])[% END %]</option>
                                            [% END %]
                                        </select>
                                    </td>
                                    <td>
                                        <select data-id="mapping_sort">
                                        <option value="0">No</option>
                                        <option value="1">Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select data-id="mapping_facet">
                                            <option value="0">No</option>
                                            <option value="1">Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select data-id="mapping_suggestible">
                                            <option value="0">No</option>
                                            <option value="1">Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select data-id="mapping_search">
                                            <option value="0">No</option>
                                            <option value="1">Yes</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select data-id="mapping_filter">
                                            <option value="">None</option>
                                            <option value="punctuation">Punctuation</option>
                                        </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>
                            </tfoot>
                        </table>

                        [% IF index.index_name == 'biblios' %]
                            <h3>Facet order</h3>
                            <div id="facet_[% index.index_name | html %]" class="ui-tabs-panel">
                                <table>
                                    <thead>
                                        <tr>
                                            <th>Search field</th>
                                            <th>Label</th>
                                            <th>Display</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        [% FOREACH f IN facetable_fields %]
                                            <tr>
                                                <td>
                                                    [% f.name | html %]
                                                </td>
                                                <td>
                                                    [% SWITCH f.name %]
                                                    [% CASE 'author' %]<span>Authors</span>
                                                    [% CASE 'itype' %]<span>Item types</span>
                                                    [% CASE 'location' %]<span>Locations</span>
                                                    [% CASE 'su-geo' %]<span>Places</span>
                                                    [% CASE 'title-series' %]<span>Series</span>
                                                    [% CASE 'subject' %]<span>Topics</span>
                                                    [% CASE 'ccode' %]<span>Collections</span>
                                                    [% CASE 'holdingbranch' %]<span>Holding libraries</span>
                                                    [% CASE 'homebranch' %]<span>Home libraries</span>
                                                    [% CASE 'ln' %]<span>Language</span>
                                                    [% CASE %][% f | html %]
                                                    [% END %]
                                                </td>
                                                <td>
                                                    [% IF f.facet_order %]
                                                        <input type="checkbox" name="display_facet" value="[% f.name | html %]" checked="checked" />
                                                    [% ELSE %]
                                                        <input type="checkbox" name="display_facet" value="[% f.name | html %]" />
                                                    [% END %]
                                                </td>
                                            </tr>
                                        [% END %]
                                    </tbody>
                                </table>
                            </div>
                        [% END %]
                    [% END # /WRAPPER tab_panel#mapping_{ index.index_name} %]
                [% END # /FOREACH index %]
            [% END # /WRAPPER tab_panels %]
        [% END # /WRAPPER tabs %]

        <fieldset class="action">
            <button class="btn btn-primary" type="submit" name="op" value="edit"><i class="fa-solid fa-hard-drive" aria-hidden="true"></i> Save</button>
            <button class="btn btn-default" type="submit" name="op" value="reset_confirm"><i class="fa-solid fa-rotate" aria-hidden="true"></i> Reset mappings</button>
        </fieldset>
    </form>

            </main>
        </div> <!-- /.col-sm-10.col-sm-push-2 -->

        <div class="col-sm-2 col-sm-pull-10">
            <aside>
                [% INCLUDE 'admin-menu.inc' %]
            </aside>
        </div> <!-- /.col-sm-2.col-sm-pull-10 -->
     </div> <!-- /.row -->

[% MACRO jsinclude BLOCK %]
    [% INCLUDE 'datatables.inc' %]
    [% Asset.js("lib/jquery/plugins/jquery.tablednd.js") | $raw %]
    [% Asset.js("js/elasticsearch-mappings.js") | $raw %]
[% END %]

[% INCLUDE 'intranet-bottom.inc' %]
