Bug 15092: item type descriptions not showing in OPAC advsearch
[koha.git] / tools / modborrowers.pl
blob4172bc3c612db220ff0e111910c1b3e9549d6bae
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::Branch;
32 use C4::Koha;
33 use C4::Members;
34 use C4::Members::Attributes;
35 use C4::Members::AttributeTypes qw/GetAttributeTypes_hashref/;
36 use C4::Output;
37 use List::MoreUtils qw /any uniq/;
38 use Koha::List::Patron;
40 my $input = new CGI;
41 my $op = $input->param('op') || 'show_form';
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
43 { template_name => "tools/modborrowers.tt",
44 query => $input,
45 type => "intranet",
46 authnotrequired => 0,
47 flagsrequired => { tools => "edit_patrons" },
51 my %cookies = parse CGI::Cookie($cookie);
52 my $sessionID = $cookies{'CGISESSID'}->value;
53 my $dbh = C4::Context->dbh;
55 # Show borrower informations
56 if ( $op eq 'show' ) {
57 my $filefh = $input->upload('uploadfile');
58 my $filecontent = $input->param('filecontent');
59 my $patron_list_id = $input->param('patron_list_id');
60 my @borrowers;
61 my @cardnumbers;
62 my @notfoundcardnumbers;
64 # Get cardnumbers from a file or the input area
65 my @contentlist;
66 if ($filefh) {
67 while ( my $content = <$filefh> ) {
68 $content =~ s/[\r\n]*$//g;
69 push @cardnumbers, $content if $content;
71 } elsif ( $patron_list_id ) {
72 my ($list) = GetPatronLists( { patron_list_id => $patron_list_id } );
74 @cardnumbers =
75 $list->patron_list_patrons()->search_related('borrowernumber')
76 ->get_column('cardnumber')->all();
78 } else {
79 if ( my $list = $input->param('cardnumberlist') ) {
80 push @cardnumbers, split( /\s\n/, $list );
84 my $max_nb_attr = 0;
85 for my $cardnumber ( @cardnumbers ) {
86 my $borrower = GetBorrowerInfos( cardnumber => $cardnumber );
87 if ( $borrower ) {
88 $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
89 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
90 push @borrowers, $borrower;
91 } else {
92 push @notfoundcardnumbers, $cardnumber;
96 # Just for a correct display
97 for my $borrower ( @borrowers ) {
98 my $length = scalar( @{ $borrower->{patron_attributes} } );
99 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
102 # Construct the patron attributes list
103 my @patron_attributes_values;
104 my @patron_attributes_codes;
105 my $patron_attribute_types = C4::Members::AttributeTypes::GetAttributeTypes_hashref('all');
106 my $patron_categories = C4::Members::GetBorrowercategoryList;
107 for ( values %$patron_attribute_types ) {
108 my $attr_type = C4::Members::AttributeTypes->fetch( $_->{code} );
109 my $options = $attr_type->authorised_value_category
110 ? GetAuthorisedValues( $attr_type->authorised_value_category )
111 : undef;
112 push @patron_attributes_values,
114 attribute_code => $_->{code},
115 options => $options,
118 my $category_code = $_->{category_code};
119 my ( $category_lib ) = map {
120 ( defined $category_code and $_->{categorycode} eq $category_code ) ? $_->{description} : ()
121 } @$patron_categories;
122 push @patron_attributes_codes,
124 attribute_code => $_->{code},
125 attribute_lib => $_->{description},
126 category_lib => $category_lib,
127 type => $attr_type->authorised_value_category ? 'select' : 'text',
131 my @attributes_header = ();
132 for ( 1 .. scalar( $max_nb_attr ) ) {
133 push @attributes_header, { attribute => "Attributes $_" };
135 $template->param( borrowers => \@borrowers );
136 $template->param( attributes_header => \@attributes_header );
137 @notfoundcardnumbers = map { { cardnumber => $_ } } @notfoundcardnumbers;
138 $template->param( notfoundcardnumbers => \@notfoundcardnumbers )
139 if @notfoundcardnumbers;
141 # Construct drop-down list values
142 my $branches = GetBranchesLoop;
143 my @branches_option;
144 push @branches_option, { value => $_->{value}, lib => $_->{branchname} } for @$branches;
145 unshift @branches_option, { value => "", lib => "" };
146 my $categories = GetBorrowercategoryList;
147 my @categories_option;
148 push @categories_option, { value => $_->{categorycode}, lib => $_->{description} } for @$categories;
149 unshift @categories_option, { value => "", lib => "" };
150 my $bsort1 = GetAuthorisedValues("Bsort1");
151 my @sort1_option;
152 push @sort1_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort1;
153 unshift @sort1_option, { value => "", lib => "" }
154 if @sort1_option;
155 my $bsort2 = GetAuthorisedValues("Bsort2");
156 my @sort2_option;
157 push @sort2_option, { value => $_->{authorised_value}, lib => $_->{lib} } for @$bsort2;
158 unshift @sort2_option, { value => "", lib => "" }
159 if @sort2_option;
161 my @mandatoryFields = split( /\|/, C4::Context->preference("BorrowerMandatoryField") );
163 my @fields = (
165 name => "surname",
166 type => "text",
167 mandatory => ( grep /surname/, @mandatoryFields ) ? 1 : 0
171 name => "firstname",
172 type => "text",
173 mandatory => ( grep /firstname/, @mandatoryFields ) ? 1 : 0,
177 name => "branchcode",
178 type => "select",
179 option => \@branches_option,
180 mandatory => ( grep /branchcode/, @mandatoryFields ) ? 1 : 0,
184 name => "categorycode",
185 type => "select",
186 option => \@categories_option,
187 mandatory => ( grep /categorycode/, @mandatoryFields ) ? 1 : 0,
191 name => "city",
192 type => "text",
193 mandatory => ( grep /city/, @mandatoryFields ) ? 1 : 0,
197 name => "state",
198 type => "text",
199 mandatory => ( grep /state/, @mandatoryFields ) ? 1 : 0,
203 name => "zipcode",
204 type => "text",
205 mandatory => ( grep /zipcode/, @mandatoryFields ) ? 1 : 0,
209 name => "country",
210 type => "text",
211 mandatory => ( grep /country/, @mandatoryFields ) ? 1 : 0,
215 name => "sort1",
216 type => @sort1_option ? "select" : "text",
217 option => \@sort1_option,
218 mandatory => ( grep /sort1/, @mandatoryFields ) ? 1 : 0,
222 name => "sort2",
223 type => @sort2_option ? "select" : "text",
224 option => \@sort2_option,
225 mandatory => ( grep /sort2/, @mandatoryFields ) ? 1 : 0,
229 name => "dateenrolled",
230 type => "date",
231 mandatory => ( grep /dateenrolled/, @mandatoryFields ) ? 1 : 0,
235 name => "dateexpiry",
236 type => "date",
237 mandatory => ( grep /dateexpiry/, @mandatoryFields ) ? 1 : 0,
241 name => "borrowernotes",
242 type => "text",
243 mandatory => ( grep /borrowernotes/, @mandatoryFields ) ? 1 : 0,
247 $template->param('patron_attributes_codes', \@patron_attributes_codes);
248 $template->param('patron_attributes_values', \@patron_attributes_values);
250 $template->param( fields => \@fields );
253 # Process modifications
254 if ( $op eq 'do' ) {
256 my @disabled = $input->param('disable_input');
257 my $infos;
258 for my $field ( qw/surname firstname branchcode categorycode city state zipcode country sort1 sort2 dateenrolled dateexpiry borrowernotes/ ) {
259 my $value = $input->param($field);
260 $infos->{$field} = $value if $value;
261 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
264 my @attributes = $input->param('patron_attributes');
265 my @attr_values = $input->param('patron_attributes_value');
267 my @errors;
268 my @borrowernumbers = $input->param('borrowernumber');
269 # For each borrower selected
270 for my $borrowernumber ( @borrowernumbers ) {
271 # If at least one field are filled, we want to modify the borrower
272 if ( defined $infos ) {
273 $infos->{borrowernumber} = $borrowernumber;
274 my $success = ModMember(%$infos);
275 if (!$success) {
276 my $borrowerinfo = GetBorrowerInfos( borrowernumber => $borrowernumber );
277 $infos->{cardnumber} = $borrowerinfo->{cardnumber} || '';
278 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
283 my $borrower_categorycode = GetBorrowerCategorycode $borrowernumber;
284 my $i=0;
285 for ( @attributes ) {
286 my $attribute;
287 $attribute->{code} = $_;
288 $attribute->{attribute} = $attr_values[$i];
289 my $attr_type = C4::Members::AttributeTypes->fetch( $_ );
290 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
291 ++$i and next if $attr_type->{category_code} and $attr_type->{category_code} ne $borrower_categorycode;
292 my $valuename = "attr" . $i . "_value";
293 if ( grep { /^$valuename$/ } @disabled ) {
294 # The attribute is disabled, we remove it for this borrower !
295 eval {
296 C4::Members::Attributes::DeleteBorrowerAttribute( $borrowernumber, $attribute );
298 push @errors, { error => $@ } if $@;
299 } else {
300 # Attribute's value is empty, we don't want to modify it
301 ++$i and next if not $attribute->{attribute};
303 eval {
304 C4::Members::Attributes::UpdateBorrowerAttribute( $borrowernumber, $attribute );
306 push @errors, { error => $@ } if $@;
308 $i++;
311 $op = "show_results"; # We have process modifications, the user want to view its
313 # Construct the results list
314 my @borrowers;
315 my $max_nb_attr = 0;
316 for my $borrowernumber ( @borrowernumbers ) {
317 my $borrower = GetBorrowerInfos( borrowernumber => $borrowernumber );
318 if ( $borrower ) {
319 $max_nb_attr = scalar( @{ $borrower->{patron_attributes} } )
320 if scalar( @{ $borrower->{patron_attributes} } ) > $max_nb_attr;
321 push @borrowers, $borrower;
324 my @patron_attributes_option;
325 for my $borrower ( @borrowers ) {
326 push @patron_attributes_option, { value => "$_->{code}", lib => $_->{code} } for @{ $borrower->{patron_attributes} };
327 my $length = scalar( @{ $borrower->{patron_attributes} } );
328 push @{ $borrower->{patron_attributes} }, {} for ( $length .. $max_nb_attr - 1);
331 my @attributes_header = ();
332 for ( 1 .. scalar( $max_nb_attr ) ) {
333 push @attributes_header, { attribute => "Attributes $_" };
336 $template->param( borrowers => \@borrowers );
337 $template->param( attributes_header => \@attributes_header );
339 $template->param( borrowers => \@borrowers );
340 $template->param( errors => \@errors );
341 } else {
343 $template->param( patron_lists => [ GetPatronLists() ] );
346 $template->param(
347 op => $op,
349 output_html_with_http_headers $input, $cookie, $template->output;
350 exit;
352 sub GetBorrowerInfos {
353 my ( %info ) = @_;
354 my $borrower = GetMember( %info );
355 if ( $borrower ) {
356 $borrower->{branchname} = GetBranchName( $borrower->{branchcode} );
357 for ( qw(dateenrolled dateexpiry) ) {
358 my $userdate = $borrower->{$_};
359 unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
360 $borrower->{$_} = '';
361 next;
363 $borrower->{$_} = $userdate || '';
365 $borrower->{category_description} = GetBorrowercategory( $borrower->{categorycode} )->{description};
366 my $attr_loop = C4::Members::Attributes::GetBorrowerAttributes( $borrower->{borrowernumber} );
367 $borrower->{patron_attributes} = $attr_loop;
369 return $borrower;