Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
34ade5a6 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
e7b93962 FB |
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 | { | |
f5642983 FB |
24 | static private $v_values = array('public' => array('public'), |
25 | 'ax' => array('ax', 'public'), | |
26 | 'private' => array('private', 'ax', 'public')); | |
27 | const VISIBILITY_PUBLIC = 'public'; | |
28 | const VISIBILITY_AX = 'ax'; | |
29 | const VISIBILITY_PRIVATE = 'private'; | |
30 | ||
31 | const ADDRESS_MAIN = 0x000001; | |
32 | const ADDRESS_PERSO = 0x000002; | |
33 | const ADDRESS_PRO = 0x000004; | |
34 | const ADDRESS_ALL = 0x000006; | |
35 | const ADDRESS_POSTAL = 0x000008; | |
36 | ||
37 | const EDUCATION_MAIN = 0x000010; | |
38 | const EDUCATION_ALL = 0x000020; | |
39 | const EDUCATION_FINISHED = 0x000040; | |
40 | const EDUCATION_CURRENT = 0x000080; | |
41 | ||
42 | const JOBS_MAIN = 0x000100; | |
43 | const JOBS_ALL = 0x000200; | |
44 | const JOBS_FINISHED = 0x000400; | |
45 | const JOBS_CURRENT = 0x000800; | |
46 | ||
e7b93962 FB |
47 | private $pid; |
48 | private $hrpid; | |
3e53a496 FB |
49 | private $data = array(); |
50 | ||
f5642983 FB |
51 | private $visibility = null; |
52 | ||
b774ddab | 53 | private function __construct(array $data) |
e7b93962 | 54 | { |
b774ddab | 55 | $this->data = $data; |
832e6fcb FB |
56 | $this->pid = $this->data['pid']; |
57 | $this->hrpid = $this->data['hrpid']; | |
e7b93962 FB |
58 | } |
59 | ||
60 | public function id() | |
61 | { | |
62 | return $this->pid; | |
63 | } | |
64 | ||
65 | public function hrid() | |
66 | { | |
67 | return $this->hrpid; | |
68 | } | |
69 | ||
d5e60905 FB |
70 | public function promo() |
71 | { | |
72 | return $this->promo; | |
73 | } | |
74 | ||
94b72319 FB |
75 | /** Print a name with the given formatting: |
76 | * %s = • for women | |
77 | * %f = firstname | |
78 | * %l = lastname | |
79 | * %F = fullname | |
80 | * %S = shortname | |
81 | * %p = promo | |
82 | */ | |
83 | public function name($format) | |
84 | { | |
85 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
86 | array($this->isFemale() ? '•' : '', | |
87 | $this->first_name, $this->last_name, | |
88 | $this->full_name, $this->short_name, | |
89 | $this->promo), $format); | |
90 | } | |
91 | ||
92 | public function fullName($with_promo = false) | |
93 | { | |
94 | if ($with_promo) { | |
95 | return $this->full_name . ' (' . $this->promo . ')'; | |
96 | } | |
97 | return $this->full_name; | |
98 | } | |
99 | ||
100 | public function shortName($with_promo = false) | |
101 | { | |
102 | if ($with_promo) { | |
103 | return $this->short_name . ' (' . $this->promo . ')'; | |
104 | } | |
105 | return $this->short_name; | |
106 | } | |
107 | ||
108 | public function firstName() | |
109 | { | |
08c91036 | 110 | return $this->firstname; |
94b72319 FB |
111 | } |
112 | ||
113 | public function lastName() | |
114 | { | |
08c91036 | 115 | return $this->lastname; |
94b72319 FB |
116 | } |
117 | ||
118 | public function isFemale() | |
119 | { | |
120 | return $this->sex == PlUser::GENDER_FEMALE; | |
121 | } | |
122 | ||
123 | public function data() | |
124 | { | |
125 | $this->first_name; | |
126 | return $this->data; | |
127 | } | |
128 | ||
3e53a496 FB |
129 | public function __get($name) |
130 | { | |
131 | if (property_exists($this, $name)) { | |
132 | return $this->$name; | |
133 | } | |
134 | ||
3e53a496 FB |
135 | if (isset($this->data[$name])) { |
136 | return $this->data[$name]; | |
137 | } | |
138 | ||
139 | return null; | |
140 | } | |
141 | ||
142 | public function __isset($name) | |
143 | { | |
144 | return property_exists($this, $name) || isset($this->data[$name]); | |
145 | } | |
146 | ||
f5642983 FB |
147 | public function setVisibilityLevel($visibility) |
148 | { | |
149 | if ($visibility != self::VISIBILITY_PRIVATE | |
150 | && $visibility != self::VISIBILITY_AX | |
151 | && $visibility != self::VISIBILITY_PUBLIC) { | |
152 | Platal::page()->kill("Visibility invalide: " . $visibility); | |
153 | } | |
154 | $this->visibility = self::$v_values[$visibility]; | |
155 | } | |
156 | ||
157 | public function getAddresses($flags) | |
158 | { | |
159 | $where = XDB::format('pa.pid = {?}', $this->id()); | |
160 | if ($flags & self::ADDRESS_MAIN) { | |
161 | $where .= ' AND FIND_IN_SET(\'current\', pa.flags)'; | |
162 | } | |
163 | if ($flags & self::ADDRESS_POSTAL) { | |
164 | $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)'; | |
165 | } | |
166 | if ($this->visibility) { | |
167 | $where .= ' AND pa.pub IN ' . XDB::formatArray($this->visibility); | |
168 | } | |
169 | $type = array(); | |
170 | if ($flags & self::ADDRESS_PRO) { | |
171 | $type[] = 'job'; | |
172 | } | |
173 | if ($flags & self::ADDRESS_PERSO) { | |
174 | $type[] = 'home'; | |
175 | } | |
176 | if (count($type) > 0) { | |
177 | $where .= ' AND pa.type IN ' . XDB::formatArray($type); | |
178 | } | |
179 | return XDB::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude, | |
180 | gl.name AS locality, gas.name AS subAdministrativeArea, | |
181 | ga.name AS administrativeArea, gc.countryFR AS country, | |
182 | FIND_IN_SET(\'current\', pa.flags) AS current, | |
183 | FIND_IN_SET(\'temporary\', pa.flags) AS temporary, | |
184 | FIND_IN_SET(\'secondary\', pa.flags) AS secondary, | |
185 | FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type | |
186 | FROM profile_addresses AS pa | |
187 | LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) | |
188 | LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) | |
189 | LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId) | |
190 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId) | |
191 | WHERE ' . $where); | |
192 | } | |
193 | ||
194 | public function getMainAddress() | |
195 | { | |
196 | $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN); | |
197 | if ($it->total() == 0) { | |
198 | return null; | |
199 | } else { | |
200 | return $it->next(); | |
201 | } | |
202 | } | |
3e53a496 | 203 | |
e7b93962 FB |
204 | public function owner() |
205 | { | |
206 | return User::getSilent($this); | |
207 | } | |
208 | ||
b774ddab FB |
209 | private static function fetchProfileData(array $pids) |
210 | { | |
211 | if (count($pids) == 0) { | |
212 | return array(); | |
213 | } | |
214 | return XDB::fetchAllAssoc('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, | |
215 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, | |
216 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual, | |
217 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual, | |
218 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name | |
219 | FROM profiles AS p | |
220 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
221 | INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
222 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid | |
223 | AND pn_f.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
224 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
225 | AND pn_l.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
226 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
227 | AND pn_uf.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
228 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
229 | AND pn_ul.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
230 | LEFT JOIN profile_name aS pn_n ON (pn_n.pid = p.pid | |
231 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
232 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' | |
233 | GROUP BY p.pid'); | |
234 | } | |
235 | ||
236 | public static function getPID($login) | |
237 | { | |
238 | if ($login instanceof PlUser) { | |
239 | return XDB::fetchOneCell('SELECT pid | |
240 | FROM account_profiles | |
241 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
242 | $login->id()); | |
243 | } else if (ctype_digit($login)) { | |
244 | return XDB::fetchOneCell('SELECT pid | |
245 | FROM profiles | |
246 | WHERE pid = {?}', $login); | |
247 | } else { | |
248 | return XDB::fetchOneCell('SELECT pid | |
249 | FROM profiles | |
250 | WHERE hrpid = {?}', $login); | |
251 | } | |
252 | } | |
253 | ||
254 | ||
e7b93962 FB |
255 | /** Return the profile associated with the given login. |
256 | */ | |
a3118782 FB |
257 | public static function get($login) |
258 | { | |
b774ddab FB |
259 | $pid = self::getPID($login); |
260 | if (!is_null($pid)) { | |
261 | $data = self::fetchProfileData(array($pid)); | |
262 | return new Profile(array_pop($data)); | |
263 | } else { | |
efe597c5 FB |
264 | /* Let say we can identify a profile using the identifiers of its owner. |
265 | */ | |
455ea0c9 FB |
266 | if (!($login instanceof PlUser)) { |
267 | $user = User::getSilent($login); | |
268 | if ($user && $user->hasProfile()) { | |
269 | return $user->profile(); | |
270 | } | |
efe597c5 | 271 | } |
3e53a496 | 272 | return null; |
e7b93962 FB |
273 | } |
274 | } | |
a3118782 | 275 | |
b774ddab FB |
276 | /** Return profiles for the list of pids. |
277 | */ | |
278 | public static function getBulkProfilesWithPIDs(array $pids) | |
279 | { | |
280 | if (count($pids) == 0) { | |
281 | return array(); | |
282 | } | |
283 | $data = self::fetchProfileData($pids); | |
284 | $inv = array_flip($pids); | |
285 | $profiles = array(); | |
286 | foreach ($data AS $p) { | |
287 | $p = new Profile($p); | |
288 | $key = $inv[$p->id()]; | |
289 | $profiles[$key] = $p; | |
290 | } | |
291 | return $profiles; | |
292 | } | |
293 | ||
294 | /** Return profiles for uids. | |
295 | */ | |
296 | public static function getBulkProfilesWithUIDS(array $uids) | |
297 | { | |
298 | if (count($uids) == 0) { | |
299 | return array(); | |
300 | } | |
301 | $table = XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
302 | FROM account_profiles AS ap | |
303 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
304 | AND ap.uid IN ' . XDB::formatArray($uids)); | |
305 | return self::getBulkProfilesWithPIDs($table); | |
306 | } | |
307 | ||
a3118782 FB |
308 | public static function getNameTypeId($type, $for_sql = false) |
309 | { | |
310 | if (!S::has('name_types')) { | |
eb6207f7 | 311 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type |
32742f84 | 312 | FROM profile_name_enum'); |
a3118782 FB |
313 | S::set('name_types', $table); |
314 | } else { | |
315 | $table = S::v('name_types'); | |
316 | } | |
317 | if ($for_sql) { | |
318 | return XDB::escape($table[$type]); | |
319 | } else { | |
320 | return $table[$type]; | |
321 | } | |
322 | } | |
e7b93962 FB |
323 | } |
324 | ||
325 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
326 | ?> |