a848ca6791c8192f0b0416d4567358d8b13117e3
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_search AS pns_f ON (pns_f.pid = p.pid AND pns_f.typeid = ' . self
::getNameTypeId('Nom patronymique', true
) . ')
50 INNER JOIN profile_name_search AS pns_l ON (pns_l.pid = p.pid AND pns_l.typeid = ' . self
::getNameTypeId('Prénom', true
) . ')
51 LEFT JOIN profile_name_search AS pns_uf ON (pns_uf.pid = p.pid AND pns_uf.typeid = ' . self
::getNameTypeId('Prénom usuel', true
) . ')
52 LEFT JOIN profile_name_search AS pns_ul ON (pns_ul.pid = p.pid AND pns_ul.typeid = ' . self
::getNameTypeId('Nom usuel', true
) . ')
53 LEFT JOIN profile_name_search aS pns_n ON (pns_n.pid = p.pid AND pns_n.typeid = ' . self
::getNameTypeId('Surnom', true
) . ')
55 if ($res->numRows() != 1) {
56 throw new UserNotFoundException();
58 $this->data
= $res->fetchOneAssoc();
59 $this->pid
= $this->data
['pid'];
60 $this->hrpid
= $this->data
['hrpid'];
68 public function hrid()
73 public function promo()
78 /** Print a name with the given formatting:
86 public function name($format)
88 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
89 array($this->isFemale() ?
'•' : '',
90 $this->first_name
, $this->last_name
,
91 $this->full_name
, $this->short_name
,
92 $this->promo
), $format);
95 public function fullName($with_promo = false
)
98 return $this->full_name
. ' (' . $this->promo
. ')';
100 return $this->full_name
;
103 public function shortName($with_promo = false
)
106 return $this->short_name
. ' (' . $this->promo
. ')';
108 return $this->short_name
;
111 public function firstName()
113 return $this->first_name
;
116 public function lastName()
118 return $this->last_name
;
121 public function isFemale()
123 return $this->sex
== PlUser
::GENDER_FEMALE
;
126 public function data()
132 public function __get($name)
134 if (property_exists($this, $name)) {
138 if (isset($this->data
[$name])) {
139 return $this->data
[$name];
145 public function __isset($name)
147 return property_exists($this, $name) ||
isset($this->data
[$name]);
151 public function owner()
153 return User
::getSilent($this);
156 /** Return the profile associated with the given login.
158 public static function get($login)
161 return new Profile($login);
162 } catch (UserNotFoundException
$e) {
163 /* Let say we can identify a profile using the identifiers of its owner.
165 $user = User
::getSilent($login);
166 if ($user && $user->hasProfile()) {
167 return $user->profile();
173 public static function getNameTypeId($type, $for_sql = false
)
175 if (!S
::has('name_types')) {
176 $table = XDB
::fetchAllAssoc('name', 'SELECT id, name
177 FROM profile_name_search_enum');
178 S
::set('name_types', $table);
180 $table = S
::v('name_types');
183 return XDB
::escape($table[$type]);
185 return $table[$type];
190 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: