Update core and cleanup code.
[platal.git] / classes / profile.php
CommitLineData
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
22class 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
845f0a4d
SJ
116 public function yearpromo()
117 {
118 return intval(substr($this->promo, 1, 4));
119 }
120
94b72319
FB
121 /** Print a name with the given formatting:
122 * %s = • for women
123 * %f = firstname
124 * %l = lastname
125 * %F = fullname
126 * %S = shortname
127 * %p = promo
128 */
129 public function name($format)
130 {
131 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
132 array($this->isFemale() ? '•' : '',
faf75faf
SJ
133 $this->firstName(), $this->lastName(),
134 $this->fullName(), $this->shortName(),
135 $this->promo()), $format);
94b72319
FB
136 }
137
138 public function fullName($with_promo = false)
139 {
140 if ($with_promo) {
141 return $this->full_name . ' (' . $this->promo . ')';
142 }
143 return $this->full_name;
144 }
145
146 public function shortName($with_promo = false)
147 {
148 if ($with_promo) {
149 return $this->short_name . ' (' . $this->promo . ')';
150 }
151 return $this->short_name;
152 }
153
154 public function firstName()
155 {
08c91036 156 return $this->firstname;
94b72319
FB
157 }
158
5c005b37
RB
159 public function firstNames()
160 {
161 return $this->nameVariants(self::FIRSTNAME);
162 }
163
94b72319
FB
164 public function lastName()
165 {
08c91036 166 return $this->lastname;
94b72319
FB
167 }
168
5c005b37
RB
169 public function lastNames()
170 {
171 return $this->nameVariants(self::LASTNAME);
172 }
173
94b72319
FB
174 public function isFemale()
175 {
176 return $this->sex == PlUser::GENDER_FEMALE;
177 }
178
179 public function data()
180 {
181 $this->first_name;
182 return $this->data;
183 }
184
5c005b37
RB
185 private function nameVariants($type)
186 {
187 $vals = array($this->$type);
188 foreach (self::$name_variants[$type] as $var) {
189 $vartype = $type . '_' . $var;
190 $varname = $this->$vartype;
191 if ($varname != null && $varname != "") {
192 $vals[] = $varname;
193 }
194 }
195 return array_unique($vals);
196 }
197
b6569a95
FB
198 public function nationalities()
199 {
200 $nats = array();
201 if ($this->nationality1) {
202 $nats[] = $this->nationality1;
203 }
204 if ($this->nationality2) {
205 $nats[] = $this->nationality2;
206 }
207 if ($this->nationality3) {
208 $nats[] = $this->nationality3;
209 }
210 return $nats;
211 }
212
3e53a496
FB
213 public function __get($name)
214 {
215 if (property_exists($this, $name)) {
216 return $this->$name;
217 }
218
3e53a496
FB
219 if (isset($this->data[$name])) {
220 return $this->data[$name];
221 }
222
223 return null;
224 }
225
226 public function __isset($name)
227 {
228 return property_exists($this, $name) || isset($this->data[$name]);
229 }
230
c159e50f
RB
231 /** Sets the level of visibility of the profile
232 * Sets $this->visibility to a list of valid visibilities.
233 * @param one of the self::VISIBILITY_* values
234 */
f5642983
FB
235 public function setVisibilityLevel($visibility)
236 {
0d906109
RB
237 if ($visibility != self::VISIBILITY_PRIVATE
238 && $visibility != self::VISIBILITY_AX
f5642983
FB
239 && $visibility != self::VISIBILITY_PUBLIC) {
240 Platal::page()->kill("Visibility invalide: " . $visibility);
241 }
242 $this->visibility = self::$v_values[$visibility];
5c005b37 243 if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) {
46e9eb99
FB
244 unset($this->data['mobile']);
245 }
f5642983
FB
246 }
247
c159e50f
RB
248 /** Determine whether an item with visibility $visibility can be displayed
249 * with the current level of visibility of the profile
250 * @param $visibility The level of visibility to be checked
251 */
252 public function isVisible($visibility)
253 {
254 return in_array($visibility, $this->visibility);
255 }
4bc5b8f0 256
833a6e86
FB
257 /* Photo
258 */
259 public function getPhoto($fallback = true)
260 {
833a6e86
FB
261 $cond = '';
262 if ($this->visibility) {
bde68f05 263 $cond = XDB::format(' AND pub IN {?}', $this->visibility);
833a6e86 264 }
5d83c3be 265 $res = XDB::query("SELECT *
5c4ea53f 266 FROM profile_photos
5d83c3be
SJ
267 WHERE attachmime IN ('jpeg', 'png')
268 " . $cond . ' AND pid = {?}',
833a6e86
FB
269 $this->id());
270 if ($res->numRows() > 0) {
271 $photo = $res->fetchOneAssoc();
272 return PlImage::fromData($photo['attach'], 'image/' . $photo['attachmime'],
273 $photo['x'], $photo['y']);
274 } else if ($fallback) {
275 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png',
276 'image/png');
277 }
278 return null;
279 }
280
4bc5b8f0
FB
281 /* Addresses
282 */
283 public function getAddresses($flags, $limit = null)
f5642983
FB
284 {
285 $where = XDB::format('pa.pid = {?}', $this->id());
286 if ($flags & self::ADDRESS_MAIN) {
287 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
288 }
289 if ($flags & self::ADDRESS_POSTAL) {
290 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
291 }
292 if ($this->visibility) {
bde68f05 293 $where .= XDB::format(' AND pa.pub IN {?}', $this->visibility);
f5642983
FB
294 }
295 $type = array();
296 if ($flags & self::ADDRESS_PRO) {
297 $type[] = 'job';
298 }
299 if ($flags & self::ADDRESS_PERSO) {
300 $type[] = 'home';
301 }
302 if (count($type) > 0) {
bde68f05 303 $where .= XDB::format(' AND pa.type IN {?}', $type);
f5642983 304 }
4bc5b8f0 305 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
f5642983
FB
306 return XDB::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
307 gl.name AS locality, gas.name AS subAdministrativeArea,
308 ga.name AS administrativeArea, gc.countryFR AS country,
6b83ce06 309 ppfix.display_tel AS fixed_tel, ppfax.display_tel AS fax_tel,
f5642983
FB
310 FIND_IN_SET(\'current\', pa.flags) AS current,
311 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
312 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
313 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
314 FROM profile_addresses AS pa
315 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
316 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
317 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
318 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
ce0b2c6f
FB
319 LEFT JOIN profile_phones AS ppfix ON (ppfix.link_type = \'address\' AND ppfix.pid = pa.pid AND ppfix.link_id = pa.id AND ppfix.tel_type = \'fixed\')
320 LEFT JOIN profile_phones AS ppfax ON (ppfax.link_type = \'address\' AND ppfax.pid = pa.pid AND ppfax.link_id = pa.id AND ppfax.tel_type = \'fax\')
4bc5b8f0
FB
321 WHERE ' . $where . '
322 ORDER BY pa.id
323 ' . $limit);
f5642983
FB
324 }
325
326 public function getMainAddress()
327 {
328 $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
329 if ($it->total() == 0) {
330 return null;
331 } else {
332 return $it->next();
333 }
334 }
3e53a496 335
4bc5b8f0
FB
336
337 /* Educations
338 */
339 public function getEducations($flags, $limit = null)
340 {
ce0b2c6f 341 $where = XDB::format('pe.pid = {?}', $this->id());
4bc5b8f0
FB
342 if ($flags & self::EDUCATION_MAIN) {
343 $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
344 } else if ($flags & self::EDUCATION_EXTRA) {
345 $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
346 } else if ($flags & self::EDUCATION_FINISHED) {
347 $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
348 } else if ($flags & self::EDUCATION_CURRENT) {
349 $where .= ' AND pe.grad_year > YEAR(CURDATE())';
350 }
351 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
352 return XDB::iterator('SELECT pe.entry_year, pe.grad_year, pe.program,
353 pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
354 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
355 FIND_IN_SET(\'primary\', pe.flags) AS prim
356 FROM profile_education AS pe
357 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
358 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
359 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
360 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
361 WHERE ' . $where . '
362 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
363 ' . $limit);
364 }
365
366 public function getExtraEducations($limit = null)
367 {
368 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
369 }
370
371
04a94b1d
FB
372 /** Networking
373 */
374
375 public function getNetworking($flags, $limit = null)
376 {
ce0b2c6f 377 $where = XDB::format('pn.pid = {?}', $this->id());
04a94b1d
FB
378 if ($flags & self::NETWORKING_WEB) {
379 $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index
380 }
381 if ($this->visibility) {
bde68f05 382 $where .= XDB::format(' AND pn.pub IN {?}', $this->visibility);
04a94b1d
FB
383 }
384 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
385 return XDB::iterator('SELECT pne.name, pne.icon,
386 IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address),
387 pn.address) AS address
388 FROM profile_networking AS pn
389 INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type)
390 WHERE ' . $where . '
391 ORDER BY pn.network_type, pn.nwid
392 ' . $limit);
393 }
394
395 public function getWebSite()
396 {
397 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
398 if ($site->total() != 1) {
399 return null;
400 }
401 $site = $site->next();
402 return $site['address'];
403 }
404
405
e718bd18
FB
406 /** Jobs
407 */
408
409 public function getJobs($flags, $limit = null)
410 {
ce0b2c6f 411 $where = XDB::format('pj.pid = {?}', $this->id());
e718bd18
FB
412 $cond = 'TRUE';
413 if ($this->visibility) {
bde68f05
FB
414 $where .= XDB::format(' AND pj.pub IN {?}', $this->visibility);
415 $cond = XDB::format('pj.email_pub IN {?}', $this->visibility);
e718bd18
FB
416 }
417 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
418 return XDB::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code,
419 pj.description, pj.url AS user_site,
420 IF (' . $cond . ', pj.email, NULL) AS user_email,
83ff5281
SJ
421 pjse.name AS sector, pjsse.name AS subsector,
422 pjssse.name AS subsubsector
e718bd18
FB
423 FROM profile_job AS pj
424 INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid)
e718bd18
FB
425 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
426 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
427 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
428 WHERE ' . $where . '
429 ORDER BY pj.id
430 ' . $limit);
431 }
432
433 public function getMailJob()
434 {
435 $job = $this->getJobs(self::JOBS_MAIN, 1);
436 if ($job->total() != 1) {
437 return null;
438 }
439 return $job->next();
440 }
441
5c005b37
RB
442 /* Binets
443 */
444 public function getBinets()
445 {
446 return XDB::fetchColumn('SELECT binet_id
5c8a71f2 447 FROM profile_binets
bdd977d7 448 WHERE pid = {?}', $this->id());
5c005b37
RB
449 }
450
e718bd18 451
e7b93962
FB
452 public function owner()
453 {
454 return User::getSilent($this);
455 }
456
94590511
SJ
457 public function compareNames($firstname, $lastname)
458 {
459 $_lastname = mb_strtoupper($this->lastName());
460 $_firstname = mb_strtoupper($this->firstName());
461 $lastname = mb_strtoupper($lastname);
462 $firstname = mb_strtoupper($firstname);
463
464 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
465 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY);
466 $maxlen = 0;
467
468 foreach ($tokens as $str) {
469 $isOk &= (strpos($_lastname, $str) !== false);
470 $maxlen = max($maxlen, strlen($str));
471 }
472
473 return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname)));
474 }
475
6150f591
SJ
476 /**
477 * Clears a profile.
478 * *always deletes in: profile_addresses, profile_binets, profile_job,
479 * profile_langskills, profile_mentor, profile_networking,
480 * profile_phones, profile_skills, watch_profile
481 * *always keeps in: profile_corps, profile_display, profile_education,
482 * profile_medals, profile_name, profile_photos, search_name
483 * *modifies: profiles
484 */
485 public function clear()
486 {
487 XDB::execute('DELETE FROM profile_job, profile_langskills, profile_mentor,
488 profile_networking, profile_skills, watch_profile
489 WHERE pid = {?}',
490 $this->id());
491 XDB::execute('DELETE FROM profile_addresses, profile_binets,
492 profile_phones
493 WHERE pid = {?}',
494 $this->id());
495 XDB::execute("UPDATE profiles
496 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
497 medals_pub = 'private', alias_pub = 'private',
498 email_directory = NULL
499 WHERE pid = {?}",
500 $this->id());
501 }
502
0d906109 503 private static function fetchProfileData(array $pids, $respect_order = true)
b774ddab
FB
504 {
505 if (count($pids) == 0) {
506 return array();
507 }
0d906109
RB
508
509 if ($respect_order) {
510 $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids);
511 } else {
512 $order = '';
513 }
514
515 return XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
516 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
517 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
518 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
519 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
520 pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub,
88c31faf
FB
521 ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub,
522 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
523 ap.uid AS owner_id
0d906109
RB
524 FROM profiles AS p
525 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
ce0b2c6f 526 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
ec8e2c4a
FB
527 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid AND pn_f.typeid = {?})
528 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid AND pn_l.typeid = {?})
529 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid AND pn_uf.typeid = {?})
530 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid AND pn_ul.typeid = {?})
531 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid AND pn_n.typeid = {?})
ce0b2c6f 532 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
5c4ea53f 533 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
0d906109 534 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
bde68f05 535 WHERE p.pid IN {?}
0d906109 536 GROUP BY p.pid
69ef14a5
FB
537 ' . $order,
538 DirEnum::getID(DirEnum::NAMETYPES, 'firstname'),
539 DirEnum::getID(DirEnum::NAMETYPES, 'lastname'),
540 DirEnum::getID(DirEnum::NAMETYPES, 'firstname_ordinary'),
541 DirEnum::getID(DirEnum::NAMETYPES, 'lastname_ordinary'),
542 DirEnum::getID(DirEnum::NAMETYPES, 'nickname'), $pids);
b774ddab
FB
543 }
544
545 public static function getPID($login)
546 {
547 if ($login instanceof PlUser) {
548 return XDB::fetchOneCell('SELECT pid
549 FROM account_profiles
550 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
551 $login->id());
4aae4d2c 552 } else if (is_int($login) || ctype_digit($login)) {
b774ddab
FB
553 return XDB::fetchOneCell('SELECT pid
554 FROM profiles
555 WHERE pid = {?}', $login);
556 } else {
557 return XDB::fetchOneCell('SELECT pid
558 FROM profiles
559 WHERE hrpid = {?}', $login);
560 }
561 }
562
0d906109
RB
563 public static function getPIDsFromUIDs($uids, $respect_order = true)
564 {
565 if ($respect_order) {
566 $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids);
567 } else {
568 $order = '';
569 }
570 return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
571 FROM account_profiles AS ap
572 WHERE FIND_IN_SET(\'owner\', ap.perms)
bde68f05
FB
573 AND ap.uid IN {?}
574 ' . $order, $uids);
0d906109 575 }
b774ddab 576
e7b93962
FB
577 /** Return the profile associated with the given login.
578 */
a3118782
FB
579 public static function get($login)
580 {
641f2115
FB
581 if (is_array($login)) {
582 return new Profile($login);
583 }
b774ddab
FB
584 $pid = self::getPID($login);
585 if (!is_null($pid)) {
0d906109
RB
586 $it = self::iterOverPIDs(array($pid), false);
587 return $it->next();
b774ddab 588 } else {
efe597c5
FB
589 /* Let say we can identify a profile using the identifiers of its owner.
590 */
455ea0c9
FB
591 if (!($login instanceof PlUser)) {
592 $user = User::getSilent($login);
593 if ($user && $user->hasProfile()) {
594 return $user->profile();
595 }
efe597c5 596 }
3e53a496 597 return null;
e7b93962
FB
598 }
599 }
a3118782 600
0d906109
RB
601 public static function iterOverUIDs($uids, $respect_order = true)
602 {
603 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order);
604 }
605
606 public static function iterOverPIDs($pids, $respect_order = true)
607 {
608 return new ProfileIterator(self::fetchProfileData($pids, $respect_order));
609 }
610
b774ddab
FB
611 /** Return profiles for the list of pids.
612 */
613 public static function getBulkProfilesWithPIDs(array $pids)
614 {
615 if (count($pids) == 0) {
616 return array();
617 }
0d906109 618 $it = self::iterOverPIDs($pids);
b774ddab 619 $profiles = array();
0d906109
RB
620 while ($p = $it->next()) {
621 $profiles[$p->id()] = $p;
b774ddab
FB
622 }
623 return $profiles;
624 }
625
626 /** Return profiles for uids.
627 */
628 public static function getBulkProfilesWithUIDS(array $uids)
629 {
630 if (count($uids) == 0) {
631 return array();
632 }
0d906109 633 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids));
b774ddab
FB
634 }
635
913a4e90
RB
636 public static function isDisplayName($name)
637 {
638 return $name == self::DN_FULL || $name == self::DN_DISPLAY
639 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
640 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
641 || $name == self::DN_SHORT || $name == self::DN_SORT;
642 }
643
726eaf7a
SJ
644 public static function rebuildSearchTokens($pid)
645 {
646 XDB::execute('DELETE FROM search_name
6dbb167e 647 WHERE pid = {?}',
726eaf7a
SJ
648 $pid);
649 $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
650 FIND_IN_SET('public', e.flags) AS public
651 FROM profile_name AS n
652 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
653 WHERE n.pid = {?}",
654 $pid);
655
656 foreach ($keys as $i => $key) {
657 if ($key['name'] == '') {
658 continue;
659 }
660 $toks = preg_split('/[ \'\-]+/', $key['name']);
661 $token = '';
662 $first = 5;
663 while ($toks) {
664 $token = strtolower(replace_accent(array_pop($toks) . $token));
665 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
6dbb167e 666 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
726eaf7a 667 VALUES ({?}, {?}, {?}, {?}, {?})',
6dbb167e 668 $token, $pid, soundex_fr($token), $score, $key['public']);
726eaf7a
SJ
669 $first = 0;
670 }
671 }
69b46857
SJ
672 }
673
674 /** The school identifier consists of 6 digits. The first 3 represent the
675 * promotion entry year. The last 3 indicate the student's rank.
676 *
677 * Our identifier consists of 8 digits and both half have the same role.
678 * This enables us to deal with bigger promotions and with a wider range
679 * of promotions.
680 *
681 * getSchoolId returns a school identifier given one of ours.
682 * getXorgId returns a X.org identifier given a school identifier.
683 */
684 public static function getSchoolId($xorgId)
685 {
686 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
687 return null;
688 }
689
690 $year = intval(substr($xorgId, 0, 4));
691 $rank = intval(substr($xorgId, 5, 3));
692 if ($year < 1996) {
693 return null;
694 } elseif ($year < 2000) {
695 $year = intval(substr(1900 - $year, 1, 3));
696 return sprintf('%02u0%03u', $year, $rank);
697 } else {
698 $year = intval(substr(1900 - $year, 1, 3));
699 return sprintf('%03u%03u', $year, $rank);
700 }
701 }
726eaf7a 702
69b46857
SJ
703 public static function getXorgId($schoolId)
704 {
94590511 705 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
a292484d
SJ
706 return null;
707 }
708
69b46857
SJ
709 $year = intval(substr($schoolId, 0, 3));
710 $rank = intval(substr($schoolId, 3, 3));
726eaf7a 711
69b46857
SJ
712 if ($year > 200) {
713 $year /= 10;
714 }
715 if ($year < 96) {
716 return null;
717 } else {
718 return sprintf('%04u%04u', 1900 + $year, $rank);
719 }
726eaf7a 720 }
e7b93962
FB
721}
722
0d906109
RB
723/** Iterator over a set of Profiles
724 * @param an XDB::Iterator obtained from a Profile::fetchProfileData
725 */
726class ProfileIterator implements PlIterator
727{
728 private $dbiter;
729
730 public function __construct($dbiter)
731 {
732 $this->dbiter = $dbiter;
733 }
734
735 public function next()
736 {
737 $data = $this->dbiter->next();
738 if ($data == null) {
739 return null;
740 } else {
641f2115 741 return Profile::get($data);
0d906109
RB
742 }
743 }
744
745 public function total()
746 {
747 return $this->dbiter->total();
748 }
749
750 public function first()
751 {
752 return $this->dbiter->first();
753 }
754
755 public function last()
756 {
757 return $this->dbiter->last();
758 }
759}
760
e7b93962
FB
761// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
762?>