From 7e345bb1e992c9b02fa8e5fa5c9eea18076e0e0c Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@biblibre.com>
Date: Wed, 23 Apr 2014 13:32:47 +0200
Subject: [PATCH] Bug 8992: Interfacing with the Idref webservice
Content-Type: text/plain; charset=utf-8

On the detail page (in the opac), if the biblio comes from the Sudoc,
you must have a link (on the right of the author link) which open a popup with
informations about this author (publications by role).

To test:

1/ Switch on the Idref system preference

2/ Simulate a SUDOC record:
  Fill a 7..$3 field with a ppn (032581270 for example).
  Fill the 009 field with an integer

3/ Go to the opac detail page of this notice.
You should see the IDREF link.
If you click on it, a popup displays a loading icon and after a few
seconds (depending of the productivity of the authority :)), a list of
roles. For each role, a table displays all his corresponding publications.

4/ On the right, you have 2 links: 1 for a koha search for this result
and 1 for a SUDOC link

Signed-off-by: valerie bertrand <valerie.bertrand@univ-lyon3.fr>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
---
 C4/Biblio.pm                                       |    6 +
 .../opac-tmpl/bootstrap/en/modules/opac-detail.tt  |    4 +
 .../opac-tmpl/bootstrap/en/modules/opac-idref.tt   |   74 ++++++++++++++
 opac/opac-detail.pl                                |   13 +++
 opac/opac-idref.pl                                 |  102 ++++++++++++++++++++
 5 files changed, 199 insertions(+), 0 deletions(-)
 create mode 100644 koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-idref.tt
 create mode 100755 opac/opac-idref.pl

diff --git a/C4/Biblio.pm b/C4/Biblio.pm
index 8bdb25f..9f09394 100644
--- a/C4/Biblio.pm
+++ b/C4/Biblio.pm
@@ -1922,9 +1922,14 @@ sub GetMarcAuthors {
         }
 
         # other subfields
+        my $unimarc3;
         for my $authors_subfield (@subfields) {
             next if ( $authors_subfield->[0] eq '9' );
 
+            # unimarc3 contains the $3 of the author for UNIMARC.
+            # For french academic libraries, it's the "ppn", and it's required for idref webservice
+            $unimarc3 = $authors_subfield->[1] if $marcflavour eq 'UNIMARC' and $authors_subfield->[0] =~ /3/;
+
             # don't load unimarc subfields 3, 5
             next if ( $marcflavour eq 'UNIMARC' and ( $authors_subfield->[0] =~ /3|5/ ) );
 
@@ -1960,6 +1965,7 @@ sub GetMarcAuthors {
         push @marcauthors, {
             MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop,
             authoritylink => $subfield9,
+            unimarc3 => $unimarc3
         };
     }
     return \@marcauthors;
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt
index 71b022b..829bc59 100644
--- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-detail.tt
@@ -31,6 +31,7 @@
 [% IF ( OpacStarRatings != 'disable' ) %]
     [% BLOCK cssinclude %]<link rel="stylesheet" type="text/css" href="[% interface %]/[% theme %]/css/jquery.rating.css" />[% END %]
 [% END %]
+[% INCLUDE greybox.inc %]
 </head>
 [% INCLUDE 'bodytag.inc' bodyid='opac-detail' bodyclass='scrollto' %]
 [% INCLUDE 'masthead.inc' %]
@@ -176,6 +177,9 @@
                                     [% IF (MARCAUTHOR.authoritylink) %]
                                         <a class='authlink' href="/cgi-bin/koha/opac-authoritiesdetail.pl?authid=[% MARCAUTHOR.authoritylink %]"><img style="vertical-align:middle" height="15" width="15" src="[% interface %]/[% theme %]/images/filefind.png"></a>
                                     [% END %]
+                                    [% IF IDREF and MARCAUTHOR.unimarc3 %]
+                                        <a href="/cgi-bin/koha/opac-idref.pl?unimarc3=[% MARCAUTHOR.unimarc3 %]" title="IdRef" rel="gb_page_center[600,500]">IdRef</a>
+                                    [% END %]
                                     [% UNLESS ( loop.last ) %]|[% END %]
                                 [% END # /FOREACH MARCAUTHORS %]
                             </div>
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-idref.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-idref.tt
new file mode 100644
index 0000000..0575680
--- /dev/null
+++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-idref.tt
@@ -0,0 +1,74 @@
+[% INCLUDE 'doc-head-open.inc' %]
+<title>[% IF ( LibraryNameTitle ) %][% LibraryNameTitle %][% ELSE %]Koha online[% END %] catalog &rsaquo; Your search IDREF for ppn [% unimarc3 %]</title>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+[% INCLUDE 'doc-head-close.inc' %]
+[% BLOCK cssinclude %]
+  <style type="text/css">
+    ul.ui-tabs-nav li a, ul.ui-tabs-nav li span.a  { padding:0.6em 1em; }
+    #idref h2.role {
+        background-color: #DDDEEE;
+        cursor: pointer;
+    }
+    #idref table {
+        margin: 0;
+    }
+  </style>
+[% END %]
+
+</head>
+<body>
+<div id="idref">
+  [% IF error %]
+    This ppn is not found on the idref service.
+  [% ELSE %]
+    [% FOREACH role IN content %]
+      <div>
+        <h2 class="role" title="Click to expand this role">[% role.role_name %] ([% role.count %])</h2>
+          <div class="docs">
+            <table>
+              <thead>
+                <th>Citation</th>
+                <th>Koha</th>
+                <th>Sudoc</th>
+              </thead>
+              <tbody>
+                [% FOREACH doc IN role.docs %]
+                  <tr>
+                    <td>[% doc.citation %]</td>
+                    <td><a href="/cgi-bin/koha/opac-search.pl?q=ident:[% doc.ppn %]" target="_blank">Koha</a></td>
+                    <td><a href="http://www.sudoc.fr/[% doc.ppn %]" target="_blank">Sudoc</a></td>
+                  </tr>
+                [% END %]
+              </tbody>
+            </table>
+          </div>
+      </div>
+    [% END %]
+  [% END %]
+</div>
+[% BLOCK jsinclude %]
+[% INCLUDE 'datatables.inc' %]
+<script type="text/javascript">
+  $(document).ready(function (){
+      $("table").dataTable($.extend(true, {}, dataTablesDefaults, {
+          'bFilter': false,
+          'aoColumnDefs': [
+              { 'aTargets': [-1, -2], 'bSortable': false }
+          ],
+      }));
+
+      $(".role").click(function(){
+          var docs_node = $(this).parent().find("div.docs");
+          if ( $(docs_node).is(":visible") ) {
+              $(".docs").hide();
+          } else {
+              $(".docs").hide();
+              docs_node.show();
+          }
+      });
+      $(".docs").hide();
+  });
+</script>
+[% END %]
+
+[% INCLUDE 'opac-bottom.inc' is_popup=1%]
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index 9ba8120..778cfdd 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -1091,6 +1091,19 @@ if (my $search_for_title = C4::Context->preference('OPACSearchForTitleIn')){
     $template->param('OPACSearchForTitleIn' => $search_for_title);
 }
 
+#IDREF
+if ( C4::Context->preference("IDREF") ) {
+    # If the record comes from the SUDOC
+    if ( $record->field('009') ) {
+        my $unimarc3 = $record->field("009")->data;
+        if ( $unimarc3 =~ /^\d+$/ ) {
+            $template->param(
+                IDREF => 1,
+            );
+        }
+    }
+}
+
 # We try to select the best default tab to show, according to what
 # the user wants, and what's available for display
 my $opac_serial_default = C4::Context->preference('opacSerialDefaultTab');
diff --git a/opac/opac-idref.pl b/opac/opac-idref.pl
new file mode 100755
index 0000000..7335008
--- /dev/null
+++ b/opac/opac-idref.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/perl
+
+# This file is part of Koha.
+#
+# Copyright (C) 2014 BibLibre
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use CGI;
+use LWP::UserAgent;
+use HTTP::Request::Common;
+use JSON;
+use Encode;
+
+use C4::Auth;
+use C4::Context;
+use C4::Search;
+use C4::Output;
+
+my $cgi = new CGI;
+my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
+    {
+        template_name   => "opac-idref.tt",
+        query           => $cgi,
+        type            => "opac",
+        authnotrequired => ( C4::Context->preference("OpacPublic") ? 1 : 0 ),
+        flagsrequired   => { borrow => 1 },
+    }
+);
+
+my $ua = LWP::UserAgent->new;
+
+my $base = 'http://www.idref.fr/services/biblio/';
+my $unimarc3 = $cgi->param('unimarc3');
+
+my $request = HTTP::Request->new(
+    'GET',
+    $base . $unimarc3 . ".json",
+);
+$request->protocol('HTTP/1.1');
+my $response = $ua->request($request);
+if ( not $response->is_success) {
+    $template->param(error => $base.$unimarc3.'.json');
+    output_html_with_http_headers $cgi, $cookie, $template->output;
+    exit;
+}
+
+my $content = Encode::decode("utf8", $response->content);
+my $json = from_json( $content );
+my $r;
+my $role_name;
+my @unimarc3;
+my @results = ref $json->{sudoc}{result} eq "ARRAY"
+            ? @{ $json->{sudoc}{result} }
+            : ($json->{sudoc}{result});
+
+for my $role_node ( @results ) {
+    while ( my ( $k, $v ) = each %$role_node ) {
+        next unless $k eq "role";
+        my $role_name;
+        my $count = 0;
+        my $role_data = {};
+        my @nodes = ref $v eq "ARRAY"
+                    ? @$v
+                    : ($v);
+        for my $node ( @nodes ) {
+            while ( ( $k, $v ) = each %$node ) {
+                if ( $k eq 'roleName' ) {
+                    $role_name = $v;
+                    $role_data->{role_name} = $role_name;
+                }
+                elsif ( $k eq 'count' ) {
+                    $role_data->{count} = $v;
+                }
+                elsif ( $k eq 'doc' ) {
+                    push @{ $role_data->{docs} }, $v;
+                }
+            }
+        }
+        push @$r, $role_data;
+    }
+}
+
+$template->param(
+    content => $r,
+    unimarc3 => $unimarc3,
+);
+
+output_html_with_http_headers $cgi, $cookie, $template->output;
-- 
1.7.7.6