Sorts language alphabetically in skill tab. Closes #1081
[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
1a9affb7 22class ProfileVisibility
e7b93962 23{
1a9affb7
RB
24 static private $v_values = array(self::VIS_PUBLIC => array(self::VIS_PUBLIC),
25 self::VIS_AX => array(self::VIS_AX, self::VIS_PUBLIC),
26 self::VIS_PRIVATE => array(self::VIS_PRIVATE, self::VIS_AX, self::VIS_PUBLIC));
27
28 const VIS_PUBLIC = 'public';
29 const VIS_AX = 'ax';
30 const VIS_PRIVATE = 'private';
31
32 private $level;
913a4e90 33
1a9affb7
RB
34 public function __construct($level = null)
35 {
36 $this->setLevel($level);
37 }
38
39 public function setLevel($level = self::VIS_PUBLIC)
40 {
1f8250e4 41 if ($level != null && $level != self::VIS_PRIVATE && $level != self::VIS_AX && $level != self::VIS_PUBLIC) {
1a9affb7
RB
42 Platal::page()->kill("Invalid visibility: " . $level);
43 }
44
45 if (!S::logged()) {
46 $level = self::VIS_PUBLIC;
47 } else if ($level == null) {
48 $level = self::VIS_PRIVATE;
49 }
50
51 if ($this->level == null || $this->level == self::VIS_PRIVATE) {
52 $this->level = $level;
53 } else if ($this->level == self::VIS_AX && $level == self::VIS_PRIVATE) {
54 return;
55 } else {
56 $this->level = self::VIS_PUBLIC;
57 }
58 }
59
60 public function level()
61 {
62 if ($this->level == null) {
63 return self::VIS_PUBLIC;
64 } else {
65 return $this->level;
66 }
67 }
68
69 public function levels()
70 {
71 return self::$v_values[$this->level()];
72 }
73
74 public function isVisible($visibility)
75 {
76 return in_array($visibility, $this->levels());
77 }
78}
79
80class Profile
81{
f5642983 82
913a4e90
RB
83 /* name tokens */
84 const LASTNAME = 'lastname';
85 const FIRSTNAME = 'firstname';
86 const NICKNAME = 'nickname';
87 const PSEUDONYM = 'pseudonym';
88 const NAME = 'name';
89 /* name variants */
90 const VN_MARITAL = 'marital';
91 const VN_ORDINARY = 'ordinary';
92 const VN_OTHER = 'other';
93 const VN_INI = 'ini';
94 /* display names */
95 const DN_FULL = 'directory_name';
96 const DN_DISPLAY = 'yourself';
97 const DN_YOURSELF = 'yourself';
98 const DN_DIRECTORY = 'directory_name';
99 const DN_PRIVATE = 'private_name';
100 const DN_PUBLIC = 'public_name';
101 const DN_SHORT = 'short_name';
102 const DN_SORT = 'sort_name';
e02d9fbb
SJ
103 /* education related names */
104 const EDU_X = 'École polytechnique';
105 const DEGREE_X = 'Ingénieur';
106 const DEGREE_M = 'Master';
107 const DEGREE_D = 'Doctorat';
913a4e90
RB
108
109 static public $name_variants = array(
110 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
111 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
112 );
113
f5642983
FB
114 const ADDRESS_MAIN = 0x000001;
115 const ADDRESS_PERSO = 0x000002;
116 const ADDRESS_PRO = 0x000004;
117 const ADDRESS_ALL = 0x000006;
118 const ADDRESS_POSTAL = 0x000008;
119
120 const EDUCATION_MAIN = 0x000010;
4bc5b8f0
FB
121 const EDUCATION_EXTRA = 0x000020;
122 const EDUCATION_ALL = 0x000040;
123 const EDUCATION_FINISHED = 0x000080;
124 const EDUCATION_CURRENT = 0x000100;
f5642983 125
4bc5b8f0
FB
126 const JOBS_MAIN = 0x001000;
127 const JOBS_ALL = 0x002000;
128 const JOBS_FINISHED = 0x004000;
129 const JOBS_CURRENT = 0x008000;
f5642983 130
04a94b1d
FB
131 const NETWORKING_ALL = 0x000000;
132 const NETWORKING_WEB = 0x010000;
133 const NETWORKING_IM = 0x020000;
134 const NETWORKING_SOCIAL = 0x040000;
135
4d1f0f6b
RB
136 const FETCH_ADDRESSES = 0x000001;
137 const FETCH_CORPS = 0x000002;
138 const FETCH_EDU = 0x000004;
139 const FETCH_JOBS = 0x000008;
140 const FETCH_MEDALS = 0x000010;
141 const FETCH_NETWORKING = 0x000020;
142 const FETCH_MENTOR_SECTOR = 0x000040;
143 const FETCH_MENTOR_COUNTRY = 0x000080;
144 const FETCH_PHONES = 0x000100;
9f21bd15 145
4d1f0f6b 146 const FETCH_MINIFICHES = 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
fa9994fa 147
4d1f0f6b 148 const FETCH_ALL = 0x0001FF; // OR of FETCH_*
6b5008ec 149
1f8250e4
RB
150 private $fetched_fields = 0x000000;
151
e7b93962
FB
152 private $pid;
153 private $hrpid;
564e2b2a
RB
154 private $owner;
155 private $owner_fetched = false;
3e53a496
FB
156 private $data = array();
157
f5642983
FB
158 private $visibility = null;
159
1f8250e4 160
b774ddab 161 private function __construct(array $data)
e7b93962 162 {
b774ddab 163 $this->data = $data;
832e6fcb
FB
164 $this->pid = $this->data['pid'];
165 $this->hrpid = $this->data['hrpid'];
1a9affb7 166 $this->visibility = new ProfileVisibility();
9f21bd15
RB
167 }
168
e7b93962
FB
169 public function id()
170 {
171 return $this->pid;
172 }
173
174 public function hrid()
175 {
176 return $this->hrpid;
177 }
178
564e2b2a
RB
179 public function owner()
180 {
181 if ($this->owner == null && !$this->owner_fetched) {
182 $this->owner_fetched = true;
183 $this->owner = User::getSilent($this);
184 }
185 return $this->owner;
186 }
187
d5e60905
FB
188 public function promo()
189 {
190 return $this->promo;
191 }
192
845f0a4d
SJ
193 public function yearpromo()
194 {
195 return intval(substr($this->promo, 1, 4));
196 }
197
963bc7fc
SJ
198 public function mainEducation()
199 {
39e2c58c
FB
200 if (empty($this->promo)) {
201 return null;
202 } else {
203 return $this->promo{0};
204 }
963bc7fc
SJ
205 }
206
02d9dff0
FB
207 public function mainGrade()
208 {
209 switch ($this->mainEducation()) {
210 case 'X':
211 return UserFilter::GRADE_ING;
212 case 'M':
213 return UserFilter::GRADE_MST;
214 case 'D':
215 return UserFilter::GRADE_PHD;
216 default:
217 return null;
218 }
219 }
220
963bc7fc
SJ
221 public function mainEducationDuration()
222 {
223 switch ($this->mainEducation()) {
224 case 'X':
225 return 3;
226 case 'M':
227 return 2;
228 case 'D':
229 return 3;
230 default:
231 return 0;
232 }
233 }
234
94b72319
FB
235 /** Print a name with the given formatting:
236 * %s = • for women
237 * %f = firstname
238 * %l = lastname
239 * %F = fullname
240 * %S = shortname
241 * %p = promo
242 */
243 public function name($format)
244 {
245 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
246 array($this->isFemale() ? '•' : '',
faf75faf
SJ
247 $this->firstName(), $this->lastName(),
248 $this->fullName(), $this->shortName(),
249 $this->promo()), $format);
94b72319
FB
250 }
251
252 public function fullName($with_promo = false)
253 {
254 if ($with_promo) {
255 return $this->full_name . ' (' . $this->promo . ')';
256 }
257 return $this->full_name;
258 }
259
260 public function shortName($with_promo = false)
261 {
262 if ($with_promo) {
263 return $this->short_name . ' (' . $this->promo . ')';
264 }
265 return $this->short_name;
266 }
267
268 public function firstName()
269 {
08c91036 270 return $this->firstname;
94b72319
FB
271 }
272
5c005b37
RB
273 public function firstNames()
274 {
275 return $this->nameVariants(self::FIRSTNAME);
276 }
277
94b72319
FB
278 public function lastName()
279 {
08c91036 280 return $this->lastname;
94b72319
FB
281 }
282
5c005b37
RB
283 public function lastNames()
284 {
285 return $this->nameVariants(self::LASTNAME);
286 }
287
94b72319
FB
288 public function isFemale()
289 {
290 return $this->sex == PlUser::GENDER_FEMALE;
291 }
292
400ac338
RB
293 public function isDead()
294 {
295 return ($this->deathdate != null);
296 }
297
564e2b2a
RB
298 public function displayEmail()
299 {
300 $o = $this->owner();
301 if ($o != null) {
302 return $o->bestEmail();
303 } else {
304 return $this->email_directory;
305 }
306 }
307
94b72319
FB
308 public function data()
309 {
310 $this->first_name;
311 return $this->data;
312 }
313
5c005b37
RB
314 private function nameVariants($type)
315 {
316 $vals = array($this->$type);
317 foreach (self::$name_variants[$type] as $var) {
318 $vartype = $type . '_' . $var;
319 $varname = $this->$vartype;
320 if ($varname != null && $varname != "") {
321 $vals[] = $varname;
322 }
323 }
324 return array_unique($vals);
325 }
326
b6569a95
FB
327 public function nationalities()
328 {
329 $nats = array();
330 if ($this->nationality1) {
331 $nats[] = $this->nationality1;
332 }
333 if ($this->nationality2) {
334 $nats[] = $this->nationality2;
335 }
336 if ($this->nationality3) {
337 $nats[] = $this->nationality3;
338 }
339 return $nats;
340 }
341
3e53a496
FB
342 public function __get($name)
343 {
344 if (property_exists($this, $name)) {
345 return $this->$name;
346 }
347
3e53a496
FB
348 if (isset($this->data[$name])) {
349 return $this->data[$name];
350 }
351
352 return null;
353 }
354
355 public function __isset($name)
356 {
357 return property_exists($this, $name) || isset($this->data[$name]);
358 }
359
c159e50f
RB
360 /** Sets the level of visibility of the profile
361 * Sets $this->visibility to a list of valid visibilities.
1a9affb7 362 * @param one of the self::VIS_* values
c159e50f 363 */
f5642983
FB
364 public function setVisibilityLevel($visibility)
365 {
1a9affb7 366 $this->visibility->setLevel($visibility);
f5642983
FB
367 }
368
c159e50f
RB
369 /** Determine whether an item with visibility $visibility can be displayed
370 * with the current level of visibility of the profile
371 * @param $visibility The level of visibility to be checked
372 */
373 public function isVisible($visibility)
374 {
1a9affb7 375 return $this->visibility->isVisible($visibility);
9f21bd15
RB
376 }
377
1f8250e4
RB
378 /** Stores the list of fields which have already been fetched for this Profile
379 */
380 public function setFetchedFields($fields)
990cb17b 381 {
1f8250e4
RB
382 if (($fields | self::FETCH_ALL) != self::FETCH_ALL) {
383 Platal::page()->kill("Invalid fetched fields: $fields");
384 }
385
386 $this->fetched_fields = $fields;
387 }
388
389 private function fetched($field)
390 {
391 if (!array_key_exists($field, ProfileField::$fields)) {
392 Platal::page()->kill("Invalid field: $field");
393 }
394
395 return ($this->fetched_fields & $field);
396 }
397
398 /** If not already done, fetches data for the given field
399 * @param $field One of the Profile::FETCH_*
400 * @return A ProfileField, or null
401 */
402 private function getProfileField($field)
403 {
404 if ($this->fetched($field)) {
405 return null;
406 } else {
407 $this->fetched_fields = $this->fetched_fields | $field;
408 }
409
410 $cls = ProfileField::$fields[$field];
411
990cb17b
RB
412 return ProfileField::getForPID($cls, $this->id(), $this->visibility);
413 }
414
8cf886dc
RB
415 /** Consolidates internal data (addresses, phones, jobs)
416 */
417 private function consolidateFields()
418 {
419 if ($this->phones != null) {
420 if ($this->addresses != null) {
421 $this->addresses->addPhones($this->phones);
422 }
423
424 if ($this->jobs != null) {
425 $this->jobs->addPhones($this->phones);
426 }
427 }
428
429 if ($this->addresses != null && $this->jobs != null) {
430 $this->jobs->addAddresses($this->addresses);
431 }
432 }
433
833a6e86
FB
434 /* Photo
435 */
1a9affb7 436 private $photo = null;
7988f7d6 437 public function getPhoto($fallback = true, $data = false)
9f21bd15 438 {
1a9affb7
RB
439 if ($this->has_photo) {
440 if ($data && ($this->photo == null || $this->photo->mimeType == null)) {
441 $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y
1f8250e4
RB
442 FROM profile_photos
443 WHERE pid = {?}', $this->pid);
1a9affb7
RB
444 $this->photo = PlImage::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
445 } else if ($this->photo == null) {
446 $this->photo = PlImage::fromData(null, null, $this->photo_width, $this->photo_height);
447 }
448 return $this->photo;
9f21bd15
RB
449 } else if ($fallback) {
450 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png',
451 'image/png');
452 }
453 return null;
454 }
7d0ebdf5 455
4bc5b8f0
FB
456 /* Addresses
457 */
7d0ebdf5
RB
458 private $addresses = null;
459 public function setAddresses(ProfileAddresses $addr)
460 {
461 $this->addresses = $addr;
8cf886dc 462 $this->consolidateFields();
7d0ebdf5
RB
463 }
464
4bc5b8f0 465 public function getAddresses($flags, $limit = null)
f5642983 466 {
1f8250e4 467 if ($this->addresses == null && !$this->fetched(self::FETCH_ADDRESSES)) {
61ee68c7
FB
468 $addr = $this->getProfileField(self::FETCH_ADDRESSES);
469 if ($addr) {
470 $this->setAddresses($addr);
471 }
f5642983 472 }
0907501b
RB
473
474 if ($this->addresses == null) {
598c57f6 475 return array();
0907501b 476 }
990cb17b 477 return $this->addresses->get($flags, $limit);
f5642983
FB
478 }
479
598c57f6
RB
480 public function iterAddresses($flags, $limit = null)
481 {
482 return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true);
483 }
484
f5642983
FB
485 public function getMainAddress()
486 {
598c57f6
RB
487 $addr = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
488 if (count($addr) == 0) {
f5642983
FB
489 return null;
490 } else {
598c57f6 491 return array_pop($addr);
f5642983
FB
492 }
493 }
3e53a496 494
bdef0d33
RB
495 /* Phones
496 */
497 private $phones = null;
498 public function setPhones(ProfilePhones $phones)
499 {
500 $this->phones = $phones;
501 $this->consolidateFields();
502 }
503
504 public function getPhones($flags, $limit = null)
505 {
1f8250e4
RB
506 if ($this->phones == null && !$this->fetched(self::FETCH_PHONES)) {
507 $this->setPhones($this->getProfileField(self::FETCH_PHONES));
bdef0d33
RB
508 }
509
510 if ($this->phones == null) {
598c57f6 511 return array();
bdef0d33
RB
512 }
513 return $this->phones->get($flags, $limit);
514 }
4bc5b8f0
FB
515
516 /* Educations
517 */
d4d395bb 518 private $educations = null;
a060e1c3
RB
519 public function setEducations(ProfileEducation $edu)
520 {
521 $this->educations = $edu;
522 }
523
4bc5b8f0
FB
524 public function getEducations($flags, $limit = null)
525 {
1f8250e4
RB
526 if ($this->educations == null && !$this->fetched(self::FETCH_EDU)) {
527 $this->setEducations($this->getProfileField(self::FETCH_EDU));
d4d395bb 528 }
0907501b
RB
529
530 if ($this->educations == null) {
598c57f6 531 return array();
0907501b 532 }
a060e1c3 533 return $this->educations->get($flags, $limit);
4bc5b8f0
FB
534 }
535
536 public function getExtraEducations($limit = null)
537 {
538 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
539 }
540
0396c259
RB
541 /* Corps
542 */
543 private $corps = null;
544 public function setCorps(ProfileCorps $corps)
545 {
546 $this->corps = $corps;
547 }
548
549 public function getCorps()
550 {
1f8250e4
RB
551 if ($this->corps == null && !$this->fetched(self::FETCH_CORPS)) {
552 $this->setCorps($this->getProfileField(self::FETCH_CORPS));
0396c259
RB
553 }
554 return $this->corps;
555 }
4bc5b8f0 556
04a94b1d
FB
557 /** Networking
558 */
d4d395bb
RB
559 private $networks = null;
560 public function setNetworking(ProfileNetworking $nw)
561 {
562 $this->networks = $nw;
563 }
04a94b1d
FB
564
565 public function getNetworking($flags, $limit = null)
566 {
1f8250e4 567 if ($this->networks == null && !$this->fetched(self::FETCH_NETWORKING)) {
dad85695
FB
568 $nw = $this->getProfileField(self::FETCH_NETWORKING);
569 if ($nw) {
570 $this->setNetworking($nw);
571 }
04a94b1d 572 }
0907501b 573 if ($this->networks == null) {
598c57f6 574 return array();
0907501b 575 }
d4d395bb 576 return $this->networks->get($flags, $limit);
04a94b1d
FB
577 }
578
579 public function getWebSite()
580 {
581 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
598c57f6 582 if (count($site) != 1) {
04a94b1d
FB
583 return null;
584 }
598c57f6 585 $site = array_pop($site);
dad85695 586 return $site;
04a94b1d
FB
587 }
588
589
e718bd18
FB
590 /** Jobs
591 */
949fc736
RB
592 private $jobs = null;
593 public function setJobs(ProfileJobs $jobs)
594 {
595 $this->jobs = $jobs;
596 $this->consolidateFields();
597 }
e718bd18
FB
598
599 public function getJobs($flags, $limit = null)
600 {
1f8250e4 601 if ($this->jobs == null && !$this->fetched(self::FETCH_JOBS)) {
61ee68c7
FB
602 $jobs = $this->getProfileField(self::FETCH_JOBS);
603 if ($jobs) {
604 $this->setJobs($jobs);
605 }
e718bd18 606 }
949fc736 607
0907501b 608 if ($this->jobs == null) {
598c57f6 609 return array();
0907501b 610 }
949fc736 611 return $this->jobs->get($flags, $limit);
e718bd18
FB
612 }
613
44ec5eb5 614 public function getMainJob()
e718bd18
FB
615 {
616 $job = $this->getJobs(self::JOBS_MAIN, 1);
598c57f6 617 if (count($job) != 1) {
e718bd18
FB
618 return null;
619 }
598c57f6 620 return array_pop($job);
e718bd18
FB
621 }
622
4d1f0f6b
RB
623 /* Mentoring
624 */
625 private $mentor_sectors = null;
626 public function setMentoringSectors(ProfileMentoringSectors $sectors)
627 {
628 $this->mentor_sectors = $sectors;
629 }
630
631 public function getMentoringSectors()
632 {
633 if ($this->mentor_sectors == null && !$this->fetched(self::FETCH_MENTOR_SECTOR)) {
634 $this->setMentoringSectors($this->getProfileField(self::FETCH_MENTOR_SECTOR));
635 }
636
637 if ($this->mentor_sectors == null) {
638 return array();
639 } else {
640 return $this->mentor_sectors->sectors;
641 }
642 }
643
644 private $mentor_countries = null;
645 public function setMentoringCountries(ProfileMentoringCountries $countries)
646 {
647 $this->mentor_countries = $countries;
648 }
649
650 public function getMentoringCountries()
651 {
652 if ($this->mentor_countries == null && !$this->fetched(self::FETCH_MENTOR_COUNTRY)) {
653 $this->setMentoringCountries($this->getProfileField(self::FETCH_MENTOR_COUNTRY));
654 }
655
656 if ($this->mentor_countries == null) {
657 return array();
658 } else {
659 return $this->mentor_countries->countries;
660 }
661 }
662
5c005b37
RB
663 /* Binets
664 */
665 public function getBinets()
666 {
875a88d3
RB
667 if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) {
668 return XDB::fetchColumn('SELECT binet_id
669 FROM profile_binets
670 WHERE pid = {?}', $this->id());
671 } else {
672 return array();
673 }
5c005b37 674 }
97b71de5
RB
675 public function getBinetsNames()
676 {
875a88d3
RB
677 if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) {
678 return XDB::fetchColumn('SELECT text
679 FROM profile_binets AS pb
680 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
681 WHERE pb.pid = {?}', $this->id());
682 } else {
683 return array();
684 }
97b71de5 685 }
5c005b37 686
26ca919b
RB
687 /* Medals
688 */
689 private $medals = null;
690 public function setMedals(ProfileMedals $medals)
691 {
692 $this->medals = $medals;
693 }
694
695 public function getMedals()
696 {
1f8250e4
RB
697 if ($this->medals == null && !$this->fetched(self::FETCH_MEDALS)) {
698 $this->setMedals($this->getProfileField(self::FETCH_MEDALS));
26ca919b
RB
699 }
700 if ($this->medals == null) {
701 return array();
702 }
703 return $this->medals->medals;
704 }
e718bd18 705
94590511
SJ
706 public function compareNames($firstname, $lastname)
707 {
708 $_lastname = mb_strtoupper($this->lastName());
709 $_firstname = mb_strtoupper($this->firstName());
710 $lastname = mb_strtoupper($lastname);
711 $firstname = mb_strtoupper($firstname);
712
713 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
714 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY);
715 $maxlen = 0;
716
717 foreach ($tokens as $str) {
718 $isOk &= (strpos($_lastname, $str) !== false);
719 $maxlen = max($maxlen, strlen($str));
720 }
721
722 return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname)));
723 }
724
9f21bd15 725 private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null)
b774ddab
FB
726 {
727 if (count($pids) == 0) {
728 return array();
729 }
0d906109
RB
730
731 if ($respect_order) {
732 $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids);
733 } else {
734 $order = '';
735 }
736
1a9affb7 737 $visibility = new ProfileVisibility($visibility);
9f21bd15 738
3e2442cd
RB
739 $it = XDB::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
740 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
741 p.cv, p.medals_pub, p.alias_pub, p.email_directory, p.last_change,
742 p.nationality1, p.nationality2, p.nationality3,
743 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
744 pe.entry_year, pe.grad_year,
875a88d3 745 IF ({?} IN {?}, pse.text, NULL) AS section,
9f21bd15
RB
746 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
747 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
748 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
749 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
1a9affb7
RB
750 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
751 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
7988f7d6 752 ph.x AS photo_width, ph.y AS photo_height,
9f21bd15 753 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
bdef0d33 754 pm.expertise AS mentor_expertise,
9f21bd15
RB
755 ap.uid AS owner_id
756 FROM profiles AS p
757 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
758 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
97b71de5 759 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
9f21bd15
RB
760 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
761 AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
762 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
763 AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
764 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
765 AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
766 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
767 AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
768 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
769 AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
770 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
771 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
bdef0d33 772 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
9f21bd15 773 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
3e2442cd 774 WHERE p.pid IN {?}
9f21bd15 775 GROUP BY p.pid
3e2442cd
RB
776 ' . $order,
777 $visibility->levels(),
778 ProfileVisibility::VIS_PRIVATE, $visibility->levels(),
779 $visibility->levels(), $visibility->levels(),
780 $pids
781 );
31ef12ef 782 return new ProfileIterator($it, $pids, $fields, $visibility);
b774ddab
FB
783 }
784
785 public static function getPID($login)
786 {
787 if ($login instanceof PlUser) {
788 return XDB::fetchOneCell('SELECT pid
789 FROM account_profiles
790 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
791 $login->id());
9f21bd15 792 } else if (ctype_digit($login)) {
b774ddab
FB
793 return XDB::fetchOneCell('SELECT pid
794 FROM profiles
795 WHERE pid = {?}', $login);
796 } else {
797 return XDB::fetchOneCell('SELECT pid
798 FROM profiles
799 WHERE hrpid = {?}', $login);
800 }
801 }
802
0d906109
RB
803 public static function getPIDsFromUIDs($uids, $respect_order = true)
804 {
805 if ($respect_order) {
806 $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids);
807 } else {
808 $order = '';
809 }
810 return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
811 FROM account_profiles AS ap
812 WHERE FIND_IN_SET(\'owner\', ap.perms)
9f21bd15
RB
813 AND ap.uid IN ' . XDB::formatArray($uids) .'
814 ' . $order);
0d906109 815 }
b774ddab 816
e7b93962
FB
817 /** Return the profile associated with the given login.
818 */
9f21bd15 819 public static function get($login, $fields = 0x0000, $visibility = null)
a3118782 820 {
641f2115 821 if (is_array($login)) {
1a9affb7
RB
822 $pf = new Profile($login);
823 $pf->setVisibilityLevel($visibility);
824 return $pf;
641f2115 825 }
b774ddab
FB
826 $pid = self::getPID($login);
827 if (!is_null($pid)) {
9f21bd15 828 $it = self::iterOverPIDs(array($pid), false, $fields, $visibility);
0d906109 829 return $it->next();
b774ddab 830 } else {
efe597c5
FB
831 /* Let say we can identify a profile using the identifiers of its owner.
832 */
455ea0c9
FB
833 if (!($login instanceof PlUser)) {
834 $user = User::getSilent($login);
835 if ($user && $user->hasProfile()) {
836 return $user->profile();
837 }
efe597c5 838 }
3e53a496 839 return null;
e7b93962
FB
840 }
841 }
a3118782 842
9f21bd15 843 public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 844 {
9f21bd15 845 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
0d906109
RB
846 }
847
9f21bd15 848 public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 849 {
9f21bd15 850 return self::fetchProfileData($pids, $respect_order, $fields, $visibility);
0d906109
RB
851 }
852
b774ddab
FB
853 /** Return profiles for the list of pids.
854 */
1a9affb7 855 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null)
b774ddab
FB
856 {
857 if (count($pids) == 0) {
858 return array();
859 }
9f21bd15 860 $it = self::iterOverPIDs($pids, true, $fields, $visibility);
b774ddab 861 $profiles = array();
0d906109
RB
862 while ($p = $it->next()) {
863 $profiles[$p->id()] = $p;
b774ddab
FB
864 }
865 return $profiles;
866 }
867
868 /** Return profiles for uids.
869 */
9f21bd15 870 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null)
b774ddab
FB
871 {
872 if (count($uids) == 0) {
873 return array();
874 }
9f21bd15 875 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility);
b774ddab
FB
876 }
877
913a4e90
RB
878 public static function isDisplayName($name)
879 {
880 return $name == self::DN_FULL || $name == self::DN_DISPLAY
881 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
882 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
883 || $name == self::DN_SHORT || $name == self::DN_SORT;
884 }
885
9f21bd15
RB
886 public static function getNameTypeId($type, $for_sql = false)
887 {
888 if (!S::has('name_types')) {
889 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
890 FROM profile_name_enum');
891 S::set('name_types', $table);
892 } else {
893 $table = S::v('name_types');
894 }
895 if ($for_sql) {
896 return XDB::escape($table[$type]);
897 } else {
898 return $table[$type];
899 }
900 }
901
726eaf7a
SJ
902 public static function rebuildSearchTokens($pid)
903 {
904 XDB::execute('DELETE FROM search_name
6dbb167e 905 WHERE pid = {?}',
726eaf7a
SJ
906 $pid);
907 $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
908 FIND_IN_SET('public', e.flags) AS public
909 FROM profile_name AS n
910 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
911 WHERE n.pid = {?}",
912 $pid);
913
2f62eba7 914 while ($key = $keys->next()) {
726eaf7a
SJ
915 if ($key['name'] == '') {
916 continue;
917 }
918 $toks = preg_split('/[ \'\-]+/', $key['name']);
919 $token = '';
920 $first = 5;
921 while ($toks) {
922 $token = strtolower(replace_accent(array_pop($toks) . $token));
923 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
6dbb167e 924 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
726eaf7a 925 VALUES ({?}, {?}, {?}, {?}, {?})',
6dbb167e 926 $token, $pid, soundex_fr($token), $score, $key['public']);
726eaf7a
SJ
927 $first = 0;
928 }
929 }
69b46857
SJ
930 }
931
932 /** The school identifier consists of 6 digits. The first 3 represent the
933 * promotion entry year. The last 3 indicate the student's rank.
934 *
935 * Our identifier consists of 8 digits and both half have the same role.
936 * This enables us to deal with bigger promotions and with a wider range
937 * of promotions.
938 *
939 * getSchoolId returns a school identifier given one of ours.
940 * getXorgId returns a X.org identifier given a school identifier.
941 */
942 public static function getSchoolId($xorgId)
943 {
944 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
945 return null;
946 }
947
948 $year = intval(substr($xorgId, 0, 4));
949 $rank = intval(substr($xorgId, 5, 3));
950 if ($year < 1996) {
951 return null;
952 } elseif ($year < 2000) {
953 $year = intval(substr(1900 - $year, 1, 3));
954 return sprintf('%02u0%03u', $year, $rank);
955 } else {
956 $year = intval(substr(1900 - $year, 1, 3));
957 return sprintf('%03u%03u', $year, $rank);
958 }
959 }
726eaf7a 960
69b46857
SJ
961 public static function getXorgId($schoolId)
962 {
94590511 963 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
a292484d
SJ
964 return null;
965 }
966
69b46857
SJ
967 $year = intval(substr($schoolId, 0, 3));
968 $rank = intval(substr($schoolId, 3, 3));
726eaf7a 969
69b46857
SJ
970 if ($year > 200) {
971 $year /= 10;
972 }
973 if ($year < 96) {
974 return null;
975 } else {
976 return sprintf('%04u%04u', 1900 + $year, $rank);
977 }
726eaf7a 978 }
e7b93962
FB
979}
980
31ef12ef
RB
981
982/** Iterator over a set of Profiles
983 */
984class ProfileIterator implements PlIterator
9f21bd15
RB
985{
986 private $iterator = null;
987 private $fields;
1a9affb7 988 private $visibility;
9f21bd15 989
1a9affb7 990 public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null)
9f21bd15
RB
991 {
992 require_once 'profilefields.inc.php';
1a9affb7
RB
993
994 if ($visibility == null) {
995 $visibility = new ProfileVisibility();
996 }
997
9f21bd15 998 $this->fields = $fields;
1a9affb7 999 $this->visibility = $visibility;
9f21bd15
RB
1000
1001 $subits = array();
1002 $callbacks = array();
1003
1004 $subits[0] = $it;
1005 $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid');
1006 $cb = PlIteratorUtils::objectPropertyCallback('pid');
1007
1008 if ($fields & Profile::FETCH_ADDRESSES) {
1009 $callbacks[Profile::FETCH_ADDRESSES] = $cb;
1010 $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
1011 }
1012
1013 if ($fields & Profile::FETCH_CORPS) {
1014 $callbacks[Profile::FETCH_CORPS] = $cb;
1015 $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
1016 }
1017
1018 if ($fields & Profile::FETCH_EDU) {
1019 $callbacks[Profile::FETCH_EDU] = $cb;
1020 $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
1021 }
1022
1023 if ($fields & Profile::FETCH_JOBS) {
1024 $callbacks[Profile::FETCH_JOBS] = $cb;
1025 $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
1026 }
1027
1028 if ($fields & Profile::FETCH_MEDALS) {
1029 $callbacks[Profile::FETCH_MEDALS] = $cb;
1030 $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
1031 }
1032
1033 if ($fields & Profile::FETCH_NETWORKING) {
1034 $callbacks[Profile::FETCH_NETWORKING] = $cb;
1035 $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
1036 }
1037
1038 if ($fields & Profile::FETCH_PHONES) {
1039 $callbacks[Profile::FETCH_PHONES] = $cb;
1040 $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1041 }
1042
9f21bd15
RB
1043 $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0);
1044 }
1045
1f8250e4 1046 private function hasData($field, $vals)
9f21bd15 1047 {
1f8250e4 1048 return ($this->fields & $field) && ($vals[$field] != null);
9f21bd15
RB
1049 }
1050
1051 private function fillProfile(array $vals)
1052 {
9f21bd15 1053 $pf = Profile::get($vals[0]);
1a9affb7 1054 $pf->setVisibilityLevel($this->visibility->level());
1f8250e4 1055 $pf->setFetchedFields($this->fields);
1a9affb7 1056
8cf886dc
RB
1057 if ($this->hasData(Profile::FETCH_PHONES, $vals)) {
1058 $pf->setPhones($vals[Profile::FETCH_PHONES]);
9f21bd15 1059 }
8cf886dc
RB
1060 if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) {
1061 $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]);
1062 }
1063 if ($this->hasData(Profile::FETCH_JOBS, $vals)) {
1064 $pf->setJobs($vals[Profile::FETCH_JOBS]);
1065 }
1066
1067 if ($this->hasData(Profile::FETCH_CORPS, $vals)) {
9f21bd15
RB
1068 $pf->setCorps($vals[Profile::FETCH_CORPS]);
1069 }
8cf886dc 1070 if ($this->hasData(Profile::FETCH_EDU, $vals)) {
26ca919b 1071 $pf->setEducations($vals[Profile::FETCH_EDU]);
9f21bd15 1072 }
8cf886dc 1073 if ($this->hasData(Profile::FETCH_MEDALS, $vals)) {
9f21bd15
RB
1074 $pf->setMedals($vals[Profile::FETCH_MEDALS]);
1075 }
8cf886dc 1076 if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) {
9f21bd15
RB
1077 $pf->setNetworking($vals[Profile::FETCH_NETWORKING]);
1078 }
9f21bd15
RB
1079
1080 return $pf;
1081 }
1082
1083 public function next()
1084 {
1085 $vals = $this->iterator->next();
1086 if ($vals == null) {
1087 return null;
1088 }
1089 return $this->fillProfile($vals);
1090 }
1091
1092 public function first()
1093 {
1094 return $this->iterator->first();
1095 }
1096
1097 public function last()
1098 {
1099 return $this->iterator->last();
1100 }
1101
1102 public function total()
1103 {
1104 return $this->iterator->total();
1105 }
1106}
1107
e7b93962
FB
1108// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1109?>