Add release notes to 16.05.14 release
[koha.git] / members / member-flags.pl
blob50062f6b7194c2e05232bcf5e66aa076f6d26620
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 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;
19 use C4::Output;
20 use Koha::Patron::Images;
21 use Koha::Token;
23 my $input = new CGI;
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;
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 });
41 my %member2;
42 $member2{'borrowernumber'}=$member;
44 if ($input->param('newflags')) {
46 die "Wrong CSRF token"
47 unless Koha::Token->new->check_csrf({
48 session_id => scalar $input->cookie('CGISESSID'),
49 token => scalar $input->param('csrf_token'),
50 });
53 my $dbh=C4::Context->dbh();
55 my @perms = $input->multi_param('flag');
56 my %all_module_perms = ();
57 my %sub_perms = ();
58 foreach my $perm (@perms) {
59 if ($perm !~ /:/) {
60 $all_module_perms{$perm} = 1;
61 } else {
62 my ($module, $sub_perm) = split /:/, $perm, 2;
63 push @{ $sub_perms{$module} }, $sub_perm;
67 # construct flags
68 my $module_flags = 0;
69 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
70 $sth->execute();
71 while (my ($bit, $flag) = $sth->fetchrow_array) {
72 if (exists $all_module_perms{$flag}) {
73 $module_flags += 2**$bit;
77 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
78 $sth->execute($module_flags, $member);
80 # deal with subpermissions
81 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
82 $sth->execute($member);
83 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
84 SELECT ?, bit, ?
85 FROM userflags
86 WHERE flag = ?");
87 foreach my $module (keys %sub_perms) {
88 next if exists $all_module_perms{$module};
89 foreach my $sub_perm (@{ $sub_perms{$module} }) {
90 $sth->execute($member, $sub_perm, $module);
94 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
95 } else {
96 # my ($bor,$flags,$accessflags)=GetMemberDetails($member,'');
97 my $flags = $bor->{'flags'};
98 my $accessflags = $bor->{'authflags'};
99 my $dbh=C4::Context->dbh();
100 my $all_perms = get_all_subpermissions();
101 my $user_perms = get_user_subpermissions($bor->{'userid'});
102 my $sth=$dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
103 $sth->execute;
104 my @loop;
105 while (my ($bit, $flag) = $sth->fetchrow) {
106 my $checked='';
107 if ($accessflags->{$flag}) {
108 $checked= 1;
111 my %row = ( bit => $bit,
112 flag => $flag,
113 checked => $checked,
116 my @sub_perm_loop = ();
117 my $expand_parent = 0;
118 if ($checked) {
119 if (exists $all_perms->{$flag}) {
120 $expand_parent = 1;
121 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
122 push @sub_perm_loop, {
123 id => "${flag}_$sub_perm",
124 perm => "$flag:$sub_perm",
125 code => $sub_perm,
126 checked => 1
130 } else {
131 if (exists $user_perms->{$flag}) {
132 $expand_parent = 1;
133 # put selected ones first
134 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
135 push @sub_perm_loop, {
136 id => "${flag}_$sub_perm",
137 perm => "$flag:$sub_perm",
138 code => $sub_perm,
139 checked => 1
143 # then ones not selected
144 if (exists $all_perms->{$flag}) {
145 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
146 push @sub_perm_loop, {
147 id => "${flag}_$sub_perm",
148 perm => "$flag:$sub_perm",
149 code => $sub_perm,
150 checked => 0
151 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
155 $row{expand} = $expand_parent;
156 if ($#sub_perm_loop > -1) {
157 $row{sub_perm_loop} = \@sub_perm_loop;
159 push @loop, \%row;
162 if ( $bor->{'category_type'} eq 'C') {
163 my ( $catcodes, $labels ) = GetborCatFromCatType( 'A', 'WHERE category_type = ?' );
164 my $cnt = scalar(@$catcodes);
165 $template->param( 'CATCODE_MULTI' => 1) if $cnt > 1;
166 $template->param( 'catcode' => $catcodes->[0]) if $cnt == 1;
169 $template->param( adultborrower => 1 ) if ( $bor->{'category_type'} eq 'A' || $bor->{'category_type'} eq 'I' );
170 my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
171 $template->param( picture => 1 ) if $patron_image;
173 if (C4::Context->preference('ExtendedPatronAttributes')) {
174 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
175 $template->param(
176 ExtendedPatronAttributes => 1,
177 extendedattributes => $attributes
181 $template->param(
182 borrowernumber => $bor->{'borrowernumber'},
183 cardnumber => $bor->{'cardnumber'},
184 surname => $bor->{'surname'},
185 firstname => $bor->{'firstname'},
186 othernames => $bor->{'othernames'},
187 categorycode => $bor->{'categorycode'},
188 category_type => $bor->{'category_type'},
189 categoryname => $bor->{'description'},
190 address => $bor->{address},
191 address2 => $bor->{'address2'},
192 streettype => $bor->{streettype},
193 city => $bor->{'city'},
194 state => $bor->{'state'},
195 zipcode => $bor->{'zipcode'},
196 country => $bor->{'country'},
197 phone => $bor->{'phone'},
198 phonepro => $bor->{'phonepro'},
199 mobile => $bor->{'mobile'},
200 email => $bor->{'email'},
201 emailpro => $bor->{'emailpro'},
202 branchcode => $bor->{'branchcode'},
203 branchname => GetBranchName($bor->{'branchcode'}),
204 loop => \@loop,
205 is_child => ($bor->{'category_type'} eq 'C'),
206 RoutingSerials => C4::Context->preference('RoutingSerials'),
207 csrf_token => Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
210 output_html_with_http_headers $input, $cookie, $template->output;