Fix for Bug 5436 - Extended patron attributes display improvements
[koha.git] / admin / branch_transfer_limits.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 # copyright 2010 BibLibre
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23
24 use CGI;
25 use C4::Auth;
26 use C4::Context;
27 use C4::Output;
28 use C4::Koha;
29 use C4::Branch; 
30 use C4::Circulation qw{ IsBranchTransferAllowed DeleteBranchTransferLimits CreateBranchTransferLimit };
31
32 my $input = new CGI;
33
34 my ($template, $loggedinuser, $cookie)
35     = get_template_and_user({template_name => "admin/branch_transfer_limits.tmpl",
36                              query => $input,
37                              type => "intranet",
38                              flagsrequired => {borrowers => 1},
39                              debug => 1,
40                              });
41
42 my $dbh = C4::Context->dbh;
43 my $branchcode;
44 if((!defined($input->param('branchcode'))) & mybranch() ne '')
45 {
46         $branchcode = mybranch();
47 }
48 else
49 {
50         $branchcode = $input->param('branchcode');
51 }
52
53 my $branchname = GetBranchName($branchcode);
54
55 # Getting the branches for user selection
56 my $branches = GetBranches();
57 my @branch_loop;
58 for my $thisbranch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
59     my %row =(value => $thisbranch,
60               branchname => $branches->{$thisbranch}->{'branchname'},
61               selected => $thisbranch eq $branchcode ? 1 : 0,
62              );
63     push @branch_loop, \%row;
64 }
65
66
67 # Set the template language for the correct limit type
68 my $limit_phrase = 'Collection Code';
69 my $limitType = C4::Context->preference("BranchTransferLimitsType") || "ccode";
70 if ( $limitType eq 'itemtype' ) {
71         $limit_phrase = 'Item Type';
72 }
73
74 my @codes;
75 my @branchcodes;
76
77 my $sth;
78 if ( $limitType eq 'ccode' ) {
79         $sth = $dbh->prepare('SELECT authorised_value AS ccode FROM authorised_values WHERE category = "CCODE"');
80 } elsif ( $limitType eq 'itemtype' ) {
81         $sth = $dbh->prepare('SELECT itemtype FROM itemtypes');
82 }
83 $sth->execute();
84 while ( my $row = $sth->fetchrow_hashref ) {
85         push( @codes, $row->{ $limitType } );
86 }
87
88 $sth = $dbh->prepare("SELECT branchcode FROM branches");
89 $sth->execute();
90 while ( my $row = $sth->fetchrow_hashref ) {
91         push( @branchcodes, $row->{'branchcode'} );
92 }
93
94 ## If Form Data Passed, Update the Database
95 if ( $input->param('updateLimits') ) {
96         DeleteBranchTransferLimits();
97
98         foreach my $code ( @codes ) {
99                 foreach my $toBranch ( @branchcodes ) {
100                         my $isSet = not $input->param( $code . "_" . $toBranch);
101                         if ( $isSet ) {
102                             CreateBranchTransferLimit( $toBranch, $branchcode, $code );
103                         }
104                 }
105         }
106 }
107
108 ## Build branchcode loop
109 my @branchcode_loop;
110 foreach my $branchcode ( @branchcodes ) {
111         my %row_data;
112         $row_data{ branchcode } = $branchcode;
113         push ( @branchcode_loop, \%row_data );
114 }
115 my $branchcount = scalar(@branchcode_loop);
116
117 ## Build the default data
118 my @codes_loop;
119 foreach my $code ( @codes ) {
120         my @to_branch_loop;
121         my %row_data;
122         $row_data{ code } = $code;
123         $row_data{ to_branch_loop } = \@to_branch_loop;
124         foreach my $toBranch ( @branchcodes ) {
125                 my %row_data;
126                 my $isChecked = IsBranchTransferAllowed( $toBranch, $branchcode, $code );
127                 $row_data{ code }         = $code;
128                 $row_data{ toBranch }     = $toBranch;
129                 $row_data{ isChecked }    = $isChecked; 
130                 $row_data{ toBranchname } = GetBranchName($toBranch);   
131                 push( @to_branch_loop, \%row_data );
132         }
133
134         push( @codes_loop, \%row_data );
135 }
136
137
138 $template->param(
139                 branchcount => $branchcount,
140                 codes_loop => \@codes_loop,
141                 branch_loop => \@branch_loop,
142                 branchcode_loop => \@branchcode_loop,
143                 branchcode => $branchcode,
144                 branchname => $branchname,
145                 limit_phrase => $limit_phrase,
146                 );
147
148 output_html_with_http_headers $input, $cookie, $template->output;
149