package Koha::Plugin::Pt::KEEPS::ShelvesPrintFormats;

use Modern::Perl;

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

our $VERSION = '1.0';

our $metadata = {
    name   => 'KEEPS - Shelves Print Formats',
    author => 'Keep Solutions',
    description => 'Adds download options to shelves and biblio records',
    date_authored   => '2022-01-25',
    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 intranet_js {
    my ( $self ) = @_;

    my $path = $self->get_plugin_http_path();

    return qq%
<script>
	var pathname = window.location.pathname;
	var print_form_plugin_path = "$path";

    var lang = \$('html').attr('lang');
    if (lang == null) {
        lang = \$('html').attr('xml:lang');
    }

    // Set the global configs to synchronous 
    \$.ajaxSetup({
        async: false
    });

    var TOKENS = {};
    \$.getJSON(print_form_plugin_path + `/i18n/\${lang}.json`, function (data) {
        TOKENS = data;
    }).fail(function() { 
        \$.getJSON(print_form_plugin_path + `/i18n/default.json`, function (data) {
            TOKENS = data;
        }); 
    });

    \$.ajaxSetup({
        async: true
    });
    
    if (pathname.match("/cgi-bin/koha/virtualshelves/shelves.pl") && \$("a:contains('RIS')").length) {
        var RIS_parent = \$("a:contains('RIS')").parent().parent();
        var href = \$("a:contains('RIS')").attr('href');
        var shelfid = href.substring(href.lastIndexOf('=') + 1);

        RIS_parent.append('<li><a href="' + print_form_plugin_path + '/print.pl?format=txt&op=export&shelfid=' + shelfid + '&showbranchcode=1">' + TOKENS.marc_txt + '</a></li>');
		RIS_parent.append('<li><a href="' + print_form_plugin_path + '/print.pl?format=abrev&op=export&shelfid=' + shelfid + '&showbranchcode=1">' + TOKENS.abbreviate_list + '</a></li>');
		RIS_parent.append('<li><a href="' + print_form_plugin_path + '/print.pl?format=nov&op=export&shelfid=' + shelfid + '&showbranchcode=1">' + TOKENS.callnumber_list + '</a></li>');
		RIS_parent.append('<li><a href="' + print_form_plugin_path + '/print.pl?format=creator&op=export&shelfid=' + shelfid + '&showbranchcode=1">' + TOKENS.author_ordered_list + '</a></li>');
		RIS_parent.append('<li><a href="' + print_form_plugin_path + '/print.pl?format=reg&op=export&shelfid=' + shelfid + '&showbranchcode=1">' + TOKENS.record_book + '</a></li>');
        RIS_parent.append('<li><a href="' + print_form_plugin_path + '/print.pl?format=NP405&op=export&shelfid=' + shelfid + '&showbranchcode=1">' + TOKENS.np405 + '</a></li>');
    }
    if (pathname.match("/cgi-bin/koha/catalogue/detail.pl") && \$("a:contains('MARCXML')").length) {
        var MARC_parent = \$("a:contains('MARCXML')").parent().parent();
        var href = \$("a:contains('MARCXML')").attr('href');
        var biblionumber = href.substring(href.lastIndexOf('=') + 1);
        MARC_parent.append('<li><a href="' + print_form_plugin_path + '/print.pl?format=txt&op=export&bib=' + biblionumber + '">' + TOKENS.marc_txt + '</a></li>');
    }
</script>
%;
}

1;