Bug 17578: GetMemberDetails - Remove authflags - 1
[koha.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 qw ( -utf8 );
11 use C4::Output;
12 use C4::Auth qw(:DEFAULT :EditPermissions);
13 use C4::Context;
14 use C4::Members;
15 use C4::Members::Attributes qw(GetBorrowerAttributes);
16 #use C4::Acquisitions;
17
18 use Koha::Patron::Categories;
19
20 use C4::Output;
21 use Koha::Patron::Images;
22
23 my $input = new CGI;
24
25 my $flagsrequired = { permissions => 1 };
26 my $member=$input->param('member');
27 my $bor = GetMember( borrowernumber => $member );
28 if( $bor->{'category_type'} eq 'S' )  {
29         $flagsrequired->{'staffaccess'} = 1;
30 }
31 my ($template, $loggedinuser, $cookie) = get_template_and_user({
32         template_name   => "members/member-flags.tt",
33         query           => $input,
34         type            => "intranet",
35         authnotrequired => 0,
36         flagsrequired   => $flagsrequired,
37         debug           => 1,
38 });
39
40
41 my %member2;
42 $member2{'borrowernumber'}=$member;
43
44 if ($input->param('newflags')) {
45     my $dbh=C4::Context->dbh();
46
47     my @perms = $input->multi_param('flag');
48     my %all_module_perms = ();
49     my %sub_perms = ();
50     foreach my $perm (@perms) {
51         if ($perm !~ /:/) {
52             $all_module_perms{$perm} = 1;
53         } else {
54             my ($module, $sub_perm) = split /:/, $perm, 2;
55             push @{ $sub_perms{$module} }, $sub_perm;
56         }
57     }
58
59     # construct flags
60     my $module_flags = 0;
61     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
62     $sth->execute();
63     while (my ($bit, $flag) = $sth->fetchrow_array) {
64         if (exists $all_module_perms{$flag}) {
65             $module_flags += 2**$bit;
66         }
67     }
68     
69     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
70     $sth->execute($module_flags, $member);
71     
72     # deal with subpermissions
73     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
74     $sth->execute($member); 
75     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
76                         SELECT ?, bit, ?
77                         FROM userflags
78                         WHERE flag = ?");
79     foreach my $module (keys %sub_perms) {
80         next if exists $all_module_perms{$module};
81         foreach my $sub_perm (@{ $sub_perms{$module} }) {
82             $sth->execute($member, $sub_perm, $module);
83         }
84     }
85     
86     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
87 } else {
88
89     my $flags = $bor->{'flags'};
90     my $accessflags;
91     my $dbh = C4::Context->dbh();
92     # FIXME This needs to be improved to avoid doing the same query
93     my $sth = $dbh->prepare("select bit,flag from userflags");
94     $sth->execute;
95     while ( my ( $bit, $flag ) = $sth->fetchrow ) {
96         if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
97             $accessflags->{$flag} = 1;
98         }
99     }
100
101     my $all_perms  = get_all_subpermissions();
102     my $user_perms = get_user_subpermissions($bor->{'userid'});
103     $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
104     $sth->execute;
105     my @loop;
106
107     while (my ($bit, $flag) = $sth->fetchrow) {
108             my $checked='';
109             if ($accessflags->{$flag}) {
110                 $checked= 1;
111             }
112
113             my %row = ( bit => $bit,
114                     flag => $flag,
115                     checked => $checked,
116         );
117
118         my @sub_perm_loop = ();
119         my $expand_parent = 0;
120         if ($checked) {
121             if (exists $all_perms->{$flag}) {
122                 $expand_parent = 1;
123                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
124                     push @sub_perm_loop, {
125                         id => "${flag}_$sub_perm",
126                         perm => "$flag:$sub_perm",
127                         code => $sub_perm,
128                         checked => 1
129                     };
130                 }
131             }
132         } else {
133             if (exists $user_perms->{$flag}) {
134                 $expand_parent = 1;
135                 # put selected ones first
136                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
137                     push @sub_perm_loop, {
138                         id => "${flag}_$sub_perm",
139                         perm => "$flag:$sub_perm",
140                         code => $sub_perm,
141                         checked => 1
142                     };
143                 }
144             }
145             # then ones not selected
146             if (exists $all_perms->{$flag}) {
147                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
148                     push @sub_perm_loop, {
149                         id => "${flag}_$sub_perm",
150                         perm => "$flag:$sub_perm",
151                         code => $sub_perm,
152                         checked => 0
153                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
154                 }
155             }
156         }
157         $row{expand} = $expand_parent;
158         if ($#sub_perm_loop > -1) {
159             $row{sub_perm_loop} = \@sub_perm_loop;
160         }
161             push @loop, \%row;
162     }
163
164     if ( $bor->{'category_type'} eq 'C') {
165         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
166         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
167         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
168     }
169         
170 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
171     my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
172     $template->param( picture => 1 ) if $patron_image;
173
174 if (C4::Context->preference('ExtendedPatronAttributes')) {
175     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
176     $template->param(
177         ExtendedPatronAttributes => 1,
178         extendedattributes => $attributes
179     );
180 }
181
182 $template->param(
183                 borrowernumber => $bor->{'borrowernumber'},
184     cardnumber => $bor->{'cardnumber'},
185                 surname => $bor->{'surname'},
186                 firstname => $bor->{'firstname'},
187         othernames => $bor->{'othernames'},
188                 categorycode => $bor->{'categorycode'},
189                 category_type => $bor->{'category_type'},
190                 categoryname => $bor->{'description'},
191         address => $bor->{address},
192                 address2 => $bor->{'address2'},
193         streettype => $bor->{streettype},
194                 city => $bor->{'city'},
195         state => $bor->{'state'},
196                 zipcode => $bor->{'zipcode'},
197                 country => $bor->{'country'},
198                 phone => $bor->{'phone'},
199         phonepro => $bor->{'phonepro'},
200         mobile => $bor->{'mobile'},
201                 email => $bor->{'email'},
202         emailpro => $bor->{'emailpro'},
203                 branchcode => $bor->{'branchcode'},
204                 loop => \@loop,
205                 is_child        => ($bor->{'category_type'} eq 'C'),
206                 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
207         RoutingSerials => C4::Context->preference('RoutingSerials'),
208                 );
209
210     output_html_with_http_headers $input, $cookie, $template->output;
211
212 }