Removes entreprise name from validation email.
[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
1f8250e4
RB
415 /** Stores the list of fields which have already been fetched for this Profile
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
426 private function fetched($field)
427 {
3ac45f10
PC
428 if (($fields | self::FETCH_ALL) != self::FETCH_ALL) {
429 Platal::page()->kill("Invalid fetched fields: $fields");
1f8250e4
RB
430 }
431
432 return ($this->fetched_fields & $field);
433 }
434
435 /** If not already done, fetches data for the given field
436 * @param $field One of the Profile::FETCH_*
437 * @return A ProfileField, or null
438 */
439 private function getProfileField($field)
440 {
3ac45f10
PC
441 if (!array_key_exists($field, ProfileField::$fields)) {
442 Platal::page()->kill("Invalid field: $field");
443 }
1f8250e4
RB
444 if ($this->fetched($field)) {
445 return null;
446 } else {
447 $this->fetched_fields = $this->fetched_fields | $field;
448 }
449
450 $cls = ProfileField::$fields[$field];
451
990cb17b
RB
452 return ProfileField::getForPID($cls, $this->id(), $this->visibility);
453 }
454
8cf886dc
RB
455 /** Consolidates internal data (addresses, phones, jobs)
456 */
457 private function consolidateFields()
458 {
459 if ($this->phones != null) {
460 if ($this->addresses != null) {
461 $this->addresses->addPhones($this->phones);
462 }
463
464 if ($this->jobs != null) {
465 $this->jobs->addPhones($this->phones);
466 }
467 }
468
469 if ($this->addresses != null && $this->jobs != null) {
470 $this->jobs->addAddresses($this->addresses);
471 }
3ac45f10
PC
472 if ($this->jobs != null && $this->jobterms != null) {
473 $this->jobs->addJobTerms($this->jobterms);
474 }
8cf886dc
RB
475 }
476
833a6e86
FB
477 /* Photo
478 */
1a9affb7 479 private $photo = null;
7988f7d6 480 public function getPhoto($fallback = true, $data = false)
9f21bd15 481 {
1a9affb7
RB
482 if ($this->has_photo) {
483 if ($data && ($this->photo == null || $this->photo->mimeType == null)) {
484 $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y
1f8250e4
RB
485 FROM profile_photos
486 WHERE pid = {?}', $this->pid);
1a9affb7
RB
487 $this->photo = PlImage::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
488 } else if ($this->photo == null) {
489 $this->photo = PlImage::fromData(null, null, $this->photo_width, $this->photo_height);
490 }
491 return $this->photo;
9f21bd15
RB
492 } else if ($fallback) {
493 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png',
494 'image/png');
495 }
496 return null;
497 }
7d0ebdf5 498
4bc5b8f0
FB
499 /* Addresses
500 */
7d0ebdf5
RB
501 private $addresses = null;
502 public function setAddresses(ProfileAddresses $addr)
503 {
504 $this->addresses = $addr;
8cf886dc 505 $this->consolidateFields();
7d0ebdf5
RB
506 }
507
dbcc3b3d 508 private function fetchAddresses()
f5642983 509 {
1f8250e4 510 if ($this->addresses == null && !$this->fetched(self::FETCH_ADDRESSES)) {
61ee68c7
FB
511 $addr = $this->getProfileField(self::FETCH_ADDRESSES);
512 if ($addr) {
513 $this->setAddresses($addr);
dbcc3b3d 514 $this->fetchPhones();
61ee68c7 515 }
f5642983 516 }
dbcc3b3d
RB
517 }
518
519 public function getAddresses($flags, $limit = null)
520 {
521 $this->fetchAddresses();
0907501b
RB
522
523 if ($this->addresses == null) {
598c57f6 524 return array();
0907501b 525 }
990cb17b 526 return $this->addresses->get($flags, $limit);
f5642983
FB
527 }
528
598c57f6
RB
529 public function iterAddresses($flags, $limit = null)
530 {
531 return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true);
532 }
533
f5642983
FB
534 public function getMainAddress()
535 {
598c57f6
RB
536 $addr = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
537 if (count($addr) == 0) {
f5642983
FB
538 return null;
539 } else {
598c57f6 540 return array_pop($addr);
f5642983
FB
541 }
542 }
3e53a496 543
bdef0d33
RB
544 /* Phones
545 */
546 private $phones = null;
547 public function setPhones(ProfilePhones $phones)
548 {
549 $this->phones = $phones;
550 $this->consolidateFields();
551 }
552
dbcc3b3d 553 private function fetchPhones()
bdef0d33 554 {
1f8250e4 555 if ($this->phones == null && !$this->fetched(self::FETCH_PHONES)) {
56721636
PC
556 $phones = $this->getProfileField(self::FETCH_PHONES);
557 if (isset($phones)) {
558 $this->setPhones($phones);
559 }
bdef0d33 560 }
dbcc3b3d 561 }
bdef0d33 562
dbcc3b3d
RB
563 public function getPhones($flags, $limit = null)
564 {
565 $this->fetchPhones();
bdef0d33 566 if ($this->phones == null) {
598c57f6 567 return array();
bdef0d33
RB
568 }
569 return $this->phones->get($flags, $limit);
570 }
4bc5b8f0
FB
571
572 /* Educations
573 */
d4d395bb 574 private $educations = null;
a060e1c3
RB
575 public function setEducations(ProfileEducation $edu)
576 {
577 $this->educations = $edu;
578 }
579
4bc5b8f0
FB
580 public function getEducations($flags, $limit = null)
581 {
1f8250e4
RB
582 if ($this->educations == null && !$this->fetched(self::FETCH_EDU)) {
583 $this->setEducations($this->getProfileField(self::FETCH_EDU));
d4d395bb 584 }
0907501b
RB
585
586 if ($this->educations == null) {
598c57f6 587 return array();
0907501b 588 }
a060e1c3 589 return $this->educations->get($flags, $limit);
4bc5b8f0
FB
590 }
591
592 public function getExtraEducations($limit = null)
593 {
594 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
595 }
596
0396c259
RB
597 /* Corps
598 */
599 private $corps = null;
600 public function setCorps(ProfileCorps $corps)
601 {
602 $this->corps = $corps;
603 }
604
605 public function getCorps()
606 {
1f8250e4
RB
607 if ($this->corps == null && !$this->fetched(self::FETCH_CORPS)) {
608 $this->setCorps($this->getProfileField(self::FETCH_CORPS));
0396c259
RB
609 }
610 return $this->corps;
611 }
4bc5b8f0 612
04a94b1d
FB
613 /** Networking
614 */
d4d395bb
RB
615 private $networks = null;
616 public function setNetworking(ProfileNetworking $nw)
617 {
618 $this->networks = $nw;
619 }
04a94b1d
FB
620
621 public function getNetworking($flags, $limit = null)
622 {
1f8250e4 623 if ($this->networks == null && !$this->fetched(self::FETCH_NETWORKING)) {
dad85695
FB
624 $nw = $this->getProfileField(self::FETCH_NETWORKING);
625 if ($nw) {
626 $this->setNetworking($nw);
627 }
04a94b1d 628 }
0907501b 629 if ($this->networks == null) {
598c57f6 630 return array();
0907501b 631 }
d4d395bb 632 return $this->networks->get($flags, $limit);
04a94b1d
FB
633 }
634
635 public function getWebSite()
636 {
637 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
598c57f6 638 if (count($site) != 1) {
04a94b1d
FB
639 return null;
640 }
598c57f6 641 $site = array_pop($site);
dad85695 642 return $site;
04a94b1d
FB
643 }
644
645
e718bd18
FB
646 /** Jobs
647 */
949fc736
RB
648 private $jobs = null;
649 public function setJobs(ProfileJobs $jobs)
650 {
651 $this->jobs = $jobs;
652 $this->consolidateFields();
653 }
e718bd18 654
dbcc3b3d 655 private function fetchJobs()
e718bd18 656 {
1f8250e4 657 if ($this->jobs == null && !$this->fetched(self::FETCH_JOBS)) {
61ee68c7
FB
658 $jobs = $this->getProfileField(self::FETCH_JOBS);
659 if ($jobs) {
660 $this->setJobs($jobs);
dbcc3b3d 661 $this->fetchAddresses();
61ee68c7 662 }
e718bd18 663 }
dbcc3b3d
RB
664 }
665
666 public function getJobs($flags, $limit = null)
667 {
668 $this->fetchJobs();
949fc736 669
0907501b 670 if ($this->jobs == null) {
598c57f6 671 return array();
0907501b 672 }
949fc736 673 return $this->jobs->get($flags, $limit);
e718bd18
FB
674 }
675
44ec5eb5 676 public function getMainJob()
e718bd18
FB
677 {
678 $job = $this->getJobs(self::JOBS_MAIN, 1);
598c57f6 679 if (count($job) != 1) {
e718bd18
FB
680 return null;
681 }
598c57f6 682 return array_pop($job);
e718bd18
FB
683 }
684
3ac45f10
PC
685 /** JobTerms
686 */
687 private $jobterms = null;
688 public function setJobTerms(ProfileJobTerms $jobterms)
689 {
690 $this->jobterms = $jobterms;
691 $this->consolidateFields();
692 }
693
4d1f0f6b
RB
694 /* Mentoring
695 */
696 private $mentor_sectors = null;
697 public function setMentoringSectors(ProfileMentoringSectors $sectors)
698 {
699 $this->mentor_sectors = $sectors;
700 }
701
702 public function getMentoringSectors()
703 {
704 if ($this->mentor_sectors == null && !$this->fetched(self::FETCH_MENTOR_SECTOR)) {
705 $this->setMentoringSectors($this->getProfileField(self::FETCH_MENTOR_SECTOR));
706 }
707
708 if ($this->mentor_sectors == null) {
709 return array();
710 } else {
711 return $this->mentor_sectors->sectors;
712 }
713 }
714
715 private $mentor_countries = null;
716 public function setMentoringCountries(ProfileMentoringCountries $countries)
717 {
718 $this->mentor_countries = $countries;
719 }
720
721 public function getMentoringCountries()
722 {
723 if ($this->mentor_countries == null && !$this->fetched(self::FETCH_MENTOR_COUNTRY)) {
724 $this->setMentoringCountries($this->getProfileField(self::FETCH_MENTOR_COUNTRY));
725 }
726
727 if ($this->mentor_countries == null) {
728 return array();
729 } else {
730 return $this->mentor_countries->countries;
731 }
732 }
733
3ac45f10
PC
734 /** List of job terms to specify mentoring */
735 private $mentor_terms = null;
736 /**
737 * set job terms to specify mentoring
738 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
739 */
740 public function setMentoringTerms(ProfileMentoringTerms $terms)
741 {
742 $this->mentor_terms = $terms;
743 }
744 /**
745 * get all job terms that specify mentoring
746 * @return an array of JobTerms objects
747 */
748 public function getMentoringTerms()
749 {
750 if ($this->mentor_terms == null && !$this->fetched(self::FETCH_MENTOR_TERMS)) {
751 $this->setMentoringTerms($this->getProfileField(self::FETCH_MENTOR_TERMS));
752 }
753
754 if ($this->mentor_terms == null) {
755 return array();
756 } else {
757 return $this->mentor_terms->get();
758 }
759 }
760
761
5c005b37
RB
762 /* Binets
763 */
764 public function getBinets()
765 {
875a88d3
RB
766 if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) {
767 return XDB::fetchColumn('SELECT binet_id
768 FROM profile_binets
769 WHERE pid = {?}', $this->id());
770 } else {
771 return array();
772 }
5c005b37 773 }
97b71de5
RB
774 public function getBinetsNames()
775 {
875a88d3
RB
776 if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) {
777 return XDB::fetchColumn('SELECT text
778 FROM profile_binets AS pb
779 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
780 WHERE pb.pid = {?}', $this->id());
781 } else {
782 return array();
783 }
97b71de5 784 }
5c005b37 785
26ca919b
RB
786 /* Medals
787 */
788 private $medals = null;
789 public function setMedals(ProfileMedals $medals)
790 {
791 $this->medals = $medals;
792 }
793
794 public function getMedals()
795 {
1f8250e4
RB
796 if ($this->medals == null && !$this->fetched(self::FETCH_MEDALS)) {
797 $this->setMedals($this->getProfileField(self::FETCH_MEDALS));
26ca919b
RB
798 }
799 if ($this->medals == null) {
800 return array();
801 }
802 return $this->medals->medals;
803 }
e718bd18 804
94590511
SJ
805 public function compareNames($firstname, $lastname)
806 {
807 $_lastname = mb_strtoupper($this->lastName());
808 $_firstname = mb_strtoupper($this->firstName());
809 $lastname = mb_strtoupper($lastname);
810 $firstname = mb_strtoupper($firstname);
811
812 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
813 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY);
814 $maxlen = 0;
815
816 foreach ($tokens as $str) {
817 $isOk &= (strpos($_lastname, $str) !== false);
818 $maxlen = max($maxlen, strlen($str));
819 }
820
821 return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname)));
822 }
823
9f21bd15 824 private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null)
b774ddab
FB
825 {
826 if (count($pids) == 0) {
7a8da8e8 827 return null;
b774ddab 828 }
0d906109
RB
829
830 if ($respect_order) {
831 $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids);
832 } else {
833 $order = '';
834 }
835
1a9affb7 836 $visibility = new ProfileVisibility($visibility);
9f21bd15 837
3e2442cd
RB
838 $it = XDB::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
839 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
77d2ae52
RB
840 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
841 p.last_change, p.nationality1, p.nationality2, p.nationality3,
3e2442cd
RB
842 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
843 pe.entry_year, pe.grad_year,
348b3844
RB
844 IF ({?}, pse.text, NULL) AS section,
845 pn_f.name AS firstname, pn_l.name AS lastname,
846 IF( {?}, pn_n.name, NULL) AS nickname,
9f21bd15
RB
847 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
848 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
09e54905
SJ
849 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
850 pd.directory_name, pd.public_name, pd.private_name,
851 IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
1a9affb7 852 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
7988f7d6 853 ph.x AS photo_width, ph.y AS photo_height,
9f21bd15 854 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
bdef0d33 855 pm.expertise AS mentor_expertise,
9f21bd15
RB
856 ap.uid AS owner_id
857 FROM profiles AS p
858 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
859 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
97b71de5 860 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
9f21bd15
RB
861 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
862 AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
863 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
864 AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
865 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
866 AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
867 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
868 AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
869 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
870 AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
871 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
872 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
bdef0d33 873 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
9f21bd15 874 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
3e2442cd 875 WHERE p.pid IN {?}
9f21bd15 876 GROUP BY p.pid
3e2442cd 877 ' . $order,
77d2ae52
RB
878 $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // CV
879 $visibility->levels(), // freetext
880 $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // section
881 $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // nickname
882 $visibility->levels(), // mobile
883 $visibility->levels(), // photo
3e2442cd
RB
884 $pids
885 );
31ef12ef 886 return new ProfileIterator($it, $pids, $fields, $visibility);
b774ddab
FB
887 }
888
889 public static function getPID($login)
890 {
891 if ($login instanceof PlUser) {
892 return XDB::fetchOneCell('SELECT pid
893 FROM account_profiles
894 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
895 $login->id());
9f21bd15 896 } else if (ctype_digit($login)) {
b774ddab
FB
897 return XDB::fetchOneCell('SELECT pid
898 FROM profiles
899 WHERE pid = {?}', $login);
900 } else {
901 return XDB::fetchOneCell('SELECT pid
902 FROM profiles
903 WHERE hrpid = {?}', $login);
904 }
905 }
906
0d906109
RB
907 public static function getPIDsFromUIDs($uids, $respect_order = true)
908 {
909 if ($respect_order) {
910 $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids);
911 } else {
912 $order = '';
913 }
914 return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
915 FROM account_profiles AS ap
916 WHERE FIND_IN_SET(\'owner\', ap.perms)
9f21bd15
RB
917 AND ap.uid IN ' . XDB::formatArray($uids) .'
918 ' . $order);
0d906109 919 }
b774ddab 920
e7b93962
FB
921 /** Return the profile associated with the given login.
922 */
9f21bd15 923 public static function get($login, $fields = 0x0000, $visibility = null)
a3118782 924 {
641f2115 925 if (is_array($login)) {
1a9affb7
RB
926 $pf = new Profile($login);
927 $pf->setVisibilityLevel($visibility);
928 return $pf;
641f2115 929 }
b774ddab
FB
930 $pid = self::getPID($login);
931 if (!is_null($pid)) {
9f21bd15 932 $it = self::iterOverPIDs(array($pid), false, $fields, $visibility);
0d906109 933 return $it->next();
b774ddab 934 } else {
efe597c5
FB
935 /* Let say we can identify a profile using the identifiers of its owner.
936 */
455ea0c9
FB
937 if (!($login instanceof PlUser)) {
938 $user = User::getSilent($login);
939 if ($user && $user->hasProfile()) {
940 return $user->profile();
941 }
efe597c5 942 }
3e53a496 943 return null;
e7b93962
FB
944 }
945 }
a3118782 946
9f21bd15 947 public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 948 {
9f21bd15 949 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
0d906109
RB
950 }
951
9f21bd15 952 public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 953 {
9f21bd15 954 return self::fetchProfileData($pids, $respect_order, $fields, $visibility);
0d906109
RB
955 }
956
b774ddab
FB
957 /** Return profiles for the list of pids.
958 */
1a9affb7 959 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null)
b774ddab
FB
960 {
961 if (count($pids) == 0) {
962 return array();
963 }
9f21bd15 964 $it = self::iterOverPIDs($pids, true, $fields, $visibility);
b774ddab 965 $profiles = array();
0d906109
RB
966 while ($p = $it->next()) {
967 $profiles[$p->id()] = $p;
b774ddab
FB
968 }
969 return $profiles;
970 }
971
972 /** Return profiles for uids.
973 */
9f21bd15 974 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null)
b774ddab
FB
975 {
976 if (count($uids) == 0) {
977 return array();
978 }
9f21bd15 979 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility);
b774ddab
FB
980 }
981
913a4e90
RB
982 public static function isDisplayName($name)
983 {
984 return $name == self::DN_FULL || $name == self::DN_DISPLAY
985 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
986 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
987 || $name == self::DN_SHORT || $name == self::DN_SORT;
988 }
989
a9ef52c9
RB
990 /** Returns the closest "accounts only" name type for $name
991 */
992 public static function getAccountEquivalentName($name)
993 {
994 switch ($name)
995 {
996 case self::DN_DIRECTORY:
997 case self::DN_SORT:
998 return 'directory_name';
999 case self::DN_FULL:
1000 case self::DN_PUBLIC:
1001 return 'full_name';
1002 case self::DN_PRIVATE:
1003 case self::DN_SHORT:
1004 case self::DN_YOURSELF:
1005 default:
1006 return 'display_name';
1007 }
1008 }
1009
9f21bd15
RB
1010 public static function getNameTypeId($type, $for_sql = false)
1011 {
1012 if (!S::has('name_types')) {
1013 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
1014 FROM profile_name_enum');
1015 S::set('name_types', $table);
1016 } else {
1017 $table = S::v('name_types');
1018 }
1019 if ($for_sql) {
1020 return XDB::escape($table[$type]);
1021 } else {
1022 return $table[$type];
1023 }
1024 }
1025
726eaf7a
SJ
1026 public static function rebuildSearchTokens($pid)
1027 {
1028 XDB::execute('DELETE FROM search_name
6dbb167e 1029 WHERE pid = {?}',
726eaf7a
SJ
1030 $pid);
1031 $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
1032 FIND_IN_SET('public', e.flags) AS public
1033 FROM profile_name AS n
1034 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1035 WHERE n.pid = {?}",
1036 $pid);
1037
2f62eba7 1038 while ($key = $keys->next()) {
726eaf7a
SJ
1039 if ($key['name'] == '') {
1040 continue;
1041 }
1042 $toks = preg_split('/[ \'\-]+/', $key['name']);
1043 $token = '';
1044 $first = 5;
1045 while ($toks) {
1046 $token = strtolower(replace_accent(array_pop($toks) . $token));
1047 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
6dbb167e 1048 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
726eaf7a 1049 VALUES ({?}, {?}, {?}, {?}, {?})',
6dbb167e 1050 $token, $pid, soundex_fr($token), $score, $key['public']);
726eaf7a
SJ
1051 $first = 0;
1052 }
1053 }
69b46857
SJ
1054 }
1055
1056 /** The school identifier consists of 6 digits. The first 3 represent the
1057 * promotion entry year. The last 3 indicate the student's rank.
aab2ffdd 1058 *
69b46857
SJ
1059 * Our identifier consists of 8 digits and both half have the same role.
1060 * This enables us to deal with bigger promotions and with a wider range
1061 * of promotions.
1062 *
1063 * getSchoolId returns a school identifier given one of ours.
1064 * getXorgId returns a X.org identifier given a school identifier.
1065 */
1066 public static function getSchoolId($xorgId)
1067 {
1068 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1069 return null;
1070 }
1071
1072 $year = intval(substr($xorgId, 0, 4));
1073 $rank = intval(substr($xorgId, 5, 3));
1074 if ($year < 1996) {
1075 return null;
1076 } elseif ($year < 2000) {
1077 $year = intval(substr(1900 - $year, 1, 3));
1078 return sprintf('%02u0%03u', $year, $rank);
1079 } else {
1080 $year = intval(substr(1900 - $year, 1, 3));
1081 return sprintf('%03u%03u', $year, $rank);
1082 }
1083 }
726eaf7a 1084
69b46857
SJ
1085 public static function getXorgId($schoolId)
1086 {
94590511 1087 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
a292484d
SJ
1088 return null;
1089 }
1090
69b46857
SJ
1091 $year = intval(substr($schoolId, 0, 3));
1092 $rank = intval(substr($schoolId, 3, 3));
726eaf7a 1093
69b46857
SJ
1094 if ($year > 200) {
1095 $year /= 10;
1096 }
1097 if ($year < 96) {
1098 return null;
1099 } else {
1100 return sprintf('%04u%04u', 1900 + $year, $rank);
1101 }
726eaf7a 1102 }
e7b93962
FB
1103}
1104
31ef12ef
RB
1105
1106/** Iterator over a set of Profiles
1107 */
1108class ProfileIterator implements PlIterator
9f21bd15
RB
1109{
1110 private $iterator = null;
1111 private $fields;
1a9affb7 1112 private $visibility;
9f21bd15 1113
3ac45f10
PC
1114 const FETCH_ALL = 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1115
1a9affb7 1116 public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null)
9f21bd15
RB
1117 {
1118 require_once 'profilefields.inc.php';
1a9affb7
RB
1119
1120 if ($visibility == null) {
1121 $visibility = new ProfileVisibility();
1122 }
1123
9f21bd15 1124 $this->fields = $fields;
1a9affb7 1125 $this->visibility = $visibility;
9f21bd15
RB
1126
1127 $subits = array();
1128 $callbacks = array();
1129
1130 $subits[0] = $it;
1131 $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid');
1132 $cb = PlIteratorUtils::objectPropertyCallback('pid');
1133
3ac45f10
PC
1134 $fields = $fields & self::FETCH_ALL;
1135 for ($field = 1; $field < $fields; $field *= 2) {
1136 if (($fields & $field) ) {
1137 $callbacks[$field] = $cb;
1138 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1139 }
9f21bd15
RB
1140 }
1141
9f21bd15
RB
1142 $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0);
1143 }
1144
1f8250e4 1145 private function hasData($field, $vals)
9f21bd15 1146 {
1f8250e4 1147 return ($this->fields & $field) && ($vals[$field] != null);
9f21bd15
RB
1148 }
1149
1150 private function fillProfile(array $vals)
1151 {
9f21bd15 1152 $pf = Profile::get($vals[0]);
1a9affb7 1153 $pf->setVisibilityLevel($this->visibility->level());
1f8250e4 1154 $pf->setFetchedFields($this->fields);
1a9affb7 1155
8cf886dc
RB
1156 if ($this->hasData(Profile::FETCH_PHONES, $vals)) {
1157 $pf->setPhones($vals[Profile::FETCH_PHONES]);
9f21bd15 1158 }
8cf886dc
RB
1159 if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) {
1160 $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]);
1161 }
1162 if ($this->hasData(Profile::FETCH_JOBS, $vals)) {
1163 $pf->setJobs($vals[Profile::FETCH_JOBS]);
1164 }
3ac45f10
PC
1165 if ($this->hasData(Profile::FETCH_JOB_TERMS, $vals)) {
1166 $pf->setJobTerms($vals[Profile::FETCH_JOB_TERMS]);
1167 }
8cf886dc
RB
1168
1169 if ($this->hasData(Profile::FETCH_CORPS, $vals)) {
9f21bd15
RB
1170 $pf->setCorps($vals[Profile::FETCH_CORPS]);
1171 }
8cf886dc 1172 if ($this->hasData(Profile::FETCH_EDU, $vals)) {
26ca919b 1173 $pf->setEducations($vals[Profile::FETCH_EDU]);
9f21bd15 1174 }
8cf886dc 1175 if ($this->hasData(Profile::FETCH_MEDALS, $vals)) {
9f21bd15
RB
1176 $pf->setMedals($vals[Profile::FETCH_MEDALS]);
1177 }
8cf886dc 1178 if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) {
9f21bd15
RB
1179 $pf->setNetworking($vals[Profile::FETCH_NETWORKING]);
1180 }
9f21bd15
RB
1181
1182 return $pf;
1183 }
1184
1185 public function next()
1186 {
1187 $vals = $this->iterator->next();
1188 if ($vals == null) {
1189 return null;
1190 }
1191 return $this->fillProfile($vals);
1192 }
1193
1194 public function first()
1195 {
1196 return $this->iterator->first();
1197 }
1198
1199 public function last()
1200 {
1201 return $this->iterator->last();
1202 }
1203
1204 public function total()
1205 {
1206 return $this->iterator->total();
1207 }
1208}
1209
e7b93962
FB
1210// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1211?>