3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
12 use C4::Auth qw(:DEFAULT :EditPermissions);
15 use C4::Members::Attributes qw(GetBorrowerAttributes);
16 #use C4::Acquisitions;
18 use Koha::Patron::Categories;
22 use Koha::Patron::Images;
27 my $flagsrequired = { permissions => 1 };
28 my $member=$input->param('member');
29 my $patron = Koha::Patrons->find( $member );
30 my $category_type = $patron->category->category_type;
31 my $bor = $patron->unblessed;
32 if( $category_type eq 'S' ) {
33 $flagsrequired->{'staffaccess'} = 1;
35 my ($template, $loggedinuser, $cookie) = get_template_and_user({
36 template_name => "members/member-flags.tt",
40 flagsrequired => $flagsrequired,
46 $member2{'borrowernumber'}=$member;
48 if ($input->param('newflags')) {
50 die "Wrong CSRF token"
51 unless Koha::Token->new->check_csrf({
52 session_id => scalar $input->cookie('CGISESSID'),
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 ( $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 ( $category_type =~ /^(A|I)$/ );
183 $template->param( picture => 1 ) if $patron->image;
185 if (C4::Context->preference('ExtendedPatronAttributes')) {
186 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
188 ExtendedPatronAttributes => 1,
189 extendedattributes => $attributes
194 borrowernumber => $bor->{'borrowernumber'},
195 cardnumber => $bor->{'cardnumber'},
196 surname => $bor->{'surname'},
197 firstname => $bor->{'firstname'},
198 othernames => $bor->{'othernames'},
199 categorycode => $bor->{'categorycode'},
200 category_type => $category_type,
201 categoryname => $bor->{'description'},
202 address => $bor->{address},
203 address2 => $bor->{'address2'},
204 streettype => $bor->{streettype},
205 city => $bor->{'city'},
206 state => $bor->{'state'},
207 zipcode => $bor->{'zipcode'},
208 country => $bor->{'country'},
209 phone => $bor->{'phone'},
210 phonepro => $bor->{'phonepro'},
211 mobile => $bor->{'mobile'},
212 email => $bor->{'email'},
213 emailpro => $bor->{'emailpro'},
214 branchcode => $bor->{'branchcode'},
216 is_child => ( $category_type eq 'C' ),
217 RoutingSerials => C4::Context->preference('RoutingSerials'),
219 Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
222 output_html_with_http_headers $input, $cookie, $template->output;