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 isActive()
203 if ($this->owner()) {
204 return $this->owner
->isActive();
209 public function promo()
214 public function yearpromo()
216 return intval(substr($this->promo
, 1, 4));
219 /** Check if user is an orange (associated with several promos)
221 public function isMultiPromo()
223 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
226 /** Returns an array with all associated promo years.
228 public function yearspromo()
231 $d = -$this->deltaPromoToGradYear();
232 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
238 public function mainEducation()
240 if (empty($this->promo
)) {
243 return $this->promo
{0};
247 public function mainGrade()
249 switch ($this->mainEducation()) {
251 return UserFilter
::GRADE_ING
;
253 return UserFilter
::GRADE_MST
;
255 return UserFilter
::GRADE_PHD
;
261 public function mainEducationDuration()
263 switch ($this->mainEducation()) {
275 /** Number of years between the promotion year until the
276 * graduation year. In standard schools it's 0, but for
277 * Polytechnique the promo year is the entry year.
279 public function deltaPromoToGradYear()
281 if ($this->mainEducation() == 'X') {
282 return $this->mainEducationDuration();
287 /** Print a name with the given formatting:
295 public function name($format)
297 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
298 array($this->isFemale() ?
'•' : '',
299 $this->firstName(), $this->lastName(),
300 $this->fullName(), $this->shortName(),
301 $this->promo()), $format);
304 public function fullName($with_promo = false
)
307 return $this->full_name
. ' (' . $this->promo
. ')';
309 return $this->full_name
;
312 public function shortName($with_promo = false
)
315 return $this->short_name
. ' (' . $this->promo
. ')';
317 return $this->short_name
;
320 public function firstName()
322 return $this->firstname
;
325 public function firstNames()
327 return $this->nameVariants(self
::FIRSTNAME
);
330 public function lastName()
332 return $this->lastname
;
335 public function lastNames()
337 return $this->nameVariants(self
::LASTNAME
);
340 public function isFemale()
342 return $this->sex
== PlUser
::GENDER_FEMALE
;
345 public function isDead()
347 return ($this->deathdate
!= null
);
350 public function displayEmail()
354 return $o->bestEmail();
356 return $this->email_directory
;
360 public function data()
366 private function nameVariants($type)
368 $vals = array($this->$type);
369 foreach (self
::$name_variants[$type] as $var) {
370 $vartype = $type . '_' . $var;
371 $varname = $this->$vartype;
372 if ($varname != null
&& $varname != "") {
376 return array_unique($vals);
379 public function nationalities()
382 $countries = DirEnum
::getOptions(DirEnum
::COUNTRIES
);
383 if ($this->nationality1
) {
384 $nats[$this->nationality1
] = $countries[$this->nationality1
];
386 if ($this->nationality2
) {
387 $nats[$this->nationality2
] = $countries[$this->nationality2
];
389 if ($this->nationality3
) {
390 $nats[$this->nationality3
] = $countries[$this->nationality3
];
395 public function __get($name)
397 if (property_exists($this, $name)) {
401 if (isset($this->data
[$name])) {
402 return $this->data
[$name];
408 public function __isset($name)
410 return property_exists($this, $name) ||
isset($this->data
[$name]);
413 public function __unset($name)
415 if (property_exists($this, $name)) {
418 unset($this->data
[$name]);
425 * *always deletes in: profile_addresses, profile_binets, profile_job,
426 * profile_langskills, profile_mentor, profile_networking,
427 * profile_phones, profile_skills, watch_profile
428 * *always keeps in: profile_corps, profile_display, profile_education,
429 * profile_medals, profile_name, profile_photos, search_name
430 * *modifies: profiles
432 public function clear()
435 'profile_job', 'profile_langskills', 'profile_mentor',
436 'profile_networking', 'profile_skills', 'watch_profile',
437 'profile_phones', 'profile_addresses', 'profile_binets');
439 foreach ($tables as $t) {
440 XDB
::execute('DELETE FROM ' . $t . '
445 XDB
::execute("UPDATE profiles
446 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
447 medals_pub = 'private', alias_pub = 'private',
448 email_directory = NULL
453 /** Sets the level of visibility of the profile
454 * Sets $this->visibility to a list of valid visibilities.
455 * @param one of the self::VIS_* values
457 public function setVisibilityLevel($visibility)
459 $this->visibility
->setLevel($visibility);
462 /** Determine whether an item with visibility $visibility can be displayed
463 * with the current level of visibility of the profile
464 * @param $visibility The level of visibility to be checked
466 public function isVisible($visibility)
468 return $this->visibility
->isVisible($visibility);
471 /** Stores the list of fields which have already been fetched for this Profile
473 public function setFetchedFields($fields)
475 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
476 Platal
::page()->kill("Invalid fetched fields: $fields");
479 $this->fetched_fields
= $fields;
482 private function fetched($field)
484 if (!array_key_exists($field, ProfileField
::$fields)) {
485 Platal
::page()->kill("Invalid field: $field");
488 return ($this->fetched_fields
& $field);
491 /** If not already done, fetches data for the given field
492 * @param $field One of the Profile::FETCH_*
493 * @return A ProfileField, or null
495 private function getProfileField($field)
497 if ($this->fetched($field)) {
500 $this->fetched_fields
= $this->fetched_fields |
$field;
503 $cls = ProfileField
::$fields[$field];
505 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
508 /** Consolidates internal data (addresses, phones, jobs)
510 private function consolidateFields()
512 if ($this->phones
!= null
) {
513 if ($this->addresses
!= null
) {
514 $this->addresses
->addPhones($this->phones
);
517 if ($this->jobs
!= null
) {
518 $this->jobs
->addPhones($this->phones
);
522 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
523 $this->jobs
->addAddresses($this->addresses
);
529 private $photo = null
;
530 public function getPhoto($fallback = true
, $data = false
)
532 if ($this->has_photo
) {
533 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
534 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
536 WHERE pid = {?}', $this->pid
);
537 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
538 } else if ($this->photo
== null
) {
539 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
542 } else if ($fallback) {
543 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
551 private $addresses = null
;
552 public function setAddresses(ProfileAddresses
$addr)
554 $this->addresses
= $addr;
555 $this->consolidateFields();
558 private function fetchAddresses()
560 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
561 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
563 $this->setAddresses($addr);
564 $this->fetchPhones();
569 public function getAddresses($flags, $limit = null
)
571 $this->fetchAddresses();
573 if ($this->addresses
== null
) {
576 return $this->addresses
->get($flags, $limit);
579 public function iterAddresses($flags, $limit = null
)
581 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
584 public function getMainAddress()
586 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
587 if (count($addr) == 0) {
590 return array_pop($addr);
596 private $phones = null
;
597 public function setPhones(ProfilePhones
$phones)
599 $this->phones
= $phones;
600 $this->consolidateFields();
603 private function fetchPhones()
605 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
606 $phones = $this->getProfileField(self
::FETCH_PHONES
);
607 if (isset($phones)) {
608 $this->setPhones($phones);
613 public function getPhones($flags, $limit = null
)
615 $this->fetchPhones();
616 if ($this->phones
== null
) {
619 return $this->phones
->get($flags, $limit);
624 private $educations = null
;
625 public function setEducations(ProfileEducation
$edu)
627 $this->educations
= $edu;
630 public function getEducations($flags, $limit = null
)
632 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
633 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
636 if ($this->educations
== null
) {
639 return $this->educations
->get($flags, $limit);
642 public function getExtraEducations($limit = null
)
644 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
649 private $corps = null
;
650 public function setCorps(ProfileCorps
$corps)
652 $this->corps
= $corps;
655 public function getCorps()
657 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
658 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
665 private $networks = null
;
666 public function setNetworking(ProfileNetworking
$nw)
668 $this->networks
= $nw;
671 public function getNetworking($flags, $limit = null
)
673 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
674 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
676 $this->setNetworking($nw);
679 if ($this->networks
== null
) {
682 return $this->networks
->get($flags, $limit);
685 public function getWebSite()
687 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
688 if (count($site) != 1) {
691 $site = array_pop($site);
698 private $jobs = null
;
699 public function setJobs(ProfileJobs
$jobs)
702 $this->consolidateFields();
705 private function fetchJobs()
707 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
708 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
710 $this->setJobs($jobs);
711 $this->fetchAddresses();
716 public function getJobs($flags, $limit = null
)
720 if ($this->jobs
== null
) {
723 return $this->jobs
->get($flags, $limit);
726 public function getMainJob()
728 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
729 if (count($job) != 1) {
732 return array_pop($job);
737 private $mentor_sectors = null
;
738 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
740 $this->mentor_sectors
= $sectors;
743 public function getMentoringSectors()
745 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
746 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
749 if ($this->mentor_sectors
== null
) {
752 return $this->mentor_sectors
->sectors
;
756 private $mentor_countries = null
;
757 public function setMentoringCountries(ProfileMentoringCountries
$countries)
759 $this->mentor_countries
= $countries;
762 public function getMentoringCountries()
764 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
765 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
768 if ($this->mentor_countries
== null
) {
771 return $this->mentor_countries
->countries
;
777 public function getBinets()
779 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
780 return XDB
::fetchColumn('SELECT binet_id
782 WHERE pid = {?}', $this->id());
787 public function getBinetsNames()
789 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
790 return XDB
::fetchColumn('SELECT text
791 FROM profile_binets AS pb
792 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
793 WHERE pb.pid = {?}', $this->id());
801 private $medals = null
;
802 public function setMedals(ProfileMedals
$medals)
804 $this->medals
= $medals;
807 public function getMedals()
809 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
810 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
812 if ($this->medals
== null
) {
815 return $this->medals
->medals
;
818 public function compareNames($firstname, $lastname)
820 $_lastname = mb_strtoupper($this->lastName());
821 $_firstname = mb_strtoupper($this->firstName());
822 $lastname = mb_strtoupper($lastname);
823 $firstname = mb_strtoupper($firstname);
825 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
826 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
829 foreach ($tokens as $str) {
830 $isOk &= (strpos($_lastname, $str) !== false
);
831 $maxlen = max($maxlen, strlen($str));
834 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
837 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
839 if (count($pids) == 0) {
843 if ($respect_order) {
844 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
849 $visibility = new ProfileVisibility($visibility);
851 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
852 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
853 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
854 p.last_change, p.nationality1, p.nationality2, p.nationality3,
855 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
856 pe.entry_year, pe.grad_year,
857 IF ({?}, pse.text, NULL) AS section,
858 pn_f.name AS firstname, pn_l.name AS lastname,
859 IF( {?}, pn_n.name, NULL) AS nickname,
860 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
861 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
862 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
863 pd.directory_name, pd.public_name, pd.private_name,
864 IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
865 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
866 ph.x AS photo_width, ph.y AS photo_height,
867 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
868 pm.expertise AS mentor_expertise,
871 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
872 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
873 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
874 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
875 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
876 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
877 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
878 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
879 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
880 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
881 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
882 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
883 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
884 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
885 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
886 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
887 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
891 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // CV
892 $visibility->levels(), // freetext
893 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // section
894 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // nickname
895 $visibility->levels(), // mobile
896 $visibility->levels(), // photo
899 return new ProfileIterator($it, $pids, $fields, $visibility);
902 public static function getPID($login)
904 if ($login instanceof PlUser
) {
905 return XDB
::fetchOneCell('SELECT pid
906 FROM account_profiles
907 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
909 } else if (ctype_digit($login)) {
910 return XDB
::fetchOneCell('SELECT pid
912 WHERE pid = {?}', $login);
914 return XDB
::fetchOneCell('SELECT pid
916 WHERE hrpid = {?}', $login);
920 public static function getPIDsFromUIDs($uids, $respect_order = true
)
922 if ($respect_order) {
923 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
927 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
928 FROM account_profiles AS ap
929 WHERE FIND_IN_SET(\'owner\', ap.perms)
930 AND ap.uid IN ' . XDB
::formatArray($uids) .'
934 /** Return the profile associated with the given login.
936 public static function get($login, $fields = 0x0000, $visibility = null
)
938 if (is_array($login)) {
939 $pf = new Profile($login);
940 $pf->setVisibilityLevel($visibility);
943 $pid = self
::getPID($login);
944 if (!is_null($pid)) {
945 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
948 /* Let say we can identify a profile using the identifiers of its owner.
950 if (!($login instanceof PlUser
)) {
951 $user = User
::getSilent($login);
952 if ($user && $user->hasProfile()) {
953 return $user->profile();
960 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
962 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
965 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
967 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
970 /** Return profiles for the list of pids.
972 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
974 if (count($pids) == 0) {
977 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
979 while ($p = $it->next()) {
980 $profiles[$p->id()] = $p;
985 /** Return profiles for uids.
987 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
989 if (count($uids) == 0) {
992 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
995 public static function isDisplayName($name)
997 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
998 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
999 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1000 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1003 /** Returns the closest "accounts only" name type for $name
1005 public static function getAccountEquivalentName($name)
1009 case self
::DN_DIRECTORY
:
1011 return 'directory_name';
1013 case self
::DN_PUBLIC
:
1015 case self
::DN_PRIVATE
:
1016 case self
::DN_SHORT
:
1017 case self
::DN_YOURSELF
:
1019 return 'display_name';
1023 public static function getNameTypeId($type, $for_sql = false
)
1025 if (!S
::has('name_types')) {
1026 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
1027 FROM profile_name_enum');
1028 S
::set('name_types', $table);
1030 $table = S
::v('name_types');
1033 return XDB
::escape($table[$type]);
1035 return $table[$type];
1039 public static function rebuildSearchTokens($pid)
1041 XDB
::execute('DELETE FROM search_name
1044 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
1045 FIND_IN_SET('public', e.flags) AS public
1046 FROM profile_name AS n
1047 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1051 while ($key = $keys->next()) {
1052 if ($key['name'] == '') {
1055 $toks = preg_split('/[ \'\-]+/', $key['name']);
1059 $token = strtolower(replace_accent(array_pop($toks) . $token));
1060 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
1061 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
1062 VALUES ({?}, {?}, {?}, {?}, {?})',
1063 $token, $pid, soundex_fr($token), $score, $key['public']);
1069 /** The school identifier consists of 6 digits. The first 3 represent the
1070 * promotion entry year. The last 3 indicate the student's rank.
1072 * Our identifier consists of 8 digits and both half have the same role.
1073 * This enables us to deal with bigger promotions and with a wider range
1076 * getSchoolId returns a school identifier given one of ours.
1077 * getXorgId returns a X.org identifier given a school identifier.
1079 public static function getSchoolId($xorgId)
1081 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1085 $year = intval(substr($xorgId, 0, 4));
1086 $rank = intval(substr($xorgId, 5, 3));
1089 } elseif ($year < 2000) {
1090 $year = intval(substr(1900 - $year, 1, 3));
1091 return sprintf('%02u0%03u', $year, $rank);
1093 $year = intval(substr(1900 - $year, 1, 3));
1094 return sprintf('%03u%03u', $year, $rank);
1098 public static function getXorgId($schoolId)
1100 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1104 $year = intval(substr($schoolId, 0, 3));
1105 $rank = intval(substr($schoolId, 3, 3));
1113 return sprintf('%04u%04u', 1900 +
$year, $rank);
1119 /** Iterator over a set of Profiles
1121 class ProfileIterator
implements PlIterator
1123 private $iterator = null
;
1125 private $visibility;
1127 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1129 require_once 'profilefields.inc.php';
1131 if ($visibility == null
) {
1132 $visibility = new ProfileVisibility();
1135 $this->fields
= $fields;
1136 $this->visibility
= $visibility;
1139 $callbacks = array();
1142 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1143 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1145 if ($fields & Profile
::FETCH_ADDRESSES
) {
1146 $callbacks[Profile
::FETCH_ADDRESSES
] = $cb;
1147 $subits[Profile
::FETCH_ADDRESSES
] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility);
1150 if ($fields & Profile
::FETCH_CORPS
) {
1151 $callbacks[Profile
::FETCH_CORPS
] = $cb;
1152 $subits[Profile
::FETCH_CORPS
] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility);
1155 if ($fields & Profile
::FETCH_EDU
) {
1156 $callbacks[Profile
::FETCH_EDU
] = $cb;
1157 $subits[Profile
::FETCH_EDU
] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility);
1160 if ($fields & Profile
::FETCH_JOBS
) {
1161 $callbacks[Profile
::FETCH_JOBS
] = $cb;
1162 $subits[Profile
::FETCH_JOBS
] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility);
1165 if ($fields & Profile
::FETCH_MEDALS
) {
1166 $callbacks[Profile
::FETCH_MEDALS
] = $cb;
1167 $subits[Profile
::FETCH_MEDALS
] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility);
1170 if ($fields & Profile
::FETCH_NETWORKING
) {
1171 $callbacks[Profile
::FETCH_NETWORKING
] = $cb;
1172 $subits[Profile
::FETCH_NETWORKING
] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility);
1175 if ($fields & Profile
::FETCH_PHONES
) {
1176 $callbacks[Profile
::FETCH_PHONES
] = $cb;
1177 $subits[Profile
::FETCH_PHONES
] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility);
1180 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1183 private function hasData($field, $vals)
1185 return ($this->fields
& $field) && ($vals[$field] != null
);
1188 private function fillProfile(array $vals)
1190 $pf = Profile
::get($vals[0]);
1191 $pf->setVisibilityLevel($this->visibility
->level());
1192 $pf->setFetchedFields($this->fields
);
1194 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1195 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1197 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1198 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1200 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1201 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1204 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1205 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1207 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1208 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1210 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1211 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1213 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1214 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1220 public function next()
1222 $vals = $this->iterator
->next();
1223 if ($vals == null
) {
1226 return $this->fillProfile($vals);
1229 public function first()
1231 return $this->iterator
->first();
1234 public function last()
1236 return $this->iterator
->last();
1239 public function total()
1241 return $this->iterator
->total();
1245 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: