Bug 7804 - Add Koha Plugin System - QA Followup 2
[koha.git] / Koha / Plugins / Base.pm
blob2681ee7076f4104619a393832dfeae3418526ae6
1 package Koha::Plugins::Base;
3 # Copyright 2012 Kyle Hall
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
22 use Module::Pluggable require => 1;
24 use base qw{Module::Bundled::Files};
26 use C4::Context;
27 use C4::Auth;
29 BEGIN {
30 push @INC, C4::Context->config("pluginsdir");
33 =head1 NAME
35 C4::Plugins::Base - Base Module for plugins
37 =cut
39 sub new {
40 my ( $class, $args ) = @_;
42 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
44 $args->{'class'} = $class;
45 $args->{'template'} = Template->new( { ABSOLUTE => 1 } );
47 my $self = bless( $args, $class );
49 ## Run the installation method if it exists and hasn't been run before
50 if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
51 if ( $self->install() ) {
52 $self->store_data( { '__INSTALLED__' => 1 } );
53 } else {
54 warn "Plugin $class failed during installation!";
58 return $self;
61 =head2 store_data
63 set_data allows a plugin to store key value pairs in the database for future use.
65 usage: $self->set_data({ param1 => 'param1val', param2 => 'param2value' })
67 =cut
69 sub store_data {
70 my ( $self, $data ) = @_;
72 my $dbh = C4::Context->dbh;
73 my $sql = "REPLACE INTO plugin_data SET plugin_class = ?, plugin_key = ?, plugin_value = ?";
74 my $sth = $dbh->prepare($sql);
76 foreach my $key ( keys %$data ) {
77 $sth->execute( $self->{'class'}, $key, $data->{$key} );
81 =head2 retrieve_data
83 retrieve_data allows a plugin to read the values that were previously saved with store_data
85 usage: my $value = $self->retrieve_data( $key );
87 =cut
89 sub retrieve_data {
90 my ( $self, $key ) = @_;
92 my $dbh = C4::Context->dbh;
93 my $sql = "SELECT plugin_value FROM plugin_data WHERE plugin_class = ? AND plugin_key = ?";
94 my $sth = $dbh->prepare($sql);
95 $sth->execute( $self->{'class'}, $key );
96 my $row = $sth->fetchrow_hashref();
98 return $row->{'plugin_value'};
101 =head2 get_template
103 get_template returns a Template object. Eventually this will probably be calling
104 C4:Template, but at the moment, it does not.
106 =cut
108 sub get_template {
109 my ( $self, $args ) = @_;
111 # my $template =
112 # C4::Templates->new( my $interface = 'intranet', my $filename = $self->mbf_path( $args->{'file'} ), my $tmplbase = '', my $query = $self->{'cgi'} );
114 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
115 { template_name => $self->mbf_path( $args->{'file'} ),
116 query => $self->{'cgi'},
117 type => "intranet",
118 authnotrequired => 1,
119 # flagsrequired => { tools => '*' },
120 is_plugin => 1,
124 $template->param(
125 CLASS => $self->{'class'},
126 METHOD => $self->{'cgi'}->param('method'),
127 PLUGIN_PATH => $self->get_plugin_http_path(),
130 return $template;
133 sub get_metadata {
134 my ( $self, $args ) = @_;
136 return $self->{'metadata'};
139 =head2 get_qualified_table_name
141 To avoid naming conflict, each plugins tables should use a fully qualified namespace.
142 To avoid hardcoding and make plugins more flexible, this method will return the proper
143 fully qualified table name.
145 usage: my $table = $self->get_qualified_table_name( 'myTable' );
147 =cut
149 sub get_qualified_table_name {
150 my ( $self, $table_name ) = @_;
152 return lc( join( '_', split( '::', $self->{'class'} ), $table_name ) );
155 =head2 get_plugin_http_path
157 To access a plugin's own resources ( images, js files, css files, etc... )
158 a plugin will need to know what path to use in the template files. This
159 method returns that path.
161 usage: my $path = $self->get_plugin_http_path();
163 =cut
165 sub get_plugin_http_path {
166 my ($self) = @_;
168 return "/plugin/" . join( '/', split( '::', $self->{'class'} ) );
171 =head2 go_home
173 go_home is a quick redirect to the Koha plugins home page
175 =cut
177 sub go_home {
178 my ( $self, $params ) = @_;
180 print $self->{'cgi'}->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
184 __END__
186 =head1 AUTHOR
188 Kyle M Hall <kyle.m.hall@gmail.com>
190 =cut