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>.
29 use C4::Auth qw( get_template_and_user haspermission );
31 use C4::Output qw( output_and_exit output_and_exit_if_error output_html_with_http_headers );
32 use C4::Members qw( checkcardnumber get_cardnumber_length );
33 use C4::Koha qw( GetAuthorisedValues );
34 use C4::Letters qw( GetPreparedLetter EnqueueLetter SendQueuedMessages );
35 use C4::Form::MessagingPreferences;
37 use Koha::AuthorisedValues;
39 use Koha::Patron::Debarments qw( AddDebarment DelDebarment );
40 use Koha::Patron::Restriction::Types;
42 use Koha::DateUtils qw( dt_from_string );
45 use Koha::Patron::Attribute::Types;
46 use Koha::Patron::Categories;
47 use Koha::Patron::HouseboundRole;
48 use Koha::Patron::HouseboundRoles;
51 use Koha::SMS::Providers;
56 my $dbh = C4::Context->dbh;
58 my ($template, $loggedinuser, $cookie)
59 = get_template_and_user({template_name => "members/memberentrygen.tt",
62 flagsrequired => {borrowers => 'edit_borrowers'},
65 my $borrowernumber = $input->param('borrowernumber');
66 my $patron = Koha::Patrons->find($borrowernumber);
68 if ( $borrowernumber and not $patron ) {
69 output_and_exit( $input, $cookie, $template, 'unknown_patron' );
72 if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
73 my @providers = Koha::SMS::Providers->search( {}, { order_by => 'name' } )->as_list;
74 $template->param( sms_providers => \@providers );
77 my $actionType = $input->param('actionType') || '';
78 my $modify = $input->param('modify');
79 my $delete = $input->param('delete');
80 my $op = $input->param('op');
81 my $destination = $input->param('destination');
82 my $cardnumber = $input->param('cardnumber');
83 my $check_member = $input->param('check_member');
84 my $nodouble = $input->param('nodouble');
85 my $duplicate = $input->param('duplicate');
86 my $quickadd = $input->param('quickadd');
87 $nodouble = 1 if ($op eq 'modify' or $op eq 'duplicate'); # FIXME hack to represent fact that if we're
88 # modifying an existing patron, it ipso facto
89 # isn't a duplicate. Marking FIXME because this
90 # script needs to be refactored.
91 my $nok = $input->param('nok');
92 my $step = $input->param('step') || 0;
97 my $CanUpdatePasswordExpiration;
98 my $userenv = C4::Context->userenv;
101 ## Deal with guarantor stuff
102 $template->param( relationships => $patron->guarantor_relationships ) if $patron;
104 my @relations = split /\|/, C4::Context->preference('borrowerRelationship'), -1;
105 @relations = ('') unless @relations;
106 my $empty_relationship_allowed = grep {$_ eq ""} @relations;
107 $template->param( empty_relationship_allowed => $empty_relationship_allowed );
109 my $guarantor_id = $input->param('guarantor_id');
110 my $guarantor = undef;
111 $guarantor = Koha::Patrons->find( $guarantor_id ) if $guarantor_id;
112 $template->param( guarantor => $guarantor );
114 my @delete_guarantor = $input->multi_param('delete_guarantor');
115 foreach my $id ( @delete_guarantor ) {
116 my $r = Koha::Patron::Relationships->find( $id );
120 ## Deal with debarments
122 restriction_types => scalar Koha::Patron::Restriction::Types->search()
124 my @debarments_to_remove = $input->multi_param('remove_debarment');
125 foreach my $d ( @debarments_to_remove ) {
128 if ( $input->param('add_debarment') ) {
130 my $expiration = $input->param('debarred_expiration');
133 ? dt_from_string($expiration)->ymd
138 borrowernumber => $borrowernumber,
139 type => scalar $input->param('debarred_type') // 'MANUAL',
140 comment => scalar $input->param('debarred_comment'),
141 expiration => $expiration,
146 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
148 # function to designate mandatory fields (visually with css)
149 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
150 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
151 foreach (@field_check) {
152 $template->param( "mandatory$_" => 1 );
154 # function to designate unwanted fields
155 my $check_BorrowerUnwantedField=C4::Context->preference("BorrowerUnwantedField");
156 @field_check=split(/\|/,$check_BorrowerUnwantedField);
157 foreach (@field_check) {
159 $template->param( "no$_" => 1 );
161 $template->param( "add" => 1 ) if ( $op eq 'add' );
162 $template->param( "quickadd" => 1 ) if ( $quickadd );
163 $template->param( "duplicate" => 1 ) if ( $op eq 'duplicate' );
164 $template->param( "checked" => 1 ) if ( defined($nodouble) && $nodouble eq 1 );
165 if ( $op eq 'modify' or $op eq 'save' or $op eq 'duplicate' ) {
166 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
167 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
169 # check permission to modify email info.
170 if ( $patron->is_superlibrarian && !$logged_in_user->is_superlibrarian ) {
173 if ($logged_in_user->is_superlibrarian) {
174 $CanUpdatePasswordExpiration = 1;
177 $borrower_data = $patron->unblessed;
180 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
181 my $category = Koha::Patron::Categories->find($categorycode);
182 $template->param( patron_category => $category );
184 # if a add or modify is requested => check validity of data.
185 %data = %$borrower_data if ($borrower_data);
187 # initialize %newdata
188 my %newdata; # comes from $input->param()
189 if ( $op eq 'insert' || $op eq 'modify' || $op eq 'save' || $op eq 'duplicate' ) {
190 my @names = ( $borrower_data && $op ne 'save' ) ? keys %$borrower_data : $input->param();
191 foreach my $key (@names) {
192 if (defined $input->param($key)) {
193 $newdata{$key} = $input->param($key);
197 # check permission to modify login info.
198 if (ref($borrower_data) && ($category->category_type eq 'S') && ! (C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) ) {
203 # remove keys from %newdata that is not part of patron's attributes
205 my @keys_to_delete = (
206 qr/^(borrowernumber|date_renewed|debarred|debarredcomment|flags|privacy|updated_on|lastseen|login_attempts|overdrive_auth_token|anonymized)$/, # Bug 28935
207 qr/^BorrowerMandatoryField$/,
215 qr/^setting_extended_patron_attributes$/,
216 qr/^setting_messaging_prefs$/,
224 qr/^add_debarment$/, qr/^debarred_comment$/,qr/^debarred_expiration$/, qr/^debarred_type$/, qr/^remove_debarment$/, # We already dealt with debarments previously
225 qr/^housebound_chooser$/, qr/^housebound_deliverer$/,
228 qr/^guarantor_firstname$/,
229 qr/^guarantor_surname$/,
230 qr/^delete_guarantor$/,
232 push @keys_to_delete, map { qr/^$_$/ } split( /\s*\|\s*/, C4::Context->preference('BorrowerUnwantedField') || q{} );
233 push @keys_to_delete, qr/^password_expiration_date$/ unless $CanUpdatePasswordExpiration;
234 for my $regexp (@keys_to_delete) {
235 for (keys %newdata) {
236 delete($newdata{$_}) if /$regexp/;
241 # Test uniqueness of surname, firstname and dateofbirth
242 if ( ( $op eq 'insert' ) and !$nodouble ) {
243 my @dup_fields = split '\|', C4::Context->preference('PatronDuplicateMatchingAddFields');
245 for my $f ( @dup_fields ) {
246 $conditions->{$f} = $newdata{$f} if $newdata{$f};
249 my $patrons = Koha::Patrons->search($conditions); # FIXME Should be search_limited?
250 if ( $patrons->count > 0) {
252 $check_member = $patrons->next->borrowernumber;
256 ###############test to take the right zipcode, country and city name ##############
257 # set only if parameter was passed from the form
258 $newdata{'city'} = $input->param('city') if defined($input->param('city'));
259 $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
260 $newdata{'country'} = $input->param('country') if defined($input->param('country'));
262 $newdata{'lang'} = $input->param('lang') if defined($input->param('lang'));
264 # Bug 32426 removed the fake_patron code here that filled $newdata{userid}. We should leave it now to patron->store.
266 my $extended_patron_attributes;
267 if ($op eq 'save' || $op eq 'insert'){
269 output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
270 unless Koha::Token->new->check_csrf({
271 session_id => scalar $input->cookie('CGISESSID'),
272 token => scalar $input->param('csrf_token'),
275 # If the cardnumber is blank, treat it as null.
276 $newdata{'cardnumber'} = undef if $newdata{'cardnumber'} =~ /^\s*$/;
278 my $new_barcode = $newdata{'cardnumber'};
279 Koha::Plugins->call( 'patron_barcode_transform', \$new_barcode );
281 $newdata{'cardnumber'} = $new_barcode;
283 if (my $error_code = checkcardnumber( $newdata{cardnumber}, $borrowernumber )){
284 push @errors, $error_code == 1
285 ? 'ERROR_cardnumber_already_exists'
287 ? 'ERROR_cardnumber_length'
292 if ($op eq 'save' && $step == 3) {
293 $dateofbirth = $patron->dateofbirth;
296 $dateofbirth = $newdata{dateofbirth};
299 if ( $dateofbirth ) {
300 my $patron = Koha::Patron->new({ dateofbirth => $dateofbirth });
301 my $age = $patron->get_age;
302 my ($low,$high) = ($category->dateofbirthrequired, $category->upperagelimit);
303 if (($high && ($age > $high)) or ($age < $low)) {
304 push @errors, 'ERROR_age_limitations';
305 $template->param( age_low => $low);
306 $template->param( age_high => $high);
310 if (C4::Context->preference("IndependentBranches")) {
311 unless ( C4::Context->IsSuperLibrarian() ){
312 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
313 push @errors, "ERROR_branch";
318 # Bug 32426 removed the userid unique-check here. Postpone it to patron->store.
320 my $password = $input->param('password');
321 my $password2 = $input->param('password2');
322 push @errors, "ERROR_password_mismatch" if ( $password ne $password2 );
324 if ( $password and $password ne '****' ) {
325 my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $password, $category );
326 unless ( $is_valid ) {
327 push @errors, 'ERROR_password_too_short' if $error eq 'too_short';
328 push @errors, 'ERROR_password_too_weak' if $error eq 'too_weak';
329 push @errors, 'ERROR_password_has_whitespaces' if $error eq 'has_whitespaces';
334 my $emailprimary = $input->param('email');
335 my $emailsecondary = $input->param('emailpro');
336 my $emailalt = $input->param('B_email');
339 push (@errors, "ERROR_bad_email") unless Koha::Email->is_valid($emailprimary);
341 if ($emailsecondary) {
342 push (@errors, "ERROR_bad_email_secondary") unless Koha::Email->is_valid($emailsecondary);
345 push (@errors, "ERROR_bad_email_alternative") unless Koha::Email->is_valid($emailalt);
348 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
349 $extended_patron_attributes = parse_extended_patron_attributes($input);
350 for my $attr ( @$extended_patron_attributes ) {
351 $attr->{borrowernumber} = $borrowernumber if $borrowernumber;
352 my $attribute = Koha::Patron::Attribute->new($attr);
353 if ( !$attribute->unique_ok ) {
354 push @errors, "ERROR_extended_unique_id_failed";
355 my $attr_type = Koha::Patron::Attribute::Types->find($attr->{code});
357 ERROR_extended_unique_id_failed_code => $attr->{code},
358 ERROR_extended_unique_id_failed_value => $attr->{attribute},
359 ERROR_extended_unique_id_failed_description => $attr_type->description()
365 elsif ( $borrowernumber ) {
366 $extended_patron_attributes = Koha::Patrons->find($borrowernumber)->extended_attributes->unblessed;
369 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){
370 unless ($newdata{'dateexpiry'}){
371 $newdata{'dateexpiry'} = $category->get_expiry_date( $newdata{dateenrolled} ) if $category;
375 # BZ 14683: Do not mixup mobile [read: other phone] with smsalertnumber
376 my $sms = $input->param('SMSnumber');
377 if ( defined $sms ) {
378 $newdata{smsalertnumber} = $sms;
381 ### Error checks should happen before this line.
382 $nok = $nok || scalar(@errors);
383 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
385 if ($op eq 'insert'){
386 # we know it's not a duplicate borrowernumber or there would already be an error
387 delete $newdata{password2};
390 Koha::Patron->new(\%newdata)->store;
394 if( ref($_) eq 'Koha::Exceptions::Patron::InvalidUserid' ) {
395 push @errors, "ERROR_login_exist";
397 # FIXME Urgent error handling here, we cannot fail without relevant feedback
398 # Lot of code will need to be removed from this script to handle exceptions raised by Koha::Patron->store
399 warn "Patron creation failed! - $_"; # Maybe we must die instead of just warn
400 push @messages, {error => 'error_on_insert_patron'};
407 add_guarantors( $patron, $input );
408 $borrowernumber = $patron->borrowernumber;
409 $newdata{'borrowernumber'} = $borrowernumber;
410 delete $newdata{password};
413 # If 'AutoEmailNewUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
414 if ( $patron && C4::Context->preference("AutoEmailNewUser") ) {
415 #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
416 my $emailaddr = $patron->notice_email_address;
417 # if we manage to find a valid email address, send notice
420 my $letter = GetPreparedLetter(
422 letter_code => 'WELCOME',
423 branchcode => $patron->branchcode,,
424 lang => $patron->lang || 'default',
426 'branches' => $patron->branchcode,
427 'borrowers' => $patron->borrowernumber,
432 my $message_id = EnqueueLetter(
435 borrowernumber => $patron->id,
436 to_address => $emailaddr,
437 message_transport_type => 'email'
440 SendQueuedMessages( { message_id => $message_id } ) if $message_id;
443 $template->param( error_alert => $@ );
448 if ( $patron && (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) ) {
449 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template, 1, $newdata{'categorycode'});
452 # Create HouseboundRole if necessary.
453 # Borrower did not exist, so HouseboundRole *cannot* yet exist.
454 my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
455 $hsbnd_chooser = 1 if $input->param('housebound_chooser');
456 $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
457 # Only create a HouseboundRole if patron has a role.
458 if ( $patron && ( $hsbnd_chooser || $hsbnd_deliverer ) ) {
459 Koha::Patron::HouseboundRole->new({
460 borrowernumber_id => $borrowernumber,
461 housebound_chooser => $hsbnd_chooser,
462 housebound_deliverer => $hsbnd_deliverer,
466 } elsif ($op eq 'save') {
468 if ($NoUpdateLogin) {
469 delete $newdata{'password'};
470 delete $newdata{'userid'};
473 $patron = Koha::Patrons->find( $borrowernumber );
475 if ($NoUpdateEmail) {
476 delete $newdata{'email'};
477 delete $newdata{'emailpro'};
478 delete $newdata{'B_email'};
481 delete $newdata{password2};
484 $patron->set(\%newdata)->store if scalar(keys %newdata) > 1;
485 # bug 4508 - avoid crash if we're not updating any columns in the borrowers table (editing patron attrs or msg prefs)
490 if( ref($_) eq 'Koha::Exceptions::Patron::InvalidUserid' ) {
491 push @errors, "ERROR_login_exist";
493 warn "Patron modification failed! - $@"; # Maybe we must die instead of just warn
494 push @messages, {error => 'error_on_update_patron'};
501 # Update or create our HouseboundRole if necessary.
502 my $housebound_role = Koha::Patron::HouseboundRoles->find($borrowernumber);
503 my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
504 $hsbnd_chooser = 1 if $input->param('housebound_chooser');
505 $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
506 if ( $housebound_role ) {
507 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
508 # Update our HouseboundRole.
510 ->housebound_chooser($hsbnd_chooser)
511 ->housebound_deliverer($hsbnd_deliverer)
514 $housebound_role->delete; # No longer needed.
517 # Only create a HouseboundRole if patron has a role.
518 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
519 $housebound_role = Koha::Patron::HouseboundRole->new({
520 borrowernumber_id => $borrowernumber,
521 housebound_chooser => $hsbnd_chooser,
522 housebound_deliverer => $hsbnd_deliverer,
527 # should never raise an exception as password validity is checked above
528 my $password = $newdata{password};
529 if ( $password and $password ne '****' ) {
530 $patron->set_password({ password => $password });
533 add_guarantors( $patron, $input );
534 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
535 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template);
541 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
542 $patron->extended_attributes->filter_by_branch_limitations->delete;
543 $patron->extended_attributes($extended_patron_attributes);
546 if ( $destination eq 'circ' and not C4::Auth::haspermission( C4::Context->userenv->{id}, { circulate => 'circulate_remaining_permissions' } ) ) {
547 # If we want to redirect to circulation.pl and need to check if the logged in user has the necessary permission
548 $destination = 'not_circ';
550 print scalar( $destination eq "circ" )
552 "/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber")
554 "/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber"
556 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
561 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
562 exit; # same as above
565 if ($nok or !$nodouble){
566 $op="add" if ($op eq "insert");
567 $op="modify" if ($op eq "save");
569 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
571 $template->param( step_1 => 1,step_2 => 1,step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1, step_7 => 1 );
574 if (C4::Context->preference("IndependentBranches")) {
575 my $userenv = C4::Context->userenv;
576 if ( !C4::Context->IsSuperLibrarian() && $data{'branchcode'} ) {
577 unless ($userenv->{branch} eq $data{'branchcode'}){
578 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
584 # Define the fields to be pre-filled in guarantee records
585 my $prefillguarantorfields=C4::Context->preference("PrefillGuaranteeField");
586 my @prefill_fields=split(/\,/,$prefillguarantorfields);
590 foreach (@prefill_fields) {
591 $newdata{$_} = $guarantor->$_;
594 $template->param( updtype => 'I', step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1, step_7 => 1);
596 if ($op eq "modify") {
597 $template->param( updtype => 'M',modify => 1 );
598 $template->param( step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1, step_7 => 1) unless $step;
600 $template->param( categorycode => $borrower_data->{'categorycode'} );
603 if ( $op eq "duplicate" ) {
604 $template->param( updtype => 'I' );
605 $template->param( step_1 => 1, step_2 => 1, step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1, step_7 => 1 ) unless $step;
606 $data{'cardnumber'} = "";
609 if(!defined($data{'sex'})){
610 $template->param( none => 1);
611 } elsif($data{'sex'} eq 'F'){
612 $template->param( female => 1);
613 } elsif ($data{'sex'} eq 'M'){
614 $template->param( male => 1);
615 } elsif ($data{'sex'} eq 'O') {
616 $template->param( other => 1);
618 $template->param( none => 1);
621 ##Now all the data to modify a member.
623 my $patron_categories = Koha::Patron::Categories->search_with_library_limits(
625 category_type => [qw(C A S P I X)],
626 ( $guarantor_id ? ( can_be_guarantee => 1 ) : () )
628 { order_by => ['categorycode'] }
630 my $no_categories = ! $patron_categories->count;
632 my @patron_categories = $patron_categories->as_list;
633 # When adding a guarantor we don't have a category yet, and only want to choose from the eligible categories
634 unless ( !$category || $patron_categories->find( $category->id ) ){
635 $template->param( limited_category => 1 );
636 push @patron_categories, $category;
638 foreach my $patron_category ( @patron_categories ) {
639 push @{ $categories->{ $patron_category->category_type } }, $patron_category;
643 patron_categories => $categories,
644 no_categories => $no_categories,
647 my $cities = Koha::Cities->search( {}, { order_by => 'city_name' } );
652 my $default_borrowertitle = '';
653 unless ( $op eq 'duplicate' ) { $default_borrowertitle=$data{'title'} }
655 my @relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
657 while (@relationships) {
658 my $relship = shift @relationships || '';
659 my %row = ('relationship' => $relship);
660 if (defined($data{'relationship'}) and $data{'relationship'} eq $relship) {
661 $row{'selected'}=' selected';
665 push(@relshipdata, \%row);
669 # in modify mod: userbranch value comes from borrowers table
670 # in add mod: userbranch value comes from branches table (ip correspondence)
673 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
674 $userbranch = C4::Context->userenv->{'branch'};
677 if (defined ($data{'branchcode'}) and ( $op eq 'modify' || $op eq 'duplicate' || ( $op eq 'add' && $category->category_type eq 'C' ) )) {
678 $userbranch = $data{'branchcode'};
680 $template->param( userbranch => $userbranch );
683 if ( Koha::Libraries->search->count < 1 ){
685 $template->param(no_branches => 1);
689 $template->param(no_categories => 1);
691 $template->param(no_add => $no_add);
692 # --------------------------------------------------------------------------------------------------------
694 $template->param( sort1 => $data{'sort1'});
695 $template->param( sort2 => $data{'sort2'});
696 $template->param( autorenew => $data{'autorenew'});
699 foreach my $error (@errors) {
700 $template->param($error) || $template->param( $error => 1);
702 $template->param(nok => 1);
704 #Prevent losing guarantor data if error occurs
706 my @new_guarantor_id = $input->multi_param('new_guarantor_id');
707 my @new_guarantor_relationship = $input->multi_param('new_guarantor_relationship');
708 foreach my $gid ( @new_guarantor_id ) {
709 my $patron = Koha::Patrons->find( $gid );
710 my $relationship = shift( @new_guarantor_relationship );
712 my $g = { patron => $patron, relationship => $relationship };
713 push( @new_guarantors, $g );
715 $template->param( new_guarantors => \@new_guarantors );
718 #Formatting data for display
720 if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){
721 $data{'dateenrolled'} = dt_from_string;
723 if ( $op eq 'duplicate' ) {
724 $data{'dateenrolled'} = dt_from_string;
725 $data{dateexpiry} = $category->get_expiry_date( $data{dateenrolled} );
727 if (C4::Context->preference('uppercasesurnames')) {
728 $data{'surname'} &&= uc( $data{'surname'} );
729 $data{'contactname'} &&= uc( $data{'contactname'} );
732 if ( C4::Context->preference('ExtendedPatronAttributes') ) {
733 patron_attributes_form( $template, $extended_patron_attributes, $op );
736 if (C4::Context->preference('EnhancedMessagingPreferences')) {
738 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
740 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
742 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
743 $template->param(SMSnumber => $data{'smsalertnumber'} );
744 $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
747 $template->param( borrower_data => \%data );
748 $template->param( "show_guarantor" => $category ? $category->can_be_guarantee : 1); # associate with step to know where you are
749 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
750 $template->param( step => $step ) if $step; # associate with step to know where u are
753 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
754 destination => $destination,#to know where u come from and where u must go in redirect
755 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
759 patron => $patron ? $patron : \%newdata, # Used by address include templates now
760 nodouble => $nodouble,
761 borrowernumber => $borrowernumber, #register number
762 relshiploop => \@relshipdata,
763 btitle=> $default_borrowertitle,
765 nok => $nok,#flag to know if an error
766 NoUpdateLogin => $NoUpdateLogin,
767 NoUpdateEmail => $NoUpdateEmail,
768 CanUpdatePasswordExpiration => $CanUpdatePasswordExpiration,
771 # Generate CSRF token
772 $template->param( csrf_token =>
773 Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
776 # HouseboundModule data
778 housebound_role => Koha::Patron::HouseboundRoles->find($borrowernumber),
781 if(defined($data{'flags'})){
782 $template->param(flags=>$data{'flags'});
784 if(defined($data{'contacttitle'})){
785 $template->param("contacttitle_" . $data{'contacttitle'} => "SELECTED");
789 my ( $min, $max ) = C4::Members::get_cardnumber_length();
790 if ( defined $min ) {
792 minlength_cardnumber => $min,
793 maxlength_cardnumber => $max
797 if ( C4::Context->preference('TranslateNotices') ) {
798 my $translated_languages = C4::Languages::getTranslatedLanguages( 'opac', C4::Context->preference('template') );
799 $template->param( languages => $translated_languages );
802 $template->param( messages => \@messages );
803 output_html_with_http_headers $input, $cookie, $template->output;
805 sub parse_extended_patron_attributes {
807 my @patron_attr = grep { /^patron_attr_\d+$/ } $input->multi_param();
811 foreach my $key (@patron_attr) {
812 my $value = $input->param($key);
813 next unless defined($value) and $value ne '';
814 my $code = $input->param("${key}_code");
815 next if exists $dups{$code}->{$value};
816 $dups{$code}->{$value} = 1;
817 push @attr, { code => $code, attribute => $value };
822 sub patron_attributes_form {
823 my $template = shift;
824 my $attributes = shift;
827 my $library_id = C4::Context->userenv ? C4::Context->userenv->{'branch'} : undef;
828 my $attribute_types = Koha::Patron::Attribute::Types->search_with_library_limits({}, {}, $library_id);
829 if ( $attribute_types->count == 0 ) {
830 $template->param(no_patron_attribute_types => 1);
834 # map patron's attributes into a more convenient structure
836 foreach my $attr (@$attributes) {
837 push @{ $attr_hash{$attr->{code}} }, $attr;
840 my @attribute_loop = ();
843 while ( my ( $attr_type ) = $attribute_types->next ) {
845 class => $attr_type->class(),
846 code => $attr_type->code(),
847 description => $attr_type->description(),
848 repeatable => $attr_type->repeatable(),
849 category => $attr_type->authorised_value_category(),
850 category_code => $attr_type->category_code(),
851 mandatory => $attr_type->mandatory(),
853 if (exists $attr_hash{$attr_type->code()}) {
854 foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
855 my $newentry = { %$entry };
856 $newentry->{value} = $attr->{attribute};
857 $newentry->{use_dropdown} = 0;
858 if ($attr_type->authorised_value_category()) {
859 $newentry->{use_dropdown} = 1;
860 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{attribute});
863 undef $newentry->{value} if ($attr_type->unique_id() && $op eq 'duplicate');
864 $newentry->{form_id} = "patron_attr_$i";
865 push @{$items_by_class{$attr_type->class()}}, $newentry;
869 my $newentry = { %$entry };
870 if ($attr_type->authorised_value_category()) {
871 $newentry->{use_dropdown} = 1;
872 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
874 $newentry->{form_id} = "patron_attr_$i";
875 push @{$items_by_class{$attr_type->class()}}, $newentry;
878 for my $class ( sort keys %items_by_class ) {
879 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
880 my $lib = $av->count ? $av->next->lib : $class;
881 push @attribute_loop, {
883 items => $items_by_class{$class},
888 $template->param(patron_attributes => \@attribute_loop);
893 my ( $patron, $input ) = @_;
895 my @new_guarantor_id = $input->multi_param('new_guarantor_id');
896 my @new_guarantor_relationship = $input->multi_param('new_guarantor_relationship');
898 for ( my $i = 0 ; $i < scalar @new_guarantor_id; $i++ ) {
899 my $guarantor_id = $new_guarantor_id[$i];
900 my $relationship = $new_guarantor_relationship[$i];
902 next unless $guarantor_id;
904 $patron->add_guarantor(
906 guarantor_id => $guarantor_id,
907 relationship => $relationship,