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 debarred debarredcomment 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/;
39 my $op = $input->param('op') || 'show_form';
40 my ( $template, $loggedinuser, $cookie ) = get_template_and_user
(
41 { template_name
=> "tools/modborrowers.tmpl",
45 flagsrequired
=> { tools
=> "edit_patrons" },
49 my %cookies = parse CGI
::Cookie
($cookie);
50 my $sessionID = $cookies{'CGISESSID'}->value;
51 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');
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;
71 if ( my $list = $input->param('cardnumberlist') ) {
72 push @cardnumbers, split( /\s\n/, $list );
77 for my $cardnumber ( @cardnumbers ) {
78 my $borrower = GetBorrowerInfos
( cardnumber
=> $cardnumber );
80 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
81 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
82 push @borrowers, $borrower;
84 push @notfoundcardnumbers, $cardnumber;
88 # Just for a correct display
89 for my $borrower ( @borrowers ) {
90 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
91 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
94 # Construct the patron attributes list
95 my @patron_attributes_values;
96 my @patron_attributes_codes;
97 my $patron_attribute_types = C4
::Members
::AttributeTypes
::GetAttributeTypes_hashref
('all');
98 my $patron_categories = C4
::Members
::GetBorrowercategoryList
;
99 for ( values %$patron_attribute_types ) {
100 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_->{code
} );
101 my $options = $attr_type->authorised_value_category
102 ? GetAuthorisedValues
( $attr_type->authorised_value_category )
104 push @patron_attributes_values,
106 attribute_code
=> $_->{code
},
110 my $category_code = $_->{category_code
};
111 my ( $category_lib ) = map {
112 ( defined $category_code and $_->{categorycode
} eq $category_code ) ?
$_->{description
} : ()
113 } @
$patron_categories;
114 push @patron_attributes_codes,
116 attribute_code
=> $_->{code
},
117 attribute_lib
=> $_->{description
},
118 category_lib
=> $category_lib,
119 type
=> $attr_type->authorised_value_category ?
'select' : 'text',
123 my @attributes_header = ();
124 for ( 1 .. scalar( $max_nb_attr ) ) {
125 push @attributes_header, { attribute
=> "Attributes $_" };
127 $template->param( borrowers
=> \
@borrowers );
128 $template->param( attributes_header
=> \
@attributes_header );
129 @notfoundcardnumbers = map { { cardnumber
=> $_ } } @notfoundcardnumbers;
130 $template->param( notfoundcardnumbers
=> \
@notfoundcardnumbers )
131 if @notfoundcardnumbers;
133 # Construct drop-down list values
134 my $branches = GetBranchesLoop
;
136 push @branches_option, { value
=> $_->{value
}, lib
=> $_->{branchname
} } for @
$branches;
137 unshift @branches_option, { value
=> "", lib
=> "" };
138 my $categories = GetBorrowercategoryList
;
139 my @categories_option;
140 push @categories_option, { value
=> $_->{categorycode
}, lib
=> $_->{description
} } for @
$categories;
141 unshift @categories_option, { value
=> "", lib
=> "" };
142 my $bsort1 = GetAuthorisedValues
("Bsort1");
144 push @sort1_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort1;
145 unshift @sort1_option, { value
=> "", lib
=> "" }
147 my $bsort2 = GetAuthorisedValues
("Bsort2");
149 push @sort2_option, { value
=> $_->{authorised_value
}, lib
=> $_->{lib
} } for @
$bsort2;
150 unshift @sort2_option, { value
=> "", lib
=> "" }
153 my @mandatoryFields = split( /\|/, C4
::Context
->preference("BorrowerMandatoryField") );
160 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0
167 mandatory
=> ( grep /surname/, @mandatoryFields ) ?
1 : 0,
171 name
=> "branchcode",
174 option
=> \
@branches_option,
175 mandatory
=> ( grep /branchcode/, @mandatoryFields ) ?
1 : 0,
179 name
=> "categorycode",
182 option
=> \
@categories_option,
183 mandatory
=> ( grep /categorycode/, @mandatoryFields ) ?
1 : 0,
189 type
=> @sort1_option ?
"select" : "text",
190 option
=> \
@sort1_option,
191 mandatory
=> ( grep /sort1/, @mandatoryFields ) ?
1 : 0,
197 type
=> @sort2_option ?
"select" : "text",
198 option
=> \
@sort2_option,
199 mandatory
=> ( grep /sort2/, @mandatoryFields ) ?
1 : 0,
203 name
=> "dateenrolled",
204 lib
=> "Date enrolled",
206 mandatory
=> ( grep /dateenrolled/, @mandatoryFields ) ?
1 : 0,
210 name
=> "dateexpiry",
211 lib
=> "Date expiry",
213 mandatory
=> ( grep /dateexpiry/, @mandatoryFields ) ?
1 : 0,
220 mandatory
=> ( grep /debarred/, @mandatoryFields ) ?
1 : 0,
224 name
=> "debarredcomment",
225 lib
=> "Debarred comment",
227 mandatory
=> ( grep /debarredcomment/, @mandatoryFields ) ?
1 : 0,
231 name
=> "borrowernotes",
232 lib
=> "Borrower Notes",
234 mandatory
=> ( grep /borrowernotes/, @mandatoryFields ) ?
1 : 0,
238 $template->param('patron_attributes_codes', \
@patron_attributes_codes);
239 $template->param('patron_attributes_values', \
@patron_attributes_values);
241 $template->param( fields
=> \
@fields );
242 $template->param( DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar() );
245 # Process modifications
248 my @disabled = $input->param('disable_input');
250 for my $field ( qw
/surname firstname branchcode categorycode sort1 sort2 dateenrolled dateexpiry debarred debarredcomment borrowernotes/ ) {
251 my $value = $input->param($field);
252 $infos->{$field} = $value if $value;
253 $infos->{$field} = "" if grep { /^$field$/ } @disabled;
256 my @attributes = $input->param('patron_attributes');
257 my @attr_values = $input->param('patron_attributes_value');
260 my @borrowernumbers = $input->param('borrowernumber');
261 # For each borrower selected
262 for my $borrowernumber ( @borrowernumbers ) {
263 # If at least one field are filled, we want to modify the borrower
264 if ( defined $infos ) {
265 $infos->{borrowernumber
} = $borrowernumber;
266 my $success = ModMember
(%$infos);
267 push @errors, { error
=> "can_not_update", borrowernumber
=> $infos->{borrowernumber
} } if not $success;
271 my $borrower_categorycode = GetBorrowerCategorycode
$borrowernumber;
273 for ( @attributes ) {
275 $attribute->{code
} = $_;
276 $attribute->{attribute
} = $attr_values[$i];
277 my $attr_type = C4
::Members
::AttributeTypes
->fetch( $_ );
278 # If this borrower is not in the category of this attribute, we don't want to modify this attribute
279 ++$i and next if $attr_type->{category_code
} and $attr_type->{category_code
} ne $borrower_categorycode;
280 my $valuename = "attr" . $i . "_value";
281 if ( grep { /^$valuename$/ } @disabled ) {
282 # The attribute is disabled, we remove it for this borrower !
284 C4
::Members
::Attributes
::DeleteBorrowerAttribute
( $borrowernumber, $attribute );
286 push @errors, { error
=> $@
} if $@
;
288 # Attribute's value is empty, we don't want to modify it
289 ++$i and next if not $attribute->{attribute
};
292 C4
::Members
::Attributes
::UpdateBorrowerAttribute
( $borrowernumber, $attribute );
294 push @errors, { error
=> $@
} if $@
;
299 $op = "show_results"; # We have process modifications, the user want to view its
301 # Construct the results list
304 for my $borrowernumber ( @borrowernumbers ) {
305 my $borrower = GetBorrowerInfos
( borrowernumber
=> $borrowernumber );
307 $max_nb_attr = scalar( @
{ $borrower->{patron_attributes
} } )
308 if scalar( @
{ $borrower->{patron_attributes
} } ) > $max_nb_attr;
309 push @borrowers, $borrower;
312 my @patron_attributes_option;
313 for my $borrower ( @borrowers ) {
314 push @patron_attributes_option, { value
=> "$_->{code}", lib
=> $_->{code
} } for @
{ $borrower->{patron_attributes
} };
315 my $length = scalar( @
{ $borrower->{patron_attributes
} } );
316 push @
{ $borrower->{patron_attributes
} }, {} for ( $length .. $max_nb_attr - 1);
319 my @attributes_header = ();
320 for ( 1 .. scalar( $max_nb_attr ) ) {
321 push @attributes_header, { attribute
=> "Attributes $_" };
324 $template->param( borrowers
=> \
@borrowers );
325 $template->param( attributes_header
=> \
@attributes_header );
327 $template->param( borrowers
=> \
@borrowers );
328 $template->param( errors
=> \
@errors );
334 output_html_with_http_headers
$input, $cookie, $template->output;
337 sub GetBorrowerInfos
{
339 my $borrower = GetMember
( %info );
341 $borrower->{branchname
} = GetBranchName
( $borrower->{branchcode
} );
342 for ( qw(dateenrolled dateexpiry debarred) ) {
343 my $userdate = $borrower->{$_};
344 unless ($userdate && $userdate ne "0000-00-00" and $userdate ne "9999-12-31") {
345 $borrower->{$_} = '';
348 $borrower->{$_} = $userdate || '';
350 $borrower->{category_description
} = GetBorrowercategory
( $borrower->{categorycode
} )->{description
};
351 my $attr_loop = C4
::Members
::Attributes
::GetBorrowerAttributes
( $borrower->{borrowernumber
} );
352 $borrower->{patron_attributes
} = $attr_loop;