MT1109 , follow-up to 888cde546e5c62d71f312755f4a1dbc8631626eb
[koha.git] / members / member-flags.pl
blob749ceccb343fccde7b47050e8a149eeab1c1ec12
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::Acquisitions;
18 use C4::Output;
20 my $input = new CGI;
22 my $flagsrequired = { permissions => 1 };
23 my $member=$input->param('member');
24 my $bor = GetMemberDetails( $member,'');
25 if( $bor->{'category_type'} eq 'S' ) {
26 $flagsrequired->{'staffaccess'} = 1;
28 my ($template, $loggedinuser, $cookie)
29 = get_template_and_user({template_name => "members/member-flags.tmpl",
30 query => $input,
31 type => "intranet",
32 authnotrequired => 0,
33 flagsrequired => $flagsrequired,
34 debug => 1,
35 });
38 my %member2;
39 $member2{'borrowernumber'}=$member;
41 if ($input->param('newflags')) {
42 my $dbh=C4::Context->dbh();
44 my @perms = $input->param('flag');
45 my %all_module_perms = ();
46 my %sub_perms = ();
47 foreach my $perm (@perms) {
48 if ($perm !~ /:/) {
49 $all_module_perms{$perm} = 1;
50 } else {
51 my ($module, $sub_perm) = split /:/, $perm, 2;
52 push @{ $sub_perms{$module} }, $sub_perm;
56 # construct flags
57 my $module_flags = 0;
58 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
59 $sth->execute();
60 while (my ($bit, $flag) = $sth->fetchrow_array) {
61 if (exists $all_module_perms{$flag}) {
62 $module_flags += 2**$bit;
66 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
67 $sth->execute($module_flags, $member);
69 if (C4::Context->preference('GranularPermissions')) {
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);
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,flagdesc FROM userflags ORDER BY bit");
94 $sth->execute;
95 my @loop;
96 while (my ($bit, $flag, $flagdesc) = $sth->fetchrow) {
97 my $checked='';
98 if ($accessflags->{$flag}) {
99 $checked= 1;
102 my %row = ( bit => $bit,
103 flag => $flag,
104 checked => $checked,
105 flagdesc => $flagdesc );
107 if (C4::Context->preference('GranularPermissions')) {
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 description => $all_perms->{$flag}->{$sub_perm},
119 checked => 1
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 description => $all_perms->{$flag}->{$sub_perm},
133 checked => 1
137 # then ones not selected
138 if (exists $all_perms->{$flag}) {
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 description => $all_perms->{$flag}->{$sub_perm},
145 checked => 0
146 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
150 $row{expand} = $expand_parent;
151 if ($#sub_perm_loop > -1) {
152 $row{sub_perm_loop} = \@sub_perm_loop;
155 push @loop, \%row;
158 if ( $bor->{'category_type'} eq 'C') {
159 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
160 my $cnt = scalar(@$catcodes);
161 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
162 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
165 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' );
166 my ($picture, $dberror) = GetPatronImage($bor->{'cardnumber'});
167 $template->param( picture => 1 ) if $picture;
169 $template->param(
170 borrowernumber => $bor->{'borrowernumber'},
171 cardnumber => $bor->{'cardnumber'},
172 surname => $bor->{'surname'},
173 firstname => $bor->{'firstname'},
174 categorycode => $bor->{'categorycode'},
175 category_type => $bor->{'category_type'},
176 categoryname => $bor->{'description'},
177 address => $bor->{'address'},
178 address2 => $bor->{'address2'},
179 city => $bor->{'city'},
180 zipcode => $bor->{'zipcode'},
181 country => $bor->{'country'},
182 phone => $bor->{'phone'},
183 email => $bor->{'email'},
184 branchcode => $bor->{'branchcode'},
185 branchname => GetBranchName($bor->{'branchcode'}),
186 loop => \@loop,
187 is_child => ($bor->{'category_type'} eq 'C'),
190 output_html_with_http_headers $input, $cookie, $template->output;