Bug 20691: (QA follow-up) Fix self-reg
[koha.git] / opac / opac-memberentry.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw ( -utf8 );
21 use Digest::MD5 qw( md5_base64 md5_hex );
22 use JSON;
23 use List::MoreUtils qw( any each_array uniq );
24 use String::Random qw( random_string );
25
26 use C4::Auth;
27 use C4::Output;
28 use C4::Members;
29 use C4::Members::Attributes qw( GetBorrowerAttributes );
30 use C4::Form::MessagingPreferences;
31 use Koha::AuthUtils;
32 use Koha::Patrons;
33 use Koha::Patron::Consent;
34 use Koha::Patron::Modification;
35 use Koha::Patron::Modifications;
36 use C4::Scrubber;
37 use Email::Valid;
38 use Koha::DateUtils;
39 use Koha::Libraries;
40 use Koha::Patron::Attribute::Types;
41 use Koha::Patron::Attributes;
42 use Koha::Patron::Images;
43 use Koha::Patron::Modification;
44 use Koha::Patron::Modifications;
45 use Koha::Patrons;
46 use Koha::Token;
47
48 my $cgi = new CGI;
49 my $dbh = C4::Context->dbh;
50
51 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
52     {
53         template_name   => "opac-memberentry.tt",
54         type            => "opac",
55         query           => $cgi,
56         authnotrequired => 1,
57     }
58 );
59
60 unless ( C4::Context->preference('PatronSelfRegistration') || $borrowernumber )
61 {
62     print $cgi->redirect("/cgi-bin/koha/opac-main.pl");
63     exit;
64 }
65
66 my $action = $cgi->param('action') || q{};
67 if ( $action eq q{} ) {
68     if ($borrowernumber) {
69         $action = 'edit';
70     }
71     else {
72         $action = 'new';
73     }
74 }
75
76 my $mandatory = GetMandatoryFields($action);
77
78 my @libraries = Koha::Libraries->search;
79 if ( my @libraries_to_display = split '\|', C4::Context->preference('PatronSelfRegistrationLibraryList') ) {
80     @libraries = map { my $b = $_; my $branchcode = $_->branchcode; grep( /^$branchcode$/, @libraries_to_display ) ? $b : () } @libraries;
81 }
82 my ( $min, $max ) = C4::Members::get_cardnumber_length();
83 if ( defined $min ) {
84      $template->param(
85          minlength_cardnumber => $min,
86          maxlength_cardnumber => $max
87      );
88  }
89
90 $template->param(
91     action            => $action,
92     hidden            => GetHiddenFields( $mandatory, $action ),
93     mandatory         => $mandatory,
94     libraries         => \@libraries,
95     OPACPatronDetails => C4::Context->preference('OPACPatronDetails'),
96 );
97
98 my $attributes = ParsePatronAttributes($borrowernumber,$cgi);
99 my $conflicting_attribute = 0;
100
101 foreach my $attr (@$attributes) {
102     unless ( C4::Members::Attributes::CheckUniqueness($attr->{code}, $attr->{value}, $borrowernumber) ) {
103         my $attr_info = C4::Members::AttributeTypes->fetch($attr->{code});
104         $template->param(
105             extended_unique_id_failed_code => $attr->{code},
106             extended_unique_id_failed_value => $attr->{value},
107             extended_unique_id_failed_description => $attr_info->description()
108         );
109         $conflicting_attribute = 1;
110     }
111 }
112
113 if ( $action eq 'create' ) {
114
115     my %borrower = ParseCgiForBorrower($cgi);
116
117     %borrower = DelEmptyFields(%borrower);
118
119     my @empty_mandatory_fields = CheckMandatoryFields( \%borrower, $action );
120     my $invalidformfields = CheckForInvalidFields(\%borrower);
121     delete $borrower{'password2'};
122     my $cardnumber_error_code;
123     if ( !grep { $_ eq 'cardnumber' } @empty_mandatory_fields ) {
124         # No point in checking the cardnumber if it's missing and mandatory, it'll just generate a
125         # spurious length warning.
126         $cardnumber_error_code = checkcardnumber( $borrower{cardnumber}, $borrower{borrowernumber} );
127     }
128
129     if ( @empty_mandatory_fields || @$invalidformfields || $cardnumber_error_code || $conflicting_attribute ) {
130         if ( $cardnumber_error_code == 1 ) {
131             $template->param( cardnumber_already_exists => 1 );
132         } elsif ( $cardnumber_error_code == 2 ) {
133             $template->param( cardnumber_wrong_length => 1 );
134         }
135
136         $template->param(
137             empty_mandatory_fields => \@empty_mandatory_fields,
138             invalid_form_fields    => $invalidformfields,
139             borrower               => \%borrower
140         );
141         $template->param( patron_attribute_classes => GeneratePatronAttributesForm( undef, $attributes ) );
142     }
143     elsif (
144         md5_base64( uc( $cgi->param('captcha') ) ) ne $cgi->param('captcha_digest') )
145     {
146         $template->param(
147             failed_captcha => 1,
148             borrower       => \%borrower
149         );
150         $template->param( patron_attribute_classes => GeneratePatronAttributesForm( undef, $attributes ) );
151     }
152     else {
153         if (
154             C4::Context->boolean_preference(
155                 'PatronSelfRegistrationVerifyByEmail')
156           )
157         {
158             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
159                 {
160                     template_name   => "opac-registration-email-sent.tt",
161                     type            => "opac",
162                     query           => $cgi,
163                     authnotrequired => 1,
164                 }
165             );
166             $template->param( 'email' => $borrower{'email'} );
167
168             my $verification_token = md5_hex( time().{}.rand().{}.$$ );
169             while ( Koha::Patron::Modifications->search( { verification_token => $verification_token } )->count() ) {
170                 $verification_token = md5_hex( time().{}.rand().{}.$$ );
171             }
172
173             $borrower{password}          = Koha::AuthUtils::generate_password unless $borrower{password};
174             $borrower{verification_token} = $verification_token;
175
176             Koha::Patron::Modification->new( \%borrower )->store();
177
178             #Send verification email
179             my $letter = C4::Letters::GetPreparedLetter(
180                 module      => 'members',
181                 letter_code => 'OPAC_REG_VERIFY',
182                 lang        => 'default', # Patron does not have a preferred language defined yet
183                 tables      => {
184                     borrower_modifications => $verification_token,
185                 },
186             );
187
188             C4::Letters::EnqueueLetter(
189                 {
190                     letter                 => $letter,
191                     message_transport_type => 'email',
192                     to_address             => $borrower{'email'},
193                     from_address =>
194                       C4::Context->preference('KohaAdminEmailAddress'),
195                 }
196             );
197         }
198         else {
199             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
200                 {
201                     template_name   => "opac-registration-confirmation.tt",
202                     type            => "opac",
203                     query           => $cgi,
204                     authnotrequired => 1,
205                 }
206             );
207
208             $borrower{categorycode}     ||= C4::Context->preference('PatronSelfRegistrationDefaultCategory');
209             $borrower{password}         ||= Koha::AuthUtils::generate_password;
210             my $consent_dt = delete $borrower{gdpr_proc_consent};
211             my $patron = Koha::Patron->new( \%borrower )->store;
212             Koha::Patron::Consent->new({ borrowernumber => $patron->borrowernumber, type => 'GDPR_PROCESSING', given_on => $consent_dt })->store if $consent_dt;
213             if ( $patron ) {
214                 C4::Members::Attributes::SetBorrowerAttributes( $patron->borrowernumber, $attributes );
215                 if ( C4::Context->preference('EnhancedMessagingPreferences') ) {
216                     C4::Form::MessagingPreferences::handle_form_action(
217                         $cgi,
218                         { borrowernumber => $patron->borrowernumber },
219                         $template,
220                         1,
221                         C4::Context->preference('PatronSelfRegistrationDefaultCategory')
222                     );
223                 }
224
225                 $template->param( password_cleartext => $patron->plain_text_password );
226                 $template->param( borrower => $patron->unblessed );
227             } else {
228                 # FIXME Handle possible errors here
229             }
230             $template->param(
231                 PatronSelfRegistrationAdditionalInstructions =>
232                   C4::Context->preference(
233                     'PatronSelfRegistrationAdditionalInstructions')
234             );
235         }
236     }
237 }
238 elsif ( $action eq 'update' ) {
239
240     my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
241     die "Wrong CSRF token"
242         unless Koha::Token->new->check_csrf({
243             session_id => scalar $cgi->cookie('CGISESSID'),
244             token  => scalar $cgi->param('csrf_token'),
245         });
246
247     my %borrower = ParseCgiForBorrower($cgi);
248     $borrower{borrowernumber} = $borrowernumber;
249
250     my @empty_mandatory_fields =
251       CheckMandatoryFields( \%borrower, $action );
252     my $invalidformfields = CheckForInvalidFields(\%borrower);
253
254     # Send back the data to the template
255     %borrower = ( %$borrower, %borrower );
256
257     if (@empty_mandatory_fields || @$invalidformfields) {
258         $template->param(
259             empty_mandatory_fields => \@empty_mandatory_fields,
260             invalid_form_fields    => $invalidformfields,
261             borrower               => \%borrower,
262             csrf_token             => Koha::Token->new->generate_csrf({
263                 session_id => scalar $cgi->cookie('CGISESSID'),
264             }),
265         );
266         $template->param( patron_attribute_classes => GeneratePatronAttributesForm( $borrowernumber, $attributes ) );
267
268         $template->param( action => 'edit' );
269     }
270     else {
271         my %borrower_changes = DelUnchangedFields( $borrowernumber, %borrower );
272         $borrower_changes{'changed_fields'} = join ',', keys %borrower_changes;
273         my $extended_attributes_changes = FilterUnchangedAttributes( $borrowernumber, $attributes );
274
275         if ( %borrower_changes || scalar @{$extended_attributes_changes} > 0 ) {
276             ( $template, $borrowernumber, $cookie ) = get_template_and_user(
277                 {
278                     template_name   => "opac-memberentry-update-submitted.tt",
279                     type            => "opac",
280                     query           => $cgi,
281                     authnotrequired => 1,
282                 }
283             );
284
285             $borrower_changes{borrowernumber} = $borrowernumber;
286             $borrower_changes{extended_attributes} = to_json($extended_attributes_changes);
287
288             Koha::Patron::Modifications->search({ borrowernumber => $borrowernumber })->delete;
289
290             my $m = Koha::Patron::Modification->new( \%borrower_changes )->store();
291
292             my $patron = Koha::Patrons->find( $borrowernumber );
293             $template->param( borrower => $patron->unblessed );
294         }
295         else {
296             my $patron = Koha::Patrons->find( $borrowernumber );
297             $template->param(
298                 action => 'edit',
299                 nochanges => 1,
300                 borrower => $patron->unblessed,
301                 patron_attribute_classes => GeneratePatronAttributesForm( $borrowernumber, $attributes ),
302                 csrf_token => Koha::Token->new->generate_csrf({
303                     session_id => scalar $cgi->cookie('CGISESSID'),
304                 }),
305             );
306         }
307     }
308 }
309 elsif ( $action eq 'edit' ) {    #Display logged in borrower's data
310     my $patron = Koha::Patrons->find( $borrowernumber );
311     my $borrower = $patron->unblessed;
312
313     $template->param(
314         borrower  => $borrower,
315         hidden => GetHiddenFields( $mandatory, 'edit' ),
316         csrf_token => Koha::Token->new->generate_csrf({
317             session_id => scalar $cgi->cookie('CGISESSID'),
318         }),
319     );
320
321     if (C4::Context->preference('OPACpatronimages')) {
322         $template->param( display_patron_image => 1 ) if $patron->image;
323     }
324
325     $template->param( patron_attribute_classes => GeneratePatronAttributesForm( $borrowernumber ) );
326 } else {
327     # Render self-registration page
328     $template->param( patron_attribute_classes => GeneratePatronAttributesForm() );
329 }
330
331 my $captcha = random_string("CCCCC");
332 my $patron_param = scalar Koha::Patrons->find( $borrowernumber );
333 $template->param(
334     has_guarantor_flag => $patron_param->guarantor_relationships->guarantors->_resultset->count
335 ) if $patron_param;
336
337 $template->param(
338     captcha        => $captcha,
339     captcha_digest => md5_base64($captcha),
340     patron         => $patron_param
341 );
342
343 output_html_with_http_headers $cgi, $cookie, $template->output, undef, { force_no_caching => 1 };
344
345 sub GetHiddenFields {
346     my ( $mandatory, $action ) = @_;
347     my %hidden_fields;
348
349     my $BorrowerUnwantedField = $action eq 'edit' || $action eq 'update' ?
350       C4::Context->preference( "PatronSelfModificationBorrowerUnwantedField" ) :
351       C4::Context->preference( "PatronSelfRegistrationBorrowerUnwantedField" );
352
353     my @fields = split( /\|/, $BorrowerUnwantedField || q|| );
354     foreach (@fields) {
355         next unless m/\w/o;
356         #Don't hide mandatory fields
357         next if $mandatory->{$_};
358         $hidden_fields{$_} = 1;
359     }
360
361     return \%hidden_fields;
362 }
363
364 sub GetMandatoryFields {
365     my ($action) = @_;
366
367     my %mandatory_fields;
368
369     my $BorrowerMandatoryField =
370       C4::Context->preference("PatronSelfRegistrationBorrowerMandatoryField");
371
372     my @fields = split( /\|/, $BorrowerMandatoryField );
373     push @fields, 'gdpr_proc_consent' if C4::Context->preference('GDPR_Policy') && $action eq 'create';
374
375     foreach (@fields) {
376         $mandatory_fields{$_} = 1;
377     }
378
379     if ( $action eq 'create' || $action eq 'new' ) {
380         $mandatory_fields{'email'} = 1
381           if C4::Context->boolean_preference(
382             'PatronSelfRegistrationVerifyByEmail');
383     }
384
385     return \%mandatory_fields;
386 }
387
388 sub CheckMandatoryFields {
389     my ( $borrower, $action ) = @_;
390
391     my @empty_mandatory_fields;
392
393     my $mandatory_fields = GetMandatoryFields($action);
394     delete $mandatory_fields->{'cardnumber'};
395
396     foreach my $key ( keys %$mandatory_fields ) {
397         push( @empty_mandatory_fields, $key )
398           unless ( defined( $borrower->{$key} ) && $borrower->{$key} );
399     }
400
401     return @empty_mandatory_fields;
402 }
403
404 sub CheckForInvalidFields {
405     my $borrower = shift;
406     my @invalidFields;
407     if ($borrower->{'email'}) {
408         unless ( Email::Valid->address($borrower->{'email'}) ) {
409             push(@invalidFields, "email");
410         } elsif ( C4::Context->preference("PatronSelfRegistrationEmailMustBeUnique") ) {
411             my $patrons_with_same_email = Koha::Patrons->search( # FIXME Should be search_limited?
412                 {
413                     email => $borrower->{email},
414                     (
415                         exists $borrower->{borrowernumber}
416                         ? ( borrowernumber =>
417                               { '!=' => $borrower->{borrowernumber} } )
418                         : ()
419                     )
420                 }
421             )->count;
422             if ( $patrons_with_same_email ) {
423                 push @invalidFields, "duplicate_email";
424             }
425         }
426     }
427     if ($borrower->{'emailpro'}) {
428         push(@invalidFields, "emailpro") if (!Email::Valid->address($borrower->{'emailpro'}));
429     }
430     if ($borrower->{'B_email'}) {
431         push(@invalidFields, "B_email") if (!Email::Valid->address($borrower->{'B_email'}));
432     }
433     if ( defined $borrower->{'password'}
434         and $borrower->{'password'} ne $borrower->{'password2'} )
435     {
436         push( @invalidFields, "password_match" );
437     }
438     if ( $borrower->{'password'} ) {
439         my ( $is_valid, $error ) = Koha::AuthUtils::is_password_valid( $borrower->{password} );
440           unless ( $is_valid ) {
441               push @invalidFields, 'password_too_short' if $error eq 'too_short';
442               push @invalidFields, 'password_too_weak' if $error eq 'too_weak';
443               push @invalidFields, 'password_has_whitespaces' if $error eq 'has_whitespaces';
444           }
445     }
446
447     return \@invalidFields;
448 }
449
450 sub ParseCgiForBorrower {
451     my ($cgi) = @_;
452
453     my $scrubber = C4::Scrubber->new();
454     my %borrower;
455
456     foreach my $field ( $cgi->param ) {
457         if ( $field =~ '^borrower_' ) {
458             my ($key) = substr( $field, 9 );
459             if ( $field !~ '^borrower_password' ) {
460                 $borrower{$key} = $scrubber->scrub( scalar $cgi->param($field) );
461             } else {
462                 # Allow html characters for passwords
463                 $borrower{$key} = $cgi->param($field);
464             }
465         }
466     }
467
468     my $dob_dt;
469     $dob_dt = eval { dt_from_string( $borrower{'dateofbirth'} ); }
470         if ( $borrower{'dateofbirth'} );
471
472     if ( $dob_dt ) {
473         $borrower{'dateofbirth'} = output_pref ( { dt => $dob_dt, dateonly => 1, dateformat => 'iso' } );
474     }
475     else {
476         # Trigger validation
477         $borrower{'dateofbirth'} = undef;
478     }
479
480     # Replace checkbox 'agreed' by datetime in gdpr_proc_consent
481     $borrower{gdpr_proc_consent} = dt_from_string if  $borrower{gdpr_proc_consent} && $borrower{gdpr_proc_consent} eq 'agreed';
482
483     return %borrower;
484 }
485
486 sub DelUnchangedFields {
487     my ( $borrowernumber, %new_data ) = @_;
488     # get the mandatory fields so we can get the hidden fields
489     my $mandatory = GetMandatoryFields('edit');
490     my $patron = Koha::Patrons->find( $borrowernumber );
491     my $current_data = $patron->unblessed;
492     # get the hidden fields so we don't obliterate them should they have data patrons aren't allowed to modify
493     my $hidden_fields = GetHiddenFields($mandatory, 'edit');
494
495
496     foreach my $key ( keys %new_data ) {
497         if ( ($current_data->{$key} eq $new_data{$key}) || $hidden_fields->{$key} ) {
498            delete $new_data{$key};
499         }
500     }
501
502     return %new_data;
503 }
504
505 sub DelEmptyFields {
506     my (%borrower) = @_;
507
508     foreach my $key ( keys %borrower ) {
509         delete $borrower{$key} unless $borrower{$key};
510     }
511
512     return %borrower;
513 }
514
515 sub FilterUnchangedAttributes {
516     my ( $borrowernumber, $entered_attributes ) = @_;
517
518     my @patron_attributes = grep {$_->opac_editable} Koha::Patron::Attributes->search({ borrowernumber => $borrowernumber })->as_list;
519
520     my $patron_attribute_types;
521     foreach my $attr (@patron_attributes) {
522         $patron_attribute_types->{ $attr->code } += 1;
523     }
524
525     my $passed_attribute_types;
526     foreach my $attr (@{ $entered_attributes }) {
527         $passed_attribute_types->{ $attr->{ code } } += 1;
528     }
529
530     my @changed_attributes;
531
532     # Loop through the current patron attributes
533     foreach my $attribute_type ( keys %{ $patron_attribute_types } ) {
534         if ( $patron_attribute_types->{ $attribute_type } !=  $passed_attribute_types->{ $attribute_type } ) {
535             # count differs, overwrite all attributes for given type
536             foreach my $attr (@{ $entered_attributes }) {
537                 push @changed_attributes, $attr
538                     if $attr->{ code } eq $attribute_type;
539             }
540         } else {
541             # count matches, check values
542             my $changes = 0;
543             foreach my $attr (grep { $_->code eq $attribute_type } @patron_attributes) {
544                 $changes = 1
545                     unless any { $_->{ value } eq $attr->attribute } @{ $entered_attributes };
546                 last if $changes;
547             }
548
549             if ( $changes ) {
550                 foreach my $attr (@{ $entered_attributes }) {
551                     push @changed_attributes, $attr
552                         if $attr->{ code } eq $attribute_type;
553                 }
554             }
555         }
556     }
557
558     # Loop through passed attributes, looking for new ones
559     foreach my $attribute_type ( keys %{ $passed_attribute_types } ) {
560         if ( !defined $patron_attribute_types->{ $attribute_type } ) {
561             # YAY, new stuff
562             foreach my $attr (grep { $_->{code} eq $attribute_type } @{ $entered_attributes }) {
563                 push @changed_attributes, $attr;
564             }
565         }
566     }
567
568     return \@changed_attributes;
569 }
570
571 sub GeneratePatronAttributesForm {
572     my ( $borrowernumber, $entered_attributes ) = @_;
573
574     # Get all attribute types and the values for this patron (if applicable)
575     my @types = grep { $_->opac_editable() or $_->opac_display }
576         Koha::Patron::Attribute::Types->search()->as_list();
577     if ( scalar(@types) == 0 ) {
578         return [];
579     }
580
581     my @displayable_attributes = grep { $_->opac_display }
582         Koha::Patron::Attributes->search({ borrowernumber => $borrowernumber })->as_list;
583
584     my %attr_values = ();
585
586     # Build the attribute values list either from the passed values
587     # or taken from the patron itself
588     if ( defined $entered_attributes ) {
589         foreach my $attr (@$entered_attributes) {
590             push @{ $attr_values{ $attr->{code} } }, $attr->{value};
591         }
592     }
593     elsif ( defined $borrowernumber ) {
594         my @editable_attributes = grep { $_->opac_editable } @displayable_attributes;
595         foreach my $attr (@editable_attributes) {
596             push @{ $attr_values{ $attr->code } }, $attr->attribute;
597         }
598     }
599
600     # Add the non-editable attributes (that don't come from the form)
601     foreach my $attr ( grep { !$_->opac_editable } @displayable_attributes ) {
602         push @{ $attr_values{ $attr->code } }, $attr->attribute;
603     }
604
605     # Find all existing classes
606     my @classes = sort( uniq( map { $_->class } @types ) );
607     my %items_by_class;
608
609     foreach my $attr_type (@types) {
610         push @{ $items_by_class{ $attr_type->class() } }, {
611             type => $attr_type,
612             # If editable, make sure there's at least one empty entry,
613             # to make the template's job easier
614             values => $attr_values{ $attr_type->code() } || ['']
615         }
616             unless !defined $attr_values{ $attr_type->code() }
617                     and !$attr_type->opac_editable;
618     }
619
620     # Finally, build a list of containing classes
621     my @class_loop;
622     foreach my $class (@classes) {
623         next unless ( $items_by_class{$class} );
624
625         my $av = Koha::AuthorisedValues->search(
626             { category => 'PA_CLASS', authorised_value => $class } );
627
628         my $lib = $av->count ? $av->next->opac_description : $class;
629
630         push @class_loop,
631             {
632             class => $class,
633             items => $items_by_class{$class},
634             lib   => $lib,
635             };
636     }
637
638     return \@class_loop;
639 }
640
641 sub ParsePatronAttributes {
642     my ( $borrowernumber, $cgi ) = @_;
643
644     my @codes  = $cgi->multi_param('patron_attribute_code');
645     my @values = $cgi->multi_param('patron_attribute_value');
646
647     my @editable_attribute_types
648         = map { $_->code } Koha::Patron::Attribute::Types->search({ opac_editable => 1 });
649
650     my $ea = each_array( @codes, @values );
651     my @attributes;
652
653     my $delete_candidates = {};
654
655     while ( my ( $code, $value ) = $ea->() ) {
656         if ( any { $_ eq $code } @editable_attribute_types ) {
657             # It is an editable attribute
658             if ( !defined($value) or $value eq '' ) {
659                 $delete_candidates->{$code} = 1
660                     unless $delete_candidates->{$code};
661             }
662             else {
663                 # we've got a value
664                 push @attributes, { code => $code, value => $value };
665
666                 # 'code' is no longer a delete candidate
667                 delete $delete_candidates->{$code}
668                     if defined $delete_candidates->{$code};
669             }
670         }
671     }
672
673     foreach my $code ( keys %{$delete_candidates} ) {
674         if ( Koha::Patron::Attributes->search({
675                 borrowernumber => $borrowernumber, code => $code })->count > 0 )
676         {
677             push @attributes, { code => $code, value => '' }
678                 unless any { $_->{code} eq $code } @attributes;
679         }
680     }
681
682     return \@attributes;
683 }
684
685
686 1;