3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
11 use Digest
::MD5
qw(md5_base64);
12 use Encode
qw( encode );
14 use C4
::Auth
qw(:DEFAULT :EditPermissions);
17 use C4
::Members
::Attributes
qw(GetBorrowerAttributes);
18 #use C4::Acquisitions;
20 use Koha
::Patron
::Categories
;
23 use Koha
::Patron
::Images
;
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",
39 flagsrequired
=> $flagsrequired,
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'),
57 my $dbh=C4
::Context
->dbh();
59 my @perms = $input->multi_param('flag');
60 my %all_module_perms = ();
62 foreach my $perm (@perms) {
64 $all_module_perms{$perm} = 1;
66 my ($module, $sub_perm) = split /:/, $perm, 2;
67 push @
{ $sub_perms{$module} }, $sub_perm;
73 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
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)
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");
101 my $flags = C4
::Members
::patronflags
( $bor );
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");
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");
119 while (my ($bit, $flag) = $sth->fetchrow) {
121 if ($accessflags->{$flag}) {
125 my %row = ( bit
=> $bit,
130 my @sub_perm_loop = ();
131 my $expand_parent = 0;
133 if (exists $all_perms->{$flag}) {
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",
145 if (exists $user_perms->{$flag}) {
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",
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",
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;
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'});
189 ExtendedPatronAttributes
=> 1,
190 extendedattributes
=> $attributes
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'},
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;