Bug 12478: Take the FacetMaxCount pref into account
[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::Branch;
16 use C4::Members::Attributes qw(GetBorrowerAttributes);
17 #use C4::Acquisitions;
18
19 use C4::Output;
20 use Koha::Patron::Images;
21
22 my $input = new CGI;
23
24 my $flagsrequired = { permissions => 1 };
25 my $member=$input->param('member');
26 my $bor = GetMemberDetails( $member,'');
27 if( $bor->{'category_type'} eq 'S' )  {
28         $flagsrequired->{'staffaccess'} = 1;
29 }
30 my ($template, $loggedinuser, $cookie) = get_template_and_user({
31         template_name   => "members/member-flags.tt",
32         query           => $input,
33         type            => "intranet",
34         authnotrequired => 0,
35         flagsrequired   => $flagsrequired,
36         debug           => 1,
37 });
38
39
40 my %member2;
41 $member2{'borrowernumber'}=$member;
42
43 if ($input->param('newflags')) {
44     my $dbh=C4::Context->dbh();
45
46     my @perms = $input->param('flag');
47     my %all_module_perms = ();
48     my %sub_perms = ();
49     foreach my $perm (@perms) {
50         if ($perm !~ /:/) {
51             $all_module_perms{$perm} = 1;
52         } else {
53             my ($module, $sub_perm) = split /:/, $perm, 2;
54             push @{ $sub_perms{$module} }, $sub_perm;
55         }
56     }
57
58     # construct flags
59     my $module_flags = 0;
60     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
61     $sth->execute();
62     while (my ($bit, $flag) = $sth->fetchrow_array) {
63         if (exists $all_module_perms{$flag}) {
64             $module_flags += 2**$bit;
65         }
66     }
67     
68     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
69     $sth->execute($module_flags, $member);
70     
71     # deal with subpermissions
72     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
73     $sth->execute($member); 
74     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
75                         SELECT ?, bit, ?
76                         FROM userflags
77                         WHERE flag = ?");
78     foreach my $module (keys %sub_perms) {
79         next if exists $all_module_perms{$module};
80         foreach my $sub_perm (@{ $sub_perms{$module} }) {
81             $sth->execute($member, $sub_perm, $module);
82         }
83     }
84     
85     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
86 } else {
87 #     my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
88     my $flags = $bor->{'flags'};
89     my $accessflags = $bor->{'authflags'};
90     my $dbh=C4::Context->dbh();
91     my $all_perms  = get_all_subpermissions();
92     my $user_perms = get_user_subpermissions($bor->{'userid'});
93     my $sth=$dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
94     $sth->execute;
95     my @loop;
96     while (my ($bit, $flag) = $sth->fetchrow) {
97             my $checked='';
98             if ($accessflags->{$flag}) {
99                 $checked= 1;
100             }
101
102             my %row = ( bit => $bit,
103                     flag => $flag,
104                     checked => $checked,
105         );
106
107         my @sub_perm_loop = ();
108         my $expand_parent = 0;
109         if ($checked) {
110             if (exists $all_perms->{$flag}) {
111                 $expand_parent = 1;
112                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
113                     push @sub_perm_loop, {
114                         id => "${flag}_$sub_perm",
115                         perm => "$flag:$sub_perm",
116                         code => $sub_perm,
117                         checked => 1
118                     };
119                 }
120             }
121         } else {
122             if (exists $user_perms->{$flag}) {
123                 $expand_parent = 1;
124                 # put selected ones first
125                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
126                     push @sub_perm_loop, {
127                         id => "${flag}_$sub_perm",
128                         perm => "$flag:$sub_perm",
129                         code => $sub_perm,
130                         checked => 1
131                     };
132                 }
133             }
134             # then ones not selected
135             if (exists $all_perms->{$flag}) {
136                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
137                     push @sub_perm_loop, {
138                         id => "${flag}_$sub_perm",
139                         perm => "$flag:$sub_perm",
140                         code => $sub_perm,
141                         checked => 0
142                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
143                 }
144             }
145         }
146         $row{expand} = $expand_parent;
147         if ($#sub_perm_loop > -1) {
148             $row{sub_perm_loop} = \@sub_perm_loop;
149         }
150             push @loop, \%row;
151     }
152
153     if ( $bor->{'category_type'} eq 'C') {
154         my  ( $catcodes, $labels ) =  GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
155         my $cnt = scalar(@$catcodes);
156         $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
157         $template->param( 'catcode' =>    $catcodes->[0])  if $cnt == 1;
158     }
159         
160 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
161     my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
162     $template->param( picture => 1 ) if $patron_image;
163
164 if (C4::Context->preference('ExtendedPatronAttributes')) {
165     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
166     $template->param(
167         ExtendedPatronAttributes => 1,
168         extendedattributes => $attributes
169     );
170 }
171
172 $template->param(
173                 borrowernumber => $bor->{'borrowernumber'},
174     cardnumber => $bor->{'cardnumber'},
175                 surname => $bor->{'surname'},
176                 firstname => $bor->{'firstname'},
177         othernames => $bor->{'othernames'},
178                 categorycode => $bor->{'categorycode'},
179                 category_type => $bor->{'category_type'},
180                 categoryname => $bor->{'description'},
181         address => $bor->{address},
182                 address2 => $bor->{'address2'},
183         streettype => $bor->{streettype},
184                 city => $bor->{'city'},
185         state => $bor->{'state'},
186                 zipcode => $bor->{'zipcode'},
187                 country => $bor->{'country'},
188                 phone => $bor->{'phone'},
189         phonepro => $bor->{'phonepro'},
190         mobile => $bor->{'mobile'},
191                 email => $bor->{'email'},
192         emailpro => $bor->{'emailpro'},
193                 branchcode => $bor->{'branchcode'},
194                 branchname => GetBranchName($bor->{'branchcode'}),
195                 loop => \@loop,
196                 is_child        => ($bor->{'category_type'} eq 'C'),
197                 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
198         RoutingSerials => C4::Context->preference('RoutingSerials'),
199                 );
200
201     output_html_with_http_headers $input, $cookie, $template->output;
202
203 }