BUG 8010: Correct a few syntax errors, and moved tests
[koha.git] / members / member-flags.pl
blobd54984adb9a5ddb587adfabcd8f2138a3aa3acfa
1 #!/usr/bin/perl
3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
7 use strict;
8 use warnings;
10 use CGI qw ( -utf8 );
11 use Digest::MD5 qw(md5_base64);
12 use Encode qw( encode );
13 use C4::Output;
14 use C4::Auth qw(:DEFAULT :EditPermissions);
15 use C4::Context;
16 use C4::Members;
17 use C4::Members::Attributes qw(GetBorrowerAttributes);
18 #use C4::Acquisitions;
20 use Koha::Patron::Categories;
22 use C4::Output;
23 use Koha::Patron::Images;
24 use Koha::Token;
26 my $input = new CGI;
28 my $flagsrequired = { permissions => 1 };
29 my $member=$input->param('member');
30 my $bor = GetMember( borrowernumber => $member );
31 if( $bor->{'category_type'} eq 'S' ) {
32 $flagsrequired->{'staffaccess'} = 1;
34 my ($template, $loggedinuser, $cookie) = get_template_and_user({
35 template_name => "members/member-flags.tt",
36 query => $input,
37 type => "intranet",
38 authnotrequired => 0,
39 flagsrequired => $flagsrequired,
40 debug => 1,
41 });
44 my %member2;
45 $member2{'borrowernumber'}=$member;
47 if ($input->param('newflags')) {
49 die "Wrong CSRF token"
50 unless Koha::Token->new->check_csrf({
51 id => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
52 secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
53 token => scalar $input->param('csrf_token'),
54 });
57 my $dbh=C4::Context->dbh();
59 my @perms = $input->multi_param('flag');
60 my %all_module_perms = ();
61 my %sub_perms = ();
62 foreach my $perm (@perms) {
63 if ($perm !~ /:/) {
64 $all_module_perms{$perm} = 1;
65 } else {
66 my ($module, $sub_perm) = split /:/, $perm, 2;
67 push @{ $sub_perms{$module} }, $sub_perm;
71 # construct flags
72 my $module_flags = 0;
73 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
74 $sth->execute();
75 while (my ($bit, $flag) = $sth->fetchrow_array) {
76 if (exists $all_module_perms{$flag}) {
77 $module_flags += 2**$bit;
81 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
82 $sth->execute($module_flags, $member);
84 # deal with subpermissions
85 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
86 $sth->execute($member);
87 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
88 SELECT ?, bit, ?
89 FROM userflags
90 WHERE flag = ?");
91 foreach my $module (keys %sub_perms) {
92 next if exists $all_module_perms{$module};
93 foreach my $sub_perm (@{ $sub_perms{$module} }) {
94 $sth->execute($member, $sub_perm, $module);
98 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
99 } else {
101 my $flags = C4::Members::patronflags( $bor );
102 my $accessflags;
103 my $dbh = C4::Context->dbh();
104 # FIXME This needs to be improved to avoid doing the same query
105 my $sth = $dbh->prepare("select bit,flag from userflags");
106 $sth->execute;
107 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
108 if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
109 $accessflags->{$flag} = 1;
113 my $all_perms = get_all_subpermissions();
114 my $user_perms = get_user_subpermissions($bor->{'userid'});
115 $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
116 $sth->execute;
117 my @loop;
119 while (my ($bit, $flag) = $sth->fetchrow) {
120 my $checked='';
121 if ($accessflags->{$flag}) {
122 $checked= 1;
125 my %row = ( bit => $bit,
126 flag => $flag,
127 checked => $checked,
130 my @sub_perm_loop = ();
131 my $expand_parent = 0;
132 if ($checked) {
133 if (exists $all_perms->{$flag}) {
134 $expand_parent = 1;
135 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
136 push @sub_perm_loop, {
137 id => "${flag}_$sub_perm",
138 perm => "$flag:$sub_perm",
139 code => $sub_perm,
140 checked => 1
144 } else {
145 if (exists $user_perms->{$flag}) {
146 $expand_parent = 1;
147 # put selected ones first
148 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
149 push @sub_perm_loop, {
150 id => "${flag}_$sub_perm",
151 perm => "$flag:$sub_perm",
152 code => $sub_perm,
153 checked => 1
157 # then ones not selected
158 if (exists $all_perms->{$flag}) {
159 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
160 push @sub_perm_loop, {
161 id => "${flag}_$sub_perm",
162 perm => "$flag:$sub_perm",
163 code => $sub_perm,
164 checked => 0
165 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
169 $row{expand} = $expand_parent;
170 if ($#sub_perm_loop > -1) {
171 $row{sub_perm_loop} = \@sub_perm_loop;
173 push @loop, \%row;
176 if ( $bor->{'category_type'} eq 'C') {
177 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
178 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
179 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
182 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
183 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
184 $template->param( picture => 1 ) if $patron_image;
186 if (C4::Context->preference('ExtendedPatronAttributes')) {
187 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
188 $template->param(
189 ExtendedPatronAttributes => 1,
190 extendedattributes => $attributes
194 $template->param(
195 borrowernumber => $bor->{'borrowernumber'},
196 cardnumber => $bor->{'cardnumber'},
197 surname => $bor->{'surname'},
198 firstname => $bor->{'firstname'},
199 othernames => $bor->{'othernames'},
200 categorycode => $bor->{'categorycode'},
201 category_type => $bor->{'category_type'},
202 categoryname => $bor->{'description'},
203 address => $bor->{address},
204 address2 => $bor->{'address2'},
205 streettype => $bor->{streettype},
206 city => $bor->{'city'},
207 state => $bor->{'state'},
208 zipcode => $bor->{'zipcode'},
209 country => $bor->{'country'},
210 phone => $bor->{'phone'},
211 phonepro => $bor->{'phonepro'},
212 mobile => $bor->{'mobile'},
213 email => $bor->{'email'},
214 emailpro => $bor->{'emailpro'},
215 branchcode => $bor->{'branchcode'},
216 loop => \@loop,
217 is_child => ($bor->{'category_type'} eq 'C'),
218 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
219 RoutingSerials => C4::Context->preference('RoutingSerials'),
220 csrf_token => Koha::Token->new->generate_csrf(
221 { id => Encode::encode( 'UTF-8', C4::Context->userenv->{id} ),
222 secret => md5_base64( Encode::encode( 'UTF-8', C4::Context->config('pass') ) ),
227 output_html_with_http_headers $input, $cookie, $template->output;