package Koha::Plugin::Pt::KEEPS::OpacLastDocuments;

use Modern::Perl;

use base qw(Koha::Plugins::Base);
use Data::Dumper;
use JSON;

our $VERSION = '1.6';

our $metadata = {
    name   => 'KEEPS - OPAC Last Documents',
    author => 'Keep Solutions',
    description => 'Shows the last documents added to the system',
    date_authored   => '2022-01-28',
    date_updated    => '2024-09-09',
    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);

    return $self;
}

sub cronjob {
    my $dir = C4::Context->config("pluginsdir");
    my $path = $dir . "/Koha/Plugin/Pt/KEEPS/OpacLastDocuments";

    my $PERL= "/usr/bin/perl";
    my $PERL5LIB= "/KEEPS/koha/lib/";

    system("$PERL -I $PERL5LIB $path/opac-lastdocuments-cronjob.pl");
}

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

    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,
            opac_book_news_records => $self->retrieve_data('opac_book_news_records') || 5,
            opac_book_news_refresh => $self->retrieve_data('opac_book_news_refresh') || 86000,
            opac_book_news_limit => $self->retrieve_data('opac_book_news_limit') || 100,
            opac_book_news_slick => $self->retrieve_data('opac_book_news_slick'),
            opac_book_news => $self->retrieve_data('opac_book_news'),
            opac_book_news_biblionumbers => $self->retrieve_data('opac_book_news_biblionumbers'),
            opac_suggestions_biblionumbers => $self->retrieve_data('opac_suggestions_biblionumbers'),
            opac_top_issues_biblionumbers => $self->retrieve_data('opac_top_issues_biblionumbers'),
            custom_prefs => $self->retrieve_data('custom_prefs'),
            select_criteria => $self->retrieve_data('select_criteria') || "biblio",
            last_upgraded   => $self->retrieve_data('last_upgraded'),
            img_alt_text => $self->retrieve_data('img_alt_text') || "",
        );

        $self->output_html( $template->output() );
    } else {
        my @custom_prefs = ();
        my @param_names = $cgi->param();
        foreach my $p (@param_names) {
            my %custom_prefs = ();
            if($p =~ /^opac_[a-zA-Z0-9].*_biblionumbers$/ && $p ne "opac_book_news_biblionumbers" && $p ne "opac_suggestions_biblionumbers" && $p ne "opac_top_issues_biblionumbers") {
                $custom_prefs{name} = $p;
                $custom_prefs{desc} = $cgi->param($p . "_desc");
                $custom_prefs{value} = $cgi->param($p);
                push(@custom_prefs, \%custom_prefs);
            }
        }
        my $json_text = to_json \@custom_prefs, {ascii=>1, pretty => 1}; 

        $self->store_data(
            {
                opac_book_news_records => $cgi->param('opac_book_news_records'),
                opac_book_news_refresh => $cgi->param('opac_book_news_refresh'),
                opac_book_news_limit => $cgi->param('opac_book_news_limit'),
                opac_book_news_slick => $cgi->param('opac_book_news_slick'),
                opac_book_news => $cgi->param('opac_book_news'),
                opac_book_news_biblionumbers => $cgi->param('opac_book_news_biblionumbers'),
                opac_suggestions_biblionumbers => $cgi->param('opac_suggestions_biblionumbers'),
                opac_top_issues_biblionumbers => $cgi->param('opac_top_issues_biblionumbers'),
                select_criteria => $cgi->param('select_criteria'),
                custom_prefs => $json_text,
                last_configured_by => C4::Context->userenv->{'number'},
                img_alt_text => $cgi->param('img_alt_text'),
            }
        );
        $self->go_home();
    }
}

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

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

    return qq%
<script>

</script>
%;
}

1;