From 1d6fb7f57d9125ce5a4163ccf3da3acd668af909 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 25 Feb 2026 11:09:28 -0100 Subject: [PATCH] Bug 37762: Hook to holds preparation: DRY holdfor and searchToOrder In preparation to implement 'hold for supply ILL' This DRYs the existing functionality, meaning the new similar functionality will only be added once in the Controller::Catalogue IMPORTANT: The following command must only return occurrences in Koha::Controller::Catalogue before this is pushed to main: git grep "\->cookie(\"holdfor\")" Signed-off-by: Jason Robb Signed-off-by: Lisette Scheer Signed-off-by: Lucas Gass --- Koha/Controller/Catalogue.pm | 82 ++++++++++++++++++++++++++++++++++ catalogue/ISBDdetail.pl | 20 +-------- catalogue/MARCdetail.pl | 20 +-------- catalogue/detail.pl | 22 +-------- catalogue/imageviewer.pl | 20 +-------- catalogue/labeledMARCdetail.pl | 20 +-------- catalogue/moredetail.pl | 20 +-------- catalogue/search.pl | 24 ++-------- 8 files changed, 97 insertions(+), 131 deletions(-) create mode 100644 Koha/Controller/Catalogue.pm diff --git a/Koha/Controller/Catalogue.pm b/Koha/Controller/Catalogue.pm new file mode 100644 index 0000000000..a4793b4a55 --- /dev/null +++ b/Koha/Controller/Catalogue.pm @@ -0,0 +1,82 @@ +package Koha::Controller::Catalogue; + +use Modern::Perl; + +use C4::Auth qw( get_template_and_user ); +use Koha::Patrons; + +=head1 NAME + +Koha::Controller::Catalogue - Koha Controller + +=head1 API + +=head2 Class Methods + +=cut + +=head3 init + + my ( $template, $loggedinuser, $cookie, $flags ) = Koha::Controller::Catalogue->init( $args ); + +Initializes the catalogue controller by generating the template, authenticating the user, and populating the template with standard search-related variables. + +Takes a hashref C<$args> containing the CGI C, C, C, and C. + +Returns a list containing the C<$template> object, the C<$loggedinuser> ID, the session C<$cookie>, and the user's C<$flags>. + +=cut + +sub init { + my ( $class, $args ) = @_; + + my $query = $args->{query}; + + my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( + { + template_name => $args->{template_name}, + query => $query, + type => $args->{type}, + flagsrequired => $args->{flagsrequired}, + } + ); + + _prep_searchto_template_params( $template, $query ); + + return ( $template, $loggedinuser, $cookie, $flags ); +} + +=head3 _prep_searchto_template_params + +This prepares the common 'search to' functionality params e.g.: +- 'Search to hold' +- 'Search to order' +- etc + +=cut + +sub _prep_searchto_template_params { + my ( $template, $query ) = @_; + + if ( $query->cookie("holdfor") ) { + my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") ); + if ($holdfor_patron) { + $template->param( + holdfor => $query->cookie("holdfor"), + holdfor_patron => $holdfor_patron, + ); + } + } + + if ( $query->cookie("searchToOrder") ) { + my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") ); + $template->param( + searchtoorder_basketno => $basketno, + searchtoorder_vendorid => $vendorid + ); + } + + return 1; +} + +1; diff --git a/catalogue/ISBDdetail.pl b/catalogue/ISBDdetail.pl index 82fa327c64..1596af5c88 100755 --- a/catalogue/ISBDdetail.pl +++ b/catalogue/ISBDdetail.pl @@ -36,7 +36,6 @@ This script needs a biblionumber as parameter use Modern::Perl; use HTML::Entities; -use C4::Auth qw( get_template_and_user ); use C4::Context; use C4::Output qw( output_html_with_http_headers ); use CGI qw ( -utf8 ); @@ -45,6 +44,7 @@ use C4::Serials qw( CountSubscriptionFromBiblionumber GetSubscription GetSubscri use C4::Search qw( z3950_search_args enabled_staff_search_views ); use Koha::Biblios; +use Koha::Controller::Catalogue; use Koha::Patrons; use Koha::RecordProcessor; use Koha::Virtualshelves; @@ -56,7 +56,7 @@ my $biblionumber = $query->param('biblionumber'); $biblionumber = HTML::Entities::encode($biblionumber); # open template -my ( $template, $loggedinuser, $cookie ) = get_template_and_user( +my ( $template, $loggedinuser, $cookie ) = Koha::Controller::Catalogue->init( { template_name => "catalogue/ISBDdetail.tt", query => $query, @@ -98,22 +98,6 @@ my $res = GetISBDView( } ); -if ( $query->cookie("holdfor") ) { - my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") ); - $template->param( - holdfor => $query->cookie("holdfor"), - holdfor_patron => $holdfor_patron, - ); -} - -if ( $query->cookie("searchToOrder") ) { - my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") ); - $template->param( - searchtoorder_basketno => $basketno, - searchtoorder_vendorid => $vendorid - ); -} - # count of item linked with biblio my $itemcount = $biblio->items->count; $template->param( count => $itemcount ); diff --git a/catalogue/MARCdetail.pl b/catalogue/MARCdetail.pl index 72cf4807c4..be9e173006 100755 --- a/catalogue/MARCdetail.pl +++ b/catalogue/MARCdetail.pl @@ -47,7 +47,6 @@ use Modern::Perl; use CGI qw ( -utf8 ); use HTML::Entities; -use C4::Auth qw( get_template_and_user ); use C4::Context; use C4::Output qw( output_html_with_http_headers ); use C4::Koha; @@ -63,6 +62,7 @@ use C4::Search qw( z3950_search_args enabled_staff_search_views ); use Koha::Biblios; use Koha::BiblioFrameworks; +use Koha::Controller::Catalogue; use Koha::Patrons; use Koha::DateUtils qw( output_pref ); use Koha::Virtualshelves; @@ -78,7 +78,7 @@ my $popup = $query->param('popup'); # if set to 1, then don't insert my $subscriptionid = $query->param('subscriptionid'); # open template -my ( $template, $loggedinuser, $cookie ) = get_template_and_user( +my ( $template, $loggedinuser, $cookie ) = Koha::Controller::Catalogue->init( { template_name => "catalogue/MARCdetail.tt", query => $query, @@ -104,22 +104,6 @@ if ( !$record ) { my $tagslib = &GetMarcStructure( 1, $frameworkcode ); my $biblio = GetBiblioData($biblionumber); -if ( $query->cookie("holdfor") ) { - my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") ); - $template->param( - holdfor => $query->cookie("holdfor"), - holdfor_patron => $holdfor_patron, - ); -} - -if ( $query->cookie("searchToOrder") ) { - my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") ); - $template->param( - searchtoorder_basketno => $basketno, - searchtoorder_vendorid => $vendorid - ); -} - $template->param( ocoins => $biblio_object->get_coins ); #count of item linked diff --git a/catalogue/detail.pl b/catalogue/detail.pl index d5549762d6..2a6e1ebb43 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -19,7 +19,6 @@ use Modern::Perl; use CGI qw ( -utf8 ); use HTML::Entities; -use C4::Auth qw( get_template_and_user ); use C4::Context; use C4::Koha qw( GetAuthorisedValues @@ -47,6 +46,7 @@ use Koha::AuthorisedValues; use Koha::Biblios; use Koha::Biblio::ItemGroup::Items; use Koha::Biblio::ItemGroups; +use Koha::Controller::Catalogue; use Koha::CoverImages; use Koha::DateUtils; use Koha::Database::DataInconsistency; @@ -66,7 +66,7 @@ my $query = CGI->new(); my $analyze = $query->param('analyze'); -my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user( +my ( $template, $borrowernumber, $cookie, $flags ) = Koha::Controller::Catalogue->init( { template_name => 'catalogue/detail.tt', query => $query, @@ -152,24 +152,6 @@ if ( $op eq 'set_item_group' ) { } } -if ( $query->cookie("holdfor") ) { - my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") ); - if ($holdfor_patron) { - $template->param( - holdfor => $query->cookie("holdfor"), - holdfor_patron => $holdfor_patron, - ); - } -} - -if ( $query->cookie("searchToOrder") ) { - my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") ); - $template->param( - searchtoorder_basketno => $basketno, - searchtoorder_vendorid => $vendorid - ); -} - my $fw = GetFrameworkCode($biblionumber); my $showallitems = $query->param('showallitems'); my $marcflavour = C4::Context->preference("marcflavour"); diff --git a/catalogue/imageviewer.pl b/catalogue/imageviewer.pl index 64e9b22507..3bf605b947 100755 --- a/catalogue/imageviewer.pl +++ b/catalogue/imageviewer.pl @@ -20,16 +20,16 @@ use Modern::Perl; use CGI qw ( -utf8 ); -use C4::Auth qw( get_template_and_user ); use C4::Output qw( output_html_with_http_headers ); use C4::Search qw( enabled_staff_search_views ); use Koha::Biblios; +use Koha::Controller::Catalogue; use Koha::Items; use Koha::Patrons; my $query = CGI->new; -my ( $template, $borrowernumber, $cookie ) = get_template_and_user( +my ( $template, $borrowernumber, $cookie ) = Koha::Controller::Catalogue->init( { template_name => "catalogue/imageviewer.tt", query => $query, @@ -45,22 +45,6 @@ my $imagenumber = $query->param('imagenumber'); my $biblio = Koha::Biblios->find($biblionumber); my $itemcount = $biblio ? $biblio->items->count : 0; -if ( $query->cookie("holdfor") ) { - my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") ); - $template->param( - holdfor => $query->cookie("holdfor"), - holdfor_patron => $holdfor_patron, - ); -} - -if ( $query->cookie("searchToOrder") ) { - my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") ); - $template->param( - searchtoorder_basketno => $basketno, - searchtoorder_vendorid => $vendorid - ); -} - if ( C4::Context->preference("LocalCoverImages") ) { my $images; if ($itemnumber) { diff --git a/catalogue/labeledMARCdetail.pl b/catalogue/labeledMARCdetail.pl index 39f6604742..3d71c3c556 100755 --- a/catalogue/labeledMARCdetail.pl +++ b/catalogue/labeledMARCdetail.pl @@ -20,7 +20,6 @@ use Modern::Perl; use CGI qw ( -utf8 ); use HTML::Entities; -use C4::Auth qw( get_template_and_user ); use C4::Context; use C4::Output qw( output_html_with_http_headers ); use C4::Biblio qw( @@ -33,6 +32,7 @@ use C4::Serials qw( CountSubscriptionFromBiblionumber ); use Koha::Biblios; use Koha::BiblioFrameworks; +use Koha::Controller::Catalogue; use Koha::Patrons; use Koha::Virtualshelves; @@ -44,7 +44,7 @@ my $frameworkcode = $query->param('frameworkcode') // GetFrameworkCode($biblionu my $popup = $query->param('popup'); # if set to 1, then don't insert links, it's just to show the biblio # open template -my ( $template, $loggedinuser, $cookie ) = get_template_and_user( +my ( $template, $loggedinuser, $cookie ) = Koha::Controller::Catalogue->init( { template_name => "catalogue/labeledMARCdetail.tt", query => $query, @@ -69,22 +69,6 @@ my $record = $biblio_object->metadata->record; my $tagslib = GetMarcStructure( 1, $frameworkcode ); my $biblio = GetBiblioData($biblionumber); -if ( $query->cookie("holdfor") ) { - my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") ); - $template->param( - holdfor => $query->cookie("holdfor"), - holdfor_patron => $holdfor_patron, - ); -} - -if ( $query->cookie("searchToOrder") ) { - my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") ); - $template->param( - searchtoorder_basketno => $basketno, - searchtoorder_vendorid => $vendorid - ); -} - #count of item linked my $itemcount = $biblio_object->items->count; $template->param( diff --git a/catalogue/moredetail.pl b/catalogue/moredetail.pl index cdd6c09be0..220c74c9e4 100755 --- a/catalogue/moredetail.pl +++ b/catalogue/moredetail.pl @@ -25,19 +25,19 @@ use HTML::Entities; use C4::Biblio qw( GetBiblioData GetFrameworkCode ); use C4::Acquisition qw( GetOrderFromItemnumber GetBasket GetInvoice ); use C4::Output qw( output_and_exit output_html_with_http_headers ); -use C4::Auth qw( get_template_and_user ); use C4::Serials qw( CountSubscriptionFromBiblionumber ); use C4::Search qw( enabled_staff_search_views z3950_search_args ); use Koha::Acquisition::Booksellers; use Koha::AuthorisedValues; use Koha::Biblios; +use Koha::Controller::Catalogue; use Koha::Items; use Koha::Patrons; my $query = CGI->new; -my ( $template, $loggedinuser, $cookie ) = get_template_and_user( +my ( $template, $loggedinuser, $cookie ) = Koha::Controller::Catalogue->init( { template_name => 'catalogue/moredetail.tt', query => $query, @@ -49,22 +49,6 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( $template->param( updated_exclude_from_local_holds_priority => scalar( $query->param('updated_exclude_from_local_holds_priority') ) ); -if ( $query->cookie("holdfor") ) { - my $holdfor_patron = Koha::Patrons->find( $query->cookie("holdfor") ); - $template->param( - holdfor => $query->cookie("holdfor"), - holdfor_patron => $holdfor_patron, - ); -} - -if ( $query->cookie("searchToOrder") ) { - my ( $basketno, $vendorid ) = split( /\//, $query->cookie("searchToOrder") ); - $template->param( - searchtoorder_basketno => $basketno, - searchtoorder_vendorid => $vendorid - ); -} - # get variables my $biblionumber; my $itemnumber; diff --git a/catalogue/search.pl b/catalogue/search.pl index e4d88e9683..34d1c28d2d 100755 --- a/catalogue/search.pl +++ b/catalogue/search.pl @@ -142,13 +142,13 @@ use Modern::Perl; use C4::Context; use C4::Output qw( output_html_with_http_headers pagination_bar ); use C4::Circulation qw( barcodedecode ); -use C4::Auth qw( get_template_and_user ); use C4::Search qw( searchResults enabled_staff_search_views z3950_search_args new_record_from_zebra ); use C4::Languages qw( getlanguage getLanguages ); use C4::Koha qw( getitemtypeimagelocation GetAuthorisedValues ); use URI::Escape; use POSIX qw(ceil floor); +use Koha::Controller::Catalogue; use Koha::ItemTypes; use Koha::Library::Groups; use Koha::Patrons; @@ -189,7 +189,7 @@ if ( !$cgi->param('edit_search') } # load the template -my ( $template, $borrowernumber, $cookie ) = get_template_and_user( +my ( $template, $borrowernumber, $cookie ) = Koha::Controller::Catalogue->init( { template_name => $template_name, query => $cgi, @@ -207,19 +207,9 @@ if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) { $template->param( 'UNIMARC' => 1 ); } -if ( $cgi->cookie("holdfor") ) { - my $holdfor_patron = Koha::Patrons->find( $cgi->cookie("holdfor") ); - if ($holdfor_patron) { # may have been deleted in the meanwhile - $template->param( - holdfor => $cgi->cookie("holdfor"), - holdfor_patron => $holdfor_patron, - ); - } -} - if ( $cgi->cookie("holdforclub") ) { my $holdfor_club = Koha::Clubs->find( $cgi->cookie("holdforclub") ); - if ($holdfor_club) { # May have been deleted in the meanwhile + if ($holdfor_club) { # May have been deleted in the meanwhile $template->param( holdforclub => $cgi->cookie("holdforclub"), holdforclub_name => $holdfor_club->name, @@ -227,14 +217,6 @@ if ( $cgi->cookie("holdforclub") ) { } } -if ( $cgi->cookie("searchToOrder") ) { - my ( $basketno, $vendorid ) = split( /\//, $cgi->cookie("searchToOrder") ); - $template->param( - searchtoorder_basketno => $basketno, - searchtoorder_vendorid => $vendorid - ); -} - # get biblionumbers stored in the cart my @cart_list;