Merge remote-tracking branch 'kc/new/bug_6170' 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::Acquisitions;
17
18 use C4::Output;
19
20 my $input = new CGI;
21
22 my $flagsrequired = { permissions => 1 };
23 my $member=$input->param('member');
24 my $bor = GetMemberDetails( $member,'');
25 if( $bor->{'category_type'} eq 'S' )  {
26         $flagsrequired->{'staffaccess'} = 1;
27 }
28 my ($template, $loggedinuser, $cookie)
29         = get_template_and_user({template_name => "members/member-flags.tmpl",
30                                 query => $input,
31                                 type => "intranet",
32                                 authnotrequired => 0,
33                                 flagsrequired => $flagsrequired,
34                                 debug => 1,
35                                 });
36
37
38 my %member2;
39 $member2{'borrowernumber'}=$member;
40
41 if ($input->param('newflags')) {
42     my $dbh=C4::Context->dbh();
43
44     my @perms = $input->param('flag');
45     my %all_module_perms = ();
46     my %sub_perms = ();
47     foreach my $perm (@perms) {
48         if ($perm !~ /:/) {
49             $all_module_perms{$perm} = 1;
50         } else {
51             my ($module, $sub_perm) = split /:/, $perm, 2;
52             push @{ $sub_perms{$module} }, $sub_perm;
53         }
54     }
55
56     # construct flags
57     my $module_flags = 0;
58     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
59     $sth->execute();
60     while (my ($bit, $flag) = $sth->fetchrow_array) {
61         if (exists $all_module_perms{$flag}) {
62             $module_flags += 2**$bit;
63         }
64     }
65     
66     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
67     $sth->execute($module_flags, $member);
68     
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     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         my @sub_perm_loop = ();
106         my $expand_parent = 0;
107         if ($checked) {
108             if (exists $all_perms->{$flag}) {
109                 $expand_parent = 1;
110                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
111                     push @sub_perm_loop, {
112                         id => "${flag}_$sub_perm",
113                         perm => "$flag:$sub_perm",
114                         code => $sub_perm,
115                         description => $all_perms->{$flag}->{$sub_perm},
116                         checked => 1
117                     };
118                 }
119             }
120         } else {
121             if (exists $user_perms->{$flag}) {
122                 $expand_parent = 1;
123                 # put selected ones first
124                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
125                     push @sub_perm_loop, {
126                         id => "${flag}_$sub_perm",
127                         perm => "$flag:$sub_perm",
128                         code => $sub_perm,
129                         description => $all_perms->{$flag}->{$sub_perm},
130                         checked => 1
131                     };
132                 }
133             }
134             # then ones not selected
135             if (exists $all_perms->{$flag}) {
136                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
137                     push @sub_perm_loop, {
138                         id => "${flag}_$sub_perm",
139                         perm => "$flag:$sub_perm",
140                         code => $sub_perm,
141                         description => $all_perms->{$flag}->{$sub_perm},
142                         checked => 0
143                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
144                 }
145             }
146         }
147         $row{expand} = $expand_parent;
148         if ($#sub_perm_loop > -1) {
149             $row{sub_perm_loop} = \@sub_perm_loop;
150         }
151             push @loop, \%row;
152     }
153
154     if ( $bor->{'category_type'} eq 'C') {
155         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
156         my $cnt = scalar(@$catcodes);
157         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
158         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
159     }
160         
161 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
162     my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
163     $template->param( picture => 1 ) if $picture;
164                 
165 $template->param(
166                 borrowernumber => $bor->{'borrowernumber'},
167     cardnumber => $bor->{'cardnumber'},
168                 surname => $bor->{'surname'},
169                 firstname => $bor->{'firstname'},
170                 categorycode => $bor->{'categorycode'},
171                 category_type => $bor->{'category_type'},
172                 categoryname => $bor->{'description'},
173                 address => $bor->{'address'},
174                 address2 => $bor->{'address2'},
175                 city => $bor->{'city'},
176         state => $bor->{'state'},
177                 zipcode => $bor->{'zipcode'},
178                 country => $bor->{'country'},
179                 phone => $bor->{'phone'},
180                 email => $bor->{'email'},
181                 branchcode => $bor->{'branchcode'},
182                 branchname => GetBranchName($bor->{'branchcode'}),
183                 loop => \@loop,
184                 is_child        => ($bor->{'category_type'} eq 'C'),
185                 );
186
187     output_html_with_http_headers $input, $cookie, $template->output;
188
189 }