<?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 *
{
private $pid;
private $hrpid;
- private $promo;
-
private $data = array();
private function __construct($login)
{
if ($login instanceof PlUser) {
- $res = XDB::query('SELECT p.pid, p.hrpid, pd.promo_display
- FROM account_profiles AS ap
- INNER JOIN profiles AS p ON (p.pid = ap.pid)
- INNER JOIN profile_display AS pd ON (pd.uid = p.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, 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.*, 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()
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];
}
/** 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: