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);
45 // Unlogged or not allowed to view directory_ax or requesting public
47 if (!S
::logged() ||
!S
::user()->checkPerms('directory_ax') ||
$level == self
::VIS_PUBLIC
) {
48 $level = self
::VIS_PUBLIC
;
49 // Not allowed to view directory_private or requesting ax
50 } else if (!S
::user()->checkPerms('directory_private') ||
$level == self
::VIS_AX
) {
51 $level = self
::VIS_AX
;
53 $level = self
::VIS_PRIVATE
;
56 if ($this->level
== null ||
$this->level
== self
::VIS_PRIVATE
) {
57 $this->level
= $level;
58 } else if ($this->level
== self
::VIS_AX
&& $level == self
::VIS_PRIVATE
) {
61 $this->level
= self
::VIS_PUBLIC
;
65 public function level()
67 if ($this->level
== null
) {
68 return self
::VIS_PUBLIC
;
74 public function levels()
76 return self
::$v_values[$this->level()];
79 public function isVisible($visibility)
81 return in_array($visibility, $this->levels());
89 const LASTNAME
= 'lastname';
90 const FIRSTNAME
= 'firstname';
91 const NICKNAME
= 'nickname';
92 const PSEUDONYM
= 'pseudonym';
95 const VN_MARITAL
= 'marital';
96 const VN_ORDINARY
= 'ordinary';
97 const VN_OTHER
= 'other';
100 const DN_FULL
= 'directory_name';
101 const DN_DISPLAY
= 'yourself';
102 const DN_YOURSELF
= 'yourself';
103 const DN_DIRECTORY
= 'directory_name';
104 const DN_PRIVATE
= 'private_name';
105 const DN_PUBLIC
= 'public_name';
106 const DN_SHORT
= 'short_name';
107 const DN_SORT
= 'sort_name';
108 /* education related names */
109 const EDU_X
= 'École polytechnique';
110 const DEGREE_X
= 'Ingénieur';
111 const DEGREE_M
= 'Master';
112 const DEGREE_D
= 'Doctorat';
114 static public $name_variants = array(
115 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
116 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
119 const ADDRESS_MAIN
= 0x00000001;
120 const ADDRESS_PERSO
= 0x00000002;
121 const ADDRESS_PRO
= 0x00000004;
122 const ADDRESS_ALL
= 0x00000006;
123 const ADDRESS_POSTAL
= 0x00000008;
125 const EDUCATION_MAIN
= 0x00000010;
126 const EDUCATION_EXTRA
= 0x00000020;
127 const EDUCATION_ALL
= 0x00000040;
128 const EDUCATION_FINISHED
= 0x00000080;
129 const EDUCATION_CURRENT
= 0x00000100;
131 const JOBS_MAIN
= 0x00001000;
132 const JOBS_ALL
= 0x00002000;
133 const JOBS_FINISHED
= 0x00004000;
134 const JOBS_CURRENT
= 0x00008000;
136 const NETWORKING_ALL
= 0x00070000;
137 const NETWORKING_WEB
= 0x00010000;
138 const NETWORKING_IM
= 0x00020000;
139 const NETWORKING_SOCIAL
= 0x00040000;
141 const PHONE_LINK_JOB
= 0x00100000;
142 const PHONE_LINK_ADDRESS
= 0x00200000;
143 const PHONE_LINK_PROFILE
= 0x00400000;
144 const PHONE_LINK_COMPANY
= 0x00800000;
145 const PHONE_LINK_ANY
= 0x00F00000;
147 const PHONE_TYPE_FAX
= 0x01000000;
148 const PHONE_TYPE_FIXED
= 0x02000000;
149 const PHONE_TYPE_MOBILE
= 0x04000000;
150 const PHONE_TYPE_ANY
= 0x07000000;
152 const PHONE_ANY
= 0x07F00000;
154 const FETCH_ADDRESSES
= 0x000001;
155 const FETCH_CORPS
= 0x000002;
156 const FETCH_EDU
= 0x000004;
157 const FETCH_JOBS
= 0x000008;
158 const FETCH_MEDALS
= 0x000010;
159 const FETCH_NETWORKING
= 0x000020;
160 const FETCH_MENTOR_SECTOR
= 0x000040;
161 const FETCH_MENTOR_COUNTRY
= 0x000080;
162 const FETCH_PHONES
= 0x000100;
163 const FETCH_JOB_TERMS
= 0x000200;
164 const FETCH_MENTOR_TERMS
= 0x000400;
166 const FETCH_MINIFICHES
= 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
168 const FETCH_ALL
= 0x0007FF; // OR of FETCH_*
170 private $fetched_fields = 0x000000;
175 private $owner_fetched = false
;
176 private $data = array();
178 private $visibility = null
;
181 private function __construct(array $data)
184 $this->pid
= $this->data
['pid'];
185 $this->hrpid
= $this->data
['hrpid'];
186 $this->visibility
= new ProfileVisibility();
194 public function hrid()
199 public function owner()
201 if ($this->owner
== null
&& !$this->owner_fetched
) {
202 $this->owner_fetched
= true
;
203 $this->owner
= User
::getSilent($this);
208 public function isActive()
210 if ($this->owner()) {
211 return $this->owner
->isActive();
216 public function promo()
221 public function yearpromo()
223 return intval(substr($this->promo
, 1, 4));
226 /** Check if user is an orange (associated with several promos)
228 public function isMultiPromo()
230 return $this->grad_year
!= $this->entry_year +
$this->mainEducationDuration();
233 /** Returns an array with all associated promo years.
235 public function yearspromo()
238 $d = -$this->deltaPromoToGradYear();
239 for ($g = $this->entry_year +
$this->mainEducationDuration(); $g <= $this->grad_year
; ++
$g) {
245 public function mainEducation()
247 if (empty($this->promo
)) {
250 return $this->promo
{0};
254 public function mainGrade()
256 switch ($this->mainEducation()) {
258 return UserFilter
::GRADE_ING
;
260 return UserFilter
::GRADE_MST
;
262 return UserFilter
::GRADE_PHD
;
268 public function mainEducationDuration()
270 switch ($this->mainEducation()) {
282 /** Number of years between the promotion year until the
283 * graduation year. In standard schools it's 0, but for
284 * Polytechnique the promo year is the entry year.
286 public function deltaPromoToGradYear()
288 if ($this->mainEducation() == 'X') {
289 return $this->mainEducationDuration();
294 /** Print a name with the given formatting:
302 public function name($format)
304 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
305 array($this->isFemale() ?
'•' : '',
306 $this->firstName(), $this->lastName(),
307 $this->fullName(), $this->shortName(),
308 $this->promo()), $format);
311 public function fullName($with_promo = false
)
314 return $this->full_name
. ' (' . $this->promo
. ')';
316 return $this->full_name
;
319 public function shortName($with_promo = false
)
322 return $this->short_name
. ' (' . $this->promo
. ')';
324 return $this->short_name
;
327 public function firstName()
329 return $this->firstname
;
332 public function firstNames()
334 return $this->nameVariants(self
::FIRSTNAME
);
337 public function lastName()
339 return $this->lastname
;
342 public function lastNames()
344 return $this->nameVariants(self
::LASTNAME
);
347 public function isFemale()
349 return $this->sex
== PlUser
::GENDER_FEMALE
;
352 public function isDead()
354 return ($this->deathdate
!= null
);
357 public function displayEmail()
361 return $o->bestEmail();
363 return $this->email_directory
;
367 public function data()
373 private function nameVariants($type)
375 $vals = array($this->$type);
376 foreach (self
::$name_variants[$type] as $var) {
377 $vartype = $type . '_' . $var;
378 $varname = $this->$vartype;
379 if ($varname != null
&& $varname != "") {
383 return array_unique($vals);
386 public function nationalities()
389 $countries = DirEnum
::getOptions(DirEnum
::COUNTRIES
);
390 if ($this->nationality1
) {
391 $nats[$this->nationality1
] = $countries[$this->nationality1
];
393 if ($this->nationality2
) {
394 $nats[$this->nationality2
] = $countries[$this->nationality2
];
396 if ($this->nationality3
) {
397 $nats[$this->nationality3
] = $countries[$this->nationality3
];
402 public function __get($name)
404 if (property_exists($this, $name)) {
408 if (isset($this->data
[$name])) {
409 return $this->data
[$name];
415 public function __isset($name)
417 return property_exists($this, $name) ||
isset($this->data
[$name]);
420 public function __unset($name)
422 if (property_exists($this, $name)) {
425 unset($this->data
[$name]);
432 * *always deletes in: profile_addresses, profile_binets, profile_job,
433 * profile_langskills, profile_mentor, profile_networking,
434 * profile_phones, profile_skills, watch_profile
435 * *always keeps in: profile_corps, profile_display, profile_education,
436 * profile_medals, profile_name, profile_photos, search_name
437 * *modifies: profiles
439 public function clear()
442 'profile_job', 'profile_langskills', 'profile_mentor',
443 'profile_networking', 'profile_skills', 'watch_profile',
444 'profile_phones', 'profile_addresses', 'profile_binets');
446 foreach ($tables as $t) {
447 XDB
::execute('DELETE FROM ' . $t . '
452 XDB
::execute("UPDATE profiles
453 SET cv = NULL, freetext = NULL, freetext_pub = 'private',
454 medals_pub = 'private', alias_pub = 'private',
455 email_directory = NULL
460 /** Sets the level of visibility of the profile
461 * Sets $this->visibility to a list of valid visibilities.
462 * @param one of the self::VIS_* values
464 public function setVisibilityLevel($visibility)
466 $this->visibility
->setLevel($visibility);
469 /** Determine whether an item with visibility $visibility can be displayed
470 * with the current level of visibility of the profile
471 * @param $visibility The level of visibility to be checked
473 public function isVisible($visibility)
475 return $this->visibility
->isVisible($visibility);
478 /** Stores the list of fields which have already been fetched for this Profile
480 public function setFetchedFields($fields)
482 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
483 Platal
::page()->kill("Invalid fetched fields: $fields");
486 $this->fetched_fields
= $fields;
489 private function fetched($field)
491 if (($fields | self
::FETCH_ALL
) != self
::FETCH_ALL
) {
492 Platal
::page()->kill("Invalid fetched fields: $fields");
495 return ($this->fetched_fields
& $field);
498 /** If not already done, fetches data for the given field
499 * @param $field One of the Profile::FETCH_*
500 * @return A ProfileField, or null
502 private function getProfileField($field)
504 if (!array_key_exists($field, ProfileField
::$fields)) {
505 Platal
::page()->kill("Invalid field: $field");
507 if ($this->fetched($field)) {
510 $this->fetched_fields
= $this->fetched_fields |
$field;
513 $cls = ProfileField
::$fields[$field];
515 return ProfileField
::getForPID($cls, $this->id(), $this->visibility
);
518 /** Consolidates internal data (addresses, phones, jobs)
520 private function consolidateFields()
522 if ($this->phones
!= null
) {
523 if ($this->addresses
!= null
) {
524 $this->addresses
->addPhones($this->phones
);
527 if ($this->jobs
!= null
) {
528 $this->jobs
->addPhones($this->phones
);
532 if ($this->addresses
!= null
&& $this->jobs
!= null
) {
533 $this->jobs
->addAddresses($this->addresses
);
535 if ($this->jobs
!= null
&& $this->jobterms
!= null
) {
536 $this->jobs
->addJobTerms($this->jobterms
);
542 private $photo = null
;
543 public function getPhoto($fallback = true
, $data = false
)
545 if ($this->has_photo
) {
546 if ($data && ($this->photo
== null ||
$this->photo
->mimeType
== null
)) {
547 $res = XDB
::fetchOneAssoc('SELECT attach, attachmime, x, y
549 WHERE pid = {?}', $this->pid
);
550 $this->photo
= PlImage
::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']);
551 } else if ($this->photo
== null
) {
552 $this->photo
= PlImage
::fromData(null
, null
, $this->photo_width
, $this->photo_height
);
555 } else if ($fallback) {
556 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
564 private $addresses = null
;
565 public function setAddresses(ProfileAddresses
$addr)
567 $this->addresses
= $addr;
568 $this->consolidateFields();
571 private function fetchAddresses()
573 if ($this->addresses
== null
&& !$this->fetched(self
::FETCH_ADDRESSES
)) {
574 $addr = $this->getProfileField(self
::FETCH_ADDRESSES
);
576 $this->setAddresses($addr);
577 $this->fetchPhones();
582 public function getAddresses($flags, $limit = null
)
584 $this->fetchAddresses();
586 if ($this->addresses
== null
) {
589 return $this->addresses
->get($flags, $limit);
592 public function iterAddresses($flags, $limit = null
)
594 return PlIteratorUtils
::fromArray($this->getAddresses($flags, $limit), 1, true
);
597 public function getMainAddress()
599 $addr = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
600 if (count($addr) == 0) {
603 return array_pop($addr);
609 private $phones = null
;
610 public function setPhones(ProfilePhones
$phones)
612 $this->phones
= $phones;
613 $this->consolidateFields();
616 private function fetchPhones()
618 if ($this->phones
== null
&& !$this->fetched(self
::FETCH_PHONES
)) {
619 $phones = $this->getProfileField(self
::FETCH_PHONES
);
620 if (isset($phones)) {
621 $this->setPhones($phones);
626 public function getPhones($flags, $limit = null
)
628 $this->fetchPhones();
629 if ($this->phones
== null
) {
632 return $this->phones
->get($flags, $limit);
637 private $educations = null
;
638 public function setEducations(ProfileEducation
$edu)
640 $this->educations
= $edu;
643 public function getEducations($flags, $limit = null
)
645 if ($this->educations
== null
&& !$this->fetched(self
::FETCH_EDU
)) {
646 $this->setEducations($this->getProfileField(self
::FETCH_EDU
));
649 if ($this->educations
== null
) {
652 return $this->educations
->get($flags, $limit);
655 public function getExtraEducations($limit = null
)
657 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
662 private $corps = null
;
663 public function setCorps(ProfileCorps
$corps)
665 $this->corps
= $corps;
668 public function getCorps()
670 if ($this->corps
== null
&& !$this->fetched(self
::FETCH_CORPS
)) {
671 $this->setCorps($this->getProfileField(self
::FETCH_CORPS
));
678 private $networks = null
;
679 public function setNetworking(ProfileNetworking
$nw)
681 $this->networks
= $nw;
684 public function getNetworking($flags, $limit = null
)
686 if ($this->networks
== null
&& !$this->fetched(self
::FETCH_NETWORKING
)) {
687 $nw = $this->getProfileField(self
::FETCH_NETWORKING
);
689 $this->setNetworking($nw);
692 if ($this->networks
== null
) {
695 return $this->networks
->get($flags, $limit);
698 public function getWebSite()
700 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
701 if (count($site) != 1) {
704 $site = array_pop($site);
711 private $jobs = null
;
712 public function setJobs(ProfileJobs
$jobs)
715 $this->consolidateFields();
718 private function fetchJobs()
720 if ($this->jobs
== null
&& !$this->fetched(self
::FETCH_JOBS
)) {
721 $jobs = $this->getProfileField(self
::FETCH_JOBS
);
723 $this->setJobs($jobs);
724 $this->fetchAddresses();
729 public function getJobs($flags, $limit = null
)
733 if ($this->jobs
== null
) {
736 return $this->jobs
->get($flags, $limit);
739 public function getMainJob()
741 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
742 if (count($job) != 1) {
745 return array_pop($job);
750 private $jobterms = null
;
751 public function setJobTerms(ProfileJobTerms
$jobterms)
753 $this->jobterms
= $jobterms;
754 $this->consolidateFields();
759 private $mentor_sectors = null
;
760 public function setMentoringSectors(ProfileMentoringSectors
$sectors)
762 $this->mentor_sectors
= $sectors;
765 public function getMentoringSectors()
767 if ($this->mentor_sectors
== null
&& !$this->fetched(self
::FETCH_MENTOR_SECTOR
)) {
768 $this->setMentoringSectors($this->getProfileField(self
::FETCH_MENTOR_SECTOR
));
771 if ($this->mentor_sectors
== null
) {
774 return $this->mentor_sectors
->sectors
;
778 private $mentor_countries = null
;
779 public function setMentoringCountries(ProfileMentoringCountries
$countries)
781 $this->mentor_countries
= $countries;
784 public function getMentoringCountries()
786 if ($this->mentor_countries
== null
&& !$this->fetched(self
::FETCH_MENTOR_COUNTRY
)) {
787 $this->setMentoringCountries($this->getProfileField(self
::FETCH_MENTOR_COUNTRY
));
790 if ($this->mentor_countries
== null
) {
793 return $this->mentor_countries
->countries
;
797 /** List of job terms to specify mentoring */
798 private $mentor_terms = null
;
800 * set job terms to specify mentoring
801 * @param $terms a ProfileMentoringTerms object listing terms only for this profile
803 public function setMentoringTerms(ProfileMentoringTerms
$terms)
805 $this->mentor_terms
= $terms;
808 * get all job terms that specify mentoring
809 * @return an array of JobTerms objects
811 public function getMentoringTerms()
813 if ($this->mentor_terms
== null
&& !$this->fetched(self
::FETCH_MENTOR_TERMS
)) {
814 $this->setMentoringTerms($this->getProfileField(self
::FETCH_MENTOR_TERMS
));
817 if ($this->mentor_terms
== null
) {
820 return $this->mentor_terms
->get();
827 public function getBinets()
829 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
830 return XDB
::fetchColumn('SELECT binet_id
832 WHERE pid = {?}', $this->id());
837 public function getBinetsNames()
839 if ($this->visibility
->isVisible(ProfileVisibility
::VIS_PRIVATE
)) {
840 return XDB
::fetchColumn('SELECT text
841 FROM profile_binets AS pb
842 LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id)
843 WHERE pb.pid = {?}', $this->id());
851 private $medals = null
;
852 public function setMedals(ProfileMedals
$medals)
854 $this->medals
= $medals;
857 public function getMedals()
859 if ($this->medals
== null
&& !$this->fetched(self
::FETCH_MEDALS
)) {
860 $this->setMedals($this->getProfileField(self
::FETCH_MEDALS
));
862 if ($this->medals
== null
) {
865 return $this->medals
->medals
;
868 public function compareNames($firstname, $lastname)
870 $_lastname = mb_strtoupper($this->lastName());
871 $_firstname = mb_strtoupper($this->firstName());
872 $lastname = mb_strtoupper($lastname);
873 $firstname = mb_strtoupper($firstname);
875 $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname));
876 $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY
);
879 foreach ($tokens as $str) {
880 $isOk &= (strpos($_lastname, $str) !== false
);
881 $maxlen = max($maxlen, strlen($str));
884 return ($isOk && ($maxlen > 2 ||
$maxlen == strlen($_lastname)));
887 private static function fetchProfileData(array $pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
889 if (count($pids) == 0) {
893 if ($respect_order) {
894 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
899 $visibility = new ProfileVisibility($visibility);
901 $it = XDB
::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref,
902 p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex,
903 IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory,
904 p.last_change, p.nationality1, p.nationality2, p.nationality3,
905 IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext,
906 pe.entry_year, pe.grad_year,
907 IF ({?}, pse.text, NULL) AS section,
908 pn_f.name AS firstname, pn_l.name AS lastname,
909 IF( {?}, pn_n.name, NULL) AS nickname,
910 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
911 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
912 pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name,
913 pd.directory_name, pd.public_name, pd.private_name,
914 IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile,
915 (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo,
916 ph.x AS photo_width, ph.y AS photo_height,
917 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
918 pm.expertise AS mentor_expertise,
921 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
922 INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
923 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
924 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
925 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
926 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
927 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
928 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
929 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
930 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
931 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
932 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
933 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
934 LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
935 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
936 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
937 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
941 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // CV
942 $visibility->levels(), // freetext
943 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // section
944 $visibility->isVisible(ProfileVisibility
::VIS_PRIVATE
), // nickname
945 $visibility->levels(), // mobile
946 $visibility->levels(), // photo
949 return new ProfileIterator($it, $pids, $fields, $visibility);
952 public static function getPID($login)
954 if ($login instanceof PlUser
) {
955 return XDB
::fetchOneCell('SELECT pid
956 FROM account_profiles
957 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
959 } else if (ctype_digit($login)) {
960 return XDB
::fetchOneCell('SELECT pid
962 WHERE pid = {?}', $login);
964 return XDB
::fetchOneCell('SELECT pid
966 WHERE hrpid = {?}', $login);
970 public static function getPIDsFromUIDs($uids, $respect_order = true
)
972 if ($respect_order) {
973 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
977 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
978 FROM account_profiles AS ap
979 WHERE FIND_IN_SET(\'owner\', ap.perms)
980 AND ap.uid IN ' . XDB
::formatArray($uids) .'
984 /** Return the profile associated with the given login.
986 public static function get($login, $fields = 0x0000, $visibility = null
)
988 if (is_array($login)) {
989 $pf = new Profile($login);
990 $pf->setVisibilityLevel($visibility);
993 $pid = self
::getPID($login);
994 if (!is_null($pid)) {
995 $it = self
::iterOverPIDs(array($pid), false
, $fields, $visibility);
998 /* Let say we can identify a profile using the identifiers of its owner.
1000 if (!($login instanceof PlUser
)) {
1001 $user = User
::getSilent($login);
1002 if ($user && $user->hasProfile()) {
1003 return $user->profile();
1010 public static function iterOverUIDs($uids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1012 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
1015 public static function iterOverPIDs($pids, $respect_order = true
, $fields = 0x0000, $visibility = null
)
1017 return self
::fetchProfileData($pids, $respect_order, $fields, $visibility);
1020 /** Return profiles for the list of pids.
1022 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null
)
1024 if (count($pids) == 0) {
1027 $it = self
::iterOverPIDs($pids, true
, $fields, $visibility);
1028 $profiles = array();
1029 while ($p = $it->next()) {
1030 $profiles[$p->id()] = $p;
1035 /** Return profiles for uids.
1037 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null
)
1039 if (count($uids) == 0) {
1042 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids), $fields, $visibility);
1045 public static function isDisplayName($name)
1047 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
1048 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
1049 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
1050 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
1053 /** Returns the closest "accounts only" name type for $name
1055 public static function getAccountEquivalentName($name)
1059 case self
::DN_DIRECTORY
:
1061 return 'directory_name';
1063 case self
::DN_PUBLIC
:
1065 case self
::DN_PRIVATE
:
1066 case self
::DN_SHORT
:
1067 case self
::DN_YOURSELF
:
1069 return 'display_name';
1073 public static function getNameTypeId($type, $for_sql = false
)
1075 if (!S
::has('name_types')) {
1076 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
1077 FROM profile_name_enum');
1078 S
::set('name_types', $table);
1080 $table = S
::v('name_types');
1083 return XDB
::escape($table[$type]);
1085 return $table[$type];
1089 public static function rebuildSearchTokens($pid)
1091 XDB
::execute('DELETE FROM search_name
1094 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
1095 FIND_IN_SET('public', e.flags) AS public
1096 FROM profile_name AS n
1097 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
1101 while ($key = $keys->next()) {
1102 if ($key['name'] == '') {
1105 $toks = preg_split('/[ \'\-]+/', $key['name']);
1109 $token = strtolower(replace_accent(array_pop($toks) . $token));
1110 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
1111 XDB
::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
1112 VALUES ({?}, {?}, {?}, {?}, {?})',
1113 $token, $pid, soundex_fr($token), $score, $key['public']);
1119 /** The school identifier consists of 6 digits. The first 3 represent the
1120 * promotion entry year. The last 3 indicate the student's rank.
1122 * Our identifier consists of 8 digits and both half have the same role.
1123 * This enables us to deal with bigger promotions and with a wider range
1126 * getSchoolId returns a school identifier given one of ours.
1127 * getXorgId returns a X.org identifier given a school identifier.
1129 public static function getSchoolId($xorgId)
1131 if (!preg_match('/^[0-9]{8}$/', $xorgId)) {
1135 $year = intval(substr($xorgId, 0, 4));
1136 $rank = intval(substr($xorgId, 5, 3));
1139 } elseif ($year < 2000) {
1140 $year = intval(substr(1900 - $year, 1, 3));
1141 return sprintf('%02u0%03u', $year, $rank);
1143 $year = intval(substr(1900 - $year, 1, 3));
1144 return sprintf('%03u%03u', $year, $rank);
1148 public static function getXorgId($schoolId)
1150 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
1154 $year = intval(substr($schoolId, 0, 3));
1155 $rank = intval(substr($schoolId, 3, 3));
1163 return sprintf('%04u%04u', 1900 +
$year, $rank);
1169 /** Iterator over a set of Profiles
1171 class ProfileIterator
implements PlIterator
1173 private $iterator = null
;
1175 private $visibility;
1177 const FETCH_ALL
= 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS
1179 public function __construct(PlIterator
$it, array $pids, $fields = 0x0000, ProfileVisibility
$visibility = null
)
1181 require_once 'profilefields.inc.php';
1183 if ($visibility == null
) {
1184 $visibility = new ProfileVisibility();
1187 $this->fields
= $fields;
1188 $this->visibility
= $visibility;
1191 $callbacks = array();
1194 $callbacks[0] = PlIteratorUtils
::arrayValueCallback('pid');
1195 $cb = PlIteratorUtils
::objectPropertyCallback('pid');
1197 $fields = $fields & self
::FETCH_ALL
;
1198 for ($field = 1; $field < $fields; $field *= 2) {
1199 if (($fields & $field) ) {
1200 $callbacks[$field] = $cb;
1201 $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility);
1205 $this->iterator
= PlIteratorUtils
::parallelIterator($subits, $callbacks, 0);
1208 private function hasData($field, $vals)
1210 return ($this->fields
& $field) && ($vals[$field] != null
);
1213 private function fillProfile(array $vals)
1215 $pf = Profile
::get($vals[0]);
1216 $pf->setVisibilityLevel($this->visibility
->level());
1217 $pf->setFetchedFields($this->fields
);
1219 if ($this->hasData(Profile
::FETCH_PHONES
, $vals)) {
1220 $pf->setPhones($vals[Profile
::FETCH_PHONES
]);
1222 if ($this->hasData(Profile
::FETCH_ADDRESSES
, $vals)) {
1223 $pf->setAddresses($vals[Profile
::FETCH_ADDRESSES
]);
1225 if ($this->hasData(Profile
::FETCH_JOBS
, $vals)) {
1226 $pf->setJobs($vals[Profile
::FETCH_JOBS
]);
1228 if ($this->hasData(Profile
::FETCH_JOB_TERMS
, $vals)) {
1229 $pf->setJobTerms($vals[Profile
::FETCH_JOB_TERMS
]);
1232 if ($this->hasData(Profile
::FETCH_CORPS
, $vals)) {
1233 $pf->setCorps($vals[Profile
::FETCH_CORPS
]);
1235 if ($this->hasData(Profile
::FETCH_EDU
, $vals)) {
1236 $pf->setEducations($vals[Profile
::FETCH_EDU
]);
1238 if ($this->hasData(Profile
::FETCH_MEDALS
, $vals)) {
1239 $pf->setMedals($vals[Profile
::FETCH_MEDALS
]);
1241 if ($this->hasData(Profile
::FETCH_NETWORKING
, $vals)) {
1242 $pf->setNetworking($vals[Profile
::FETCH_NETWORKING
]);
1248 public function next()
1250 $vals = $this->iterator
->next();
1251 if ($vals == null
) {
1254 return $this->fillProfile($vals);
1257 public function first()
1259 return $this->iterator
->first();
1262 public function last()
1264 return $this->iterator
->last();
1267 public function total()
1269 return $this->iterator
->total();
1273 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: