Moves make_username to PlUser.
authorStéphane Jacob <sj@m4x.org>
Mon, 22 Feb 2010 10:44:18 +0000 (11:44 +0100)
committerStéphane Jacob <sj@m4x.org>
Mon, 22 Feb 2010 13:49:51 +0000 (14:49 +0100)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
classes/pluser.php
include/misc.inc.php

index 84bcc60..862ea9d 100644 (file)
@@ -370,6 +370,22 @@ abstract class PlUser
      */
     abstract public static function isForeignEmailAddress($email);
 
+    private static function stripBadChars($text)
+    {
+        return str_replace(array(' ', "'"), array('-', ''),
+                           strtolower(stripslashed(replace_accent(trim($text)))));
+    }
+
+    /** Creates a username from a first and last name
+     * @param $firstname User's firstname
+     * @param $lasttname User's lastname
+     * return STRING the corresponding username
+     */
+    public static function makeUsername($firstname, $lastname)
+    {
+        return self::stripBadChars($firstname) . '.' . self::stripBadChars($lastname);
+    }
+
     /**
      * Creates a user forlive identifier from:
      * @param $firstname User's firstname
@@ -378,14 +394,12 @@ abstract class PlUser
      */
     public static function makeHrid($firstname, $lastname, $category)
     {
-        assert(trim($category));
-        $plainFirstname = replace_accent(trim($firstname));
-        $plainLastname  = replace_accent(trim($lastname));
-
-        $hrid = strtolower($plainFirstname . '.' . $plainLastname . '.' . trim($category));
-        $hrid = str_replace(' ', '-', $hrid);
-        $hrid = str_replace("'", '', $hrid);
-        return $hrid;
+        $cat = self::stripBadChars($category);
+        if (!cat) {
+            Platal::page()->kill("$category is not a suitable category.");
+        }
+
+        return self::makeUserName($firstname, $lastname) . '.' . $cat;
     }
 
 
index 831685e..fa080e8 100644 (file)
@@ -112,30 +112,6 @@ function replace_accent($string)
     return strtr($string, $uc_convert);
 }
 
-/** creates a username from a first and last name
- *
- * @param $prenom the firstname
- * @param $nom the last name
- *
- * return STRING the corresponding username
- */
-function make_username($prenom,$nom)
-{
-    /* on traite le prenom */
-    $prenomUS=replace_accent(trim($prenom));
-    $prenomUS=stripslashes($prenomUS);
-
-    /* on traite le nom */
-    $nomUS=replace_accent(trim($nom));
-    $nomUS=stripslashes($nomUS);
-
-    // calcul du login
-    $username = strtolower($prenomUS.".".$nomUS);
-    $username = str_replace(" ","-",$username);
-    $username = str_replace("'","",$username);
-    return $username;
-}
-
 /* Un soundex en français posté par Frédéric Bouchery
    Voici une adaptation en PHP de la fonction soundex2 francisée de Frédéric BROUARD (http://sqlpro.developpez.com/Soundex/).
    C'est une bonne démonstration de la force des expressions régulières compatible Perl.