Merge commit 'origin/fusionax' into account
[platal.git] / classes / profile.php
index 217a3f0..9c89e25 100644 (file)
@@ -23,30 +23,39 @@ class Profile
 {
     private $pid;
     private $hrpid;
+    private $data = array();
 
     private function __construct($login)
     {
         if ($login instanceof PlUser) {
-            $res = XDB::query('SELECT  p.pid, p.hrpid
-                                 FROM  account_profiles AS ap
-                           INNER JOIN  profiles AS p ON (p.pid = ap.pid)
-                                WHERE  ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)',
-                             $login->id());
+            $from  = 'account_profiles AS ap
+                INNER JOIN profiles AS p ON (p.pid = ap.pid)';
+            $where = XDB::format('ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)', $login->id());
         } else if (is_numeric($login)) {
-            $res = XDB::query('SELECT  p.pid, p.hrpid
-                                 FROM  profiles AS p
-                                WHERE  p.pid = {?}',
-                              $login);
+            $from = 'profiles AS p';
+            $where = XDB::format('p.pid = {?}', $login);
         } else {
-            $res = XDB::query('SELECT  p.pid, p.hrpid
-                                 FROM  profiles AS p
-                                WHERE  p.hrpid = {?}',
-                              $login);
+            $from = 'profiles AS p';
+            $where = XDB::format('p.hrpid = {?}', $login);
         }
+        // XXX: Temporary, use data from auth_user_md5 (waiting for data from newdirectory
+        $res = XDB::query('SELECT  p.*, u.prenom AS first_name,
+                                   IF(u.nom_usage != "", u.nom_usage, u.nom) AS last_name,
+                                   pd.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  ' . $from . '
+                       INNER JOIN  auth_user_md5 AS u ON (u.user_id = p.pid)
+                       INNER JOIN  profile_display AS pd ON (pd.pid = p.pid)
+                            WHERE  ' . $where);
         if ($res->numRows() != 1) {
             throw new UserNotFoundException();
         }
-        list($this->pid, $this->hrpid) = $res->fetchOneRow();
+        $this->data = $res->fetchOneAssoc();
+        $this->pid = $this->data['pid'];
+        $this->hrpid = $this->data['hrpid'];
     }
 
     public function id()
@@ -59,6 +68,84 @@ 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 (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 +157,13 @@ class Profile
         try {
             return new Profile($login);
         } catch (UserNotFoundException $e) {
-            return false;
+            /* Let say we can identify a profile using the identifiers of its owner.
+             */
+            $user = User::getSilent($login);
+            if ($user && $user->hasProfile()) {
+                return $user->profile();
+            }
+            return null;
         }
     }
 }