diff --git a/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm index c338791..f9e7502 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm +++ b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink.pm @@ -24,6 +24,8 @@ use MARC::Record; use Mojo::JSON qw(decode_json);; use URI::Escape qw(uri_unescape); +use Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter; + ## Here we set our plugin version our $VERSION = "{VERSION}"; our $MINIMUM_VERSION = "{MINIMUM_VERSION}"; @@ -40,6 +42,7 @@ our $metadata = { description => 'This plugin implements every available feature ' . 'of the plugin system and is meant ' . 'to be documentation and a starting point for writing your own plugins!', + namespace => 'kitchensink', }; ## This is the minimum code required for a plugin's 'new' method @@ -85,13 +88,15 @@ sub tool { my $cgi = $self->{'cgi'}; - unless ( $cgi->param('submitted') ) { - $self->tool_step1(); + if ( $cgi->param('submitted') ) { + $self->tool_step2(); + } + elsif ( $cgi->param('greet') ) { + $self->schedule_greets(); } else { - $self->tool_step2(); + $self->tool_step1(); } - } ## The existiance of a 'to_marc' subroutine means the plugin is capable @@ -507,6 +512,20 @@ sub tool_step2 { $self->output_html( $template->output() ); } +sub schedule_greets { + my ($self) = @_; + + my $cgi = $self->{cgi}; + my $count = $cgi->param('count'); + + Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter->new->enqueue( { size => $count } ); + + my $template = $self->get_template( { file => 'greets_scheduled.tt' } ); + $template->param( count => $count ); + + $self->output_html( $template->output() ); +} + ## API methods # If your plugin implements API routes, then the 'api_routes' method needs # to be implemented, returning valid OpenAPI 2.0 paths serialized as a hashref. @@ -645,4 +664,16 @@ sub intranet_catalog_biblio_tab { return @tabs; } +=head3 background_tasks + +Plugin hook used to register new background_job types + +=cut + +sub background_tasks { + return { + greeter => 'Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter' + }; +} + 1; diff --git a/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/Greeter.pm b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/Greeter.pm new file mode 100644 index 0000000..3f7de45 --- /dev/null +++ b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/Greeter.pm @@ -0,0 +1,98 @@ +package Koha::Plugin::Com::ByWaterSolutions::KitchenSink::Greeter; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use base 'Koha::BackgroundJob'; + +=head1 NAME + +KitchenSink::Greeter - Background task for greeting in the logs + +This is a subclass of Koha::BackgroundJob. + +=head1 API + +=head2 Class methods + +=head3 job_type + +Define the job type of this job: greeter + +=cut + +sub job_type { + return 'plugin_kitchensink_greeter'; +} + +=head3 process + +Process the modification. + +=cut + +sub process { + my ( $self, $args ) = @_; + + $self->start; + + my @messages; + my $report = { + total_greets => $self->size, + total_success => 0, + }; + + foreach my $step ( 1 .. $self->size ) { + + warn "Greeting: Hola! ($step)"; + + push @messages, + { + type => 'success', + code => 'greeted', + }; + + $report->{total_success}++; + + $self->step; + } + + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; + + $self->finish($data); +} + +=head3 enqueue + +Enqueue the new job + +=cut + +sub enqueue { + my ( $self, $args ) = @_; + + $self->SUPER::enqueue( + { + job_size => $args->{size} // 5, + job_args => {}, + } + ); +} + +1; diff --git a/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/greets_scheduled.tt b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/greets_scheduled.tt new file mode 100644 index 0000000..9ead901 --- /dev/null +++ b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/greets_scheduled.tt @@ -0,0 +1,15 @@ +[% INCLUDE 'doc-head-open.inc' %] + Koha: Kitchen Sink Plugin: Example background job scheduling +[% INCLUDE 'doc-head-close.inc' %] + + +[% INCLUDE 'header.inc' %] +[% INCLUDE 'cat-search.inc' %] + + + +
+ +

Hello! You scheduled [% count | html %] greetings!

+ +[% INCLUDE 'intranet-bottom.inc' %] \ No newline at end of file diff --git a/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/tool-step1.tt b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/tool-step1.tt index faa7d13..9a0ac38 100644 --- a/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/tool-step1.tt +++ b/Koha/Plugin/Com/ByWaterSolutions/KitchenSink/tool-step1.tt @@ -21,4 +21,16 @@ +
+ +
+ + + +
+ + + +
+ [% INCLUDE 'intranet-bottom.inc' %] \ No newline at end of file