Bug 15758: Koha::Libraries - Remove GetBranchesLoop
[koha.git] / circ / selectbranchprinter.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use CGI qw ( -utf8 );
23
24 use C4::Context;
25 use C4::Output;
26 use C4::Auth qw/:DEFAULT get_session/;
27 use C4::Print;  # GetPrinters
28 use C4::Koha;
29 use C4::Branch; # GetBranches
30
31 use Koha::Libraries;
32
33 # this will be the script that chooses branch and printer settings....
34
35 my $query = CGI->new();
36
37 my ( $template, $borrowernumber, $cookie ) = get_template_and_user({
38     template_name   => "circ/selectbranchprinter.tt",
39     query           => $query,
40     type            => "intranet",
41     debug           => 1,
42     authnotrequired => 0,
43     flagsrequired   => { catalogue => 1, },
44 });
45
46 my $sessionID = $query->cookie("CGISESSID");
47 my $session = get_session($sessionID);
48
49 # try to get the branch and printer settings from http, fallback to userenv
50 my $branches = GetBranches();
51 my $printers = GetPrinters();
52 my $branch   = $query->param('branch' );
53 my $printer  = $query->param('printer');
54 # fallbacks for $branch and $printer after possible session updates
55
56 my $userenv_branch  = C4::Context->userenv->{'branch'}        || '';
57 my $userenv_printer = C4::Context->userenv->{'branchprinter'} || '';
58 my @updated;
59
60 # $session lddines here are doing the updating
61 if ($branch and $branches->{$branch}) {
62     if (! $userenv_branch or $userenv_branch ne $branch ) {
63         my $branchname = Koha::Libraries->find($branch)->branchname;
64         $template->param(LoginBranchname => $branchname);   # update template for new branch
65         $template->param(LoginBranchcode => $branch);       # update template for new branch
66         $session->param('branchname', $branchname);         # update sesssion in DB
67         $session->param('branch', $branch);                 # update sesssion in DB
68         push @updated, {
69             updated_branch => 1,
70                 old_branch => $userenv_branch,
71         };
72     } # else branch the same, no update
73 } else {
74     $branch = $userenv_branch;  # fallback value
75 }
76
77 # FIXME: branchprinter is not retained by session.  This feature was not adequately
78 # ported from Koha 2.2.3 where it had been a separate cookie.
79 # So this needs to be fixed for Koha 3 or removed outright.
80 #   --atz (w/ info from chris cormack)
81
82 if ($printer) {
83     if (! $userenv_printer or $userenv_printer ne $printer ) {
84         $session->param('branchprinter', $printer);         # update sesssion in DB
85         $template->param('new_printer', $printer);          # update template
86         push @updated, {
87             updated_printer => 1,
88                 old_printer => $userenv_printer,
89         };
90     } # else printer is the same, no update
91 } else {
92     $printer = $userenv_printer;  # fallback value
93 }
94
95 $template->param(updated => \@updated) if (scalar @updated);
96
97 unless ($branches->{$branch}) {
98     $branch = (keys %$branches)[0];  # if branch didn't really exist, then replace it w/ one that does
99 }
100
101 my @printkeys = sort keys %$printers;
102 if (scalar(@printkeys) == 1 or not $printers->{$printer}) {
103     $printer = $printkeys[0];   # if printer didn't really exist, or there is only 1 anyway, then replace it w/ one that does
104 }
105
106 my @printerloop;
107 foreach ( @printkeys ) {
108     next unless ($_); # skip printer if blank.
109     push @printerloop, {
110         selected => ( $_ eq $printer ),
111         name     => $printers->{$_}->{'printername'},
112         value    => $_,
113     };
114 }
115
116 my @recycle_loop;
117 foreach ($query->param()) {
118     $_ or next;                   # disclude blanks
119     $_ eq "branch"     and next;  # disclude branch
120     $_ eq "printer"    and next;  # disclude printer
121     $_ eq "oldreferer" and next;  # disclude oldreferer
122     push @recycle_loop, {
123         param => $_,
124         value => scalar $query->param($_),
125     };
126 }
127
128 my $referer =  $query->param('oldreferer') || $ENV{HTTP_REFERER};
129 $referer =~ /selectbranchprinter\.pl/ and undef $referer;   # avoid sending them back to this same page.
130
131 if (scalar @updated and not scalar @recycle_loop) {
132     # we updated something, and there were no extra params to POST: quick redirect
133     print $query->redirect($referer || '/cgi-bin/koha/circ/circulation.pl');
134 }
135
136 $template->param(
137     referer     => $referer,
138     printerloop => \@printerloop,
139     branch      => $branch,
140     recycle_loop=> \@recycle_loop,
141 );
142
143 output_html_with_http_headers $query, $cookie, $template->output;