Bug 25765: Replace LoginBranchname and LoginBranchcode with use of Branches template...
[koha.git] / circ / set-library.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::Koha;
27 use Koha::BiblioFrameworks;
28 use Koha::Libraries;
29
30 my $query = CGI->new();
31
32 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user({
33     template_name   => "circ/set-library.tt",
34     query           => $query,
35     type            => "intranet",
36     debug           => 1,
37     authnotrequired => 0,
38     flagsrequired   => { catalogue => 1, },
39 });
40
41 my $sessionID = $query->cookie("CGISESSID");
42 my $session = get_session($sessionID);
43
44 my $branch   = $query->param('branch' );
45 my $userenv_branch  = C4::Context->userenv->{'branch'}        || '';
46 my @updated;
47
48 # $session lddines here are doing the updating
49 if ( $branch and my $library = Koha::Libraries->find($branch) ) {
50     if (! $userenv_branch or $userenv_branch ne $branch ) {
51         my $branchname = $library->branchname;
52         $session->param('branchname', $branchname);         # update sesssion in DB
53         $session->param('branch', $branch);                 # update sesssion in DB
54         $session->flush();
55         push @updated, {
56             updated_branch => 1,
57                 old_branch => $userenv_branch,
58                 new_branch => $branch,
59         };
60     } # else branch the same, no update
61 } else {
62     $branch = $userenv_branch;  # fallback value
63 }
64
65 $template->param(updated => \@updated) if (scalar @updated);
66
67 my @recycle_loop;
68 foreach ($query->param()) {
69     $_ or next;                   # disclude blanks
70     $_ eq "branch"     and next;  # disclude branch
71     $_ eq "oldreferer" and next;  # disclude oldreferer
72     push @recycle_loop, {
73         param => $_,
74         value => scalar $query->param($_),
75     };
76 }
77
78 my $referer =  $query->param('oldreferer') || $ENV{HTTP_REFERER};
79 $referer =~ /set-library\.pl/ and undef $referer;   # avoid sending them back to this same page.
80
81 if (scalar @updated and not scalar @recycle_loop) {
82     # we updated something, and there were no extra params to POST: quick redirect
83     print $query->redirect($referer || '/cgi-bin/koha/circ/circulation.pl');
84 }
85
86 $template->param(
87     referer     => $referer,
88     branch      => $branch,
89     recycle_loop=> \@recycle_loop,
90 );
91
92 # Checking if there is a Fast Cataloging Framework
93 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
94
95 output_html_with_http_headers $query, $cookie, $template->output;