Kill calls to auth_user... in module profile.
[platal.git] / classes / profile.php
index 9c89e25..7af43e9 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2009 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -38,19 +38,23 @@ class Profile
             $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
+        $res = XDB::query('SELECT  p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
+                                   pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
+                                   IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual,
+                                   IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual,
+                                   pd.promo AS promo, pd.short_name, pd.directory_name 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);
+                       INNER JOIN  profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
+                       INNER JOIN  profile_name AS pn_f ON (pn_f.pid = p.pid AND pn_f.typeid = ' . self::getNameTypeId('lastname', true) . ')
+                       INNER JOIN  profile_name AS pn_l ON (pn_l.pid = p.pid AND pn_l.typeid = ' . self::getNameTypeId('firstname', true) . ')
+                        LEFT JOIN  profile_name AS pn_uf ON (pn_uf.pid = p.pid AND pn_uf.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
+                        LEFT JOIN  profile_name AS pn_ul ON (pn_ul.pid = p.pid AND pn_ul.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
+                        LEFT JOIN  profile_name aS pn_n ON (pn_n.pid = p.pid AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
+                            WHERE  ' . $where . '
+                         GROUP BY  p.pid');
         if ($res->numRows() != 1) {
+            __autoload('PlUser');
             throw new UserNotFoundException();
         }
         $this->data = $res->fetchOneAssoc();
@@ -108,12 +112,12 @@ class Profile
 
     public function firstName()
     {
-        return $this->first_name;
+        return $this->firstname;
     }
 
     public function lastName()
     {
-        return $this->last_name;
+        return $this->lastname;
     }
 
     public function isFemale()
@@ -153,19 +157,38 @@ class Profile
 
     /** Return the profile associated with the given login.
      */
-    public static function get($login) {
+    public static function get($login)
+    {
         try {
             return new Profile($login);
         } catch (UserNotFoundException $e) {
             /* Let say we can identify a profile using the identifiers of its owner.
              */
-            $user = User::getSilent($login);
-            if ($user && $user->hasProfile()) {
-                return $user->profile();
+            if (!($login instanceof PlUser)) {
+                $user = User::getSilent($login);
+                if ($user && $user->hasProfile()) {
+                    return $user->profile();
+                }
             }
             return null;
         }
     }
+
+    public static function getNameTypeId($type, $for_sql = false)
+    {
+        if (!S::has('name_types')) {
+            $table = XDB::fetchAllAssoc('type', 'SELECT  id, type
+                                                   FROM  profile_name_enum');
+            S::set('name_types', $table);
+        } else {
+            $table = S::v('name_types');
+        }
+        if ($for_sql) {
+            return XDB::escape($table[$type]);
+        } else {
+            return $table[$type];
+        }
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: