+my $CGIsort = buildCGIsort("Bsort1","sort1",$data{'sort1'});
+if ($CGIsort) {
+ $template->param(CGIsort1 => $CGIsort);
+}
+$template->param( sort1 => $data{'sort1'}); # shouldn't this be in an "else" statement like the 2nd one?
+
+$CGIsort = buildCGIsort("Bsort2","sort2",$data{'sort2'});
+if ($CGIsort) {
+ $template->param(CGIsort2 => $CGIsort);
+} else {
+ $template->param( sort2 => $data{'sort2'});
+}
+
+if ($nok) {
+ foreach my $error (@errors) {
+ $template->param($error) || $template->param( $error => 1);
+ }
+ $template->param(nok => 1);
+}
+
+ #Formatting data for display
+
+if ($data{'dateenrolled'} eq ''){
+ $data{'dateenrolled'}=C4::Dates->today('iso');
+}
+if (C4::Context->preference('uppercasesurnames')) {
+ $data{'surname'} =uc($data{'surname'} );
+ $data{'contactname'}=uc($data{'contactname'});
+}
+foreach (qw(dateenrolled dateexpiry dateofbirth)) {
+ $data{$_} = format_date($data{$_}); # back to syspref for display
+ $template->param( $_ => $data{$_});
+}
+
+if (C4::Context->preference('ExtendedPatronAttributes')) {
+ $template->param(ExtendedPatronAttributes => 1);
+ patron_attributes_form($template, $borrowernumber);
+}
+
+$template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ? 0 : 1); # associate with step to know where you are
+$debug and warn "memberentry step: $step";
+$template->param(%data);
+$template->param( "step_$step" => 1) if $step; # associate with step to know where u are
+$template->param( step => $step ) if $step; # associate with step to know where u are
+$template->param( debug => $debug ) if $debug;
+$template->param(
+ BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
+ category_type => $category_type,#to know the category type of the borrower
+ DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
+ select_city => $select_city,
+ "$category_type" => 1,# associate with step to know where u are
+ destination => $destination,#to know wher u come from and wher u must go in redirect
+ check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
+ flags =>$data{'flags'},
+ "op$op" => 1,
+ nodouble => $nodouble,
+ borrowernumber => $borrowernumber,#register number
+ "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
+ guarantorid => $guarantorid,
+ ethcatpopup => $ethcatpopup,
+ relshiploop => \@relshipdata,
+ citypopup => $citypopup,
+ roadpopup => $roadpopup,
+ borrotitlepopup => $borrotitlepopup,
+ guarantorinfo => $guarantorinfo,
+ flagloop => \@flagdata,
+ dateformat => C4::Dates->new()->visual(),
+ C4::Context->preference('dateformat') => 1,
+ check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
+ modify => $modify,
+ nok => $nok,#flag to konw if an error
+ CGIbranch => $CGIbranch,
+ memberofinstution => $member_of_institution,
+ CGIorganisations => $CGIorganisations,
+ NoUpdateLogin => $NoUpdateLogin
+ );
+
+output_html_with_http_headers $input, $cookie, $template->output;
+
+sub parse_extended_patron_attributes {
+ my ($input) = @_;
+ my @patron_attr = grep { /^patron_attr_\d+$/ } $input->param();
+
+ my @attr = ();
+ my %dups = ();
+ foreach my $key (@patron_attr) {
+ my $value = $input->param($key);
+ next unless defined($value) and $value ne '';
+ my $password = $input->param("${key}_password");
+ my $code = $input->param("${key}_code");
+ next if exists $dups{$code}->{$value};
+ $dups{$code}->{$value} = 1;
+ push @attr, { code => $code, value => $value, password => $password };
+ }
+ return \@attr;
+}
+
+sub patron_attributes_form {
+ my $template = shift;
+ my $borrowernumber = shift;
+
+ my @types = C4::Members::AttributeTypes::GetAttributeTypes();
+ if (scalar(@types) == 0) {
+ $template->param(no_patron_attribute_types => 1);
+ return;
+ }
+ my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
+
+ # map patron's attributes into a more convenient structure
+ my %attr_hash = ();
+ foreach my $attr (@$attributes) {
+ push @{ $attr_hash{$attr->{code}} }, $attr;
+ }
+
+ my @attribute_loop = ();
+ my $i = 0;
+ foreach my $type_code (map { $_->{code} } @types) {
+ my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
+ my $entry = {
+ code => $attr_type->code(),
+ description => $attr_type->description(),
+ repeatable => $attr_type->repeatable(),
+ password_allowed => $attr_type->password_allowed(),
+ category => $attr_type->authorised_value_category(),
+ password => '',
+ };
+ if (exists $attr_hash{$attr_type->code()}) {
+ foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
+ my $newentry = { map { $_ => $entry->{$_} } %$entry };
+ $newentry->{value} = $attr->{value};
+ $newentry->{password} = $attr->{password};
+ $newentry->{use_dropdown} = 0;
+ if ($attr_type->authorised_value_category()) {
+ $newentry->{use_dropdown} = 1;
+ $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{value});
+ }
+ $i++;
+ $newentry->{form_id} = "patron_attr_$i";
+ #use Data::Dumper; die Dumper($entry) if $entry->{use_dropdown};
+ push @attribute_loop, $newentry;