Bug 29407: Make the pickup locations dropdown JS reusable
[koha.git] / plugins / plugins-home.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 Kyle M Hall <kyle.m.hall@gmail.com>
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 CGI qw ( -utf8 );
23
24 use JSON qw( from_json );
25 use LWP::Simple qw( get );
26
27 use Koha::Plugins;
28 use C4::Auth qw( get_template_and_user );
29 use C4::Output qw( output_html_with_http_headers );
30 use C4::Context;
31
32 my $plugins_enabled = C4::Context->config("enable_plugins");
33
34 my $input  = CGI->new;
35 my $method = $input->param('method');
36 my $plugin_search = $input->param('plugin-search');
37
38 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
39     {   template_name => ($plugins_enabled) ? "plugins/plugins-home.tt" : "plugins/plugins-disabled.tt",
40         query         => $input,
41         type          => "intranet",
42         flagsrequired   => { plugins => '*' },
43     }
44 );
45
46 if ($plugins_enabled) {
47
48     $template->param(
49         koha_version => C4::Context->preference("Version"),
50         method       => $method,
51     );
52
53     my @plugins = Koha::Plugins->new()->GetPlugins(
54         {
55             method => $method,
56             all    => 1,
57             errors => 1
58         }
59     );
60
61     $template->param( plugins => \@plugins, );
62
63     $template->param( can_search => C4::Context->config('plugin_repos') ? 1 : 0 );
64     my @results;
65     if ($plugin_search) {
66         my $repos = C4::Context->config('plugin_repos');
67
68         # Fix data structure if only one repo defined
69         if ( ref($repos->{repo}) eq 'HASH' ) {
70             $repos = { repo => [ $repos->{repo} ] };
71         }
72
73         foreach my $r ( @{ $repos->{repo} } ) {
74             if ( $r->{service} eq 'github' ) {
75                 my $url = "https://api.github.com/search/repositories?q=$plugin_search+user:$r->{org_name}+in:name,description";
76                 my $response = from_json( get($url) );
77                 foreach my $result ( @{ $response->{items} } ) {
78                     next unless $result->{name} =~ /^koha-plugin-/;
79                     my $releases = $result->{url} . "/releases/latest";
80                     my $release = from_json( get($releases) );
81                     for my $asset ( @{$release->{assets}} ) {
82                         if ($asset->{browser_download_url} =~ m/\.kpz$/) {
83                             $result->{install_name} = $asset->{name};
84                             $result->{install_url} = $asset->{browser_download_url};
85                         }
86                     }
87                     push( @results, { repo => $r, result => $result } );
88                 }
89             }
90             elsif ( $r->{service} eq 'gitlab' ) {
91                 my $org_name = $r->{org_name};
92                 my $url = "https://gitlab.com/api/v4/groups/$org_name/projects?with_issues_enabled=no\&with_merge_requests_enabled=no\&with_shared=no\&include_subgroups=yes\&search=koha-plugin+$plugin_search";
93                 my $response = from_json( get($url) );
94                 foreach my $result ( @{ $response } ) {
95                     next unless $result->{name} =~ /^koha-plugin-/;
96                     my $project_id   = $result->{id};
97                     my $description  = $result->{description} // '';
98                     my $web_url      = $result->{web_url};
99                     my $releases_url = "https://gitlab.com/api/v4/projects/$project_id/releases";
100                     my @releases     = @{ from_json( get($releases_url) ) };
101
102                     if ( scalar @releases > 0 ) {
103                         # Pick the first one, the latest release
104                         my $latest = $releases[0];
105                         my $name  = $latest->{name};
106                         my @links = @{$latest->{assets}->{links}};
107                         my $url   = $links[0]->{direct_asset_url};
108                         my @parts = split( '/', $url);
109                         my $filename = $parts[-1];
110                         next unless $url =~ m/\.kpz$/;
111                         my $result = {
112                             description  => $description,
113                             install_name => $filename,
114                             install_url  => $url,
115                             html_url     => $web_url,
116                             name         => $name,
117                         };
118                         push @results, { repo => $r, result => $result };
119                     }
120                 }
121             }
122         }
123
124         $template->param(
125             search_results => \@results,
126             search_term    => $plugin_search,
127         );
128     }
129 }
130
131 output_html_with_http_headers( $input, $cookie, $template->output );