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