From f31757c260c01b38d70f1cfa1acbd8c8d70e1033 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 8 Jan 2018 18:44:56 -0300 Subject: [PATCH] Bug 19936: Add the Koha::Patron->generate_userid method --- Koha/Patron.pm | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index fd7e06194e..7d95bd75c9 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -21,6 +21,7 @@ package Koha::Patron; use Modern::Perl; use Carp; +use Text::Unaccent qw( unac_string ); use C4::Context; use C4::Log; @@ -744,6 +745,42 @@ sub has_valid_userid { return $already_exists ? 0 : 1; } +=head3 generate_userid + +my $patron = Koha::Patron->new( $params ); +my $userid = $patron->generate_userid + +Generate a userid using the $surname and the $firstname (if there is a value in $firstname). + +Return the generate userid ($firstname.$surname if there is a $firstname, or $surname if there is no value in $firstname) plus offset (0 if the $userid is unique, or a higher numeric value if not unique). + +# Note: Should we set $self->userid with the generated value? +# Certainly yes, but we AddMember and ModMember will be rewritten + +=cut + +sub generate_userid { + my ($self) = @_; + my $userid; + my $offset = 0; + my $patron = Koha::Patron->new; + my $firstname = $self->firstname; + my $surname = $self->surname; + #The script will "do" the following code and increment the $offset until the generated userid is unique + do { + $firstname =~ s/[[:digit:][:space:][:blank:][:punct:][:cntrl:]]//g; + $surname =~ s/[[:digit:][:space:][:blank:][:punct:][:cntrl:]]//g; + $userid = lc(($firstname)? "$firstname.$surname" : $surname); + $userid = unac_string('utf-8',$userid); + $userid .= $offset unless $offset == 0; + $patron->userid( $userid ); + $offset++; + } while (! $patron->has_valid_userid ); + + return $userid; + +} + =head3 type =cut -- 2.11.0