Bug 19049: Testing RecordsFromMarcPlugin with a to_marc plugin
[koha.git] / members / member-flags.pl
1 #!/usr/bin/perl
2
3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
6
7 use strict;
8 use warnings;
9
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;
17
18 use Koha::Patron::Categories;
19
20 use C4::Output;
21 use Koha::Patron::Images;
22 use Koha::Token;
23
24 my $input = new CGI;
25
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;
31 }
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 });
40
41
42 my %member2;
43 $member2{'borrowernumber'}=$member;
44
45 if ($input->param('newflags')) {
46
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         });
52
53
54     my $dbh=C4::Context->dbh();
55
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;
65         }
66     }
67
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;
75         }
76     }
77     
78     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
79     $sth->execute($module_flags, $member);
80     
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);
92         }
93     }
94     
95     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
96 } else {
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");
104     $sth->execute;
105     my @loop;
106     while (my ($bit, $flag) = $sth->fetchrow) {
107             my $checked='';
108             if ($accessflags->{$flag}) {
109                 $checked= 1;
110             }
111
112             my %row = ( bit => $bit,
113                     flag => $flag,
114                     checked => $checked,
115         );
116
117         my @sub_perm_loop = ();
118         my $expand_parent = 0;
119         if ($checked) {
120             if (exists $all_perms->{$flag}) {
121                 $expand_parent = 1;
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",
126                         code => $sub_perm,
127                         checked => 1
128                     };
129                 }
130             }
131         } else {
132             if (exists $user_perms->{$flag}) {
133                 $expand_parent = 1;
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",
139                         code => $sub_perm,
140                         checked => 1
141                     };
142                 }
143             }
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",
150                         code => $sub_perm,
151                         checked => 0
152                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
153                 }
154             }
155         }
156         $row{expand} = $expand_parent;
157         if ($#sub_perm_loop > -1) {
158             $row{sub_perm_loop} = \@sub_perm_loop;
159         }
160             push @loop, \%row;
161     }
162
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;
167     }
168         
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;
172
173 if (C4::Context->preference('ExtendedPatronAttributes')) {
174     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
175     $template->param(
176         ExtendedPatronAttributes => 1,
177         extendedattributes => $attributes
178     );
179 }
180
181 $template->param(
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'},
203                 loop => \@loop,
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'), } ),
207                 );
208
209     output_html_with_http_headers $input, $cookie, $template->output;
210
211 }