From d202f32d12813c395134b877c7aaabf57fdbfab3 Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Thu, 18 Jul 2019 19:53:13 +0300 Subject: [PATCH] Bug 23342 - Branch Transfer Limits branch_transfer_limits.pl DBI to Koha::Object, fix variable semantics branch_transfer_limits.pl uses DBI SQL statements in the controller. Replace those with Koha::Objects. change term 'branchcode' to 'fromBranch' to clarify what is the purpose of the branchcode. Prepare for more BranchTransferLimit authorised value limiting types. Sponsored-by: The National Library of Finland --- admin/branch_transfer_limits.pl | 55 +++++++------------ .../modules/admin/branch_transfer_limits.tt | 22 +++++--- 2 files changed, 34 insertions(+), 43 deletions(-) diff --git a/admin/branch_transfer_limits.pl b/admin/branch_transfer_limits.pl index 82a8d214c9..0334950b26 100755 --- a/admin/branch_transfer_limits.pl +++ b/admin/branch_transfer_limits.pl @@ -37,63 +37,48 @@ my ($template, $loggedinuser, $cookie) debug => 1, }); -my $dbh = C4::Context->dbh; -my $branchcode; -if((!defined($input->param('branchcode'))) & C4::Context::mybranch() ne '') +my $fromBranch; +if((!defined($input->param('fromBranch'))) && C4::Context::mybranch() ne '') { - $branchcode = C4::Context::mybranch(); + $fromBranch = Koha::Libraries->find(C4::Context::mybranch()); } else { - $branchcode = $input->param('branchcode'); + $fromBranch = Koha::Libraries->find($input->param('fromBranch')); } # Set the template language for the correct limit type using $limitType my $limitType = C4::Context->preference("BranchTransferLimitsType") || "ccode"; +my @branches = Koha::Libraries->search({}); my @codes; -my @branchcodes; -my $sth; if ( $limitType eq 'ccode' ) { - $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"'); -} elsif ( $limitType eq 'itemtype' ) { - $sth = $dbh->prepare('SELECT itemtype FROM itemtypes'); + @codes = map {$_->authorised_value} Koha::AuthorisedValues->search({category => 'CCODE'}, {columns => ['authorised_value']}); } -$sth->execute(); -while ( my $row = $sth->fetchrow_hashref ) { - push( @codes, $row->{ $limitType } ); -} - -$sth = $dbh->prepare("SELECT branchcode FROM branches"); -$sth->execute(); -while ( my $row = $sth->fetchrow_hashref ) { - push( @branchcodes, $row->{'branchcode'} ); +elsif ( $limitType eq 'itemtype' ) { + @codes = map {$_->itemtype} Koha::ItemTypes->search({}, {columns => ['itemtype']}); } ## If Form Data Passed, Update the Database if ( $input->param('updateLimits') ) { - DeleteBranchTransferLimits($branchcode); - + DeleteBranchTransferLimits($fromBranch->branchcode); foreach my $code ( @codes ) { - foreach my $toBranch ( @branchcodes ) { - my $isSet = not $input->param( $code . "_" . $toBranch); + foreach my $toBranch ( @branches ) { + my $isSet = not $input->param( $code . "_" . $toBranch->branchcode); if ( $isSet ) { - CreateBranchTransferLimit( $toBranch, $branchcode, $code ); + CreateBranchTransferLimit( $toBranch->branchcode, $fromBranch->branchcode, $code ); } } } } -## Build branchcode loop -my @branchcode_loop; -foreach my $branchcode ( @branchcodes ) { - my %row_data; - $row_data{ branchcode } = $branchcode; - push ( @branchcode_loop, \%row_data ); +## Caclulate the selected branch here, this is to avoid calling Branches.all(selected...) in the Template Toolkit since we already need all the branches here. +foreach my $branch ( @branches ) { + $branch->selected(1) if $branch->branchcode eq $fromBranch->branchcode; } -my $branchcount = scalar(@branchcode_loop); +my $branchcount = scalar(@branches); ## Build the default data my @codes_loop; @@ -102,9 +87,9 @@ foreach my $code ( @codes ) { my %row_data; $row_data{ code } = $code; $row_data{ to_branch_loop } = \@to_branch_loop; - foreach my $toBranch ( @branchcodes ) { + foreach my $toBranch ( @branches ) { my %row_data; - my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code ); + my $isChecked = IsBranchTransferAllowed( $toBranch->branchcode, $fromBranch->branchcode, $code ); $row_data{ code } = $code; $row_data{ toBranch } = $toBranch; $row_data{ isChecked } = $isChecked; @@ -118,8 +103,8 @@ foreach my $code ( @codes ) { $template->param( branchcount => $branchcount, codes_loop => \@codes_loop, - branchcode_loop => \@branchcode_loop, - branchcode => $branchcode, + branches => \@branches, + fromBranch => $fromBranch, limitType => $limitType, ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tt index e3cc5bb6ae..0d656e7bef 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/branch_transfer_limits.tt @@ -19,16 +19,22 @@
-

Library [% branchcode | html %] - [% Branches.GetName( branchcode ) | html %] Checkin and transfer policy

+

Library [% fromBranch.branchcode | html %] - [% fromBranch.branchname | html %] Checkin and transfer policy

- + [% PROCESS options_for_libraries libraries => branches %]

Check the boxes for the libraries you accept to checkin items from.

-
[% IF ( limitType == 'ccode' ) %]For all collection codes: [% ELSE %]For all item types: [% END %] Select all | Clear all
+
+ [% IF ( limitType == 'ccode' ) %] + For all collection codes: + [% ELSIF ( limitType == 'itype' ) %] + For all item types: + [% END %] + Select all | Clear all
@@ -54,12 +60,12 @@ [% FOREACH to_branch_loo IN codes_loo.to_branch_loop %] - + [% IF ( to_branch_loo.isChecked ) %] - + [% ELSE %] - + [% END %] @@ -71,7 +77,7 @@
- + Cancel
-- 2.17.1