-use C4::Auth;
-use C4::Print;
-use C4::Interface::CGI::Output;
-use HTML::Template;
-use DBI;
-
-
-# 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....
-
-#general design stuff...
-my $headerbackgroundcolor='#99cc33';
-my $circbackgroundcolor='#ffffcc';
-my $circbackgroundcolor='white';
-my $linecolor1='#ffffcc';
-my $linecolor2='white';
-my $backgroundimage="/images/background-mem.gif";
-
-# try to get the branch and printer settings from the http....
-my %env;
-my $query=new CGI;
-my $branches=getbranches(\%env);
-my $printers=getprinters(\%env);
-my $branch=$query->param('branch');
-my $printer=$query->param('printer');
-
-($branch) || ($branch=$query->cookie('branch'));
-($printer) || ($printer=$query->cookie('printer'));
-
-($branches->{$branch}) || ($branch=(keys %$branches)[0]);
-($printers->{$printer}) || ($printer=(keys %$printers)[0]);
-
-
-# is you force a selection....
-my $oldbranch = $branch;
-my $oldprinter = $printer;
-
-$branch='';
-$printer='';
-
-
-$env{'branchcode'}=$branch;
-$env{'printer'}=$printer;
-$env{'queue'}=$printer;
-
-# set up select options....
-my $branchcount=0;
-my $printercount=0;
-my @branchloop;
-foreach my $br (keys %$branches) {
- next unless $br =~ /\S/;
- #(next) unless ($branches->{$_}->{'IS'}); # FIXME disabled to fix bug 202
- $branchcount++;
- my %branch;
- $branch{selected}=($br eq $oldbranch);
- $branch{name}=$branches->{$br}->{'branchname'};
- $branch{value}=$br;
- push(@branchloop,\%branch);
+use C4::Auth qw/:DEFAULT get_session/;
+use C4::Print; # GetPrinters
+use C4::Koha;
+use C4::Branch; # GetBranches GetBranchesLoop
+
+# this will be the script that chooses branch and printer settings....
+
+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 => { catalogue => 1, },
+});
+
+my $sessionID = $query->cookie("CGISESSID");
+my $session = get_session($sessionID);
+
+# 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');
+# fallbacks for $branch and $printer after possible session updates
+
+my $userenv_branch = C4::Context->userenv->{'branch'} || '';
+my $userenv_printer = C4::Context->userenv->{'branchprinter'} || '';
+my @updated;
+
+# $session lddines here are doing the updating
+if ($branch and $branches->{$branch}) {
+ if (! $userenv_branch or $userenv_branch ne $branch ) {
+ my $branchname = GetBranchName($branch);
+ $template->param(LoginBranchname => $branchname); # update template for new branch
+ $template->param(LoginBranchcode => $branch); # update template for new branch
+ $session->param('branchname', $branchname); # update sesssion in DB
+ $session->param('branch', $branch); # update sesssion in DB
+ push @updated, {
+ updated_branch => 1,
+ old_branch => $userenv_branch,
+ };
+ } # else branch the same, no update
+} else {
+ $branch = $userenv_branch; # fallback value