Bug 18999: Add regression tests
[koha.git] / members / member-flags.pl
blob158074a4fce08212756ba524dcac5ef6b412b0c1
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::Members::Attributes qw(GetBorrowerAttributes);
16 #use C4::Acquisitions;
18 use Koha::Patron::Categories;
19 use Koha::Patrons;
21 use C4::Output;
22 use Koha::Patron::Images;
23 use Koha::Token;
25 my $input = new CGI;
27 my $flagsrequired = { permissions => 1 };
28 my $member=$input->param('member');
29 my $patron = Koha::Patrons->find( $member );
30 unless ( $patron ) {
31 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
32 exit;
35 my $category_type = $patron->category->category_type;
36 my $bor = $patron->unblessed;
37 if( $category_type eq 'S' ) {
38 $flagsrequired->{'staffaccess'} = 1;
40 my ($template, $loggedinuser, $cookie) = get_template_and_user({
41 template_name => "members/member-flags.tt",
42 query => $input,
43 type => "intranet",
44 authnotrequired => 0,
45 flagsrequired => $flagsrequired,
46 debug => 1,
47 });
50 my %member2;
51 $member2{'borrowernumber'}=$member;
53 if ($input->param('newflags')) {
55 die "Wrong CSRF token"
56 unless Koha::Token->new->check_csrf({
57 session_id => scalar $input->cookie('CGISESSID'),
58 token => scalar $input->param('csrf_token'),
59 });
62 my $dbh=C4::Context->dbh();
64 my @perms = $input->multi_param('flag');
65 my %all_module_perms = ();
66 my %sub_perms = ();
67 foreach my $perm (@perms) {
68 if ($perm !~ /:/) {
69 $all_module_perms{$perm} = 1;
70 } else {
71 my ($module, $sub_perm) = split /:/, $perm, 2;
72 push @{ $sub_perms{$module} }, $sub_perm;
76 # construct flags
77 my $module_flags = 0;
78 my $sth=$dbh->prepare("SELECT bit,flag FROM userflags ORDER BY bit");
79 $sth->execute();
80 while (my ($bit, $flag) = $sth->fetchrow_array) {
81 if (exists $all_module_perms{$flag}) {
82 $module_flags += 2**$bit;
86 $sth = $dbh->prepare("UPDATE borrowers SET flags=? WHERE borrowernumber=?");
87 $sth->execute($module_flags, $member);
89 # deal with subpermissions
90 $sth = $dbh->prepare("DELETE FROM user_permissions WHERE borrowernumber = ?");
91 $sth->execute($member);
92 $sth = $dbh->prepare("INSERT INTO user_permissions (borrowernumber, module_bit, code)
93 SELECT ?, bit, ?
94 FROM userflags
95 WHERE flag = ?");
96 foreach my $module (keys %sub_perms) {
97 next if exists $all_module_perms{$module};
98 foreach my $sub_perm (@{ $sub_perms{$module} }) {
99 $sth->execute($member, $sub_perm, $module);
103 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member");
104 } else {
106 my $flags = C4::Members::patronflags( $bor );
107 my $accessflags;
108 my $dbh = C4::Context->dbh();
109 # FIXME This needs to be improved to avoid doing the same query
110 my $sth = $dbh->prepare("select bit,flag from userflags");
111 $sth->execute;
112 while ( my ( $bit, $flag ) = $sth->fetchrow ) {
113 if ( $bor->{flags} && $bor->{flags} & 2**$bit ) {
114 $accessflags->{$flag} = 1;
118 my $all_perms = get_all_subpermissions();
119 my $user_perms = get_user_subpermissions($bor->{'userid'});
120 $sth = $dbh->prepare("SELECT bit, flag FROM userflags ORDER BY bit");
121 $sth->execute;
122 my @loop;
124 while (my ($bit, $flag) = $sth->fetchrow) {
125 my $checked='';
126 if ($accessflags->{$flag}) {
127 $checked= 1;
130 my %row = ( bit => $bit,
131 flag => $flag,
132 checked => $checked,
135 my @sub_perm_loop = ();
136 my $expand_parent = 0;
137 if ($checked) {
138 if (exists $all_perms->{$flag}) {
139 $expand_parent = 1;
140 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
141 push @sub_perm_loop, {
142 id => "${flag}_$sub_perm",
143 perm => "$flag:$sub_perm",
144 code => $sub_perm,
145 checked => 1
149 } else {
150 if (exists $user_perms->{$flag}) {
151 $expand_parent = 1;
152 # put selected ones first
153 foreach my $sub_perm (sort keys %{ $user_perms->{$flag} }) {
154 push @sub_perm_loop, {
155 id => "${flag}_$sub_perm",
156 perm => "$flag:$sub_perm",
157 code => $sub_perm,
158 checked => 1
162 # then ones not selected
163 if (exists $all_perms->{$flag}) {
164 foreach my $sub_perm (sort keys %{ $all_perms->{$flag} }) {
165 push @sub_perm_loop, {
166 id => "${flag}_$sub_perm",
167 perm => "$flag:$sub_perm",
168 code => $sub_perm,
169 checked => 0
170 } unless exists $user_perms->{$flag} and exists $user_perms->{$flag}->{$sub_perm};
174 $row{expand} = $expand_parent;
175 if ($#sub_perm_loop > -1) {
176 $row{sub_perm_loop} = \@sub_perm_loop;
178 push @loop, \%row;
181 if ( $category_type eq 'C') {
182 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
183 $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
184 $template->param( 'catcode' => $patron_categories->next ) if $patron_categories->count == 1;
187 $template->param( adultborrower => 1 ) if ( $category_type =~ /^(A|I)$/ );
188 $template->param( picture => 1 ) if $patron->image;
190 if (C4::Context->preference('ExtendedPatronAttributes')) {
191 my $attributes = GetBorrowerAttributes($bor->{'borrowernumber'});
192 $template->param(
193 ExtendedPatronAttributes => 1,
194 extendedattributes => $attributes
198 $template->param(
199 borrowernumber => $bor->{'borrowernumber'},
200 cardnumber => $bor->{'cardnumber'},
201 surname => $bor->{'surname'},
202 firstname => $bor->{'firstname'},
203 othernames => $bor->{'othernames'},
204 categorycode => $bor->{'categorycode'},
205 category_type => $category_type,
206 categoryname => $bor->{'description'},
207 address => $bor->{address},
208 address2 => $bor->{'address2'},
209 streettype => $bor->{streettype},
210 city => $bor->{'city'},
211 state => $bor->{'state'},
212 zipcode => $bor->{'zipcode'},
213 country => $bor->{'country'},
214 phone => $bor->{'phone'},
215 phonepro => $bor->{'phonepro'},
216 mobile => $bor->{'mobile'},
217 email => $bor->{'email'},
218 emailpro => $bor->{'emailpro'},
219 branchcode => $bor->{'branchcode'},
220 loop => \@loop,
221 is_child => ( $category_type eq 'C' ),
222 RoutingSerials => C4::Context->preference('RoutingSerials'),
223 csrf_token =>
224 Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
227 output_html_with_http_headers $input, $cookie, $template->output;