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