Bug 15758: Koha::Libraries - Remove GetBranchName
[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 Koha::Patron::Categories;
20
21 use C4::Output;
22 use Koha::Patron::Images;
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     my $dbh=C4::Context->dbh();
47
48     my @perms = $input->multi_param('flag');
49     my %all_module_perms = ();
50     my %sub_perms = ();
51     foreach my $perm (@perms) {
52         if ($perm !~ /:/) {
53             $all_module_perms{$perm} = 1;
54         } else {
55             my ($module, $sub_perm) = split /:/, $perm, 2;
56             push @{ $sub_perms{$module} }, $sub_perm;
57         }
58     }
59
60     # construct flags
61     my $module_flags = 0;
62     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
63     $sth->execute();
64     while (my ($bit, $flag) = $sth->fetchrow_array) {
65         if (exists $all_module_perms{$flag}) {
66             $module_flags += 2**$bit;
67         }
68     }
69     
70     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
71     $sth->execute($module_flags, $member);
72     
73     # deal with subpermissions
74     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
75     $sth->execute($member); 
76     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
77                         SELECT ?, bit, ?
78                         FROM userflags
79                         WHERE flag = ?");
80     foreach my $module (keys %sub_perms) {
81         next if exists $all_module_perms{$module};
82         foreach my $sub_perm (@{ $sub_perms{$module} }) {
83             $sth->execute($member, $sub_perm, $module);
84         }
85     }
86     
87     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
88 } else {
89 #     my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
90     my $flags = $bor->{'flags'};
91     my $accessflags = $bor->{'authflags'};
92     my $dbh=C4::Context->dbh();
93     my $all_perms  = get_all_subpermissions();
94     my $user_perms = get_user_subpermissions($bor->{'userid'});
95     my $sth=$dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
96     $sth->execute;
97     my @loop;
98     while (my ($bit, $flag) = $sth->fetchrow) {
99             my $checked='';
100             if ($accessflags->{$flag}) {
101                 $checked= 1;
102             }
103
104             my %row = ( bit => $bit,
105                     flag => $flag,
106                     checked => $checked,
107         );
108
109         my @sub_perm_loop = ();
110         my $expand_parent = 0;
111         if ($checked) {
112             if (exists $all_perms->{$flag}) {
113                 $expand_parent = 1;
114                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
115                     push @sub_perm_loop, {
116                         id => "${flag}_$sub_perm",
117                         perm => "$flag:$sub_perm",
118                         code => $sub_perm,
119                         checked => 1
120                     };
121                 }
122             }
123         } else {
124             if (exists $user_perms->{$flag}) {
125                 $expand_parent = 1;
126                 # put selected ones first
127                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
128                     push @sub_perm_loop, {
129                         id => "${flag}_$sub_perm",
130                         perm => "$flag:$sub_perm",
131                         code => $sub_perm,
132                         checked => 1
133                     };
134                 }
135             }
136             # then ones not selected
137             if (exists $all_perms->{$flag}) {
138                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
139                     push @sub_perm_loop, {
140                         id => "${flag}_$sub_perm",
141                         perm => "$flag:$sub_perm",
142                         code => $sub_perm,
143                         checked => 0
144                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
145                 }
146             }
147         }
148         $row{expand} = $expand_parent;
149         if ($#sub_perm_loop > -1) {
150             $row{sub_perm_loop} = \@sub_perm_loop;
151         }
152             push @loop, \%row;
153     }
154
155     if ( $bor->{'category_type'} eq 'C') {
156         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
157         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
158         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 1;
159     }
160         
161 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
162     my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
163     $template->param( picture => 1 ) if $patron_image;
164
165 if (C4::Context->preference('ExtendedPatronAttributes')) {
166     my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
167     $template->param(
168         ExtendedPatronAttributes => 1,
169         extendedattributes => $attributes
170     );
171 }
172
173 $template->param(
174                 borrowernumber => $bor->{'borrowernumber'},
175     cardnumber => $bor->{'cardnumber'},
176                 surname => $bor->{'surname'},
177                 firstname => $bor->{'firstname'},
178         othernames => $bor->{'othernames'},
179                 categorycode => $bor->{'categorycode'},
180                 category_type => $bor->{'category_type'},
181                 categoryname => $bor->{'description'},
182         address => $bor->{address},
183                 address2 => $bor->{'address2'},
184         streettype => $bor->{streettype},
185                 city => $bor->{'city'},
186         state => $bor->{'state'},
187                 zipcode => $bor->{'zipcode'},
188                 country => $bor->{'country'},
189                 phone => $bor->{'phone'},
190         phonepro => $bor->{'phonepro'},
191         mobile => $bor->{'mobile'},
192                 email => $bor->{'email'},
193         emailpro => $bor->{'emailpro'},
194                 branchcode => $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 }