From b5cfe3e20e8a39dd83457ad34e78f5d2df143c70 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Fri, 12 Jan 2024 08:44:12 +0100
Subject: [PATCH] Bug 35782: Replace TT plugin's method Biblio::HoldsCount

We can use biblio.holds.count instead.

The main idea here is to make sure we are passing a Koha::Biblio object
as 'biblio' to all the templates including biblio-view-menu.inc

Test plan:
1. Go to the biblio detail view, click on the different entries in the menu
on the left. Confirm that the "Holds" tab always has the correct number
of Holds display in the parenthesis.

2. Run a search and confirm that the number of holds are still displayed
for each result.

QA:
git grep biblio-view-menu.inc
notice the tt list, open the corresponding perl controllers and confirm
that 'biblio' is passed and that it is a Koha::Biblio object.
The only missing place I found was in viewlog.
Note that we are not removing the TT method yet, we are marking it as
deprecated and also display a warning during the update DB process in
case one of the notice templates is using it.

Signed-off-by: David Nind <david@davidnind.com>
---
 Koha/Template/Plugin/Biblio.pm                |  3 +++
 .../data/mysql/atomicupdate/bug_35782.pl      | 25 +++++++++++++++++++
 .../prog/en/includes/biblio-view-menu.inc     |  4 +--
 .../prog/en/modules/catalogue/results.tt      |  4 +--
 .../prog/en/modules/reserve/request.tt        |  2 +-
 tools/viewlog.pl                              |  4 ++-
 6 files changed, 36 insertions(+), 6 deletions(-)
 create mode 100755 installer/data/mysql/atomicupdate/bug_35782.pl

diff --git a/Koha/Template/Plugin/Biblio.pm b/Koha/Template/Plugin/Biblio.pm
index 79c7284292..d23eb094b6 100644
--- a/Koha/Template/Plugin/Biblio.pm
+++ b/Koha/Template/Plugin/Biblio.pm
@@ -31,9 +31,12 @@ use Koha::Recalls;
 
 use Koha::DateUtils qw(dt_from_string);
 
+# Do not use HoldsCount, it is deprecated and will be removed in a future release.
 sub HoldsCount {
     my ( $self, $biblionumber ) = @_;
 
+    warn "HoldsCount is deprecated, you should use biblio.holds.count instead";
+
     my $holds = Koha::Holds->search( { biblionumber => $biblionumber } );
 
     return $holds->count();
diff --git a/installer/data/mysql/atomicupdate/bug_35782.pl b/installer/data/mysql/atomicupdate/bug_35782.pl
new file mode 100755
index 0000000000..a2d0bcedd9
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_35782.pl
@@ -0,0 +1,25 @@
+use Modern::Perl;
+
+return {
+    bug_number  => "BUG_35872",
+    description => "Biblio.HoldsCount is deprecated",
+    up          => sub {
+        my ($args) = @_;
+        my ( $dbh, $out ) = @$args{qw(dbh out)};
+
+        # Do you stuffs here
+        my ($count) = $dbh->selectrow_array(
+            q{
+            SELECT COUNT(*)
+            FROM letter
+            WHERE content LIKE "%Biblio.HoldsCount%";
+        }
+        );
+
+        if ($count) {
+            say $out "WARNING - Biblio.HoldsCount is used in at least one notice template";
+            say $out "It is deprecated and will be remove in a future version of Koha.";
+            say $out "Replace it with biblio.holds.count instead";
+        }
+    },
+};
diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc
index 7d3aee9462..c7fbc727e8 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc
+++ b/koha-tmpl/intranet-tmpl/prog/en/includes/biblio-view-menu.inc
@@ -1,6 +1,6 @@
 [% USE Koha %]
 [% USE Biblio %]
-[% SET biblio_object_id = object || biblionumber || biblio.biblionumber %]
+[% SET biblio_object_id = biblio.biblionumber %]
 
 <div id="menu">
     <ul>
@@ -66,7 +66,7 @@
         [%- ELSE -%]
         <li>
         [%- END -%]
-            <a href="/cgi-bin/koha/reserve/request.pl?biblionumber=[% biblio_object_id | url  %]">Holds ([% Biblio.HoldsCount( biblio_object_id ) | html %])</a>
+            <a href="/cgi-bin/koha/reserve/request.pl?biblionumber=[% biblio_object_id | url  %]">Holds ([% biblio.holds.count | html %])</a>
         </li>
         [%- END -%]
 
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 b6966286d4..1b5dd336da 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/catalogue/results.tt
@@ -562,11 +562,11 @@
                                                         <span class="noholdstext">No holds allowed</span>
                                                     [% ELSE %]
                                                         [% IF CAN_user_reserveforothers_place_holds %]
-                                                            <a id="reserve_[% SEARCH_RESULT.biblionumber | html %]" href="/cgi-bin/koha/reserve/request.pl?biblionumber=[% SEARCH_RESULT.biblionumber | html %]">Holds ([% Biblio.HoldsCount( SEARCH_RESULT.biblionumber ) | html %])</a>
+                                                            <a id="reserve_[% SEARCH_RESULT.biblionumber | html %]" href="/cgi-bin/koha/reserve/request.pl?biblionumber=[% SEARCH_RESULT.biblionumber | html %]">Holds ([% SEARCH_RESULT.biblio_object.holds.count | html %])</a>
                                                             [% IF ( holdfor ) %] <span class="holdforlink">| <a href="/cgi-bin/koha/reserve/request.pl?biblionumber=[% SEARCH_RESULT.biblionumber | uri %]&amp;findborrower=[% holdfor_patron.cardnumber | uri %]">Place hold for [% INCLUDE 'patron-title.inc' patron => holdfor_patron no_title => 1 no_cardnumber => 1 %]</a></span>[% END %]
                                                             [% IF ( holdforclub ) %] <span class="holdforlink">| <a href="/cgi-bin/koha/reserve/request.pl?biblionumber=[% SEARCH_RESULT.biblionumber | uri %]&amp;club=[% holdforclub | uri %]">Place hold for [% holdforclub_name | html %]</a></span>[% END %]
                                                         [% ELSE %]
-                                                            <span>Holds ([% Biblio.HoldsCount( SEARCH_RESULT.biblionumber ) | html %])</span>
+                                                            <span>Holds ([% SEARCH_RESULT.biblio_object.holds.count | html %])</span>
                                                         [% END %]
                                                     [% END # /IF SEARCH_RESULT.norequests %]
                                                     [% IF Koha.Preference('UseRecalls') and CAN_user_recalls %]
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
index c29439bf01..8d512eede7 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt
@@ -1041,7 +1041,7 @@
 
             [% UNLESS ( patron ) %]
                 [% UNLESS borrowers %]
-                    [% SET hold_count = Biblio.HoldsCount( biblio.biblionumber ) | html %]
+                    [% SET hold_count = biblio.holds.count | html %]
                     [% IF hold_count %]
                         <label for ="always_show_holds">Always show holds</label>
                         [% IF always_show_holds == 'DONT' %]
diff --git a/tools/viewlog.pl b/tools/viewlog.pl
index 9db4e430ed..b6707b712c 100755
--- a/tools/viewlog.pl
+++ b/tools/viewlog.pl
@@ -130,11 +130,13 @@ if ($do_it) {
 
     if ( @modules == 1 && $object_type eq 'biblio' ) {
         # Handle 'Modification log' from cataloguing
-        my @itemnumbers = Koha::Items->search({ biblionumber => $object })->get_column('itemnumber');
+        my $biblio = Koha::Biblios->find( $object );
+        my @itemnumbers = $biblio->items->get_column('itemnumber');
         $search_params{'-or'} = [
             { -and => { object => $object, info => { -like => 'biblio%' }}},
             { -and => { object => \@itemnumbers, info => { -like => 'item%' }}},
         ];
+        $template->param( biblio => $biblio );
     } else {
         $search_params{info} = { -like => '%' . $info . '%' } if $info;
         $search_params{object} = $object if $object;
-- 
2.30.2