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 ***************************************************************************/
26 const LASTNAME
= 'lastname';
27 const FIRSTNAME
= 'firstname';
28 const NICKNAME
= 'nickname';
29 const PSEUDONYM
= 'pseudonym';
32 const VN_MARITAL
= 'marital';
33 const VN_ORDINARY
= 'ordinary';
34 const VN_OTHER
= 'other';
37 const DN_FULL
= 'directory_name';
38 const DN_DISPLAY
= 'yourself';
39 const DN_YOURSELF
= 'yourself';
40 const DN_DIRECTORY
= 'directory_name';
41 const DN_PRIVATE
= 'private_name';
42 const DN_PUBLIC
= 'public_name';
43 const DN_SHORT
= 'short_name';
44 const DN_SORT
= 'sort_name';
45 /* education related names */
46 const EDU_X
= 'École polytechnique';
47 const DEGREE_X
= 'Ingénieur';
48 const DEGREE_M
= 'Master';
49 const DEGREE_D
= 'Doctorat';
51 static public $name_variants = array(
52 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
53 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
56 const ADDRESS_MAIN
= 0x00000001;
57 const ADDRESS_PERSO
= 0x00000002;
58 const ADDRESS_PRO
= 0x00000004;
59 const ADDRESS_ALL
= 0x00000006;
60 const ADDRESS_POSTAL
= 0x00000008;
62 const EDUCATION_MAIN
= 0x00000010;
63 const EDUCATION_EXTRA
= 0x00000020;
64 const EDUCATION_ALL
= 0x00000040;
65 const EDUCATION_FINISHED
= 0x00000080;
66 const EDUCATION_CURRENT
= 0x00000100;
68 const JOBS_MAIN
= 0x00001000;
69 const JOBS_ALL
= 0x00002000;
70 const JOBS_FINISHED
= 0x00004000;
71 const JOBS_CURRENT
= 0x00008000;
73 const NETWORKING_ALL
= 0x00070000;
74 const NETWORKING_WEB
= 0x00010000;
75 const NETWORKING_IM
= 0x00020000;
76 const NETWORKING_SOCIAL
= 0x00040000;
78 const PHONE_LINK_JOB
= 0x00100000;
79 const PHONE_LINK_ADDRESS
= 0x00200000;
80 const PHONE_LINK_PROFILE
= 0x00400000;
81 const PHONE_LINK_COMPANY
= 0x00800000;
82 const PHONE_LINK_ANY
= 0x00F00000;
84 const PHONE_TYPE_FAX
= 0x01000000;
85 const PHONE_TYPE_FIXED
= 0x02000000;
86 const PHONE_TYPE_MOBILE
= 0x04000000;
87 const PHONE_TYPE_ANY
= 0x07000000;
89 const PHONE_ANY
= 0x07F00000;
91 const FETCH_ADDRESSES
= 0x000001;
92 const FETCH_CORPS
= 0x000002;
93 const FETCH_EDU
= 0x000004;
94 const FETCH_JOBS
= 0x000008;
95 const FETCH_MEDALS
= 0x000010;
96 const FETCH_NETWORKING
= 0x000020;
97 const FETCH_MENTOR_SECTOR
= 0x000040;
98 const FETCH_MENTOR_COUNTRY
= 0x000080;
99 const FETCH_PHONES
= 0x000100;
100 const FETCH_JOB_TERMS
= 0x000200;
101 const FETCH_MENTOR_TERMS
= 0x000400;
103 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
105 const FETCH_ALL
= 0x0007FF; // OR of FETCH_*
107 private $fetched_fields = 0x000000;
112 private $owner_fetched = false
;
113 private $data = array();
115 private $visibility = null
;
118 private function __construct(array $data)
121 $this->pid
= $this->data
['pid'];
122 $this->hrpid
= $this->data
['hrpid'];
123 $this->visibility
= new ProfileVisibility();
131 public function hrid()
136 public function owner()
138 if ($this->owner
== null
&& !$this->owner_fetched
) {
139 $this->owner_fetched
= true
;
140 $this->owner
= User
::getSilent($this);
145 public function isActive()
147 if ($this->owner()) {
148 return $this->owner
->isActive();
153 public function promo()
158 public function yearpromo()
160 return intval(substr($this->promo
, 1, 4));
163 /** Check if user is an orange (associated with several promos)
165 public function isMultiPromo()
167 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
170 /** Returns an array with all associated promo years.
172 public function yearspromo()
175 $d = -$this->deltaPromoToGradYear();
176 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
182 public function mainEducation()
184 if (empty($this->promo
)) {
187 return $this->promo
{0};
191 public function mainGrade()
193 switch ($this->mainEducation()) {
195 return UserFilter
::GRADE_ING
;
197 return UserFilter
::GRADE_MST
;
199 return UserFilter
::GRADE_PHD
;
205 public function mainEducationDuration()
207 switch ($this->mainEducation()) {
219 /** Number of years between the promotion year until the
220 * graduation year. In standard schools it's 0, but for
221 * Polytechnique the promo year is the entry year.
223 public function deltaPromoToGradYear()
225 if ($this->mainEducation() == 'X') {
226 return $this->mainEducationDuration();
231 /** Print a name with the given formatting:
239 public function name($format)
241 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
242 array($this->isFemale() ?
'•' : '',
243 $this->firstName(), $this->lastName(),
244 $this->fullName(), $this->shortName(),
245 $this->promo()), $format);
248 public function fullName($with_promo = false
)
251 return $this->full_name
. ' (' . $this->promo
. ')';
253 return $this->full_name
;
256 public function shortName($with_promo = false
)
259 return $this->short_name
. ' (' . $this->promo
. ')';
261 return $this->short_name
;
264 public function firstName()
266 return $this->firstname
;
269 public function firstNames()
271 return $this->nameVariants(self
::FIRSTNAME
);
274 public function lastName()
276 return $this->lastname
;
279 public function lastNames()
281 return $this->nameVariants(self
::LASTNAME
);
284 public function isFemale()
286 return $this->sex
== PlUser
::GENDER_FEMALE
;
289 public function isDead()
291 return ($this->deathdate
!= null
);
294 public function displayEmail()
298 return $o->bestEmail();
300 return $this->email_directory
;
304 public function data()
310 private function nameVariants($type)
312 $vals = array($this->$type);
313 foreach (self
::$name_variants[$type] as $var) {
314 $vartype = $type . '_' . $var;
315 $varname = $this->$vartype;
316 if ($varname != null
&& $varname != "") {
320 return array_unique($vals);
323 public function nationalities()
326 $countries = DirEnum
::getOptions(DirEnum
::COUNTRIES
);
327 if ($this->nationality1
) {
328 $nats[$this->nationality1
] = $countries[$this->nationality1
];
330 if ($this->nationality2
) {
331 $nats[$this->nationality2
] = $countries[$this->nationality2
];
333 if ($this->nationality3
) {
334 $nats[$this->nationality3
] = $countries[$this->nationality3
];
339 public function __get($name)
341 if (property_exists($this, $name)) {
345 if (isset($this->data
[$name])) {
346 return $this->data
[$name];
352 public function __isset($name)
354 return property_exists($this, $name) ||
isset($this->data
[$name]);
357 public function __unset($name)
359 if (property_exists($this, $name)) {
362 unset($this->data
[$name]);
369 * *always deletes in: profile_addresses, profile_binets, profile_job,
370 * profile_langskills, profile_mentor, profile_networking,
371 * profile_phones, profile_skills, watch_profile
372 * *always keeps in: profile_corps, profile_display, profile_education,
373 * profile_medals, profile_name, profile_photos, search_name
374 * *modifies: profiles
376 public function clear()
379 'profile_job', 'profile_langskills', 'profile_mentor',
380 'profile_networking', 'profile_skills', 'watch_profile',
381 'profile_phones', 'profile_addresses', 'profile_binets');
383 foreach ($tables as $t) {
384 XDB
::execute('DELETE FROM ' . $t . '
389 XDB
::execute("UPDATE profiles
390 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
391 medals_pub = 'private', alias_pub = 'private',
392 email_directory = NULL
397 /** Sets the level of visibility of the profile
398 * Sets $this->visibility to a list of valid visibilities.
399 * @param one of the self::VIS_* values
401 public function setVisibilityLevel($visibility)
403 $this->visibility
->setLevel($visibility);
406 /** Determine whether an item with visibility $visibility can be displayed
407 * with the current level of visibility of the profile
408 * @param $visibility The level of visibility to be checked
410 public function isVisible($visibility)
412 return $this->visibility
->isVisible($visibility);
415 /** Stores the list of fields which have already been fetched for this Profile
417 public function setFetchedFields($fields)
419 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
420 Platal
::page()->kill("Invalid fetched fields: $fields");
423 $this->fetched_fields
= $fields;
426 /** Have we already fetched this field ?
428 private function fetched($field)
430 if (!array_key_exists($field, ProfileField
::$fields)) {
431 Platal
::page()->kill("Invalid field: $field");
434 return ($this->fetched_fields
& $field);
437 /** If not already done, fetches data for the given field
438 * @param $field One of the Profile::FETCH_*
439 * @return A ProfileField, or null
441 private function getProfileField($field)
443 if (!array_key_exists($field, ProfileField
::$fields)) {
444 Platal
::page()->kill("Invalid field: $field");
446 if ($this->fetched($field)) {
449 $this->fetched_fields
= $this->fetched_fields |
$field;
452 $cls = ProfileField
::$fields[$field];
454 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
457 /** Consolidates internal data (addresses, phones, jobs)
459 private function consolidateFields()
461 // Link phones to addresses
462 if ($this->phones
!= null
) {
463 if ($this->addresses
!= null
) {
464 $this->addresses
->addPhones($this->phones
);
467 if ($this->jobs
!= null
) {
468 $this->jobs
->addPhones($this->phones
);
472 // Link addresses to jobs
473 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
474 $this->jobs
->addAddresses($this->addresses
);
477 // Link jobterms to jobs
478 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
479 $this->jobs
->addJobTerms($this->jobterms
);
485 private $photo = null
;
486 public function getPhoto($fallback = true
, $data = false
)
488 if ($this->has_photo
) {
489 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
490 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
492 WHERE pid = {?}', $this->pid
);
493 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
494 } else if ($this->photo
== null
) {
495 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
498 } else if ($fallback) {
499 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
507 private $addresses = null
;
508 public function setAddresses(ProfileAddresses
$addr)
510 $this->addresses
= $addr;
511 $this->consolidateFields();
514 private function fetchAddresses()
516 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
517 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
519 $this->setAddresses($addr);
520 $this->fetchPhones();
525 public function getAddresses($flags, $limit = null
)
527 $this->fetchAddresses();
529 if ($this->addresses
== null
) {
532 return $this->addresses
->get($flags, $limit);
535 public function iterAddresses($flags, $limit = null
)
537 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
540 public function getMainAddress()
542 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
543 if (count($addr) == 0) {
546 return array_pop($addr);
552 private $phones = null
;
553 public function setPhones(ProfilePhones
$phones)
555 $this->phones
= $phones;
556 $this->consolidateFields();
559 private function fetchPhones()
561 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
562 $phones = $this->getProfileField(self
::FETCH_PHONES
);
563 if (isset($phones)) {
564 $this->setPhones($phones);
569 public function getPhones($flags, $limit = null
)
571 $this->fetchPhones();
572 if ($this->phones
== null
) {
575 return $this->phones
->get($flags, $limit);
580 private $educations = null
;
581 public function setEducations(ProfileEducation
$edu)
583 $this->educations
= $edu;
586 public function getEducations($flags, $limit = null
)
588 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
589 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
592 if ($this->educations
== null
) {
595 return $this->educations
->get($flags, $limit);
598 public function getExtraEducations($limit = null
)
600 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
605 private $corps = null
;
606 public function setCorps(ProfileCorps
$corps)
608 $this->corps
= $corps;
611 public function getCorps()
613 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
614 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
621 private $networks = null
;
622 public function setNetworking(ProfileNetworking
$nw)
624 $this->networks
= $nw;
627 public function getNetworking($flags, $limit = null
)
629 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
630 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
632 $this->setNetworking($nw);
635 if ($this->networks
== null
) {
638 return $this->networks
->get($flags, $limit);
641 public function getWebSite()
643 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
644 if (count($site) != 1) {
647 $site = array_pop($site);
654 private $jobs = null
;
655 public function setJobs(ProfileJobs
$jobs)
658 $this->consolidateFields();
661 private function fetchJobs()
663 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
664 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
666 $this->setJobs($jobs);
667 $this->fetchAddresses();
672 public function getJobs($flags, $limit = null
)
676 if ($this->jobs
== null
) {
679 return $this->jobs
->get($flags, $limit);
682 public function getMainJob()
684 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
685 if (count($job) != 1) {
688 return array_pop($job);
693 private $jobterms = null
;
694 public function setJobTerms(ProfileJobTerms
$jobterms)
696 $this->jobterms
= $jobterms;
697 $this->consolidateFields();
702 private $mentor_sectors = null
;
703 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
705 $this->mentor_sectors
= $sectors;
708 public function getMentoringSectors()
710 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
711 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
714 if ($this->mentor_sectors
== null
) {
717 return $this->mentor_sectors
->sectors
;
721 private $mentor_countries = null
;
722 public function setMentoringCountries(ProfileMentoringCountries
$countries)
724 $this->mentor_countries
= $countries;
727 public function getMentoringCountries()
729 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
730 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
733 if ($this->mentor_countries
== null
) {
736 return $this->mentor_countries
->countries
;
740 /** List of job terms to specify mentoring */
741 private $mentor_terms = null
;
743 * set job terms to specify mentoring
744 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
746 public function setMentoringTerms(ProfileMentoringTerms
$terms)
748 $this->mentor_terms
= $terms;
751 * get all job terms that specify mentoring
752 * @return an array of JobTerms objects
754 public function getMentoringTerms()
756 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
757 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
760 if ($this->mentor_terms
== null
) {
763 return $this->mentor_terms
->get();
770 public function getBinets()
772 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
773 return XDB
::fetchColumn('SELECT binet_id
775 WHERE pid = {?}', $this->id());
780 public function getBinetsNames()
782 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
783 return XDB
::fetchColumn('SELECT text
784 FROM profile_binets AS pb
785 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
786 WHERE pb.pid = {?}', $this->id());
794 private $medals = null
;
795 public function setMedals(ProfileMedals
$medals)
797 $this->medals
= $medals;
800 public function getMedals()
802 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
803 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
805 if ($this->medals
== null
) {
808 return $this->medals
->medals
;
811 public function compareNames($firstname, $lastname)
813 $_lastname = mb_strtoupper($this->lastName());
814 $_firstname = mb_strtoupper($this->firstName());
815 $lastname = mb_strtoupper($lastname);
816 $firstname = mb_strtoupper($firstname);
818 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
819 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
822 foreach ($tokens as $str) {
823 $isOk &= (strpos($_lastname, $str) !== false
);
824 $maxlen = max($maxlen, strlen($str));
827 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
830 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
832 if (count($pids) == 0) {
836 if ($respect_order) {
837 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
842 $visibility = new ProfileVisibility($visibility);
844 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
845 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
846 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
847 p.last_change, p.nationality1, p.nationality2, p.nationality3,
848 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
849 pe.entry_year, pe.grad_year,
850 IF ({?}, pse.text, NULL) AS section,
851 pn_f.name AS firstname, pn_l.name AS lastname,
852 IF( {?}, pn_n.name, NULL) AS nickname,
853 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
854 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
855 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
856 pd.directory_name, pd.public_name, pd.private_name,
857 IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
858 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
859 ph.x AS photo_width, ph.y AS photo_height,
860 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
861 pm.expertise AS mentor_expertise,
864 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
865 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
866 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
867 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
868 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
869 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
870 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
871 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
872 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
873 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
874 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
875 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
876 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
877 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
878 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
879 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
880 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
884 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // CV
885 $visibility->levels(), // freetext
886 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // section
887 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // nickname
888 $visibility->levels(), // mobile
889 $visibility->levels(), // photo
892 return new ProfileIterator($it, $pids, $fields, $visibility);
895 public static function getPID($login)
897 if ($login instanceof PlUser
) {
898 return XDB
::fetchOneCell('SELECT pid
899 FROM account_profiles
900 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
902 } else if (ctype_digit($login)) {
903 return XDB
::fetchOneCell('SELECT pid
905 WHERE pid = {?}', $login);
907 return XDB
::fetchOneCell('SELECT pid
909 WHERE hrpid = {?}', $login);
913 public static function getPIDsFromUIDs($uids, $respect_order = true
)
915 if ($respect_order) {
916 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
920 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
921 FROM account_profiles AS ap
922 WHERE FIND_IN_SET(\'owner\', ap.perms)
923 AND ap.uid IN ' . XDB
::formatArray($uids) .'
927 /** Return the profile associated with the given login.
929 public static function get($login, $fields = 0x0000, $visibility = null
)
931 if (is_array($login)) {
932 $pf = new Profile($login);
933 $pf->setVisibilityLevel($visibility);
936 $pid = self
::getPID($login);
937 if (!is_null($pid)) {
938 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
941 /* Let say we can identify a profile using the identifiers of its owner.
943 if (!($login instanceof PlUser
)) {
944 $user = User
::getSilent($login);
945 if ($user && $user->hasProfile()) {
946 return $user->profile();
953 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
955 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
958 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
960 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
963 /** Return profiles for the list of pids.
965 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
967 if (count($pids) == 0) {
970 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
972 while ($p = $it->next()) {
973 $profiles[$p->id()] = $p;
978 /** Return profiles for uids.
980 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
982 if (count($uids) == 0) {
985 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
988 public static function isDisplayName($name)
990 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
991 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
992 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
993 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
996 /** Returns the closest "accounts only" name type for $name
998 public static function getAccountEquivalentName($name)
1002 case self
::DN_DIRECTORY
:
1004 return 'directory_name';
1006 case self
::DN_PUBLIC
:
1008 case self
::DN_PRIVATE
:
1009 case self
::DN_SHORT
:
1010 case self
::DN_YOURSELF
:
1012 return 'display_name';
1016 public static function getNameTypeId($type, $for_sql = false
)
1018 if (!S
::has('name_types')) {
1019 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
1020 FROM profile_name_enum');
1021 S
::set('name_types', $table);
1023 $table = S
::v('name_types');
1026 return XDB
::escape($table[$type]);
1028 return $table[$type];
1032 public static function rebuildSearchTokens($pid)
1034 XDB
::execute('DELETE FROM search_name
1037 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
1038 FIND_IN_SET('public', e.flags) AS public
1039 FROM profile_name AS n
1040 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1044 while ($key = $keys->next()) {
1045 if ($key['name'] == '') {
1048 $toks = preg_split('/[ \'\-]+/', $key['name']);
1052 $token = strtolower(replace_accent(array_pop($toks) . $token));
1053 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
1054 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
1055 VALUES ({?}, {?}, {?}, {?}, {?})',
1056 $token, $pid, soundex_fr($token), $score, $key['public']);
1062 /** The school identifier consists of 6 digits. The first 3 represent the
1063 * promotion entry year. The last 3 indicate the student's rank.
1065 * Our identifier consists of 8 digits and both half have the same role.
1066 * This enables us to deal with bigger promotions and with a wider range
1069 * getSchoolId returns a school identifier given one of ours.
1070 * getXorgId returns a X.org identifier given a school identifier.
1072 public static function getSchoolId($xorgId)
1074 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1078 $year = intval(substr($xorgId, 0, 4));
1079 $rank = intval(substr($xorgId, 5, 3));
1082 } elseif ($year < 2000) {
1083 $year = intval(substr(1900 - $year, 1, 3));
1084 return sprintf('%02u0%03u', $year, $rank);
1086 $year = intval(substr(1900 - $year, 1, 3));
1087 return sprintf('%03u%03u', $year, $rank);
1091 public static function getXorgId($schoolId)
1093 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1097 $year = intval(substr($schoolId, 0, 3));
1098 $rank = intval(substr($schoolId, 3, 3));
1106 return sprintf('%04u%04u', 1900 +
$year, $rank);
1112 /** Iterator over a set of Profiles
1114 class ProfileIterator
implements PlIterator
1116 private $iterator = null
;
1118 private $visibility;
1120 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1122 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1124 require_once 'profilefields.inc.php';
1126 if ($visibility == null
) {
1127 $visibility = new ProfileVisibility();
1130 $this->fields
= $fields;
1131 $this->visibility
= $visibility;
1134 $callbacks = array();
1137 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1138 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1140 $fields = $fields & self
::FETCH_ALL
;
1141 for ($field = 1; $field < $fields; $field *= 2) {
1142 if (($fields & $field) ) {
1143 $callbacks[$field] = $cb;
1144 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1148 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1151 private function hasData($field, $vals)
1153 return ($this->fields
& $field) && ($vals[$field] != null
);
1156 private function fillProfile(array $vals)
1158 $pf = Profile
::get($vals[0]);
1159 $pf->setVisibilityLevel($this->visibility
->level());
1160 $pf->setFetchedFields($this->fields
);
1162 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1163 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1165 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1166 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1168 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1169 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1171 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1172 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1175 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1176 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1178 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1179 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1181 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1182 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1184 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1185 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1191 public function next()
1193 $vals = $this->iterator
->next();
1194 if ($vals == null
) {
1197 return $this->fillProfile($vals);
1200 public function first()
1202 return $this->iterator
->first();
1205 public function last()
1207 return $this->iterator
->last();
1210 public function total()
1212 return $this->iterator
->total();
1216 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: