From 1bc723b33c1b3cb6baa67c277fe3ce64779fede1 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 4 Aug 2020 13:27:06 +0100 Subject: [PATCH] Bug 24786: (follow-up) Cache the register_name in the session This leads to one DB hit less per page load in the staff client when cash registers are enabled. Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Katrin Fischer Signed-off-by: Jonathan Druart --- C4/Auth.pm | 16 ++++++++------ C4/Context.pm | 21 ++++++++++--------- Koha/Template/Plugin/Registers.pm | 5 +++-- circ/set-library.pl | 6 +++--- .../prog/en/modules/circ/set-library.tt | 4 ++-- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/C4/Auth.pm b/C4/Auth.pm index b818d2398b..b366c9cf14 100644 --- a/C4/Auth.pm +++ b/C4/Auth.pm @@ -38,6 +38,7 @@ use Koha::Checkouts; use Koha::DateUtils qw(dt_from_string); use Koha::Library::Groups; use Koha::Libraries; +use Koha::Cash::Registers; use Koha::Desks; use Koha::Patrons; use Koha::Patron::Consents; @@ -868,7 +869,7 @@ sub checkauth { $session->param('branchname'), $session->param('flags'), $session->param('emailaddress'), $session->param('shibboleth'), $session->param('desk_id'), $session->param('desk_name'), - $session->param('register_id') + $session->param('register_id'), $session->param('register_name') ); C4::Context::set_shelves_userenv( 'bar', $session->param('barshelves') ); C4::Context::set_shelves_userenv( 'pub', $session->param('pubshelves') ); @@ -1091,7 +1092,7 @@ sub checkauth { } my ( $borrowernumber, $firstname, $surname, $userflags, $branchcode, $branchname, $emailaddress, $desk_id, - $desk_name, $register_id ); + $desk_name, $register_id, $register_name ); if ( $return == 1 ) { my $select = " @@ -1142,6 +1143,8 @@ sub checkauth { } if ( $query->param('register_id') ) { $register_id = $query->param('register_id'); + my $register = Koha::Cash::Registers->find($register_id); + $register_name = $register ? $register->name : ''; } my $branches = { map { $_->branchcode => $_->unblessed } Koha::Libraries->search }; if ( $type ne 'opac' and C4::Context->boolean_preference('AutoLocation') ) { @@ -1188,6 +1191,7 @@ sub checkauth { $session->param( 'interface', $type); $session->param( 'shibboleth', $shibSuccess ); $session->param( 'register_id', $register_id ); + $session->param( 'register_name', $register_name ); $debug and printf STDERR "AUTH_4: (%s)\t%s %s - %s\n", map { $session->param($_) } qw(cardnumber firstname surname branch); } $session->param('cas_ticket', $cas_ticket) if $cas_ticket; @@ -1198,7 +1202,7 @@ sub checkauth { $session->param('branchname'), $session->param('flags'), $session->param('emailaddress'), $session->param('shibboleth'), $session->param('desk_id'), $session->param('desk_name'), - $session->param('register_id') + $session->param('register_id'), $session->param('register_name') ); } @@ -1478,7 +1482,7 @@ sub check_api_auth { $session->param('branchname'), $session->param('flags'), $session->param('emailaddress'), $session->param('shibboleth'), $session->param('desk_id'), $session->param('desk_name'), - $session->param('register_id') + $session->param('register_id'), $session->param('register_name') ); my $ip = $session->param('ip'); @@ -1642,7 +1646,7 @@ sub check_api_auth { $session->param('branchname'), $session->param('flags'), $session->param('emailaddress'), $session->param('shibboleth'), $session->param('desk_id'), $session->param('desk_name'), - $session->param('register_id') + $session->param('register_id'), $session->param('register_name') ); return ( "ok", $cookie, $sessionID ); } else { @@ -1732,7 +1736,7 @@ sub check_cookie_auth { $session->param('branchname'), $session->param('flags'), $session->param('emailaddress'), $session->param('shibboleth'), $session->param('desk_id'), $session->param('desk_name'), - $session->param('register_id') + $session->param('register_id'), $session->param('register_name') ); my $ip = $session->param('ip'); diff --git a/C4/Context.pm b/C4/Context.pm index 7ce0516f9a..27c0e3ef8d 100644 --- a/C4/Context.pm +++ b/C4/Context.pm @@ -806,7 +806,7 @@ sub userenv { $userbranch, $branchname, $userflags, $emailaddress, $shibboleth $desk_id, $desk_name, - $register_id); + $register_id, $register_name); Establish a hash of user environment variables. @@ -817,7 +817,7 @@ set_userenv is called in Auth.pm #' sub set_userenv { shift @_; - my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $shibboleth, $desk_id, $desk_name, $register_id)= + my ($usernum, $userid, $usercnum, $userfirstname, $usersurname, $userbranch, $branchname, $userflags, $emailaddress, $shibboleth, $desk_id, $desk_name, $register_id, $register_name)= map { Encode::is_utf8( $_ ) ? $_ : Encode::decode('UTF-8', $_) } # CGI::Session doesn't handle utf-8, so we decode it here @_; my $var=$context->{"activeuser"} || ''; @@ -829,14 +829,15 @@ sub set_userenv { "surname" => $usersurname, #possibly a law problem - "branch" => $userbranch, - "branchname" => $branchname, - "flags" => $userflags, - "emailaddress" => $emailaddress, - "shibboleth" => $shibboleth, - "desk_id" => $desk_id, - "desk_name" => $desk_name, - "register_id" => $register_id, + "branch" => $userbranch, + "branchname" => $branchname, + "flags" => $userflags, + "emailaddress" => $emailaddress, + "shibboleth" => $shibboleth, + "desk_id" => $desk_id, + "desk_name" => $desk_name, + "register_id" => $register_id, + "register_name" => $register_name }; $context->{userenv}->{$var} = $cell; return $cell; diff --git a/Koha/Template/Plugin/Registers.pm b/Koha/Template/Plugin/Registers.pm index ea38dff1eb..d68c834e0a 100644 --- a/Koha/Template/Plugin/Registers.pm +++ b/Koha/Template/Plugin/Registers.pm @@ -37,8 +37,9 @@ sub session_register_id { sub session_register_name { my ($self) = @_; - my $register = Koha::Cash::Registers->find($self->session_register_id); - return $register ? $register->name : ''; + return C4::Context->userenv + ? C4::Context->userenv->{'register_name'} + : ''; } =head2 diff --git a/circ/set-library.pl b/circ/set-library.pl index 4e7b7be128..b460b15466 100755 --- a/circ/set-library.pl +++ b/circ/set-library.pl @@ -81,14 +81,14 @@ if ( $branch and my $library = Koha::Libraries->find($branch) ) { if ( defined($userenv_register_id) && ( $userenv_register_id ne $register_id ) ) { + my $old_register_name = C4::Context->userenv->{'register_name'} || ''; my $register = Koha::Cash::Registers->find($register_id); - $template->param(LoginRegisterID => $register_id); # update template for new register - $template->param(LoginRegisterName => $register ? $register->name : ''); # update template for new register $session->param( 'register_id', $register_id ); + $session->param( 'register_name', $register ? $register->name : '' ); push @updated, { updated_register => 1, - new_register => $register ? $register->name : '' + old_register => $old_register_name }; } $session->flush(); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt index 49db4c3da7..be0003b93d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/set-library.tt @@ -45,14 +45,14 @@ Updated:
    [% FOREACH update IN updated %] [% IF ( update.updated_branch || update.updated_desk || update.updated_register ) %] - [% IF ( updated.updated.branch ) %] + [% IF ( updated.updated_branch ) %]
  • Library: [% update.old_branch or "?" | html %] ⇒ [% update.new_branch or "?" | html %]
  • [% END %] [% IF ( update.updated_desk ) %]
  • Desk: [% update.old_desk or "?" | html %] ⇒ [% LoginDeskname or "?" | html %]
  • [% END %] [% IF ( updated.updated_register ) %] -
  • Register: [% updated.new_register | html %]
  • +
  • Register: [% updated.old_register or "?" | html %] ⇒ [% Registers.session_register_name or "?" | html %]
  • [% END %] [% ELSE %]
  • ERROR - unknown
  • -- 2.39.5