Merge commit 'origin/fusionax' into account
[platal.git] / classes / profile.php
index 217a3f0..fc819f6 100644 (file)
@@ -23,6 +23,7 @@ class Profile
 {
     private $pid;
     private $hrpid;
+    private $data = array();
 
     private function __construct($login)
     {
@@ -59,6 +60,98 @@ class Profile
         return $this->hrpid;
     }
 
+    public function promo()
+    {
+        return $this->promo;
+    }
+
+    /** Print a name with the given formatting:
+     * %s = • for women
+     * %f = firstname
+     * %l = lastname
+     * %F = fullname
+     * %S = shortname
+     * %p = promo
+     */
+    public function name($format)
+    {
+        return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
+                           array($this->isFemale() ? '•' : '',
+                                 $this->first_name, $this->last_name,
+                                 $this->full_name, $this->short_name,
+                                 $this->promo), $format);
+    }
+
+    public function fullName($with_promo = false)
+    {
+        if ($with_promo) {
+            return $this->full_name . ' (' . $this->promo . ')';
+        }
+        return $this->full_name;
+    }
+
+    public function shortName($with_promo = false)
+    {
+        if ($with_promo) {
+            return $this->short_name . ' (' . $this->promo . ')';
+        }
+        return $this->short_name;
+    }
+
+    public function firstName()
+    {
+        return $this->first_name;
+    }
+
+    public function lastName()
+    {
+        return $this->last_name;
+    }
+
+    public function isFemale()
+    {
+        return $this->sex == PlUser::GENDER_FEMALE;
+    }
+
+    public function data()
+    {
+        $this->first_name;
+        return $this->data;
+    }
+
+    public function __get($name)
+    {
+        if (property_exists($this, $name)) {
+            return $this->$name;
+        }
+
+        if (empty($this->data)) {
+            // XXX: Temporary, use data from auth_user_md5 (waiting for data from newdirectory
+            $this->data = XDB::fetchOneAssoc('SELECT  p.*, u.prenom AS first_name,
+                                                      IF(u.nom_usage != "", u.nom_usage, u.nom) AS last_name,
+                                                      u.promo AS promo,
+                                                      CONCAT(u.prenom, " ", u.nom) AS short_name,
+                                                      IF(u.nom_usage != "",
+                                                         CONCAT(u.nom_usage, " (", u.nom, "),", u.prenom),
+                                                         CONCAT(u.nom, ", ", u.prenom)) AS full_name
+                                                FROM  profiles AS p
+                                          INNER JOIN  auth_user_md5 AS u ON (u.user_id = p.pid)
+                                               WHERE  p.pid = {?}',
+                                             $this->id());
+        }
+        if (isset($this->data[$name])) {
+            return $this->data[$name];
+        }
+
+        return null;
+    }
+
+    public function __isset($name)
+    {
+        return property_exists($this, $name) || isset($this->data[$name]);
+    }
+
+
     public function owner()
     {
         return User::getSilent($this);
@@ -70,7 +163,7 @@ class Profile
         try {
             return new Profile($login);
         } catch (UserNotFoundException $e) {
-            return false;
+            return null;
         }
     }
 }