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