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