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