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;
155 private $owner_fetched = false
;
156 private $data = array();
158 private $visibility = null
;
161 private function __construct(array $data)
164 $this->pid
= $this->data
['pid'];
165 $this->hrpid
= $this->data
['hrpid'];
166 $this->visibility
= new ProfileVisibility();
174 public function hrid()
179 public function owner()
181 if ($this->owner
== null
&& !$this->owner_fetched
) {
182 $this->owner_fetched
= true
;
183 $this->owner
= User
::getSilent($this);
188 public function promo()
193 public function yearpromo()
195 return intval(substr($this->promo
, 1, 4));
198 public function mainEducation()
200 return $this->promo
{0};
203 public function mainEducationDuration()
205 switch ($this->mainEducation()) {
217 /** Print a name with the given formatting:
225 public function name($format)
227 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
228 array($this->isFemale() ?
'•' : '',
229 $this->firstName(), $this->lastName(),
230 $this->fullName(), $this->shortName(),
231 $this->promo()), $format);
234 public function fullName($with_promo = false
)
237 return $this->full_name
. ' (' . $this->promo
. ')';
239 return $this->full_name
;
242 public function shortName($with_promo = false
)
245 return $this->short_name
. ' (' . $this->promo
. ')';
247 return $this->short_name
;
250 public function firstName()
252 return $this->firstname
;
255 public function firstNames()
257 return $this->nameVariants(self
::FIRSTNAME
);
260 public function lastName()
262 return $this->lastname
;
265 public function lastNames()
267 return $this->nameVariants(self
::LASTNAME
);
270 public function isFemale()
272 return $this->sex
== PlUser
::GENDER_FEMALE
;
275 public function isDead()
277 return ($this->deathdate
!= null
);
280 public function displayEmail()
284 return $o->bestEmail();
286 return $this->email_directory
;
290 public function data()
296 private function nameVariants($type)
298 $vals = array($this->$type);
299 foreach (self
::$name_variants[$type] as $var) {
300 $vartype = $type . '_' . $var;
301 $varname = $this->$vartype;
302 if ($varname != null
&& $varname != "") {
306 return array_unique($vals);
309 public function nationalities()
312 if ($this->nationality1
) {
313 $nats[] = $this->nationality1
;
315 if ($this->nationality2
) {
316 $nats[] = $this->nationality2
;
318 if ($this->nationality3
) {
319 $nats[] = $this->nationality3
;
324 public function __get($name)
326 if (property_exists($this, $name)) {
330 if (isset($this->data
[$name])) {
331 return $this->data
[$name];
337 public function __isset($name)
339 return property_exists($this, $name) ||
isset($this->data
[$name]);
342 /** Sets the level of visibility of the profile
343 * Sets $this->visibility to a list of valid visibilities.
344 * @param one of the self::VIS_* values
346 public function setVisibilityLevel($visibility)
348 $this->visibility
->setLevel($visibility);
351 /** Determine whether an item with visibility $visibility can be displayed
352 * with the current level of visibility of the profile
353 * @param $visibility The level of visibility to be checked
355 public function isVisible($visibility)
357 return $this->visibility
->isVisible($visibility);
360 /** Stores the list of fields which have already been fetched for this Profile
362 public function setFetchedFields($fields)
364 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
365 Platal
::page()->kill("Invalid fetched fields: $fields");
368 $this->fetched_fields
= $fields;
371 private function fetched($field)
373 if (!array_key_exists($field, ProfileField
::$fields)) {
374 Platal
::page()->kill("Invalid field: $field");
377 return ($this->fetched_fields
& $field);
380 /** If not already done, fetches data for the given field
381 * @param $field One of the Profile::FETCH_*
382 * @return A ProfileField, or null
384 private function getProfileField($field)
386 if ($this->fetched($field)) {
389 $this->fetched_fields
= $this->fetched_fields |
$field;
392 $cls = ProfileField
::$fields[$field];
394 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
397 /** Consolidates internal data (addresses, phones, jobs)
399 private function consolidateFields()
401 if ($this->phones
!= null
) {
402 if ($this->addresses
!= null
) {
403 $this->addresses
->addPhones($this->phones
);
406 if ($this->jobs
!= null
) {
407 $this->jobs
->addPhones($this->phones
);
411 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
412 $this->jobs
->addAddresses($this->addresses
);
418 private $photo = null
;
419 public function getPhoto($fallback = true
, $data = false
)
421 if ($this->has_photo
) {
422 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
423 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
425 WHERE pid = {?}', $this->pid
);
426 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
427 } else if ($this->photo
== null
) {
428 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
431 } else if ($fallback) {
432 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
440 private $addresses = null
;
441 public function setAddresses(ProfileAddresses
$addr)
443 $this->addresses
= $addr;
444 $this->consolidateFields();
447 public function getAddresses($flags, $limit = null
)
449 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
450 $this->setAddresses($this->getProfileField(self
::FETCH_ADDRESSES
));
453 if ($this->addresses
== null
) {
456 return $this->addresses
->get($flags, $limit);
459 public function iterAddresses($flags, $limit = null
)
461 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
464 public function getMainAddress()
466 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
467 if (count($addr) == 0) {
470 return array_pop($addr);
476 private $phones = null
;
477 public function setPhones(ProfilePhones
$phones)
479 $this->phones
= $phones;
480 $this->consolidateFields();
483 public function getPhones($flags, $limit = null
)
485 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
486 $this->setPhones($this->getProfileField(self
::FETCH_PHONES
));
489 if ($this->phones
== null
) {
492 return $this->phones
->get($flags, $limit);
497 private $educations = null
;
498 public function setEducations(ProfileEducation
$edu)
500 $this->educations
= $edu;
503 public function getEducations($flags, $limit = null
)
505 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
506 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
509 if ($this->educations
== null
) {
512 return $this->educations
->get($flags, $limit);
515 public function getExtraEducations($limit = null
)
517 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
522 private $corps = null
;
523 public function setCorps(ProfileCorps
$corps)
525 $this->corps
= $corps;
528 public function getCorps()
530 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
531 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
538 private $networks = null
;
539 public function setNetworking(ProfileNetworking
$nw)
541 $this->networks
= $nw;
544 public function getNetworking($flags, $limit = null
)
546 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
547 $this->setNetworking($this->getProfileField(self
::FETCH_NETWORKING
));
549 if ($this->networks
== null
) {
552 return $this->networks
->get($flags, $limit);
555 public function getWebSite()
557 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
558 if (count($site) != 1) {
561 $site = array_pop($site);
562 return $site['address'];
568 private $jobs = null
;
569 public function setJobs(ProfileJobs
$jobs)
572 $this->consolidateFields();
575 public function getJobs($flags, $limit = null
)
577 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
578 $this->setJobs($this->getProfileField(self
::FETCH_JOBS
));
581 if ($this->jobs
== null
) {
584 return $this->jobs
->get($flags, $limit);
587 public function getMainJob()
589 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
590 if (count($job) != 1) {
593 return array_pop($job);
598 private $mentor_sectors = null
;
599 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
601 $this->mentor_sectors
= $sectors;
604 public function getMentoringSectors()
606 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
607 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
610 if ($this->mentor_sectors
== null
) {
613 return $this->mentor_sectors
->sectors
;
617 private $mentor_countries = null
;
618 public function setMentoringCountries(ProfileMentoringCountries
$countries)
620 $this->mentor_countries
= $countries;
623 public function getMentoringCountries()
625 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
626 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
629 if ($this->mentor_countries
== null
) {
632 return $this->mentor_countries
->countries
;
638 public function getBinets()
640 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
641 return XDB
::fetchColumn('SELECT binet_id
643 WHERE pid = {?}', $this->id());
648 public function getBinetsNames()
650 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
651 return XDB
::fetchColumn('SELECT text
652 FROM profile_binets AS pb
653 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
654 WHERE pb.pid = {?}', $this->id());
662 private $medals = null
;
663 public function setMedals(ProfileMedals
$medals)
665 $this->medals
= $medals;
668 public function getMedals()
670 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
671 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
673 if ($this->medals
== null
) {
676 return $this->medals
->medals
;
679 public function compareNames($firstname, $lastname)
681 $_lastname = mb_strtoupper($this->lastName());
682 $_firstname = mb_strtoupper($this->firstName());
683 $lastname = mb_strtoupper($lastname);
684 $firstname = mb_strtoupper($firstname);
686 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
687 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
690 foreach ($tokens as $str) {
691 $isOk &= (strpos($_lastname, $str) !== false
);
692 $maxlen = max($maxlen, strlen($str));
695 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
698 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
700 if (count($pids) == 0) {
704 if ($respect_order) {
705 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
710 $visibility = new ProfileVisibility($visibility);
712 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
713 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
714 p.cv, p.medals_pub, p.alias_pub, p.email_directory, p.last_change,
715 p.nationality1, p.nationality2, p.nationality3,
716 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
717 pe.entry_year, pe.grad_year,
718 IF ({?} IN {?}, pse.text, NULL) AS section,
719 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
720 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
721 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
722 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
723 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
724 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
725 ph.x AS photo_width, ph.y AS photo_height,
726 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
727 pm.expertise AS mentor_expertise,
730 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
731 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
732 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
733 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
734 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
735 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
736 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
737 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
738 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
739 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
740 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
741 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
742 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
743 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
744 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
745 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
746 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
750 $visibility->levels(),
751 ProfileVisibility
::VIS_PRIVATE
, $visibility->levels(),
752 $visibility->levels(), $visibility->levels(),
755 return new ProfileIterator($it, $pids, $fields, $visibility);
758 public static function getPID($login)
760 if ($login instanceof PlUser
) {
761 return XDB
::fetchOneCell('SELECT pid
762 FROM account_profiles
763 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
765 } else if (ctype_digit($login)) {
766 return XDB
::fetchOneCell('SELECT pid
768 WHERE pid = {?}', $login);
770 return XDB
::fetchOneCell('SELECT pid
772 WHERE hrpid = {?}', $login);
776 public static function getPIDsFromUIDs($uids, $respect_order = true
)
778 if ($respect_order) {
779 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
783 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
784 FROM account_profiles AS ap
785 WHERE FIND_IN_SET(\'owner\', ap.perms)
786 AND ap.uid IN ' . XDB
::formatArray($uids) .'
790 /** Return the profile associated with the given login.
792 public static function get($login, $fields = 0x0000, $visibility = null
)
794 if (is_array($login)) {
795 $pf = new Profile($login);
796 $pf->setVisibilityLevel($visibility);
799 $pid = self
::getPID($login);
800 if (!is_null($pid)) {
801 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
804 /* Let say we can identify a profile using the identifiers of its owner.
806 if (!($login instanceof PlUser
)) {
807 $user = User
::getSilent($login);
808 if ($user && $user->hasProfile()) {
809 return $user->profile();
816 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
818 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
821 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
823 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
826 /** Return profiles for the list of pids.
828 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
830 if (count($pids) == 0) {
833 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
835 while ($p = $it->next()) {
836 $profiles[$p->id()] = $p;
841 /** Return profiles for uids.
843 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
845 if (count($uids) == 0) {
848 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
851 public static function isDisplayName($name)
853 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
854 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
855 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
856 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
859 public static function getNameTypeId($type, $for_sql = false
)
861 if (!S
::has('name_types')) {
862 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
863 FROM profile_name_enum');
864 S
::set('name_types', $table);
866 $table = S
::v('name_types');
869 return XDB
::escape($table[$type]);
871 return $table[$type];
875 public static function rebuildSearchTokens($pid)
877 XDB
::execute('DELETE FROM search_name
880 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
881 FIND_IN_SET('public', e.flags) AS public
882 FROM profile_name AS n
883 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
887 foreach ($keys as $i => $key) {
888 if ($key['name'] == '') {
891 $toks = preg_split('/[ \'\-]+/', $key['name']);
895 $token = strtolower(replace_accent(array_pop($toks) . $token));
896 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
897 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
898 VALUES ({?}, {?}, {?}, {?}, {?})',
899 $token, $pid, soundex_fr($token), $score, $key['public']);
905 /** The school identifier consists of 6 digits. The first 3 represent the
906 * promotion entry year. The last 3 indicate the student's rank.
908 * Our identifier consists of 8 digits and both half have the same role.
909 * This enables us to deal with bigger promotions and with a wider range
912 * getSchoolId returns a school identifier given one of ours.
913 * getXorgId returns a X.org identifier given a school identifier.
915 public static function getSchoolId($xorgId)
917 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
921 $year = intval(substr($xorgId, 0, 4));
922 $rank = intval(substr($xorgId, 5, 3));
925 } elseif ($year < 2000) {
926 $year = intval(substr(1900 - $year, 1, 3));
927 return sprintf('%02u0%03u', $year, $rank);
929 $year = intval(substr(1900 - $year, 1, 3));
930 return sprintf('%03u%03u', $year, $rank);
934 public static function getXorgId($schoolId)
936 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
940 $year = intval(substr($schoolId, 0, 3));
941 $rank = intval(substr($schoolId, 3, 3));
949 return sprintf('%04u%04u', 1900 +
$year, $rank);
955 /** Iterator over a set of Profiles
957 class ProfileIterator
implements PlIterator
959 private $iterator = null
;
963 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
965 require_once 'profilefields.inc.php';
967 if ($visibility == null
) {
968 $visibility = new ProfileVisibility();
971 $this->fields
= $fields;
972 $this->visibility
= $visibility;
975 $callbacks = array();
978 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
979 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
981 if ($fields & Profile
::FETCH_ADDRESSES
) {
982 $callbacks[Profile
::FETCH_ADDRESSES
] = $cb;
983 $subits[Profile
::FETCH_ADDRESSES
] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
986 if ($fields & Profile
::FETCH_CORPS
) {
987 $callbacks[Profile
::FETCH_CORPS
] = $cb;
988 $subits[Profile
::FETCH_CORPS
] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
991 if ($fields & Profile
::FETCH_EDU
) {
992 $callbacks[Profile
::FETCH_EDU
] = $cb;
993 $subits[Profile
::FETCH_EDU
] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
996 if ($fields & Profile
::FETCH_JOBS
) {
997 $callbacks[Profile
::FETCH_JOBS
] = $cb;
998 $subits[Profile
::FETCH_JOBS
] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
1001 if ($fields & Profile
::FETCH_MEDALS
) {
1002 $callbacks[Profile
::FETCH_MEDALS
] = $cb;
1003 $subits[Profile
::FETCH_MEDALS
] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
1006 if ($fields & Profile
::FETCH_NETWORKING
) {
1007 $callbacks[Profile
::FETCH_NETWORKING
] = $cb;
1008 $subits[Profile
::FETCH_NETWORKING
] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
1011 if ($fields & Profile
::FETCH_PHONES
) {
1012 $callbacks[Profile
::FETCH_PHONES
] = $cb;
1013 $subits[Profile
::FETCH_PHONES
] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1016 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1019 private function hasData($field, $vals)
1021 return ($this->fields
& $field) && ($vals[$field] != null
);
1024 private function fillProfile(array $vals)
1026 $pf = Profile
::get($vals[0]);
1027 $pf->setVisibilityLevel($this->visibility
->level());
1028 $pf->setFetchedFields($this->fields
);
1030 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1031 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1033 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1034 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1036 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1037 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1040 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1041 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1043 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1044 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1046 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1047 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1049 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1050 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1056 public function next()
1058 $vals = $this->iterator
->next();
1059 if ($vals == null
) {
1062 return $this->fillProfile($vals);
1065 public function first()
1067 return $this->iterator
->first();
1070 public function last()
1072 return $this->iterator
->last();
1075 public function total()
1077 return $this->iterator
->total();
1081 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: