From 47bd3bfaaa3c308b7d7fd92edf24cf20015231d3 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 13 Jul 2021 18:18:34 +0000 Subject: [PATCH] Bug 28709: Refactor Koha::Biblio->custom_cover_image_url to allow passing parameters Currently the search result template receive a Koha::Biblio object and call the method. The searchResults routine already has the isbn, issn, and normalized_isbn calculated. Rather than fetching the object and passing to the template we can pass the parameters, generate the url, and pass only the URL to the template To test: 1 - Set the system preferences: CustomCoverImages : Display OPACCustomCoverImages: Display CustomCoverImagesURL: https://images-na.ssl-images-amazon.com/images/P/{normalized_isbn}.01.LZZZZZZZ.jpg 2 - Search on staff and opac for a term that returns titles with covers, e.g. 'shuffle' 3 - Confirm the covers show 4 - Apply patch 5 - Restart all 6 - Confirm covers still show 7 - prove -v t/db_dependent/Koha/Biblios.t --- C4/Search.pm | 11 ++++- Koha/Biblio.pm | 12 +++--- .../prog/en/modules/catalogue/results.tt | 2 +- .../bootstrap/en/modules/opac-opensearch.tt | 2 +- .../bootstrap/en/modules/opac-results.tt | 2 +- t/db_dependent/Koha/Biblios.t | 41 ++++++++++++++++--- 6 files changed, 56 insertions(+), 14 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 1ca987add2..580801a6cb 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -2069,7 +2069,16 @@ sub searchResults { $oldbiblio->{'alternateholdings_count'} = $alternateholdingscount; } - $oldbiblio->{biblio_object} = Koha::Biblios->find( $oldbiblio->{biblionumber} ); + if ( + ( C4::Context->preference('CustomCoverImages') || C4::Context->preference('OPACCustomCoverIMages') ) && C4::Context->preference('CustomCoverImagesURL') + ){ + $oldbiblio->{custom_cover_image_url} = Koha::Biblio->custom_cover_image_url({ + isbn => $oldbiblio->{isbn}, + normalized_isbn => $oldbiblio->{normalized_isbn}, + issn => $oldbiblio->{issn}, + record => $marcrecord + }); + } push( @newresults, $oldbiblio ); } diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 26ee7abd96..477dc7e317 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -754,20 +754,22 @@ It is built regaring the value of the system preference CustomCoverImagesURL =cut sub custom_cover_image_url { - my ( $self ) = @_; + my ( $self, $params ) = @_; + return unless (ref $self || $params); + my $url = C4::Context->preference('CustomCoverImagesURL'); if ( $url =~ m|{isbn}| ) { - my $isbn = $self->biblioitem->isbn; + my $isbn = $params->{isbn} // $self->biblioitem->isbn; return unless $isbn; $url =~ s|{isbn}|$isbn|g; } if ( $url =~ m|{normalized_isbn}| ) { - my $normalized_isbn = C4::Koha::GetNormalizedISBN($self->biblioitem->isbn); + my $normalized_isbn = $params->{normalized_isbn} // C4::Koha::GetNormalizedISBN($self->biblioitem->isbn); return unless $normalized_isbn; $url =~ s|{normalized_isbn}|$normalized_isbn|g; } if ( $url =~ m|{issn}| ) { - my $issn = $self->biblioitem->issn; + my $issn = $params->{issn} // $self->biblioitem->issn; return unless $issn; $url =~ s|{issn}|$issn|g; } @@ -776,7 +778,7 @@ sub custom_cover_image_url { if ( $url =~ $re ) { my $field = $+{field}; my $subfield = $+{subfield}; - my $marc_record = $self->metadata->record; + my $marc_record = $params->{record}// $self->metadata->record; my $value = $marc_record->subfield($field, $subfield); return unless $value; $url =~ s|$re|$value|; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt index cbaec622ab..db601e97e9 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt @@ -476,7 +476,7 @@ [% END %] [% IF Koha.Preference('CustomCoverImages') && Koha.Preference('CustomCoverImagesURL') %] - [% SET custom_cover_image_url = SEARCH_RESULT.biblio_object.custom_cover_image_url %] + [% SET custom_cover_image_url = SEARCH_RESULT.custom_cover_image_url %] [% IF custom_cover_image_url %]
diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt index a423f8f5c3..b39cfa610d 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-opensearch.tt @@ -66,7 +66,7 @@ [% IF ( BakerTaylorEnabled ) %][% IF bt_id %]See Baker & Taylor[% END %][% END %] [% IF Koha.Preference('OPACCustomCoverImages') AND Koha.Preference('CustomCoverImagesURL') %] - [% SET custom_cover_image_url = SEARCH_RESULT.biblio_object.custom_cover_image_url %] + [% SET custom_cover_image_url = SEARCH_RESULT.custom_cover_image_url %] [% IF custom_cover_image_url %] Cover image [% END %] diff --git a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt index b00f066f98..a4aca139d8 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt +++ b/koha-tmpl/opac-tmpl/bootstrap/en/modules/opac-results.tt @@ -431,7 +431,7 @@ [% END %] [% IF Koha.Preference('OPACCustomCoverImages') AND Koha.Preference('CustomCoverImagesURL') %] - [% SET custom_cover_image_url = SEARCH_RESULT.biblio_object.custom_cover_image_url %] + [% SET custom_cover_image_url = SEARCH_RESULT.custom_cover_image_url %] [% IF custom_cover_image_url %] [% IF ( OPACURLOpenInNewWindow ) %] Cover image diff --git a/t/db_dependent/Koha/Biblios.t b/t/db_dependent/Koha/Biblios.t index d19e8f3c0d..6830bd27f6 100755 --- a/t/db_dependent/Koha/Biblios.t +++ b/t/db_dependent/Koha/Biblios.t @@ -193,33 +193,64 @@ subtest 'can_be_transferred' => sub { }; subtest 'custom_cover_image_url' => sub { - plan tests => 4; + plan tests => 8; t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{isbn}_{issn}.png' ); my $isbn = '0553573403 | 9780553573404 (pbk.).png'; + my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn); my $issn = 'my_issn'; my $marc_record = MARC::Record->new; my ( $biblionumber, undef ) = C4::Biblio::AddBiblio($marc_record, ''); + my $custom_cover_image_url; + my $biblio = Koha::Biblios->find( $biblionumber ); my $biblioitem = $biblio->biblioitem->set( { isbn => $isbn, issn => $issn }); - is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png" ); + is( $biblio->custom_cover_image_url, "https://my_url/${isbn}_${issn}.png", "URL is correctly generated for isbn and issbn when called as object" ); + $custom_cover_image_url = Koha::Biblio->custom_cover_image_url({ + isbn => $isbn, + issn => $issn, + record => $marc_record, + normalized_isbn => $normalized_isbn + }); + is( $custom_cover_image_url, "https://my_url/${isbn}_${issn}.png", "URL is correctly generated for isbn and issn when values passed as parameters" ); + my $marc_024a = '710347104926'; $marc_record->append_fields( MARC::Field->new( '024', '', '', a => $marc_024a ) ); C4::Biblio::ModBiblio( $marc_record, $biblio->biblionumber ); t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{024$a}.png' ); - is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png" ); + is( $biblio->custom_cover_image_url, "https://my_url/$marc_024a.png", "URL is correctly generated using a custom field when called as object method" ); + $custom_cover_image_url = Koha::Biblio->custom_cover_image_url({ + isbn => $isbn, + issn => $issn, + record => $marc_record, + normalized_isbn => $normalized_isbn + }); + is( $custom_cover_image_url, "https://my_url/$marc_024a.png", "URL is correctly generated using a custom fieldwhen parameters passed" ); t::lib::Mocks::mock_preference( 'CustomCoverImagesURL', 'https://my_url/{normalized_isbn}.png' ); - my $normalized_isbn = C4::Koha::GetNormalizedISBN($isbn); - is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png" ); + is( $biblio->custom_cover_image_url, "https://my_url/$normalized_isbn.png", "URL correctly generated using normalized ISBN when called as object method" ); + $custom_cover_image_url = Koha::Biblio->custom_cover_image_url({ + isbn => $isbn, + issn => $issn, + record => $marc_record, + normalized_isbn => $normalized_isbn + }); + is( $custom_cover_image_url, "https://my_url/$normalized_isbn.png", "URL correctly generated using normalized ISBN when passed parameters" ); $biblio->biblioitem->isbn('')->store; is( $biblio->custom_cover_image_url, undef, "Don't generate the url if the biblio does not have the value needed to generate it" ); + $custom_cover_image_url = Koha::Biblio->custom_cover_image_url({ + isbn => $isbn, + issn => $issn, + record => $marc_record, + normalized_isbn => '' + }); + is( $custom_cover_image_url, undef, "Don't generate the url if the value needed to generate it is not passed" ); }; -- 2.20.1