Bug 15903 - Remove use of recordpayment in paycollect.pl
[koha.git] / Koha / Plugins / Base.pm
blob4aa8e94a82d5f9960b2595a0fdd1edef2e1b2d15
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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Module::Pluggable require => 1;
23 use Cwd qw(abs_path);
25 use base qw{Module::Bundled::Files};
27 use C4::Context;
29 =head1 NAME
31 C4::Plugins::Base - Base Module for plugins
33 =cut
35 sub new {
36 my ( $class, $args ) = @_;
38 return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
40 $args->{'class'} = $class;
41 $args->{'template'} = Template->new( { ABSOLUTE => 1 } );
43 my $self = bless( $args, $class );
45 ## Run the installation method if it exists and hasn't been run before
46 if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
47 if ( $self->install() ) {
48 $self->store_data( { '__INSTALLED__' => 1 } );
49 } else {
50 warn "Plugin $class failed during installation!";
54 return $self;
57 =head2 store_data
59 store_data allows a plugin to store key value pairs in the database for future use.
61 usage: $self->store_data({ param1 => 'param1val', param2 => 'param2value' })
63 =cut
65 sub store_data {
66 my ( $self, $data ) = @_;
68 my $dbh = C4::Context->dbh;
69 my $sql = "REPLACE INTO plugin_data SET plugin_class = ?, plugin_key = ?, plugin_value = ?";
70 my $sth = $dbh->prepare($sql);
72 foreach my $key ( keys %$data ) {
73 $sth->execute( $self->{'class'}, $key, $data->{$key} );
77 =head2 retrieve_data
79 retrieve_data allows a plugin to read the values that were previously saved with store_data
81 usage: my $value = $self->retrieve_data( $key );
83 =cut
85 sub retrieve_data {
86 my ( $self, $key ) = @_;
88 my $dbh = C4::Context->dbh;
89 my $sql = "SELECT plugin_value FROM plugin_data WHERE plugin_class = ? AND plugin_key = ?";
90 my $sth = $dbh->prepare($sql);
91 $sth->execute( $self->{'class'}, $key );
92 my $row = $sth->fetchrow_hashref();
94 return $row->{'plugin_value'};
97 =head2 get_template
99 get_template returns a Template object. Eventually this will probably be calling
100 C4:Template, but at the moment, it does not.
102 =cut
104 sub get_template {
105 my ( $self, $args ) = @_;
107 require C4::Auth;
109 my ( $template, $loggedinuser, $cookie ) = C4::Auth::get_template_and_user(
110 { template_name => abs_path( $self->mbf_path( $args->{'file'} ) ),
111 query => $self->{'cgi'},
112 type => "intranet",
113 authnotrequired => 1,
114 is_plugin => 1,
118 $template->param(
119 CLASS => $self->{'class'},
120 METHOD => scalar $self->{'cgi'}->param('method'),
121 PLUGIN_PATH => $self->get_plugin_http_path(),
124 return $template;
127 sub get_metadata {
128 my ( $self, $args ) = @_;
130 return $self->{'metadata'};
133 =head2 get_qualified_table_name
135 To avoid naming conflict, each plugins tables should use a fully qualified namespace.
136 To avoid hardcoding and make plugins more flexible, this method will return the proper
137 fully qualified table name.
139 usage: my $table = $self->get_qualified_table_name( 'myTable' );
141 =cut
143 sub get_qualified_table_name {
144 my ( $self, $table_name ) = @_;
146 return lc( join( '_', split( '::', $self->{'class'} ), $table_name ) );
149 =head2 get_plugin_http_path
151 To access a plugin's own resources ( images, js files, css files, etc... )
152 a plugin will need to know what path to use in the template files. This
153 method returns that path.
155 usage: my $path = $self->get_plugin_http_path();
157 =cut
159 sub get_plugin_http_path {
160 my ($self) = @_;
162 return "/plugin/" . join( '/', split( '::', $self->{'class'} ) );
165 =head2 go_home
167 go_home is a quick redirect to the Koha plugins home page
169 =cut
171 sub go_home {
172 my ( $self, $params ) = @_;
174 print $self->{'cgi'}->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
178 __END__
180 =head1 AUTHOR
182 Kyle M Hall <kyle.m.hall@gmail.com>
184 =cut