Bug 18370: Use splice instead of splice
[koha.git] / members / member-flags.pl
blob95f533017dc138c0d542f8346ef09c658dd2c79d
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 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;
18 use Koha::Patron::Categories;
20 use C4::Output;
21 use Koha::Patron::Images;
22 use Koha::Token;
24 my $input = new CGI;
26 my $flagsrequired = { permissions => 1 };
27 my $member=$input->param('member');
28 my $bor = GetMember( borrowernumber => $member );
29 if( $bor->{'category_type'} eq 'S' ) {
30 $flagsrequired->{'staffaccess'} = 1;
32 my ($template, $loggedinuser, $cookie) = get_template_and_user({
33 template_name => "members/member-flags.tt",
34 query => $input,
35 type => "intranet",
36 authnotrequired => 0,
37 flagsrequired => $flagsrequired,
38 debug => 1,
39 });
42 my %member2;
43 $member2{'borrowernumber'}=$member;
45 if ($input->param('newflags')) {
47 die "Wrong CSRF token"
48 unless Koha::Token->new->check_csrf({
49 session_id => scalar $input->cookie('CGISESSID'),
50 token => scalar $input->param('csrf_token'),
51 });
54 my $dbh=C4::Context->dbh();
56 my @perms = $input->multi_param('flag');
57 my %all_module_perms = ();
58 my %sub_perms = ();
59 foreach my $perm (@perms) {
60 if ($perm !~ /:/) {
61 $all_module_perms{$perm} = 1;
62 } else {
63 my ($module, $sub_perm) = split /:/, $perm, 2;
64 push @{ $sub_perms{$module} }, $sub_perm;
68 # construct flags
69 my $module_flags = 0;
70 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
71 $sth->execute();
72 while (my ($bit, $flag) = $sth->fetchrow_array) {
73 if (exists $all_module_perms{$flag}) {
74 $module_flags += 2**$bit;
78 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
79 $sth->execute($module_flags, $member);
81 # deal with subpermissions
82 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
83 $sth->execute($member);
84 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
85 SELECT ?, bit, ?
86 FROM userflags
87 WHERE flag = ?");
88 foreach my $module (keys %sub_perms) {
89 next if exists $all_module_perms{$module};
90 foreach my $sub_perm (@{ $sub_perms{$module} }) {
91 $sth->execute($member, $sub_perm, $module);
95 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
96 } else {
98 my $flags = C4::Members::patronflags( $bor );
99 my $accessflags;
100 my $dbh = C4::Context->dbh();
101 # FIXME This needs to be improved to avoid doing the same query
102 my $sth = $dbh->prepare("select bit,flag from userflags");
103 $sth->execute;
104 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
105 if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
106 $accessflags->{$flag} = 1;
110 my $all_perms = get_all_subpermissions();
111 my $user_perms = get_user_subpermissions($bor->{'userid'});
112 $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
113 $sth->execute;
114 my @loop;
116 while (my ($bit, $flag) = $sth->fetchrow) {
117 my $checked='';
118 if ($accessflags->{$flag}) {
119 $checked= 1;
122 my %row = ( bit => $bit,
123 flag => $flag,
124 checked => $checked,
127 my @sub_perm_loop = ();
128 my $expand_parent = 0;
129 if ($checked) {
130 if (exists $all_perms->{$flag}) {
131 $expand_parent = 1;
132 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
133 push @sub_perm_loop, {
134 id => "${flag}_$sub_perm",
135 perm => "$flag:$sub_perm",
136 code => $sub_perm,
137 checked => 1
141 } else {
142 if (exists $user_perms->{$flag}) {
143 $expand_parent = 1;
144 # put selected ones first
145 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
146 push @sub_perm_loop, {
147 id => "${flag}_$sub_perm",
148 perm => "$flag:$sub_perm",
149 code => $sub_perm,
150 checked => 1
154 # then ones not selected
155 if (exists $all_perms->{$flag}) {
156 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
157 push @sub_perm_loop, {
158 id => "${flag}_$sub_perm",
159 perm => "$flag:$sub_perm",
160 code => $sub_perm,
161 checked => 0
162 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
166 $row{expand} = $expand_parent;
167 if ($#sub_perm_loop > -1) {
168 $row{sub_perm_loop} = \@sub_perm_loop;
170 push @loop, \%row;
173 if ( $bor->{'category_type'} eq 'C') {
174 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
175 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
176 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
179 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' || $bor->{'category_type'} eq 'I' );
180 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
181 $template->param( picture => 1 ) if $patron_image;
183 if (C4::Context->preference('ExtendedPatronAttributes')) {
184 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
185 $template->param(
186 ExtendedPatronAttributes => 1,
187 extendedattributes => $attributes
191 $template->param(
192 borrowernumber => $bor->{'borrowernumber'},
193 cardnumber => $bor->{'cardnumber'},
194 surname => $bor->{'surname'},
195 firstname => $bor->{'firstname'},
196 othernames => $bor->{'othernames'},
197 categorycode => $bor->{'categorycode'},
198 category_type => $bor->{'category_type'},
199 categoryname => $bor->{'description'},
200 address => $bor->{address},
201 address2 => $bor->{'address2'},
202 streettype => $bor->{streettype},
203 city => $bor->{'city'},
204 state => $bor->{'state'},
205 zipcode => $bor->{'zipcode'},
206 country => $bor->{'country'},
207 phone => $bor->{'phone'},
208 phonepro => $bor->{'phonepro'},
209 mobile => $bor->{'mobile'},
210 email => $bor->{'email'},
211 emailpro => $bor->{'emailpro'},
212 branchcode => $bor->{'branchcode'},
213 loop => \@loop,
214 is_child => ($bor->{'category_type'} eq 'C'),
215 RoutingSerials => C4::Context->preference('RoutingSerials'),
216 csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
219 output_html_with_http_headers $input, $cookie, $template->output;