Bug 9978: Replace license header with the correct license (GPLv3+)
[koha-equinox.git] / Koha / Plugins / Base.pm
1 package Koha::Plugins::Base;
2
3 # Copyright 2012 Kyle Hall
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Module::Pluggable require => 1;
23
24 use base qw{Module::Bundled::Files};
25
26 use C4::Context;
27
28 =head1 NAME
29
30 C4::Plugins::Base - Base Module for plugins
31
32 =cut
33
34 sub new {
35     my ( $class, $args ) = @_;
36
37     return unless ( C4::Context->config("enable_plugins") || $args->{'enable_plugins'} );
38
39     $args->{'class'} = $class;
40     $args->{'template'} = Template->new( { ABSOLUTE => 1 } );
41
42     my $self = bless( $args, $class );
43
44     ## Run the installation method if it exists and hasn't been run before
45     if ( $self->can('install') && !$self->retrieve_data('__INSTALLED__') ) {
46         if ( $self->install() ) {
47             $self->store_data( { '__INSTALLED__' => 1 } );
48         } else {
49             warn "Plugin $class failed during installation!";
50         }
51     }
52
53     return $self;
54 }
55
56 =head2 store_data
57
58 store_data allows a plugin to store key value pairs in the database for future use.
59
60 usage: $self->store_data({ param1 => 'param1val', param2 => 'param2value' })
61
62 =cut
63
64 sub store_data {
65     my ( $self, $data ) = @_;
66
67     my $dbh = C4::Context->dbh;
68     my $sql = "REPLACE INTO plugin_data SET plugin_class = ?, plugin_key = ?, plugin_value = ?";
69     my $sth = $dbh->prepare($sql);
70
71     foreach my $key ( keys %$data ) {
72         $sth->execute( $self->{'class'}, $key, $data->{$key} );
73     }
74 }
75
76 =head2 retrieve_data
77
78 retrieve_data allows a plugin to read the values that were previously saved with store_data
79
80 usage: my $value = $self->retrieve_data( $key );
81
82 =cut
83
84 sub retrieve_data {
85     my ( $self, $key ) = @_;
86
87     my $dbh = C4::Context->dbh;
88     my $sql = "SELECT plugin_value FROM plugin_data WHERE plugin_class = ? AND plugin_key = ?";
89     my $sth = $dbh->prepare($sql);
90     $sth->execute( $self->{'class'}, $key );
91     my $row = $sth->fetchrow_hashref();
92
93     return $row->{'plugin_value'};
94 }
95
96 =head2 get_template
97
98 get_template returns a Template object. Eventually this will probably be calling
99 C4:Template, but at the moment, it does not.
100
101 =cut
102
103 sub get_template {
104     my ( $self, $args ) = @_;
105
106     require C4::Auth;
107
108     my ( $template, $loggedinuser, $cookie ) = C4::Auth::get_template_and_user(
109         {   template_name   => $self->mbf_path( $args->{'file'} ),
110             query           => $self->{'cgi'},
111             type            => "intranet",
112             authnotrequired => 1,
113             is_plugin       => 1,
114         }
115     );
116
117     $template->param(
118         CLASS       => $self->{'class'},
119         METHOD      => $self->{'cgi'}->param('method'),
120         PLUGIN_PATH => $self->get_plugin_http_path(),
121     );
122
123     return $template;
124 }
125
126 sub get_metadata {
127     my ( $self, $args ) = @_;
128
129     return $self->{'metadata'};
130 }
131
132 =head2 get_qualified_table_name
133
134 To avoid naming conflict, each plugins tables should use a fully qualified namespace.
135 To avoid hardcoding and make plugins more flexible, this method will return the proper
136 fully qualified table name.
137
138 usage: my $table = $self->get_qualified_table_name( 'myTable' );
139
140 =cut
141
142 sub get_qualified_table_name {
143     my ( $self, $table_name ) = @_;
144
145     return lc( join( '_', split( '::', $self->{'class'} ), $table_name ) );
146 }
147
148 =head2 get_plugin_http_path
149
150 To access a plugin's own resources ( images, js files, css files, etc... )
151 a plugin will need to know what path to use in the template files. This
152 method returns that path.
153
154 usage: my $path = $self->get_plugin_http_path();
155
156 =cut
157
158 sub get_plugin_http_path {
159     my ($self) = @_;
160
161     return "/plugin/" . join( '/', split( '::', $self->{'class'} ) );
162 }
163
164 =head2 go_home
165
166    go_home is a quick redirect to the Koha plugins home page
167
168 =cut
169
170 sub go_home {
171     my ( $self, $params ) = @_;
172
173     print $self->{'cgi'}->redirect("/cgi-bin/koha/plugins/plugins-home.pl");
174 }
175
176 1;
177 __END__
178
179 =head1 AUTHOR
180
181 Kyle M Hall <kyle.m.hall@gmail.com>
182
183 =cut