Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2008 Polytechnique.org * | |
4 | * http://opensource.polytechnique.org/ * | |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | class Profile | |
23 | { | |
24 | private $pid; | |
25 | private $hrpid; | |
3e53a496 FB |
26 | private $data = array(); |
27 | ||
e7b93962 FB |
28 | private function __construct($login) |
29 | { | |
30 | if ($login instanceof PlUser) { | |
94b72319 | 31 | $res = XDB::query('SELECT p.pid, p.hrpid |
e7b93962 FB |
32 | FROM account_profiles AS ap |
33 | INNER JOIN profiles AS p ON (p.pid = ap.pid) | |
34 | WHERE ap.uid = {?} AND FIND_IN_SET(\'owner\', ap.perms)', | |
35 | $login->id()); | |
36 | } else if (is_numeric($login)) { | |
94b72319 | 37 | $res = XDB::query('SELECT p.pid, p.hrpid |
e7b93962 FB |
38 | FROM profiles AS p |
39 | WHERE p.pid = {?}', | |
40 | $login); | |
41 | } else { | |
94b72319 | 42 | $res = XDB::query('SELECT p.pid, p.hrpid |
e7b93962 FB |
43 | FROM profiles AS p |
44 | WHERE p.hrpid = {?}', | |
45 | $login); | |
46 | } | |
47 | if ($res->numRows() != 1) { | |
48 | throw new UserNotFoundException(); | |
49 | } | |
94b72319 | 50 | list($this->pid, $this->hrpid) = $res->fetchOneRow(); |
e7b93962 FB |
51 | } |
52 | ||
53 | public function id() | |
54 | { | |
55 | return $this->pid; | |
56 | } | |
57 | ||
58 | public function hrid() | |
59 | { | |
60 | return $this->hrpid; | |
61 | } | |
62 | ||
d5e60905 FB |
63 | public function promo() |
64 | { | |
65 | return $this->promo; | |
66 | } | |
67 | ||
94b72319 FB |
68 | /** Print a name with the given formatting: |
69 | * %s = • for women | |
70 | * %f = firstname | |
71 | * %l = lastname | |
72 | * %F = fullname | |
73 | * %S = shortname | |
74 | * %p = promo | |
75 | */ | |
76 | public function name($format) | |
77 | { | |
78 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
79 | array($this->isFemale() ? '•' : '', | |
80 | $this->first_name, $this->last_name, | |
81 | $this->full_name, $this->short_name, | |
82 | $this->promo), $format); | |
83 | } | |
84 | ||
85 | public function fullName($with_promo = false) | |
86 | { | |
87 | if ($with_promo) { | |
88 | return $this->full_name . ' (' . $this->promo . ')'; | |
89 | } | |
90 | return $this->full_name; | |
91 | } | |
92 | ||
93 | public function shortName($with_promo = false) | |
94 | { | |
95 | if ($with_promo) { | |
96 | return $this->short_name . ' (' . $this->promo . ')'; | |
97 | } | |
98 | return $this->short_name; | |
99 | } | |
100 | ||
101 | public function firstName() | |
102 | { | |
103 | return $this->first_name; | |
104 | } | |
105 | ||
106 | public function lastName() | |
107 | { | |
108 | return $this->last_name; | |
109 | } | |
110 | ||
111 | public function isFemale() | |
112 | { | |
113 | return $this->sex == PlUser::GENDER_FEMALE; | |
114 | } | |
115 | ||
116 | public function data() | |
117 | { | |
118 | $this->first_name; | |
119 | return $this->data; | |
120 | } | |
121 | ||
3e53a496 FB |
122 | public function __get($name) |
123 | { | |
124 | if (property_exists($this, $name)) { | |
125 | return $this->$name; | |
126 | } | |
127 | ||
128 | if (empty($this->data)) { | |
94b72319 FB |
129 | // XXX: Temporary, use data from auth_user_md5 (waiting for data from newdirectory |
130 | $this->data = XDB::fetchOneAssoc('SELECT p.*, u.prenom AS first_name, | |
131 | IF(u.nom_usage != "", u.nom_usage, u.nom) AS last_name, | |
132 | u.promo AS promo, | |
133 | CONCAT(u.prenom, " ", u.nom) AS short_name, | |
134 | IF(u.nom_usage != "", | |
135 | CONCAT(u.nom_usage, " (", u.nom, "),", u.prenom), | |
136 | CONCAT(u.nom, ", ", u.prenom)) AS full_name | |
137 | FROM profiles AS p | |
138 | INNER JOIN auth_user_md5 AS u ON (u.user_id = p.pid) | |
139 | WHERE p.pid = {?}', | |
3e53a496 FB |
140 | $this->id()); |
141 | } | |
142 | if (isset($this->data[$name])) { | |
143 | return $this->data[$name]; | |
144 | } | |
145 | ||
146 | return null; | |
147 | } | |
148 | ||
149 | public function __isset($name) | |
150 | { | |
151 | return property_exists($this, $name) || isset($this->data[$name]); | |
152 | } | |
153 | ||
154 | ||
e7b93962 FB |
155 | public function owner() |
156 | { | |
157 | return User::getSilent($this); | |
158 | } | |
159 | ||
160 | /** Return the profile associated with the given login. | |
161 | */ | |
162 | public static function get($login) { | |
163 | try { | |
164 | return new Profile($login); | |
165 | } catch (UserNotFoundException $e) { | |
efe597c5 FB |
166 | /* Let say we can identify a profile using the identifiers of its owner. |
167 | */ | |
168 | $user = User::getSilent($login); | |
169 | if ($user && $user->hasProfile()) { | |
170 | return $user->profile(); | |
171 | } | |
3e53a496 | 172 | return null; |
e7b93962 FB |
173 | } |
174 | } | |
175 | } | |
176 | ||
177 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
178 | ?> |