2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
26 private $data = array();
28 private function __construct($login)
30 if ($login instanceof PlUser
) {
31 $from = 'account_profiles AS ap
32 INNER JOIN profiles AS p ON (p.pid = ap.pid)';
33 $where = XDB
::format('ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)', $login->id());
34 } else if (is_numeric($login)) {
35 $from = 'profiles AS p';
36 $where = XDB
::format('p.pid = {?}', $login);
38 $from = 'profiles AS p';
39 $where = XDB
::format('p.hrpid = {?}', $login);
41 $res = XDB
::query('SELECT p.*, pe.entry_year, pe.grad_year,
42 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
43 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual,
44 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual,
45 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name
47 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
48 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
49 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid AND pn_f.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
50 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid AND pn_l.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
51 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid AND pn_uf.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
52 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid AND pn_ul.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
53 LEFT JOIN profile_name aS pn_n ON (pn_n.pid = p.pid AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
56 if ($res->numRows() != 1) {
58 throw new UserNotFoundException();
60 $this->data
= $res->fetchOneAssoc();
61 $this->pid
= $this->data
['pid'];
62 $this->hrpid
= $this->data
['hrpid'];
70 public function hrid()
75 public function promo()
80 /** Print a name with the given formatting:
88 public function name($format)
90 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
91 array($this->isFemale() ?
'•' : '',
92 $this->first_name
, $this->last_name
,
93 $this->full_name
, $this->short_name
,
94 $this->promo
), $format);
97 public function fullName($with_promo = false
)
100 return $this->full_name
. ' (' . $this->promo
. ')';
102 return $this->full_name
;
105 public function shortName($with_promo = false
)
108 return $this->short_name
. ' (' . $this->promo
. ')';
110 return $this->short_name
;
113 public function firstName()
115 return $this->firstname
;
118 public function lastName()
120 return $this->lastname
;
123 public function isFemale()
125 return $this->sex
== PlUser
::GENDER_FEMALE
;
128 public function data()
134 public function __get($name)
136 if (property_exists($this, $name)) {
140 if (isset($this->data
[$name])) {
141 return $this->data
[$name];
147 public function __isset($name)
149 return property_exists($this, $name) ||
isset($this->data
[$name]);
153 public function owner()
155 return User
::getSilent($this);
158 /** Return the profile associated with the given login.
160 public static function get($login)
163 return new Profile($login);
164 } catch (UserNotFoundException
$e) {
165 /* Let say we can identify a profile using the identifiers of its owner.
167 if (!($login instanceof PlUser
)) {
168 $user = User
::getSilent($login);
169 if ($user && $user->hasProfile()) {
170 return $user->profile();
177 public static function getNameTypeId($type, $for_sql = false
)
179 if (!S
::has('name_types')) {
180 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
181 FROM profile_name_enum');
182 S
::set('name_types', $table);
184 $table = S
::v('name_types');
187 return XDB
::escape($table[$type]);
189 return $table[$type];
194 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: