Scripts missing check for Child category type would not correctly show 'update child...
[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::Acquisitions;
15
16 use C4::Output;
17
18 my $input = new CGI;
19
20 my $flagsrequired = { permissions => 1 };
21 my $member=$input->param('member');
22 my $bor = GetMemberDetails( $member,'');
23 if( $bor->{'category_type'} eq 'S' )  {
24         $flagsrequired->{'staffaccess'} = 1;
25 }
26 my ($template, $loggedinuser, $cookie)
27         = get_template_and_user({template_name => "members/member-flags.tmpl",
28                                 query => $input,
29                                 type => "intranet",
30                                 authnotrequired => 0,
31                                 flagsrequired => $flagsrequired,
32                                 debug => 1,
33                                 });
34
35
36 my %member2;
37 $member2{'borrowernumber'}=$member;
38
39 if ($input->param('newflags')) {
40     my $dbh=C4::Context->dbh();
41
42     my @perms = $input->param('flag');
43     my %all_module_perms = ();
44     my %sub_perms = ();
45     foreach my $perm (@perms) {
46         if ($perm !~ /:/) {
47             $all_module_perms{$perm} = 1;
48         } else {
49             my ($module, $sub_perm) = split /:/, $perm, 2;
50             push @{ $sub_perms{$module} }, $sub_perm;
51         }
52     }
53
54     # construct flags
55     my $module_flags = 0;
56     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
57     $sth->execute();
58     while (my ($bit, $flag) = $sth->fetchrow_array) {
59         if (exists $all_module_perms{$flag}) {
60             $module_flags += 2**$bit;
61         }
62     }
63     
64     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
65     $sth->execute($module_flags, $member);
66     
67     if (C4::Context->preference('GranularPermissions')) {
68         # deal with subpermissions
69         $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
70         $sth->execute($member); 
71         $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
72                             SELECT ?, bit, ?
73                             FROM userflags
74                             WHERE flag = ?");
75         foreach my $module (keys %sub_perms) {
76             next if exists $all_module_perms{$module};
77             foreach my $sub_perm (@{ $sub_perms{$module} }) {
78                 $sth->execute($member, $sub_perm, $module);
79             }
80         }
81     }
82     
83     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
84 } else {
85 #     my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
86     my $flags = $bor->{'flags'};
87     my $accessflags = $bor->{'authflags'};
88     my $dbh=C4::Context->dbh();
89     my $all_perms  = get_all_subpermissions();
90     my $user_perms = get_user_subpermissions($bor->{'userid'});
91     my $sth=$dbh->prepare("SELECT bit,flag,flagdesc FROM userflags ORDER BY bit");
92     $sth->execute;
93     my @loop;
94     while (my ($bit, $flag, $flagdesc) = $sth->fetchrow) {
95             my $checked='';
96             if ($accessflags->{$flag}) {
97                 $checked= 1;
98             }
99
100             my %row = ( bit => $bit,
101                     flag => $flag,
102                     checked => $checked,
103                     flagdesc => $flagdesc );
104
105         if (C4::Context->preference('GranularPermissions')) {
106             my @sub_perm_loop = ();
107             my $expand_parent = 0;
108             if ($checked) {
109                 if (exists $all_perms->{$flag}) {
110                     $expand_parent = 1;
111                     foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
112                         push @sub_perm_loop, {
113                             id => "${flag}_$sub_perm",
114                             perm => "$flag:$sub_perm",
115                             code => $sub_perm,
116                             description => $all_perms->{$flag}->{$sub_perm},
117                             checked => 1
118                         };
119                     }
120                 }
121             } else {
122                 if (exists $user_perms->{$flag}) {
123                     $expand_parent = 1;
124                     # put selected ones first
125                     foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
126                         push @sub_perm_loop, {
127                             id => "${flag}_$sub_perm",
128                             perm => "$flag:$sub_perm",
129                             code => $sub_perm,
130                             description => $all_perms->{$flag}->{$sub_perm},
131                             checked => 1
132                         };
133                     }
134                 }
135                 # then ones not selected
136                 if (exists $all_perms->{$flag}) {
137                     foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
138                         push @sub_perm_loop, {
139                             id => "${flag}_$sub_perm",
140                             perm => "$flag:$sub_perm",
141                             code => $sub_perm,
142                             description => $all_perms->{$flag}->{$sub_perm},
143                             checked => 0
144                         } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
145                     }
146                 }
147             }
148             $row{expand} = $expand_parent;
149             if ($#sub_perm_loop > -1) {
150             $row{sub_perm_loop} = \@sub_perm_loop;
151             }
152         }
153             push @loop, \%row;
154     }
155
156 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
157     my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
158     $template->param( picture => 1 ) if $picture;
159                 
160 $template->param(
161                 borrowernumber => $bor->{'borrowernumber'},
162     cardnumber => $bor->{'cardnumber'},
163                 surname => $bor->{'surname'},
164                 firstname => $bor->{'firstname'},
165                 categorycode => $bor->{'categorycode'},
166                 category_type => $bor->{'category_type'},
167                 category_description => $bor->{'description'},
168                 address => $bor->{'address'},
169                 address2 => $bor->{'address2'},
170                 city => $bor->{'city'},
171                 zipcode => $bor->{'zipcode'},
172                 phone => $bor->{'phone'},
173                 email => $bor->{'email'},
174                 branchcode => $bor->{'branchcode'},
175                 loop => \@loop,
176                 is_child        => ($bor->{'category_type'} eq 'C'),
177                 );
178
179     output_html_with_http_headers $input, $cookie, $template->output;
180
181 }