Bug 11188 - Make gather_print_notices.pl die on failed open()
[koha.git] / members / memberentry.pl
blob591aa7c56f806e979beec3af177c6b9e2dc589ca
1 #!/usr/bin/perl
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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 # pragma
22 use strict;
23 use warnings;
25 # external modules
26 use CGI;
27 # use Digest::MD5 qw(md5_base64);
28 use List::MoreUtils qw/uniq/;
30 # internal modules
31 use C4::Auth;
32 use C4::Context;
33 use C4::Output;
34 use C4::Members;
35 use C4::Members::Attributes;
36 use C4::Members::AttributeTypes;
37 use C4::Koha;
38 use C4::Dates qw/format_date format_date_in_iso/;
39 use C4::Input;
40 use C4::Log;
41 use C4::Letters;
42 use C4::Branch; # GetBranches
43 use C4::Form::MessagingPreferences;
44 use Koha::Borrower::Debarments;
45 use Koha::DateUtils;
47 use vars qw($debug);
49 BEGIN {
50 $debug = $ENV{DEBUG} || 0;
53 my $input = new CGI;
54 ($debug) or $debug = $input->param('debug') || 0;
55 my %data;
57 my $dbh = C4::Context->dbh;
59 my ($template, $loggedinuser, $cookie)
60 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
61 query => $input,
62 type => "intranet",
63 authnotrequired => 0,
64 flagsrequired => {borrowers => 1},
65 debug => ($debug) ? 1 : 0,
66 });
68 my $guarantorid = $input->param('guarantorid');
69 my $borrowernumber = $input->param('borrowernumber');
70 my $actionType = $input->param('actionType') || '';
71 my $modify = $input->param('modify');
72 my $delete = $input->param('delete');
73 my $op = $input->param('op');
74 my $destination = $input->param('destination');
75 my $cardnumber = $input->param('cardnumber');
76 my $check_member = $input->param('check_member');
77 my $nodouble = $input->param('nodouble');
78 my $duplicate = $input->param('duplicate');
79 $nodouble = 1 if ($op eq 'modify' or $op eq 'duplicate'); # FIXME hack to represent fact that if we're
80 # modifying an existing patron, it ipso facto
81 # isn't a duplicate. Marking FIXME because this
82 # script needs to be refactored.
83 my $select_city = $input->param('select_city');
84 my $nok = $input->param('nok');
85 my $guarantorinfo = $input->param('guarantorinfo');
86 my $step = $input->param('step') || 0;
87 my @errors;
88 my $default_city;
89 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
90 my $check_categorytype=$input->param('check_categorytype');
91 # NOTE: Alert for ethnicity and ethnotes fields, they are invalid in all borrowers form
92 my $borrower_data;
93 my $NoUpdateLogin;
94 my $userenv = C4::Context->userenv;
97 ## Deal with debarments
98 $template->param(
99 debarments => GetDebarments( { borrowernumber => $borrowernumber } ) );
100 my @debarments_to_remove = $input->param('remove_debarment');
101 foreach my $d ( @debarments_to_remove ) {
102 DelDebarment( $d );
104 if ( $input->param('add_debarment') ) {
106 my $expiration = $input->param('debarred_expiration');
107 $expiration =
108 $expiration
109 ? output_pref(
110 { 'dt' => dt_from_string($expiration), 'dateformat' => 'iso' } )
111 : undef;
113 AddDebarment(
115 borrowernumber => $borrowernumber,
116 type => 'MANUAL',
117 comment => $input->param('debarred_comment'),
118 expiration => $expiration,
123 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
125 my $minpw = C4::Context->preference('minPasswordLength');
126 $template->param("minPasswordLength" => $minpw);
128 # function to designate mandatory fields (visually with css)
129 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
130 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
131 foreach (@field_check) {
132 $template->param( "mandatory$_" => 1);
134 # we'll need this, later.
135 my $dateofbirthmandatory = (scalar grep {$_ eq "dateofbirth"} @field_check) ? 1 : 0;
136 # function to designate unwanted fields
137 my $check_BorrowerUnwantedField=C4::Context->preference("BorrowerUnwantedField");
138 @field_check=split(/\|/,$check_BorrowerUnwantedField);
139 foreach (@field_check) {
140 next unless m/\w/o;
141 $template->param( "no$_" => 1);
143 $template->param( "add" => 1 ) if ( $op eq 'add' );
144 $template->param( "duplicate" => 1 ) if ( $op eq 'duplicate' );
145 $template->param( "checked" => 1 ) if ( defined($nodouble) && $nodouble eq 1 );
146 ( $borrower_data = GetMember( 'borrowernumber' => $borrowernumber ) ) if ( $op eq 'modify' or $op eq 'save' or $op eq 'duplicate' );
147 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
148 my $category_type = $input->param('category_type') || '';
149 if ($category_type){
150 $template->{VARS}->{'type_only'} = 1;
152 my $new_c_type = $category_type; #if we have input param, then we've already chosen the cat_type.
153 unless ($category_type or !($categorycode)){
154 my $borrowercategory = GetBorrowercategory($categorycode);
155 $category_type = $borrowercategory->{'category_type'};
156 my $category_name = $borrowercategory->{'description'};
157 $template->param("categoryname"=>$category_name);
159 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
161 # if a add or modify is requested => check validity of data.
162 %data = %$borrower_data if ($borrower_data);
164 # initialize %newdata
165 my %newdata; # comes from $input->param()
166 if ( $op eq 'insert' || $op eq 'modify' || $op eq 'save' || $op eq 'duplicate' ) {
167 my @names = ( $borrower_data && $op ne 'save' ) ? keys %$borrower_data : $input->param();
168 foreach my $key (@names) {
169 if (defined $input->param($key)) {
170 $newdata{$key} = $input->param($key);
171 $newdata{$key} =~ s/\"/"/g unless $key eq 'borrowernotes' or $key eq 'opacnote';
175 my $dateobject = C4::Dates->new();
176 my $syspref = $dateobject->regexp(); # same syspref format for all 3 dates
177 my $iso = $dateobject->regexp('iso'); #
178 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
179 next unless exists $newdata{$_};
180 my $userdate = $newdata{$_} or next;
181 if ($userdate =~ /$syspref/) {
182 $newdata{$_} = format_date_in_iso($userdate); # if they match syspref format, then convert to ISO
183 } elsif ($userdate =~ /$iso/) {
184 warn "Date $_ ($userdate) is already in ISO format";
185 } else {
186 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
187 $template->param( "ERROR_$_" => 1 ); # else ERROR!
188 push(@errors,"ERROR_$_");
191 # check permission to modify login info.
192 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) ) {
193 $NoUpdateLogin = 1;
197 # remove keys from %newdata that ModMember() doesn't like
199 my @keys_to_delete = (
200 qr/^BorrowerMandatoryField$/,
201 qr/^category_type$/,
202 qr/^check_member$/,
203 qr/^destination$/,
204 qr/^nodouble$/,
205 qr/^op$/,
206 qr/^save$/,
207 qr/^select_roadtype$/,
208 qr/^updtype$/,
209 qr/^SMSnumber$/,
210 qr/^setting_extended_patron_attributes$/,
211 qr/^setting_messaging_prefs$/,
212 qr/^digest$/,
213 qr/^modify$/,
214 qr/^step$/,
215 qr/^\d+$/,
216 qr/^\d+-DAYS/,
217 qr/^patron_attr_/,
219 for my $regexp (@keys_to_delete) {
220 for (keys %newdata) {
221 delete($newdata{$_}) if /$regexp/;
226 #############test for member being unique #############
227 if ( ( $op eq 'insert' ) and !$nodouble ) {
228 my $category_type_send;
229 if ( $category_type eq 'I' ) {
230 $category_type_send = $category_type;
232 my $check_category; # recover the category code of the doublon suspect borrowers
233 # ($result,$categorycode) = checkuniquemember($collectivity,$surname,$firstname,$dateofbirth)
234 ( $check_member, $check_category ) = checkuniquemember(
235 $category_type_send,
236 ( $newdata{surname} ? $newdata{surname} : $data{surname} ),
237 ( $newdata{firstname} ? $newdata{firstname} : $data{firstname} ),
238 ( $newdata{dateofbirth} ? $newdata{dateofbirth} : $data{dateofbirth} )
240 if ( !$check_member ) {
241 $nodouble = 1;
244 # recover the category type if the borrowers is a doublon
245 if ($check_category) {
246 my $tmpborrowercategory = GetBorrowercategory($check_category);
247 $check_categorytype = $tmpborrowercategory->{'category_type'};
251 #recover all data from guarantor address phone ,fax...
252 if ( $guarantorid and ( $category_type eq 'C' || $category_type eq 'P' )) {
253 if (my $guarantordata=GetMember(borrowernumber => $guarantorid)) {
254 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
255 $newdata{'contactfirstname'}= $guarantordata->{'firstname'};
256 $newdata{'contactname'} = $guarantordata->{'surname'};
257 $newdata{'contacttitle'} = $guarantordata->{'title'};
258 if ( $op eq 'add' ) {
259 foreach (qw(streetnumber address streettype address2
260 zipcode country city state phone phonepro mobile fax email emailpro branchcode
261 B_streetnumber B_streettype B_address B_address2
262 B_city B_state B_zipcode B_country B_email B_phone)) {
263 $newdata{$_} = $guarantordata->{$_};
269 ###############test to take the right zipcode, country and city name ##############
270 # set only if parameter was passed from the form
271 $newdata{'city'} = $input->param('city') if defined($input->param('city'));
272 $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
273 $newdata{'country'} = $input->param('country') if defined($input->param('country'));
275 #builds default userid
276 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
277 if ( ( defined $newdata{'firstname'} ) && ( defined $newdata{'surname'} ) ) {
278 # Full page edit, firstname and surname input zones are present
279 $newdata{'userid'} = Generate_Userid( $borrowernumber, $newdata{'firstname'}, $newdata{'surname'} );
281 elsif ( ( defined $data{'firstname'} ) && ( defined $data{'surname'} ) ) {
282 # Partial page edit (access through "Details"/"Library details" tab), firstname and surname input zones are not used
283 # Still, if the userid field is erased, we can create a new userid with available firstname and surname
284 $newdata{'userid'} = Generate_Userid( $borrowernumber, $data{'firstname'}, $data{'surname'} );
286 else {
287 $newdata{'userid'} = $data{'userid'};
291 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
292 my $extended_patron_attributes = ();
293 if ($op eq 'save' || $op eq 'insert'){
294 # If the cardnumber is blank, treat it as null.
295 $newdata{'cardnumber'} = undef if $newdata{'cardnumber'} =~ /^\s*$/;
297 if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
298 push @errors, 'ERROR_cardnumber';
300 if ($newdata{dateofbirth} && $dateofbirthmandatory) {
301 my $age = GetAge($newdata{dateofbirth});
302 my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});
303 my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'});
304 if (($high && ($age > $high)) or ($age < $low)) {
305 push @errors, 'ERROR_age_limitations';
306 $template->param( age_low => $low);
307 $template->param( age_high => $high);
311 if($newdata{surname} && C4::Context->preference('uppercasesurnames')) {
312 $newdata{'surname'} = uc($newdata{'surname'});
315 if (C4::Context->preference("IndependentBranches")) {
316 if ($userenv && $userenv->{flags} % 2 != 1){
317 $debug and print STDERR " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
318 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
319 push @errors, "ERROR_branch";
323 # Check if the userid is unique
324 unless (Check_Userid($newdata{'userid'},$borrowernumber)) {
325 push @errors, "ERROR_login_exist";
328 my $password = $input->param('password');
329 my $password2 = $input->param('password2');
330 push @errors, "ERROR_password_mismatch" if ( $password ne $password2 );
331 push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
333 if (C4::Context->preference('ExtendedPatronAttributes')) {
334 $extended_patron_attributes = parse_extended_patron_attributes($input);
335 foreach my $attr (@$extended_patron_attributes) {
336 unless (C4::Members::Attributes::CheckUniqueness($attr->{code}, $attr->{value}, $borrowernumber)) {
337 push @errors, "ERROR_extended_unique_id_failed";
338 $template->param(ERROR_extended_unique_id_failed_value => "$attr->{code}/$attr->{value}");
344 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){
345 unless ($newdata{'dateexpiry'}){
346 my $arg2 = $newdata{'dateenrolled'} || C4::Dates->today('iso');
347 $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
351 if (
352 defined $input->param('SMSnumber')
353 && (
354 $input->param('SMSnumber') eq ""
355 or $input->param('SMSnumber') ne $newdata{'mobile'}
358 $newdata{smsalertnumber} = $input->param('SMSnumber');
361 ### Error checks should happen before this line.
362 $nok = $nok || scalar(@errors);
363 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
364 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
365 if ($op eq 'insert'){
366 # we know it's not a duplicate borrowernumber or there would already be an error
367 $borrowernumber = &AddMember(%newdata);
368 $newdata{'borrowernumber'} = $borrowernumber;
370 # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
371 if ( C4::Context->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'} && $newdata{'password'}) {
372 #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
373 my $emailaddr;
374 if (C4::Context->preference("AutoEmailPrimaryAddress") ne 'OFF' &&
375 $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} =~ /\w\@\w/ ) {
376 $emailaddr = $newdata{C4::Context->preference("AutoEmailPrimaryAddress")}
378 elsif ($newdata{email} =~ /\w\@\w/) {
379 $emailaddr = $newdata{email}
381 elsif ($newdata{emailpro} =~ /\w\@\w/) {
382 $emailaddr = $newdata{emailpro}
384 elsif ($newdata{B_email} =~ /\w\@\w/) {
385 $emailaddr = $newdata{B_email}
387 # if we manage to find a valid email address, send notice
388 if ($emailaddr) {
389 $newdata{emailaddr} = $emailaddr;
390 my $err;
391 eval {
392 $err = SendAlerts ( 'members', \%newdata, "ACCTDETAILS" );
394 if ( $@ ) {
395 $template->param(error_alert => $@);
396 } elsif ( ref($err) eq "HASH" && defined $err->{error} and $err->{error} eq "no_email" ) {
397 $template->{VARS}->{'error_alert'} = "no_email";
398 } else {
399 $template->{VARS}->{'info_alert'} = 1;
404 if ($data{'organisations'}){
405 # need to add the members organisations
406 my @orgs=split(/\|/,$data{'organisations'});
407 add_member_orgs($borrowernumber,\@orgs);
409 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
410 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
412 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
413 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template, 1, $newdata{'categorycode'});
415 } elsif ($op eq 'save'){
416 if ($NoUpdateLogin) {
417 delete $newdata{'password'};
418 delete $newdata{'userid'};
420 &ModMember(%newdata) unless scalar(keys %newdata) <= 1; # bug 4508 - avoid crash if we're not
421 # updating any columns in the borrowers table,
422 # which can happen if we're only editing the
423 # patron attributes or messaging preferences sections
424 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
425 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
427 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
428 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template);
431 print scalar ($destination eq "circ") ?
432 $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
433 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
434 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
437 if ($delete){
438 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
439 exit; # same as above
442 if ($nok or !$nodouble){
443 $op="add" if ($op eq "insert");
444 $op="modify" if ($op eq "save");
445 %data=%newdata;
446 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
447 unless ($step){
448 $template->param( step_1 => 1,step_2 => 1,step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1);
451 if (C4::Context->preference("IndependentBranches")) {
452 my $userenv = C4::Context->userenv;
453 if ($userenv->{flags} % 2 != 1 && $data{'branchcode'}){
454 unless ($userenv->{branch} eq $data{'branchcode'}){
455 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
456 exit;
460 if ($op eq 'add'){
461 $template->param( updtype => 'I', step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1);
463 if ($op eq "modify") {
464 $template->param( updtype => 'M',modify => 1 );
465 $template->param( step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1) unless $step;
466 if ( $step == 4 ) {
467 $template->param( categorycode => $borrower_data->{'categorycode'} );
470 if ( $op eq "duplicate" ) {
471 $template->param( updtype => 'I' );
472 $template->param( step_1 => 1, step_2 => 1, step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1 ) unless $step;
473 $data{'cardnumber'} = "";
476 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if ( ( $op eq 'add' ) or ( $op eq 'duplicate' ) );
477 if(!defined($data{'sex'})){
478 $template->param( none => 1);
479 } elsif($data{'sex'} eq 'F'){
480 $template->param( female => 1);
481 } elsif ($data{'sex'} eq 'M'){
482 $template->param( male => 1);
483 } else {
484 $template->param( none => 1);
487 ##Now all the data to modify a member.
488 my ($categories,$labels)=ethnicitycategories();
490 my $ethnicitycategoriescount=$#{$categories};
491 my $ethcatpopup;
492 if ($ethnicitycategoriescount>=0) {
493 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
494 -id => 'ethnicity',
495 -tabindex=>'',
496 -values=>$categories,
497 -default=>$data{'ethnicity'},
498 -labels=>$labels);
499 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
502 my @typeloop;
503 my $no_categories = 1;
504 my $no_add;
505 foreach (qw(C A S P I X)) {
506 my $action="WHERE category_type=?";
507 ($categories,$labels)=GetborCatFromCatType($_,$action);
508 if(scalar(@$categories) > 0){ $no_categories = 0; }
509 my @categoryloop;
510 foreach my $cat (@$categories){
511 push @categoryloop,{'categorycode' => $cat,
512 'categoryname' => $labels->{$cat},
513 'categorycodeselected' => ((defined($borrower_data->{'categorycode'}) &&
514 $cat eq $borrower_data->{'categorycode'})
515 || (defined($categorycode) && $cat eq $categorycode)),
518 my %typehash;
519 $typehash{'typename'}=$_;
520 my $typedescription = "typename_".$typehash{'typename'};
521 $typehash{'categoryloop'}=\@categoryloop;
522 push @typeloop,{'typename' => $_,
523 $typedescription => 1,
524 'categoryloop' => \@categoryloop};
526 $template->param('typeloop' => \@typeloop,
527 no_categories => $no_categories);
528 if($no_categories){ $no_add = 1; }
529 # test in city
530 if ( $guarantorid ) {
531 $select_city = getidcity($data{city});
533 ($default_city=$select_city) if ($step eq 0);
534 if (!defined($select_city) or $select_city eq '' ){
535 $default_city = &getidcity($data{'city'});
538 my $city_arrayref = GetCities();
539 if (@{$city_arrayref} ) {
540 $template->param( city_cgipopup => 1);
542 if ($default_city) { # flag the current or default val
543 for my $city ( @{$city_arrayref} ) {
544 if ($default_city == $city->{cityid}) {
545 $city->{selected} = 1;
546 last;
552 my $default_roadtype;
553 $default_roadtype=$data{'streettype'} ;
554 my($roadtypeid,$road_type)=GetRoadTypes();
555 $template->param( road_cgipopup => 1) if ($roadtypeid );
556 my $roadpopup = CGI::popup_menu(-name=>'streettype',
557 -id => 'streettype',
558 -values=>$roadtypeid,
559 -labels=>$road_type,
560 -override => 1,
561 -default=>$default_roadtype
564 my $default_borrowertitle = '';
565 unless ( $op eq 'duplicate' ) { $default_borrowertitle=$data{'title'} }
566 my($borrowertitle)=GetTitles();
567 $template->param( title_cgipopup => 1) if ($borrowertitle);
568 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
569 -id => 'btitle',
570 -values=>$borrowertitle,
571 -override => 1,
572 -default=>$default_borrowertitle
575 my @relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
576 my @relshipdata;
577 while (@relationships) {
578 my $relship = shift @relationships || '';
579 my %row = ('relationship' => $relship);
580 if (defined($data{'relationship'}) and $data{'relationship'} eq $relship) {
581 $row{'selected'}=' selected';
582 } else {
583 $row{'selected'}='';
585 push(@relshipdata, \%row);
588 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
589 'lost' => ['lost']);
592 my @flagdata;
593 foreach (keys(%flags)) {
594 my $key = $_;
595 my %row = ('key' => $key,
596 'name' => $flags{$key}[0]);
597 if ($data{$key}) {
598 $row{'yes'}=' checked';
599 $row{'no'}='';
601 else {
602 $row{'yes'}='';
603 $row{'no'}=' checked';
605 push @flagdata,\%row;
608 # get Branch Loop
609 # in modify mod: userbranch value for GetBranchesLoop() comes from borrowers table
610 # in add mod: userbranch value come from branches table (ip correspondence)
612 my $userbranch = '';
613 if (C4::Context->userenv && C4::Context->userenv->{'branch'}) {
614 $userbranch = C4::Context->userenv->{'branch'};
617 if (defined ($data{'branchcode'}) and ( $op eq 'modify' || ( $op eq 'add' && $category_type eq 'C' ) )) {
618 $userbranch = $data{'branchcode'};
621 my $branchloop = GetBranchesLoop( $userbranch );
623 if( !$branchloop ){
624 $no_add = 1;
625 $template->param(no_branches => 1);
627 if($no_categories){
628 $no_add = 1;
629 $template->param(no_categories => 1);
631 $template->param(no_add => $no_add);
632 my $CGIorganisations;
633 my $member_of_institution;
634 if (C4::Context->preference("memberofinstitution")){
635 my $organisations=get_institutions();
636 my @orgs;
637 my %org_labels;
638 foreach my $organisation (keys %$organisations) {
639 push @orgs,$organisation;
640 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
642 $member_of_institution=1;
644 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
645 -name => 'organisations',
646 -labels => \%org_labels,
647 -values => \@orgs,
648 -size => 5,
649 -multiple => 'true'
654 # --------------------------------------------------------------------------------------------------------
656 my $CGIsort = buildCGIsort("Bsort1","sort1",$data{'sort1'});
657 if ($CGIsort) {
658 $template->param(CGIsort1 => $CGIsort);
660 $template->param( sort1 => $data{'sort1'}); # shouldn't this be in an "else" statement like the 2nd one?
662 $CGIsort = buildCGIsort("Bsort2","sort2",$data{'sort2'});
663 if ($CGIsort) {
664 $template->param(CGIsort2 => $CGIsort);
665 } else {
666 $template->param( sort2 => $data{'sort2'});
669 if ($nok) {
670 foreach my $error (@errors) {
671 $template->param($error) || $template->param( $error => 1);
673 $template->param(nok => 1);
676 #Formatting data for display
678 if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){
679 $data{'dateenrolled'}=C4::Dates->today('iso');
681 if ( $op eq 'duplicate' ) {
682 $data{'dateenrolled'} = C4::Dates->today('iso');
683 $data{'dateexpiry'} = GetExpiryDate( $data{'categorycode'}, $data{'dateenrolled'} );
685 if (C4::Context->preference('uppercasesurnames')) {
686 $data{'surname'} &&= uc( $data{'surname'} );
687 $data{'contactname'} &&= uc( $data{'contactname'} );
690 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
691 $data{$_} = format_date($data{$_}); # back to syspref for display
692 $template->param( $_ => $data{$_});
695 if (C4::Context->preference('ExtendedPatronAttributes')) {
696 $template->param(ExtendedPatronAttributes => 1);
697 patron_attributes_form($template, $borrowernumber);
700 if (C4::Context->preference('EnhancedMessagingPreferences')) {
701 if ($op eq 'add') {
702 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
703 } else {
704 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
706 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
707 $template->param(SMSnumber => defined $data{'smsalertnumber'} ? $data{'smsalertnumber'} : $data{'mobile'});
708 $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
711 $template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ? 0 : 1); # associate with step to know where you are
712 $debug and warn "memberentry step: $step";
713 $template->param(%data);
714 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
715 $template->param( step => $step ) if $step; # associate with step to know where u are
717 $template->param(
718 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
719 category_type => $category_type,#to know the category type of the borrower
720 select_city => $select_city,
721 "$category_type" => 1,# associate with step to know where u are
722 destination => $destination,#to know wher u come from and wher u must go in redirect
723 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
724 "op$op" => 1);
726 $template->param( branchloop => $branchloop ) if ( $branchloop );
727 $template->param(
728 nodouble => $nodouble,
729 borrowernumber => $borrowernumber, #register number
730 guarantorid => ($borrower_data->{'guarantorid'} || $guarantorid),
731 ethcatpopup => $ethcatpopup,
732 relshiploop => \@relshipdata,
733 city_loop => $city_arrayref,
734 roadpopup => $roadpopup,
735 borrotitlepopup => $borrotitlepopup,
736 guarantorinfo => $guarantorinfo,
737 flagloop => \@flagdata,
738 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
739 category_type =>$category_type,
740 modify => $modify,
741 nok => $nok,#flag to konw if an error
742 memberofinstution => $member_of_institution,
743 CGIorganisations => $CGIorganisations,
744 NoUpdateLogin => $NoUpdateLogin
747 if(defined($data{'flags'})){
748 $template->param(flags=>$data{'flags'});
750 if(defined($data{'contacttitle'})){
751 $template->param("contacttitle_" . $data{'contacttitle'} => "SELECTED");
755 output_html_with_http_headers $input, $cookie, $template->output;
757 sub parse_extended_patron_attributes {
758 my ($input) = @_;
759 my @patron_attr = grep { /^patron_attr_\d+$/ } $input->param();
761 my @attr = ();
762 my %dups = ();
763 foreach my $key (@patron_attr) {
764 my $value = $input->param($key);
765 next unless defined($value) and $value ne '';
766 my $password = $input->param("${key}_password");
767 my $code = $input->param("${key}_code");
768 next if exists $dups{$code}->{$value};
769 $dups{$code}->{$value} = 1;
770 push @attr, { code => $code, value => $value, password => $password };
772 return \@attr;
775 sub patron_attributes_form {
776 my $template = shift;
777 my $borrowernumber = shift;
779 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
780 if (scalar(@types) == 0) {
781 $template->param(no_patron_attribute_types => 1);
782 return;
784 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
785 my @classes = uniq( map {$_->{class}} @$attributes );
786 @classes = sort @classes;
788 # map patron's attributes into a more convenient structure
789 my %attr_hash = ();
790 foreach my $attr (@$attributes) {
791 push @{ $attr_hash{$attr->{code}} }, $attr;
794 my @attribute_loop = ();
795 my $i = 0;
796 my %items_by_class;
797 foreach my $type_code (map { $_->{code} } @types) {
798 my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
799 my $entry = {
800 class => $attr_type->class(),
801 code => $attr_type->code(),
802 description => $attr_type->description(),
803 repeatable => $attr_type->repeatable(),
804 password_allowed => $attr_type->password_allowed(),
805 category => $attr_type->authorised_value_category(),
806 category_code => $attr_type->category_code(),
807 password => '',
809 if (exists $attr_hash{$attr_type->code()}) {
810 foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
811 my $newentry = { map { $_ => $entry->{$_} } %$entry };
812 $newentry->{value} = $attr->{value};
813 $newentry->{password} = $attr->{password};
814 $newentry->{use_dropdown} = 0;
815 if ($attr_type->authorised_value_category()) {
816 $newentry->{use_dropdown} = 1;
817 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{value});
819 $i++;
820 $newentry->{form_id} = "patron_attr_$i";
821 push @{$items_by_class{$attr_type->class()}}, $newentry;
823 } else {
824 $i++;
825 my $newentry = { map { $_ => $entry->{$_} } %$entry };
826 if ($attr_type->authorised_value_category()) {
827 $newentry->{use_dropdown} = 1;
828 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
830 $newentry->{form_id} = "patron_attr_$i";
831 push @{$items_by_class{$attr_type->class()}}, $newentry;
834 while ( my ($class, @items) = each %items_by_class ) {
835 my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
836 push @attribute_loop, {
837 class => $class,
838 items => @items,
839 lib => $lib,
843 $template->param(patron_attributes => \@attribute_loop);
847 # Local Variables:
848 # tab-width: 8
849 # End: