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