Bug 17829: Move GetMember to Koha::Patron
[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 use Koha::Patrons;
20
21 use C4::Output;
22 use Koha::Patron::Images;
23 use Koha::Token;
24
25 my $input = new CGI;
26
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;
34 }
35 my ($template, $loggedinuser, $cookie) = get_template_and_user({
36         template_name   => "members/member-flags.tt",
37         query           => $input,
38         type            => "intranet",
39         authnotrequired => 0,
40         flagsrequired   => $flagsrequired,
41         debug           => 1,
42 });
43
44
45 my %member2;
46 $member2{'borrowernumber'}=$member;
47
48 if ($input->param('newflags')) {
49
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'),
54         });
55
56
57     my $dbh=C4::Context->dbh();
58
59     my @perms = $input->multi_param('flag');
60     my %all_module_perms = ();
61     my %sub_perms = ();
62     foreach my $perm (@perms) {
63         if ($perm !~ /:/) {
64             $all_module_perms{$perm} = 1;
65         } else {
66             my ($module, $sub_perm) = split /:/, $perm, 2;
67             push @{ $sub_perms{$module} }, $sub_perm;
68         }
69     }
70
71     # construct flags
72     my $module_flags = 0;
73     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
74     $sth->execute();
75     while (my ($bit, $flag) = $sth->fetchrow_array) {
76         if (exists $all_module_perms{$flag}) {
77             $module_flags += 2**$bit;
78         }
79     }
80     
81     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
82     $sth->execute($module_flags, $member);
83     
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)
88                         SELECT ?, bit, ?
89                         FROM userflags
90                         WHERE flag = ?");
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);
95         }
96     }
97     
98     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
99 } else {
100
101     my $flags = C4::Members::patronflags( $bor );
102     my $accessflags;
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");
106     $sth->execute;
107     while ( my ( $bit, $flag ) = $sth->fetchrow ) {
108         if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
109             $accessflags->{$flag} = 1;
110         }
111     }
112
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");
116     $sth->execute;
117     my @loop;
118
119     while (my ($bit, $flag) = $sth->fetchrow) {
120             my $checked='';
121             if ($accessflags->{$flag}) {
122                 $checked= 1;
123             }
124
125             my %row = ( bit => $bit,
126                     flag => $flag,
127                     checked => $checked,
128         );
129
130         my @sub_perm_loop = ();
131         my $expand_parent = 0;
132         if ($checked) {
133             if (exists $all_perms->{$flag}) {
134                 $expand_parent = 1;
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",
139                         code => $sub_perm,
140                         checked => 1
141                     };
142                 }
143             }
144         } else {
145             if (exists $user_perms->{$flag}) {
146                 $expand_parent = 1;
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",
152                         code => $sub_perm,
153                         checked => 1
154                     };
155                 }
156             }
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",
163                         code => $sub_perm,
164                         checked => 0
165                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
166                 }
167             }
168         }
169         $row{expand} = $expand_parent;
170         if ($#sub_perm_loop > -1) {
171             $row{sub_perm_loop} = \@sub_perm_loop;
172         }
173             push @loop, \%row;
174     }
175
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;
180     }
181         
182 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
183     $template->param( picture => 1 ) if $patron->image;
184
185 if (C4::Context->preference('ExtendedPatronAttributes')) {
186     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
187     $template->param(
188         ExtendedPatronAttributes => 1,
189         extendedattributes => $attributes
190     );
191 }
192
193 $template->param(
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'},
215                 loop => \@loop,
216                 is_child        => ($category_type eq 'C'),
217         RoutingSerials => C4::Context->preference('RoutingSerials'),
218         csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
219                 );
220
221     output_html_with_http_headers $input, $cookie, $template->output;
222
223 }