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
= 0x00000001;
115 const ADDRESS_PERSO
= 0x00000002;
116 const ADDRESS_PRO
= 0x00000004;
117 const ADDRESS_ALL
= 0x00000006;
118 const ADDRESS_POSTAL
= 0x00000008;
120 const EDUCATION_MAIN
= 0x00000010;
121 const EDUCATION_EXTRA
= 0x00000020;
122 const EDUCATION_ALL
= 0x00000040;
123 const EDUCATION_FINISHED
= 0x00000080;
124 const EDUCATION_CURRENT
= 0x00000100;
126 const JOBS_MAIN
= 0x00001000;
127 const JOBS_ALL
= 0x00002000;
128 const JOBS_FINISHED
= 0x00004000;
129 const JOBS_CURRENT
= 0x00008000;
131 const NETWORKING_ALL
= 0x00070000;
132 const NETWORKING_WEB
= 0x00010000;
133 const NETWORKING_IM
= 0x00020000;
134 const NETWORKING_SOCIAL
= 0x00040000;
136 const PHONE_LINK_JOB
= 0x00100000;
137 const PHONE_LINK_ADDRESS
= 0x00200000;
138 const PHONE_LINK_PROFILE
= 0x00400000;
139 const PHONE_LINK_COMPANY
= 0x00800000;
140 const PHONE_LINK_ANY
= 0x00F00000;
142 const PHONE_TYPE_FAX
= 0x01000000;
143 const PHONE_TYPE_FIXED
= 0x02000000;
144 const PHONE_TYPE_MOBILE
= 0x04000000;
145 const PHONE_TYPE_ANY
= 0x07000000;
147 const PHONE_ANY
= 0x07F00000;
149 const FETCH_ADDRESSES
= 0x000001;
150 const FETCH_CORPS
= 0x000002;
151 const FETCH_EDU
= 0x000004;
152 const FETCH_JOBS
= 0x000008;
153 const FETCH_MEDALS
= 0x000010;
154 const FETCH_NETWORKING
= 0x000020;
155 const FETCH_MENTOR_SECTOR
= 0x000040;
156 const FETCH_MENTOR_COUNTRY
= 0x000080;
157 const FETCH_PHONES
= 0x000100;
159 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
161 const FETCH_ALL
= 0x0001FF; // OR of FETCH_*
163 private $fetched_fields = 0x000000;
168 private $owner_fetched = false
;
169 private $data = array();
171 private $visibility = null
;
174 private function __construct(array $data)
177 $this->pid
= $this->data
['pid'];
178 $this->hrpid
= $this->data
['hrpid'];
179 $this->visibility
= new ProfileVisibility();
187 public function hrid()
192 public function owner()
194 if ($this->owner
== null
&& !$this->owner_fetched
) {
195 $this->owner_fetched
= true
;
196 $this->owner
= User
::getSilent($this);
201 public function promo()
206 public function yearpromo()
208 return intval(substr($this->promo
, 1, 4));
211 /** Check if user is an orange (associated with several promos)
213 public function isMultiPromo()
215 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
218 /** Returns an array with all associated promo years.
220 public function yearspromo()
223 $d = -$this->deltaPromoToGradYear();
224 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
230 public function mainEducation()
232 if (empty($this->promo
)) {
235 return $this->promo
{0};
239 public function mainGrade()
241 switch ($this->mainEducation()) {
243 return UserFilter
::GRADE_ING
;
245 return UserFilter
::GRADE_MST
;
247 return UserFilter
::GRADE_PHD
;
253 public function mainEducationDuration()
255 switch ($this->mainEducation()) {
267 /** Number of years between the promotion year until the
268 * graduation year. In standard schools it's 0, but for
269 * Polytechnique the promo year is the entry year.
271 public function deltaPromoToGradYear()
273 if ($this->mainEducation() == 'X') {
274 return $this->mainEducationDuration();
279 /** Print a name with the given formatting:
287 public function name($format)
289 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
290 array($this->isFemale() ?
'•' : '',
291 $this->firstName(), $this->lastName(),
292 $this->fullName(), $this->shortName(),
293 $this->promo()), $format);
296 public function fullName($with_promo = false
)
299 return $this->full_name
. ' (' . $this->promo
. ')';
301 return $this->full_name
;
304 public function shortName($with_promo = false
)
307 return $this->short_name
. ' (' . $this->promo
. ')';
309 return $this->short_name
;
312 public function firstName()
314 return $this->firstname
;
317 public function firstNames()
319 return $this->nameVariants(self
::FIRSTNAME
);
322 public function lastName()
324 return $this->lastname
;
327 public function lastNames()
329 return $this->nameVariants(self
::LASTNAME
);
332 public function isFemale()
334 return $this->sex
== PlUser
::GENDER_FEMALE
;
337 public function isDead()
339 return ($this->deathdate
!= null
);
342 public function displayEmail()
346 return $o->bestEmail();
348 return $this->email_directory
;
352 public function data()
358 private function nameVariants($type)
360 $vals = array($this->$type);
361 foreach (self
::$name_variants[$type] as $var) {
362 $vartype = $type . '_' . $var;
363 $varname = $this->$vartype;
364 if ($varname != null
&& $varname != "") {
368 return array_unique($vals);
371 public function nationalities()
374 if ($this->nationality1
) {
375 $nats[] = $this->nationality1
;
377 if ($this->nationality2
) {
378 $nats[] = $this->nationality2
;
380 if ($this->nationality3
) {
381 $nats[] = $this->nationality3
;
386 public function __get($name)
388 if (property_exists($this, $name)) {
392 if (isset($this->data
[$name])) {
393 return $this->data
[$name];
399 public function __isset($name)
401 return property_exists($this, $name) ||
isset($this->data
[$name]);
404 public function __unset($name)
406 if (property_exists($this, $name)) {
409 unset($this->data
[$name]);
416 * *always deletes in: profile_addresses, profile_binets, profile_job,
417 * profile_langskills, profile_mentor, profile_networking,
418 * profile_phones, profile_skills, watch_profile
419 * *always keeps in: profile_corps, profile_display, profile_education,
420 * profile_medals, profile_name, profile_photos, search_name
421 * *modifies: profiles
423 public function clear()
426 'profile_job', 'profile_langskills', 'profile_mentor',
427 'profile_networking', 'profile_skills', 'watch_profile',
428 'profile_phones', 'profile_addresses', 'profile_binets');
430 foreach ($tables as $t) {
431 XDB
::execute('DELETE FROM ' . $t . '
436 XDB
::execute("UPDATE profiles
437 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
438 medals_pub = 'private', alias_pub = 'private',
439 email_directory = NULL
444 /** Sets the level of visibility of the profile
445 * Sets $this->visibility to a list of valid visibilities.
446 * @param one of the self::VIS_* values
448 public function setVisibilityLevel($visibility)
450 $this->visibility
->setLevel($visibility);
453 /** Determine whether an item with visibility $visibility can be displayed
454 * with the current level of visibility of the profile
455 * @param $visibility The level of visibility to be checked
457 public function isVisible($visibility)
459 return $this->visibility
->isVisible($visibility);
462 /** Stores the list of fields which have already been fetched for this Profile
464 public function setFetchedFields($fields)
466 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
467 Platal
::page()->kill("Invalid fetched fields: $fields");
470 $this->fetched_fields
= $fields;
473 private function fetched($field)
475 if (!array_key_exists($field, ProfileField
::$fields)) {
476 Platal
::page()->kill("Invalid field: $field");
479 return ($this->fetched_fields
& $field);
482 /** If not already done, fetches data for the given field
483 * @param $field One of the Profile::FETCH_*
484 * @return A ProfileField, or null
486 private function getProfileField($field)
488 if ($this->fetched($field)) {
491 $this->fetched_fields
= $this->fetched_fields |
$field;
494 $cls = ProfileField
::$fields[$field];
496 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
499 /** Consolidates internal data (addresses, phones, jobs)
501 private function consolidateFields()
503 if ($this->phones
!= null
) {
504 if ($this->addresses
!= null
) {
505 $this->addresses
->addPhones($this->phones
);
508 if ($this->jobs
!= null
) {
509 $this->jobs
->addPhones($this->phones
);
513 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
514 $this->jobs
->addAddresses($this->addresses
);
520 private $photo = null
;
521 public function getPhoto($fallback = true
, $data = false
)
523 if ($this->has_photo
) {
524 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
525 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
527 WHERE pid = {?}', $this->pid
);
528 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
529 } else if ($this->photo
== null
) {
530 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
533 } else if ($fallback) {
534 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
542 private $addresses = null
;
543 public function setAddresses(ProfileAddresses
$addr)
545 $this->addresses
= $addr;
546 $this->consolidateFields();
549 private function fetchAddresses()
551 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
552 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
554 $this->setAddresses($addr);
555 $this->fetchPhones();
560 public function getAddresses($flags, $limit = null
)
562 $this->fetchAddresses();
564 if ($this->addresses
== null
) {
567 return $this->addresses
->get($flags, $limit);
570 public function iterAddresses($flags, $limit = null
)
572 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
575 public function getMainAddress()
577 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
578 if (count($addr) == 0) {
581 return array_pop($addr);
587 private $phones = null
;
588 public function setPhones(ProfilePhones
$phones)
590 $this->phones
= $phones;
591 $this->consolidateFields();
594 private function fetchPhones()
596 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
597 $this->setPhones($this->getProfileField(self
::FETCH_PHONES
));
601 public function getPhones($flags, $limit = null
)
603 $this->fetchPhones();
604 if ($this->phones
== null
) {
607 return $this->phones
->get($flags, $limit);
612 private $educations = null
;
613 public function setEducations(ProfileEducation
$edu)
615 $this->educations
= $edu;
618 public function getEducations($flags, $limit = null
)
620 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
621 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
624 if ($this->educations
== null
) {
627 return $this->educations
->get($flags, $limit);
630 public function getExtraEducations($limit = null
)
632 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
637 private $corps = null
;
638 public function setCorps(ProfileCorps
$corps)
640 $this->corps
= $corps;
643 public function getCorps()
645 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
646 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
653 private $networks = null
;
654 public function setNetworking(ProfileNetworking
$nw)
656 $this->networks
= $nw;
659 public function getNetworking($flags, $limit = null
)
661 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
662 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
664 $this->setNetworking($nw);
667 if ($this->networks
== null
) {
670 return $this->networks
->get($flags, $limit);
673 public function getWebSite()
675 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
676 if (count($site) != 1) {
679 $site = array_pop($site);
686 private $jobs = null
;
687 public function setJobs(ProfileJobs
$jobs)
690 $this->consolidateFields();
693 private function fetchJobs()
695 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
696 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
698 $this->setJobs($jobs);
699 $this->fetchAddresses();
704 public function getJobs($flags, $limit = null
)
708 if ($this->jobs
== null
) {
711 return $this->jobs
->get($flags, $limit);
714 public function getMainJob()
716 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
717 if (count($job) != 1) {
720 return array_pop($job);
725 private $mentor_sectors = null
;
726 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
728 $this->mentor_sectors
= $sectors;
731 public function getMentoringSectors()
733 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
734 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
737 if ($this->mentor_sectors
== null
) {
740 return $this->mentor_sectors
->sectors
;
744 private $mentor_countries = null
;
745 public function setMentoringCountries(ProfileMentoringCountries
$countries)
747 $this->mentor_countries
= $countries;
750 public function getMentoringCountries()
752 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
753 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
756 if ($this->mentor_countries
== null
) {
759 return $this->mentor_countries
->countries
;
765 public function getBinets()
767 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
768 return XDB
::fetchColumn('SELECT binet_id
770 WHERE pid = {?}', $this->id());
775 public function getBinetsNames()
777 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
778 return XDB
::fetchColumn('SELECT text
779 FROM profile_binets AS pb
780 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
781 WHERE pb.pid = {?}', $this->id());
789 private $medals = null
;
790 public function setMedals(ProfileMedals
$medals)
792 $this->medals
= $medals;
795 public function getMedals()
797 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
798 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
800 if ($this->medals
== null
) {
803 return $this->medals
->medals
;
806 public function compareNames($firstname, $lastname)
808 $_lastname = mb_strtoupper($this->lastName());
809 $_firstname = mb_strtoupper($this->firstName());
810 $lastname = mb_strtoupper($lastname);
811 $firstname = mb_strtoupper($firstname);
813 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
814 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
817 foreach ($tokens as $str) {
818 $isOk &= (strpos($_lastname, $str) !== false
);
819 $maxlen = max($maxlen, strlen($str));
822 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
825 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
827 if (count($pids) == 0) {
831 if ($respect_order) {
832 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
837 $visibility = new ProfileVisibility($visibility);
839 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
840 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
841 p.cv, p.medals_pub, p.alias_pub, p.email_directory, p.last_change,
842 p.nationality1, p.nationality2, p.nationality3,
843 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
844 pe.entry_year, pe.grad_year,
845 IF ({?}, pse.text, NULL) AS section,
846 pn_f.name AS firstname, pn_l.name AS lastname,
847 IF( {?}, pn_n.name, NULL) AS nickname,
848 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
849 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
850 pd.yourself, pd.promo, pd.short_name, pd.directory_name AS full_name,
851 pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
852 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
853 ph.x AS photo_width, ph.y AS photo_height,
854 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
855 pm.expertise AS mentor_expertise,
858 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
859 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
860 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
861 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
862 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
863 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
864 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
865 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
866 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
867 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
868 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
869 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
870 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
871 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
872 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
873 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
874 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
878 $visibility->levels(),
879 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
),
880 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
),
881 $visibility->levels(),
882 $visibility->levels(),
885 return new ProfileIterator($it, $pids, $fields, $visibility);
888 public static function getPID($login)
890 if ($login instanceof PlUser
) {
891 return XDB
::fetchOneCell('SELECT pid
892 FROM account_profiles
893 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
895 } else if (ctype_digit($login)) {
896 return XDB
::fetchOneCell('SELECT pid
898 WHERE pid = {?}', $login);
900 return XDB
::fetchOneCell('SELECT pid
902 WHERE hrpid = {?}', $login);
906 public static function getPIDsFromUIDs($uids, $respect_order = true
)
908 if ($respect_order) {
909 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
913 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
914 FROM account_profiles AS ap
915 WHERE FIND_IN_SET(\'owner\', ap.perms)
916 AND ap.uid IN ' . XDB
::formatArray($uids) .'
920 /** Return the profile associated with the given login.
922 public static function get($login, $fields = 0x0000, $visibility = null
)
924 if (is_array($login)) {
925 $pf = new Profile($login);
926 $pf->setVisibilityLevel($visibility);
929 $pid = self
::getPID($login);
930 if (!is_null($pid)) {
931 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
934 /* Let say we can identify a profile using the identifiers of its owner.
936 if (!($login instanceof PlUser
)) {
937 $user = User
::getSilent($login);
938 if ($user && $user->hasProfile()) {
939 return $user->profile();
946 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
948 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
951 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
953 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
956 /** Return profiles for the list of pids.
958 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
960 if (count($pids) == 0) {
963 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
965 while ($p = $it->next()) {
966 $profiles[$p->id()] = $p;
971 /** Return profiles for uids.
973 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
975 if (count($uids) == 0) {
978 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
981 public static function isDisplayName($name)
983 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
984 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
985 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
986 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
989 public static function getNameTypeId($type, $for_sql = false
)
991 if (!S
::has('name_types')) {
992 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
993 FROM profile_name_enum');
994 S
::set('name_types', $table);
996 $table = S
::v('name_types');
999 return XDB
::escape($table[$type]);
1001 return $table[$type];
1005 public static function rebuildSearchTokens($pid)
1007 XDB
::execute('DELETE FROM search_name
1010 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
1011 FIND_IN_SET('public', e.flags) AS public
1012 FROM profile_name AS n
1013 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1017 while ($key = $keys->next()) {
1018 if ($key['name'] == '') {
1021 $toks = preg_split('/[ \'\-]+/', $key['name']);
1025 $token = strtolower(replace_accent(array_pop($toks) . $token));
1026 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
1027 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
1028 VALUES ({?}, {?}, {?}, {?}, {?})',
1029 $token, $pid, soundex_fr($token), $score, $key['public']);
1035 /** The school identifier consists of 6 digits. The first 3 represent the
1036 * promotion entry year. The last 3 indicate the student's rank.
1038 * Our identifier consists of 8 digits and both half have the same role.
1039 * This enables us to deal with bigger promotions and with a wider range
1042 * getSchoolId returns a school identifier given one of ours.
1043 * getXorgId returns a X.org identifier given a school identifier.
1045 public static function getSchoolId($xorgId)
1047 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1051 $year = intval(substr($xorgId, 0, 4));
1052 $rank = intval(substr($xorgId, 5, 3));
1055 } elseif ($year < 2000) {
1056 $year = intval(substr(1900 - $year, 1, 3));
1057 return sprintf('%02u0%03u', $year, $rank);
1059 $year = intval(substr(1900 - $year, 1, 3));
1060 return sprintf('%03u%03u', $year, $rank);
1064 public static function getXorgId($schoolId)
1066 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1070 $year = intval(substr($schoolId, 0, 3));
1071 $rank = intval(substr($schoolId, 3, 3));
1079 return sprintf('%04u%04u', 1900 +
$year, $rank);
1085 /** Iterator over a set of Profiles
1087 class ProfileIterator
implements PlIterator
1089 private $iterator = null
;
1091 private $visibility;
1093 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1095 require_once 'profilefields.inc.php';
1097 if ($visibility == null
) {
1098 $visibility = new ProfileVisibility();
1101 $this->fields
= $fields;
1102 $this->visibility
= $visibility;
1105 $callbacks = array();
1108 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1109 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1111 if ($fields & Profile
::FETCH_ADDRESSES
) {
1112 $callbacks[Profile
::FETCH_ADDRESSES
] = $cb;
1113 $subits[Profile
::FETCH_ADDRESSES
] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
1116 if ($fields & Profile
::FETCH_CORPS
) {
1117 $callbacks[Profile
::FETCH_CORPS
] = $cb;
1118 $subits[Profile
::FETCH_CORPS
] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
1121 if ($fields & Profile
::FETCH_EDU
) {
1122 $callbacks[Profile
::FETCH_EDU
] = $cb;
1123 $subits[Profile
::FETCH_EDU
] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
1126 if ($fields & Profile
::FETCH_JOBS
) {
1127 $callbacks[Profile
::FETCH_JOBS
] = $cb;
1128 $subits[Profile
::FETCH_JOBS
] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
1131 if ($fields & Profile
::FETCH_MEDALS
) {
1132 $callbacks[Profile
::FETCH_MEDALS
] = $cb;
1133 $subits[Profile
::FETCH_MEDALS
] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
1136 if ($fields & Profile
::FETCH_NETWORKING
) {
1137 $callbacks[Profile
::FETCH_NETWORKING
] = $cb;
1138 $subits[Profile
::FETCH_NETWORKING
] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
1141 if ($fields & Profile
::FETCH_PHONES
) {
1142 $callbacks[Profile
::FETCH_PHONES
] = $cb;
1143 $subits[Profile
::FETCH_PHONES
] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1146 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1149 private function hasData($field, $vals)
1151 return ($this->fields
& $field) && ($vals[$field] != null
);
1154 private function fillProfile(array $vals)
1156 $pf = Profile
::get($vals[0]);
1157 $pf->setVisibilityLevel($this->visibility
->level());
1158 $pf->setFetchedFields($this->fields
);
1160 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1161 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1163 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1164 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1166 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1167 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1170 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1171 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1173 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1174 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1176 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1177 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1179 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1180 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1186 public function next()
1188 $vals = $this->iterator
->next();
1189 if ($vals == null
) {
1192 return $this->fillProfile($vals);
1195 public function first()
1197 return $this->iterator
->first();
1200 public function last()
1202 return $this->iterator
->last();
1205 public function total()
1207 return $this->iterator
->total();
1211 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: