Add isDead function 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
4bc5b8f0
FB
394
395 /* Educations
396 */
d4d395bb 397 private $educations = null;
a060e1c3
RB
398 public function setEducations(ProfileEducation $edu)
399 {
400 $this->educations = $edu;
401 }
402
4bc5b8f0
FB
403 public function getEducations($flags, $limit = null)
404 {
d4d395bb 405 if ($this->educations == null) {
990cb17b 406 $this->setEducations($this->getProfileField('ProfileEducation'));
d4d395bb 407 }
0907501b
RB
408
409 if ($this->educations == null) {
410 return PlIteratorUtils::emptyIterator();
411 }
a060e1c3 412 return $this->educations->get($flags, $limit);
4bc5b8f0
FB
413 }
414
415 public function getExtraEducations($limit = null)
416 {
417 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
418 }
419
420
04a94b1d
FB
421 /** Networking
422 */
d4d395bb
RB
423 private $networks = null;
424 public function setNetworking(ProfileNetworking $nw)
425 {
426 $this->networks = $nw;
427 }
04a94b1d
FB
428
429 public function getNetworking($flags, $limit = null)
430 {
d4d395bb 431 if ($this->networks == null) {
990cb17b 432 $this->setNetworking($this->getProfileField('ProfileNetworking'));
04a94b1d 433 }
0907501b
RB
434 if ($this->networks == null) {
435 return PlIteratorUtils::emptyIterator();
436 }
d4d395bb 437 return $this->networks->get($flags, $limit);
04a94b1d
FB
438 }
439
440 public function getWebSite()
441 {
442 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
443 if ($site->total() != 1) {
444 return null;
445 }
446 $site = $site->next();
447 return $site['address'];
448 }
449
450
e718bd18
FB
451 /** Jobs
452 */
949fc736
RB
453 private $jobs = null;
454 public function setJobs(ProfileJobs $jobs)
455 {
456 $this->jobs = $jobs;
457 $this->consolidateFields();
458 }
e718bd18
FB
459
460 public function getJobs($flags, $limit = null)
461 {
949fc736
RB
462 if ($this->jobs == null) {
463 $this->setJobs($this->getProfileField('ProfileJobs'));
e718bd18 464 }
949fc736 465
0907501b
RB
466 if ($this->jobs == null) {
467 return PlIteratorUtils::emptyIterator();
468 }
949fc736 469 return $this->jobs->get($flags, $limit);
e718bd18
FB
470 }
471
44ec5eb5 472 public function getMainJob()
e718bd18
FB
473 {
474 $job = $this->getJobs(self::JOBS_MAIN, 1);
475 if ($job->total() != 1) {
476 return null;
477 }
478 return $job->next();
479 }
480
5c005b37
RB
481 /* Binets
482 */
483 public function getBinets()
484 {
485 return XDB::fetchColumn('SELECT binet_id
5c8a71f2 486 FROM profile_binets
bdd977d7 487 WHERE pid = {?}', $this->id());
5c005b37 488 }
97b71de5
RB
489 public function getBinetsNames()
490 {
491 return XDB::fetchColumn('SELECT text
492 FROM profile_binets AS pb
493 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
494 WHERE pb.pid = {?}', $this->id());
495 }
5c005b37 496
26ca919b
RB
497 /* Medals
498 */
499 private $medals = null;
500 public function setMedals(ProfileMedals $medals)
501 {
502 $this->medals = $medals;
503 }
504
505 public function getMedals()
506 {
507 if ($this->medals == null) {
508 $this->setMedals($this->getProfileField('ProfileMedals'));
509 }
510 if ($this->medals == null) {
511 return array();
512 }
513 return $this->medals->medals;
514 }
e718bd18 515
e7b93962
FB
516 public function owner()
517 {
518 return User::getSilent($this);
519 }
520
94590511
SJ
521 public function compareNames($firstname, $lastname)
522 {
523 $_lastname = mb_strtoupper($this->lastName());
524 $_firstname = mb_strtoupper($this->firstName());
525 $lastname = mb_strtoupper($lastname);
526 $firstname = mb_strtoupper($firstname);
527
528 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
529 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY);
530 $maxlen = 0;
531
532 foreach ($tokens as $str) {
533 $isOk &= (strpos($_lastname, $str) !== false);
534 $maxlen = max($maxlen, strlen($str));
535 }
536
537 return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname)));
538 }
539
9f21bd15 540 private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null)
b774ddab
FB
541 {
542 if (count($pids) == 0) {
543 return array();
544 }
0d906109
RB
545
546 if ($respect_order) {
547 $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids);
548 } else {
549 $order = '';
550 }
551
9f21bd15 552
97b71de5 553 $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, pse.text AS section,
9f21bd15
RB
554 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
555 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
556 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
557 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
558 pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub,
559 ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub,
560 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
561 ap.uid AS owner_id
562 FROM profiles AS p
563 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
564 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
97b71de5 565 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
9f21bd15
RB
566 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
567 AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
568 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
569 AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
570 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
571 AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
572 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
573 AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
574 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
575 AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
576 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
577 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
578 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
579 WHERE p.pid IN ' . XDB::formatArray($pids) . '
580 GROUP BY p.pid
581 ' . $order);
31ef12ef 582 return new ProfileIterator($it, $pids, $fields, $visibility);
b774ddab
FB
583 }
584
585 public static function getPID($login)
586 {
587 if ($login instanceof PlUser) {
588 return XDB::fetchOneCell('SELECT pid
589 FROM account_profiles
590 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
591 $login->id());
9f21bd15 592 } else if (ctype_digit($login)) {
b774ddab
FB
593 return XDB::fetchOneCell('SELECT pid
594 FROM profiles
595 WHERE pid = {?}', $login);
596 } else {
597 return XDB::fetchOneCell('SELECT pid
598 FROM profiles
599 WHERE hrpid = {?}', $login);
600 }
601 }
602
0d906109
RB
603 public static function getPIDsFromUIDs($uids, $respect_order = true)
604 {
605 if ($respect_order) {
606 $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids);
607 } else {
608 $order = '';
609 }
610 return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
611 FROM account_profiles AS ap
612 WHERE FIND_IN_SET(\'owner\', ap.perms)
9f21bd15
RB
613 AND ap.uid IN ' . XDB::formatArray($uids) .'
614 ' . $order);
0d906109 615 }
b774ddab 616
e7b93962
FB
617 /** Return the profile associated with the given login.
618 */
9f21bd15 619 public static function get($login, $fields = 0x0000, $visibility = null)
a3118782 620 {
641f2115
FB
621 if (is_array($login)) {
622 return new Profile($login);
623 }
b774ddab
FB
624 $pid = self::getPID($login);
625 if (!is_null($pid)) {
9f21bd15 626 $it = self::iterOverPIDs(array($pid), false, $fields, $visibility);
0d906109 627 return $it->next();
b774ddab 628 } else {
efe597c5
FB
629 /* Let say we can identify a profile using the identifiers of its owner.
630 */
455ea0c9
FB
631 if (!($login instanceof PlUser)) {
632 $user = User::getSilent($login);
633 if ($user && $user->hasProfile()) {
634 return $user->profile();
635 }
efe597c5 636 }
3e53a496 637 return null;
e7b93962
FB
638 }
639 }
a3118782 640
9f21bd15 641 public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 642 {
9f21bd15 643 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
0d906109
RB
644 }
645
9f21bd15 646 public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null)
0d906109 647 {
9f21bd15 648 return self::fetchProfileData($pids, $respect_order, $fields, $visibility);
0d906109
RB
649 }
650
b774ddab
FB
651 /** Return profiles for the list of pids.
652 */
7d0ebdf5 653 public static function getBulkProfilesWithPIDs(array $pids, $fields = self::FETCH_ADDRESSES, $visibility = null)
b774ddab
FB
654 {
655 if (count($pids) == 0) {
656 return array();
657 }
9f21bd15 658 $it = self::iterOverPIDs($pids, true, $fields, $visibility);
b774ddab 659 $profiles = array();
0d906109
RB
660 while ($p = $it->next()) {
661 $profiles[$p->id()] = $p;
b774ddab
FB
662 }
663 return $profiles;
664 }
665
666 /** Return profiles for uids.
667 */
9f21bd15 668 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null)
b774ddab
FB
669 {
670 if (count($uids) == 0) {
671 return array();
672 }
9f21bd15 673 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility);
b774ddab
FB
674 }
675
913a4e90
RB
676 public static function isDisplayName($name)
677 {
678 return $name == self::DN_FULL || $name == self::DN_DISPLAY
679 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
680 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
681 || $name == self::DN_SHORT || $name == self::DN_SORT;
682 }
683
9f21bd15
RB
684 public static function getNameTypeId($type, $for_sql = false)
685 {
686 if (!S::has('name_types')) {
687 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
688 FROM profile_name_enum');
689 S::set('name_types', $table);
690 } else {
691 $table = S::v('name_types');
692 }
693 if ($for_sql) {
694 return XDB::escape($table[$type]);
695 } else {
696 return $table[$type];
697 }
698 }
699
726eaf7a
SJ
700 public static function rebuildSearchTokens($pid)
701 {
702 XDB::execute('DELETE FROM search_name
6dbb167e 703 WHERE pid = {?}',
726eaf7a
SJ
704 $pid);
705 $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
706 FIND_IN_SET('public', e.flags) AS public
707 FROM profile_name AS n
708 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
709 WHERE n.pid = {?}",
710 $pid);
711
712 foreach ($keys as $i => $key) {
713 if ($key['name'] == '') {
714 continue;
715 }
716 $toks = preg_split('/[ \'\-]+/', $key['name']);
717 $token = '';
718 $first = 5;
719 while ($toks) {
720 $token = strtolower(replace_accent(array_pop($toks) . $token));
721 $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10);
6dbb167e 722 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
726eaf7a 723 VALUES ({?}, {?}, {?}, {?}, {?})',
6dbb167e 724 $token, $pid, soundex_fr($token), $score, $key['public']);
726eaf7a
SJ
725 $first = 0;
726 }
727 }
69b46857
SJ
728 }
729
730 /** The school identifier consists of 6 digits. The first 3 represent the
731 * promotion entry year. The last 3 indicate the student's rank.
732 *
733 * Our identifier consists of 8 digits and both half have the same role.
734 * This enables us to deal with bigger promotions and with a wider range
735 * of promotions.
736 *
737 * getSchoolId returns a school identifier given one of ours.
738 * getXorgId returns a X.org identifier given a school identifier.
739 */
740 public static function getSchoolId($xorgId)
741 {
742 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
743 return null;
744 }
745
746 $year = intval(substr($xorgId, 0, 4));
747 $rank = intval(substr($xorgId, 5, 3));
748 if ($year < 1996) {
749 return null;
750 } elseif ($year < 2000) {
751 $year = intval(substr(1900 - $year, 1, 3));
752 return sprintf('%02u0%03u', $year, $rank);
753 } else {
754 $year = intval(substr(1900 - $year, 1, 3));
755 return sprintf('%03u%03u', $year, $rank);
756 }
757 }
726eaf7a 758
69b46857
SJ
759 public static function getXorgId($schoolId)
760 {
94590511 761 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
a292484d
SJ
762 return null;
763 }
764
69b46857
SJ
765 $year = intval(substr($schoolId, 0, 3));
766 $rank = intval(substr($schoolId, 3, 3));
726eaf7a 767
69b46857
SJ
768 if ($year > 200) {
769 $year /= 10;
770 }
771 if ($year < 96) {
772 return null;
773 } else {
774 return sprintf('%04u%04u', 1900 + $year, $rank);
775 }
726eaf7a 776 }
e7b93962
FB
777}
778
31ef12ef
RB
779
780/** Iterator over a set of Profiles
781 */
782class ProfileIterator implements PlIterator
9f21bd15
RB
783{
784 private $iterator = null;
785 private $fields;
786
787 public function __construct(PlIterator $it, array $pids, $fields = 0x0000, $visibility = null)
788 {
789 require_once 'profilefields.inc.php';
790 $visibility = Profile::getVisibilityContext($visibility);
791 $this->fields = $fields;
792
793 $subits = array();
794 $callbacks = array();
795
796 $subits[0] = $it;
797 $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid');
798 $cb = PlIteratorUtils::objectPropertyCallback('pid');
799
800 if ($fields & Profile::FETCH_ADDRESSES) {
801 $callbacks[Profile::FETCH_ADDRESSES] = $cb;
802 $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
803 }
804
805 if ($fields & Profile::FETCH_CORPS) {
806 $callbacks[Profile::FETCH_CORPS] = $cb;
807 $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
808 }
809
810 if ($fields & Profile::FETCH_EDU) {
811 $callbacks[Profile::FETCH_EDU] = $cb;
812 $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
813 }
814
815 if ($fields & Profile::FETCH_JOBS) {
816 $callbacks[Profile::FETCH_JOBS] = $cb;
817 $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
818 }
819
820 if ($fields & Profile::FETCH_MEDALS) {
821 $callbacks[Profile::FETCH_MEDALS] = $cb;
822 $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
823 }
824
825 if ($fields & Profile::FETCH_NETWORKING) {
826 $callbacks[Profile::FETCH_NETWORKING] = $cb;
827 $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
828 }
829
830 if ($fields & Profile::FETCH_PHONES) {
831 $callbacks[Profile::FETCH_PHONES] = $cb;
832 $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
833 }
834
835 if ($fields & Profile::FETCH_PHOTO) {
836 $callbacks[Profile::FETCH_PHOTO] = $cb;
837 $subits[Profile::FETCH_PHOTO] = new ProfileFieldIterator('ProfilePhoto', $pids, $visibility);
838 }
839
840 $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0);
841 }
842
8cf886dc 843 private function hasData($flag, $vals)
9f21bd15 844 {
8cf886dc 845 return ($this->fields & $flag) && ($vals[$flag] != null);
9f21bd15
RB
846 }
847
848 private function fillProfile(array $vals)
849 {
9f21bd15 850 $pf = Profile::get($vals[0]);
8cf886dc
RB
851 if ($this->hasData(Profile::FETCH_PHONES, $vals)) {
852 $pf->setPhones($vals[Profile::FETCH_PHONES]);
9f21bd15 853 }
8cf886dc
RB
854 if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) {
855 $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]);
856 }
857 if ($this->hasData(Profile::FETCH_JOBS, $vals)) {
858 $pf->setJobs($vals[Profile::FETCH_JOBS]);
859 }
860
861 if ($this->hasData(Profile::FETCH_CORPS, $vals)) {
9f21bd15
RB
862 $pf->setCorps($vals[Profile::FETCH_CORPS]);
863 }
8cf886dc 864 if ($this->hasData(Profile::FETCH_EDU, $vals)) {
26ca919b 865 $pf->setEducations($vals[Profile::FETCH_EDU]);
9f21bd15 866 }
8cf886dc 867 if ($this->hasData(Profile::FETCH_MEDALS, $vals)) {
9f21bd15
RB
868 $pf->setMedals($vals[Profile::FETCH_MEDALS]);
869 }
8cf886dc 870 if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) {
9f21bd15
RB
871 $pf->setNetworking($vals[Profile::FETCH_NETWORKING]);
872 }
8cf886dc
RB
873 if ($this->hasData(Profile::FETCH_PHOTO, $vals)) {
874 $pf->setPhoto($vals[Profile::FETCH_PHOTO]);
9f21bd15
RB
875 }
876
877 return $pf;
878 }
879
880 public function next()
881 {
882 $vals = $this->iterator->next();
883 if ($vals == null) {
884 return null;
885 }
886 return $this->fillProfile($vals);
887 }
888
889 public function first()
890 {
891 return $this->iterator->first();
892 }
893
894 public function last()
895 {
896 return $this->iterator->last();
897 }
898
899 public function total()
900 {
901 return $this->iterator->total();
902 }
903}
904
e7b93962
FB
905// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
906?>