Bug 17234: Need to separate KEY and FOREIGN KEY checks
[koha.git] / opac / opac-idref.pl
blob43692dda80969a7c0ee64f753ed653be16703f80
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2014 BibLibre
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 CGI;
23 use LWP::UserAgent;
24 use HTTP::Request::Common;
25 use JSON;
26 use Encode;
28 use C4::Auth;
29 use C4::Context;
30 use C4::Search;
31 use C4::Output;
33 my $cgi = new CGI;
34 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36 template_name => "opac-idref.tt",
37 query => $cgi,
38 type => "opac",
39 authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
43 my $ua = LWP::UserAgent->new;
45 my $base = 'http://www.idref.fr/services/biblio/';
46 my $unimarc3 = $cgi->param('unimarc3');
48 my $request = HTTP::Request->new(
49 'GET',
50 $base . $unimarc3 . ".json",
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;
60 my $content = Encode::decode("utf8", $response->content);
61 my $json = from_json( $content );
62 my $r;
63 my $role_name;
64 my @unimarc3;
65 my @results = ref $json->{sudoc}{result} eq "ARRAY"
66 ? @{ $json->{sudoc}{result} }
67 : ($json->{sudoc}{result});
69 for my $role_node ( @results ) {
70 while ( my ( $k, $v ) = each %$role_node ) {
71 next unless $k eq "role";
72 my $role_name;
73 my $count = 0;
74 my $role_data = {};
75 my @nodes = ref $v eq "ARRAY"
76 ? @$v
77 : ($v);
78 for my $node ( @nodes ) {
79 while ( ( $k, $v ) = each %$node ) {
80 if ( $k eq 'roleName' ) {
81 $role_name = $v;
82 $role_data->{role_name} = $role_name;
84 elsif ( $k eq 'count' ) {
85 $role_data->{count} = $v;
87 elsif ( $k eq 'doc' ) {
88 push @{ $role_data->{docs} }, $v;
92 push @$r, $role_data;
96 $template->param(
97 content => $r,
98 unimarc3 => $unimarc3,
101 output_html_with_http_headers $cgi, $cookie, $template->output;