Bug 12250: DBRev 3.17.00.049
[koha.git] / members / member-flags.pl
blobab96951ca9a9499b95f2c9a67ed768109a707bc8
1 #!/usr/bin/perl
3 # script to edit a member's flags
4 # Written by Steve Tonnesen
5 # July 26, 2002 (my birthday!)
7 use strict;
8 use warnings;
10 use CGI;
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;
19 use C4::Output;
21 my $input = new CGI;
23 my $flagsrequired = { permissions => 1 };
24 my $member=$input->param('member');
25 my $bor = GetMemberDetails( $member,'');
26 if( $bor->{'category_type'} eq 'S' ) {
27 $flagsrequired->{'staffaccess'} = 1;
29 my ($template, $loggedinuser, $cookie) = get_template_and_user({
30 template_name => "members/member-flags.tt",
31 query => $input,
32 type => "intranet",
33 authnotrequired => 0,
34 flagsrequired => $flagsrequired,
35 debug => 1,
36 });
39 my %member2;
40 $member2{'borrowernumber'}=$member;
42 if ($input->param('newflags')) {
43 my $dbh=C4::Context->dbh();
45 my @perms = $input->param('flag');
46 my %all_module_perms = ();
47 my %sub_perms = ();
48 foreach my $perm (@perms) {
49 if ($perm !~ /:/) {
50 $all_module_perms{$perm} = 1;
51 } else {
52 my ($module, $sub_perm) = split /:/, $perm, 2;
53 push @{ $sub_perms{$module} }, $sub_perm;
57 # construct flags
58 my $module_flags = 0;
59 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
60 $sth->execute();
61 while (my ($bit, $flag) = $sth->fetchrow_array) {
62 if (exists $all_module_perms{$flag}) {
63 $module_flags += 2**$bit;
67 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
68 $sth->execute($module_flags, $member);
70 # deal with subpermissions
71 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
72 $sth->execute($member);
73 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
74 SELECT ?, bit, ?
75 FROM userflags
76 WHERE flag = ?");
77 foreach my $module (keys %sub_perms) {
78 next if exists $all_module_perms{$module};
79 foreach my $sub_perm (@{ $sub_perms{$module} }) {
80 $sth->execute($member, $sub_perm, $module);
84 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
85 } else {
86 # my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
87 my $flags = $bor->{'flags'};
88 my $accessflags = $bor->{'authflags'};
89 my $dbh=C4::Context->dbh();
90 my $all_perms = get_all_subpermissions();
91 my $user_perms = get_user_subpermissions($bor->{'userid'});
92 my $sth=$dbh->prepare("SELECT bit,flag,flagdesc FROM userflags ORDER BY bit");
93 $sth->execute;
94 my @loop;
95 while (my ($bit, $flag, $flagdesc) = $sth->fetchrow) {
96 my $checked='';
97 if ($accessflags->{$flag}) {
98 $checked= 1;
101 my %row = ( bit => $bit,
102 flag => $flag,
103 checked => $checked,
104 flagdesc => $flagdesc );
106 my @sub_perm_loop = ();
107 my $expand_parent = 0;
108 if ($checked) {
109 if (exists $all_perms->{$flag}) {
110 $expand_parent = 1;
111 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
112 push @sub_perm_loop, {
113 id => "${flag}_$sub_perm",
114 perm => "$flag:$sub_perm",
115 code => $sub_perm,
116 description => $all_perms->{$flag}->{$sub_perm},
117 checked => 1
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 description => $all_perms->{$flag}->{$sub_perm},
131 checked => 1
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 description => $all_perms->{$flag}->{$sub_perm},
143 checked => 0
144 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
148 $row{expand} = $expand_parent;
149 if ($#sub_perm_loop > -1) {
150 $row{sub_perm_loop} = \@sub_perm_loop;
152 push @loop, \%row;
155 if ( $bor->{'category_type'} eq 'C') {
156 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
157 my $cnt = scalar(@$catcodes);
158 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
159 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
162 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
163 my ($picture, $dberror) = GetPatronImage($bor->{'borrowernumber'});
164 $template->param( picture => 1 ) if $picture;
166 if (C4::Context->preference('ExtendedPatronAttributes')) {
167 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
168 $template->param(
169 ExtendedPatronAttributes => 1,
170 extendedattributes => $attributes
174 # Computes full borrower address
175 my $roadtype = C4::Koha::GetAuthorisedValueByCode( 'ROADTYPE', $bor->{streettype} );
176 my $address = $bor->{'streetnumber'} . " $roadtype " . $bor->{'address'};
178 $template->param(
179 borrowernumber => $bor->{'borrowernumber'},
180 cardnumber => $bor->{'cardnumber'},
181 surname => $bor->{'surname'},
182 firstname => $bor->{'firstname'},
183 othernames => $bor->{'othernames'},
184 categorycode => $bor->{'categorycode'},
185 category_type => $bor->{'category_type'},
186 categoryname => $bor->{'description'},
187 address => $address,
188 address2 => $bor->{'address2'},
189 city => $bor->{'city'},
190 state => $bor->{'state'},
191 zipcode => $bor->{'zipcode'},
192 country => $bor->{'country'},
193 phone => $bor->{'phone'},
194 phonepro => $bor->{'phonepro'},
195 mobile => $bor->{'mobile'},
196 email => $bor->{'email'},
197 emailpro => $bor->{'emailpro'},
198 branchcode => $bor->{'branchcode'},
199 branchname => GetBranchName($bor->{'branchcode'}),
200 loop => \@loop,
201 is_child => ($bor->{'category_type'} eq 'C'),
202 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
203 RoutingSerials => C4::Context->preference('RoutingSerials'),
206 output_html_with_http_headers $input, $cookie, $template->output;