Bug 33168: Prevent debhelper from renaming history.txt to changelog
[koha-ffzg.git] / Koha / Plugins / Base.pm
index 20d8b79..8e547fb 100644 (file)
@@ -19,14 +19,17 @@ package Koha::Plugins::Base;
 
 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
 
@@ -49,26 +52,38 @@ sub new {
 
     ## 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;
 }
 
@@ -90,6 +105,10 @@ sub store_data {
     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
@@ -117,6 +136,32 @@ sub 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 {
@@ -135,11 +180,12 @@ 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;
@@ -148,7 +194,10 @@ sub get_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
@@ -210,6 +259,20 @@ sub output_html {
     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]]);
@@ -249,8 +312,14 @@ if ( _version_compare( '2.6.26', '2.6.0' ) == 1 ) {
 =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;
@@ -262,6 +331,11 @@ sub _version_compare {
         # 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;
         }
@@ -272,6 +346,20 @@ sub _version_compare {
     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
@@ -300,6 +388,8 @@ sub disable {
     my ($self) = @_;
 
     $self->store_data( {'__ENABLED__' => 0}  );
+
+    return $self;
 }
 
 1;