X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=classes%2Fprofile.php;h=7af43e9d060723a1e7ac35279363c3bc35c974d1;hb=c52d86d1d876e6def108e1d458604cc9714c252d;hp=130cd35649dc161b37fb92f20e2d4f8685588a70;hpb=384712208a260ae1b5b7e90ac95f0df84ffbe8af;p=platal.git diff --git a/classes/profile.php b/classes/profile.php index 130cd35..7af43e9 100644 --- a/classes/profile.php +++ b/classes/profile.php @@ -1,6 +1,6 @@ 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, pd.promo_display - FROM profiles AS p - INNER JOIN profile_display AS pd ON (pd.uid = p.pid) - WHERE p.pid = {?}', - $login); + $from = 'profiles AS p'; + $where = XDB::format('p.pid = {?}', $login); } else { - $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display - FROM profiles AS p - INNER JOIN profile_display AS pd ON (pd.uid = p.pid) - WHERE p.hrpid = {?}', - $login); + $from = 'profiles AS p'; + $where = XDB::format('p.hrpid = {?}', $login); } + $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 profile_display AS pd ON (pd.pid = p.pid) + 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(); } - list($this->pid, $this->hrpid, $this->promo) = $res->fetchOneRow(); + $this->data = $res->fetchOneAssoc(); + $this->pid = $this->data['pid']; + $this->hrpid = $this->data['hrpid']; } public function id() @@ -70,18 +77,66 @@ class Profile 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->firstname; + } + + public function lastName() + { + return $this->lastname; + } + + 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)) { - $this->data = XDB::fetchOneAssoc('SELECT * - FROM profiles - WHERE pid = {?}', - $this->id()); - } if (isset($this->data[$name])) { return $this->data[$name]; } @@ -102,13 +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. + */ + 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: