Merge remote-tracking branch 'kc/new/enh/bug_5263' into kcmaster
[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 use warnings;
9
10 use CGI;
11 use C4::Output;
12 use C4::Auth qw(:DEFAULT :EditPermissions);
13 use C4::Context;
14 use C4::Members;
15 use C4::Branch;
16 use C4::Members::Attributes qw(GetBorrowerAttributes);
17 #use C4::Acquisitions;
18
19 use C4::Output;
20
21 my $input = new CGI;
22
23 my $flagsrequired = { permissions => 1 };
24 my $member=$input->param('member');
25 my $bor = GetMemberDetails( $member,'');
26 if( $bor->{'category_type'} eq 'S' )  {
27         $flagsrequired->{'staffaccess'} = 1;
28 }
29 my ($template, $loggedinuser, $cookie)
30         = get_template_and_user({template_name => "members/member-flags.tmpl",
31                                 query => $input,
32                                 type => "intranet",
33                                 authnotrequired => 0,
34                                 flagsrequired => $flagsrequired,
35                                 debug => 1,
36                                 });
37
38
39 my %member2;
40 $member2{'borrowernumber'}=$member;
41
42 if ($input->param('newflags')) {
43     my $dbh=C4::Context->dbh();
44
45     my @perms = $input->param('flag');
46     my %all_module_perms = ();
47     my %sub_perms = ();
48     foreach my $perm (@perms) {
49         if ($perm !~ /:/) {
50             $all_module_perms{$perm} = 1;
51         } else {
52             my ($module, $sub_perm) = split /:/, $perm, 2;
53             push @{ $sub_perms{$module} }, $sub_perm;
54         }
55     }
56
57     # construct flags
58     my $module_flags = 0;
59     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
60     $sth->execute();
61     while (my ($bit, $flag) = $sth->fetchrow_array) {
62         if (exists $all_module_perms{$flag}) {
63             $module_flags += 2**$bit;
64         }
65     }
66     
67     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
68     $sth->execute($module_flags, $member);
69     
70     # deal with subpermissions
71     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
72     $sth->execute($member); 
73     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
74                         SELECT ?, bit, ?
75                         FROM userflags
76                         WHERE flag = ?");
77     foreach my $module (keys %sub_perms) {
78         next if exists $all_module_perms{$module};
79         foreach my $sub_perm (@{ $sub_perms{$module} }) {
80             $sth->execute($member, $sub_perm, $module);
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         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             push @loop, \%row;
153     }
154
155     if ( $bor->{'category_type'} eq 'C') {
156         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
157         my $cnt = scalar(@$catcodes);
158         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
159         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
160     }
161         
162 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
163     my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
164     $template->param( picture => 1 ) if $picture;
165 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
166
167 $template->param(
168                 borrowernumber => $bor->{'borrowernumber'},
169     cardnumber => $bor->{'cardnumber'},
170                 surname => $bor->{'surname'},
171                 firstname => $bor->{'firstname'},
172                 categorycode => $bor->{'categorycode'},
173                 category_type => $bor->{'category_type'},
174                 categoryname => $bor->{'description'},
175                 address => $bor->{'address'},
176                 address2 => $bor->{'address2'},
177                 city => $bor->{'city'},
178         state => $bor->{'state'},
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         extendedattributes => $attributes,
188                 );
189
190     output_html_with_http_headers $input, $cookie, $template->output;
191
192 }