Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[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 use Koha::Desks;
30
31 my $query = CGI->new();
32
33 my ( $template, $borrowernumber, $cookie, $flags ) = get_template_and_user({
34     template_name   => "circ/set-library.tt",
35     query           => $query,
36     type            => "intranet",
37     debug           => 1,
38     authnotrequired => 0,
39     flagsrequired   => { catalogue => 1, },
40 });
41
42 my $sessionID = $query->cookie("CGISESSID");
43 my $session = get_session($sessionID);
44
45 my $branch   = $query->param('branch' );
46 my $desk_id = $query->param('desk_id');
47 my $userenv_branch  = C4::Context->userenv->{'branch'}        || '';
48 my $userenv_desk = C4::Context->userenv->{'desk_id'} || '';
49 my @updated;
50
51 # $session lddines here are doing the updating
52 if ( $branch and my $library = Koha::Libraries->find($branch) ) {
53     if (! $userenv_branch or $userenv_branch ne $branch ) {
54         my $branchname = $library->branchname;
55         $session->param('branchname', $branchname);         # update sesssion in DB
56         $session->param('branch', $branch);                 # update sesssion in DB
57         $session->flush();
58         push @updated, {
59             updated_branch => 1,
60                 old_branch => $userenv_branch,
61                 new_branch => $branch,
62         };
63     } # else branch the same, no update
64     if ( $desk_id && (!$userenv_desk or $userenv_desk ne $desk_id) ) {
65         my $desk = Koha::Desks->find( { desk_id => $desk_id } );
66         my $old_desk_name = '';
67         if ($userenv_desk) {
68             $old_desk_name = Koha::Desks->find( { desk_id => $userenv_desk })->desk_name;
69         }
70         $template->param( LoginDeskname => $desk->desk_name );
71         $template->param( LoginDeskid => $desk->desk_id );
72         $session->param( desk_name => $desk->desk_name );
73         $session->param( desk_id => $desk->desk_id );
74         $session->flush();
75         push @updated, {
76             updated_desk => 1,
77             old_desk => $old_desk_name,
78         };
79     }
80 } else {
81     $branch = $userenv_branch;  # fallback value
82     $desk_id = $userenv_desk;
83 }
84
85 $template->param(updated => \@updated) if (scalar @updated);
86
87 my @recycle_loop;
88 foreach ($query->param()) {
89     $_ or next;                   # disclude blanks
90     $_ eq "branch"     and next;  # disclude branch
91     $_ eq "desk_id"    and next;  # disclude desk_id
92     $_ eq "oldreferer" and next;  # disclude oldreferer
93     push @recycle_loop, {
94         param => $_,
95         value => scalar $query->param($_),
96     };
97 }
98
99 my $referer =  $query->param('oldreferer') || $ENV{HTTP_REFERER};
100 $referer =~ /set-library\.pl/ and undef $referer;   # avoid sending them back to this same page.
101
102 if (scalar @updated and not scalar @recycle_loop) {
103     # we updated something, and there were no extra params to POST: quick redirect
104     print $query->redirect($referer || '/cgi-bin/koha/circ/circulation.pl');
105 }
106
107 $template->param(
108     referer     => $referer,
109     branch      => $branch,
110     desk_id     => $desk_id,
111     recycle_loop=> \@recycle_loop,
112 );
113
114 # Checking if there is a Fast Cataloging Framework
115 $template->param( fast_cataloging => 1 ) if Koha::BiblioFrameworks->find( 'FA' );
116
117 output_html_with_http_headers $query, $cookie, $template->output;