3 # Copyright 2012 BibLibre
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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.
34 use C4
::Members
::Attributes
;
35 use C4
::Members
::AttributeTypes qw
/GetAttributeTypes_hashref/;
37 use List
::MoreUtils qw
/any uniq/;
38 use Koha
::List
::Patron
;
41 my $op = $input->param('op') || 'show_form';
42 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
43 { template_name
=> "tools/modborrowers.tt",
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');
62 my @notfoundcardnumbers;
64 # Get cardnumbers from a file or the input area
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 } );
75 $list->patron_list_patrons()->search_related('borrowernumber')
76 ->get_column('cardnumber')->all();
79 if ( my $list = $input->param('cardnumberlist') ) {
80 push @cardnumbers, split( /\s\n/, $list );
85 for my $cardnumber ( @cardnumbers ) {
86 my $borrower = GetBorrowerInfos
( cardnumber
=> $cardnumber );
88 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
89 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
90 push @borrowers, $borrower;
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 )
112 push @patron_attributes_values,
114 attribute_code
=> $_->{code
},
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
;
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");
152 push @sort1_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort1;
153 unshift @sort1_option, { value
=> "", lib
=> "" }
155 my $bsort2 = GetAuthorisedValues
("Bsort2");
157 push @sort2_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort2;
158 unshift @sort2_option, { value
=> "", lib
=> "" }
161 my @mandatoryFields = split( /\|/, C4
::Context
->preference("BorrowerMandatoryField") );
167 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0
173 mandatory
=> ( grep /firstname/, @mandatoryFields ) ?
1 : 0,
177 name
=> "branchcode",
179 option
=> \
@branches_option,
180 mandatory
=> ( grep /branchcode/, @mandatoryFields ) ?
1 : 0,
184 name
=> "categorycode",
186 option
=> \
@categories_option,
187 mandatory
=> ( grep /categorycode/, @mandatoryFields ) ?
1 : 0,
193 mandatory
=> ( grep /city/, @mandatoryFields ) ?
1 : 0,
199 mandatory
=> ( grep /state/, @mandatoryFields ) ?
1 : 0,
205 mandatory
=> ( grep /zipcode/, @mandatoryFields ) ?
1 : 0,
211 mandatory
=> ( grep /country/, @mandatoryFields ) ?
1 : 0,
216 type
=> @sort1_option ?
"select" : "text",
217 option
=> \
@sort1_option,
218 mandatory
=> ( grep /sort1/, @mandatoryFields ) ?
1 : 0,
223 type
=> @sort2_option ?
"select" : "text",
224 option
=> \
@sort2_option,
225 mandatory
=> ( grep /sort2/, @mandatoryFields ) ?
1 : 0,
229 name
=> "dateenrolled",
231 mandatory
=> ( grep /dateenrolled/, @mandatoryFields ) ?
1 : 0,
235 name
=> "dateexpiry",
237 mandatory
=> ( grep /dateexpiry/, @mandatoryFields ) ?
1 : 0,
241 name
=> "borrowernotes",
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
256 my @disabled = $input->param('disable_input');
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');
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 push @errors, { error
=> "can_not_update", borrowernumber
=> $infos->{borrowernumber
} } if not $success;
279 my $borrower_categorycode = GetBorrowerCategorycode
$borrowernumber;
281 for ( @attributes ) {
283 $attribute->{code
} = $_;
284 $attribute->{attribute
} = $attr_values[$i];
285 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_ );
286 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
287 ++$i and next if $attr_type->{category_code
} and $attr_type->{category_code
} ne $borrower_categorycode;
288 my $valuename = "attr" . $i . "_value";
289 if ( grep { /^$valuename$/ } @disabled ) {
290 # The attribute is disabled, we remove it for this borrower !
292 C4
::Members
::Attributes
::DeleteBorrowerAttribute
( $borrowernumber, $attribute );
294 push @errors, { error
=> $@
} if $@
;
296 # Attribute's value is empty, we don't want to modify it
297 ++$i and next if not $attribute->{attribute
};
300 C4
::Members
::Attributes
::UpdateBorrowerAttribute
( $borrowernumber, $attribute );
302 push @errors, { error
=> $@
} if $@
;
307 $op = "show_results"; # We have process modifications, the user want to view its
309 # Construct the results list
312 for my $borrowernumber ( @borrowernumbers ) {
313 my $borrower = GetBorrowerInfos
( borrowernumber
=> $borrowernumber );
315 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
316 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
317 push @borrowers, $borrower;
320 my @patron_attributes_option;
321 for my $borrower ( @borrowers ) {
322 push @patron_attributes_option, { value
=> "$_->{code}", lib
=> $_->{code
} } for @
{ $borrower->{patron_attributes
} };
323 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
324 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
327 my @attributes_header = ();
328 for ( 1 .. scalar( $max_nb_attr ) ) {
329 push @attributes_header, { attribute
=> "Attributes $_" };
332 $template->param( borrowers
=> \
@borrowers );
333 $template->param( attributes_header
=> \
@attributes_header );
335 $template->param( borrowers
=> \
@borrowers );
336 $template->param( errors
=> \
@errors );
339 $template->param( patron_lists
=> [ GetPatronLists
() ] );
345 output_html_with_http_headers
$input, $cookie, $template->output;
348 sub GetBorrowerInfos
{
350 my $borrower = GetMember
( %info );
352 $borrower->{branchname
} = GetBranchName
( $borrower->{branchcode
} );
353 for ( qw(dateenrolled dateexpiry) ) {
354 my $userdate = $borrower->{$_};
355 unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
356 $borrower->{$_} = '';
359 $borrower->{$_} = $userdate || '';
361 $borrower->{category_description
} = GetBorrowercategory
( $borrower->{categorycode
} )->{description
};
362 my $attr_loop = C4
::Members
::Attributes
::GetBorrowerAttributes
( $borrower->{borrowernumber
} );
363 $borrower->{patron_attributes
} = $attr_loop;