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