From 0120de4603409d78c10f320b361c1737787f7e4f 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 00d254e5f1..427d9a4600 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; @@ -866,6 +867,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