Bug 17109: Add CSRF token to [opac-]sendbasket
[koha.git] / tools / modborrowers.pl
blob31e4ec1fa284e1814615299e20fa3f0bf727a48b
1 #!/usr/bin/perl
3 # Copyright 2012 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 # modborrowers.pl
22 # Batch Edit Patrons
23 # Modification for patron's fields:
24 # surname firstname branchcode categorycode city state zipcode country sort1
25 # sort2 dateenrolled dateexpiry borrowernotes
26 # And for patron attributes.
28 use Modern::Perl;
29 use CGI qw ( -utf8 );
30 use C4::Auth;
31 use C4::Koha;
32 use C4::Members;
33 use C4::Members::Attributes;
34 use C4::Members::AttributeTypes qw/GetAttributeTypes_hashref/;
35 use C4::Output;
36 use List::MoreUtils qw /any uniq/;
37 use Koha::DateUtils qw( dt_from_string );
38 use Koha::List::Patron;
39 use Koha::Libraries;
40 use Koha::Patron::Categories;
42 my $input = new CGI;
43 my $op = $input->param('op') || 'show_form';
44 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
45 { template_name => "tools/modborrowers.tt",
46 query => $input,
47 type => "intranet",
48 authnotrequired => 0,
49 flagsrequired => { tools => "edit_patrons" },
53 my %cookies = parse CGI::Cookie($cookie);
54 my $sessionID = $cookies{'CGISESSID'}->value;
55 my $dbh = C4::Context->dbh;
57 # Show borrower informations
58 if ( $op eq 'show' ) {
59 my $filefh = $input->upload('uploadfile');
60 my $filecontent = $input->param('filecontent');
61 my $patron_list_id = $input->param('patron_list_id');
62 my @borrowers;
63 my @cardnumbers;
64 my @notfoundcardnumbers;
66 # Get cardnumbers from a file or the input area
67 my @contentlist;
68 if ($filefh) {
69 while ( my $content = <$filefh> ) {
70 $content =~ s/[\r\n]*$//g;
71 push @cardnumbers, $content if $content;
73 } elsif ( $patron_list_id ) {
74 my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
76 @cardnumbers =
77 $list->patron_list_patrons()->search_related('borrowernumber')
78 ->get_column('cardnumber')->all();
80 } else {
81 if ( my $list = $input->param('cardnumberlist') ) {
82 push @cardnumbers, split( /\s\n/, $list );
86 my $max_nb_attr = 0;
87 for my $cardnumber ( @cardnumbers ) {
88 my $borrower = GetBorrowerInfos( cardnumber => $cardnumber );
89 if ( $borrower ) {
90 $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
91 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
92 push @borrowers, $borrower;
93 } else {
94 push @notfoundcardnumbers, $cardnumber;
98 # Just for a correct display
99 for my $borrower ( @borrowers ) {
100 my $length = scalar( @{ $borrower->{patron_attributes} } );
101 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
104 # Construct the patron attributes list
105 my @patron_attributes_values;
106 my @patron_attributes_codes;
107 my $patron_attribute_types = C4::Members::AttributeTypes::GetAttributeTypes_hashref('all');
108 my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
109 for ( values %$patron_attribute_types ) {
110 my $attr_type = C4::Members::AttributeTypes->fetch( $_->{code} );
111 # TODO Repeatable attributes are not correctly managed and can cause data lost.
112 # This should be implemented.
113 next if $attr_type->{repeatable};
114 next if $attr_type->{unique_id}; # Don't display patron attributes that must be unqiue
115 my $options = $attr_type->authorised_value_category
116 ? GetAuthorisedValues( $attr_type->authorised_value_category )
117 : undef;
118 push @patron_attributes_values,
120 attribute_code => $_->{code},
121 options => $options,
124 my $category_code = $_->{category_code};
125 my ( $category_lib ) = map {
126 ( defined $category_code and $_->categorycode eq $category_code ) ? $_->description : ()
127 } @patron_categories;
128 push @patron_attributes_codes,
130 attribute_code => $_->{code},
131 attribute_lib => $_->{description},
132 category_lib => $category_lib,
133 type => $attr_type->authorised_value_category ? 'select' : 'text',
137 my @attributes_header = ();
138 for ( 1 .. scalar( $max_nb_attr ) ) {
139 push @attributes_header, { attribute => "Attributes $_" };
141 $template->param( borrowers => \@borrowers );
142 $template->param( attributes_header => \@attributes_header );
143 @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
144 $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
145 if @notfoundcardnumbers;
147 # Construct drop-down list values
148 my $branches = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;
149 my @branches_option;
150 push @branches_option, { value => $_->{value}, lib => $_->{branchname} } for @$branches;
151 unshift @branches_option, { value => "", lib => "" };
152 my @categories_option;
153 push @categories_option, { value => $_->categorycode, lib => $_->description } for @patron_categories;
154 unshift @categories_option, { value => "", lib => "" };
155 my $bsort1 = GetAuthorisedValues("Bsort1");
156 my @sort1_option;
157 push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
158 unshift @sort1_option, { value => "", lib => "" }
159 if @sort1_option;
160 my $bsort2 = GetAuthorisedValues("Bsort2");
161 my @sort2_option;
162 push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
163 unshift @sort2_option, { value => "", lib => "" }
164 if @sort2_option;
166 my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
168 my @fields = (
170 name => "surname",
171 type => "text",
172 mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
176 name => "firstname",
177 type => "text",
178 mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
182 name => "branchcode",
183 type => "select",
184 option => \@branches_option,
185 mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
189 name => "categorycode",
190 type => "select",
191 option => \@categories_option,
192 mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
196 name => "city",
197 type => "text",
198 mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
202 name => "state",
203 type => "text",
204 mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
208 name => "zipcode",
209 type => "text",
210 mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
214 name => "country",
215 type => "text",
216 mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
220 name => "sort1",
221 type => @sort1_option ? "select" : "text",
222 option => \@sort1_option,
223 mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
227 name => "sort2",
228 type => @sort2_option ? "select" : "text",
229 option => \@sort2_option,
230 mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
234 name => "dateenrolled",
235 type => "date",
236 mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
240 name => "dateexpiry",
241 type => "date",
242 mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
246 name => "borrowernotes",
247 type => "text",
248 mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
252 name => "opacnote",
253 type => "text",
254 mandatory => ( grep /opacnote/, @mandatoryFields ) ? 1 : 0,
258 $template->param('patron_attributes_codes', \@patron_attributes_codes);
259 $template->param('patron_attributes_values', \@patron_attributes_values);
261 $template->param( fields => \@fields );
264 # Process modifications
265 if ( $op eq 'do' ) {
267 my @disabled = $input->multi_param('disable_input');
268 my $infos;
269 for my $field ( qw/surname firstname branchcode categorycode city state zipcode country sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote/ ) {
270 my $value = $input->param($field);
271 $infos->{$field} = $value if $value;
272 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
275 for my $field ( qw( dateenrolled dateexpiry ) ) {
276 $infos->{$field} = dt_from_string($infos->{$field}) if $infos->{$field};
279 my @attributes = $input->multi_param('patron_attributes');
280 my @attr_values = $input->multi_param('patron_attributes_value');
282 my @errors;
283 my @borrowernumbers = $input->multi_param('borrowernumber');
284 # For each borrower selected
285 for my $borrowernumber ( @borrowernumbers ) {
286 # If at least one field are filled, we want to modify the borrower
287 if ( defined $infos ) {
288 $infos->{borrowernumber} = $borrowernumber;
289 my $success = ModMember(%$infos);
290 if (!$success) {
291 my $borrowerinfo = GetBorrowerInfos( borrowernumber => $borrowernumber );
292 $infos->{cardnumber} = $borrowerinfo->{cardnumber} || '';
293 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
298 my $borrower_categorycode = GetBorrowerCategorycode $borrowernumber;
299 my $i=0;
300 for ( @attributes ) {
301 my $attribute;
302 $attribute->{code} = $_;
303 $attribute->{attribute} = $attr_values[$i];
304 my $attr_type = C4::Members::AttributeTypes->fetch( $_ );
305 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
306 ++$i and next if $attr_type->{category_code} and $attr_type->{category_code} ne $borrower_categorycode;
307 my $valuename = "attr" . $i . "_value";
308 if ( grep { /^$valuename$/ } @disabled ) {
309 # The attribute is disabled, we remove it for this borrower !
310 eval {
311 C4::Members::Attributes::DeleteBorrowerAttribute( $borrowernumber, $attribute );
313 push @errors, { error => $@ } if $@;
314 } else {
315 # Attribute's value is empty, we don't want to modify it
316 ++$i and next if not $attribute->{attribute};
318 eval {
319 C4::Members::Attributes::UpdateBorrowerAttribute( $borrowernumber, $attribute );
321 push @errors, { error => $@ } if $@;
323 $i++;
326 $op = "show_results"; # We have process modifications, the user want to view its
328 # Construct the results list
329 my @borrowers;
330 my $max_nb_attr = 0;
331 for my $borrowernumber ( @borrowernumbers ) {
332 my $borrower = GetBorrowerInfos( borrowernumber => $borrowernumber );
333 if ( $borrower ) {
334 $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
335 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
336 push @borrowers, $borrower;
339 my @patron_attributes_option;
340 for my $borrower ( @borrowers ) {
341 push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
342 my $length = scalar( @{ $borrower->{patron_attributes} } );
343 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
346 my @attributes_header = ();
347 for ( 1 .. scalar( $max_nb_attr ) ) {
348 push @attributes_header, { attribute => "Attributes $_" };
351 $template->param( borrowers => \@borrowers );
352 $template->param( attributes_header => \@attributes_header );
354 $template->param( borrowers => \@borrowers );
355 $template->param( errors => \@errors );
356 } else {
358 $template->param( patron_lists => [ GetPatronLists() ] );
361 $template->param(
362 op => $op,
364 output_html_with_http_headers $input, $cookie, $template->output;
365 exit;
367 sub GetBorrowerInfos {
368 my ( %info ) = @_;
369 my $borrower = GetMember( %info );
370 if ( $borrower ) {
371 for ( qw(dateenrolled dateexpiry) ) {
372 my $userdate = $borrower->{$_};
373 unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
374 $borrower->{$_} = '';
375 next;
377 $borrower->{$_} = $userdate || '';
379 $borrower->{category_description} = Koha::Patron::Categories->find( $borrower->{categorycode} )->{description};
380 my $attr_loop = C4::Members::Attributes::GetBorrowerAttributes( $borrower->{borrowernumber} );
381 $borrower->{patron_attributes} = $attr_loop;
383 return $borrower;