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