MT3212 : members modification wipeout dateexpiry
[koha.git] / members / memberentry.pl
blobc99dcb99b26b3c6366436208707b5e106dbd58a6
1 #!/usr/bin/perl
3 # Copyright 2006 SAN OUEST PROVENCE et Paul POULAIN
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 # pragma
21 use strict;
22 use warnings;
24 # external modules
25 use CGI;
26 # use Digest::MD5 qw(md5_base64);
28 # internal modules
29 use C4::Auth;
30 use C4::Context;
31 use C4::Output;
32 use C4::Members;
33 use C4::Members::Attributes;
34 use C4::Members::AttributeTypes;
35 use C4::Koha;
36 use C4::Dates qw/format_date format_date_in_iso/;
37 use C4::Input;
38 use C4::Log;
39 use C4::Letters;
40 use C4::Branch; # GetBranches
41 use C4::Form::MessagingPreferences;
43 use vars qw($debug);
45 BEGIN {
46 $debug = $ENV{DEBUG} || 0;
49 my $input = new CGI;
50 ($debug) or $debug = $input->param('debug') || 0;
51 my %data;
53 my $dbh = C4::Context->dbh;
55 my ($template, $loggedinuser, $cookie)
56 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
57 query => $input,
58 type => "intranet",
59 authnotrequired => 0,
60 flagsrequired => {borrowers => 1},
61 debug => ($debug) ? 1 : 0,
62 });
63 my $guarantorid = $input->param('guarantorid');
64 my $borrowernumber = $input->param('borrowernumber');
65 my $actionType = $input->param('actionType') || '';
66 my $modify = $input->param('modify');
67 my $delete = $input->param('delete');
68 my $op = $input->param('op');
69 my $destination = $input->param('destination');
70 my $cardnumber = $input->param('cardnumber');
71 my $check_member = $input->param('check_member');
72 my $name_city = $input->param('name_city');
73 my $nodouble = $input->param('nodouble');
74 $nodouble = 1 if $op eq 'modify'; # FIXME hack to represent fact that if we're
75 # modifying an existing patron, it ipso facto
76 # isn't a duplicate. Marking FIXME because this
77 # script needs to be refactored.
78 my $select_city = $input->param('select_city');
79 my $nok = $input->param('nok');
80 my $guarantorinfo = $input->param('guarantorinfo');
81 my $step = $input->param('step') || 0;
82 my @errors;
83 my $default_city;
84 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
85 my $check_categorytype=$input->param('check_categorytype');
86 # NOTE: Alert for ethnicity and ethnotes fields, they are invalid in all borrowers form
87 my $borrower_data;
88 my $NoUpdateLogin;
89 my $userenv = C4::Context->userenv;
91 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
93 my $minpw = C4::Context->preference('minPasswordLength');
94 $template->param("minPasswordLength" => $minpw);
96 # function to designate mandatory fields (visually with css)
97 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
98 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
99 foreach (@field_check) {
100 $template->param( "mandatory$_" => 1);
102 $template->param("add"=>1) if ($op eq 'add');
103 $template->param("checked" => 1) if (defined($nodouble) && $nodouble eq 1);
104 ($borrower_data = GetMember( 'borrowernumber'=>$borrowernumber )) if ($op eq 'modify' or $op eq 'save');
105 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
106 my $category_type = $input->param('category_type');
107 my $new_c_type = $category_type; #if we have input param, then we've already chosen the cat_type.
108 unless ($category_type or !($categorycode)){
109 my $borrowercategory = GetBorrowercategory($categorycode);
110 $category_type = $borrowercategory->{'category_type'};
111 my $category_name = $borrowercategory->{'description'};
112 $template->param("categoryname"=>$category_name);
114 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
116 # if a add or modify is requested => check validity of data.
117 %data = %$borrower_data if ($borrower_data);
119 # initialize %newdata
120 my %newdata; # comes from $input->param()
121 if ($op eq 'insert' || $op eq 'modify' || $op eq 'save') {
122 my @names= ($borrower_data && $op ne 'save') ? keys %$borrower_data : $input->param();
123 foreach my $key (@names) {
124 if (defined $input->param($key)) {
125 $newdata{$key} = $input->param($key);
126 $newdata{$key} =~ s/\"/"/g unless $key eq 'borrowernotes' or $key eq 'opacnote';
129 my $dateobject = C4::Dates->new();
130 my $syspref = $dateobject->regexp(); # same syspref format for all 3 dates
131 my $iso = $dateobject->regexp('iso'); #
132 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
133 next unless exists $newdata{$_};
134 my $userdate = $newdata{$_} or next;
135 if ($userdate =~ /$syspref/) {
136 $newdata{$_} = format_date_in_iso($userdate); # if they match syspref format, then convert to ISO
137 } elsif ($userdate =~ /$iso/) {
138 warn "Date $_ ($userdate) is already in ISO format";
139 } else {
140 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
141 $template->param( "ERROR_$_" => 1 ); # else ERROR!
142 push(@errors,"ERROR_$_");
145 # check permission to modify login info.
146 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) ) {
147 $NoUpdateLogin = 1;
151 # remove keys from %newdata that ModMember() doesn't like
153 my @keys_to_delete = (
154 qr/^BorrowerMandatoryField$/,
155 qr/^category_type$/,
156 qr/^check_member$/,
157 qr/^destination$/,
158 qr/^nodouble$/,
159 qr/^op$/,
160 qr/^save$/,
161 qr/^select_roadtype$/,
162 qr/^updtype$/,
163 qr/^SMSnumber$/,
164 qr/^setting_extended_patron_attributes$/,
165 qr/^setting_messaging_prefs$/,
166 qr/^digest$/,
167 qr/^modify$/,
168 qr/^step$/,
169 qr/^\d+$/,
170 qr/^\d+-DAYS/,
171 qr/^patron_attr_/,
173 for my $regexp (@keys_to_delete) {
174 for (keys %newdata) {
175 delete($newdata{$_}) if /$regexp/;
180 #############test for member being unique #############
181 if (($op eq 'insert') and !$nodouble){
182 my $category_type_send=$category_type if ($category_type eq 'I');
183 my $check_category; # recover the category code of the doublon suspect borrowers
184 # ($result,$categorycode) = checkuniquemember($collectivity,$surname,$firstname,$dateofbirth)
185 ($check_member,$check_category) = checkuniquemember(
186 $category_type_send,
187 ($newdata{surname} ? $newdata{surname} : $data{surname} ),
188 ($newdata{firstname} ? $newdata{firstname} : $data{firstname} ),
189 ($newdata{dateofbirth} ? $newdata{dateofbirth} : $data{dateofbirth})
191 if(!$check_member){
192 $nodouble = 1;
194 # recover the category type if the borrowers is a doublon
195 if ($check_category) {
196 my $tmpborrowercategory=GetBorrowercategory($check_category);
197 $check_categorytype=$tmpborrowercategory->{'category_type'};
201 #recover all data from guarantor address phone ,fax...
202 if ( defined($guarantorid) and
203 ( $category_type eq 'C' || $category_type eq 'P' ) and
204 $guarantorid ne '' and
205 $guarantorid ne '0' ) {
206 if (my $guarantordata=GetMember(borrowernumber => $guarantorid)) {
207 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
208 if ( !defined($data{'contactname'}) or $data{'contactname'} eq '' or
209 $data{'contactname'} ne $guarantordata->{'surname'} ) {
210 $newdata{'contactfirstname'}= $guarantordata->{'firstname'};
211 $newdata{'contactname'} = $guarantordata->{'surname'};
212 $newdata{'contacttitle'} = $guarantordata->{'title'};
213 foreach (qw(streetnumber address streettype address2
214 zipcode country city phone phonepro mobile fax email emailpro branchcode)) {
215 $newdata{$_} = $guarantordata->{$_};
221 ###############test to take the right zipcode, country and city name ##############
222 if (!defined($guarantorid) or $guarantorid eq '' or $guarantorid eq '0') {
223 # set only if parameter was passed from the form
224 $newdata{'city'} = $input->param('city') if defined($input->param('city'));
225 $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
226 $newdata{'country'} = $input->param('country') if defined($input->param('country'));
229 #builds default userid
230 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
231 $newdata{'userid'} = Generate_Userid($borrowernumber, $newdata{'firstname'}, $newdata{'surname'});
234 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
235 my $extended_patron_attributes = ();
236 if ($op eq 'save' || $op eq 'insert'){
237 if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
238 push @errors, 'ERROR_cardnumber';
240 my $dateofbirthmandatory = (scalar grep {$_ eq "dateofbirth"} @field_check) ? 1 : 0;
241 if ($newdata{dateofbirth} && $dateofbirthmandatory) {
242 my $age = GetAge($newdata{dateofbirth});
243 my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});
244 my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'});
245 if (($high && ($age > $high)) or ($age < $low)) {
246 push @errors, 'ERROR_age_limitations';
247 $template->param('ERROR_age_limitations' => "$low to $high");
250 if (C4::Context->preference("IndependantBranches")) {
251 if ($userenv && $userenv->{flags} % 2 != 1){
252 $debug and print STDERR " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
253 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
254 push @errors, "ERROR_branch";
258 # Check if the userid is unique
259 unless (Check_Userid($newdata{'userid'},$borrowernumber)) {
260 push @errors, "ERROR_login_exist";
263 my $password = $input->param('password');
264 push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
266 if (C4::Context->preference('ExtendedPatronAttributes')) {
267 $extended_patron_attributes = parse_extended_patron_attributes($input);
268 foreach my $attr (@$extended_patron_attributes) {
269 unless (C4::Members::Attributes::CheckUniqueness($attr->{code}, $attr->{value}, $borrowernumber)) {
270 push @errors, "ERROR_extended_unique_id_failed";
271 $template->param(ERROR_extended_unique_id_failed => "$attr->{code}/$attr->{value}");
277 if ($op eq 'modify' || $op eq 'insert' || $op eq 'save' ){
278 if (exists ($newdata{'dateexpiry'}) && !($newdata{'dateexpiry'})){
279 my $arg2 = $newdata{'dateenrolled'} || C4::Dates->today('iso');
280 $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
284 if ( ( defined $input->param('SMSnumber') ) && ( $input->param('SMSnumber') ne $newdata{'mobile'} ) ) {
285 $newdata{smsalertnumber} = $input->param('SMSnumber');
288 ### Error checks should happen before this line.
289 $nok = $nok || scalar(@errors);
290 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
291 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
292 if ($op eq 'insert'){
293 # we know it's not a duplicate borrowernumber or there would already be an error
294 $borrowernumber = &AddMember(%newdata);
296 # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
297 if ( C4::Context->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'} && $newdata{'password'}) {
298 #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
299 my $emailaddr;
300 if (C4::Context->preference("AutoEmailPrimaryAddress") ne 'OFF' &&
301 $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} =~ /\w\@\w/ ) {
302 $emailaddr = $newdata{C4::Context->preference("AutoEmailPrimaryAddress")}
304 elsif ($newdata{email} =~ /\w\@\w/) {
305 $emailaddr = $newdata{email}
307 elsif ($newdata{emailpro} =~ /\w\@\w/) {
308 $emailaddr = $newdata{emailpro}
310 elsif ($newdata{B_email} =~ /\w\@\w/) {
311 $emailaddr = $newdata{B_email}
313 # if we manage to find a valid email address, send notice
314 if ($emailaddr) {
315 $newdata{emailaddr} = $emailaddr;
316 my $letter = getletter ('members', "ACCTDETAILS:$newdata{'branchcode'}") ;
317 # if $branch notice fails, then email a default notice instead.
318 $letter = getletter ('members', "ACCTDETAILS") if !$letter;
319 SendAlerts ( 'members' , \%newdata , $letter ) if $letter
323 if ($data{'organisations'}){
324 # need to add the members organisations
325 my @orgs=split(/\|/,$data{'organisations'});
326 add_member_orgs($borrowernumber,\@orgs);
328 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
329 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
331 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
332 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template);
334 } elsif ($op eq 'save'){
335 if ($NoUpdateLogin) {
336 delete $newdata{'password'};
337 delete $newdata{'userid'};
339 &ModMember(%newdata);
340 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
341 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
343 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
344 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template);
347 print scalar ($destination eq "circ") ?
348 $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
349 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
350 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
353 if ($delete){
354 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
355 exit; # same as above
358 if ($nok or !$nodouble){
359 $op="add" if ($op eq "insert");
360 $op="modify" if ($op eq "save");
361 %data=%newdata;
362 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
363 unless ($step){
364 $template->param( step_1 => 1,step_2 => 1,step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1);
367 if (C4::Context->preference("IndependantBranches")) {
368 my $userenv = C4::Context->userenv;
369 if ($userenv->{flags} % 2 != 1 && $data{branchcode}){
370 unless ($userenv->{branch} eq $data{'branchcode'}){
371 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
372 exit;
376 if ($op eq 'add'){
377 $template->param( updtype => 'I', step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1);
379 if ($op eq "modify") {
380 $template->param( updtype => 'M',modify => 1 );
381 $template->param( step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1) unless $step;
383 # my $cardnumber=$data{'cardnumber'};
384 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
385 if(!defined($data{'sex'})){
386 $template->param( none => 1);
387 } elsif($data{'sex'} eq 'F'){
388 $template->param( female => 1);
389 } elsif ($data{'sex'} eq 'M'){
390 $template->param( male => 1);
391 } else {
392 $template->param( none => 1);
395 ##Now all the data to modify a member.
396 my ($categories,$labels)=ethnicitycategories();
398 my $ethnicitycategoriescount=$#{$categories};
399 my $ethcatpopup;
400 if ($ethnicitycategoriescount>=0) {
401 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
402 -id => 'ethnicity',
403 -tabindex=>'',
404 -values=>$categories,
405 -default=>$data{'ethnicity'},
406 -labels=>$labels);
407 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
410 my @typeloop;
411 foreach (qw(C A S P I X)) {
412 my $action="WHERE category_type=?";
413 ($categories,$labels)=GetborCatFromCatType($_,$action);
414 my @categoryloop;
415 foreach my $cat (@$categories){
416 push @categoryloop,{'categorycode' => $cat,
417 'categoryname' => $labels->{$cat},
418 'categorycodeselected' => ((defined($borrower_data->{'categorycode'}) &&
419 $cat eq $borrower_data->{'categorycode'})
420 || (defined($categorycode) && $cat eq $categorycode)),
423 my %typehash;
424 $typehash{'typename'}=$_;
425 $typehash{'categoryloop'}=\@categoryloop;
426 push @typeloop,{'typename' => $_,
427 'categoryloop' => \@categoryloop};
429 $template->param('typeloop' => \@typeloop);
431 # test in city
432 $select_city=getidcity($data{'city'}) if defined $guarantorid and ($guarantorid ne '0');
433 ($default_city=$select_city) if ($step eq 0);
434 if (!defined($select_city) or $select_city eq '' ){
435 $default_city = &getidcity($data{'city'});
437 my($cityid);
438 ($cityid,$name_city)=GetCities();
439 $template->param( city_cgipopup => 1) if ($cityid );
440 my $citypopup = CGI::popup_menu(-name=>'select_city',
441 -id => 'select_city',
442 '-values' =>$cityid,
443 -labels=>$name_city,
444 -default=>$default_city,
447 my $default_roadtype;
448 $default_roadtype=$data{'streettype'} ;
449 my($roadtypeid,$road_type)=GetRoadTypes();
450 $template->param( road_cgipopup => 1) if ($roadtypeid );
451 my $roadpopup = CGI::popup_menu(-name=>'streettype',
452 -id => 'streettype',
453 -values=>$roadtypeid,
454 -labels=>$road_type,
455 -override => 1,
456 -default=>$default_roadtype
459 my $default_borrowertitle;
460 $default_borrowertitle=$data{'title'} ;
461 my($borrowertitle)=GetTitles();
462 $template->param( title_cgipopup => 1) if ($borrowertitle);
463 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
464 -id => 'btitle',
465 -values=>$borrowertitle,
466 -override => 1,
467 -default=>$default_borrowertitle
470 my @relationships = split /,|\|/, C4::Context->preference('BorrowerRelationship');
471 my @relshipdata;
472 while (@relationships) {
473 my $relship = shift @relationships || '';
474 my %row = ('relationship' => $relship);
475 if (defined($data{'relationship'}) and $data{'relationship'} eq $relship) {
476 $row{'selected'}=' selected';
477 } else {
478 $row{'selected'}='';
480 push(@relshipdata, \%row);
483 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
484 'lost' => ['lost'],
485 'debarred' => ['debarred']);
488 my @flagdata;
489 foreach (keys(%flags)) {
490 my $key = $_;
491 my %row = ('key' => $key,
492 'name' => $flags{$key}[0]);
493 if ($data{$key}) {
494 $row{'yes'}=' checked';
495 $row{'no'}='';
497 else {
498 $row{'yes'}='';
499 $row{'no'}=' checked';
501 push @flagdata,\%row;
504 #get Branches
505 my @branches;
506 my @select_branch;
507 my %select_branches;
509 my $onlymine=(C4::Context->preference('IndependantBranches') &&
510 C4::Context->userenv &&
511 C4::Context->userenv->{flags} % 2 !=1 &&
512 C4::Context->userenv->{branch}?1:0);
514 my $branches=GetBranches($onlymine);
515 my $default;
517 for my $branch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
518 push @select_branch,$branch;
519 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
520 $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
522 # --------------------------------------------------------------------------------------------------------
523 #in modify mod :default value from $CGIbranch comes from borrowers table
524 #in add mod: default value come from branches table (ip correspendence)
525 $default=$data{'branchcode'} if ($op eq 'modify' || ($op eq 'add' && $category_type eq 'C'));
526 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
527 -name => 'branchcode',
528 -values => \@select_branch,
529 -labels => \%select_branches,
530 -size => 1,
531 -override => 1,
532 -multiple =>0,
533 -default => $default,
535 my $CGIorganisations;
536 my $member_of_institution;
537 if (C4::Context->preference("memberofinstitution")){
538 my $organisations=get_institutions();
539 my @orgs;
540 my %org_labels;
541 foreach my $organisation (keys %$organisations) {
542 push @orgs,$organisation;
543 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
545 $member_of_institution=1;
547 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
548 -name => 'organisations',
549 -labels => \%org_labels,
550 -values => \@orgs,
551 -size => 5,
552 -multiple => 'true'
557 # --------------------------------------------------------------------------------------------------------
559 my $CGIsort = buildCGIsort("Bsort1","sort1",$data{'sort1'});
560 if ($CGIsort) {
561 $template->param(CGIsort1 => $CGIsort);
563 $template->param( sort1 => $data{'sort1'}); # shouldn't this be in an "else" statement like the 2nd one?
565 $CGIsort = buildCGIsort("Bsort2","sort2",$data{'sort2'});
566 if ($CGIsort) {
567 $template->param(CGIsort2 => $CGIsort);
568 } else {
569 $template->param( sort2 => $data{'sort2'});
572 if ($nok) {
573 foreach my $error (@errors) {
574 $template->param($error) || $template->param( $error => 1);
576 $template->param(nok => 1);
579 #Formatting data for display
581 if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){
582 $data{'dateenrolled'}=C4::Dates->today('iso');
584 if (C4::Context->preference('uppercasesurnames')) {
585 $data{'surname'} =uc($data{'surname'} );
586 $data{'contactname'}=uc($data{'contactname'});
588 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
589 $data{$_} = format_date($data{$_}); # back to syspref for display
590 $template->param( $_ => $data{$_});
593 if (C4::Context->preference('ExtendedPatronAttributes')) {
594 $template->param(ExtendedPatronAttributes => 1);
595 patron_attributes_form($template, $borrowernumber);
598 if (C4::Context->preference('EnhancedMessagingPreferences')) {
599 if ($op eq 'add') {
600 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
601 } else {
602 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
604 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
605 $template->param(SMSnumber => defined $data{'smsalertnumber'} ? $data{'smsalertnumber'} : $data{'mobile'});
608 $template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ? 0 : 1); # associate with step to know where you are
609 $debug and warn "memberentry step: $step";
610 $template->param(%data);
611 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
612 $template->param( step => $step ) if $step; # associate with step to know where u are
613 $template->param( debug => $debug ) if $debug;
615 $template->param(
616 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
617 category_type => $category_type,#to know the category type of the borrower
618 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
619 select_city => $select_city,
620 "$category_type" => 1,# associate with step to know where u are
621 destination => $destination,#to know wher u come from and wher u must go in redirect
622 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
623 "op$op" => 1);
625 $template->param(
626 nodouble => $nodouble,
627 borrowernumber => $borrowernumber, #register number
628 guarantorid => (defined($borrower_data->{'guarantorid'})) ? $borrower_data->{'guarantorid'} : $guarantorid,
629 ethcatpopup => $ethcatpopup,
630 relshiploop => \@relshipdata,
631 citypopup => $citypopup,
632 roadpopup => $roadpopup,
633 borrotitlepopup => $borrotitlepopup,
634 guarantorinfo => $guarantorinfo,
635 flagloop => \@flagdata,
636 dateformat => C4::Dates->new()->visual(),
637 C4::Context->preference('dateformat') => 1,
638 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
639 modify => $modify,
640 nok => $nok,#flag to konw if an error
641 CGIbranch => $CGIbranch,
642 memberofinstution => $member_of_institution,
643 CGIorganisations => $CGIorganisations,
644 NoUpdateLogin => $NoUpdateLogin
647 if(defined($data{'flags'})){
648 $template->param(flags=>$data{'flags'});
650 if(defined($data{'contacttitle'})){
651 $template->param("contacttitle_" . $data{'contacttitle'} => "SELECTED");
655 output_html_with_http_headers $input, $cookie, $template->output;
657 sub parse_extended_patron_attributes {
658 my ($input) = @_;
659 my @patron_attr = grep { /^patron_attr_\d+$/ } $input->param();
661 my @attr = ();
662 my %dups = ();
663 foreach my $key (@patron_attr) {
664 my $value = $input->param($key);
665 next unless defined($value) and $value ne '';
666 my $password = $input->param("${key}_password");
667 my $code = $input->param("${key}_code");
668 next if exists $dups{$code}->{$value};
669 $dups{$code}->{$value} = 1;
670 push @attr, { code => $code, value => $value, password => $password };
672 return \@attr;
675 sub patron_attributes_form {
676 my $template = shift;
677 my $borrowernumber = shift;
679 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
680 if (scalar(@types) == 0) {
681 $template->param(no_patron_attribute_types => 1);
682 return;
684 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
686 # map patron's attributes into a more convenient structure
687 my %attr_hash = ();
688 foreach my $attr (@$attributes) {
689 push @{ $attr_hash{$attr->{code}} }, $attr;
692 my @attribute_loop = ();
693 my $i = 0;
694 foreach my $type_code (map { $_->{code} } @types) {
695 my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
696 my $entry = {
697 code => $attr_type->code(),
698 description => $attr_type->description(),
699 repeatable => $attr_type->repeatable(),
700 password_allowed => $attr_type->password_allowed(),
701 category => $attr_type->authorised_value_category(),
702 password => '',
704 if (exists $attr_hash{$attr_type->code()}) {
705 foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
706 my $newentry = { map { $_ => $entry->{$_} } %$entry };
707 $newentry->{value} = $attr->{value};
708 $newentry->{password} = $attr->{password};
709 $newentry->{use_dropdown} = 0;
710 if ($attr_type->authorised_value_category()) {
711 $newentry->{use_dropdown} = 1;
712 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{value});
714 $i++;
715 $newentry->{form_id} = "patron_attr_$i";
716 #use Data::Dumper; die Dumper($entry) if $entry->{use_dropdown};
717 push @attribute_loop, $newentry;
719 } else {
720 $i++;
721 my $newentry = { map { $_ => $entry->{$_} } %$entry };
722 if ($attr_type->authorised_value_category()) {
723 $newentry->{use_dropdown} = 1;
724 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
726 $newentry->{form_id} = "patron_attr_$i";
727 push @attribute_loop, $newentry;
730 $template->param(patron_attributes => \@attribute_loop);
734 # Local Variables:
735 # tab-width: 8
736 # End: