Bug 17600: Standardize our EXPORT_OK
[koha.git] / opac / opac-idref.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright (C) 2014 BibLibre
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;
23 use LWP::UserAgent;
24 use JSON qw( from_json );
25 use Encode;
26
27 use C4::Auth qw( get_template_and_user );
28 use C4::Context;
29 use C4::Search;
30 use C4::Output qw( output_html_with_http_headers );
31
32 my $cgi = CGI->new;
33 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
34     {
35         template_name   => "opac-idref.tt",
36         query           => $cgi,
37         type            => "opac",
38         authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
39     }
40 );
41
42 my $ua = LWP::UserAgent->new;
43
44 # See http://documentation.abes.fr/aideidrefdeveloppeur/index.html#MicroWebBiblio
45 my $base = 'http://www.idref.fr/services/biblio/';
46 my $unimarc3 = $cgi->param('unimarc3');
47
48 my $request = HTTP::Request->new(
49     'GET',
50     $base . $unimarc3 . ".json",
51 );
52 $request->protocol('HTTP/1.1');
53 my $response = $ua->request($request);
54 if ( not $response->is_success) {
55     $template->param(error => $base.$unimarc3.'.json');
56     output_html_with_http_headers $cgi, $cookie, $template->output;
57     exit;
58 }
59
60 my $content = Encode::decode("utf8", $response->content);
61 my $json = from_json( $content );
62 my $r;
63 my @results = ref $json->{sudoc}{result} eq "ARRAY"
64             ? @{ $json->{sudoc}{result} }
65             : ($json->{sudoc}{result});
66
67 for my $result (@results) {
68     my $role_node = $result->{'role'};
69     my @roles =
70       ref $role_node eq "ARRAY"
71       ? @$role_node
72       : ($role_node);
73     for my $role (@roles) {
74         my @docs = ref $role->{doc} eq "ARRAY"
75             ? @{ $role->{doc} }
76             : $role->{doc};
77         push @$r,
78           {
79             role_name => $role->{roleName},
80             count     => $role->{count},
81             docs      => \@docs,
82           };
83     }
84 }
85
86 $template->param(
87     content => $r,
88     unimarc3 => $unimarc3,
89 );
90
91 output_html_with_http_headers $cgi, $cookie, $template->output;