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