-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 );
+
+# FIXME: branchprinter is not retained by session. This feature was not adequately
+# ported from Koha 2.2.3 where it had been a separate cookie.
+# So this needs to be fixed for Koha 3 or removed outright.
+# --atz (w/ info from chris cormack)
+
+if ($printer) {
+ if (! $userenv_printer or $userenv_printer ne $printer ) {
+ $session->param('branchprinter', $printer); # update sesssion in DB
+ $template->param('new_printer', $printer); # update template
+ push @updated, {
+ updated_printer => 1,
+ old_printer => $userenv_printer,
+ };
+ } # else printer is the same, no update
+} else {
+ $printer = $userenv_printer; # fallback value
+}
+
+$template->param(updated => \@updated) if (scalar @updated);
+
+my @printkeys = sort keys %$printers;
+if (scalar(@printkeys) == 1 or not $printers->{$printer}) {
+ $printer = $printkeys[0]; # if printer didn't really exist, or there is only 1 anyway, then replace it w/ one that does