Add ProfileCorps to Profile
[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
22class Profile
23{
f5642983
FB
24 static private $v_values = array('public' => array('public'),
25 'ax' => array('ax', 'public'),
26 'private' => array('private', 'ax', 'public'));
913a4e90 27
f5642983
FB
28 const VISIBILITY_PUBLIC = 'public';
29 const VISIBILITY_AX = 'ax';
30 const VISIBILITY_PRIVATE = 'private';
31
913a4e90
RB
32 /* name tokens */
33 const LASTNAME = 'lastname';
34 const FIRSTNAME = 'firstname';
35 const NICKNAME = 'nickname';
36 const PSEUDONYM = 'pseudonym';
37 const NAME = 'name';
38 /* name variants */
39 const VN_MARITAL = 'marital';
40 const VN_ORDINARY = 'ordinary';
41 const VN_OTHER = 'other';
42 const VN_INI = 'ini';
43 /* display names */
44 const DN_FULL = 'directory_name';
45 const DN_DISPLAY = 'yourself';
46 const DN_YOURSELF = 'yourself';
47 const DN_DIRECTORY = 'directory_name';
48 const DN_PRIVATE = 'private_name';
49 const DN_PUBLIC = 'public_name';
50 const DN_SHORT = 'short_name';
51 const DN_SORT = 'sort_name';
e02d9fbb
SJ
52 /* education related names */
53 const EDU_X = 'École polytechnique';
54 const DEGREE_X = 'Ingénieur';
55 const DEGREE_M = 'Master';
56 const DEGREE_D = 'Doctorat';
913a4e90
RB
57
58 static public $name_variants = array(
59 self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY),
60 self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER)
61 );
62
f5642983
FB
63 const ADDRESS_MAIN = 0x000001;
64 const ADDRESS_PERSO = 0x000002;
65 const ADDRESS_PRO = 0x000004;
66 const ADDRESS_ALL = 0x000006;
67 const ADDRESS_POSTAL = 0x000008;
68
69 const EDUCATION_MAIN = 0x000010;
4bc5b8f0
FB
70 const EDUCATION_EXTRA = 0x000020;
71 const EDUCATION_ALL = 0x000040;
72 const EDUCATION_FINISHED = 0x000080;
73 const EDUCATION_CURRENT = 0x000100;
f5642983 74
4bc5b8f0
FB
75 const JOBS_MAIN = 0x001000;
76 const JOBS_ALL = 0x002000;
77 const JOBS_FINISHED = 0x004000;
78 const JOBS_CURRENT = 0x008000;
f5642983 79
04a94b1d
FB
80 const NETWORKING_ALL = 0x000000;
81 const NETWORKING_WEB = 0x010000;
82 const NETWORKING_IM = 0x020000;
83 const NETWORKING_SOCIAL = 0x040000;
84
e7ba5725
RB
85 const FETCH_ADDRESSES = 0x000001;
86 const FETCH_CORPS = 0x000002;
87 const FETCH_EDU = 0x000004;
88 const FETCH_JOBS = 0x000008;
89 const FETCH_MEDALS = 0x000010;
90 const FETCH_NETWORKING = 0x000020;
91 const FETCH_PHONES = 0x000040;
92 const FETCH_PHOTO = 0x000080;
9f21bd15 93
e7ba5725
RB
94 // xor of all FETCH_XYZ
95 const FETCH_ALL = 0x0000FF;
6b5008ec 96
e7b93962
FB
97 private $pid;
98 private $hrpid;
3e53a496
FB
99 private $data = array();
100
f5642983
FB
101 private $visibility = null;
102
b774ddab 103 private function __construct(array $data)
e7b93962 104 {
b774ddab 105 $this->data = $data;
832e6fcb
FB
106 $this->pid = $this->data['pid'];
107 $this->hrpid = $this->data['hrpid'];
f74fb084
FB
108 if (!S::logged()) {
109 $this->setVisibilityLevel(self::VISIBILITY_PUBLIC);
d52b48a0
RB
110 } else {
111 $this->setVisibilityLevel(self::VISIBILITY_PRIVATE);
f74fb084 112 }
e7b93962
FB
113 }
114
9f21bd15
RB
115 static private $contexts = array();
116
117 /** Returns the best visibility context toward $visibility
118 * @param $visibility A wished visibility level
abb8de12 119 * @return An array of compatible visibilities
9f21bd15
RB
120 *
121 * if $visibility is null, the best visibility is returned
122 */
123 static public function getVisibilityContext($visibility = null)
124 {
125 if (array_key_exists($visibility, self::$contexts)) {
126 return self::$contexts[$visibility];
127 }
128
129 $asked_vis = $visibility;
130
131 if (S::logged()) {
132 $minvis = self::VISIBILITY_PRIVATE;
133 } else {
134 $minvis = self::VISIBILITY_PUBLIC;
135 }
136 if ($visibility == null) {
545ea3a5 137 $visibility = $minvis;
9f21bd15
RB
138 }
139
140 if ($minvis == self::VISIBILITY_PUBLIC) {
545ea3a5 141 $visibility = self::VISIBILITY_PUBLIC;
9f21bd15
RB
142 }
143
545ea3a5 144 $visibility = self::$v_values[$visibility];
9f21bd15
RB
145 self::$contexts[$asked_vis] = $visibility;
146
147 return $visibility;
148 }
149
e7b93962
FB
150 public function id()
151 {
152 return $this->pid;
153 }
154
155 public function hrid()
156 {
157 return $this->hrpid;
158 }
159
d5e60905
FB
160 public function promo()
161 {
162 return $this->promo;
163 }
164
845f0a4d
SJ
165 public function yearpromo()
166 {
167 return intval(substr($this->promo, 1, 4));
168 }
169
94b72319
FB
170 /** Print a name with the given formatting:
171 * %s = • for women
172 * %f = firstname
173 * %l = lastname
174 * %F = fullname
175 * %S = shortname
176 * %p = promo
177 */
178 public function name($format)
179 {
180 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
181 array($this->isFemale() ? '•' : '',
faf75faf
SJ
182 $this->firstName(), $this->lastName(),
183 $this->fullName(), $this->shortName(),
184 $this->promo()), $format);
94b72319
FB
185 }
186
187 public function fullName($with_promo = false)
188 {
189 if ($with_promo) {
190 return $this->full_name . ' (' . $this->promo . ')';
191 }
192 return $this->full_name;
193 }
194
195 public function shortName($with_promo = false)
196 {
197 if ($with_promo) {
198 return $this->short_name . ' (' . $this->promo . ')';
199 }
200 return $this->short_name;
201 }
202
203 public function firstName()
204 {
08c91036 205 return $this->firstname;
94b72319
FB
206 }
207
5c005b37
RB
208 public function firstNames()
209 {
210 return $this->nameVariants(self::FIRSTNAME);
211 }
212
94b72319
FB
213 public function lastName()
214 {
08c91036 215 return $this->lastname;
94b72319
FB
216 }
217
5c005b37
RB
218 public function lastNames()
219 {
220 return $this->nameVariants(self::LASTNAME);
221 }
222
94b72319
FB
223 public function isFemale()
224 {
225 return $this->sex == PlUser::GENDER_FEMALE;
226 }
227
400ac338
RB
228 public function isDead()
229 {
230 return ($this->deathdate != null);
231 }
232
94b72319
FB
233 public function data()
234 {
235 $this->first_name;
236 return $this->data;
237 }
238
5c005b37
RB
239 private function nameVariants($type)
240 {
241 $vals = array($this->$type);
242 foreach (self::$name_variants[$type] as $var) {
243 $vartype = $type . '_' . $var;
244 $varname = $this->$vartype;
245 if ($varname != null && $varname != "") {
246 $vals[] = $varname;
247 }
248 }
249 return array_unique($vals);
250 }
251
b6569a95
FB
252 public function nationalities()
253 {
254 $nats = array();
255 if ($this->nationality1) {
256 $nats[] = $this->nationality1;
257 }
258 if ($this->nationality2) {
259 $nats[] = $this->nationality2;
260 }
261 if ($this->nationality3) {
262 $nats[] = $this->nationality3;
263 }
264 return $nats;
265 }
266
3e53a496
FB
267 public function __get($name)
268 {
269 if (property_exists($this, $name)) {
270 return $this->$name;
271 }
272
3e53a496
FB
273 if (isset($this->data[$name])) {
274 return $this->data[$name];
275 }
276
277 return null;
278 }
279
280 public function __isset($name)
281 {
282 return property_exists($this, $name) || isset($this->data[$name]);
283 }
284
c159e50f
RB
285 /** Sets the level of visibility of the profile
286 * Sets $this->visibility to a list of valid visibilities.
287 * @param one of the self::VISIBILITY_* values
288 */
f5642983
FB
289 public function setVisibilityLevel($visibility)
290 {
0d906109
RB
291 if ($visibility != self::VISIBILITY_PRIVATE
292 && $visibility != self::VISIBILITY_AX
f5642983
FB
293 && $visibility != self::VISIBILITY_PUBLIC) {
294 Platal::page()->kill("Visibility invalide: " . $visibility);
295 }
296 $this->visibility = self::$v_values[$visibility];
5c005b37 297 if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) {
46e9eb99
FB
298 unset($this->data['mobile']);
299 }
f5642983
FB
300 }
301
c159e50f
RB
302 /** Determine whether an item with visibility $visibility can be displayed
303 * with the current level of visibility of the profile
304 * @param $visibility The level of visibility to be checked
305 */
306 public function isVisible($visibility)
307 {
308 return in_array($visibility, $this->visibility);
309 }
4bc5b8f0 310
9f21bd15
RB
311 public static function getCompatibleVisibilities($visibility)
312 {
313 return self::$v_values[$visibility];
314 }
315
990cb17b
RB
316 private function getProfileField($cls)
317 {
318 return ProfileField::getForPID($cls, $this->id(), $this->visibility);
319 }
320
8cf886dc
RB
321 /** Consolidates internal data (addresses, phones, jobs)
322 */
323 private function consolidateFields()
324 {
325 if ($this->phones != null) {
326 if ($this->addresses != null) {
327 $this->addresses->addPhones($this->phones);
328 }
329
330 if ($this->jobs != null) {
331 $this->jobs->addPhones($this->phones);
332 }
333 }
334
335 if ($this->addresses != null && $this->jobs != null) {
336 $this->jobs->addAddresses($this->addresses);
337 }
338 }
339
833a6e86
FB
340 /* Photo
341 */
9f21bd15
RB
342 private $photo = null;
343 public function setPhoto(ProfilePhoto $photo)
344 {
345 $this->photo = $photo;
346 }
347
833a6e86
FB
348 public function getPhoto($fallback = true)
349 {
990cb17b
RB
350 if ($this->photo == null) {
351 $this->setPhoto($this->getProfileField('ProfilePhoto'));
352 }
353
9f21bd15
RB
354 if ($this->photo != null) {
355 return $this->photo->pic;
356 } else if ($fallback) {
357 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png',
358 'image/png');
359 }
360 return null;
361 }
7d0ebdf5 362
4bc5b8f0
FB
363 /* Addresses
364 */
7d0ebdf5
RB
365 private $addresses = null;
366 public function setAddresses(ProfileAddresses $addr)
367 {
368 $this->addresses = $addr;
8cf886dc 369 $this->consolidateFields();
7d0ebdf5
RB
370 }
371
4bc5b8f0 372 public function getAddresses($flags, $limit = null)
f5642983 373 {
7d0ebdf5 374 if ($this->addresses == null) {
990cb17b 375 $this->setAddresses($this->getProfileField('ProfileAddresses'));
f5642983 376 }
0907501b
RB
377
378 if ($this->addresses == null) {
379 return PlIteratorUtils::emptyIterator();
380 }
990cb17b 381 return $this->addresses->get($flags, $limit);
f5642983
FB
382 }
383
384 public function getMainAddress()
385 {
386 $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
387 if ($it->total() == 0) {
388 return null;
389 } else {
390 return $it->next();
391 }
392 }
3e53a496 393
bdef0d33
RB
394 /* Phones
395 */
396 private $phones = null;
397 public function setPhones(ProfilePhones $phones)
398 {
399 $this->phones = $phones;
400 $this->consolidateFields();
401 }
402
403 public function getPhones($flags, $limit = null)
404 {
405 if ($this->phones == null) {
406 $this->setPhones($this->getProfileField('ProfilePhones'));
407 }
408
409 if ($this->phones == null) {
410 return PlIteratorUtils::emptyIterator();
411 }
412 return $this->phones->get($flags, $limit);
413 }
4bc5b8f0
FB
414
415 /* Educations
416 */
d4d395bb 417 private $educations = null;
a060e1c3
RB
418 public function setEducations(ProfileEducation $edu)
419 {
420 $this->educations = $edu;
421 }
422
4bc5b8f0
FB
423 public function getEducations($flags, $limit = null)
424 {
d4d395bb 425 if ($this->educations == null) {
990cb17b 426 $this->setEducations($this->getProfileField('ProfileEducation'));
d4d395bb 427 }
0907501b
RB
428
429 if ($this->educations == null) {
430 return PlIteratorUtils::emptyIterator();
431 }
a060e1c3 432 return $this->educations->get($flags, $limit);
4bc5b8f0
FB
433 }
434
435 public function getExtraEducations($limit = null)
436 {
437 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
438 }
439
0396c259
RB
440 /* Corps
441 */
442 private $corps = null;
443 public function setCorps(ProfileCorps $corps)
444 {
445 $this->corps = $corps;
446 }
447
448 public function getCorps()
449 {
450 if ($this->corps == null) {
451 $this->setCorps($this->getProfileField('ProfileCorps'));
452 }
453 return $this->corps;
454 }
4bc5b8f0 455
04a94b1d
FB
456 /** Networking
457 */
d4d395bb
RB
458 private $networks = null;
459 public function setNetworking(ProfileNetworking $nw)
460 {
461 $this->networks = $nw;
462 }
04a94b1d
FB
463
464 public function getNetworking($flags, $limit = null)
465 {
d4d395bb 466 if ($this->networks == null) {
990cb17b 467 $this->setNetworking($this->getProfileField('ProfileNetworking'));
04a94b1d 468 }
0907501b
RB
469 if ($this->networks == null) {
470 return PlIteratorUtils::emptyIterator();
471 }
d4d395bb 472 return $this->networks->get($flags, $limit);
04a94b1d
FB
473 }
474
475 public function getWebSite()
476 {
477 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
478 if ($site->total() != 1) {
479 return null;
480 }
481 $site = $site->next();
482 return $site['address'];
483 }
484
485
e718bd18
FB
486 /** Jobs
487 */
949fc736
RB
488 private $jobs = null;
489 public function setJobs(ProfileJobs $jobs)
490 {
491 $this->jobs = $jobs;
492 $this->consolidateFields();
493 }
e718bd18
FB
494
495 public function getJobs($flags, $limit = null)
496 {
949fc736
RB
497 if ($this->jobs == null) {
498 $this->setJobs($this->getProfileField('ProfileJobs'));
e718bd18 499 }
949fc736 500
0907501b
RB
501 if ($this->jobs == null) {
502 return PlIteratorUtils::emptyIterator();
503 }
949fc736 504 return $this->jobs->get($flags, $limit);
e718bd18
FB
505 }
506
44ec5eb5 507 public function getMainJob()
e718bd18
FB
508 {
509 $job = $this->getJobs(self::JOBS_MAIN, 1);
510 if ($job->total() != 1) {
511 return null;
512 }
513 return $job->next();
514 }
515
5c005b37
RB
516 /* Binets
517 */
518 public function getBinets()
519 {
520 return XDB::fetchColumn('SELECT binet_id
5c8a71f2 521 FROM profile_binets
bdd977d7 522 WHERE pid = {?}', $this->id());
5c005b37 523 }
97b71de5
RB
524 public function getBinetsNames()
525 {
526 return XDB::fetchColumn('SELECT text
527 FROM profile_binets AS pb
528 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
529 WHERE pb.pid = {?}', $this->id());
530 }
5c005b37 531
26ca919b
RB
532 /* Medals
533 */
534 private $medals = null;
535 public function setMedals(ProfileMedals $medals)
536 {
537 $this->medals = $medals;
538 }
539
540 public function getMedals()
541 {
542 if ($this->medals == null) {
543 $this->setMedals($this->getProfileField('ProfileMedals'));
544 }
545 if ($this->medals == null) {
546 return array();
547 }
548 return $this->medals->medals;
549 }
e718bd18 550
e7b93962
FB
551 public function owner()
552 {
553 return User::getSilent($this);
554 }
555
94590511
SJ
556 public function compareNames($firstname, $lastname)
557 {
558 $_lastname = mb_strtoupper($this->lastName());
559 $_firstname = mb_strtoupper($this->firstName());
560 $lastname = mb_strtoupper($lastname);
561 $firstname = mb_strtoupper($firstname);
562
563 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
564 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY);
565 $maxlen = 0;
566
567 foreach ($tokens as $str) {
568 $isOk &= (strpos($_lastname, $str) !== false);
569 $maxlen = max($maxlen, strlen($str));
570 }
571
572 return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname)));
573 }
574
9f21bd15 575 private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null)
b774ddab
FB
576 {
577 if (count($pids) == 0) {
578 return array();
579 }
0d906109
RB
580
581 if ($respect_order) {
582 $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids);
583 } else {
584 $order = '';
585 }
586
9f21bd15 587
97b71de5 588 $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, pse.text AS section,
9f21bd15
RB
589 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
590 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
591 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
592 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
593 pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub,
594 ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub,
595 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
bdef0d33 596 pm.expertise AS mentor_expertise,
9f21bd15
RB
597 ap.uid AS owner_id
598 FROM profiles AS p
599 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
600 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
97b71de5 601 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
9f21bd15
RB
602 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
603 AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
604 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
605 AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
606 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
607 AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
608 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
609 AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
610 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
611 AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
612 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
613 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
bdef0d33 614 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
9f21bd15
RB
615 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
616 WHERE p.pid IN ' . XDB::formatArray($pids) . '
617 GROUP BY p.pid
618 ' . $order);
31ef12ef 619 return new ProfileIterator($it, $pids, $fields, $visibility);
b774ddab
FB
620 }
621
622 public static function getPID($login)
623 {
624 if ($login instanceof PlUser) {
625 return XDB::fetchOneCell('SELECT pid
626 FROM account_profiles
627 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
628 $login->id());
9f21bd15 629 } else if (ctype_digit($login)) {
b774ddab
FB
630 return XDB::fetchOneCell('SELECT pid
631 FROM profiles
632 WHERE pid = {?}', $login);
633 } else {
634 return XDB::fetchOneCell('SELECT pid
635 FROM profiles
636 WHERE hrpid = {?}', $login);
637 }
638 }
639
0d906109
RB
640 public static function getPIDsFromUIDs($uids, $respect_order = true)
641 {
642 if ($respect_order) {
643 $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids);
644 } else {
645 $order = '';
646 }
647 return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
648 FROM account_profiles AS ap
649 WHERE FIND_IN_SET(\'owner\', ap.perms)
9f21bd15
RB
650 AND ap.uid IN ' . XDB::formatArray($uids) .'
651 ' . $order);
0d906109 652 }
b774ddab 653
e7b93962
FB
654 /** Return the profile associated with the given login.
655 */
9f21bd15 656 public static function get($login, $fields = 0x0000, $visibility = null)
a3118782 657 {
641f2115
FB
658 if (is_array($login)) {
659 return new Profile($login);
660 }
b774ddab
FB
661 $pid = self::getPID($login);
662 if (!is_null($pid)) {
9f21bd15 663 $it = self::iterOverPIDs(array($pid), false, $fields, $visibility);
0d906109 664 return $it->next();
b774ddab 665 } else {
efe597c5
FB
666 /* Let say we can identify a profile using the identifiers of its owner.
667 */
455ea0c9
FB
668 if (!($login instanceof PlUser)) {
669 $user = User::getSilent($login);
670 if ($user && $user->hasProfile()) {
671 return $user->profile();
672 }
efe597c5 673 }
3e53a496 674 return null;
e7b93962
FB
675 }
676 }
a3118782 677
9f21bd15 678 public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 679 {
9f21bd15 680 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
0d906109
RB
681 }
682
9f21bd15 683 public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 684 {
9f21bd15 685 return self::fetchProfileData($pids, $respect_order, $fields, $visibility);
0d906109
RB
686 }
687
b774ddab
FB
688 /** Return profiles for the list of pids.
689 */
7d0ebdf5 690 public static function getBulkProfilesWithPIDs(array $pids, $fields = self::FETCH_ADDRESSES, $visibility = null)
b774ddab
FB
691 {
692 if (count($pids) == 0) {
693 return array();
694 }
9f21bd15 695 $it = self::iterOverPIDs($pids, true, $fields, $visibility);
b774ddab 696 $profiles = array();
0d906109
RB
697 while ($p = $it->next()) {
698 $profiles[$p->id()] = $p;
b774ddab
FB
699 }
700 return $profiles;
701 }
702
703 /** Return profiles for uids.
704 */
9f21bd15 705 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null)
b774ddab
FB
706 {
707 if (count($uids) == 0) {
708 return array();
709 }
9f21bd15 710 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility);
b774ddab
FB
711 }
712
913a4e90
RB
713 public static function isDisplayName($name)
714 {
715 return $name == self::DN_FULL || $name == self::DN_DISPLAY
716 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
717 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
718 || $name == self::DN_SHORT || $name == self::DN_SORT;
719 }
720
9f21bd15
RB
721 public static function getNameTypeId($type, $for_sql = false)
722 {
723 if (!S::has('name_types')) {
724 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
725 FROM profile_name_enum');
726 S::set('name_types', $table);
727 } else {
728 $table = S::v('name_types');
729 }
730 if ($for_sql) {
731 return XDB::escape($table[$type]);
732 } else {
733 return $table[$type];
734 }
735 }
736
726eaf7a
SJ
737 public static function rebuildSearchTokens($pid)
738 {
739 XDB::execute('DELETE FROM search_name
6dbb167e 740 WHERE pid = {?}',
726eaf7a
SJ
741 $pid);
742 $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
743 FIND_IN_SET('public', e.flags) AS public
744 FROM profile_name AS n
745 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
746 WHERE n.pid = {?}",
747 $pid);
748
749 foreach ($keys as $i => $key) {
750 if ($key['name'] == '') {
751 continue;
752 }
753 $toks = preg_split('/[ \'\-]+/', $key['name']);
754 $token = '';
755 $first = 5;
756 while ($toks) {
757 $token = strtolower(replace_accent(array_pop($toks) . $token));
758 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
6dbb167e 759 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
726eaf7a 760 VALUES ({?}, {?}, {?}, {?}, {?})',
6dbb167e 761 $token, $pid, soundex_fr($token), $score, $key['public']);
726eaf7a
SJ
762 $first = 0;
763 }
764 }
69b46857
SJ
765 }
766
767 /** The school identifier consists of 6 digits. The first 3 represent the
768 * promotion entry year. The last 3 indicate the student's rank.
769 *
770 * Our identifier consists of 8 digits and both half have the same role.
771 * This enables us to deal with bigger promotions and with a wider range
772 * of promotions.
773 *
774 * getSchoolId returns a school identifier given one of ours.
775 * getXorgId returns a X.org identifier given a school identifier.
776 */
777 public static function getSchoolId($xorgId)
778 {
779 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
780 return null;
781 }
782
783 $year = intval(substr($xorgId, 0, 4));
784 $rank = intval(substr($xorgId, 5, 3));
785 if ($year < 1996) {
786 return null;
787 } elseif ($year < 2000) {
788 $year = intval(substr(1900 - $year, 1, 3));
789 return sprintf('%02u0%03u', $year, $rank);
790 } else {
791 $year = intval(substr(1900 - $year, 1, 3));
792 return sprintf('%03u%03u', $year, $rank);
793 }
794 }
726eaf7a 795
69b46857
SJ
796 public static function getXorgId($schoolId)
797 {
94590511 798 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
a292484d
SJ
799 return null;
800 }
801
69b46857
SJ
802 $year = intval(substr($schoolId, 0, 3));
803 $rank = intval(substr($schoolId, 3, 3));
726eaf7a 804
69b46857
SJ
805 if ($year > 200) {
806 $year /= 10;
807 }
808 if ($year < 96) {
809 return null;
810 } else {
811 return sprintf('%04u%04u', 1900 + $year, $rank);
812 }
726eaf7a 813 }
e7b93962
FB
814}
815
31ef12ef
RB
816
817/** Iterator over a set of Profiles
818 */
819class ProfileIterator implements PlIterator
9f21bd15
RB
820{
821 private $iterator = null;
822 private $fields;
823
824 public function __construct(PlIterator $it, array $pids, $fields = 0x0000, $visibility = null)
825 {
826 require_once 'profilefields.inc.php';
827 $visibility = Profile::getVisibilityContext($visibility);
828 $this->fields = $fields;
829
830 $subits = array();
831 $callbacks = array();
832
833 $subits[0] = $it;
834 $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid');
835 $cb = PlIteratorUtils::objectPropertyCallback('pid');
836
837 if ($fields & Profile::FETCH_ADDRESSES) {
838 $callbacks[Profile::FETCH_ADDRESSES] = $cb;
839 $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
840 }
841
842 if ($fields & Profile::FETCH_CORPS) {
843 $callbacks[Profile::FETCH_CORPS] = $cb;
844 $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
845 }
846
847 if ($fields & Profile::FETCH_EDU) {
848 $callbacks[Profile::FETCH_EDU] = $cb;
849 $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
850 }
851
852 if ($fields & Profile::FETCH_JOBS) {
853 $callbacks[Profile::FETCH_JOBS] = $cb;
854 $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
855 }
856
857 if ($fields & Profile::FETCH_MEDALS) {
858 $callbacks[Profile::FETCH_MEDALS] = $cb;
859 $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
860 }
861
862 if ($fields & Profile::FETCH_NETWORKING) {
863 $callbacks[Profile::FETCH_NETWORKING] = $cb;
864 $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
865 }
866
867 if ($fields & Profile::FETCH_PHONES) {
868 $callbacks[Profile::FETCH_PHONES] = $cb;
869 $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
870 }
871
872 if ($fields & Profile::FETCH_PHOTO) {
873 $callbacks[Profile::FETCH_PHOTO] = $cb;
874 $subits[Profile::FETCH_PHOTO] = new ProfileFieldIterator('ProfilePhoto', $pids, $visibility);
875 }
876
877 $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0);
878 }
879
8cf886dc 880 private function hasData($flag, $vals)
9f21bd15 881 {
8cf886dc 882 return ($this->fields & $flag) && ($vals[$flag] != null);
9f21bd15
RB
883 }
884
885 private function fillProfile(array $vals)
886 {
9f21bd15 887 $pf = Profile::get($vals[0]);
8cf886dc
RB
888 if ($this->hasData(Profile::FETCH_PHONES, $vals)) {
889 $pf->setPhones($vals[Profile::FETCH_PHONES]);
9f21bd15 890 }
8cf886dc
RB
891 if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) {
892 $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]);
893 }
894 if ($this->hasData(Profile::FETCH_JOBS, $vals)) {
895 $pf->setJobs($vals[Profile::FETCH_JOBS]);
896 }
897
898 if ($this->hasData(Profile::FETCH_CORPS, $vals)) {
9f21bd15
RB
899 $pf->setCorps($vals[Profile::FETCH_CORPS]);
900 }
8cf886dc 901 if ($this->hasData(Profile::FETCH_EDU, $vals)) {
26ca919b 902 $pf->setEducations($vals[Profile::FETCH_EDU]);
9f21bd15 903 }
8cf886dc 904 if ($this->hasData(Profile::FETCH_MEDALS, $vals)) {
9f21bd15
RB
905 $pf->setMedals($vals[Profile::FETCH_MEDALS]);
906 }
8cf886dc 907 if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) {
9f21bd15
RB
908 $pf->setNetworking($vals[Profile::FETCH_NETWORKING]);
909 }
8cf886dc
RB
910 if ($this->hasData(Profile::FETCH_PHOTO, $vals)) {
911 $pf->setPhoto($vals[Profile::FETCH_PHOTO]);
9f21bd15
RB
912 }
913
914 return $pf;
915 }
916
917 public function next()
918 {
919 $vals = $this->iterator->next();
920 if ($vals == null) {
921 return null;
922 }
923 return $this->fillProfile($vals);
924 }
925
926 public function first()
927 {
928 return $this->iterator->first();
929 }
930
931 public function last()
932 {
933 return $this->iterator->last();
934 }
935
936 public function total()
937 {
938 return $this->iterator->total();
939 }
940}
941
e7b93962
FB
942// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
943?>