Set visibility for section and binets to private
[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
875a88d3
RB
712 $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
713 IF ({?} IN {?}, pse.text, NULL) AS section,
9f21bd15
RB
714 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
715 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
716 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
717 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
1a9affb7
RB
718 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
719 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
7988f7d6 720 ph.x AS photo_width, ph.y AS photo_height,
9f21bd15 721 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
bdef0d33 722 pm.expertise AS mentor_expertise,
9f21bd15
RB
723 ap.uid AS owner_id
724 FROM profiles AS p
725 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
726 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
97b71de5 727 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
9f21bd15
RB
728 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
729 AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
730 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
731 AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
732 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
733 AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
734 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
735 AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
736 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
737 AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
738 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
739 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
bdef0d33 740 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
9f21bd15
RB
741 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
742 WHERE p.pid IN ' . XDB::formatArray($pids) . '
743 GROUP BY p.pid
875a88d3 744 ' . $order, ProfileVisibility::VIS_PRIVATE, $visibility->levels(), $visibility->levels(), $visibility->levels());
31ef12ef 745 return new ProfileIterator($it, $pids, $fields, $visibility);
b774ddab
FB
746 }
747
748 public static function getPID($login)
749 {
750 if ($login instanceof PlUser) {
751 return XDB::fetchOneCell('SELECT pid
752 FROM account_profiles
753 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
754 $login->id());
9f21bd15 755 } else if (ctype_digit($login)) {
b774ddab
FB
756 return XDB::fetchOneCell('SELECT pid
757 FROM profiles
758 WHERE pid = {?}', $login);
759 } else {
760 return XDB::fetchOneCell('SELECT pid
761 FROM profiles
762 WHERE hrpid = {?}', $login);
763 }
764 }
765
0d906109
RB
766 public static function getPIDsFromUIDs($uids, $respect_order = true)
767 {
768 if ($respect_order) {
769 $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids);
770 } else {
771 $order = '';
772 }
773 return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
774 FROM account_profiles AS ap
775 WHERE FIND_IN_SET(\'owner\', ap.perms)
9f21bd15
RB
776 AND ap.uid IN ' . XDB::formatArray($uids) .'
777 ' . $order);
0d906109 778 }
b774ddab 779
e7b93962
FB
780 /** Return the profile associated with the given login.
781 */
9f21bd15 782 public static function get($login, $fields = 0x0000, $visibility = null)
a3118782 783 {
641f2115 784 if (is_array($login)) {
1a9affb7
RB
785 $pf = new Profile($login);
786 $pf->setVisibilityLevel($visibility);
787 return $pf;
641f2115 788 }
b774ddab
FB
789 $pid = self::getPID($login);
790 if (!is_null($pid)) {
9f21bd15 791 $it = self::iterOverPIDs(array($pid), false, $fields, $visibility);
0d906109 792 return $it->next();
b774ddab 793 } else {
efe597c5
FB
794 /* Let say we can identify a profile using the identifiers of its owner.
795 */
455ea0c9
FB
796 if (!($login instanceof PlUser)) {
797 $user = User::getSilent($login);
798 if ($user && $user->hasProfile()) {
799 return $user->profile();
800 }
efe597c5 801 }
3e53a496 802 return null;
e7b93962
FB
803 }
804 }
a3118782 805
9f21bd15 806 public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 807 {
9f21bd15 808 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
0d906109
RB
809 }
810
9f21bd15 811 public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 812 {
9f21bd15 813 return self::fetchProfileData($pids, $respect_order, $fields, $visibility);
0d906109
RB
814 }
815
b774ddab
FB
816 /** Return profiles for the list of pids.
817 */
1a9affb7 818 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null)
b774ddab
FB
819 {
820 if (count($pids) == 0) {
821 return array();
822 }
9f21bd15 823 $it = self::iterOverPIDs($pids, true, $fields, $visibility);
b774ddab 824 $profiles = array();
0d906109
RB
825 while ($p = $it->next()) {
826 $profiles[$p->id()] = $p;
b774ddab
FB
827 }
828 return $profiles;
829 }
830
831 /** Return profiles for uids.
832 */
9f21bd15 833 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null)
b774ddab
FB
834 {
835 if (count($uids) == 0) {
836 return array();
837 }
9f21bd15 838 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility);
b774ddab
FB
839 }
840
913a4e90
RB
841 public static function isDisplayName($name)
842 {
843 return $name == self::DN_FULL || $name == self::DN_DISPLAY
844 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
845 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
846 || $name == self::DN_SHORT || $name == self::DN_SORT;
847 }
848
9f21bd15
RB
849 public static function getNameTypeId($type, $for_sql = false)
850 {
851 if (!S::has('name_types')) {
852 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
853 FROM profile_name_enum');
854 S::set('name_types', $table);
855 } else {
856 $table = S::v('name_types');
857 }
858 if ($for_sql) {
859 return XDB::escape($table[$type]);
860 } else {
861 return $table[$type];
862 }
863 }
864
726eaf7a
SJ
865 public static function rebuildSearchTokens($pid)
866 {
867 XDB::execute('DELETE FROM search_name
6dbb167e 868 WHERE pid = {?}',
726eaf7a
SJ
869 $pid);
870 $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
871 FIND_IN_SET('public', e.flags) AS public
872 FROM profile_name AS n
873 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
874 WHERE n.pid = {?}",
875 $pid);
876
877 foreach ($keys as $i => $key) {
878 if ($key['name'] == '') {
879 continue;
880 }
881 $toks = preg_split('/[ \'\-]+/', $key['name']);
882 $token = '';
883 $first = 5;
884 while ($toks) {
885 $token = strtolower(replace_accent(array_pop($toks) . $token));
886 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
6dbb167e 887 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
726eaf7a 888 VALUES ({?}, {?}, {?}, {?}, {?})',
6dbb167e 889 $token, $pid, soundex_fr($token), $score, $key['public']);
726eaf7a
SJ
890 $first = 0;
891 }
892 }
69b46857
SJ
893 }
894
895 /** The school identifier consists of 6 digits. The first 3 represent the
896 * promotion entry year. The last 3 indicate the student's rank.
897 *
898 * Our identifier consists of 8 digits and both half have the same role.
899 * This enables us to deal with bigger promotions and with a wider range
900 * of promotions.
901 *
902 * getSchoolId returns a school identifier given one of ours.
903 * getXorgId returns a X.org identifier given a school identifier.
904 */
905 public static function getSchoolId($xorgId)
906 {
907 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
908 return null;
909 }
910
911 $year = intval(substr($xorgId, 0, 4));
912 $rank = intval(substr($xorgId, 5, 3));
913 if ($year < 1996) {
914 return null;
915 } elseif ($year < 2000) {
916 $year = intval(substr(1900 - $year, 1, 3));
917 return sprintf('%02u0%03u', $year, $rank);
918 } else {
919 $year = intval(substr(1900 - $year, 1, 3));
920 return sprintf('%03u%03u', $year, $rank);
921 }
922 }
726eaf7a 923
69b46857
SJ
924 public static function getXorgId($schoolId)
925 {
94590511 926 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
a292484d
SJ
927 return null;
928 }
929
69b46857
SJ
930 $year = intval(substr($schoolId, 0, 3));
931 $rank = intval(substr($schoolId, 3, 3));
726eaf7a 932
69b46857
SJ
933 if ($year > 200) {
934 $year /= 10;
935 }
936 if ($year < 96) {
937 return null;
938 } else {
939 return sprintf('%04u%04u', 1900 + $year, $rank);
940 }
726eaf7a 941 }
e7b93962
FB
942}
943
31ef12ef
RB
944
945/** Iterator over a set of Profiles
946 */
947class ProfileIterator implements PlIterator
9f21bd15
RB
948{
949 private $iterator = null;
950 private $fields;
1a9affb7 951 private $visibility;
9f21bd15 952
1a9affb7 953 public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null)
9f21bd15
RB
954 {
955 require_once 'profilefields.inc.php';
1a9affb7
RB
956
957 if ($visibility == null) {
958 $visibility = new ProfileVisibility();
959 }
960
9f21bd15 961 $this->fields = $fields;
1a9affb7 962 $this->visibility = $visibility;
9f21bd15
RB
963
964 $subits = array();
965 $callbacks = array();
966
967 $subits[0] = $it;
968 $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid');
969 $cb = PlIteratorUtils::objectPropertyCallback('pid');
970
971 if ($fields & Profile::FETCH_ADDRESSES) {
972 $callbacks[Profile::FETCH_ADDRESSES] = $cb;
973 $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
974 }
975
976 if ($fields & Profile::FETCH_CORPS) {
977 $callbacks[Profile::FETCH_CORPS] = $cb;
978 $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
979 }
980
981 if ($fields & Profile::FETCH_EDU) {
982 $callbacks[Profile::FETCH_EDU] = $cb;
983 $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
984 }
985
986 if ($fields & Profile::FETCH_JOBS) {
987 $callbacks[Profile::FETCH_JOBS] = $cb;
988 $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
989 }
990
991 if ($fields & Profile::FETCH_MEDALS) {
992 $callbacks[Profile::FETCH_MEDALS] = $cb;
993 $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
994 }
995
996 if ($fields & Profile::FETCH_NETWORKING) {
997 $callbacks[Profile::FETCH_NETWORKING] = $cb;
998 $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
999 }
1000
1001 if ($fields & Profile::FETCH_PHONES) {
1002 $callbacks[Profile::FETCH_PHONES] = $cb;
1003 $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1004 }
1005
9f21bd15
RB
1006 $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0);
1007 }
1008
1f8250e4 1009 private function hasData($field, $vals)
9f21bd15 1010 {
1f8250e4 1011 return ($this->fields & $field) && ($vals[$field] != null);
9f21bd15
RB
1012 }
1013
1014 private function fillProfile(array $vals)
1015 {
9f21bd15 1016 $pf = Profile::get($vals[0]);
1a9affb7 1017 $pf->setVisibilityLevel($this->visibility->level());
1f8250e4 1018 $pf->setFetchedFields($this->fields);
1a9affb7 1019
8cf886dc
RB
1020 if ($this->hasData(Profile::FETCH_PHONES, $vals)) {
1021 $pf->setPhones($vals[Profile::FETCH_PHONES]);
9f21bd15 1022 }
8cf886dc
RB
1023 if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) {
1024 $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]);
1025 }
1026 if ($this->hasData(Profile::FETCH_JOBS, $vals)) {
1027 $pf->setJobs($vals[Profile::FETCH_JOBS]);
1028 }
1029
1030 if ($this->hasData(Profile::FETCH_CORPS, $vals)) {
9f21bd15
RB
1031 $pf->setCorps($vals[Profile::FETCH_CORPS]);
1032 }
8cf886dc 1033 if ($this->hasData(Profile::FETCH_EDU, $vals)) {
26ca919b 1034 $pf->setEducations($vals[Profile::FETCH_EDU]);
9f21bd15 1035 }
8cf886dc 1036 if ($this->hasData(Profile::FETCH_MEDALS, $vals)) {
9f21bd15
RB
1037 $pf->setMedals($vals[Profile::FETCH_MEDALS]);
1038 }
8cf886dc 1039 if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) {
9f21bd15
RB
1040 $pf->setNetworking($vals[Profile::FETCH_NETWORKING]);
1041 }
9f21bd15
RB
1042
1043 return $pf;
1044 }
1045
1046 public function next()
1047 {
1048 $vals = $this->iterator->next();
1049 if ($vals == null) {
1050 return null;
1051 }
1052 return $this->fillProfile($vals);
1053 }
1054
1055 public function first()
1056 {
1057 return $this->iterator->first();
1058 }
1059
1060 public function last()
1061 {
1062 return $this->iterator->last();
1063 }
1064
1065 public function total()
1066 {
1067 return $this->iterator->total();
1068 }
1069}
1070
e7b93962
FB
1071// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
1072?>