From 4677ae739015f062ad2db5f9aa49b5c05628ec5f Mon Sep 17 00:00:00 2001 From: Joe Atzberger Date: Fri, 29 May 2009 16:09:34 -0500 Subject: [PATCH] Cleanup selectbranchprinter.pl and .tmpl Use GetBranchesLoop and warnings. Remove unused code. Moved the Get's to after auth. I begin to show how this script will use referer to redirect back to the sending page upon successful change of branch or printer (see hidden div and recycle_loop). Indicator for singleBranchMode added. Also added another helper function for GetBranchesLoop, namely mybranch. This encapsulates the logic for finding an intelligent default selection, which is almost always desirable. This does not resolve bug 2426, but is work in that direction. Signed-off-by: Galen Charlton --- C4/Branch.pm | 10 +- circ/selectbranchprinter.pl | 118 +++++++----------- .../en/modules/circ/selectbranchprinter.tmpl | 67 ++++++---- 3 files changed, 92 insertions(+), 103 deletions(-) diff --git a/C4/Branch.pm b/C4/Branch.pm index 29b1a93829..35424d7fcf 100644 --- a/C4/Branch.pm +++ b/C4/Branch.pm @@ -45,7 +45,7 @@ BEGIN { &DelBranch &DelBranchCategory ); - @EXPORT_OK = qw( &onlymine ); + @EXPORT_OK = qw( &onlymine &mybranch ); } =head1 NAME @@ -145,8 +145,14 @@ sub onlymine { C4::Context->userenv->{branch} ; } +# always returns a string for OK comparison via "eq" or "ne" +sub mybranch { + C4::Context->userenv or return ''; + return C4::Context->userenv->{branch} || ''; +} + sub GetBranchesLoop (;$$) { # since this is what most pages want anyway - my $branch = @_ ? shift : ''; # optional first argument is branchcode of "my branch", if preselection is wanted. + my $branch = @_ ? shift : mybranch(); # optional first argument is branchcode of "my branch", if preselection is wanted. my $onlymine = @_ ? shift : onlymine(); my $branches = GetBranches($onlymine); my @loop; diff --git a/circ/selectbranchprinter.pl b/circ/selectbranchprinter.pl index cfb5831e97..c878742820 100755 --- a/circ/selectbranchprinter.pl +++ b/circ/selectbranchprinter.pl @@ -18,101 +18,67 @@ # Suite 330, Boston, MA 02111-1307 USA use strict; -use CGI qw/:standard/; +use warnings; +use CGI; use C4::Context; -use C4::Circulation; use C4::Output; use C4::Auth; -use C4::Print; +use C4::Print; # GetPrinters use C4::Koha; -use C4::Branch; # GetBranches +use C4::Branch; # GetBranches GetBranchesLoop -# this is a reorganisation of circulationold.pl -# dividing it up into three scripts...... -# this will be the first one that chooses branch and printer settings.... +# this will be the script that chooses branch and printer settings.... -#general design stuff... +my $query = CGI->new(); +my ( $template, $borrowernumber, $cookie ) = get_template_and_user({ + template_name => "circ/selectbranchprinter.tmpl", + query => $query, + type => "intranet", + debug => 1, + authnotrequired => 0, + flagsrequired => { circulate => "circulate_remaining_permissions" }, +}); -# try to get the branch and printer settings from the http.... -my $query = new CGI; +# try to get the branch and printer settings from http, fallback to userenv my $branches = GetBranches(); my $printers = GetPrinters(); -my $branch = $query->param('branch'); -my $printer = $query->param('printer'); +my $branch = $query->param('branch' ) || C4::Context->userenv->{'branch'}; +my $printer = $query->param('printer') || C4::Context->userenv->{'branchprinter'}; -# set header with cookie.... - -my ( $template, $borrowernumber, $cookie ) = get_template_and_user( - { - template_name => "circ/selectbranchprinter.tmpl", - query => $query, - type => "intranet", - authnotrequired => 0, - flagsrequired => { circulate => "circulate_remaining_permissions" }, - } -); - - -($branch) || ( $branch = C4::Context->userenv->{'branch'} ); -($printer) || ( $printer = C4::Context->userenv->{'branchprinter'} ); -( $branches->{$branch} ) || ( $branch = ( keys %$branches )[0] ); -( $printers->{$printer} ) || ( $printer = ( keys %$printers )[0] ); - -# if you force a selection.... -my $oldbranch = $branch; -my $oldprinter = $printer; - -# set up select options.... -my $branchcount = 0; -my $printercount = 0; -my @branchloop; -for my $br (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) { - next unless $br =~ /\S/; # next unless $br is not blank. +unless ($branches->{$branch}) { + $branch = (keys %$branches)[0]; # if branch didn't really exist, then replace it w/ one that does +} - $branchcount++; - my %branch; - $branch{selected} = ( $br eq $oldbranch ); - $branch{name} = $branches->{$br}->{'branchname'}; - $branch{value} = $br; - push( @branchloop, \%branch ); +my @printkeys = sort keys %$printers; +if (scalar(@printkeys) == 1 or not $printers->{$printer}) { + $printer = $printkeys[0]; } + my @printerloop; -foreach ( keys %$printers ) { - (next) unless ($_); # next unless if this printer is blank. - $printercount++; - my %printer; - $printer{selected} = ( $_ eq $oldprinter ); - $printer{name} = $printers->{$_}->{'printername'}; - $printer{value} = $_; - push( @printerloop, \%printer ); +foreach ( @printkeys ) { + next unless ($_); # skip printer if blank. + push @printerloop, { + selected => ( $_ eq $printer ), + name => $printers->{$_}->{'printername'}, + value => $_, + }; } -# if there is only one.... -my $printername; -my $branchname; - -my $oneprinter = ( $printercount == 1 ); -my $onebranch = ( $branchcount == 1 ); -if ( $printercount == 1 ) { - my ($tmpprinter) = keys %$printers; - $printername = $printers->{$tmpprinter}->{printername}; +my @recycle_loop; +foreach ($query->param()) { + /^branch(printer)?$/ and next; # disclude branch and branchprinter + push @recycle_loop, { + param => $_, + value => $query->param($_), + }; } -if ( $branchcount == 1 ) { - my ($tmpbranch) = keys %$branches; - $branchname = $branches->{$tmpbranch}->{branchname}; -} - -################################################################################ -# Start writing page.... $template->param( - oneprinter => $oneprinter, - onebranch => $onebranch, - printername => $printername, - branchname => $branchname, - printerloop => \@printerloop, - branchloop => \@branchloop, + referer => $ENV{HTTP_REFERER}, + printerloop => \@printerloop, + branchloop => GetBranchesLoop($branch), + recycle_loop=> \@recycle_loop, ); output_html_with_http_headers $query, $cookie, $template->output; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/selectbranchprinter.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/selectbranchprinter.tmpl index bc3326a630..2298ba28b0 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/selectbranchprinter.tmpl +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/selectbranchprinter.tmpl @@ -1,55 +1,72 @@ Koha › Circulation › Set Library + - +
- -
-
-
- - +
+
+
+
- - + +
Single Branch mode is ON. +
Set Library
  1. -
  2. - +
  3. -
-
+ +
+
+ + " value="" /> + + +
referer is: + + + +
+
- -
-
-
- \ No newline at end of file +
+
+
+ -- 2.39.5