Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
d4c08d89 | 3 | * Copyright (C) 2003-2010 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')); | |
913a4e90 | 27 | |
f5642983 FB |
28 | const VISIBILITY_PUBLIC = 'public'; |
29 | const VISIBILITY_AX = 'ax'; | |
30 | const VISIBILITY_PRIVATE = 'private'; | |
31 | ||
913a4e90 RB |
32 | /* name tokens */ |
33 | const LASTNAME = 'lastname'; | |
34 | const FIRSTNAME = 'firstname'; | |
35 | const NICKNAME = 'nickname'; | |
36 | const PSEUDONYM = 'pseudonym'; | |
37 | const NAME = 'name'; | |
38 | /* name variants */ | |
39 | const VN_MARITAL = 'marital'; | |
40 | const VN_ORDINARY = 'ordinary'; | |
41 | const VN_OTHER = 'other'; | |
42 | const VN_INI = 'ini'; | |
43 | /* display names */ | |
44 | const DN_FULL = 'directory_name'; | |
45 | const DN_DISPLAY = 'yourself'; | |
46 | const DN_YOURSELF = 'yourself'; | |
47 | const DN_DIRECTORY = 'directory_name'; | |
48 | const DN_PRIVATE = 'private_name'; | |
49 | const DN_PUBLIC = 'public_name'; | |
50 | const DN_SHORT = 'short_name'; | |
51 | const DN_SORT = 'sort_name'; | |
e02d9fbb SJ |
52 | /* education related names */ |
53 | const EDU_X = 'École polytechnique'; | |
54 | const DEGREE_X = 'Ingénieur'; | |
55 | const DEGREE_M = 'Master'; | |
56 | const DEGREE_D = 'Doctorat'; | |
913a4e90 RB |
57 | |
58 | static public $name_variants = array( | |
59 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
60 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) | |
61 | ); | |
62 | ||
f5642983 FB |
63 | const ADDRESS_MAIN = 0x000001; |
64 | const ADDRESS_PERSO = 0x000002; | |
65 | const ADDRESS_PRO = 0x000004; | |
66 | const ADDRESS_ALL = 0x000006; | |
67 | const ADDRESS_POSTAL = 0x000008; | |
68 | ||
69 | const EDUCATION_MAIN = 0x000010; | |
4bc5b8f0 FB |
70 | const EDUCATION_EXTRA = 0x000020; |
71 | const EDUCATION_ALL = 0x000040; | |
72 | const EDUCATION_FINISHED = 0x000080; | |
73 | const EDUCATION_CURRENT = 0x000100; | |
f5642983 | 74 | |
4bc5b8f0 FB |
75 | const JOBS_MAIN = 0x001000; |
76 | const JOBS_ALL = 0x002000; | |
77 | const JOBS_FINISHED = 0x004000; | |
78 | const JOBS_CURRENT = 0x008000; | |
f5642983 | 79 | |
04a94b1d FB |
80 | const NETWORKING_ALL = 0x000000; |
81 | const NETWORKING_WEB = 0x010000; | |
82 | const NETWORKING_IM = 0x020000; | |
83 | const NETWORKING_SOCIAL = 0x040000; | |
84 | ||
e7b93962 FB |
85 | private $pid; |
86 | private $hrpid; | |
3e53a496 FB |
87 | private $data = array(); |
88 | ||
f5642983 FB |
89 | private $visibility = null; |
90 | ||
b774ddab | 91 | private function __construct(array $data) |
e7b93962 | 92 | { |
b774ddab | 93 | $this->data = $data; |
832e6fcb FB |
94 | $this->pid = $this->data['pid']; |
95 | $this->hrpid = $this->data['hrpid']; | |
f74fb084 FB |
96 | if (!S::logged()) { |
97 | $this->setVisibilityLevel(self::VISIBILITY_PUBLIC); | |
98 | } | |
e7b93962 FB |
99 | } |
100 | ||
101 | public function id() | |
102 | { | |
103 | return $this->pid; | |
104 | } | |
105 | ||
106 | public function hrid() | |
107 | { | |
108 | return $this->hrpid; | |
109 | } | |
110 | ||
d5e60905 FB |
111 | public function promo() |
112 | { | |
113 | return $this->promo; | |
114 | } | |
115 | ||
94b72319 FB |
116 | /** Print a name with the given formatting: |
117 | * %s = • for women | |
118 | * %f = firstname | |
119 | * %l = lastname | |
120 | * %F = fullname | |
121 | * %S = shortname | |
122 | * %p = promo | |
123 | */ | |
124 | public function name($format) | |
125 | { | |
126 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
127 | array($this->isFemale() ? '•' : '', | |
128 | $this->first_name, $this->last_name, | |
129 | $this->full_name, $this->short_name, | |
130 | $this->promo), $format); | |
131 | } | |
132 | ||
133 | public function fullName($with_promo = false) | |
134 | { | |
135 | if ($with_promo) { | |
136 | return $this->full_name . ' (' . $this->promo . ')'; | |
137 | } | |
138 | return $this->full_name; | |
139 | } | |
140 | ||
141 | public function shortName($with_promo = false) | |
142 | { | |
143 | if ($with_promo) { | |
144 | return $this->short_name . ' (' . $this->promo . ')'; | |
145 | } | |
146 | return $this->short_name; | |
147 | } | |
148 | ||
149 | public function firstName() | |
150 | { | |
08c91036 | 151 | return $this->firstname; |
94b72319 FB |
152 | } |
153 | ||
5c005b37 RB |
154 | public function firstNames() |
155 | { | |
156 | return $this->nameVariants(self::FIRSTNAME); | |
157 | } | |
158 | ||
94b72319 FB |
159 | public function lastName() |
160 | { | |
08c91036 | 161 | return $this->lastname; |
94b72319 FB |
162 | } |
163 | ||
5c005b37 RB |
164 | public function lastNames() |
165 | { | |
166 | return $this->nameVariants(self::LASTNAME); | |
167 | } | |
168 | ||
94b72319 FB |
169 | public function isFemale() |
170 | { | |
171 | return $this->sex == PlUser::GENDER_FEMALE; | |
172 | } | |
173 | ||
174 | public function data() | |
175 | { | |
176 | $this->first_name; | |
177 | return $this->data; | |
178 | } | |
179 | ||
5c005b37 RB |
180 | private function nameVariants($type) |
181 | { | |
182 | $vals = array($this->$type); | |
183 | foreach (self::$name_variants[$type] as $var) { | |
184 | $vartype = $type . '_' . $var; | |
185 | $varname = $this->$vartype; | |
186 | if ($varname != null && $varname != "") { | |
187 | $vals[] = $varname; | |
188 | } | |
189 | } | |
190 | return array_unique($vals); | |
191 | } | |
192 | ||
3e53a496 FB |
193 | public function __get($name) |
194 | { | |
195 | if (property_exists($this, $name)) { | |
196 | return $this->$name; | |
197 | } | |
198 | ||
3e53a496 FB |
199 | if (isset($this->data[$name])) { |
200 | return $this->data[$name]; | |
201 | } | |
202 | ||
203 | return null; | |
204 | } | |
205 | ||
206 | public function __isset($name) | |
207 | { | |
208 | return property_exists($this, $name) || isset($this->data[$name]); | |
209 | } | |
210 | ||
f5642983 FB |
211 | public function setVisibilityLevel($visibility) |
212 | { | |
0d906109 RB |
213 | if ($visibility != self::VISIBILITY_PRIVATE |
214 | && $visibility != self::VISIBILITY_AX | |
f5642983 FB |
215 | && $visibility != self::VISIBILITY_PUBLIC) { |
216 | Platal::page()->kill("Visibility invalide: " . $visibility); | |
217 | } | |
218 | $this->visibility = self::$v_values[$visibility]; | |
5c005b37 | 219 | if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) { |
46e9eb99 FB |
220 | unset($this->data['mobile']); |
221 | } | |
f5642983 FB |
222 | } |
223 | ||
4bc5b8f0 | 224 | |
833a6e86 FB |
225 | /* Photo |
226 | */ | |
227 | public function getPhoto($fallback = true) | |
228 | { | |
229 | /* TODO: migrate photo table to profile_photo, change uid to pid | |
230 | */ | |
231 | $cond = ''; | |
232 | if ($this->visibility) { | |
233 | $cond = ' AND pub IN ' . XDB::formatArray($this->visibility); | |
234 | } | |
235 | $res = XDB::query('SELECT * | |
5c4ea53f | 236 | FROM profile_photos |
833a6e86 | 237 | WHERE attachmime IN (\'jpeg\', \'png\') |
5c4ea53f | 238 | ' . $cond . ' AND pid = {?}', |
833a6e86 FB |
239 | $this->id()); |
240 | if ($res->numRows() > 0) { | |
241 | $photo = $res->fetchOneAssoc(); | |
242 | return PlImage::fromData($photo['attach'], 'image/' . $photo['attachmime'], | |
243 | $photo['x'], $photo['y']); | |
244 | } else if ($fallback) { | |
245 | return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png', | |
246 | 'image/png'); | |
247 | } | |
248 | return null; | |
249 | } | |
250 | ||
4bc5b8f0 FB |
251 | /* Addresses |
252 | */ | |
253 | public function getAddresses($flags, $limit = null) | |
f5642983 FB |
254 | { |
255 | $where = XDB::format('pa.pid = {?}', $this->id()); | |
256 | if ($flags & self::ADDRESS_MAIN) { | |
257 | $where .= ' AND FIND_IN_SET(\'current\', pa.flags)'; | |
258 | } | |
259 | if ($flags & self::ADDRESS_POSTAL) { | |
260 | $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)'; | |
261 | } | |
262 | if ($this->visibility) { | |
263 | $where .= ' AND pa.pub IN ' . XDB::formatArray($this->visibility); | |
264 | } | |
265 | $type = array(); | |
266 | if ($flags & self::ADDRESS_PRO) { | |
267 | $type[] = 'job'; | |
268 | } | |
269 | if ($flags & self::ADDRESS_PERSO) { | |
270 | $type[] = 'home'; | |
271 | } | |
272 | if (count($type) > 0) { | |
273 | $where .= ' AND pa.type IN ' . XDB::formatArray($type); | |
274 | } | |
4bc5b8f0 | 275 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); |
f5642983 FB |
276 | return XDB::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude, |
277 | gl.name AS locality, gas.name AS subAdministrativeArea, | |
278 | ga.name AS administrativeArea, gc.countryFR AS country, | |
6b83ce06 | 279 | ppfix.display_tel AS fixed_tel, ppfax.display_tel AS fax_tel, |
f5642983 FB |
280 | FIND_IN_SET(\'current\', pa.flags) AS current, |
281 | FIND_IN_SET(\'temporary\', pa.flags) AS temporary, | |
282 | FIND_IN_SET(\'secondary\', pa.flags) AS secondary, | |
283 | FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type | |
284 | FROM profile_addresses AS pa | |
285 | LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId) | |
286 | LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId) | |
287 | LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId) | |
288 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId) | |
6b83ce06 RB |
289 | LEFT JOIN profile_phones AS ppfix ON (ppfix.link_type = \'address\' AND ppfix.uid = pa.pid AND ppfix.link_id = pa.id AND ppfix.tel_type = \'fixed\') |
290 | LEFT JOIN profile_phones AS ppfax ON (ppfax.link_type = \'address\' AND ppfax.uid = pa.pid AND ppfax.link_id = pa.id AND ppfax.tel_type = \'fax\') | |
4bc5b8f0 FB |
291 | WHERE ' . $where . ' |
292 | ORDER BY pa.id | |
293 | ' . $limit); | |
f5642983 FB |
294 | } |
295 | ||
296 | public function getMainAddress() | |
297 | { | |
298 | $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN); | |
299 | if ($it->total() == 0) { | |
300 | return null; | |
301 | } else { | |
302 | return $it->next(); | |
303 | } | |
304 | } | |
3e53a496 | 305 | |
4bc5b8f0 FB |
306 | |
307 | /* Educations | |
308 | */ | |
309 | public function getEducations($flags, $limit = null) | |
310 | { | |
311 | $where = XDB::format('pe.uid = {?}', $this->id()); | |
312 | if ($flags & self::EDUCATION_MAIN) { | |
313 | $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)'; | |
314 | } else if ($flags & self::EDUCATION_EXTRA) { | |
315 | $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)'; | |
316 | } else if ($flags & self::EDUCATION_FINISHED) { | |
317 | $where .= ' AND pe.grad_year <= YEAR(CURDATE())'; | |
318 | } else if ($flags & self::EDUCATION_CURRENT) { | |
319 | $where .= ' AND pe.grad_year > YEAR(CURDATE())'; | |
320 | } | |
321 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
322 | return XDB::iterator('SELECT pe.entry_year, pe.grad_year, pe.program, | |
323 | pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country, | |
324 | pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field, | |
325 | FIND_IN_SET(\'primary\', pe.flags) AS prim | |
326 | FROM profile_education AS pe | |
327 | INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id) | |
328 | LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country) | |
329 | INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id) | |
330 | LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id) | |
331 | WHERE ' . $where . ' | |
332 | ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id | |
333 | ' . $limit); | |
334 | } | |
335 | ||
336 | public function getExtraEducations($limit = null) | |
337 | { | |
338 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); | |
339 | } | |
340 | ||
341 | ||
04a94b1d FB |
342 | /** Networking |
343 | */ | |
344 | ||
345 | public function getNetworking($flags, $limit = null) | |
346 | { | |
347 | $where = XDB::format('pn.uid = {?}', $this->id()); | |
348 | if ($flags & self::NETWORKING_WEB) { | |
349 | $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index | |
350 | } | |
351 | if ($this->visibility) { | |
352 | $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility); | |
353 | } | |
354 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
355 | return XDB::iterator('SELECT pne.name, pne.icon, | |
356 | IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address), | |
357 | pn.address) AS address | |
358 | FROM profile_networking AS pn | |
359 | INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type) | |
360 | WHERE ' . $where . ' | |
361 | ORDER BY pn.network_type, pn.nwid | |
362 | ' . $limit); | |
363 | } | |
364 | ||
365 | public function getWebSite() | |
366 | { | |
367 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); | |
368 | if ($site->total() != 1) { | |
369 | return null; | |
370 | } | |
371 | $site = $site->next(); | |
372 | return $site['address']; | |
373 | } | |
374 | ||
375 | ||
e718bd18 FB |
376 | /** Jobs |
377 | */ | |
378 | ||
379 | public function getJobs($flags, $limit = null) | |
380 | { | |
381 | $where = XDB::format('pj.uid = {?}', $this->id()); | |
382 | $cond = 'TRUE'; | |
383 | if ($this->visibility) { | |
384 | $where .= ' AND pj.pub IN ' . XDB::formatArray($this->visibility); | |
385 | $cond = 'pj.email_pub IN ' . XDB::formatArray($this->visibility); | |
386 | } | |
387 | $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit); | |
388 | return XDB::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code, | |
389 | pj.description, pj.url AS user_site, | |
390 | IF (' . $cond . ', pj.email, NULL) AS user_email, | |
83ff5281 SJ |
391 | pjse.name AS sector, pjsse.name AS subsector, |
392 | pjssse.name AS subsubsector | |
e718bd18 FB |
393 | FROM profile_job AS pj |
394 | INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid) | |
e718bd18 FB |
395 | LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid) |
396 | LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid) | |
397 | LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid) | |
398 | WHERE ' . $where . ' | |
399 | ORDER BY pj.id | |
400 | ' . $limit); | |
401 | } | |
402 | ||
403 | public function getMailJob() | |
404 | { | |
405 | $job = $this->getJobs(self::JOBS_MAIN, 1); | |
406 | if ($job->total() != 1) { | |
407 | return null; | |
408 | } | |
409 | return $job->next(); | |
410 | } | |
411 | ||
5c005b37 RB |
412 | /* Binets |
413 | */ | |
414 | public function getBinets() | |
415 | { | |
416 | return XDB::fetchColumn('SELECT binet_id | |
5c8a71f2 | 417 | FROM profile_binets |
5c005b37 RB |
418 | WHERE user_id = {?}', $this->id()); |
419 | } | |
420 | ||
e718bd18 | 421 | |
e7b93962 FB |
422 | public function owner() |
423 | { | |
424 | return User::getSilent($this); | |
425 | } | |
426 | ||
0d906109 | 427 | private static function fetchProfileData(array $pids, $respect_order = true) |
b774ddab FB |
428 | { |
429 | if (count($pids) == 0) { | |
430 | return array(); | |
431 | } | |
0d906109 RB |
432 | |
433 | if ($respect_order) { | |
434 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); | |
435 | } else { | |
436 | $order = ''; | |
437 | } | |
438 | ||
439 | return XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, | |
440 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, | |
441 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, | |
442 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, | |
443 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name, | |
444 | pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub, | |
88c31faf FB |
445 | ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub, |
446 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, | |
447 | ap.uid AS owner_id | |
0d906109 RB |
448 | FROM profiles AS p |
449 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
450 | INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
451 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid | |
452 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
453 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
454 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
455 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
456 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
457 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
458 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
459 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid | |
460 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
461 | LEFT JOIN profile_phones AS pp ON (pp.uid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') | |
5c4ea53f | 462 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) |
0d906109 RB |
463 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
464 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' | |
465 | GROUP BY p.pid | |
466 | ' . $order); | |
b774ddab FB |
467 | } |
468 | ||
469 | public static function getPID($login) | |
470 | { | |
471 | if ($login instanceof PlUser) { | |
472 | return XDB::fetchOneCell('SELECT pid | |
473 | FROM account_profiles | |
474 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
475 | $login->id()); | |
476 | } else if (ctype_digit($login)) { | |
477 | return XDB::fetchOneCell('SELECT pid | |
478 | FROM profiles | |
479 | WHERE pid = {?}', $login); | |
480 | } else { | |
481 | return XDB::fetchOneCell('SELECT pid | |
482 | FROM profiles | |
483 | WHERE hrpid = {?}', $login); | |
484 | } | |
485 | } | |
486 | ||
0d906109 RB |
487 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
488 | { | |
489 | if ($respect_order) { | |
490 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
491 | } else { | |
492 | $order = ''; | |
493 | } | |
494 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
495 | FROM account_profiles AS ap | |
496 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
497 | AND ap.uid IN ' . XDB::formatArray($uids) .' | |
498 | ' . $order); | |
499 | } | |
b774ddab | 500 | |
e7b93962 FB |
501 | /** Return the profile associated with the given login. |
502 | */ | |
a3118782 FB |
503 | public static function get($login) |
504 | { | |
641f2115 FB |
505 | if (is_array($login)) { |
506 | return new Profile($login); | |
507 | } | |
b774ddab FB |
508 | $pid = self::getPID($login); |
509 | if (!is_null($pid)) { | |
0d906109 RB |
510 | $it = self::iterOverPIDs(array($pid), false); |
511 | return $it->next(); | |
b774ddab | 512 | } else { |
efe597c5 FB |
513 | /* Let say we can identify a profile using the identifiers of its owner. |
514 | */ | |
455ea0c9 FB |
515 | if (!($login instanceof PlUser)) { |
516 | $user = User::getSilent($login); | |
517 | if ($user && $user->hasProfile()) { | |
518 | return $user->profile(); | |
519 | } | |
efe597c5 | 520 | } |
3e53a496 | 521 | return null; |
e7b93962 FB |
522 | } |
523 | } | |
a3118782 | 524 | |
0d906109 RB |
525 | public static function iterOverUIDs($uids, $respect_order = true) |
526 | { | |
527 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order); | |
528 | } | |
529 | ||
530 | public static function iterOverPIDs($pids, $respect_order = true) | |
531 | { | |
532 | return new ProfileIterator(self::fetchProfileData($pids, $respect_order)); | |
533 | } | |
534 | ||
b774ddab FB |
535 | /** Return profiles for the list of pids. |
536 | */ | |
537 | public static function getBulkProfilesWithPIDs(array $pids) | |
538 | { | |
539 | if (count($pids) == 0) { | |
540 | return array(); | |
541 | } | |
0d906109 | 542 | $it = self::iterOverPIDs($pids); |
b774ddab | 543 | $profiles = array(); |
0d906109 RB |
544 | while ($p = $it->next()) { |
545 | $profiles[$p->id()] = $p; | |
b774ddab FB |
546 | } |
547 | return $profiles; | |
548 | } | |
549 | ||
550 | /** Return profiles for uids. | |
551 | */ | |
552 | public static function getBulkProfilesWithUIDS(array $uids) | |
553 | { | |
554 | if (count($uids) == 0) { | |
555 | return array(); | |
556 | } | |
0d906109 | 557 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids)); |
b774ddab FB |
558 | } |
559 | ||
913a4e90 RB |
560 | public static function isDisplayName($name) |
561 | { | |
562 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
563 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
564 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
565 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
566 | } | |
567 | ||
a3118782 FB |
568 | public static function getNameTypeId($type, $for_sql = false) |
569 | { | |
570 | if (!S::has('name_types')) { | |
eb6207f7 | 571 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type |
32742f84 | 572 | FROM profile_name_enum'); |
a3118782 FB |
573 | S::set('name_types', $table); |
574 | } else { | |
575 | $table = S::v('name_types'); | |
576 | } | |
577 | if ($for_sql) { | |
578 | return XDB::escape($table[$type]); | |
579 | } else { | |
580 | return $table[$type]; | |
581 | } | |
582 | } | |
726eaf7a SJ |
583 | |
584 | public static function rebuildSearchTokens($pid) | |
585 | { | |
586 | XDB::execute('DELETE FROM search_name | |
587 | WHERE uid = {?}', | |
588 | $pid); | |
589 | $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score, | |
590 | FIND_IN_SET('public', e.flags) AS public | |
591 | FROM profile_name AS n | |
592 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
593 | WHERE n.pid = {?}", | |
594 | $pid); | |
595 | ||
596 | foreach ($keys as $i => $key) { | |
597 | if ($key['name'] == '') { | |
598 | continue; | |
599 | } | |
600 | $toks = preg_split('/[ \'\-]+/', $key['name']); | |
601 | $token = ''; | |
602 | $first = 5; | |
603 | while ($toks) { | |
604 | $token = strtolower(replace_accent(array_pop($toks) . $token)); | |
605 | $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10); | |
606 | XDB::execute('REPLACE INTO search_name (token, uid, soundex, score, flags) | |
607 | VALUES ({?}, {?}, {?}, {?}, {?})', | |
608 | $token, $uid, soundex_fr($token), $score, $key['public']); | |
609 | $first = 0; | |
610 | } | |
611 | } | |
69b46857 SJ |
612 | } |
613 | ||
614 | /** The school identifier consists of 6 digits. The first 3 represent the | |
615 | * promotion entry year. The last 3 indicate the student's rank. | |
616 | * | |
617 | * Our identifier consists of 8 digits and both half have the same role. | |
618 | * This enables us to deal with bigger promotions and with a wider range | |
619 | * of promotions. | |
620 | * | |
621 | * getSchoolId returns a school identifier given one of ours. | |
622 | * getXorgId returns a X.org identifier given a school identifier. | |
623 | */ | |
624 | public static function getSchoolId($xorgId) | |
625 | { | |
626 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
627 | return null; | |
628 | } | |
629 | ||
630 | $year = intval(substr($xorgId, 0, 4)); | |
631 | $rank = intval(substr($xorgId, 5, 3)); | |
632 | if ($year < 1996) { | |
633 | return null; | |
634 | } elseif ($year < 2000) { | |
635 | $year = intval(substr(1900 - $year, 1, 3)); | |
636 | return sprintf('%02u0%03u', $year, $rank); | |
637 | } else { | |
638 | $year = intval(substr(1900 - $year, 1, 3)); | |
639 | return sprintf('%03u%03u', $year, $rank); | |
640 | } | |
641 | } | |
726eaf7a | 642 | |
69b46857 SJ |
643 | public static function getXorgId($schoolId) |
644 | { | |
645 | $year = intval(substr($schoolId, 0, 3)); | |
646 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 647 | |
69b46857 SJ |
648 | if ($year > 200) { |
649 | $year /= 10; | |
650 | } | |
651 | if ($year < 96) { | |
652 | return null; | |
653 | } else { | |
654 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
655 | } | |
726eaf7a | 656 | } |
e7b93962 FB |
657 | } |
658 | ||
0d906109 RB |
659 | /** Iterator over a set of Profiles |
660 | * @param an XDB::Iterator obtained from a Profile::fetchProfileData | |
661 | */ | |
662 | class ProfileIterator implements PlIterator | |
663 | { | |
664 | private $dbiter; | |
665 | ||
666 | public function __construct($dbiter) | |
667 | { | |
668 | $this->dbiter = $dbiter; | |
669 | } | |
670 | ||
671 | public function next() | |
672 | { | |
673 | $data = $this->dbiter->next(); | |
674 | if ($data == null) { | |
675 | return null; | |
676 | } else { | |
641f2115 | 677 | return Profile::get($data); |
0d906109 RB |
678 | } |
679 | } | |
680 | ||
681 | public function total() | |
682 | { | |
683 | return $this->dbiter->total(); | |
684 | } | |
685 | ||
686 | public function first() | |
687 | { | |
688 | return $this->dbiter->first(); | |
689 | } | |
690 | ||
691 | public function last() | |
692 | { | |
693 | return $this->dbiter->last(); | |
694 | } | |
695 | } | |
696 | ||
e7b93962 FB |
697 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
698 | ?> |