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