Bug 15758: Koha::Libraries - Ultimate duel for C4::Branch
[koha-equinox.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
23 my $input = new CGI;
24
25 my $flagsrequired = { permissions => 1 };
26 my $member=$input->param('member');
27 my $bor = GetMemberDetails( $member,'');
28 if( $bor->{'category_type'} eq 'S' )  {
29         $flagsrequired->{'staffaccess'} = 1;
30 }
31 my ($template, $loggedinuser, $cookie) = get_template_and_user({
32         template_name   => "members/member-flags.tt",
33         query           => $input,
34         type            => "intranet",
35         authnotrequired => 0,
36         flagsrequired   => $flagsrequired,
37         debug           => 1,
38 });
39
40
41 my %member2;
42 $member2{'borrowernumber'}=$member;
43
44 if ($input->param('newflags')) {
45     my $dbh=C4::Context->dbh();
46
47     my @perms = $input->multi_param('flag');
48     my %all_module_perms = ();
49     my %sub_perms = ();
50     foreach my $perm (@perms) {
51         if ($perm !~ /:/) {
52             $all_module_perms{$perm} = 1;
53         } else {
54             my ($module, $sub_perm) = split /:/, $perm, 2;
55             push @{ $sub_perms{$module} }, $sub_perm;
56         }
57     }
58
59     # construct flags
60     my $module_flags = 0;
61     my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
62     $sth->execute();
63     while (my ($bit, $flag) = $sth->fetchrow_array) {
64         if (exists $all_module_perms{$flag}) {
65             $module_flags += 2**$bit;
66         }
67     }
68     
69     $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
70     $sth->execute($module_flags, $member);
71     
72     # deal with subpermissions
73     $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
74     $sth->execute($member); 
75     $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
76                         SELECT ?, bit, ?
77                         FROM userflags
78                         WHERE flag = ?");
79     foreach my $module (keys %sub_perms) {
80         next if exists $all_module_perms{$module};
81         foreach my $sub_perm (@{ $sub_perms{$module} }) {
82             $sth->execute($member, $sub_perm, $module);
83         }
84     }
85     
86     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
87 } else {
88 #     my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
89     my $flags = $bor->{'flags'};
90     my $accessflags = $bor->{'authflags'};
91     my $dbh=C4::Context->dbh();
92     my $all_perms  = get_all_subpermissions();
93     my $user_perms = get_user_subpermissions($bor->{'userid'});
94     my $sth=$dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
95     $sth->execute;
96     my @loop;
97     while (my ($bit, $flag) = $sth->fetchrow) {
98             my $checked='';
99             if ($accessflags->{$flag}) {
100                 $checked= 1;
101             }
102
103             my %row = ( bit => $bit,
104                     flag => $flag,
105                     checked => $checked,
106         );
107
108         my @sub_perm_loop = ();
109         my $expand_parent = 0;
110         if ($checked) {
111             if (exists $all_perms->{$flag}) {
112                 $expand_parent = 1;
113                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
114                     push @sub_perm_loop, {
115                         id => "${flag}_$sub_perm",
116                         perm => "$flag:$sub_perm",
117                         code => $sub_perm,
118                         checked => 1
119                     };
120                 }
121             }
122         } else {
123             if (exists $user_perms->{$flag}) {
124                 $expand_parent = 1;
125                 # put selected ones first
126                 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
127                     push @sub_perm_loop, {
128                         id => "${flag}_$sub_perm",
129                         perm => "$flag:$sub_perm",
130                         code => $sub_perm,
131                         checked => 1
132                     };
133                 }
134             }
135             # then ones not selected
136             if (exists $all_perms->{$flag}) {
137                 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
138                     push @sub_perm_loop, {
139                         id => "${flag}_$sub_perm",
140                         perm => "$flag:$sub_perm",
141                         code => $sub_perm,
142                         checked => 0
143                     } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
144                 }
145             }
146         }
147         $row{expand} = $expand_parent;
148         if ($#sub_perm_loop > -1) {
149             $row{sub_perm_loop} = \@sub_perm_loop;
150         }
151             push @loop, \%row;
152     }
153
154     if ( $bor->{'category_type'} eq 'C') {
155         my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
156         $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
157         $template->param( 'catcode' => $patron_categories->next )  if $patron_categories->count == 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                 loop => \@loop,
195                 is_child        => ($bor->{'category_type'} eq 'C'),
196                 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
197         RoutingSerials => C4::Context->preference('RoutingSerials'),
198                 );
199
200     output_html_with_http_headers $input, $cookie, $template->output;
201
202 }