use Modern::Perl;
-use Module::Pluggable require => 1;
-use Cwd qw(abs_path);
-use List::Util qw(max);
+use Cwd qw( abs_path );
+use List::Util qw( max );
+use Try::Tiny;
use base qw{Module::Bundled::Files};
use C4::Context;
-use C4::Output qw(output_with_http_headers output_html_with_http_headers);
+use C4::Output qw( output_with_http_headers );
+
+use Koha::Exceptions::Plugin;
+use Koha::Cache::Memory::Lite;
=head1 NAME
## Run the installation method if it exists and hasn't been run before
if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
- if ( $self->install() ) {
- $self->store_data( { '__INSTALLED__' => 1, '__ENABLED__' => 1 } );
- if ( my $version = $plugin_version ) {
- $self->store_data({ '__INSTALLED_VERSION__' => $version });
+ try {
+ if ( $self->install() ) {
+ $self->store_data( { '__INSTALLED__' => 1, '__ENABLED__' => 1 } );
+ if ( my $version = $plugin_version ) {
+ $self->store_data({ '__INSTALLED_VERSION__' => $version });
+ }
+ } else {
+ warn "Plugin $class failed during installation!";
}
- } else {
- warn "Plugin $class failed during installation!";
}
+ catch {
+ Koha::Exceptions::Plugin::InstallDied->throw( plugin_class => $class );
+ };
} elsif ( $self->can('upgrade') ) {
if ( _version_compare( $plugin_version, $database_version ) == 1 ) {
- if ( $self->upgrade() ) {
- $self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
- } else {
- warn "Plugin $class failed during upgrade!";
+ try {
+ if ( $self->upgrade() ) {
+ $self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
+ } else {
+ warn "Plugin $class failed during upgrade!";
+ }
}
+ catch {
+ Koha::Exceptions::Plugin::UpgradeDied->throw( plugin_class => $class );
+ };
}
} elsif ( $plugin_version ne $database_version ) {
$self->store_data({ '__INSTALLED_VERSION__' => $plugin_version });
}
+ $self->{_bundle_path} = abs_path($self->mbf_dir);
+
return $self;
}
foreach my $key ( keys %$data ) {
$sth->execute( $self->{'class'}, $key, $data->{$key} );
}
+
+ if (exists $data->{__ENABLED__}) {
+ Koha::Cache::Memory::Lite->clear_from_cache(Koha::Plugins->ENABLED_PLUGINS_CACHE_KEY);
+ }
}
=head2 retrieve_data
get_template returns a Template object. Eventually this will probably be calling
C4:Template, but at the moment, it does not.
+The returned template contains 3 variables that can be used in the plugin
+templates:
+
+=over 8
+
+=item B<CLASS>
+
+The name of the plugin class.
+
+=item B<METHOD>
+
+Then name of the plugin method used. For example 'tool' or 'report'.
+
+=item B<PLUGIN_PATH>
+
+The URL path to the plugin. It can be used in templates in order to localize
+ressources like images in html tags, or other templates.
+
+=item B<PLUGIN_DIR>
+
+The absolute pathname to the plugin directory. Necessary to include other
+templates from a template with the [% INCLUDE %] directive.
+
+=back
+
+
=cut
sub get_template {
authnotrequired => 1,
}
);
-
$template->param(
CLASS => $self->{'class'},
METHOD => scalar $self->{'cgi'}->param('method'),
PLUGIN_PATH => $self->get_plugin_http_path(),
+ PLUGIN_DIR => $self->bundle_path(),
+ LANG => C4::Languages::getlanguage($self->{'cgi'}),
);
return $template;
sub get_metadata {
my ( $self, $args ) = @_;
- return $self->{'metadata'};
+ #FIXME: Why another encoding issue? For metadata containing non latin characters.
+ my $metadata = $self->{metadata};
+ defined($metadata->{$_}) && utf8::decode($metadata->{$_}) for keys %$metadata;
+ return $metadata;
}
=head2 get_qualified_table_name
output_with_http_headers( $self->{cgi}, undef, $data, 'html', $status, $extra_options );
}
+=head2 bundle_path
+
+ my $bundle_path = $self->bundle_path
+
+Returns the directory in which bundled files are.
+
+=cut
+
+sub bundle_path {
+ my ($self) = @_;
+
+ return $self->{_bundle_path};
+}
+
=head2 output
$self->output( $data, $content_type[, $status[, $extra_options]]);
=cut
sub _version_compare {
- my $ver1 = shift || 0;
- my $ver2 = shift || 0;
+ my @args = @_;
+
+ if ( $args[0]->isa('Koha::Plugins::Base') ) {
+ shift @args;
+ }
+
+ my $ver1 = shift @args || 0;
+ my $ver2 = shift @args || 0;
my @v1 = split /[.+:~-]/, $ver1;
my @v2 = split /[.+:~-]/, $ver2;
# 0.0.0 <=> 0.2.1 = -1
push( @v1, 0 ) unless defined( $v1[$i] );
push( @v2, 0 ) unless defined( $v2[$i] );
+
+ # Strip letters before comparing, supresses 'Argument "v1" isn't numeric in int' warning
+ $v1[$i] =~ s/^v//g;
+ $v2[$i] =~ s/^v//g;
+
if ( int( $v1[$i] ) > int( $v2[$i] ) ) {
return 1;
}
return 0;
}
+=head2 is_enabled
+
+Method that returns wether the plugin is enabled or not
+
+$plugin->enable
+
+=cut
+
+sub is_enabled {
+ my ($self) = @_;
+
+ return $self->retrieve_data( '__ENABLED__' );
+}
+
=head2 enable
Method for enabling plugin
my ($self) = @_;
$self->store_data( {'__ENABLED__' => 0} );
+
+ return $self;
}
1;