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 sort1 sort2 dateenrolled dateexpiry borrowernotes
25 # And for patron attributes.
33 use C4
::Members
::Attributes
;
34 use C4
::Members
::AttributeTypes qw
/GetAttributeTypes_hashref/;
36 use List
::MoreUtils qw
/any uniq/;
37 use Koha
::List
::Patron
;
40 my $op = $input->param('op') || 'show_form';
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
42 { template_name
=> "tools/modborrowers.tmpl",
46 flagsrequired
=> { tools
=> "edit_patrons" },
50 my %cookies = parse CGI
::Cookie
($cookie);
51 my $sessionID = $cookies{'CGISESSID'}->value;
52 my $dbh = C4
::Context
->dbh;
54 # Show borrower informations
55 if ( $op eq 'show' ) {
56 my $filefh = $input->upload('uploadfile');
57 my $filecontent = $input->param('filecontent');
58 my $patron_list_id = $input->param('patron_list_id');
61 my @notfoundcardnumbers;
63 # Get cardnumbers from a file or the input area
66 while ( my $content = <$filefh> ) {
67 $content =~ s/[\r\n]*$//g;
68 push @cardnumbers, $content if $content;
70 } elsif ( $patron_list_id ) {
71 my ($list) = GetPatronLists
( { patron_list_id
=> $patron_list_id } );
74 $list->patron_list_patrons()->search_related('borrowernumber')
75 ->get_column('cardnumber')->all();
78 if ( my $list = $input->param('cardnumberlist') ) {
79 push @cardnumbers, split( /\s\n/, $list );
84 for my $cardnumber ( @cardnumbers ) {
85 my $borrower = GetBorrowerInfos
( cardnumber
=> $cardnumber );
87 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
88 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
89 push @borrowers, $borrower;
91 push @notfoundcardnumbers, $cardnumber;
95 # Just for a correct display
96 for my $borrower ( @borrowers ) {
97 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
98 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
101 # Construct the patron attributes list
102 my @patron_attributes_values;
103 my @patron_attributes_codes;
104 my $patron_attribute_types = C4
::Members
::AttributeTypes
::GetAttributeTypes_hashref
('all');
105 my $patron_categories = C4
::Members
::GetBorrowercategoryList
;
106 for ( values %$patron_attribute_types ) {
107 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_->{code
} );
108 my $options = $attr_type->authorised_value_category
109 ? GetAuthorisedValues
( $attr_type->authorised_value_category )
111 push @patron_attributes_values,
113 attribute_code
=> $_->{code
},
117 my $category_code = $_->{category_code
};
118 my ( $category_lib ) = map {
119 ( defined $category_code and $_->{categorycode
} eq $category_code ) ?
$_->{description
} : ()
120 } @
$patron_categories;
121 push @patron_attributes_codes,
123 attribute_code
=> $_->{code
},
124 attribute_lib
=> $_->{description
},
125 category_lib
=> $category_lib,
126 type
=> $attr_type->authorised_value_category ?
'select' : 'text',
130 my @attributes_header = ();
131 for ( 1 .. scalar( $max_nb_attr ) ) {
132 push @attributes_header, { attribute
=> "Attributes $_" };
134 $template->param( borrowers
=> \
@borrowers );
135 $template->param( attributes_header
=> \
@attributes_header );
136 @notfoundcardnumbers = map { { cardnumber
=> $_ } } @notfoundcardnumbers;
137 $template->param( notfoundcardnumbers
=> \
@notfoundcardnumbers )
138 if @notfoundcardnumbers;
140 # Construct drop-down list values
141 my $branches = GetBranchesLoop
;
143 push @branches_option, { value
=> $_->{value
}, lib
=> $_->{branchname
} } for @
$branches;
144 unshift @branches_option, { value
=> "", lib
=> "" };
145 my $categories = GetBorrowercategoryList
;
146 my @categories_option;
147 push @categories_option, { value
=> $_->{categorycode
}, lib
=> $_->{description
} } for @
$categories;
148 unshift @categories_option, { value
=> "", lib
=> "" };
149 my $bsort1 = GetAuthorisedValues
("Bsort1");
151 push @sort1_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort1;
152 unshift @sort1_option, { value
=> "", lib
=> "" }
154 my $bsort2 = GetAuthorisedValues
("Bsort2");
156 push @sort2_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort2;
157 unshift @sort2_option, { value
=> "", lib
=> "" }
160 my @mandatoryFields = split( /\|/, C4
::Context
->preference("BorrowerMandatoryField") );
166 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0
172 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0,
176 name
=> "branchcode",
178 option
=> \
@branches_option,
179 mandatory
=> ( grep /branchcode/, @mandatoryFields ) ?
1 : 0,
183 name
=> "categorycode",
185 option
=> \
@categories_option,
186 mandatory
=> ( grep /categorycode/, @mandatoryFields ) ?
1 : 0,
191 type
=> @sort1_option ?
"select" : "text",
192 option
=> \
@sort1_option,
193 mandatory
=> ( grep /sort1/, @mandatoryFields ) ?
1 : 0,
198 type
=> @sort2_option ?
"select" : "text",
199 option
=> \
@sort2_option,
200 mandatory
=> ( grep /sort2/, @mandatoryFields ) ?
1 : 0,
204 name
=> "dateenrolled",
206 mandatory
=> ( grep /dateenrolled/, @mandatoryFields ) ?
1 : 0,
210 name
=> "dateexpiry",
212 mandatory
=> ( grep /dateexpiry/, @mandatoryFields ) ?
1 : 0,
216 name
=> "borrowernotes",
218 mandatory
=> ( grep /borrowernotes/, @mandatoryFields ) ?
1 : 0,
222 $template->param('patron_attributes_codes', \
@patron_attributes_codes);
223 $template->param('patron_attributes_values', \
@patron_attributes_values);
225 $template->param( fields
=> \
@fields );
228 # Process modifications
231 my @disabled = $input->param('disable_input');
233 for my $field ( qw
/surname firstname branchcode categorycode sort1 sort2 dateenrolled dateexpiry borrowernotes/ ) {
234 my $value = $input->param($field);
235 $infos->{$field} = $value if $value;
236 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
239 my @attributes = $input->param('patron_attributes');
240 my @attr_values = $input->param('patron_attributes_value');
243 my @borrowernumbers = $input->param('borrowernumber');
244 # For each borrower selected
245 for my $borrowernumber ( @borrowernumbers ) {
246 # If at least one field are filled, we want to modify the borrower
247 if ( defined $infos ) {
248 $infos->{borrowernumber
} = $borrowernumber;
249 my $success = ModMember
(%$infos);
250 push @errors, { error
=> "can_not_update", borrowernumber
=> $infos->{borrowernumber
} } if not $success;
254 my $borrower_categorycode = GetBorrowerCategorycode
$borrowernumber;
256 for ( @attributes ) {
258 $attribute->{code
} = $_;
259 $attribute->{attribute
} = $attr_values[$i];
260 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_ );
261 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
262 ++$i and next if $attr_type->{category_code
} and $attr_type->{category_code
} ne $borrower_categorycode;
263 my $valuename = "attr" . $i . "_value";
264 if ( grep { /^$valuename$/ } @disabled ) {
265 # The attribute is disabled, we remove it for this borrower !
267 C4
::Members
::Attributes
::DeleteBorrowerAttribute
( $borrowernumber, $attribute );
269 push @errors, { error
=> $@
} if $@
;
271 # Attribute's value is empty, we don't want to modify it
272 ++$i and next if not $attribute->{attribute
};
275 C4
::Members
::Attributes
::UpdateBorrowerAttribute
( $borrowernumber, $attribute );
277 push @errors, { error
=> $@
} if $@
;
282 $op = "show_results"; # We have process modifications, the user want to view its
284 # Construct the results list
287 for my $borrowernumber ( @borrowernumbers ) {
288 my $borrower = GetBorrowerInfos
( borrowernumber
=> $borrowernumber );
290 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
291 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
292 push @borrowers, $borrower;
295 my @patron_attributes_option;
296 for my $borrower ( @borrowers ) {
297 push @patron_attributes_option, { value
=> "$_->{code}", lib
=> $_->{code
} } for @
{ $borrower->{patron_attributes
} };
298 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
299 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
302 my @attributes_header = ();
303 for ( 1 .. scalar( $max_nb_attr ) ) {
304 push @attributes_header, { attribute
=> "Attributes $_" };
307 $template->param( borrowers
=> \
@borrowers );
308 $template->param( attributes_header
=> \
@attributes_header );
310 $template->param( borrowers
=> \
@borrowers );
311 $template->param( errors
=> \
@errors );
314 $template->param( patron_lists
=> [ GetPatronLists
() ] );
320 output_html_with_http_headers
$input, $cookie, $template->output;
323 sub GetBorrowerInfos
{
325 my $borrower = GetMember
( %info );
327 $borrower->{branchname
} = GetBranchName
( $borrower->{branchcode
} );
328 for ( qw(dateenrolled dateexpiry) ) {
329 my $userdate = $borrower->{$_};
330 unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
331 $borrower->{$_} = '';
334 $borrower->{$_} = $userdate || '';
336 $borrower->{category_description
} = GetBorrowercategory
( $borrower->{categorycode
} )->{description
};
337 my $attr_loop = C4
::Members
::Attributes
::GetBorrowerAttributes
( $borrower->{borrowernumber
} );
338 $borrower->{patron_attributes
} = $attr_loop;