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