2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class ProfileVisibility
24 static private $v_values = array(self
::VIS_PUBLIC
=> array(self
::VIS_PUBLIC
),
25 self
::VIS_AX
=> array(self
::VIS_AX
, self
::VIS_PUBLIC
),
26 self
::VIS_PRIVATE
=> array(self
::VIS_PRIVATE
, self
::VIS_AX
, self
::VIS_PUBLIC
));
28 const VIS_PUBLIC
= 'public';
30 const VIS_PRIVATE
= 'private';
34 public function __construct($level = null
)
36 $this->setLevel($level);
39 public function setLevel($level = self
::VIS_PUBLIC
)
41 if ($level != null
&& $level != self
::VIS_PRIVATE
&& $level != self
::VIS_AX
&& $level != self
::VIS_PUBLIC
) {
42 Platal
::page()->kill("Invalid visibility: " . $level);
46 $level = self
::VIS_PUBLIC
;
47 } else if ($level == null
) {
48 $level = self
::VIS_PRIVATE
;
51 if ($this->level
== null ||
$this->level
== self
::VIS_PRIVATE
) {
52 $this->level
= $level;
53 } else if ($this->level
== self
::VIS_AX
&& $level == self
::VIS_PRIVATE
) {
56 $this->level
= self
::VIS_PUBLIC
;
60 public function level()
62 if ($this->level
== null
) {
63 return self
::VIS_PUBLIC
;
69 public function levels()
71 return self
::$v_values[$this->level()];
74 public function isVisible($visibility)
76 return in_array($visibility, $this->levels());
84 const LASTNAME
= 'lastname';
85 const FIRSTNAME
= 'firstname';
86 const NICKNAME
= 'nickname';
87 const PSEUDONYM
= 'pseudonym';
90 const VN_MARITAL
= 'marital';
91 const VN_ORDINARY
= 'ordinary';
92 const VN_OTHER
= 'other';
95 const DN_FULL
= 'directory_name';
96 const DN_DISPLAY
= 'yourself';
97 const DN_YOURSELF
= 'yourself';
98 const DN_DIRECTORY
= 'directory_name';
99 const DN_PRIVATE
= 'private_name';
100 const DN_PUBLIC
= 'public_name';
101 const DN_SHORT
= 'short_name';
102 const DN_SORT
= 'sort_name';
103 /* education related names */
104 const EDU_X
= 'École polytechnique';
105 const DEGREE_X
= 'Ingénieur';
106 const DEGREE_M
= 'Master';
107 const DEGREE_D
= 'Doctorat';
109 static public $name_variants = array(
110 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
111 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
114 const ADDRESS_MAIN
= 0x000001;
115 const ADDRESS_PERSO
= 0x000002;
116 const ADDRESS_PRO
= 0x000004;
117 const ADDRESS_ALL
= 0x000006;
118 const ADDRESS_POSTAL
= 0x000008;
120 const EDUCATION_MAIN
= 0x000010;
121 const EDUCATION_EXTRA
= 0x000020;
122 const EDUCATION_ALL
= 0x000040;
123 const EDUCATION_FINISHED
= 0x000080;
124 const EDUCATION_CURRENT
= 0x000100;
126 const JOBS_MAIN
= 0x001000;
127 const JOBS_ALL
= 0x002000;
128 const JOBS_FINISHED
= 0x004000;
129 const JOBS_CURRENT
= 0x008000;
131 const NETWORKING_ALL
= 0x000000;
132 const NETWORKING_WEB
= 0x010000;
133 const NETWORKING_IM
= 0x020000;
134 const NETWORKING_SOCIAL
= 0x040000;
136 const FETCH_ADDRESSES
= 0x000001;
137 const FETCH_CORPS
= 0x000002;
138 const FETCH_EDU
= 0x000004;
139 const FETCH_JOBS
= 0x000008;
140 const FETCH_MEDALS
= 0x000010;
141 const FETCH_NETWORKING
= 0x000020;
142 const FETCH_MENTOR_SECTOR
= 0x000040;
143 const FETCH_MENTOR_COUNTRY
= 0x000080;
144 const FETCH_PHONES
= 0x000100;
146 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
148 const FETCH_ALL
= 0x0001FF; // OR of FETCH_*
150 private $fetched_fields = 0x000000;
154 private $data = array();
156 private $visibility = null
;
159 private function __construct(array $data)
162 $this->pid
= $this->data
['pid'];
163 $this->hrpid
= $this->data
['hrpid'];
164 $this->visibility
= new ProfileVisibility();
172 public function hrid()
177 public function promo()
182 public function yearpromo()
184 return intval(substr($this->promo
, 1, 4));
187 /** Print a name with the given formatting:
195 public function name($format)
197 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
198 array($this->isFemale() ?
'•' : '',
199 $this->firstName(), $this->lastName(),
200 $this->fullName(), $this->shortName(),
201 $this->promo()), $format);
204 public function fullName($with_promo = false
)
207 return $this->full_name
. ' (' . $this->promo
. ')';
209 return $this->full_name
;
212 public function shortName($with_promo = false
)
215 return $this->short_name
. ' (' . $this->promo
. ')';
217 return $this->short_name
;
220 public function firstName()
222 return $this->firstname
;
225 public function firstNames()
227 return $this->nameVariants(self
::FIRSTNAME
);
230 public function lastName()
232 return $this->lastname
;
235 public function lastNames()
237 return $this->nameVariants(self
::LASTNAME
);
240 public function isFemale()
242 return $this->sex
== PlUser
::GENDER_FEMALE
;
245 public function isDead()
247 return ($this->deathdate
!= null
);
250 public function data()
256 private function nameVariants($type)
258 $vals = array($this->$type);
259 foreach (self
::$name_variants[$type] as $var) {
260 $vartype = $type . '_' . $var;
261 $varname = $this->$vartype;
262 if ($varname != null
&& $varname != "") {
266 return array_unique($vals);
269 public function nationalities()
272 if ($this->nationality1
) {
273 $nats[] = $this->nationality1
;
275 if ($this->nationality2
) {
276 $nats[] = $this->nationality2
;
278 if ($this->nationality3
) {
279 $nats[] = $this->nationality3
;
284 public function __get($name)
286 if (property_exists($this, $name)) {
290 if (isset($this->data
[$name])) {
291 return $this->data
[$name];
297 public function __isset($name)
299 return property_exists($this, $name) ||
isset($this->data
[$name]);
302 /** Sets the level of visibility of the profile
303 * Sets $this->visibility to a list of valid visibilities.
304 * @param one of the self::VIS_* values
306 public function setVisibilityLevel($visibility)
308 $this->visibility
->setLevel($visibility);
311 /** Determine whether an item with visibility $visibility can be displayed
312 * with the current level of visibility of the profile
313 * @param $visibility The level of visibility to be checked
315 public function isVisible($visibility)
317 return $this->visibility
->isVisible($visibility);
320 /** Stores the list of fields which have already been fetched for this Profile
322 public function setFetchedFields($fields)
324 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
325 Platal
::page()->kill("Invalid fetched fields: $fields");
328 $this->fetched_fields
= $fields;
331 private function fetched($field)
333 if (!array_key_exists($field, ProfileField
::$fields)) {
334 Platal
::page()->kill("Invalid field: $field");
337 return ($this->fetched_fields
& $field);
340 /** If not already done, fetches data for the given field
341 * @param $field One of the Profile::FETCH_*
342 * @return A ProfileField, or null
344 private function getProfileField($field)
346 if ($this->fetched($field)) {
349 $this->fetched_fields
= $this->fetched_fields |
$field;
352 $cls = ProfileField
::$fields[$field];
354 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
357 /** Consolidates internal data (addresses, phones, jobs)
359 private function consolidateFields()
361 if ($this->phones
!= null
) {
362 if ($this->addresses
!= null
) {
363 $this->addresses
->addPhones($this->phones
);
366 if ($this->jobs
!= null
) {
367 $this->jobs
->addPhones($this->phones
);
371 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
372 $this->jobs
->addAddresses($this->addresses
);
378 private $photo = null
;
379 public function getPhoto($fallback = true
, $data = false
)
381 if ($this->has_photo
) {
382 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
383 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
385 WHERE pid = {?}', $this->pid
);
386 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
387 } else if ($this->photo
== null
) {
388 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
391 } else if ($fallback) {
392 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
400 private $addresses = null
;
401 public function setAddresses(ProfileAddresses
$addr)
403 $this->addresses
= $addr;
404 $this->consolidateFields();
407 public function getAddresses($flags, $limit = null
)
409 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
410 $this->setAddresses($this->getProfileField(self
::FETCH_ADDRESSES
));
413 if ($this->addresses
== null
) {
416 return $this->addresses
->get($flags, $limit);
419 public function iterAddresses($flags, $limit = null
)
421 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
424 public function getMainAddress()
426 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
427 if (count($addr) == 0) {
430 return array_pop($addr);
436 private $phones = null
;
437 public function setPhones(ProfilePhones
$phones)
439 $this->phones
= $phones;
440 $this->consolidateFields();
443 public function getPhones($flags, $limit = null
)
445 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
446 $this->setPhones($this->getProfileField(self
::FETCH_PHONES
));
449 if ($this->phones
== null
) {
452 return $this->phones
->get($flags, $limit);
457 private $educations = null
;
458 public function setEducations(ProfileEducation
$edu)
460 $this->educations
= $edu;
463 public function getEducations($flags, $limit = null
)
465 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
466 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
469 if ($this->educations
== null
) {
472 return $this->educations
->get($flags, $limit);
475 public function getExtraEducations($limit = null
)
477 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
482 private $corps = null
;
483 public function setCorps(ProfileCorps
$corps)
485 $this->corps
= $corps;
488 public function getCorps()
490 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
491 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
498 private $networks = null
;
499 public function setNetworking(ProfileNetworking
$nw)
501 $this->networks
= $nw;
504 public function getNetworking($flags, $limit = null
)
506 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
507 $this->setNetworking($this->getProfileField(self
::FETCH_NETWORKING
));
509 if ($this->networks
== null
) {
512 return $this->networks
->get($flags, $limit);
515 public function getWebSite()
517 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
518 if (count($site) != 1) {
521 $site = array_pop($site);
522 return $site['address'];
528 private $jobs = null
;
529 public function setJobs(ProfileJobs
$jobs)
532 $this->consolidateFields();
535 public function getJobs($flags, $limit = null
)
537 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
538 $this->setJobs($this->getProfileField(self
::FETCH_JOBS
));
541 if ($this->jobs
== null
) {
544 return $this->jobs
->get($flags, $limit);
547 public function getMainJob()
549 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
550 if (count($job) != 1) {
553 return array_pop($job);
558 private $mentor_sectors = null
;
559 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
561 $this->mentor_sectors
= $sectors;
564 public function getMentoringSectors()
566 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
567 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
570 if ($this->mentor_sectors
== null
) {
573 return $this->mentor_sectors
->sectors
;
577 private $mentor_countries = null
;
578 public function setMentoringCountries(ProfileMentoringCountries
$countries)
580 $this->mentor_countries
= $countries;
583 public function getMentoringCountries()
585 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
586 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
589 if ($this->mentor_countries
== null
) {
592 return $this->mentor_countries
->countries
;
598 public function getBinets()
600 return XDB
::fetchColumn('SELECT binet_id
602 WHERE pid = {?}', $this->id());
604 public function getBinetsNames()
606 return XDB
::fetchColumn('SELECT text
607 FROM profile_binets AS pb
608 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
609 WHERE pb.pid = {?}', $this->id());
614 private $medals = null
;
615 public function setMedals(ProfileMedals
$medals)
617 $this->medals
= $medals;
620 public function getMedals()
622 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
623 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
625 if ($this->medals
== null
) {
628 return $this->medals
->medals
;
631 public function owner()
633 return User
::getSilent($this);
636 public function compareNames($firstname, $lastname)
638 $_lastname = mb_strtoupper($this->lastName());
639 $_firstname = mb_strtoupper($this->firstName());
640 $lastname = mb_strtoupper($lastname);
641 $firstname = mb_strtoupper($firstname);
643 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
644 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
647 foreach ($tokens as $str) {
648 $isOk &= (strpos($_lastname, $str) !== false
);
649 $maxlen = max($maxlen, strlen($str));
652 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
655 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
657 if (count($pids) == 0) {
661 if ($respect_order) {
662 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
667 $visibility = new ProfileVisibility($visibility);
669 $it = XDB
::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, pse.text AS section,
670 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
671 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
672 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
673 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
674 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
675 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
676 ph.x AS photo_width, ph.y AS photo_height,
677 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
678 pm.expertise AS mentor_expertise,
681 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
682 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
683 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
684 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
685 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
686 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
687 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
688 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
689 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
690 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
691 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
692 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
693 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
694 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
695 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
696 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
697 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
698 WHERE p.pid IN ' . XDB
::formatArray($pids) . '
700 ' . $order, $visibility->levels(), $visibility->levels());
701 return new ProfileIterator($it, $pids, $fields, $visibility);
704 public static function getPID($login)
706 if ($login instanceof PlUser
) {
707 return XDB
::fetchOneCell('SELECT pid
708 FROM account_profiles
709 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
711 } else if (ctype_digit($login)) {
712 return XDB
::fetchOneCell('SELECT pid
714 WHERE pid = {?}', $login);
716 return XDB
::fetchOneCell('SELECT pid
718 WHERE hrpid = {?}', $login);
722 public static function getPIDsFromUIDs($uids, $respect_order = true
)
724 if ($respect_order) {
725 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
729 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
730 FROM account_profiles AS ap
731 WHERE FIND_IN_SET(\'owner\', ap.perms)
732 AND ap.uid IN ' . XDB
::formatArray($uids) .'
736 /** Return the profile associated with the given login.
738 public static function get($login, $fields = 0x0000, $visibility = null
)
740 if (is_array($login)) {
741 $pf = new Profile($login);
742 $pf->setVisibilityLevel($visibility);
745 $pid = self
::getPID($login);
746 if (!is_null($pid)) {
747 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
750 /* Let say we can identify a profile using the identifiers of its owner.
752 if (!($login instanceof PlUser
)) {
753 $user = User
::getSilent($login);
754 if ($user && $user->hasProfile()) {
755 return $user->profile();
762 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
764 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
767 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
769 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
772 /** Return profiles for the list of pids.
774 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
776 if (count($pids) == 0) {
779 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
781 while ($p = $it->next()) {
782 $profiles[$p->id()] = $p;
787 /** Return profiles for uids.
789 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
791 if (count($uids) == 0) {
794 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
797 public static function isDisplayName($name)
799 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
800 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
801 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
802 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
805 public static function getNameTypeId($type, $for_sql = false
)
807 if (!S
::has('name_types')) {
808 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
809 FROM profile_name_enum');
810 S
::set('name_types', $table);
812 $table = S
::v('name_types');
815 return XDB
::escape($table[$type]);
817 return $table[$type];
821 public static function rebuildSearchTokens($pid)
823 XDB
::execute('DELETE FROM search_name
826 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
827 FIND_IN_SET('public', e.flags) AS public
828 FROM profile_name AS n
829 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
833 foreach ($keys as $i => $key) {
834 if ($key['name'] == '') {
837 $toks = preg_split('/[ \'\-]+/', $key['name']);
841 $token = strtolower(replace_accent(array_pop($toks) . $token));
842 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
843 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
844 VALUES ({?}, {?}, {?}, {?}, {?})',
845 $token, $pid, soundex_fr($token), $score, $key['public']);
851 /** The school identifier consists of 6 digits. The first 3 represent the
852 * promotion entry year. The last 3 indicate the student's rank.
854 * Our identifier consists of 8 digits and both half have the same role.
855 * This enables us to deal with bigger promotions and with a wider range
858 * getSchoolId returns a school identifier given one of ours.
859 * getXorgId returns a X.org identifier given a school identifier.
861 public static function getSchoolId($xorgId)
863 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
867 $year = intval(substr($xorgId, 0, 4));
868 $rank = intval(substr($xorgId, 5, 3));
871 } elseif ($year < 2000) {
872 $year = intval(substr(1900 - $year, 1, 3));
873 return sprintf('%02u0%03u', $year, $rank);
875 $year = intval(substr(1900 - $year, 1, 3));
876 return sprintf('%03u%03u', $year, $rank);
880 public static function getXorgId($schoolId)
882 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
886 $year = intval(substr($schoolId, 0, 3));
887 $rank = intval(substr($schoolId, 3, 3));
895 return sprintf('%04u%04u', 1900 +
$year, $rank);
901 /** Iterator over a set of Profiles
903 class ProfileIterator
implements PlIterator
905 private $iterator = null
;
909 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
911 require_once 'profilefields.inc.php';
913 if ($visibility == null
) {
914 $visibility = new ProfileVisibility();
917 $this->fields
= $fields;
918 $this->visibility
= $visibility;
921 $callbacks = array();
924 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
925 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
927 if ($fields & Profile
::FETCH_ADDRESSES
) {
928 $callbacks[Profile
::FETCH_ADDRESSES
] = $cb;
929 $subits[Profile
::FETCH_ADDRESSES
] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
932 if ($fields & Profile
::FETCH_CORPS
) {
933 $callbacks[Profile
::FETCH_CORPS
] = $cb;
934 $subits[Profile
::FETCH_CORPS
] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
937 if ($fields & Profile
::FETCH_EDU
) {
938 $callbacks[Profile
::FETCH_EDU
] = $cb;
939 $subits[Profile
::FETCH_EDU
] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
942 if ($fields & Profile
::FETCH_JOBS
) {
943 $callbacks[Profile
::FETCH_JOBS
] = $cb;
944 $subits[Profile
::FETCH_JOBS
] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
947 if ($fields & Profile
::FETCH_MEDALS
) {
948 $callbacks[Profile
::FETCH_MEDALS
] = $cb;
949 $subits[Profile
::FETCH_MEDALS
] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
952 if ($fields & Profile
::FETCH_NETWORKING
) {
953 $callbacks[Profile
::FETCH_NETWORKING
] = $cb;
954 $subits[Profile
::FETCH_NETWORKING
] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
957 if ($fields & Profile
::FETCH_PHONES
) {
958 $callbacks[Profile
::FETCH_PHONES
] = $cb;
959 $subits[Profile
::FETCH_PHONES
] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
962 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
965 private function hasData($field, $vals)
967 return ($this->fields
& $field) && ($vals[$field] != null
);
970 private function fillProfile(array $vals)
972 $pf = Profile
::get($vals[0]);
973 $pf->setVisibilityLevel($this->visibility
->level());
974 $pf->setFetchedFields($this->fields
);
976 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
977 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
979 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
980 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
982 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
983 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
986 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
987 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
989 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
990 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
992 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
993 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
995 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
996 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1002 public function next()
1004 $vals = $this->iterator
->next();
1005 if ($vals == null
) {
1008 return $this->fillProfile($vals);
1011 public function first()
1013 return $this->iterator
->first();
1016 public function last()
1018 return $this->iterator
->last();
1021 public function total()
1023 return $this->iterator
->total();
1027 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: