b75d36c437e9c9b3005dfd38e752d00645366d7c
[srvgit] / Koha / Template / Plugin / KohaPlugins.pm
1 package Koha::Template::Plugin::KohaPlugins;
2
3 # This file is part of Koha.
4 #
5 # Copyright ByWater Solutions 2018
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 base qw( Template::Plugin );
23
24 use Try::Tiny;
25
26 use Koha::Plugins;
27
28 =head1 NAME
29
30 Koha::Template::Plugin::KohaPlugins - A module for adding hooks into Koha for plugins
31
32 =head1 DESCRIPTION
33
34 This plugin contains functions related to adding plugin hooks into various parts
35 of Koha.
36
37 To use, include the line '[% USE KohaPlugins %]' at the top of the template
38 to enable the plugin
39
40 =head2 Methods
41
42 =head3 get_plugins_opac_head
43
44 [% KohaPlugins.get_plugins_opac_head %]
45
46 This method collects the output of all plugins with an opac_head method
47 to output to the head section of opac pages.
48
49 =cut
50
51 sub get_plugins_opac_head {
52     return q{} unless C4::Context->config("enable_plugins");
53
54     my $p = Koha::Plugins->new();
55
56     return q{} unless $p;
57
58     my @plugins = $p->GetPlugins(
59         {
60             method => 'opac_head',
61         }
62     );
63
64     my @data = map { $_->opac_head || q{} } @plugins;
65
66     return join( "\n", @data );
67 }
68
69 =head3 get_plugins_opac_js
70
71 [% KohaPlugins.get_plugins_opac_js %]
72
73 This method collects the output of all plugins with an opac_js method
74 to output to the javascript section of at the bottom of opac pages.
75
76 =cut
77
78 sub get_plugins_opac_js {
79     return q{} unless C4::Context->config("enable_plugins");
80
81     my $p = Koha::Plugins->new();
82
83     return q{} unless $p;
84
85     my @plugins = $p->GetPlugins(
86         {
87             method => 'opac_js',
88         }
89     );
90
91     my @data = map { $_->opac_js || q{} } @plugins;
92
93     return join( "\n", @data );
94 }
95
96 =head3 get_plugins_intranet_head
97
98 [% KohaPlugins.get_plugins_intranet_head %]
99
100 This method collects the output of all plugins with an intranet_head method
101 to output to the head section of intranet pages.
102
103 =cut
104
105 sub get_plugins_intranet_head {
106     return q{} unless C4::Context->config("enable_plugins");
107
108     my $p = Koha::Plugins->new();
109
110     return q{} unless $p;
111
112     my @plugins = $p->GetPlugins(
113         {
114             method => 'intranet_head',
115         }
116     );
117
118     my @data = map { $_->intranet_head || q{} } @plugins;
119
120     return join( "\n", @data );
121 }
122
123 =head3 get_plugins_intranet_js
124
125 [% KohaPlugins.get_plugins_intranet_js %]
126
127 This method collects the output of all plugins with an intranet_js method
128 to output to the javascript section of at the bottom of intranet pages.
129
130 =cut
131
132 sub get_plugins_intranet_js {
133     return q{} unless C4::Context->config("enable_plugins");
134
135     my $p = Koha::Plugins->new();
136
137     return q{} unless $p;
138
139     my @plugins = $p->GetPlugins(
140         {
141             method => 'intranet_js',
142         }
143     );
144
145     my @data = map { $_->intranet_js || q{} } @plugins;
146
147     return join( "\n", @data );
148 }
149
150 =head3 get_plugins_intranet_catalog_biblio_tab
151
152   [% SET plugins_intranet_catalog_biblio_tabs = KohaPlugins.get_plugins_intranet_catalog_biblio_tab %]
153   [% FOREACH plugins_intranet_catalog_biblio_tab IN plugins_intranet_catalog_biblio_tabs %]
154     <li><a href="#[% plugins_intranet_catalog_biblio_tab.id | uri %]">[% plugins_intranet_catalog_biblio_tab.title | html %]</a></li>
155   [% END %]
156
157 This method collects the output of all plugins with a intranet_catalog_biblio_tab
158 method to output to the list of extra cataloguing tabs on intranet pages.
159
160 =cut
161
162 sub get_plugins_intranet_catalog_biblio_tab {
163     my ( $self, $params ) = @_;
164     my $tabs = [];
165
166     return $tabs unless C4::Context->config("enable_plugins");
167
168     my $p = Koha::Plugins->new();
169     return $tabs unless $p;
170
171     my @plugins = $p->GetPlugins(
172         {
173             method => 'intranet_catalog_biblio_tab',
174         }
175     );
176
177     foreach my $plugin (@plugins) {
178         try {
179             my @newtabs = $plugin->intranet_catalog_biblio_tab($params);
180             foreach my $newtab (@newtabs) {
181                 # Add a unique HTML id
182                 my $html_id = 'tab-'. $plugin->{class} . '-' . $newtab->title;
183                 # Using characters except ASCII letters, digits, '_', '-' and '.' may cause compatibility problems
184                 $html_id =~ s/[^0-9A-Za-z]+/-/g;
185                 $newtab->id($html_id);
186             }
187             push @$tabs, @newtabs;
188         }
189         catch {
190             warn "Error calling 'intranet_catalog_biblio_tab' on the " . $plugin->{class} . "plugin ($_)";
191         };
192     }
193
194     return $tabs;
195 }
196
197 1;