package Koha::Plugin::Pt::KEEPS::AuthoritiesDefaultSearch;
use Modern::Perl;

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

our $VERSION = '1.2';

our $metadata = {
    name            => 'KEEPS - Authorities Default Search',
    author          => 'Keep Solutions',
    description     => 'Searches entire auth record by default',
    date_authored   => '2022-11-21',
    date_updated    => '2024-11-15',
    minimum_version => '21.05',
    maximum_version => '21.05',
    version         => $VERSION
};

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

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

    my $self = $class->SUPER::new($args);
    $self->{cgi} = CGI->new();

    return $self;
}

#Mandatory even if it 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 configure {
    my ($self, $args) = @_;
    my $cgi = $self->{'cgi'};

    my $authority_types = Koha::Authority::Types->search( {}, { order_by => ['authtypetext'] } );
    
    unless ($cgi->param('save')) {
        my $lang_dialect = C4::Languages::getlanguage($cgi);

        my $template = $self->get_template({ file => 'configure.tt' });
        $template->param(
            lang_dialect => $lang_dialect,
            authority_types => $authority_types,
            authorities_search_pref => $self->retrieve_data('authorities-search-pref'),
            authorities_type_search_pref => $self->retrieve_data('authorities-type-search-pref'),
            authorities_operator_search_pref => $self->retrieve_data('authorities-operator-search-pref'),
            authorities_orderby_search_pref => $self->retrieve_data('authorities-orderby-search-pref'),
        );

        $self->output_html( $template->output() );
    } else {
        $self->store_data(
            {
                authorities_search_pref => $cgi->param('authorities-search-pref'),
                authorities_type_search_pref => $cgi->param('authorities-type-search-pref'),
                authorities_operator_search_pref => $cgi->param('authorities-operator-search-pref'),
                authorities_orderby_search_pref => $cgi->param('authorities-orderby-search-pref'),
            }
        );
        $self->go_home();
    }
    return 1;
}


sub intranet_js{
    my ( $self ) = @_;
    my $path=$self->get_plugin_http_path();

    my $authorities_tab_pref = $self->retrieve_data('authorities_search_pref');
    my $authorities_type_search_pref = $self->retrieve_data('authorities_type_search_pref');
    my $authorities_operator_search_pref = $self->retrieve_data('authorities_operator_search_pref');
    my $authorities_orderby_search_pref = $self->retrieve_data('authorities_orderby_search_pref');

    my @authority_types = Koha::Authority::Types->search->get_column('authtypecode');

    return qq%

<script>
    var pathname=window.location.pathname;
    var urlParams=new URLSearchParams(window.location.search);
    var marclist=urlParams.has('marclist');
    var authoritiesSearchPref = '$authorities_tab_pref';
    var authoritiesTypeSearchPref = '$authorities_type_search_pref';
    var authoritiesOperatorSearchPref = '$authorities_operator_search_pref';
    var authoritiesOrderBySearchPref = '$authorities_orderby_search_pref';
    var auth_types = '@authority_types'.split(/[ ,]+/);


    //if match of mainpage.pl use catalog search instead of circ-search
    \$(document).ready(function(){
        if((pathname.match("authorities/authorities-home.pl") || pathname.match("authorities/detail.pl") )&& !marclist){

            switch(authoritiesSearchPref){
                case 'mainmain-heading-panel':
                    \$('a[href="#mainmain_heading"]').trigger('click');
                    break;
                case 'main-heading-panel':
                    \$('a[href="#main_heading"]').trigger('click');
                    break;
                case 'matchheading-search-panel':
                    \$('a[href="#matchheading_search"]').trigger('click');
                    break;
                case 'entire-record-panel':
                    \$('a[href="#entire_record"]').trigger('click');
                    break;
                default:
                    \$('a[href="#mainmain_heading"]').trigger('click');
            }
            for(let a=0; a < auth_types.length; a++){
                if( auth_types[a] == authoritiesTypeSearchPref ){
                    \$("select[name='authtypecode']").val(auth_types[a]);
                }
            }
            switch(authoritiesOperatorSearchPref){
                case 'contains':
                    \$("select[name='operator']").val("contains");
                    break;
                case 'start':
                    \$("select[name='operator']").val("start");
                    break;
                case 'exact':
                    \$("select[name='operator']").val("exact");
                    break;
                default:
                    \$("select[name='operator']").val("start");
            }

            switch(authoritiesOrderBySearchPref){
                case 'HeadingAsc':
                    \$("select[name='orderby']").val("HeadingAsc");
                    break;
                case 'HeadingDsc':
                    \$("select[name='orderby']").val("HeadingDsc");
                    break;
                case 'None':
                    \$("select[name='orderby']").val("");
                    break;
                default:
                    \$("select[name='orderby']").val("");

            }
        }
        if( pathname.match("/authorities/auth_finder.pl") ){
            switch(authoritiesOperatorSearchPref){
                case 'contains':
                    \$("select[name='operator']").val("contains");
                    break;
                case 'start':
                    \$("select[name='operator']").val("start");
                    break;
                case 'exact':
                    \$("select[name='operator']").val("exact");
                    break;
                default:
                    \$("select[name='operator']").val("start");
            }

            switch(authoritiesOrderBySearchPref){
                case 'HeadingAsc':
                    \$("select[name='orderby']").val("HeadingAsc");
                    break;
                case 'HeadingDsc':
                    \$("select[name='orderby']").val("HeadingDsc");
                    break;
                case 'None':
                    \$("select[name='orderby']").val("");
                    break;
                default:
                    \$("select[name='orderby']").val("HeadingAsc");
            }
        }
    });
</script>
%;
}

1;