package Koha::Plugin::Pt::KEEPS::OpacQueryTruncate;

use Modern::Perl;

use base qw(Koha::Plugins::Base);

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - OPAC Query Truncate',
    author => 'Keep Solutions',
    description => 'Search by authority number, barcode and biblio number using phrase index',
    date_authored   => '2022-01-27',
    date_updated    => undef,
    minimum_version => '21.05',
    maximum_version => undef,
    version         => $VERSION,
};

sub new {
    my ( $class, $args ) = @_;

    $args->{'metadata'} = $metadata;
    $args->{'metadata'}->{'class'} = $class;

    my $self = $class->SUPER::new($args);

    return $self;
}

# Mandatory even if does nothing
sub install {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub upgrade {
    my ( $self, $args ) = @_;

    return 1;
}

# Mandatory even if does nothing
sub uninstall {
    my ( $self, $args ) = @_;

    return 1;
}

sub opac_js {
    my ( $self ) = @_;

    return qq%
<script>
    var pathname = window.location.pathname;

    \$(document).ready(function () {

        if (pathname.match("/cgi-bin/koha/opac-authorities-home.pl") && \$("#userauthsearchresults").length) {
            \$(".searchresults td:nth-child(4) a").map(function(i, val){
                var biblioLinkSearch = \$(val).attr("href");
                biblioLinkSearch = biblioLinkSearch.replace("q=an=", "idx=an,phr&q=");
                \$(val).attr("href", biblioLinkSearch);
            });
        }

        if(pathname.match("/cgi-bin/koha/opac-search.pl")) {
            \$('select[name="idx"] option[value="bc"]').map(function(i, val){
                \$(val).val("bc,phr");
            });
        }
    });
</script>
%;
}

1;