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 /** Check if user is an orange (associated with several promos)
200 public function isMultiPromo()
202 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
205 /** Returns an array with all associated promo years.
207 public function yearspromo()
210 $d = -$this->deltaPromoToGradYear();
211 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
217 public function mainEducation()
219 if (empty($this->promo
)) {
222 return $this->promo
{0};
226 public function mainGrade()
228 switch ($this->mainEducation()) {
230 return UserFilter
::GRADE_ING
;
232 return UserFilter
::GRADE_MST
;
234 return UserFilter
::GRADE_PHD
;
240 public function mainEducationDuration()
242 switch ($this->mainEducation()) {
254 /** Number of years between the promotion year until the
255 * graduation year. In standard schools it's 0, but for
256 * Polytechnique the promo year is the entry year.
258 public function deltaPromoToGradYear()
260 if ($this->mainEducation() == 'X') {
261 return $this->mainEducationDuration();
266 /** Print a name with the given formatting:
274 public function name($format)
276 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
277 array($this->isFemale() ?
'•' : '',
278 $this->firstName(), $this->lastName(),
279 $this->fullName(), $this->shortName(),
280 $this->promo()), $format);
283 public function fullName($with_promo = false
)
286 return $this->full_name
. ' (' . $this->promo
. ')';
288 return $this->full_name
;
291 public function shortName($with_promo = false
)
294 return $this->short_name
. ' (' . $this->promo
. ')';
296 return $this->short_name
;
299 public function firstName()
301 return $this->firstname
;
304 public function firstNames()
306 return $this->nameVariants(self
::FIRSTNAME
);
309 public function lastName()
311 return $this->lastname
;
314 public function lastNames()
316 return $this->nameVariants(self
::LASTNAME
);
319 public function isFemale()
321 return $this->sex
== PlUser
::GENDER_FEMALE
;
324 public function isDead()
326 return ($this->deathdate
!= null
);
329 public function displayEmail()
333 return $o->bestEmail();
335 return $this->email_directory
;
339 public function data()
345 private function nameVariants($type)
347 $vals = array($this->$type);
348 foreach (self
::$name_variants[$type] as $var) {
349 $vartype = $type . '_' . $var;
350 $varname = $this->$vartype;
351 if ($varname != null
&& $varname != "") {
355 return array_unique($vals);
358 public function nationalities()
361 if ($this->nationality1
) {
362 $nats[] = $this->nationality1
;
364 if ($this->nationality2
) {
365 $nats[] = $this->nationality2
;
367 if ($this->nationality3
) {
368 $nats[] = $this->nationality3
;
373 public function __get($name)
375 if (property_exists($this, $name)) {
379 if (isset($this->data
[$name])) {
380 return $this->data
[$name];
386 public function __isset($name)
388 return property_exists($this, $name) ||
isset($this->data
[$name]);
391 public function __unset($name)
393 if (property_exists($this, $name)) {
396 unset($this->data
[$name]);
401 /** Sets the level of visibility of the profile
402 * Sets $this->visibility to a list of valid visibilities.
403 * @param one of the self::VIS_* values
405 public function setVisibilityLevel($visibility)
407 $this->visibility
->setLevel($visibility);
410 /** Determine whether an item with visibility $visibility can be displayed
411 * with the current level of visibility of the profile
412 * @param $visibility The level of visibility to be checked
414 public function isVisible($visibility)
416 return $this->visibility
->isVisible($visibility);
419 /** Stores the list of fields which have already been fetched for this Profile
421 public function setFetchedFields($fields)
423 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
424 Platal
::page()->kill("Invalid fetched fields: $fields");
427 $this->fetched_fields
= $fields;
430 private function fetched($field)
432 if (!array_key_exists($field, ProfileField
::$fields)) {
433 Platal
::page()->kill("Invalid field: $field");
436 return ($this->fetched_fields
& $field);
439 /** If not already done, fetches data for the given field
440 * @param $field One of the Profile::FETCH_*
441 * @return A ProfileField, or null
443 private function getProfileField($field)
445 if ($this->fetched($field)) {
448 $this->fetched_fields
= $this->fetched_fields |
$field;
451 $cls = ProfileField
::$fields[$field];
453 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
456 /** Consolidates internal data (addresses, phones, jobs)
458 private function consolidateFields()
460 if ($this->phones
!= null
) {
461 if ($this->addresses
!= null
) {
462 $this->addresses
->addPhones($this->phones
);
465 if ($this->jobs
!= null
) {
466 $this->jobs
->addPhones($this->phones
);
470 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
471 $this->jobs
->addAddresses($this->addresses
);
477 private $photo = null
;
478 public function getPhoto($fallback = true
, $data = false
)
480 if ($this->has_photo
) {
481 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
482 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
484 WHERE pid = {?}', $this->pid
);
485 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
486 } else if ($this->photo
== null
) {
487 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
490 } else if ($fallback) {
491 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
499 private $addresses = null
;
500 public function setAddresses(ProfileAddresses
$addr)
502 $this->addresses
= $addr;
503 $this->consolidateFields();
506 public function getAddresses($flags, $limit = null
)
508 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
509 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
511 $this->setAddresses($addr);
515 if ($this->addresses
== null
) {
518 return $this->addresses
->get($flags, $limit);
521 public function iterAddresses($flags, $limit = null
)
523 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
526 public function getMainAddress()
528 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
529 if (count($addr) == 0) {
532 return array_pop($addr);
538 private $phones = null
;
539 public function setPhones(ProfilePhones
$phones)
541 $this->phones
= $phones;
542 $this->consolidateFields();
545 public function getPhones($flags, $limit = null
)
547 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
548 $this->setPhones($this->getProfileField(self
::FETCH_PHONES
));
551 if ($this->phones
== null
) {
554 return $this->phones
->get($flags, $limit);
559 private $educations = null
;
560 public function setEducations(ProfileEducation
$edu)
562 $this->educations
= $edu;
565 public function getEducations($flags, $limit = null
)
567 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
568 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
571 if ($this->educations
== null
) {
574 return $this->educations
->get($flags, $limit);
577 public function getExtraEducations($limit = null
)
579 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
584 private $corps = null
;
585 public function setCorps(ProfileCorps
$corps)
587 $this->corps
= $corps;
590 public function getCorps()
592 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
593 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
600 private $networks = null
;
601 public function setNetworking(ProfileNetworking
$nw)
603 $this->networks
= $nw;
606 public function getNetworking($flags, $limit = null
)
608 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
609 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
611 $this->setNetworking($nw);
614 if ($this->networks
== null
) {
617 return $this->networks
->get($flags, $limit);
620 public function getWebSite()
622 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
623 if (count($site) != 1) {
626 $site = array_pop($site);
633 private $jobs = null
;
634 public function setJobs(ProfileJobs
$jobs)
637 $this->consolidateFields();
640 public function getJobs($flags, $limit = null
)
642 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
643 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
645 $this->setJobs($jobs);
649 if ($this->jobs
== null
) {
652 return $this->jobs
->get($flags, $limit);
655 public function getMainJob()
657 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
658 if (count($job) != 1) {
661 return array_pop($job);
666 private $mentor_sectors = null
;
667 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
669 $this->mentor_sectors
= $sectors;
672 public function getMentoringSectors()
674 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
675 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
678 if ($this->mentor_sectors
== null
) {
681 return $this->mentor_sectors
->sectors
;
685 private $mentor_countries = null
;
686 public function setMentoringCountries(ProfileMentoringCountries
$countries)
688 $this->mentor_countries
= $countries;
691 public function getMentoringCountries()
693 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
694 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
697 if ($this->mentor_countries
== null
) {
700 return $this->mentor_countries
->countries
;
706 public function getBinets()
708 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
709 return XDB
::fetchColumn('SELECT binet_id
711 WHERE pid = {?}', $this->id());
716 public function getBinetsNames()
718 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
719 return XDB
::fetchColumn('SELECT text
720 FROM profile_binets AS pb
721 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
722 WHERE pb.pid = {?}', $this->id());
730 private $medals = null
;
731 public function setMedals(ProfileMedals
$medals)
733 $this->medals
= $medals;
736 public function getMedals()
738 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
739 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
741 if ($this->medals
== null
) {
744 return $this->medals
->medals
;
747 public function compareNames($firstname, $lastname)
749 $_lastname = mb_strtoupper($this->lastName());
750 $_firstname = mb_strtoupper($this->firstName());
751 $lastname = mb_strtoupper($lastname);
752 $firstname = mb_strtoupper($firstname);
754 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
755 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
758 foreach ($tokens as $str) {
759 $isOk &= (strpos($_lastname, $str) !== false
);
760 $maxlen = max($maxlen, strlen($str));
763 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
766 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
768 if (count($pids) == 0) {
772 if ($respect_order) {
773 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
778 $visibility = new ProfileVisibility($visibility);
780 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
781 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
782 p.cv, p.medals_pub, p.alias_pub, p.email_directory, p.last_change,
783 p.nationality1, p.nationality2, p.nationality3,
784 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
785 pe.entry_year, pe.grad_year,
786 IF ({?} IN {?}, pse.text, NULL) AS section,
787 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
788 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
789 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
790 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
791 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
792 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
793 ph.x AS photo_width, ph.y AS photo_height,
794 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
795 pm.expertise AS mentor_expertise,
798 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
799 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
800 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
801 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
802 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
803 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
804 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
805 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
806 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
807 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
808 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
809 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
810 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
811 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
812 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
813 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
814 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
818 $visibility->levels(),
819 ProfileVisibility
::VIS_PRIVATE
, $visibility->levels(),
820 $visibility->levels(), $visibility->levels(),
823 return new ProfileIterator($it, $pids, $fields, $visibility);
826 public static function getPID($login)
828 if ($login instanceof PlUser
) {
829 return XDB
::fetchOneCell('SELECT pid
830 FROM account_profiles
831 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
833 } else if (ctype_digit($login)) {
834 return XDB
::fetchOneCell('SELECT pid
836 WHERE pid = {?}', $login);
838 return XDB
::fetchOneCell('SELECT pid
840 WHERE hrpid = {?}', $login);
844 public static function getPIDsFromUIDs($uids, $respect_order = true
)
846 if ($respect_order) {
847 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
851 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
852 FROM account_profiles AS ap
853 WHERE FIND_IN_SET(\'owner\', ap.perms)
854 AND ap.uid IN ' . XDB
::formatArray($uids) .'
858 /** Return the profile associated with the given login.
860 public static function get($login, $fields = 0x0000, $visibility = null
)
862 if (is_array($login)) {
863 $pf = new Profile($login);
864 $pf->setVisibilityLevel($visibility);
867 $pid = self
::getPID($login);
868 if (!is_null($pid)) {
869 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
872 /* Let say we can identify a profile using the identifiers of its owner.
874 if (!($login instanceof PlUser
)) {
875 $user = User
::getSilent($login);
876 if ($user && $user->hasProfile()) {
877 return $user->profile();
884 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
886 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
889 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
891 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
894 /** Return profiles for the list of pids.
896 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
898 if (count($pids) == 0) {
901 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
903 while ($p = $it->next()) {
904 $profiles[$p->id()] = $p;
909 /** Return profiles for uids.
911 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
913 if (count($uids) == 0) {
916 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
919 public static function isDisplayName($name)
921 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
922 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
923 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
924 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
927 public static function getNameTypeId($type, $for_sql = false
)
929 if (!S
::has('name_types')) {
930 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
931 FROM profile_name_enum');
932 S
::set('name_types', $table);
934 $table = S
::v('name_types');
937 return XDB
::escape($table[$type]);
939 return $table[$type];
943 public static function rebuildSearchTokens($pid)
945 XDB
::execute('DELETE FROM search_name
948 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
949 FIND_IN_SET('public', e.flags) AS public
950 FROM profile_name AS n
951 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
955 while ($key = $keys->next()) {
956 if ($key['name'] == '') {
959 $toks = preg_split('/[ \'\-]+/', $key['name']);
963 $token = strtolower(replace_accent(array_pop($toks) . $token));
964 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
965 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
966 VALUES ({?}, {?}, {?}, {?}, {?})',
967 $token, $pid, soundex_fr($token), $score, $key['public']);
973 /** The school identifier consists of 6 digits. The first 3 represent the
974 * promotion entry year. The last 3 indicate the student's rank.
976 * Our identifier consists of 8 digits and both half have the same role.
977 * This enables us to deal with bigger promotions and with a wider range
980 * getSchoolId returns a school identifier given one of ours.
981 * getXorgId returns a X.org identifier given a school identifier.
983 public static function getSchoolId($xorgId)
985 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
989 $year = intval(substr($xorgId, 0, 4));
990 $rank = intval(substr($xorgId, 5, 3));
993 } elseif ($year < 2000) {
994 $year = intval(substr(1900 - $year, 1, 3));
995 return sprintf('%02u0%03u', $year, $rank);
997 $year = intval(substr(1900 - $year, 1, 3));
998 return sprintf('%03u%03u', $year, $rank);
1002 public static function getXorgId($schoolId)
1004 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1008 $year = intval(substr($schoolId, 0, 3));
1009 $rank = intval(substr($schoolId, 3, 3));
1017 return sprintf('%04u%04u', 1900 +
$year, $rank);
1023 /** Iterator over a set of Profiles
1025 class ProfileIterator
implements PlIterator
1027 private $iterator = null
;
1029 private $visibility;
1031 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1033 require_once 'profilefields.inc.php';
1035 if ($visibility == null
) {
1036 $visibility = new ProfileVisibility();
1039 $this->fields
= $fields;
1040 $this->visibility
= $visibility;
1043 $callbacks = array();
1046 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1047 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1049 if ($fields & Profile
::FETCH_ADDRESSES
) {
1050 $callbacks[Profile
::FETCH_ADDRESSES
] = $cb;
1051 $subits[Profile
::FETCH_ADDRESSES
] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
1054 if ($fields & Profile
::FETCH_CORPS
) {
1055 $callbacks[Profile
::FETCH_CORPS
] = $cb;
1056 $subits[Profile
::FETCH_CORPS
] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
1059 if ($fields & Profile
::FETCH_EDU
) {
1060 $callbacks[Profile
::FETCH_EDU
] = $cb;
1061 $subits[Profile
::FETCH_EDU
] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
1064 if ($fields & Profile
::FETCH_JOBS
) {
1065 $callbacks[Profile
::FETCH_JOBS
] = $cb;
1066 $subits[Profile
::FETCH_JOBS
] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
1069 if ($fields & Profile
::FETCH_MEDALS
) {
1070 $callbacks[Profile
::FETCH_MEDALS
] = $cb;
1071 $subits[Profile
::FETCH_MEDALS
] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
1074 if ($fields & Profile
::FETCH_NETWORKING
) {
1075 $callbacks[Profile
::FETCH_NETWORKING
] = $cb;
1076 $subits[Profile
::FETCH_NETWORKING
] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
1079 if ($fields & Profile
::FETCH_PHONES
) {
1080 $callbacks[Profile
::FETCH_PHONES
] = $cb;
1081 $subits[Profile
::FETCH_PHONES
] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1084 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1087 private function hasData($field, $vals)
1089 return ($this->fields
& $field) && ($vals[$field] != null
);
1092 private function fillProfile(array $vals)
1094 $pf = Profile
::get($vals[0]);
1095 $pf->setVisibilityLevel($this->visibility
->level());
1096 $pf->setFetchedFields($this->fields
);
1098 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1099 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1101 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1102 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1104 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1105 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1108 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1109 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1111 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1112 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1114 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1115 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1117 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1118 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1124 public function next()
1126 $vals = $this->iterator
->next();
1127 if ($vals == null
) {
1130 return $this->fillProfile($vals);
1133 public function first()
1135 return $this->iterator
->first();
1138 public function last()
1140 return $this->iterator
->last();
1143 public function total()
1145 return $this->iterator
->total();
1149 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: