Bug 5482: Update colums.def file
[koha.git] / members / memberentry.pl
blobd53f7f10fb9982664fbf867f7437f027cf7da5a7
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);
29 # internal modules
30 use C4::Auth;
31 use C4::Context;
32 use C4::Output;
33 use C4::Members;
34 use C4::Members::Attributes;
35 use C4::Members::AttributeTypes;
36 use C4::Koha;
37 use C4::Dates qw/format_date format_date_in_iso/;
38 use C4::Input;
39 use C4::Log;
40 use C4::Letters;
41 use C4::Branch; # GetBranches
42 use C4::Form::MessagingPreferences;
44 use vars qw($debug);
46 BEGIN {
47 $debug = $ENV{DEBUG} || 0;
50 my $input = new CGI;
51 ($debug) or $debug = $input->param('debug') || 0;
52 my %data;
54 my $dbh = C4::Context->dbh;
56 my ($template, $loggedinuser, $cookie)
57 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
58 query => $input,
59 type => "intranet",
60 authnotrequired => 0,
61 flagsrequired => {borrowers => 1},
62 debug => ($debug) ? 1 : 0,
63 });
64 my $guarantorid = $input->param('guarantorid');
65 my $borrowernumber = $input->param('borrowernumber');
66 my $actionType = $input->param('actionType') || '';
67 my $modify = $input->param('modify');
68 my $delete = $input->param('delete');
69 my $op = $input->param('op');
70 my $destination = $input->param('destination');
71 my $cardnumber = $input->param('cardnumber');
72 my $check_member = $input->param('check_member');
73 my $nodouble = $input->param('nodouble');
74 my $duplicate = $input->param('duplicate');
75 $nodouble = 1 if ($op eq 'modify' or $op eq 'duplicate'); # FIXME hack to represent fact that if we're
76 # modifying an existing patron, it ipso facto
77 # isn't a duplicate. Marking FIXME because this
78 # script needs to be refactored.
79 my $select_city = $input->param('select_city');
80 my $nok = $input->param('nok');
81 my $guarantorinfo = $input->param('guarantorinfo');
82 my $step = $input->param('step') || 0;
83 my @errors;
84 my $default_city;
85 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
86 my $check_categorytype=$input->param('check_categorytype');
87 # NOTE: Alert for ethnicity and ethnotes fields, they are invalid in all borrowers form
88 my $borrower_data;
89 my $NoUpdateLogin;
90 my $userenv = C4::Context->userenv;
92 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
94 my $minpw = C4::Context->preference('minPasswordLength');
95 $template->param("minPasswordLength" => $minpw);
97 # function to designate mandatory fields (visually with css)
98 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
99 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
100 foreach (@field_check) {
101 $template->param( "mandatory$_" => 1);
103 # function to designate unwanted fields
104 my $check_BorrowerUnwantedField=C4::Context->preference("BorrowerUnwantedField");
105 @field_check=split(/\|/,$check_BorrowerUnwantedField);
106 foreach (@field_check) {
107 next unless m/\w/o;
108 $template->param( "no$_" => 1);
110 $template->param( "add" => 1 ) if ( $op eq 'add' );
111 $template->param( "duplicate" => 1 ) if ( $op eq 'duplicate' );
112 $template->param( "checked" => 1 ) if ( defined($nodouble) && $nodouble eq 1 );
113 ( $borrower_data = GetMember( 'borrowernumber' => $borrowernumber ) ) if ( $op eq 'modify' or $op eq 'save' or $op eq 'duplicate' );
114 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
115 my $category_type = $input->param('category_type') || '';
116 if ($category_type){
117 $template->{VARS}->{'type_only'} = 1;
119 my $new_c_type = $category_type; #if we have input param, then we've already chosen the cat_type.
120 unless ($category_type or !($categorycode)){
121 my $borrowercategory = GetBorrowercategory($categorycode);
122 $category_type = $borrowercategory->{'category_type'};
123 my $category_name = $borrowercategory->{'description'};
124 $template->param("categoryname"=>$category_name);
126 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
128 # if a add or modify is requested => check validity of data.
129 %data = %$borrower_data if ($borrower_data);
131 # initialize %newdata
132 my %newdata; # comes from $input->param()
133 if ( $op eq 'insert' || $op eq 'modify' || $op eq 'save' || $op eq 'duplicate' ) {
134 my @names = ( $borrower_data && $op ne 'save' ) ? keys %$borrower_data : $input->param();
135 foreach my $key (@names) {
136 if (defined $input->param($key)) {
137 $newdata{$key} = $input->param($key);
138 $newdata{$key} =~ s/\"/"/g unless $key eq 'borrowernotes' or $key eq 'opacnote';
142 ## Manipulate debarred
143 if ( $newdata{debarred} ) {
144 $newdata{debarred} = $newdata{datedebarred} ? $newdata{datedebarred} : "9999-12-31";
145 } elsif ( exists( $newdata{debarred} ) && !( $newdata{debarred} ) ) {
146 undef( $newdata{debarred} );
147 undef( $newdata{debarredcomment} );
148 } elsif ( exists( $newdata{debarredcomment} ) && $newdata{debarredcomment} eq "" ) {
149 undef( $newdata{debarredcomment} );
152 my $dateobject = C4::Dates->new();
153 my $syspref = $dateobject->regexp(); # same syspref format for all 3 dates
154 my $iso = $dateobject->regexp('iso'); #
155 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
156 next unless exists $newdata{$_};
157 my $userdate = $newdata{$_} or next;
158 if ($userdate =~ /$syspref/) {
159 $newdata{$_} = format_date_in_iso($userdate); # if they match syspref format, then convert to ISO
160 } elsif ($userdate =~ /$iso/) {
161 warn "Date $_ ($userdate) is already in ISO format";
162 } else {
163 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
164 $template->param( "ERROR_$_" => 1 ); # else ERROR!
165 push(@errors,"ERROR_$_");
168 # check permission to modify login info.
169 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) ) {
170 $NoUpdateLogin = 1;
174 # remove keys from %newdata that ModMember() doesn't like
176 my @keys_to_delete = (
177 qr/^BorrowerMandatoryField$/,
178 qr/^category_type$/,
179 qr/^check_member$/,
180 qr/^destination$/,
181 qr/^nodouble$/,
182 qr/^op$/,
183 qr/^save$/,
184 qr/^select_roadtype$/,
185 qr/^updtype$/,
186 qr/^SMSnumber$/,
187 qr/^setting_extended_patron_attributes$/,
188 qr/^setting_messaging_prefs$/,
189 qr/^digest$/,
190 qr/^modify$/,
191 qr/^step$/,
192 qr/^\d+$/,
193 qr/^\d+-DAYS/,
194 qr/^patron_attr_/,
196 for my $regexp (@keys_to_delete) {
197 for (keys %newdata) {
198 delete($newdata{$_}) if /$regexp/;
203 #############test for member being unique #############
204 if ( ( $op eq 'insert' ) and !$nodouble ) {
205 my $category_type_send;
206 if ( $category_type eq 'I' ) {
207 $category_type_send = $category_type;
209 my $check_category; # recover the category code of the doublon suspect borrowers
210 # ($result,$categorycode) = checkuniquemember($collectivity,$surname,$firstname,$dateofbirth)
211 ( $check_member, $check_category ) = checkuniquemember(
212 $category_type_send,
213 ( $newdata{surname} ? $newdata{surname} : $data{surname} ),
214 ( $newdata{firstname} ? $newdata{firstname} : $data{firstname} ),
215 ( $newdata{dateofbirth} ? $newdata{dateofbirth} : $data{dateofbirth} )
217 if ( !$check_member ) {
218 $nodouble = 1;
221 # recover the category type if the borrowers is a doublon
222 if ($check_category) {
223 my $tmpborrowercategory = GetBorrowercategory($check_category);
224 $check_categorytype = $tmpborrowercategory->{'category_type'};
228 #recover all data from guarantor address phone ,fax...
229 if ( $guarantorid and ( $category_type eq 'C' || $category_type eq 'P' )) {
230 if (my $guarantordata=GetMember(borrowernumber => $guarantorid)) {
231 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
232 $newdata{'contactfirstname'}= $guarantordata->{'firstname'};
233 $newdata{'contactname'} = $guarantordata->{'surname'};
234 $newdata{'contacttitle'} = $guarantordata->{'title'};
235 if ( $op eq 'add' ) {
236 foreach (qw(streetnumber address streettype address2
237 zipcode country city state phone phonepro mobile fax email emailpro branchcode
238 B_streetnumber B_streettype B_address B_address2
239 B_city B_state B_zipcode B_country B_email B_phone)) {
240 $newdata{$_} = $guarantordata->{$_};
246 ###############test to take the right zipcode, country and city name ##############
247 # set only if parameter was passed from the form
248 $newdata{'city'} = $input->param('city') if defined($input->param('city'));
249 $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
250 $newdata{'country'} = $input->param('country') if defined($input->param('country'));
252 #builds default userid
253 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
254 $newdata{'userid'} = Generate_Userid($borrowernumber, $newdata{'firstname'}, $newdata{'surname'});
257 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
258 my $extended_patron_attributes = ();
259 if ($op eq 'save' || $op eq 'insert'){
260 # If the cardnumber is blank, treat it as null.
261 $newdata{'cardnumber'} = undef if $newdata{'cardnumber'} =~ /^\s*$/;
263 if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
264 push @errors, 'ERROR_cardnumber';
266 my $dateofbirthmandatory = (scalar grep {$_ eq "dateofbirth"} @field_check) ? 1 : 0;
267 if ($newdata{dateofbirth} && $dateofbirthmandatory) {
268 my $age = GetAge($newdata{dateofbirth});
269 my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});
270 my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'});
271 if (($high && ($age > $high)) or ($age < $low)) {
272 push @errors, 'ERROR_age_limitations';
273 $template->param('ERROR_age_limitations' => "$low to $high");
277 if($newdata{surname} && C4::Context->preference('uppercasesurnames')) {
278 $newdata{'surname'} = uc($newdata{'surname'});
281 if (C4::Context->preference("IndependantBranches")) {
282 if ($userenv && $userenv->{flags} % 2 != 1){
283 $debug and print STDERR " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
284 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
285 push @errors, "ERROR_branch";
289 # Check if the userid is unique
290 unless (Check_Userid($newdata{'userid'},$borrowernumber)) {
291 push @errors, "ERROR_login_exist";
294 my $password = $input->param('password');
295 my $password2 = $input->param('password2');
296 push @errors, "ERROR_password_mismatch" if ( $password ne $password2 );
297 push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
299 if (C4::Context->preference('ExtendedPatronAttributes')) {
300 $extended_patron_attributes = parse_extended_patron_attributes($input);
301 foreach my $attr (@$extended_patron_attributes) {
302 unless (C4::Members::Attributes::CheckUniqueness($attr->{code}, $attr->{value}, $borrowernumber)) {
303 push @errors, "ERROR_extended_unique_id_failed";
304 $template->param(ERROR_extended_unique_id_failed => "$attr->{code}/$attr->{value}");
310 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){
311 unless ($newdata{'dateexpiry'}){
312 my $arg2 = $newdata{'dateenrolled'} || C4::Dates->today('iso');
313 $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
317 if ( ( defined $input->param('SMSnumber') ) && ( $input->param('SMSnumber') ne $newdata{'mobile'} ) ) {
318 $newdata{smsalertnumber} = $input->param('SMSnumber');
321 ### Error checks should happen before this line.
322 $nok = $nok || scalar(@errors);
323 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
324 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
325 if ($op eq 'insert'){
326 # we know it's not a duplicate borrowernumber or there would already be an error
327 $borrowernumber = &AddMember(%newdata);
328 $newdata{'borrowernumber'} = $borrowernumber;
330 # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
331 if ( C4::Context->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'} && $newdata{'password'}) {
332 #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
333 my $emailaddr;
334 if (C4::Context->preference("AutoEmailPrimaryAddress") ne 'OFF' &&
335 $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} =~ /\w\@\w/ ) {
336 $emailaddr = $newdata{C4::Context->preference("AutoEmailPrimaryAddress")}
338 elsif ($newdata{email} =~ /\w\@\w/) {
339 $emailaddr = $newdata{email}
341 elsif ($newdata{emailpro} =~ /\w\@\w/) {
342 $emailaddr = $newdata{emailpro}
344 elsif ($newdata{B_email} =~ /\w\@\w/) {
345 $emailaddr = $newdata{B_email}
347 # if we manage to find a valid email address, send notice
348 if ($emailaddr) {
349 $newdata{emailaddr} = $emailaddr;
350 my $err;
351 eval {
352 $err = SendAlerts ( 'members', \%newdata, "ACCTDETAILS" );
354 if ( $@ ) {
355 $template->param(error_alert => $@);
356 } elsif ( defined $err->{error} and $err->{error} eq "no_email" ) {
357 $template->{VARS}->{'error_alert'} = "no_email";
358 } else {
359 $template->{VARS}->{'info_alert'} = 1;
364 if ($data{'organisations'}){
365 # need to add the members organisations
366 my @orgs=split(/\|/,$data{'organisations'});
367 add_member_orgs($borrowernumber,\@orgs);
369 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
370 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
372 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
373 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template, 1, $newdata{'categorycode'});
375 } elsif ($op eq 'save'){
376 if ($NoUpdateLogin) {
377 delete $newdata{'password'};
378 delete $newdata{'userid'};
380 &ModMember(%newdata) unless scalar(keys %newdata) <= 1; # bug 4508 - avoid crash if we're not
381 # updating any columns in the borrowers table,
382 # which can happen if we're only editing the
383 # patron attributes or messaging preferences sections
384 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
385 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
387 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
388 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template);
391 print scalar ($destination eq "circ") ?
392 $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
393 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
394 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
397 if ($delete){
398 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
399 exit; # same as above
402 if ($nok or !$nodouble){
403 $op="add" if ($op eq "insert");
404 $op="modify" if ($op eq "save");
405 %data=%newdata;
406 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
407 unless ($step){
408 $template->param( step_1 => 1,step_2 => 1,step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1);
411 if (C4::Context->preference("IndependantBranches")) {
412 my $userenv = C4::Context->userenv;
413 if ($userenv->{flags} % 2 != 1 && $data{'branchcode'}){
414 unless ($userenv->{branch} eq $data{'branchcode'}){
415 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
416 exit;
420 if ($op eq 'add'){
421 $template->param( updtype => 'I', step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1);
423 if ($op eq "modify") {
424 $template->param( updtype => 'M',modify => 1 );
425 $template->param( step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1) unless $step;
427 if ( $op eq "duplicate" ) {
428 $template->param( updtype => 'I' );
429 $template->param( step_1 => 1, step_2 => 1, step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1 ) unless $step;
432 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
433 if(!defined($data{'sex'})){
434 $template->param( none => 1);
435 } elsif($data{'sex'} eq 'F'){
436 $template->param( female => 1);
437 } elsif ($data{'sex'} eq 'M'){
438 $template->param( male => 1);
439 } else {
440 $template->param( none => 1);
443 ##Now all the data to modify a member.
444 my ($categories,$labels)=ethnicitycategories();
446 my $ethnicitycategoriescount=$#{$categories};
447 my $ethcatpopup;
448 if ($ethnicitycategoriescount>=0) {
449 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
450 -id => 'ethnicity',
451 -tabindex=>'',
452 -values=>$categories,
453 -default=>$data{'ethnicity'},
454 -labels=>$labels);
455 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
458 my @typeloop;
459 my $no_categories = 1;
460 my $no_add;
461 foreach (qw(C A S P I X)) {
462 my $action="WHERE category_type=?";
463 ($categories,$labels)=GetborCatFromCatType($_,$action);
464 if(scalar(@$categories) > 0){ $no_categories = 0; }
465 my @categoryloop;
466 foreach my $cat (@$categories){
467 push @categoryloop,{'categorycode' => $cat,
468 'categoryname' => $labels->{$cat},
469 'categorycodeselected' => ((defined($borrower_data->{'categorycode'}) &&
470 $cat eq $borrower_data->{'categorycode'})
471 || (defined($categorycode) && $cat eq $categorycode)),
474 my %typehash;
475 $typehash{'typename'}=$_;
476 my $typedescription = "typename_".$typehash{'typename'};
477 $typehash{'categoryloop'}=\@categoryloop;
478 push @typeloop,{'typename' => $_,
479 $typedescription => 1,
480 'categoryloop' => \@categoryloop};
482 $template->param('typeloop' => \@typeloop,
483 no_categories => $no_categories);
484 if($no_categories){ $no_add = 1; }
485 # test in city
486 if ( $guarantorid ) {
487 $select_city = getidcity($data{city});
489 ($default_city=$select_city) if ($step eq 0);
490 if (!defined($select_city) or $select_city eq '' ){
491 $default_city = &getidcity($data{'city'});
494 my $city_arrayref = GetCities();
495 if (@{$city_arrayref} ) {
496 $template->param( city_cgipopup => 1);
498 if ($default_city) { # flag the current or default val
499 for my $city ( @{$city_arrayref} ) {
500 if ($default_city == $city->{cityid}) {
501 $city->{selected} = 1;
502 last;
508 my $default_roadtype;
509 $default_roadtype=$data{'streettype'} ;
510 my($roadtypeid,$road_type)=GetRoadTypes();
511 $template->param( road_cgipopup => 1) if ($roadtypeid );
512 my $roadpopup = CGI::popup_menu(-name=>'streettype',
513 -id => 'streettype',
514 -values=>$roadtypeid,
515 -labels=>$road_type,
516 -override => 1,
517 -default=>$default_roadtype
520 my $default_borrowertitle;
521 $default_borrowertitle=$data{'title'} ;
522 my($borrowertitle)=GetTitles();
523 $template->param( title_cgipopup => 1) if ($borrowertitle);
524 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
525 -id => 'btitle',
526 -values=>$borrowertitle,
527 -override => 1,
528 -default=>$default_borrowertitle
531 my @relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
532 my @relshipdata;
533 while (@relationships) {
534 my $relship = shift @relationships || '';
535 my %row = ('relationship' => $relship);
536 if (defined($data{'relationship'}) and $data{'relationship'} eq $relship) {
537 $row{'selected'}=' selected';
538 } else {
539 $row{'selected'}='';
541 push(@relshipdata, \%row);
544 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
545 'lost' => ['lost']);
548 my @flagdata;
549 foreach (keys(%flags)) {
550 my $key = $_;
551 my %row = ('key' => $key,
552 'name' => $flags{$key}[0]);
553 if ($data{$key}) {
554 $row{'yes'}=' checked';
555 $row{'no'}='';
557 else {
558 $row{'yes'}='';
559 $row{'no'}=' checked';
561 push @flagdata,\%row;
564 #get Branches
565 my @branches;
566 my @select_branch;
567 my %select_branches;
569 my $onlymine=(C4::Context->preference('IndependantBranches') &&
570 C4::Context->userenv &&
571 C4::Context->userenv->{flags} % 2 !=1 &&
572 C4::Context->userenv->{branch}?1:0);
574 my $branches=GetBranches($onlymine);
575 my $default;
576 my $CGIbranch;
577 for my $branch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
578 push @select_branch,$branch;
579 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
580 $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
582 if(scalar(@select_branch) > 0){
583 # --------------------------------------------------------------------------------------------------------
584 #in modify mod :default value from $CGIbranch comes from borrowers table
585 #in add mod: default value come from branches table (ip correspendence)
586 if (defined ($data{'branchcode'}) and ( $op eq 'modify' || ( $op eq 'add' && $category_type eq 'C' ) )) {
587 $default = $data{'branchcode'};
589 $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
590 -name => 'branchcode',
591 -values => \@select_branch,
592 -labels => \%select_branches,
593 -size => 1,
594 -override => 1,
595 -multiple =>0,
596 -default => $default,
600 if(!$CGIbranch){
601 $no_add = 1;
602 $template->param(no_branches => 1);
604 if($no_categories){
605 $no_add = 1;
606 $template->param(no_categories => 1);
608 $template->param(no_add => $no_add);
609 my $CGIorganisations;
610 my $member_of_institution;
611 if (C4::Context->preference("memberofinstitution")){
612 my $organisations=get_institutions();
613 my @orgs;
614 my %org_labels;
615 foreach my $organisation (keys %$organisations) {
616 push @orgs,$organisation;
617 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
619 $member_of_institution=1;
621 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
622 -name => 'organisations',
623 -labels => \%org_labels,
624 -values => \@orgs,
625 -size => 5,
626 -multiple => 'true'
631 # --------------------------------------------------------------------------------------------------------
633 my $CGIsort = buildCGIsort("Bsort1","sort1",$data{'sort1'});
634 if ($CGIsort) {
635 $template->param(CGIsort1 => $CGIsort);
637 $template->param( sort1 => $data{'sort1'}); # shouldn't this be in an "else" statement like the 2nd one?
639 $CGIsort = buildCGIsort("Bsort2","sort2",$data{'sort2'});
640 if ($CGIsort) {
641 $template->param(CGIsort2 => $CGIsort);
642 } else {
643 $template->param( sort2 => $data{'sort2'});
646 if ($nok) {
647 foreach my $error (@errors) {
648 $template->param($error) || $template->param( $error => 1);
650 $template->param(nok => 1);
653 #Formatting data for display
655 if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){
656 $data{'dateenrolled'}=C4::Dates->today('iso');
658 if ( $op eq 'duplicate' ) {
659 $data{'dateenrolled'} = C4::Dates->today('iso');
660 $data{'dateexpiry'} = GetExpiryDate( $data{'categorycode'}, $data{'dateenrolled'} );
662 if (C4::Context->preference('uppercasesurnames')) {
663 $data{'surname'} =uc($data{'surname'} );
664 $data{'contactname'}=uc($data{'contactname'});
667 $data{debarred} = C4::Overdues::CheckBorrowerDebarred($borrowernumber);
668 $data{datedebarred} = $data{debarred} if ( $data{debarred} && $data{debarred} ne "9999-12-31" );
669 foreach (qw(dateenrolled dateexpiry dateofbirth datedebarred)) {
670 $data{$_} = format_date($data{$_}); # back to syspref for display
671 $template->param( $_ => $data{$_});
674 if (C4::Context->preference('ExtendedPatronAttributes')) {
675 $template->param(ExtendedPatronAttributes => 1);
676 patron_attributes_form($template, $borrowernumber);
679 if (C4::Context->preference('EnhancedMessagingPreferences')) {
680 if ($op eq 'add') {
681 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
682 } else {
683 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
685 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
686 $template->param(SMSnumber => defined $data{'smsalertnumber'} ? $data{'smsalertnumber'} : $data{'mobile'});
689 $template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ? 0 : 1); # associate with step to know where you are
690 $debug and warn "memberentry step: $step";
691 $template->param(%data);
692 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
693 $template->param( step => $step ) if $step; # associate with step to know where u are
694 $template->param( debug => $debug ) if $debug;
696 $template->param(
697 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
698 category_type => $category_type,#to know the category type of the borrower
699 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
700 select_city => $select_city,
701 "$category_type" => 1,# associate with step to know where u are
702 destination => $destination,#to know wher u come from and wher u must go in redirect
703 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
704 "op$op" => 1);
706 $template->param(CGIbranch=>$CGIbranch) if ($CGIbranch);
707 $template->param(
708 nodouble => $nodouble,
709 borrowernumber => $borrowernumber, #register number
710 guarantorid => ($borrower_data->{'guarantorid'} || $guarantorid),
711 ethcatpopup => $ethcatpopup,
712 relshiploop => \@relshipdata,
713 city_loop => $city_arrayref,
714 roadpopup => $roadpopup,
715 borrotitlepopup => $borrotitlepopup,
716 guarantorinfo => $guarantorinfo,
717 flagloop => \@flagdata,
718 dateformat => C4::Dates->new()->visual(),
719 C4::Context->preference('dateformat') => 1,
720 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
721 category_type =>$category_type,
722 modify => $modify,
723 nok => $nok,#flag to konw if an error
724 memberofinstution => $member_of_institution,
725 CGIorganisations => $CGIorganisations,
726 NoUpdateLogin => $NoUpdateLogin
729 if(defined($data{'flags'})){
730 $template->param(flags=>$data{'flags'});
732 if(defined($data{'contacttitle'})){
733 $template->param("contacttitle_" . $data{'contacttitle'} => "SELECTED");
737 output_html_with_http_headers $input, $cookie, $template->output;
739 sub parse_extended_patron_attributes {
740 my ($input) = @_;
741 my @patron_attr = grep { /^patron_attr_\d+$/ } $input->param();
743 my @attr = ();
744 my %dups = ();
745 foreach my $key (@patron_attr) {
746 my $value = $input->param($key);
747 next unless defined($value) and $value ne '';
748 my $password = $input->param("${key}_password");
749 my $code = $input->param("${key}_code");
750 next if exists $dups{$code}->{$value};
751 $dups{$code}->{$value} = 1;
752 push @attr, { code => $code, value => $value, password => $password };
754 return \@attr;
757 sub patron_attributes_form {
758 my $template = shift;
759 my $borrowernumber = shift;
761 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
762 if (scalar(@types) == 0) {
763 $template->param(no_patron_attribute_types => 1);
764 return;
766 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
768 # map patron's attributes into a more convenient structure
769 my %attr_hash = ();
770 foreach my $attr (@$attributes) {
771 push @{ $attr_hash{$attr->{code}} }, $attr;
774 my @attribute_loop = ();
775 my $i = 0;
776 foreach my $type_code (map { $_->{code} } @types) {
777 my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
778 my $entry = {
779 code => $attr_type->code(),
780 description => $attr_type->description(),
781 repeatable => $attr_type->repeatable(),
782 password_allowed => $attr_type->password_allowed(),
783 category => $attr_type->authorised_value_category(),
784 password => '',
786 if (exists $attr_hash{$attr_type->code()}) {
787 foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
788 my $newentry = { map { $_ => $entry->{$_} } %$entry };
789 $newentry->{value} = $attr->{value};
790 $newentry->{password} = $attr->{password};
791 $newentry->{use_dropdown} = 0;
792 if ($attr_type->authorised_value_category()) {
793 $newentry->{use_dropdown} = 1;
794 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{value});
796 $i++;
797 $newentry->{form_id} = "patron_attr_$i";
798 #use Data::Dumper; die Dumper($entry) if $entry->{use_dropdown};
799 push @attribute_loop, $newentry;
801 } else {
802 $i++;
803 my $newentry = { map { $_ => $entry->{$_} } %$entry };
804 if ($attr_type->authorised_value_category()) {
805 $newentry->{use_dropdown} = 1;
806 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
808 $newentry->{form_id} = "patron_attr_$i";
809 push @attribute_loop, $newentry;
812 $template->param(patron_attributes => \@attribute_loop);
816 # Local Variables:
817 # tab-width: 8
818 # End: