From dfdf89fab4f51ee57adf0890e50256dc7f898551 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 Signed-off-by: Josef Moravec --- Koha/Patron.pm | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/Koha/Patron.pm b/Koha/Patron.pm index 5acdea5..8a37d2a 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Carp; use List::MoreUtils qw( uniq ); +use Text::Unaccent qw( unac_string ); use C4::Context; use C4::Log; @@ -897,6 +898,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; + +} + =head2 Internal methods =head3 _type -- 2.1.4