bug 3464: Takes columns country and B_country of table borrowers into account in...
[wip/koha-chris_n.git] / members / member-flags.pl
1 #!/usr/bin/perl
2
3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
6
7 use strict;
8
9 use CGI;
10 use C4::Output;
11 use C4::Auth qw(:DEFAULT :EditPermissions);
12 use C4::Context;
13 use C4::Members;
14 use C4::Branch;
15 #use C4::Acquisitions;
16
17 use C4::Output;
18
19 my $input = new CGI;
20
21 my $flagsrequired = { permissions => 1 };
22 my $member=$input->param('member');
23 my $bor = GetMemberDetails( $member,'');
24 if( $bor->{'category_type'} eq 'S' )  {
25         $flagsrequired->{'staffaccess'} = 1;
26 }
27 my ($template, $loggedinuser, $cookie)
28         = get_template_and_user({template_name => "members/member-flags.tmpl",
29                                 query => $input,
30                                 type => "intranet",
31                                 authnotrequired => 0,
32                                 flagsrequired => $flagsrequired,
33                                 debug => 1,
34                                 });
35
36
37 my %member2;
38 $member2{'borrowernumber'}=$member;
39
40 if ($input->param('newflags')) {
41     my $dbh=C4::Context->dbh();
42
43     my @perms = $input->param('flag');
44     my %all_module_perms = ();
45     my %sub_perms = ();
46     foreach my $perm (@perms) {
47         if ($perm !~ /:/) {
48             $all_module_perms{$perm} = 1;
49         } else {
50             my ($module, $sub_perm) = split /:/, $perm, 2;
51             push @{ $sub_perms{$module} }, $sub_perm;
52         }
53     }
54
55     # construct flags
56     my $module_flags = 0;
57     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
58     $sth->execute();
59     while (my ($bit, $flag) = $sth->fetchrow_array) {
60         if (exists $all_module_perms{$flag}) {
61             $module_flags += 2**$bit;
62         }
63     }
64     
65     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
66     $sth->execute($module_flags, $member);
67     
68     if (C4::Context->preference('GranularPermissions')) {
69         # deal with subpermissions
70         $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
71         $sth->execute($member); 
72         $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
73                             SELECT ?, bit, ?
74                             FROM userflags
75                             WHERE flag = ?");
76         foreach my $module (keys %sub_perms) {
77             next if exists $all_module_perms{$module};
78             foreach my $sub_perm (@{ $sub_perms{$module} }) {
79                 $sth->execute($member, $sub_perm, $module);
80             }
81         }
82     }
83     
84     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
85 } else {
86 #     my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
87     my $flags = $bor->{'flags'};
88     my $accessflags = $bor->{'authflags'};
89     my $dbh=C4::Context->dbh();
90     my $all_perms  = get_all_subpermissions();
91     my $user_perms = get_user_subpermissions($bor->{'userid'});
92     my $sth=$dbh->prepare("SELECT bit,flag,flagdesc FROM userflags ORDER BY bit");
93     $sth->execute;
94     my @loop;
95     while (my ($bit, $flag, $flagdesc) = $sth->fetchrow) {
96             my $checked='';
97             if ($accessflags->{$flag}) {
98                 $checked= 1;
99             }
100
101             my %row = ( bit => $bit,
102                     flag => $flag,
103                     checked => $checked,
104                     flagdesc => $flagdesc );
105
106         if (C4::Context->preference('GranularPermissions')) {
107             my @sub_perm_loop = ();
108             my $expand_parent = 0;
109             if ($checked) {
110                 if (exists $all_perms->{$flag}) {
111                     $expand_parent = 1;
112                     foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
113                         push @sub_perm_loop, {
114                             id => "${flag}_$sub_perm",
115                             perm => "$flag:$sub_perm",
116                             code => $sub_perm,
117                             description => $all_perms->{$flag}->{$sub_perm},
118                             checked => 1
119                         };
120                     }
121                 }
122             } else {
123                 if (exists $user_perms->{$flag}) {
124                     $expand_parent = 1;
125                     # put selected ones first
126                     foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
127                         push @sub_perm_loop, {
128                             id => "${flag}_$sub_perm",
129                             perm => "$flag:$sub_perm",
130                             code => $sub_perm,
131                             description => $all_perms->{$flag}->{$sub_perm},
132                             checked => 1
133                         };
134                     }
135                 }
136                 # then ones not selected
137                 if (exists $all_perms->{$flag}) {
138                     foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
139                         push @sub_perm_loop, {
140                             id => "${flag}_$sub_perm",
141                             perm => "$flag:$sub_perm",
142                             code => $sub_perm,
143                             description => $all_perms->{$flag}->{$sub_perm},
144                             checked => 0
145                         } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
146                     }
147                 }
148             }
149             $row{expand} = $expand_parent;
150             if ($#sub_perm_loop > -1) {
151             $row{sub_perm_loop} = \@sub_perm_loop;
152             }
153         }
154             push @loop, \%row;
155     }
156
157     if ( $bor->{'category_type'} eq 'C') {
158         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
159         my $cnt = scalar(@$catcodes);
160         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
161         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
162     }
163         
164 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
165     my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
166     $template->param( picture => 1 ) if $picture;
167                 
168 $template->param(
169                 borrowernumber => $bor->{'borrowernumber'},
170     cardnumber => $bor->{'cardnumber'},
171                 surname => $bor->{'surname'},
172                 firstname => $bor->{'firstname'},
173                 categorycode => $bor->{'categorycode'},
174                 category_type => $bor->{'category_type'},
175                 categoryname => $bor->{'description'},
176                 address => $bor->{'address'},
177                 address2 => $bor->{'address2'},
178                 city => $bor->{'city'},
179                 zipcode => $bor->{'zipcode'},
180                 country => $bor->{'country'},
181                 phone => $bor->{'phone'},
182                 email => $bor->{'email'},
183                 branchcode => $bor->{'branchcode'},
184                 branchname => GetBranchName($bor->{'branchcode'}),
185                 loop => \@loop,
186                 is_child        => ($bor->{'category_type'} eq 'C'),
187                 );
188
189     output_html_with_http_headers $input, $cookie, $template->output;
190
191 }