package Koha::Plugin::Pt::KEEPS::CASRedirect;

use Modern::Perl;

use C4::Languages;

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

our $VERSION = '1.1';


our $metadata = {
    name            => 'KEEPS - CAS Redirect',
    author          => 'Keep Solutions',
    description     => 'Enables CAS redirection for the login pages in OPAC and Backoffice',
    date_authored   => '2022-04-29',
    date_updated    => '2024-03-11',
    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;
}


sub install {
    my ( $self, $args ) = @_;
    
    $self->store_data({
        opac => 0,
        backoffice => 0        
    });

    return 1;
}


sub upgrade {
    my ( $self, $args ) = @_;
    return 1;
}


sub uninstall {
    my ( $self, $args ) = @_;
    return 1;
}


sub configure {
    my ( $self, $args ) = @_;
    my $cgi = $self->{'cgi'};
    my $lang_dialect = C4::Languages::getlanguage($cgi);

    if ($cgi->param('set')) {
        my $opac = $cgi->param('cas_opac') ? 1 : 0;
        my $cas_opac_url = $cgi->param('cas_opac_url');
        my $backoffice = $cgi->param('cas_backoffice') ? 1 : 0;
        my $cas_backoffice_url = $cgi->param('cas_backoffice_url');
        my $opac_html = $cgi->param('opac_html');
        my $backoffice_html = $cgi->param('backoffice_html');
        my $opac_js = $cgi->param('opac_js');
        my $backoffice_js = $cgi->param('backoffice_js');
        my $opac_css = $cgi->param('opac_css');
        my $backoffice_css = $cgi->param('backoffice_css');

        handle_set($self, {
            opac => $opac,
            cas_opac_url => $cas_opac_url,
            backoffice => $backoffice,
            cas_backoffice_url => $cas_backoffice_url,
            opac_html => $opac_html,
            backoffice_html => $backoffice_html,
            opac_js => $opac_js,
            backoffice_js => $backoffice_js,
            opac_css => $opac_css,
            backoffice_css => $backoffice_css
        });
    }
    else {
        handle_configure_page($self, {
            lang_dialect => $lang_dialect
        });
    }
}



sub handle_set {
    my ($self, $params) = @_;

    my $opac = $params->{'opac'};
    my $cas_opac_url = $params->{'cas_opac_url'};
    my $backoffice = $params->{'backoffice'};
    my $cas_backoffice_url = $params->{'cas_backoffice_url'};
    my $opac_html = $params->{'opac_html'};
    my $backoffice_html = $params->{'backoffice_html'};
    my $opac_js = $params->{'opac_js'};
    my $backoffice_js = $params->{'backoffice_js'};
    my $opac_css = $params->{'opac_css'};
    my $backoffice_css = $params->{'backoffice_css'};
    
    $self->store_data({
        opac => $opac,
        cas_opac_url => $cas_opac_url,
        backoffice => $backoffice,
        cas_backoffice_url => $cas_backoffice_url,
        opac_html => $opac_html,
        backoffice_html => $backoffice_html,
        opac_js => $opac_js,
        backoffice_js => $backoffice_js,
        opac_css => $opac_css,
        backoffice_css => $backoffice_css
    });

    go_configure_page($self);
}


sub handle_configure_page {
    my ($self, $params) = @_;

    my $template = $self->get_template({ file => 'configure.tt' });

    my $opac = $self->retrieve_data("opac");
    my $cas_opac_url = $self->retrieve_data("cas_opac_url");
    my $backoffice = $self->retrieve_data("backoffice");
    my $cas_backoffice_url = $self->retrieve_data("cas_backoffice_url");
    my $opac_html = $self->retrieve_data("opac_html");
    my $backoffice_html = $self->retrieve_data("backoffice_html");
    my $opac_js = $self->retrieve_data("opac_js");
    my $backoffice_js = $self->retrieve_data("backoffice_js");
    my $opac_css = $self->retrieve_data("opac_css");
    my $backoffice_css = $self->retrieve_data("backoffice_css");

    $template->param(
        lang_dialect => $params->{'lang_dialect'},
        opac => $opac,
        cas_opac_url => $cas_opac_url,
        backoffice => $backoffice,
        cas_backoffice_url => $cas_backoffice_url,
        opac_html => $opac_html,
        backoffice_html => $backoffice_html,
        opac_js => $opac_js,
        backoffice_js => $backoffice_js,
        opac_css => $opac_css,
        backoffice_css => $backoffice_css
    );

    $self->output_html($template->output());
}


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

    my $class = $self->{'cgi'}->param('class');
    my $method = $self->{'cgi'}->param('method');

    my $query_str = "?class=$class&method=$method";

    print $self->{'cgi'}->redirect(
        -uri => $query_str
    );
}


sub opac_js {
    my ($self) = @_;
    
    my $opac = $self->retrieve_data("opac");
    my $cas_opac_url = $self->retrieve_data("cas_opac_url");
    my $opac_html = $self->retrieve_data("opac_html");
    my $opac_js = $self->retrieve_data("opac_js");
    my $opac_css = $self->retrieve_data("opac_css");
    my $html_size = length($opac_html);
    
    return qq%
<script>
\$(document).ready(function() {
    
    var pathname = window.location.pathname;

    if (pathname.match("/cgi-bin/koha/opac-user.pl")) {
        if ($opac) {
            var redirect_url = "$cas_opac_url";
            if (redirect_url == "") {
                console.log("WARNING: CAS URL is not set");
            }
            else {
                window.location.replace(redirect_url);
            }
        } 
        if('$html_size' != "0") {
            \$("a[href*='/cas/login']").parent().prev().remove();
            \$("a[href*='/cas/login']").parent().html(`$opac_html`);
        }
        if('$opac_css' != "") {
            \$("head").append('$opac_css');
        }
        $opac_js
    }

});
</script>
%;
}


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

    my $backoffice = $self->retrieve_data("backoffice");
    my $cas_backoffice_url = $self->retrieve_data("cas_backoffice_url");
    my $backoffice_html = $self->retrieve_data("backoffice_html");
    my $backoffice_js = $self->retrieve_data("backoffice_js");
    my $backoffice_css = $self->retrieve_data("backoffice_css");
    my $html_size = length($backoffice_html);

    return qq%
<script>
\$(document).ready(function() {
    
    if (\$("#main_auth").length) {
        if ($backoffice) {
            var redirect_url = "$cas_backoffice_url";
            if (redirect_url == "") {
                console.log("WARNING: CAS URL is not set");
            }
            else {
                window.location.replace(redirect_url);
            }
        } 
        if('$html_size' != "0") {
            \$("#loginform").before(`$backoffice_html`);
            \$("#loginform").remove();
        }
        if('$backoffice_css' != "") {
            \$("head").append('$backoffice_css');
        }
        $backoffice_js
    }

});
</script>
%;
}


1;

