+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;
+ }
+ } else {
+ $i++;
+ my $newentry = { map { $_ => $entry->{$_} } %$entry };
+ if ($attr_type->authorised_value_category()) {
+ $newentry->{use_dropdown} = 1;
+ $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
+ }
+ $newentry->{form_id} = "patron_attr_$i";
+ push @attribute_loop, $newentry;
+ }
+ }
+ $template->param(patron_attributes => \@attribute_loop);
+
+}
+