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;
21 use Koha::Patron::Images;
26 my $flagsrequired = { permissions => 1 };
27 my $member=$input->param('member');
28 my $bor = GetMemberDetails( $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",
37 flagsrequired => $flagsrequired,
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'),
54 my $dbh=C4::Context->dbh();
56 my @perms = $input->multi_param('flag');
57 my %all_module_perms = ();
59 foreach my $perm (@perms) {
61 $all_module_perms{$perm} = 1;
63 my ($module, $sub_perm) = split /:/, $perm, 2;
64 push @{ $sub_perms{$module} }, $sub_perm;
70 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
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)
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");
97 # my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
98 my $flags = $bor->{'flags'};
99 my $accessflags = $bor->{'authflags'};
100 my $dbh=C4::Context->dbh();
101 my $all_perms = get_all_subpermissions();
102 my $user_perms = get_user_subpermissions($bor->{'userid'});
103 my $sth=$dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
106 while (my ($bit, $flag) = $sth->fetchrow) {
108 if ($accessflags->{$flag}) {
112 my %row = ( bit => $bit,
117 my @sub_perm_loop = ();
118 my $expand_parent = 0;
120 if (exists $all_perms->{$flag}) {
122 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
123 push @sub_perm_loop, {
124 id => "${flag}_$sub_perm",
125 perm => "$flag:$sub_perm",
132 if (exists $user_perms->{$flag}) {
134 # put selected ones first
135 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
136 push @sub_perm_loop, {
137 id => "${flag}_$sub_perm",
138 perm => "$flag:$sub_perm",
144 # then ones not selected
145 if (exists $all_perms->{$flag}) {
146 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
147 push @sub_perm_loop, {
148 id => "${flag}_$sub_perm",
149 perm => "$flag:$sub_perm",
152 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
156 $row{expand} = $expand_parent;
157 if ($#sub_perm_loop > -1) {
158 $row{sub_perm_loop} = \@sub_perm_loop;
163 if ( $bor->{'category_type'} eq 'C') {
164 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
165 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
166 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
169 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' || $bor->{'category_type'} eq 'I' );
170 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
171 $template->param( picture => 1 ) if $patron_image;
173 if (C4::Context->preference('ExtendedPatronAttributes')) {
174 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
176 ExtendedPatronAttributes => 1,
177 extendedattributes => $attributes
182 borrowernumber => $bor->{'borrowernumber'},
183 cardnumber => $bor->{'cardnumber'},
184 surname => $bor->{'surname'},
185 firstname => $bor->{'firstname'},
186 othernames => $bor->{'othernames'},
187 categorycode => $bor->{'categorycode'},
188 category_type => $bor->{'category_type'},
189 categoryname => $bor->{'description'},
190 address => $bor->{address},
191 address2 => $bor->{'address2'},
192 streettype => $bor->{streettype},
193 city => $bor->{'city'},
194 state => $bor->{'state'},
195 zipcode => $bor->{'zipcode'},
196 country => $bor->{'country'},
197 phone => $bor->{'phone'},
198 phonepro => $bor->{'phonepro'},
199 mobile => $bor->{'mobile'},
200 email => $bor->{'email'},
201 emailpro => $bor->{'emailpro'},
202 branchcode => $bor->{'branchcode'},
204 is_child => ($bor->{'category_type'} eq 'C'),
205 RoutingSerials => C4::Context->preference('RoutingSerials'),
206 csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
209 output_html_with_http_headers $input, $cookie, $template->output;