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;
46 use Koha::Patron::Categories;
47 use Koha::Patron::HouseboundRole;
48 use Koha::Patron::HouseboundRoles;
52 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
53 load Koha::NorwegianPatronDB, qw( NLGetSyncDataFromBorrowernumber );
55 use Koha::SMS::Providers;
60 $debug = $ENV{DEBUG} || 0;
64 ($debug) or $debug = $input->param('debug') || 0;
67 my $dbh = C4::Context->dbh;
69 my ($template, $loggedinuser, $cookie)
70 = get_template_and_user({template_name => "members/memberentrygen.tt",
74 flagsrequired => {borrowers => 1},
75 debug => ($debug) ? 1 : 0,
78 if ( C4::Context->preference('SMSSendDriver') eq 'Email' ) {
79 my @providers = Koha::SMS::Providers->search();
80 $template->param( sms_providers => \@providers );
83 my $guarantorid = $input->param('guarantorid');
84 my $borrowernumber = $input->param('borrowernumber');
85 my $actionType = $input->param('actionType') || '';
86 my $modify = $input->param('modify');
87 my $delete = $input->param('delete');
88 my $op = $input->param('op');
89 my $destination = $input->param('destination');
90 my $cardnumber = $input->param('cardnumber');
91 my $check_member = $input->param('check_member');
92 my $nodouble = $input->param('nodouble');
93 my $duplicate = $input->param('duplicate');
94 my $quickadd = $input->param('quickadd');
95 $nodouble = 1 if ($op eq 'modify' or $op eq 'duplicate'); # FIXME hack to represent fact that if we're
96 # modifying an existing patron, it ipso facto
97 # isn't a duplicate. Marking FIXME because this
98 # script needs to be refactored.
99 my $nok = $input->param('nok');
100 my $guarantorinfo = $input->param('guarantorinfo');
101 my $step = $input->param('step') || 0;
105 my $userenv = C4::Context->userenv;
108 ## Deal with debarments
110 debarments => GetDebarments( { borrowernumber => $borrowernumber } ) );
111 my @debarments_to_remove = $input->multi_param('remove_debarment');
112 foreach my $d ( @debarments_to_remove ) {
115 if ( $input->param('add_debarment') ) {
117 my $expiration = $input->param('debarred_expiration');
121 { 'dt' => dt_from_string($expiration), 'dateformat' => 'iso' } )
126 borrowernumber => $borrowernumber,
128 comment => scalar $input->param('debarred_comment'),
129 expiration => $expiration,
134 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
136 # function to designate mandatory fields (visually with css)
137 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
138 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
139 foreach (@field_check) {
140 $template->param( "mandatory$_" => 1);
142 # function to designate unwanted fields
143 my $check_BorrowerUnwantedField=C4::Context->preference("BorrowerUnwantedField");
144 @field_check=split(/\|/,$check_BorrowerUnwantedField);
145 foreach (@field_check) {
147 $template->param( "no$_" => 1);
149 $template->param( "add" => 1 ) if ( $op eq 'add' );
150 $template->param( "quickadd" => 1 ) if ( $quickadd );
151 $template->param( "duplicate" => 1 ) if ( $op eq 'duplicate' );
152 $template->param( "checked" => 1 ) if ( defined($nodouble) && $nodouble eq 1 );
153 if ( $op eq 'modify' or $op eq 'save' or $op eq 'duplicate' ) {
154 my $patron = Koha::Patrons->find( $borrowernumber );
156 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber");
160 $borrower_data = $patron->unblessed;
161 $borrower_data->{category_type} = $patron->category->category_type;
163 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
164 my $category_type = $input->param('category_type') || '';
165 unless ($category_type or !($categorycode)){
166 my $borrowercategory = Koha::Patron::Categories->find($categorycode);
167 $category_type = $borrowercategory->category_type;
168 my $category_name = $borrowercategory->description;
169 $template->param("categoryname"=>$category_name);
171 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
173 # if a add or modify is requested => check validity of data.
174 %data = %$borrower_data if ($borrower_data);
176 # initialize %newdata
177 my %newdata; # comes from $input->param()
178 if ( $op eq 'insert' || $op eq 'modify' || $op eq 'save' || $op eq 'duplicate' ) {
179 my @names = ( $borrower_data && $op ne 'save' ) ? keys %$borrower_data : $input->param();
180 foreach my $key (@names) {
181 if (defined $input->param($key)) {
182 $newdata{$key} = $input->param($key);
183 $newdata{$key} =~ s/\"/"/g unless $key eq 'borrowernotes' or $key eq 'opacnote';
187 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
188 next unless exists $newdata{$_};
189 my $userdate = $newdata{$_} or next;
191 my $formatteddate = eval { output_pref({ dt => dt_from_string( $userdate ), dateformat => 'iso', dateonly => 1 } ); };
192 if ( $formatteddate ) {
193 $newdata{$_} = $formatteddate;
195 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
196 $template->param( "ERROR_$_" => 1 );
197 push(@errors,"ERROR_$_");
200 # check permission to modify login info.
201 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) ) {
206 # remove keys from %newdata that ModMember() doesn't like
208 my @keys_to_delete = (
209 qr/^BorrowerMandatoryField$/,
218 qr/^setting_extended_patron_attributes$/,
219 qr/^setting_messaging_prefs$/,
227 for my $regexp (@keys_to_delete) {
228 for (keys %newdata) {
229 delete($newdata{$_}) if /$regexp/;
234 # Test uniqueness of surname, firstname and dateofbirth
235 if ( ( $op eq 'insert' ) and !$nodouble ) {
237 $conditions->{surname} = $newdata{surname} if $newdata{surname};
238 if ( $category_type ne 'I' ) {
239 $conditions->{firstname} = $newdata{firstname} if $newdata{firstname};
240 $conditions->{dateofbirth} = $newdata{dateofbirth} if $newdata{dateofbirth};
243 my $patrons = Koha::Patrons->search($conditions);
244 if ( $patrons->count > 0) {
246 $check_member = $patrons->next->borrowernumber;
250 #recover all data from guarantor address phone ,fax...
251 if ( $guarantorid ) {
252 if (my $guarantor = Koha::Patrons->find( $guarantorid )) {
253 my $guarantordata = $guarantor->unblessed;
254 $category_type = $guarantordata->{categorycode} eq 'I' ? 'P' : 'C';
255 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
256 $newdata{'contactfirstname'}= $guarantordata->{'firstname'};
257 $newdata{'contactname'} = $guarantordata->{'surname'};
258 $newdata{'contacttitle'} = $guarantordata->{'title'};
259 if ( $op eq 'add' ) {
260 foreach (qw(streetnumber address streettype address2
261 zipcode country city state phone phonepro mobile fax email emailpro branchcode
262 B_streetnumber B_streettype B_address B_address2
263 B_city B_state B_zipcode B_country B_email B_phone)) {
264 $newdata{$_} = $guarantordata->{$_};
270 ###############test to take the right zipcode, country and city name ##############
271 # set only if parameter was passed from the form
272 $newdata{'city'} = $input->param('city') if defined($input->param('city'));
273 $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
274 $newdata{'country'} = $input->param('country') if defined($input->param('country'));
276 $newdata{'lang'} = $input->param('lang') if defined($input->param('lang'));
278 # builds default userid
279 # userid input text may be empty or missing because of syspref BorrowerUnwantedField
280 if ( ( defined $newdata{'userid'} && $newdata{'userid'} eq '' ) || $check_BorrowerUnwantedField =~ /userid/ ) {
281 if ( ( defined $newdata{'firstname'} ) && ( defined $newdata{'surname'} ) ) {
282 # Full page edit, firstname and surname input zones are present
283 $newdata{'userid'} = Generate_Userid( $borrowernumber, $newdata{'firstname'}, $newdata{'surname'} );
285 elsif ( ( defined $data{'firstname'} ) && ( defined $data{'surname'} ) ) {
286 # Partial page edit (access through "Details"/"Library details" tab), firstname and surname input zones are not used
287 # Still, if the userid field is erased, we can create a new userid with available firstname and surname
288 $newdata{'userid'} = Generate_Userid( $borrowernumber, $data{'firstname'}, $data{'surname'} );
291 $newdata{'userid'} = $data{'userid'};
295 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
296 my $extended_patron_attributes = ();
297 if ($op eq 'save' || $op eq 'insert'){
299 die "Wrong CSRF token"
300 unless Koha::Token->new->check_csrf({
301 session_id => scalar $input->cookie('CGISESSID'),
302 token => scalar $input->param('csrf_token'),
305 # If the cardnumber is blank, treat it as null.
306 $newdata{'cardnumber'} = undef if $newdata{'cardnumber'} =~ /^\s*$/;
308 if (my $error_code = checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
309 push @errors, $error_code == 1
310 ? 'ERROR_cardnumber_already_exists'
312 ? 'ERROR_cardnumber_length'
317 if ($op eq 'save' && $step == 3) {
318 my $patron = Koha::Patrons->find( $borrowernumber );
319 $dateofbirth = $patron->dateofbirth;
322 $dateofbirth = $newdata{dateofbirth};
325 if ( $dateofbirth ) {
326 my $patron = Koha::Patron->new({ dateofbirth => $dateofbirth });
327 my $age = $patron->get_age;
328 my $borrowercategory = Koha::Patron::Categories->find($categorycode);
329 my ($low,$high) = ($borrowercategory->dateofbirthrequired, $borrowercategory->upperagelimit);
330 if (($high && ($age > $high)) or ($age < $low)) {
331 push @errors, 'ERROR_age_limitations';
332 $template->param( age_low => $low);
333 $template->param( age_high => $high);
337 if($newdata{surname} && C4::Context->preference('uppercasesurnames')) {
338 $newdata{'surname'} = uc($newdata{'surname'});
341 if (C4::Context->preference("IndependentBranches")) {
342 unless ( C4::Context->IsSuperLibrarian() ){
343 $debug and print STDERR " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
344 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
345 push @errors, "ERROR_branch";
349 # Check if the 'userid' is unique. 'userid' might not always be present in
350 # the edited values list when editing certain sub-forms. Get it straight
351 # from the DB if absent.
352 my $userid = $newdata{ userid } // $borrower_data->{ userid };
353 unless (Check_Userid($userid,$borrowernumber)) {
354 push @errors, "ERROR_login_exist";
357 my $password = $input->param('password');
358 my $password2 = $input->param('password2');
359 push @errors, "ERROR_password_mismatch" if ( $password ne $password2 );
360 my $minpw = C4::Context->preference('minPasswordLength');
361 $minpw = 3 if not $minpw or $minpw < 3;
362 push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
365 my $emailprimary = $input->param('email');
366 my $emailsecondary = $input->param('emailpro');
367 my $emailalt = $input->param('B_email');
370 push (@errors, "ERROR_bad_email") if (!Email::Valid->address($emailprimary));
372 if ($emailsecondary) {
373 push (@errors, "ERROR_bad_email_secondary") if (!Email::Valid->address($emailsecondary));
376 push (@errors, "ERROR_bad_email_alternative") if (!Email::Valid->address($emailalt));
379 if (C4::Context->preference('ExtendedPatronAttributes')) {
380 $extended_patron_attributes = parse_extended_patron_attributes($input);
381 foreach my $attr (@$extended_patron_attributes) {
382 unless (C4::Members::Attributes::CheckUniqueness($attr->{code}, $attr->{value}, $borrowernumber)) {
383 my $attr_info = C4::Members::AttributeTypes->fetch($attr->{code});
384 push @errors, "ERROR_extended_unique_id_failed";
386 ERROR_extended_unique_id_failed_code => $attr->{code},
387 ERROR_extended_unique_id_failed_value => $attr->{value},
388 ERROR_extended_unique_id_failed_description => $attr_info->description()
395 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){
396 unless ($newdata{'dateexpiry'}){
397 my $patron_category = Koha::Patron::Categories->find( $newdata{categorycode} );
398 $newdata{'dateexpiry'} = $patron_category->get_expiry_date( $newdata{dateenrolled} ) if $patron_category;
402 # BZ 14683: Do not mixup mobile [read: other phone] with smsalertnumber
403 my $sms = $input->param('SMSnumber');
404 if ( defined $sms ) {
405 $newdata{smsalertnumber} = $sms;
408 ### Error checks should happen before this line.
409 $nok = $nok || scalar(@errors);
410 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
411 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
412 if ($op eq 'insert'){
413 # we know it's not a duplicate borrowernumber or there would already be an error
414 $borrowernumber = &AddMember(%newdata);
415 $newdata{'borrowernumber'} = $borrowernumber;
417 # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
418 if ( C4::Context->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'} && $newdata{'password'}) {
419 #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
421 if (C4::Context->preference("AutoEmailPrimaryAddress") ne 'OFF' &&
422 $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} =~ /\w\@\w/ ) {
423 $emailaddr = $newdata{C4::Context->preference("AutoEmailPrimaryAddress")}
425 elsif ($newdata{email} =~ /\w\@\w/) {
426 $emailaddr = $newdata{email}
428 elsif ($newdata{emailpro} =~ /\w\@\w/) {
429 $emailaddr = $newdata{emailpro}
431 elsif ($newdata{B_email} =~ /\w\@\w/) {
432 $emailaddr = $newdata{B_email}
434 # if we manage to find a valid email address, send notice
436 $newdata{emailaddr} = $emailaddr;
439 $err = SendAlerts ( 'members', \%newdata, "ACCTDETAILS" );
442 $template->param(error_alert => $@);
443 } elsif ( ref($err) eq "HASH" && defined $err->{error} and $err->{error} eq "no_email" ) {
444 $template->{VARS}->{'error_alert'} = "no_email";
446 $template->{VARS}->{'info_alert'} = 1;
451 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
452 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
454 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
455 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template, 1, $newdata{'categorycode'});
457 # Try to do the live sync with the Norwegian national patron database, if it is enabled
458 if ( exists $data{'borrowernumber'} && C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
459 NLSync({ 'borrowernumber' => $borrowernumber });
462 # Create HouseboundRole if necessary.
463 # Borrower did not exist, so HouseboundRole *cannot* yet exist.
464 my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
465 $hsbnd_chooser = 1 if $input->param('housebound_chooser');
466 $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
467 # Only create a HouseboundRole if patron has a role.
468 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
469 Koha::Patron::HouseboundRole->new({
470 borrowernumber_id => $borrowernumber,
471 housebound_chooser => $hsbnd_chooser,
472 housebound_deliverer => $hsbnd_deliverer,
476 } elsif ($op eq 'save') {
478 # Update or create our HouseboundRole if necessary.
479 my $housebound_role = Koha::Patron::HouseboundRoles->find($borrowernumber);
480 my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
481 $hsbnd_chooser = 1 if $input->param('housebound_chooser');
482 $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
483 if ( $housebound_role ) {
484 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
485 # Update our HouseboundRole.
487 ->housebound_chooser($hsbnd_chooser)
488 ->housebound_deliverer($hsbnd_deliverer)
491 $housebound_role->delete; # No longer needed.
494 # Only create a HouseboundRole if patron has a role.
495 if ( $hsbnd_chooser || $hsbnd_deliverer ) {
496 $housebound_role = Koha::Patron::HouseboundRole->new({
497 borrowernumber_id => $borrowernumber,
498 housebound_chooser => $hsbnd_chooser,
499 housebound_deliverer => $hsbnd_deliverer,
504 if ($NoUpdateLogin) {
505 delete $newdata{'password'};
506 delete $newdata{'userid'};
508 &ModMember(%newdata) unless scalar(keys %newdata) <= 1; # bug 4508 - avoid crash if we're not
509 # updating any columns in the borrowers table,
510 # which can happen if we're only editing the
511 # patron attributes or messaging preferences sections
512 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
513 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
515 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
516 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template);
519 print scalar ($destination eq "circ") ?
520 $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
521 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
522 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
526 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
527 exit; # same as above
530 if ($nok or !$nodouble){
531 $op="add" if ($op eq "insert");
532 $op="modify" if ($op eq "save");
534 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
536 $template->param( step_1 => 1,step_2 => 1,step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1);
539 if (C4::Context->preference("IndependentBranches")) {
540 my $userenv = C4::Context->userenv;
541 if ( !C4::Context->IsSuperLibrarian() && $data{'branchcode'} ) {
542 unless ($userenv->{branch} eq $data{'branchcode'}){
543 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
549 $template->param( updtype => 'I', step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1);
551 if ($op eq "modify") {
552 $template->param( updtype => 'M',modify => 1 );
553 $template->param( step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1) unless $step;
555 $template->param( categorycode => $borrower_data->{'categorycode'} );
557 # Add sync data to the user data
558 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
559 my $sync = NLGetSyncDataFromBorrowernumber( $borrowernumber );
567 if ( $op eq "duplicate" ) {
568 $template->param( updtype => 'I' );
569 $template->param( step_1 => 1, step_2 => 1, step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1 ) unless $step;
570 $data{'cardnumber'} = "";
573 if(!defined($data{'sex'})){
574 $template->param( none => 1);
575 } elsif($data{'sex'} eq 'F'){
576 $template->param( female => 1);
577 } elsif ($data{'sex'} eq 'M'){
578 $template->param( male => 1);
580 $template->param( none => 1);
583 ##Now all the data to modify a member.
586 my $no_categories = 1;
588 foreach my $category_type (qw(C A S P I X)) {
589 my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => $category_type }, {order_by => ['categorycode']});
590 $no_categories = 0 if $patron_categories->count > 0;
593 while ( my $patron_category = $patron_categories->next ) {
595 { 'categorycode' => $patron_category->categorycode,
596 'categoryname' => $patron_category->description,
597 'categorycodeselected' =>
598 ( ( defined( $borrower_data->{'categorycode'} ) && $patron_category->categorycode eq $borrower_data->{'categorycode'} ) || ( defined($categorycode) && $patron_category->categorycode eq $categorycode ) ),
602 $typehash{'typename'} = $category_type;
603 my $typedescription = "typename_" . $typehash{'typename'};
604 $typehash{'categoryloop'} = \@categoryloop;
606 { 'typename' => $category_type,
607 $typedescription => 1,
608 'categoryloop' => \@categoryloop
612 $template->param('typeloop' => \@typeloop,
613 no_categories => $no_categories);
614 if($no_categories){ $no_add = 1; }
617 my $cities = Koha::Cities->search( {}, { order_by => 'city_name' } );
618 my $roadtypes = C4::Koha::GetAuthorisedValues( 'ROADTYPE' );
620 roadtypes => $roadtypes,
624 my $default_borrowertitle = '';
625 unless ( $op eq 'duplicate' ) { $default_borrowertitle=$data{'title'} }
627 my @relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
629 while (@relationships) {
630 my $relship = shift @relationships || '';
631 my %row = ('relationship' => $relship);
632 if (defined($data{'relationship'}) and $data{'relationship'} eq $relship) {
633 $row{'selected'}=' selected';
637 push(@relshipdata, \%row);
640 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
645 foreach (keys(%flags)) {
647 my %row = ('key' => $key,
648 'name' => $flags{$key}[0]);
650 $row{'yes'}=' checked';
655 $row{'no'}=' checked';
657 push @flagdata,\%row;
661 # in modify mod: userbranch value comes from borrowers table
662 # in add mod: userbranch value comes from branches table (ip correspondence)
665 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
666 $userbranch = C4::Context->userenv->{'branch'};
669 if (defined ($data{'branchcode'}) and ( $op eq 'modify' || $op eq 'duplicate' || ( $op eq 'add' && $category_type eq 'C' ) )) {
670 $userbranch = $data{'branchcode'};
672 $template->param( userbranch => $userbranch );
674 if ( Koha::Libraries->search->count < 1 ){
676 $template->param(no_branches => 1);
680 $template->param(no_categories => 1);
682 $template->param(no_add => $no_add);
683 # --------------------------------------------------------------------------------------------------------
685 $template->param( sort1 => $data{'sort1'});
686 $template->param( sort2 => $data{'sort2'});
689 foreach my $error (@errors) {
690 $template->param($error) || $template->param( $error => 1);
692 $template->param(nok => 1);
695 #Formatting data for display
697 if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){
698 $data{'dateenrolled'} = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
700 if ( $op eq 'duplicate' ) {
701 $data{'dateenrolled'} = output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 });
702 my $patron_category = Koha::Patron::Categories->find( $data{categorycode} );
703 $data{dateexpiry} = $patron_category->get_expiry_date( $data{dateenrolled} );
705 if (C4::Context->preference('uppercasesurnames')) {
706 $data{'surname'} &&= uc( $data{'surname'} );
707 $data{'contactname'} &&= uc( $data{'contactname'} );
710 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
712 $data{$_} = eval { output_pref({ dt => dt_from_string( $data{$_} ), dateonly => 1 } ); }; # back to syspref for display
714 $template->param( $_ => $data{$_});
717 if (C4::Context->preference('ExtendedPatronAttributes')) {
718 $template->param(ExtendedPatronAttributes => 1);
719 patron_attributes_form($template, $borrowernumber);
722 if (C4::Context->preference('EnhancedMessagingPreferences')) {
724 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
726 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
728 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
729 $template->param(SMSnumber => $data{'smsalertnumber'} );
730 $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
733 $template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ? 0 : 1); # associate with step to know where you are
734 $debug and warn "memberentry step: $step";
735 $template->param(%data);
736 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
737 $template->param( step => $step ) if $step; # associate with step to know where u are
740 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
741 category_type => $category_type,#to know the category type of the borrower
742 "$category_type" => 1,# associate with step to know where u are
743 destination => $destination,#to know wher u come from and wher u must go in redirect
744 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
748 nodouble => $nodouble,
749 borrowernumber => $borrowernumber, #register number
750 guarantorid => ($borrower_data->{'guarantorid'} || $guarantorid),
751 relshiploop => \@relshipdata,
752 btitle=> $default_borrowertitle,
753 guarantorinfo => $guarantorinfo,
754 flagloop => \@flagdata,
755 category_type =>$category_type,
757 nok => $nok,#flag to know if an error
758 NoUpdateLogin => $NoUpdateLogin,
761 # Generate CSRF token
762 $template->param( csrf_token =>
763 Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
766 # HouseboundModule data
768 housebound_role => scalar Koha::Patron::HouseboundRoles->find($borrowernumber),
771 if(defined($data{'flags'})){
772 $template->param(flags=>$data{'flags'});
774 if(defined($data{'contacttitle'})){
775 $template->param("contacttitle_" . $data{'contacttitle'} => "SELECTED");
779 my ( $min, $max ) = C4::Members::get_cardnumber_length();
780 if ( defined $min ) {
782 minlength_cardnumber => $min,
783 maxlength_cardnumber => $max
787 if ( C4::Context->preference('TranslateNotices') ) {
788 my $translated_languages = C4::Languages::getTranslatedLanguages( 'opac', C4::Context->preference('template') );
789 $template->param( languages => $translated_languages );
792 output_html_with_http_headers $input, $cookie, $template->output;
794 sub parse_extended_patron_attributes {
796 my @patron_attr = grep { /^patron_attr_\d+$/ } $input->multi_param();
800 foreach my $key (@patron_attr) {
801 my $value = $input->param($key);
802 next unless defined($value) and $value ne '';
803 my $code = $input->param("${key}_code");
804 next if exists $dups{$code}->{$value};
805 $dups{$code}->{$value} = 1;
806 push @attr, { code => $code, value => $value };
811 sub patron_attributes_form {
812 my $template = shift;
813 my $borrowernumber = shift;
815 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
816 if (scalar(@types) == 0) {
817 $template->param(no_patron_attribute_types => 1);
820 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
821 my @classes = uniq( map {$_->{class}} @$attributes );
822 @classes = sort @classes;
824 # map patron's attributes into a more convenient structure
826 foreach my $attr (@$attributes) {
827 push @{ $attr_hash{$attr->{code}} }, $attr;
830 my @attribute_loop = ();
833 foreach my $type_code (map { $_->{code} } @types) {
834 my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
836 class => $attr_type->class(),
837 code => $attr_type->code(),
838 description => $attr_type->description(),
839 repeatable => $attr_type->repeatable(),
840 category => $attr_type->authorised_value_category(),
841 category_code => $attr_type->category_code(),
843 if (exists $attr_hash{$attr_type->code()}) {
844 foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
845 my $newentry = { %$entry };
846 $newentry->{value} = $attr->{value};
847 $newentry->{use_dropdown} = 0;
848 if ($attr_type->authorised_value_category()) {
849 $newentry->{use_dropdown} = 1;
850 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{value});
853 $newentry->{form_id} = "patron_attr_$i";
854 push @{$items_by_class{$attr_type->class()}}, $newentry;
858 my $newentry = { %$entry };
859 if ($attr_type->authorised_value_category()) {
860 $newentry->{use_dropdown} = 1;
861 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
863 $newentry->{form_id} = "patron_attr_$i";
864 push @{$items_by_class{$attr_type->class()}}, $newentry;
867 while ( my ($class, @items) = each %items_by_class ) {
868 my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
869 my $lib = $av->count ? $av->next->lib : $class;
870 push @attribute_loop, {
877 $template->param(patron_attributes => \@attribute_loop);