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