ad80d8abf6ba0b72368fa27e248544fbc3357982
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 ***************************************************************************/
24 static private $v_values = array('public' => array('public'),
25 'ax' => array('ax', 'public'),
26 'private' => array('private', 'ax', 'public'));
28 const VISIBILITY_PUBLIC
= 'public';
29 const VISIBILITY_AX
= 'ax';
30 const VISIBILITY_PRIVATE
= 'private';
33 const LASTNAME
= 'lastname';
34 const FIRSTNAME
= 'firstname';
35 const NICKNAME
= 'nickname';
36 const PSEUDONYM
= 'pseudonym';
39 const VN_MARITAL
= 'marital';
40 const VN_ORDINARY
= 'ordinary';
41 const VN_OTHER
= 'other';
44 const DN_FULL
= 'directory_name';
45 const DN_DISPLAY
= 'yourself';
46 const DN_YOURSELF
= 'yourself';
47 const DN_DIRECTORY
= 'directory_name';
48 const DN_PRIVATE
= 'private_name';
49 const DN_PUBLIC
= 'public_name';
50 const DN_SHORT
= 'short_name';
51 const DN_SORT
= 'sort_name';
52 /* education related names */
53 const EDU_X
= 'École polytechnique';
54 const DEGREE_X
= 'Ingénieur';
55 const DEGREE_M
= 'Master';
56 const DEGREE_D
= 'Doctorat';
58 static public $name_variants = array(
59 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
60 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
63 const ADDRESS_MAIN
= 0x000001;
64 const ADDRESS_PERSO
= 0x000002;
65 const ADDRESS_PRO
= 0x000004;
66 const ADDRESS_ALL
= 0x000006;
67 const ADDRESS_POSTAL
= 0x000008;
69 const EDUCATION_MAIN
= 0x000010;
70 const EDUCATION_EXTRA
= 0x000020;
71 const EDUCATION_ALL
= 0x000040;
72 const EDUCATION_FINISHED
= 0x000080;
73 const EDUCATION_CURRENT
= 0x000100;
75 const JOBS_MAIN
= 0x001000;
76 const JOBS_ALL
= 0x002000;
77 const JOBS_FINISHED
= 0x004000;
78 const JOBS_CURRENT
= 0x008000;
80 const NETWORKING_ALL
= 0x000000;
81 const NETWORKING_WEB
= 0x010000;
82 const NETWORKING_IM
= 0x020000;
83 const NETWORKING_SOCIAL
= 0x040000;
87 private $data = array();
89 private $visibility = null
;
91 private function __construct(array $data)
94 $this->pid
= $this->data
['pid'];
95 $this->hrpid
= $this->data
['hrpid'];
97 $this->setVisibilityLevel(self
::VISIBILITY_PUBLIC
);
106 public function hrid()
111 public function promo()
116 /** Print a name with the given formatting:
124 public function name($format)
126 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
127 array($this->isFemale() ?
'•' : '',
128 $this->first_name
, $this->last_name
,
129 $this->full_name
, $this->short_name
,
130 $this->promo
), $format);
133 public function fullName($with_promo = false
)
136 return $this->full_name
. ' (' . $this->promo
. ')';
138 return $this->full_name
;
141 public function shortName($with_promo = false
)
144 return $this->short_name
. ' (' . $this->promo
. ')';
146 return $this->short_name
;
149 public function firstName()
151 return $this->firstname
;
154 public function firstNames()
156 return $this->nameVariants(self
::FIRSTNAME
);
159 public function lastName()
161 return $this->lastname
;
164 public function lastNames()
166 return $this->nameVariants(self
::LASTNAME
);
169 public function isFemale()
171 return $this->sex
== PlUser
::GENDER_FEMALE
;
174 public function data()
180 private function nameVariants($type)
182 $vals = array($this->$type);
183 foreach (self
::$name_variants[$type] as $var) {
184 $vartype = $type . '_' . $var;
185 $varname = $this->$vartype;
186 if ($varname != null
&& $varname != "") {
190 return array_unique($vals);
193 public function __get($name)
195 if (property_exists($this, $name)) {
199 if (isset($this->data
[$name])) {
200 return $this->data
[$name];
206 public function __isset($name)
208 return property_exists($this, $name) ||
isset($this->data
[$name]);
211 public function setVisibilityLevel($visibility)
213 if ($visibility != self
::VISIBILITY_PRIVATE
214 && $visibility != self
::VISIBILITY_AX
215 && $visibility != self
::VISIBILITY_PUBLIC
) {
216 Platal
::page()->kill("Visibility invalide: " . $visibility);
218 $this->visibility
= self
::$v_values[$visibility];
219 if ($this->mobile
&& !in_array($this->mobile_pub
, $this->visibility
)) {
220 unset($this->data
['mobile']);
227 public function getPhoto($fallback = true
)
229 /* TODO: migrate photo table to profile_photo, change uid to pid
232 if ($this->visibility
) {
233 $cond = ' AND pub IN ' . XDB
::formatArray($this->visibility
);
235 $res = XDB
::query('SELECT *
237 WHERE attachmime IN (\'jpeg\', \'png\')
238 ' . $cond . ' AND pid = {?}',
240 if ($res->numRows() > 0) {
241 $photo = $res->fetchOneAssoc();
242 return PlImage
::fromData($photo['attach'], 'image/' . $photo['attachmime'],
243 $photo['x'], $photo['y']);
244 } else if ($fallback) {
245 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
253 public function getAddresses($flags, $limit = null
)
255 $where = XDB
::format('pa.pid = {?}', $this->id());
256 if ($flags & self
::ADDRESS_MAIN
) {
257 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
259 if ($flags & self
::ADDRESS_POSTAL
) {
260 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
262 if ($this->visibility
) {
263 $where .= ' AND pa.pub IN ' . XDB
::formatArray($this->visibility
);
266 if ($flags & self
::ADDRESS_PRO
) {
269 if ($flags & self
::ADDRESS_PERSO
) {
272 if (count($type) > 0) {
273 $where .= ' AND pa.type IN ' . XDB
::formatArray($type);
275 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
276 return XDB
::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
277 gl.name AS locality, gas.name AS subAdministrativeArea,
278 ga.name AS administrativeArea, gc.countryFR AS country,
279 ppfix.display_tel AS fixed_tel, ppfax.display_tel AS fax_tel,
280 FIND_IN_SET(\'current\', pa.flags) AS current,
281 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
282 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
283 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
284 FROM profile_addresses AS pa
285 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
286 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
287 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
288 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
289 LEFT JOIN profile_phones AS ppfix ON (ppfix.link_type = \'address\' AND ppfix.uid = pa.pid AND ppfix.link_id = pa.id AND ppfix.tel_type = \'fixed\')
290 LEFT JOIN profile_phones AS ppfax ON (ppfax.link_type = \'address\' AND ppfax.uid = pa.pid AND ppfax.link_id = pa.id AND ppfax.tel_type = \'fax\')
296 public function getMainAddress()
298 $it = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
299 if ($it->total() == 0) {
309 public function getEducations($flags, $limit = null
)
311 $where = XDB
::format('pe.uid = {?}', $this->id());
312 if ($flags & self
::EDUCATION_MAIN
) {
313 $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
314 } else if ($flags & self
::EDUCATION_EXTRA
) {
315 $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
316 } else if ($flags & self
::EDUCATION_FINISHED
) {
317 $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
318 } else if ($flags & self
::EDUCATION_CURRENT
) {
319 $where .= ' AND pe.grad_year > YEAR(CURDATE())';
321 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
322 return XDB
::iterator('SELECT pe.entry_year, pe.grad_year, pe.program,
323 pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
324 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
325 FIND_IN_SET(\'primary\', pe.flags) AS prim
326 FROM profile_education AS pe
327 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
328 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
329 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
330 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
332 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
336 public function getExtraEducations($limit = null
)
338 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
345 public function getNetworking($flags, $limit = null
)
347 $where = XDB
::format('pn.uid = {?}', $this->id());
348 if ($flags & self
::NETWORKING_WEB
) {
349 $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index
351 if ($this->visibility
) {
352 $where .= ' AND pn.pub IN ' . XDB
::formatArray($this->visibility
);
354 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
355 return XDB
::iterator('SELECT pne.name, pne.icon,
356 IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address),
357 pn.address) AS address
358 FROM profile_networking AS pn
359 INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type)
361 ORDER BY pn.network_type, pn.nwid
365 public function getWebSite()
367 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
368 if ($site->total() != 1) {
371 $site = $site->next();
372 return $site['address'];
379 public function getJobs($flags, $limit = null
)
381 $where = XDB
::format('pj.uid = {?}', $this->id());
383 if ($this->visibility
) {
384 $where .= ' AND pj.pub IN ' . XDB
::formatArray($this->visibility
);
385 $cond = 'pj.email_pub IN ' . XDB
::formatArray($this->visibility
);
387 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
388 return XDB
::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code,
389 pj.description, pj.url AS user_site,
390 IF (' . $cond . ', pj.email, NULL) AS user_email,
391 pjse.name AS sector, pjsse.name AS subsector,
392 pjssse.name AS subsubsector
393 FROM profile_job AS pj
394 INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid)
395 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
396 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
397 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
403 public function getMailJob()
405 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
406 if ($job->total() != 1) {
414 public function getBinets()
416 return XDB
::fetchColumn('SELECT binet_id
418 WHERE user_id = {?}', $this->id());
422 public function owner()
424 return User
::getSilent($this);
427 private static function fetchProfileData(array $pids, $respect_order = true
)
429 if (count($pids) == 0) {
433 if ($respect_order) {
434 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
439 return XDB
::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
440 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
441 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
442 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
443 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
444 pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub,
445 ph.pub AS photo_pub, ap.uid AS owner_id
447 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
448 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
449 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
450 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
451 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
452 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
453 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
454 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
455 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
456 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
457 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
458 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
459 LEFT JOIN profile_phones AS pp ON (pp.uid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
460 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
461 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
462 WHERE p.pid IN ' . XDB
::formatArray($pids) . '
467 public static function getPID($login)
469 if ($login instanceof PlUser
) {
470 return XDB
::fetchOneCell('SELECT pid
471 FROM account_profiles
472 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
474 } else if (ctype_digit($login)) {
475 return XDB
::fetchOneCell('SELECT pid
477 WHERE pid = {?}', $login);
479 return XDB
::fetchOneCell('SELECT pid
481 WHERE hrpid = {?}', $login);
485 public static function getPIDsFromUIDs($uids, $respect_order = true
)
487 if ($respect_order) {
488 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
492 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
493 FROM account_profiles AS ap
494 WHERE FIND_IN_SET(\'owner\', ap.perms)
495 AND ap.uid IN ' . XDB
::formatArray($uids) .'
499 /** Return the profile associated with the given login.
501 public static function get($login)
503 if (is_array($login)) {
504 return new Profile($login);
506 $pid = self
::getPID($login);
507 if (!is_null($pid)) {
508 $it = self
::iterOverPIDs(array($pid), false
);
511 /* Let say we can identify a profile using the identifiers of its owner.
513 if (!($login instanceof PlUser
)) {
514 $user = User
::getSilent($login);
515 if ($user && $user->hasProfile()) {
516 return $user->profile();
523 public static function iterOverUIDs($uids, $respect_order = true
)
525 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order);
528 public static function iterOverPIDs($pids, $respect_order = true
)
530 return new ProfileIterator(self
::fetchProfileData($pids, $respect_order));
533 /** Return profiles for the list of pids.
535 public static function getBulkProfilesWithPIDs(array $pids)
537 if (count($pids) == 0) {
540 $it = self
::iterOverPIDs($pids);
542 while ($p = $it->next()) {
543 $profiles[$p->id()] = $p;
548 /** Return profiles for uids.
550 public static function getBulkProfilesWithUIDS(array $uids)
552 if (count($uids) == 0) {
555 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids));
558 public static function isDisplayName($name)
560 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
561 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
562 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
563 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
566 public static function getNameTypeId($type, $for_sql = false
)
568 if (!S
::has('name_types')) {
569 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
570 FROM profile_name_enum');
571 S
::set('name_types', $table);
573 $table = S
::v('name_types');
576 return XDB
::escape($table[$type]);
578 return $table[$type];
582 public static function rebuildSearchTokens($pid)
584 XDB
::execute('DELETE FROM search_name
587 $keys = XDB
::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score,
588 FIND_IN_SET('public', e.flags) AS public
589 FROM profile_name AS n
590 INNER JOIN profile_name_enum AS e ON (n.typeid = e.id)
594 foreach ($keys as $i => $key) {
595 if ($key['name'] == '') {
598 $toks = preg_split('/[ \'\-]+/', $key['name']);
602 $token = strtolower(replace_accent(array_pop($toks) . $token));
603 $score = ($toks ?
0 : 10 +
$first) * ($key['score'] / 10);
604 XDB
::execute('REPLACE INTO search_name (token, uid, soundex, score, flags)
605 VALUES ({?}, {?}, {?}, {?}, {?})',
606 $token, $uid, soundex_fr($token), $score, $key['public']);
615 /** Iterator over a set of Profiles
616 * @param an XDB::Iterator obtained from a Profile::fetchProfileData
618 class ProfileIterator
implements PlIterator
622 public function __construct($dbiter)
624 $this->dbiter
= $dbiter;
627 public function next()
629 $data = $this->dbiter
->next();
633 return Profile
::get($data);
637 public function total()
639 return $this->dbiter
->total();
642 public function first()
644 return $this->dbiter
->first();
647 public function last()
649 return $this->dbiter
->last();
653 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: