Skip to content

Commit fbd68d0

Browse files
cam22857 - Appeler directement le plugin Bourriquet pour éviter les problèmes de ISBN13 et la duplication de la logique (#32)
Ajoute une vérification que le plugin existe et un fallback vers l'ancien fonctionnement avec préférences systèmes pour continuer de supporter toutes les versions.
1 parent 47364c6 commit fbd68d0

File tree

1 file changed

+49
-63
lines changed

1 file changed

+49
-63
lines changed

Koha/Plugin/Carrousel.pm

Lines changed: 49 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ use C4::Reports::Guided;
4242
use Koha::Uploader;
4343
use Koha::DateUtils qw( dt_from_string );
4444
use Data::Dumper;
45-
use URI::Escape;
46-
use constant ENDPOINT_IMAGES => '/images/';
4745

4846
BEGIN {
4947
my $kohaversion = Koha::version;
@@ -56,9 +54,9 @@ BEGIN {
5654
$module->import;
5755
}
5856

59-
our $VERSION = "4.3.4";
57+
our $VERSION = "4.3.5";
6058
our $metadata = {
61-
name => 'Carrousel 4.3.3',
59+
name => 'Carrousel 4.3.5',
6260
author => 'Mehdi Hamidi, Maryse Simard, Brandon Jimenez, Alexis Ripetti, Salman Ali, Hinemoea Viault, Hammat Wele, Salah Eddine Ghedda, Matthias Le Gac, Alexandre Noël, Shi Yao Wang',
6361
description => 'Generates a carrousel from available data sources (lists, reports or collections).',
6462
date_authored => '2016-05-27',
@@ -458,18 +456,12 @@ sub getCarrouselContent {
458456
next if ! $record;
459457
my $title;
460458
my $author;
461-
my $year;
462459
my $marcflavour = C4::Context->preference("marcflavour");
463460
if ($marcflavour eq 'MARC21'){
464461
$title = $record->subfield('245', 'a');
465462
$author = $record->subfield( '100', 'a' );
466463
$author = $record->subfield( '110', 'a' ) unless $author;
467464
$author = $record->subfield( '111', 'a' ) unless $author;
468-
my $date = substr( $record->field('008')->data, 7, 4 );
469-
$year = $date;
470-
$year = $record->subfield("260", "c") unless $year;
471-
$year = $record->subfield("264", "c") unless $year;
472-
$year =~ s/^\s+//;
473465
}elsif ($marcflavour eq 'UNIMARC'){
474466
$title = $record->subfield('200', 'a');
475467
$author = $record->subfield( '200', 'f' );
@@ -480,24 +472,8 @@ sub getCarrouselContent {
480472
if (!$externalUrl) {
481473
$externalUrl = "/cgi-bin/koha/opac-detail.pl?biblionumber=" . $biblionumber;
482474
}
483-
my $tmdb_info = "";
484-
if(substr( $record->leader(), 6, 1 ) eq 'g'){
485-
my @fields = $record->field('024');
486-
my $t = Encode::encode( 'UTF-8', $title ) if Encode::is_utf8($title);
487-
$t =~ s/^\s+|\s+$//g;
488-
$t = uri_escape( $t );
489-
my $title_year = "&title=$t|$biblionumber&year=$year|$biblionumber";
490-
foreach my $field (@fields) {
491-
my $external_id = $field->subfield('a');
492-
my $external_source = $field->subfield('2');
493-
next if(!$external_source || !$external_id);
494-
$external_source .= "_id" if ($external_source);
495-
$tmdb_info.= "&external_id=$external_id|$biblionumber&external_source=$external_source|$biblionumber".$title_year;
496-
}
497-
$tmdb_info.= "&external_id=|$biblionumber&external_source=|$biblionumber".$title_year unless($tmdb_info);
498-
}
499475

500-
my $url = getThumbnailUrl( $biblionumber, $tmdb_info, $record );
476+
my $url = getThumbnailUrl( $biblionumber, $record );
501477
if ( $url ){
502478
my %image = ( url => $url, title => $title, author => $author, biblionumber => $biblionumber, externalUrl => $externalUrl );
503479
push @images, \%image;
@@ -917,22 +893,54 @@ sub retrieveUrlFromBourriquetJson {
917893
}
918894

919895
sub getUrlFromExternalSources {
920-
my $isbn = shift;
921-
my $tmdb_info = shift;
896+
my $record = shift;
922897
my $biblionumber = shift;
923898

899+
# Chercher un isbn pour le biblionumber
900+
my @isbns;
901+
my $marcflavour = C4::Context->preference("marcflavour");
902+
if ( $marcflavour eq 'MARC21' ) {
903+
@isbns = $record->field('020');
904+
} elsif ( $marcflavour eq 'UNIMARC' ) {
905+
@isbns = $record->field('010');
906+
}
907+
908+
my $isbn = "";
909+
foreach my $field ( @isbns ) {
910+
$isbn = GetNormalizedISBN( $field->subfield('a') ) // "";
911+
next if ! $isbn;
912+
}
913+
924914
# les clefs sont les systempreferences du même nom
925915
my $es = {};
926-
my $bourriquet_plugin = Koha::Plugin::Com::Inlibro::Bourriquet->new;
927-
my $bourriquet_pref = $bourriquet_plugin->get_bourriquet_pref || q{};
928-
929-
if ($bourriquet_pref->{BourriquetOpac} && $bourriquet_pref->{BourriquetToken} && $bourriquet_pref->{BourriquetProviders}){
930-
$es->{BourriquetOpac} = {
931-
'priority' => 1,
932-
'retrieval' => \&retrieveUrlFromBourriquetJson,
933-
'url' => $bourriquet_pref->{BourriquetURL}.ENDPOINT_IMAGES."?stdnos=$isbn|$biblionumber&providers=".$bourriquet_pref->{BourriquetProviders}.$tmdb_info,
934-
'authorization' =>'Token '.$bourriquet_pref->{BourriquetToken},
935-
};
916+
917+
# Bourriquet
918+
# Par le plugin Koha::Plugin::Com::Inlibro::Bourriquet
919+
# on appele directement l'api pour profiter de son traitement des ISBNs
920+
eval {
921+
require Koha::Plugin::Com::Inlibro::Bourriquet;
922+
my $bourriquet = Koha::Plugin::Com::Inlibro::Bourriquet->new;
923+
my $bourriquet_pref = $bourriquet->get_bourriquet_pref();
924+
if ( $bourriquet_pref->{BourriquetOpac} ) {
925+
$es->{BourriquetOpac} = {
926+
'priority' => 1,
927+
'retrieval' => \&retrieveUrlFromBourriquetJson,
928+
'url' => C4::Context->preference('OPACBaseURL')."/api/v1/contrib/" . $bourriquet->api_namespace . "/images?"
929+
. "biblionumbers=$biblionumber"
930+
. "&providers=".$bourriquet_pref->{BourriquetProviders}
931+
. ( $bourriquet_pref->{debug_mode} ? "&debug=true" : "" ),
932+
};
933+
}
934+
};
935+
# sinon, par les anciennes préférences systèmes
936+
if ( $@ ) {
937+
if (C4::Context->preference('BourriquetOpac') && C4::Context->preference('BourriquetToken') && C4::Context->preference('BourriquetProviders')) {
938+
$es->{BourriquetOpac} = {
939+
'priority' => 1,
940+
'retrieval' => \&retrieveUrlFromBourriquetJson,
941+
'url' => C4::Context->preference('OPACBaseURL')."/cgi-bin/koha/svc/bourriquet/images/?stdnos=$isbn|$biblionumber&providers=".C4::Context->preference('BourriquetProviders'),
942+
};
943+
}
936944
}
937945

938946
if($isbn){
@@ -969,7 +977,6 @@ sub getUrlFromExternalSources {
969977
$ua->ssl_opts(verify_hostname => 0);
970978
my @orderedProvidersByPriority = sort { $es->{$a}->{priority} <=> $es->{$b}->{priority} } keys %$es;
971979

972-
#warn Data::Dumper::Dumper(@orderedProvidersByPriority);
973980
for my $provider ( @orderedProvidersByPriority ) {
974981
my $url = $es->{$provider}->{url};
975982
my $req = HTTP::Request->new( GET => $url );
@@ -1014,17 +1021,9 @@ sub getUrlFromExternalSources {
10141021
sub getThumbnailUrl
10151022
{
10161023
my $biblionumber = shift;
1017-
my $tmdb_info = shift;
10181024
my $record = shift;
10191025
return if ! $record;
1020-
my $marcflavour = C4::Context->preference("marcflavour");
1021-
my @isbns;
10221026
my $dbh = C4::Context->dbh;
1023-
if ($marcflavour eq 'MARC21' ){
1024-
@isbns = $record->field('020');
1025-
}elsif($marcflavour eq 'UNIMARC'){
1026-
@isbns = $record->field('010');
1027-
}
10281027

10291028
# We look for image localy, if available we return relative path and exit function.
10301029
my $cover_images_table='cover_images';
@@ -1100,21 +1099,8 @@ sub getThumbnailUrl
11001099
}
11011100
}
11021101

1103-
1104-
1105-
1106-
1107-
#If there is not local thumbnail, we look for one on Amazon, Google and Openlibrary in this order and we will exit when a thumbnail is found.
1108-
return getUrlFromExternalSources('', $tmdb_info, $biblionumber) unless( @isbns );
1109-
foreach my $field ( @isbns )
1110-
{
1111-
my $isbn = GetNormalizedISBN( $field->subfield('a') );
1112-
next if ! $isbn;
1113-
1114-
return getUrlFromExternalSources($isbn, $tmdb_info, $biblionumber);
1115-
}
1116-
1117-
return;
1102+
#If there is not local thumbnail, we look for one on Bourriquet, Amazon, Google and Openlibrary in this order and we will exit when a thumbnail is found.
1103+
return getUrlFromExternalSources($record, $biblionumber);
11181104
}
11191105

11201106
sub configure {

0 commit comments

Comments
 (0)