Bug 20009: use Modern::Perl in Members perl scripts
[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 Modern::Perl;
8
9 use CGI qw ( -utf8 );
10 use C4::Output;
11 use C4::Auth qw(:DEFAULT :EditPermissions);
12 use C4::Context;
13 use C4::Members;
14 use C4::Members::Attributes qw(GetBorrowerAttributes);
15 #use C4::Acquisitions;
16
17 use Koha::Patron::Categories;
18 use Koha::Patrons;
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 $patron = Koha::Patrons->find( $member );
29 unless ( $patron ) {
30     print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
31     exit;
32 }
33
34 my $category_type = $patron->category->category_type;
35 my $bor = $patron->unblessed;
36 if( $category_type eq 'S' )  {
37     $flagsrequired->{'staffaccess'} = 1;
38 }
39 my ($template, $loggedinuser, $cookie) = get_template_and_user({
40         template_name   => "members/member-flags.tt",
41         query           => $input,
42         type            => "intranet",
43         authnotrequired => 0,
44         flagsrequired   => $flagsrequired,
45         debug           => 1,
46 });
47
48
49 my %member2;
50 $member2{'borrowernumber'}=$member;
51
52 if ($input->param('newflags')) {
53
54     die "Wrong CSRF token"
55         unless Koha::Token->new->check_csrf({
56             session_id => scalar $input->cookie('CGISESSID'),
57             token  => scalar $input->param('csrf_token'),
58         });
59
60
61     my $dbh=C4::Context->dbh();
62
63     my @perms = $input->multi_param('flag');
64     my %all_module_perms = ();
65     my %sub_perms = ();
66     foreach my $perm (@perms) {
67         if ($perm !~ /:/) {
68             $all_module_perms{$perm} = 1;
69         } else {
70             my ($module, $sub_perm) = split /:/, $perm, 2;
71             push @{ $sub_perms{$module} }, $sub_perm;
72         }
73     }
74
75     # construct flags
76     my $module_flags = 0;
77     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
78     $sth->execute();
79     while (my ($bit, $flag) = $sth->fetchrow_array) {
80         if (exists $all_module_perms{$flag}) {
81             $module_flags += 2**$bit;
82         }
83     }
84     
85     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
86     $sth->execute($module_flags, $member);
87     
88     # deal with subpermissions
89     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
90     $sth->execute($member); 
91     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
92                         SELECT ?, bit, ?
93                         FROM userflags
94                         WHERE flag = ?");
95     foreach my $module (keys %sub_perms) {
96         next if exists $all_module_perms{$module};
97         foreach my $sub_perm (@{ $sub_perms{$module} }) {
98             $sth->execute($member, $sub_perm, $module);
99         }
100     }
101     
102     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
103 } else {
104
105     my $flags = C4::Members::patronflags( $bor );
106     my $accessflags;
107     my $dbh = C4::Context->dbh();
108     # FIXME This needs to be improved to avoid doing the same query
109     my $sth = $dbh->prepare("select bit,flag from userflags");
110     $sth->execute;
111     while ( my ( $bit, $flag ) = $sth->fetchrow ) {
112         if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
113             $accessflags->{$flag} = 1;
114         }
115     }
116
117     my $all_perms  = get_all_subpermissions();
118     my $user_perms = get_user_subpermissions($bor->{'userid'});
119     $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
120     $sth->execute;
121     my @loop;
122
123     while (my ($bit, $flag) = $sth->fetchrow) {
124         my $checked='';
125         if ($accessflags->{$flag}) {
126             $checked= 1;
127         }
128
129         my %row = ( bit => $bit,
130             flag => $flag,
131             checked => $checked,
132         );
133
134         my @sub_perm_loop = ();
135         my $expand_parent = 0;
136         if ($checked) {
137             if (exists $all_perms->{$flag}) {
138                 $expand_parent = 1;
139                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
140                     push @sub_perm_loop, {
141                         id => "${flag}_$sub_perm",
142                         perm => "$flag:$sub_perm",
143                         code => $sub_perm,
144                         checked => 1
145                     };
146                 }
147             }
148         } else {
149             if (exists $user_perms->{$flag}) {
150                 $expand_parent = 1;
151                 # put selected ones first
152                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
153                     push @sub_perm_loop, {
154                         id => "${flag}_$sub_perm",
155                         perm => "$flag:$sub_perm",
156                         code => $sub_perm,
157                         checked => 1
158                     };
159                 }
160             }
161             # then ones not selected
162             if (exists $all_perms->{$flag}) {
163                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
164                     push @sub_perm_loop, {
165                         id => "${flag}_$sub_perm",
166                         perm => "$flag:$sub_perm",
167                         code => $sub_perm,
168                         checked => 0
169                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
170                 }
171             }
172         }
173         $row{expand} = $expand_parent;
174         if ($#sub_perm_loop > -1) {
175             $row{sub_perm_loop} = \@sub_perm_loop;
176         }
177         push @loop, \%row;
178     }
179
180     if ( $category_type eq 'C') {
181         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
182         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
183         $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
184     }
185
186 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
187     $template->param( picture => 1 ) if $patron->image;
188
189 if (C4::Context->preference('ExtendedPatronAttributes')) {
190     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
191     $template->param(
192         ExtendedPatronAttributes => 1,
193         extendedattributes => $attributes
194     );
195 }
196
197 $template->param(
198     borrowernumber => $bor->{'borrowernumber'},
199     cardnumber     => $bor->{'cardnumber'},
200     surname        => $bor->{'surname'},
201     firstname      => $bor->{'firstname'},
202     othernames     => $bor->{'othernames'},
203     categorycode   => $bor->{'categorycode'},
204     category_type  => $category_type,
205     categoryname   => $bor->{'description'},
206     address        => $bor->{address},
207     address2       => $bor->{'address2'},
208     streettype     => $bor->{streettype},
209     city           => $bor->{'city'},
210     state          => $bor->{'state'},
211     zipcode        => $bor->{'zipcode'},
212     country        => $bor->{'country'},
213     phone          => $bor->{'phone'},
214     phonepro       => $bor->{'phonepro'},
215     mobile         => $bor->{'mobile'},
216     email          => $bor->{'email'},
217     emailpro       => $bor->{'emailpro'},
218     branchcode     => $bor->{'branchcode'},
219     loop           => \@loop,
220     is_child       => ( $category_type eq 'C' ),
221     csrf_token =>
222         Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
223 );
224
225     output_html_with_http_headers $input, $cookie, $template->output;
226
227 }