b4843fd87b8ee6ba572b5713f2eaee903f7c4cc7
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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 pns_f.name AS firstname, pns_l.name AS lastname, pns_n.name AS nickname,
43 IF(pns_uf.name IS NULL, pns_f.name, pns_uf.name) AS firstname_usual,
44 IF(pns_ul.name IS NULL, pns_l.name, pns_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 pns_f ON (pns_f.pid = p.pid AND pns_f.typeid = ' . self
::getNameTypeId('Nom patronymique', true
) . ')
50 INNER JOIN profile_name AS pns_l ON (pns_l.pid = p.pid AND pns_l.typeid = ' . self
::getNameTypeId('Prénom', true
) . ')
51 LEFT JOIN profile_name AS pns_uf ON (pns_uf.pid = p.pid AND pns_uf.typeid = ' . self
::getNameTypeId('Prénom usuel', true
) . ')
52 LEFT JOIN profile_name AS pns_ul ON (pns_ul.pid = p.pid AND pns_ul.typeid = ' . self
::getNameTypeId('Nom usuel', true
) . ')
53 LEFT JOIN profile_name aS pns_n ON (pns_n.pid = p.pid AND pns_n.typeid = ' . self
::getNameTypeId('Surnom', true
) . ')
55 if ($res->numRows() != 1) {
57 throw new UserNotFoundException();
59 $this->data
= $res->fetchOneAssoc();
60 $this->pid
= $this->data
['pid'];
61 $this->hrpid
= $this->data
['hrpid'];
69 public function hrid()
74 public function promo()
79 /** Print a name with the given formatting:
87 public function name($format)
89 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
90 array($this->isFemale() ?
'•' : '',
91 $this->first_name
, $this->last_name
,
92 $this->full_name
, $this->short_name
,
93 $this->promo
), $format);
96 public function fullName($with_promo = false
)
99 return $this->full_name
. ' (' . $this->promo
. ')';
101 return $this->full_name
;
104 public function shortName($with_promo = false
)
107 return $this->short_name
. ' (' . $this->promo
. ')';
109 return $this->short_name
;
112 public function firstName()
114 return $this->first_name
;
117 public function lastName()
119 return $this->last_name
;
122 public function isFemale()
124 return $this->sex
== PlUser
::GENDER_FEMALE
;
127 public function data()
133 public function __get($name)
135 if (property_exists($this, $name)) {
139 if (isset($this->data
[$name])) {
140 return $this->data
[$name];
146 public function __isset($name)
148 return property_exists($this, $name) ||
isset($this->data
[$name]);
152 public function owner()
154 return User
::getSilent($this);
157 /** Return the profile associated with the given login.
159 public static function get($login)
162 return new Profile($login);
163 } catch (UserNotFoundException
$e) {
164 /* Let say we can identify a profile using the identifiers of its owner.
166 if (!($login instanceof PlUser
)) {
167 $user = User
::getSilent($login);
168 if ($user && $user->hasProfile()) {
169 return $user->profile();
176 public static function getNameTypeId($type, $for_sql = false
)
178 if (!S
::has('name_types')) {
179 $table = XDB
::fetchAllAssoc('name', 'SELECT id, name
180 FROM profile_name_enum');
181 S
::set('name_types', $table);
183 $table = S
::v('name_types');
186 return XDB
::escape($table[$type]);
188 return $table[$type];
193 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: