3 # Copyright 2006 SAN OUEST PROVENCE et Paul POULAIN
4 # Copyright 2010 BibLibre
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
27 use List
::MoreUtils qw
/uniq/;
34 use C4
::Members
::Attributes
;
35 use C4
::Members
::AttributeTypes
;
39 use C4
::Form
::MessagingPreferences
;
40 use Koha
::AuthorisedValues
;
41 use Koha
::Patron
::Debarments
;
45 use Koha
::Patron
::Categories
;
46 use Koha
::Patron
::HouseboundRole
;
47 use Koha
::Patron
::HouseboundRoles
;
51 if ( C4
::Context
->preference('NorwegianPatronDBEnable') && C4
::Context
->preference('NorwegianPatronDBEnable') == 1 ) {
52 load Koha
::NorwegianPatronDB
, qw( NLGetSyncDataFromBorrowernumber );
54 use Koha
::SMS
::Providers
;
59 $debug = $ENV{DEBUG} || 0;
63 ($debug) or $debug = $input->param('debug') || 0;
66 my $dbh = C4::Context->dbh;
68 my ($template, $loggedinuser, $cookie)
69 = get_template_and_user({template_name => "members/memberentrygen.tt",
73 flagsrequired => {borrowers => 1},
74 debug => ($debug) ? 1 : 0,
77 if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
78 my @providers = Koha::SMS::Providers->search();
79 $template->param( sms_providers => \@providers );
82 my $guarantorid = $input->param('guarantorid');
83 my $borrowernumber = $input->param('borrowernumber');
84 my $actionType = $input->param('actionType') || '';
85 my $modify = $input->param('modify');
86 my $delete = $input->param('delete');
87 my $op = $input->param('op');
88 my $destination = $input->param('destination');
89 my $cardnumber = $input->param('cardnumber');
90 my $check_member = $input->param('check_member');
91 my $nodouble = $input->param('nodouble');
92 my $duplicate = $input->param('duplicate');
93 my $quickadd = $input->param('quickadd');
94 $nodouble = 1 if ($op eq 'modify' or $op eq 'duplicate'); # FIXME hack to represent fact that if we're
95 # modifying an existing patron, it ipso facto
96 # isn't a duplicate. Marking FIXME because this
97 # script needs to be refactored.
98 my $nok = $input->param('nok');
99 my $guarantorinfo = $input->param('guarantorinfo');
100 my $step = $input->param('step') || 0;
104 my $userenv = C4::Context->userenv;
107 ## Deal with debarments
109 debarments => GetDebarments( { borrowernumber => $borrowernumber } ) );
110 my @debarments_to_remove = $input->multi_param('remove_debarment');
111 foreach my $d ( @debarments_to_remove ) {
114 if ( $input->param('add_debarment') ) {
116 my $expiration = $input->param('debarred_expiration');
120 { 'dt' => dt_from_string($expiration), 'dateformat' => 'iso' } )
125 borrowernumber => $borrowernumber,
127 comment => scalar $input->param('debarred_comment'),
128 expiration => $expiration,
133 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
135 my $minpw = C4::Context->preference('minPasswordLength');
136 $template->param("minPasswordLength" => $minpw);
138 # function to designate mandatory fields (visually with css)
139 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
140 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
141 foreach (@field_check) {
142 $template->param( "mandatory$_" => 1);
144 # function to designate unwanted fields
145 my $check_BorrowerUnwantedField=C4::Context->preference("BorrowerUnwantedField");
146 @field_check=split(/\|/,$check_BorrowerUnwantedField);
147 foreach (@field_check) {
149 $template->param( "no$_" => 1);
151 $template->param( "add" => 1 ) if ( $op eq 'add' );
152 $template->param( "quickadd" => 1 ) if ( $quickadd );
153 $template->param( "duplicate" => 1 ) if ( $op eq 'duplicate' );
154 $template->param( "checked" => 1 ) if ( defined($nodouble) && $nodouble eq 1 );
155 ( $borrower_data = GetMember( 'borrowernumber' => $borrowernumber ) ) if ( $op eq 'modify' or $op eq 'save' or $op eq 'duplicate' );
156 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
157 my $category_type = $input->param('category_type') || '';
158 unless ($category_type or !($categorycode)){
159 my $borrowercategory = Koha::Patron::Categories->find($categorycode);
160 $category_type = $borrowercategory->category_type;
161 my $category_name = $borrowercategory->description;
162 $template->param("categoryname"=>$category_name);
164 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
166 # if a add or modify is requested => check validity of data.
167 %data = %$borrower_data if ($borrower_data);
169 # initialize %newdata
170 my %newdata; # comes from $input->param()
171 if ( $op eq 'insert' || $op eq 'modify' || $op eq 'save' || $op eq 'duplicate' ) {
172 my @names = ( $borrower_data && $op ne 'save' ) ? keys %$borrower_data : $input->param();
173 foreach my $key (@names) {
174 if (defined $input->param($key)) {
175 $newdata{$key} = $input->param($key);
176 $newdata{$key} =~ s/\"/"/g unless $key eq 'borrowernotes' or $key eq 'opacnote';
180 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
181 next unless exists $newdata{$_};
182 my $userdate = $newdata{$_} or next;
184 my $formatteddate = eval { output_pref
({ dt
=> dt_from_string
( $userdate ), dateformat
=> 'iso', dateonly
=> 1 } ); };
185 if ( $formatteddate ) {
186 $newdata{$_} = $formatteddate;
188 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
189 $template->param( "ERROR_$_" => 1 );
190 push(@errors,"ERROR_$_");
193 # check permission to modify login info.
194 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4
::Auth
::haspermission
($userenv->{'id'},{'staffaccess'=>1})) ) {
199 # remove keys from %newdata that ModMember() doesn't like
201 my @keys_to_delete = (
202 qr/^BorrowerMandatoryField$/,
211 qr/^setting_extended_patron_attributes$/,
212 qr/^setting_messaging_prefs$/,
220 for my $regexp (@keys_to_delete) {
221 for (keys %newdata) {
222 delete($newdata{$_}) if /$regexp/;
227 # Test uniqueness of surname, firstname and dateofbirth
228 if ( ( $op eq 'insert' ) and !$nodouble ) {
230 $conditions->{surname
} = $newdata{surname
} if $newdata{surname
};
231 if ( $category_type ne 'I' ) {
232 $conditions->{firstname
} = $newdata{firstname
} if $newdata{firstname
};
233 $conditions->{dateofbirth
} = $newdata{dateofbirth
} if $newdata{dateofbirth
};
236 my $patrons = Koha
::Patrons
->search($conditions);
237 if ( $patrons->count > 0) {
239 $check_member = $patrons->next->borrowernumber;
243 #recover all data from guarantor address phone ,fax...
244 if ( $guarantorid ) {
245 if (my $guarantordata=GetMember
(borrowernumber
=> $guarantorid)) {
246 $category_type = $guarantordata->{categorycode
} eq 'I' ?
'P' : 'C';
247 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
248 $newdata{'contactfirstname'}= $guarantordata->{'firstname'};
249 $newdata{'contactname'} = $guarantordata->{'surname'};
250 $newdata{'contacttitle'} = $guarantordata->{'title'};
251 if ( $op eq 'add' ) {
252 foreach (qw(streetnumber address streettype address2
253 zipcode country city state phone phonepro mobile fax email emailpro branchcode
254 B_streetnumber B_streettype B_address B_address2
255 B_city B_state B_zipcode B_country B_email B_phone)) {
256 $newdata{$_} = $guarantordata->{$_};
262 ###############test to take the right zipcode, country and city name ##############
263 # set only if parameter was passed from the form
264 $newdata{'city'} = $input->param('city') if defined($input->param('city'));
265 $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
266 $newdata{'country'} = $input->param('country') if defined($input->param('country'));
268 # builds default userid
269 # userid input text may be empty or missing because of syspref BorrowerUnwantedField
270 if ( ( defined $newdata{'userid'} && $newdata{'userid'} eq '' ) || $check_BorrowerUnwantedField =~ /userid/ ) {
271 if ( ( defined $newdata{'firstname'} ) && ( defined $newdata{'surname'} ) ) {
272 # Full page edit, firstname and surname input zones are present
273 $newdata{'userid'} = Generate_Userid
( $borrowernumber, $newdata{'firstname'}, $newdata{'surname'} );
275 elsif ( ( defined $data{'firstname'} ) && ( defined $data{'surname'} ) ) {
276 # Partial page edit (access through "Details"/"Library details" tab), firstname and surname input zones are not used
277 # Still, if the userid field is erased, we can create a new userid with available firstname and surname
278 $newdata{'userid'} = Generate_Userid
( $borrowernumber, $data{'firstname'}, $data{'surname'} );
281 $newdata{'userid'} = $data{'userid'};
285 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
286 my $extended_patron_attributes = ();
287 if ($op eq 'save' || $op eq 'insert'){
289 die "Wrong CSRF token"
290 unless Koha
::Token
->new->check_csrf({
291 session_id
=> scalar $input->cookie('CGISESSID'),
292 token
=> scalar $input->param('csrf_token'),
295 # If the cardnumber is blank, treat it as null.
296 $newdata{'cardnumber'} = undef if $newdata{'cardnumber'} =~ /^\s*$/;
298 if (my $error_code = checkcardnumber
($newdata{cardnumber
},$newdata{borrowernumber
})){
299 push @errors, $error_code == 1
300 ?
'ERROR_cardnumber_already_exists'
302 ?
'ERROR_cardnumber_length'
307 if ($op eq 'save' && $step == 3) {
308 my $borrower = C4
::Members
::GetMember
(borrowernumber
=> $borrowernumber);
309 $dateofbirth = $borrower->{dateofbirth
};
312 $dateofbirth = $newdata{dateofbirth
};
315 if ( $dateofbirth ) {
316 my $patron = Koha
::Patron
->new({ dateofbirth
=> $dateofbirth });
317 my $age = $patron->get_age;
318 my $borrowercategory = Koha
::Patron
::Categories
->find($categorycode);
319 my ($low,$high) = ($borrowercategory->dateofbirthrequired, $borrowercategory->upperagelimit);
320 if (($high && ($age > $high)) or ($age < $low)) {
321 push @errors, 'ERROR_age_limitations';
322 $template->param( age_low
=> $low);
323 $template->param( age_high
=> $high);
327 if($newdata{surname
} && C4
::Context
->preference('uppercasesurnames')) {
328 $newdata{'surname'} = uc($newdata{'surname'});
331 if (C4
::Context
->preference("IndependentBranches")) {
332 unless ( C4
::Context
->IsSuperLibrarian() ){
333 $debug and print STDERR
" $newdata{'branchcode'} : ".$userenv->{flags
}.":".$userenv->{branch
};
334 unless (!$newdata{'branchcode'} || $userenv->{branch
} eq $newdata{'branchcode'}){
335 push @errors, "ERROR_branch";
339 # Check if the 'userid' is unique. 'userid' might not always be present in
340 # the edited values list when editing certain sub-forms. Get it straight
341 # from the DB if absent.
342 my $userid = $newdata{ userid
} // $borrower_data->{ userid
};
343 unless (Check_Userid
($userid,$borrowernumber)) {
344 push @errors, "ERROR_login_exist";
347 my $password = $input->param('password');
348 my $password2 = $input->param('password2');
349 push @errors, "ERROR_password_mismatch" if ( $password ne $password2 );
350 push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
353 my $emailprimary = $input->param('email');
354 my $emailsecondary = $input->param('emailpro');
355 my $emailalt = $input->param('B_email');
358 push (@errors, "ERROR_bad_email") if (!Email
::Valid
->address($emailprimary));
360 if ($emailsecondary) {
361 push (@errors, "ERROR_bad_email_secondary") if (!Email
::Valid
->address($emailsecondary));
364 push (@errors, "ERROR_bad_email_alternative") if (!Email
::Valid
->address($emailalt));
367 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
368 $extended_patron_attributes = parse_extended_patron_attributes
($input);
369 foreach my $attr (@
$extended_patron_attributes) {
370 unless (C4
::Members
::Attributes
::CheckUniqueness
($attr->{code
}, $attr->{value
}, $borrowernumber)) {
371 my $attr_info = C4
::Members
::AttributeTypes
->fetch($attr->{code
});
372 push @errors, "ERROR_extended_unique_id_failed";
374 ERROR_extended_unique_id_failed_code
=> $attr->{code
},
375 ERROR_extended_unique_id_failed_value
=> $attr->{value
},
376 ERROR_extended_unique_id_failed_description
=> $attr_info->description()
383 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){
384 unless ($newdata{'dateexpiry'}){
385 my $patron_category = Koha
::Patron
::Categories
->find( $newdata{categorycode
} );
386 $newdata{'dateexpiry'} = $patron_category->get_expiry_date( $newdata{dateenrolled
} ) if $patron_category;
390 # BZ 14683: Do not mixup mobile [read: other phone] with smsalertnumber
391 my $sms = $input->param('SMSnumber');
392 if ( defined $sms ) {
393 $newdata{smsalertnumber
} = $sms;
396 ### Error checks should happen before this line.
397 $nok = $nok || scalar(@errors);
398 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
399 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
400 if ($op eq 'insert'){
401 # we know it's not a duplicate borrowernumber or there would already be an error
402 $borrowernumber = &AddMember
(%newdata);
403 $newdata{'borrowernumber'} = $borrowernumber;
405 # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
406 if ( C4
::Context
->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'} && $newdata{'password'}) {
407 #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
409 if (C4
::Context
->preference("AutoEmailPrimaryAddress") ne 'OFF' &&
410 $newdata{C4
::Context
->preference("AutoEmailPrimaryAddress")} =~ /\w\@\w/ ) {
411 $emailaddr = $newdata{C4
::Context
->preference("AutoEmailPrimaryAddress")}
413 elsif ($newdata{email
} =~ /\w\@\w/) {
414 $emailaddr = $newdata{email
}
416 elsif ($newdata{emailpro
} =~ /\w\@\w/) {
417 $emailaddr = $newdata{emailpro
}
419 elsif ($newdata{B_email
} =~ /\w\@\w/) {
420 $emailaddr = $newdata{B_email
}
422 # if we manage to find a valid email address, send notice
424 $newdata{emailaddr
} = $emailaddr;
427 $err = SendAlerts
( 'members', \
%newdata, "ACCTDETAILS" );
430 $template->param(error_alert
=> $@
);
431 } elsif ( ref($err) eq "HASH" && defined $err->{error
} and $err->{error
} eq "no_email" ) {
432 $template->{VARS
}->{'error_alert'} = "no_email";
434 $template->{VARS
}->{'info_alert'} = 1;
439 if (C4
::Context
->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
440 C4
::Members
::Attributes
::SetBorrowerAttributes
($borrowernumber, $extended_patron_attributes);
442 if (C4
::Context
->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
443 C4
::Form
::MessagingPreferences
::handle_form_action
($input, { borrowernumber
=> $borrowernumber }, $template, 1, $newdata{'categorycode'});
445 # Try to do the live sync with the Norwegian national patron database, if it is enabled
446 if ( exists $data{'borrowernumber'} && C4
::Context
->preference('NorwegianPatronDBEnable') && C4
::Context
->preference('NorwegianPatronDBEnable') == 1 ) {
447 NLSync
({ 'borrowernumber' => $borrowernumber });
450 # Create HouseboundRole if necessary.
451 # Borrower did not exist, so HouseboundRole *cannot* yet exist.
452 my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
453 $hsbnd_chooser = 1 if $input->param('housebound_chooser');
454 $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
455 # Only create a HouseboundRole if patron has a role.
456 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
457 Koha
::Patron
::HouseboundRole
->new({
458 borrowernumber_id
=> $borrowernumber,
459 housebound_chooser
=> $hsbnd_chooser,
460 housebound_deliverer
=> $hsbnd_deliverer,
464 } elsif ($op eq 'save') {
466 # Update or create our HouseboundRole if necessary.
467 my $housebound_role = Koha
::Patron
::HouseboundRoles
->find($borrowernumber);
468 my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
469 $hsbnd_chooser = 1 if $input->param('housebound_chooser');
470 $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
471 if ( $housebound_role ) {
472 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
473 # Update our HouseboundRole.
475 ->housebound_chooser($hsbnd_chooser)
476 ->housebound_deliverer($hsbnd_deliverer)
479 $housebound_role->delete; # No longer needed.
482 # Only create a HouseboundRole if patron has a role.
483 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
484 $housebound_role = Koha
::Patron
::HouseboundRole
->new({
485 borrowernumber_id
=> $borrowernumber,
486 housebound_chooser
=> $hsbnd_chooser,
487 housebound_deliverer
=> $hsbnd_deliverer,
492 if ($NoUpdateLogin) {
493 delete $newdata{'password'};
494 delete $newdata{'userid'};
496 &ModMember
(%newdata) unless scalar(keys %newdata) <= 1; # bug 4508 - avoid crash if we're not
497 # updating any columns in the borrowers table,
498 # which can happen if we're only editing the
499 # patron attributes or messaging preferences sections
500 if (C4
::Context
->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
501 C4
::Members
::Attributes
::SetBorrowerAttributes
($borrowernumber, $extended_patron_attributes);
503 if (C4
::Context
->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
504 C4
::Form
::MessagingPreferences
::handle_form_action
($input, { borrowernumber
=> $borrowernumber }, $template);
507 print scalar ($destination eq "circ") ?
508 $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
509 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
510 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
514 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
515 exit; # same as above
518 if ($nok or !$nodouble){
519 $op="add" if ($op eq "insert");
520 $op="modify" if ($op eq "save");
522 $template->param( updtype
=> ($op eq 'add' ?
'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
524 $template->param( step_1
=> 1,step_2
=> 1,step_3
=> 1, step_4
=> 1, step_5
=> 1, step_6
=> 1);
527 if (C4
::Context
->preference("IndependentBranches")) {
528 my $userenv = C4
::Context
->userenv;
529 if ( !C4
::Context
->IsSuperLibrarian() && $data{'branchcode'} ) {
530 unless ($userenv->{branch
} eq $data{'branchcode'}){
531 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
537 $template->param( updtype
=> 'I', step_1
=>1, step_2
=>1, step_3
=>1, step_4
=>1, step_5
=> 1, step_6
=> 1);
539 if ($op eq "modify") {
540 $template->param( updtype
=> 'M',modify
=> 1 );
541 $template->param( step_1
=>1, step_2
=>1, step_3
=>1, step_4
=>1, step_5
=> 1, step_6
=> 1) unless $step;
543 $template->param( categorycode
=> $borrower_data->{'categorycode'} );
545 # Add sync data to the user data
546 if ( C4
::Context
->preference('NorwegianPatronDBEnable') && C4
::Context
->preference('NorwegianPatronDBEnable') == 1 ) {
547 my $sync = NLGetSyncDataFromBorrowernumber
( $borrowernumber );
555 if ( $op eq "duplicate" ) {
556 $template->param( updtype
=> 'I' );
557 $template->param( step_1
=> 1, step_2
=> 1, step_3
=> 1, step_4
=> 1, step_5
=> 1, step_6
=> 1 ) unless $step;
558 $data{'cardnumber'} = "";
561 if(!defined($data{'sex'})){
562 $template->param( none
=> 1);
563 } elsif($data{'sex'} eq 'F'){
564 $template->param( female
=> 1);
565 } elsif ($data{'sex'} eq 'M'){
566 $template->param( male
=> 1);
568 $template->param( none
=> 1);
571 ##Now all the data to modify a member.
574 my $no_categories = 1;
576 foreach my $category_type (qw(C A S P I X)) {
577 my $patron_categories = Koha
::Patron
::Categories
->search_limited({ category_type
=> $category_type }, {order_by
=> ['categorycode']});
578 $no_categories = 0 if $patron_categories->count > 0;
581 while ( my $patron_category = $patron_categories->next ) {
583 { 'categorycode' => $patron_category->categorycode,
584 'categoryname' => $patron_category->description,
585 'categorycodeselected' =>
586 ( ( defined( $borrower_data->{'categorycode'} ) && $patron_category->categorycode eq $borrower_data->{'categorycode'} ) || ( defined($categorycode) && $patron_category->categorycode eq $categorycode ) ),
590 $typehash{'typename'} = $category_type;
591 my $typedescription = "typename_" . $typehash{'typename'};
592 $typehash{'categoryloop'} = \
@categoryloop;
594 { 'typename' => $category_type,
595 $typedescription => 1,
596 'categoryloop' => \
@categoryloop
600 $template->param('typeloop' => \
@typeloop,
601 no_categories
=> $no_categories);
602 if($no_categories){ $no_add = 1; }
605 my $cities = Koha
::Cities
->search( {}, { order_by
=> 'city_name' } );
606 my $roadtypes = C4
::Koha
::GetAuthorisedValues
( 'ROADTYPE' );
608 roadtypes
=> $roadtypes,
612 my $default_borrowertitle = '';
613 unless ( $op eq 'duplicate' ) { $default_borrowertitle=$data{'title'} }
615 my @relationships = split /,|\|/, C4
::Context
->preference('borrowerRelationship');
617 while (@relationships) {
618 my $relship = shift @relationships || '';
619 my %row = ('relationship' => $relship);
620 if (defined($data{'relationship'}) and $data{'relationship'} eq $relship) {
621 $row{'selected'}=' selected';
625 push(@relshipdata, \
%row);
628 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
633 foreach (keys(%flags)) {
635 my %row = ('key' => $key,
636 'name' => $flags{$key}[0]);
638 $row{'yes'}=' checked';
643 $row{'no'}=' checked';
645 push @flagdata,\
%row;
649 # in modify mod: userbranch value comes from borrowers table
650 # in add mod: userbranch value comes from branches table (ip correspondence)
653 if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'}) {
654 $userbranch = C4
::Context
->userenv->{'branch'};
657 if (defined ($data{'branchcode'}) and ( $op eq 'modify' || $op eq 'duplicate' || ( $op eq 'add' && $category_type eq 'C' ) )) {
658 $userbranch = $data{'branchcode'};
660 $template->param( userbranch
=> $userbranch );
662 if ( Koha
::Libraries
->search->count < 1 ){
664 $template->param(no_branches
=> 1);
668 $template->param(no_categories
=> 1);
670 $template->param(no_add
=> $no_add);
671 # --------------------------------------------------------------------------------------------------------
673 $template->param( sort1
=> $data{'sort1'});
674 $template->param( sort2
=> $data{'sort2'});
677 foreach my $error (@errors) {
678 $template->param($error) || $template->param( $error => 1);
680 $template->param(nok
=> 1);
683 #Formatting data for display
685 if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){
686 $data{'dateenrolled'} = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
688 if ( $op eq 'duplicate' ) {
689 $data{'dateenrolled'} = output_pref
({ dt
=> dt_from_string
, dateformat
=> 'iso', dateonly
=> 1 });
690 my $patron_category = Koha
::Patron
::Categories
->find( $data{categorycode
} );
691 $data{dateexpiry
} = $patron_category->get_expiry_date( $data{dateenrolled
} );
693 if (C4
::Context
->preference('uppercasesurnames')) {
694 $data{'surname'} &&= uc( $data{'surname'} );
695 $data{'contactname'} &&= uc( $data{'contactname'} );
698 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
700 $data{$_} = eval { output_pref
({ dt
=> dt_from_string
( $data{$_} ), dateonly
=> 1 } ); }; # back to syspref for display
702 $template->param( $_ => $data{$_});
705 if (C4
::Context
->preference('ExtendedPatronAttributes')) {
706 $template->param(ExtendedPatronAttributes
=> 1);
707 patron_attributes_form
($template, $borrowernumber);
710 if (C4
::Context
->preference('EnhancedMessagingPreferences')) {
712 C4
::Form
::MessagingPreferences
::set_form_values
({ categorycode
=> $categorycode }, $template);
714 C4
::Form
::MessagingPreferences
::set_form_values
({ borrowernumber
=> $borrowernumber }, $template);
716 $template->param(SMSSendDriver
=> C4
::Context
->preference("SMSSendDriver"));
717 $template->param(SMSnumber
=> $data{'smsalertnumber'} );
718 $template->param(TalkingTechItivaPhone
=> C4
::Context
->preference("TalkingTechItivaPhoneNotification"));
721 $template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ?
0 : 1); # associate with step to know where you are
722 $debug and warn "memberentry step: $step";
723 $template->param(%data);
724 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
725 $template->param( step
=> $step ) if $step; # associate with step to know where u are
728 BorrowerMandatoryField
=> C4
::Context
->preference("BorrowerMandatoryField"),#field to test with javascript
729 category_type
=> $category_type,#to know the category type of the borrower
730 "$category_type" => 1,# associate with step to know where u are
731 destination
=> $destination,#to know wher u come from and wher u must go in redirect
732 check_member
=> $check_member,#to know if the borrower already exist(=>1) or not (=>0)
736 nodouble
=> $nodouble,
737 borrowernumber
=> $borrowernumber, #register number
738 guarantorid
=> ($borrower_data->{'guarantorid'} || $guarantorid),
739 relshiploop
=> \
@relshipdata,
740 btitle
=> $default_borrowertitle,
741 guarantorinfo
=> $guarantorinfo,
742 flagloop
=> \
@flagdata,
743 category_type
=>$category_type,
745 nok
=> $nok,#flag to know if an error
746 NoUpdateLogin
=> $NoUpdateLogin,
749 # Generate CSRF token
750 $template->param( csrf_token
=>
751 Koha
::Token
->new->generate_csrf( { session_id
=> scalar $input->cookie('CGISESSID'), } ),
754 # HouseboundModule data
756 housebound_role
=> Koha
::Patron
::HouseboundRoles
->find($borrowernumber),
759 if(defined($data{'flags'})){
760 $template->param(flags
=>$data{'flags'});
762 if(defined($data{'contacttitle'})){
763 $template->param("contacttitle_" . $data{'contacttitle'} => "SELECTED");
767 my ( $min, $max ) = C4
::Members
::get_cardnumber_length
();
768 if ( defined $min ) {
770 minlength_cardnumber
=> $min,
771 maxlength_cardnumber
=> $max
775 output_html_with_http_headers
$input, $cookie, $template->output;
777 sub parse_extended_patron_attributes
{
779 my @patron_attr = grep { /^patron_attr_\d+$/ } $input->multi_param();
783 foreach my $key (@patron_attr) {
784 my $value = $input->param($key);
785 next unless defined($value) and $value ne '';
786 my $code = $input->param("${key}_code");
787 next if exists $dups{$code}->{$value};
788 $dups{$code}->{$value} = 1;
789 push @attr, { code
=> $code, value
=> $value };
794 sub patron_attributes_form
{
795 my $template = shift;
796 my $borrowernumber = shift;
798 my @types = C4
::Members
::AttributeTypes
::GetAttributeTypes
();
799 if (scalar(@types) == 0) {
800 $template->param(no_patron_attribute_types
=> 1);
803 my $attributes = C4
::Members
::Attributes
::GetBorrowerAttributes
($borrowernumber);
804 my @classes = uniq
( map {$_->{class}} @
$attributes );
805 @classes = sort @classes;
807 # map patron's attributes into a more convenient structure
809 foreach my $attr (@
$attributes) {
810 push @
{ $attr_hash{$attr->{code
}} }, $attr;
813 my @attribute_loop = ();
816 foreach my $type_code (map { $_->{code
} } @types) {
817 my $attr_type = C4
::Members
::AttributeTypes
->fetch($type_code);
819 class => $attr_type->class(),
820 code
=> $attr_type->code(),
821 description
=> $attr_type->description(),
822 repeatable
=> $attr_type->repeatable(),
823 category
=> $attr_type->authorised_value_category(),
824 category_code
=> $attr_type->category_code(),
826 if (exists $attr_hash{$attr_type->code()}) {
827 foreach my $attr (@
{ $attr_hash{$attr_type->code()} }) {
828 my $newentry = { %$entry };
829 $newentry->{value
} = $attr->{value
};
830 $newentry->{use_dropdown
} = 0;
831 if ($attr_type->authorised_value_category()) {
832 $newentry->{use_dropdown
} = 1;
833 $newentry->{auth_val_loop
} = GetAuthorisedValues
($attr_type->authorised_value_category(), $attr->{value
});
836 $newentry->{form_id
} = "patron_attr_$i";
837 push @
{$items_by_class{$attr_type->class()}}, $newentry;
841 my $newentry = { %$entry };
842 if ($attr_type->authorised_value_category()) {
843 $newentry->{use_dropdown
} = 1;
844 $newentry->{auth_val_loop
} = GetAuthorisedValues
($attr_type->authorised_value_category());
846 $newentry->{form_id
} = "patron_attr_$i";
847 push @
{$items_by_class{$attr_type->class()}}, $newentry;
850 while ( my ($class, @items) = each %items_by_class ) {
851 my $av = Koha
::AuthorisedValues
->search({ category
=> 'PA_CLASS', authorised_value
=> $class });
852 my $lib = $av->count ?
$av->next->lib : $class;
853 push @attribute_loop, {
860 $template->param(patron_attributes
=> \
@attribute_loop);