Bug 18789: (QA follow-up) Use is_child in circulation.pl
[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 Modern::Perl;
21 use CGI qw ( -utf8 );
22
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth qw/:DEFAULT get_session/;
26 use C4::Print;  # GetPrinters
27 use C4::Koha;
28 use Koha::BiblioFrameworks;
29 use Koha::Libraries;
30
31 # this will be the script that chooses branch and printer settings....
32
33 my $query = CGI->new();
34
35 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user({
36     template_name   => "circ/selectbranchprinter.tt",
37     query           => $query,
38     type            => "intranet",
39     debug           => 1,
40     authnotrequired => 0,
41     flagsrequired   => { catalogue => 1, },
42 });
43
44 my $sessionID = $query->cookie("CGISESSID");
45 my $session = get_session($sessionID);
46
47 # try to get the branch and printer settings from http, fallback to userenv
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 my $library = Koha::Libraries->find($branch) ) {
59     if (! $userenv_branch or $userenv_branch ne $branch ) {
60         my $branchname = $library->branchname;
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 my @printkeys = sort keys %$printers;
95 if (scalar(@printkeys) == 1 or not $printers->{$printer}) {
96     $printer = $printkeys[0];   # if printer didn't really exist, or there is only 1 anyway, then replace it w/ one that does
97 }
98
99 my @printerloop;
100 foreach ( @printkeys ) {
101     next unless ($_); # skip printer if blank.
102     push @printerloop, {
103         selected => ( $_ eq $printer ),
104         name     => $printers->{$_}->{'printername'},
105         value    => $_,
106     };
107 }
108
109 my @recycle_loop;
110 foreach ($query->param()) {
111     $_ or next;                   # disclude blanks
112     $_ eq "branch"     and next;  # disclude branch
113     $_ eq "printer"    and next;  # disclude printer
114     $_ eq "oldreferer" and next;  # disclude oldreferer
115     push @recycle_loop, {
116         param => $_,
117         value => scalar $query->param($_),
118     };
119 }
120
121 my $referer =  $query->param('oldreferer') || $ENV{HTTP_REFERER};
122 $referer =~ /selectbranchprinter\.pl/ and undef $referer;   # avoid sending them back to this same page.
123
124 if (scalar @updated and not scalar @recycle_loop) {
125     # we updated something, and there were no extra params to POST: quick redirect
126     print $query->redirect($referer || '/cgi-bin/koha/circ/circulation.pl');
127 }
128
129 $template->param(
130     referer     => $referer,
131     printerloop => \@printerloop,
132     branch      => $branch,
133     recycle_loop=> \@recycle_loop,
134 );
135
136 # Checking if there is a Fast Cataloging Framework
137 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
138
139 output_html_with_http_headers $query, $cookie, $template->output;