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 private function fetched($field)
428 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
429 Platal
::page()->kill("Invalid fetched fields: $fields");
432 return ($this->fetched_fields
& $field);
435 /** If not already done, fetches data for the given field
436 * @param $field One of the Profile::FETCH_*
437 * @return A ProfileField, or null
439 private function getProfileField($field)
441 if (!array_key_exists($field, ProfileField
::$fields)) {
442 Platal
::page()->kill("Invalid field: $field");
444 if ($this->fetched($field)) {
447 $this->fetched_fields
= $this->fetched_fields |
$field;
450 $cls = ProfileField
::$fields[$field];
452 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
455 /** Consolidates internal data (addresses, phones, jobs)
457 private function consolidateFields()
459 if ($this->phones
!= null
) {
460 if ($this->addresses
!= null
) {
461 $this->addresses
->addPhones($this->phones
);
464 if ($this->jobs
!= null
) {
465 $this->jobs
->addPhones($this->phones
);
469 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
470 $this->jobs
->addAddresses($this->addresses
);
472 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
473 $this->jobs
->addJobTerms($this->jobterms
);
479 private $photo = null
;
480 public function getPhoto($fallback = true
, $data = false
)
482 if ($this->has_photo
) {
483 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
484 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
486 WHERE pid = {?}', $this->pid
);
487 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
488 } else if ($this->photo
== null
) {
489 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
492 } else if ($fallback) {
493 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
501 private $addresses = null
;
502 public function setAddresses(ProfileAddresses
$addr)
504 $this->addresses
= $addr;
505 $this->consolidateFields();
508 private function fetchAddresses()
510 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
511 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
513 $this->setAddresses($addr);
514 $this->fetchPhones();
519 public function getAddresses($flags, $limit = null
)
521 $this->fetchAddresses();
523 if ($this->addresses
== null
) {
526 return $this->addresses
->get($flags, $limit);
529 public function iterAddresses($flags, $limit = null
)
531 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
534 public function getMainAddress()
536 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
537 if (count($addr) == 0) {
540 return array_pop($addr);
546 private $phones = null
;
547 public function setPhones(ProfilePhones
$phones)
549 $this->phones
= $phones;
550 $this->consolidateFields();
553 private function fetchPhones()
555 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
556 $phones = $this->getProfileField(self
::FETCH_PHONES
);
557 if (isset($phones)) {
558 $this->setPhones($phones);
563 public function getPhones($flags, $limit = null
)
565 $this->fetchPhones();
566 if ($this->phones
== null
) {
569 return $this->phones
->get($flags, $limit);
574 private $educations = null
;
575 public function setEducations(ProfileEducation
$edu)
577 $this->educations
= $edu;
580 public function getEducations($flags, $limit = null
)
582 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
583 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
586 if ($this->educations
== null
) {
589 return $this->educations
->get($flags, $limit);
592 public function getExtraEducations($limit = null
)
594 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
599 private $corps = null
;
600 public function setCorps(ProfileCorps
$corps)
602 $this->corps
= $corps;
605 public function getCorps()
607 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
608 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
615 private $networks = null
;
616 public function setNetworking(ProfileNetworking
$nw)
618 $this->networks
= $nw;
621 public function getNetworking($flags, $limit = null
)
623 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
624 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
626 $this->setNetworking($nw);
629 if ($this->networks
== null
) {
632 return $this->networks
->get($flags, $limit);
635 public function getWebSite()
637 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
638 if (count($site) != 1) {
641 $site = array_pop($site);
648 private $jobs = null
;
649 public function setJobs(ProfileJobs
$jobs)
652 $this->consolidateFields();
655 private function fetchJobs()
657 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
658 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
660 $this->setJobs($jobs);
661 $this->fetchAddresses();
666 public function getJobs($flags, $limit = null
)
670 if ($this->jobs
== null
) {
673 return $this->jobs
->get($flags, $limit);
676 public function getMainJob()
678 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
679 if (count($job) != 1) {
682 return array_pop($job);
687 private $jobterms = null
;
688 public function setJobTerms(ProfileJobTerms
$jobterms)
690 $this->jobterms
= $jobterms;
691 $this->consolidateFields();
696 private $mentor_sectors = null
;
697 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
699 $this->mentor_sectors
= $sectors;
702 public function getMentoringSectors()
704 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
705 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
708 if ($this->mentor_sectors
== null
) {
711 return $this->mentor_sectors
->sectors
;
715 private $mentor_countries = null
;
716 public function setMentoringCountries(ProfileMentoringCountries
$countries)
718 $this->mentor_countries
= $countries;
721 public function getMentoringCountries()
723 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
724 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
727 if ($this->mentor_countries
== null
) {
730 return $this->mentor_countries
->countries
;
734 /** List of job terms to specify mentoring */
735 private $mentor_terms = null
;
737 * set job terms to specify mentoring
738 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
740 public function setMentoringTerms(ProfileMentoringTerms
$terms)
742 $this->mentor_terms
= $terms;
745 * get all job terms that specify mentoring
746 * @return an array of JobTerms objects
748 public function getMentoringTerms()
750 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
751 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
754 if ($this->mentor_terms
== null
) {
757 return $this->mentor_terms
->get();
764 public function getBinets()
766 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
767 return XDB
::fetchColumn('SELECT binet_id
769 WHERE pid = {?}', $this->id());
774 public function getBinetsNames()
776 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
777 return XDB
::fetchColumn('SELECT text
778 FROM profile_binets AS pb
779 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
780 WHERE pb.pid = {?}', $this->id());
788 private $medals = null
;
789 public function setMedals(ProfileMedals
$medals)
791 $this->medals
= $medals;
794 public function getMedals()
796 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
797 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
799 if ($this->medals
== null
) {
802 return $this->medals
->medals
;
805 public function compareNames($firstname, $lastname)
807 $_lastname = mb_strtoupper($this->lastName());
808 $_firstname = mb_strtoupper($this->firstName());
809 $lastname = mb_strtoupper($lastname);
810 $firstname = mb_strtoupper($firstname);
812 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
813 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
816 foreach ($tokens as $str) {
817 $isOk &= (strpos($_lastname, $str) !== false
);
818 $maxlen = max($maxlen, strlen($str));
821 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
824 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
826 if (count($pids) == 0) {
830 if ($respect_order) {
831 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
836 $visibility = new ProfileVisibility($visibility);
838 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
839 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
840 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
841 p.last_change, p.nationality1, p.nationality2, p.nationality3,
842 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
843 pe.entry_year, pe.grad_year,
844 IF ({?}, pse.text, NULL) AS section,
845 pn_f.name AS firstname, pn_l.name AS lastname,
846 IF( {?}, pn_n.name, NULL) AS nickname,
847 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
848 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
849 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
850 pd.directory_name, pd.public_name, pd.private_name,
851 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->isVisible(ProfileVisibility
::VIS_PRIVATE
), // CV
879 $visibility->levels(), // freetext
880 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // section
881 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // nickname
882 $visibility->levels(), // mobile
883 $visibility->levels(), // photo
886 return new ProfileIterator($it, $pids, $fields, $visibility);
889 public static function getPID($login)
891 if ($login instanceof PlUser
) {
892 return XDB
::fetchOneCell('SELECT pid
893 FROM account_profiles
894 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
896 } else if (ctype_digit($login)) {
897 return XDB
::fetchOneCell('SELECT pid
899 WHERE pid = {?}', $login);
901 return XDB
::fetchOneCell('SELECT pid
903 WHERE hrpid = {?}', $login);
907 public static function getPIDsFromUIDs($uids, $respect_order = true
)
909 if ($respect_order) {
910 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
914 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
915 FROM account_profiles AS ap
916 WHERE FIND_IN_SET(\'owner\', ap.perms)
917 AND ap.uid IN ' . XDB
::formatArray($uids) .'
921 /** Return the profile associated with the given login.
923 public static function get($login, $fields = 0x0000, $visibility = null
)
925 if (is_array($login)) {
926 $pf = new Profile($login);
927 $pf->setVisibilityLevel($visibility);
930 $pid = self
::getPID($login);
931 if (!is_null($pid)) {
932 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
935 /* Let say we can identify a profile using the identifiers of its owner.
937 if (!($login instanceof PlUser
)) {
938 $user = User
::getSilent($login);
939 if ($user && $user->hasProfile()) {
940 return $user->profile();
947 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
949 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
952 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
954 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
957 /** Return profiles for the list of pids.
959 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
961 if (count($pids) == 0) {
964 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
966 while ($p = $it->next()) {
967 $profiles[$p->id()] = $p;
972 /** Return profiles for uids.
974 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
976 if (count($uids) == 0) {
979 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
982 public static function isDisplayName($name)
984 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
985 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
986 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
987 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
990 /** Returns the closest "accounts only" name type for $name
992 public static function getAccountEquivalentName($name)
996 case self
::DN_DIRECTORY
:
998 return 'directory_name';
1000 case self
::DN_PUBLIC
:
1002 case self
::DN_PRIVATE
:
1003 case self
::DN_SHORT
:
1004 case self
::DN_YOURSELF
:
1006 return 'display_name';
1010 public static function getNameTypeId($type, $for_sql = false
)
1012 if (!S
::has('name_types')) {
1013 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
1014 FROM profile_name_enum');
1015 S
::set('name_types', $table);
1017 $table = S
::v('name_types');
1020 return XDB
::escape($table[$type]);
1022 return $table[$type];
1026 public static function rebuildSearchTokens($pid)
1028 XDB
::execute('DELETE FROM search_name
1031 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
1032 FIND_IN_SET('public', e.flags) AS public
1033 FROM profile_name AS n
1034 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1038 while ($key = $keys->next()) {
1039 if ($key['name'] == '') {
1042 $toks = preg_split('/[ \'\-]+/', $key['name']);
1046 $token = strtolower(replace_accent(array_pop($toks) . $token));
1047 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
1048 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
1049 VALUES ({?}, {?}, {?}, {?}, {?})',
1050 $token, $pid, soundex_fr($token), $score, $key['public']);
1056 /** The school identifier consists of 6 digits. The first 3 represent the
1057 * promotion entry year. The last 3 indicate the student's rank.
1059 * Our identifier consists of 8 digits and both half have the same role.
1060 * This enables us to deal with bigger promotions and with a wider range
1063 * getSchoolId returns a school identifier given one of ours.
1064 * getXorgId returns a X.org identifier given a school identifier.
1066 public static function getSchoolId($xorgId)
1068 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1072 $year = intval(substr($xorgId, 0, 4));
1073 $rank = intval(substr($xorgId, 5, 3));
1076 } elseif ($year < 2000) {
1077 $year = intval(substr(1900 - $year, 1, 3));
1078 return sprintf('%02u0%03u', $year, $rank);
1080 $year = intval(substr(1900 - $year, 1, 3));
1081 return sprintf('%03u%03u', $year, $rank);
1085 public static function getXorgId($schoolId)
1087 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1091 $year = intval(substr($schoolId, 0, 3));
1092 $rank = intval(substr($schoolId, 3, 3));
1100 return sprintf('%04u%04u', 1900 +
$year, $rank);
1106 /** Iterator over a set of Profiles
1108 class ProfileIterator
implements PlIterator
1110 private $iterator = null
;
1112 private $visibility;
1114 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1116 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1118 require_once 'profilefields.inc.php';
1120 if ($visibility == null
) {
1121 $visibility = new ProfileVisibility();
1124 $this->fields
= $fields;
1125 $this->visibility
= $visibility;
1128 $callbacks = array();
1131 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1132 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1134 $fields = $fields & self
::FETCH_ALL
;
1135 for ($field = 1; $field < $fields; $field *= 2) {
1136 if (($fields & $field) ) {
1137 $callbacks[$field] = $cb;
1138 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1142 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1145 private function hasData($field, $vals)
1147 return ($this->fields
& $field) && ($vals[$field] != null
);
1150 private function fillProfile(array $vals)
1152 $pf = Profile
::get($vals[0]);
1153 $pf->setVisibilityLevel($this->visibility
->level());
1154 $pf->setFetchedFields($this->fields
);
1156 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1157 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1159 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1160 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1162 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1163 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1165 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1166 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1169 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1170 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1172 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1173 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1175 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1176 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1178 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1179 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1185 public function next()
1187 $vals = $this->iterator
->next();
1188 if ($vals == null
) {
1191 return $this->fillProfile($vals);
1194 public function first()
1196 return $this->iterator
->first();
1199 public function last()
1201 return $this->iterator
->last();
1204 public function total()
1206 return $this->iterator
->total();
1210 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: