3dd0e146e59f9033449d52cbb54cd297b6380bf0
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';
53 static public $name_variants = array(
54 self
::LASTNAME
=> array(self
::VN_MARITAL
, self
::VN_ORDINARY
),
55 self
::FIRSTNAME
=> array(self
::VN_ORDINARY
, self
::VN_INI
, self
::VN_OTHER
)
58 const ADDRESS_MAIN
= 0x000001;
59 const ADDRESS_PERSO
= 0x000002;
60 const ADDRESS_PRO
= 0x000004;
61 const ADDRESS_ALL
= 0x000006;
62 const ADDRESS_POSTAL
= 0x000008;
64 const EDUCATION_MAIN
= 0x000010;
65 const EDUCATION_EXTRA
= 0x000020;
66 const EDUCATION_ALL
= 0x000040;
67 const EDUCATION_FINISHED
= 0x000080;
68 const EDUCATION_CURRENT
= 0x000100;
70 const JOBS_MAIN
= 0x001000;
71 const JOBS_ALL
= 0x002000;
72 const JOBS_FINISHED
= 0x004000;
73 const JOBS_CURRENT
= 0x008000;
75 const NETWORKING_ALL
= 0x000000;
76 const NETWORKING_WEB
= 0x010000;
77 const NETWORKING_IM
= 0x020000;
78 const NETWORKING_SOCIAL
= 0x040000;
82 private $data = array();
84 private $visibility = null
;
86 private function __construct(array $data)
89 $this->pid
= $this->data
['pid'];
90 $this->hrpid
= $this->data
['hrpid'];
92 $this->setVisibilityLevel(self
::VISIBILITY_PUBLIC
);
101 public function hrid()
106 public function promo()
111 /** Print a name with the given formatting:
119 public function name($format)
121 return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'),
122 array($this->isFemale() ?
'•' : '',
123 $this->first_name
, $this->last_name
,
124 $this->full_name
, $this->short_name
,
125 $this->promo
), $format);
128 public function fullName($with_promo = false
)
131 return $this->full_name
. ' (' . $this->promo
. ')';
133 return $this->full_name
;
136 public function shortName($with_promo = false
)
139 return $this->short_name
. ' (' . $this->promo
. ')';
141 return $this->short_name
;
144 public function firstName()
146 return $this->firstname
;
149 public function lastName()
151 return $this->lastname
;
154 public function isFemale()
156 return $this->sex
== PlUser
::GENDER_FEMALE
;
159 public function data()
165 public function __get($name)
167 if (property_exists($this, $name)) {
171 if (isset($this->data
[$name])) {
172 return $this->data
[$name];
178 public function __isset($name)
180 return property_exists($this, $name) ||
isset($this->data
[$name]);
183 public function setVisibilityLevel($visibility)
185 if ($visibility != self
::VISIBILITY_PRIVATE
186 && $visibility != self
::VISIBILITY_AX
187 && $visibility != self
::VISIBILITY_PUBLIC
) {
188 Platal
::page()->kill("Visibility invalide: " . $visibility);
190 $this->visibility
= self
::$v_values[$visibility];
191 if ($this->mobile
&& !in_array($this->modbile_pub
, $this->visibility
)) {
192 unset($this->data
['mobile']);
199 public function getPhoto($fallback = true
)
201 /* TODO: migrate photo table to profile_photo, change uid to pid
204 if ($this->visibility
) {
205 $cond = ' AND pub IN ' . XDB
::formatArray($this->visibility
);
207 $res = XDB
::query('SELECT *
209 WHERE attachmime IN (\'jpeg\', \'png\')
210 ' . $cond . ' AND uid = {?}',
212 if ($res->numRows() > 0) {
213 $photo = $res->fetchOneAssoc();
214 return PlImage
::fromData($photo['attach'], 'image/' . $photo['attachmime'],
215 $photo['x'], $photo['y']);
216 } else if ($fallback) {
217 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
225 public function getAddresses($flags, $limit = null
)
227 $where = XDB
::format('pa.pid = {?}', $this->id());
228 if ($flags & self
::ADDRESS_MAIN
) {
229 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
231 if ($flags & self
::ADDRESS_POSTAL
) {
232 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
234 if ($this->visibility
) {
235 $where .= ' AND pa.pub IN ' . XDB
::formatArray($this->visibility
);
238 if ($flags & self
::ADDRESS_PRO
) {
241 if ($flags & self
::ADDRESS_PERSO
) {
244 if (count($type) > 0) {
245 $where .= ' AND pa.type IN ' . XDB
::formatArray($type);
247 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
248 return XDB
::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
249 gl.name AS locality, gas.name AS subAdministrativeArea,
250 ga.name AS administrativeArea, gc.countryFR AS country,
251 FIND_IN_SET(\'current\', pa.flags) AS current,
252 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
253 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
254 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
255 FROM profile_addresses AS pa
256 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
257 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
258 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
259 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
265 public function getMainAddress()
267 $it = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
268 if ($it->total() == 0) {
278 public function getEducations($flags, $limit = null
)
280 $where = XDB
::format('pe.uid = {?}', $this->id());
281 if ($flags & self
::EDUCATION_MAIN
) {
282 $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
283 } else if ($flags & self
::EDUCATION_EXTRA
) {
284 $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
285 } else if ($flags & self
::EDUCATION_FINISHED
) {
286 $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
287 } else if ($flags & self
::EDUCATION_CURRENT
) {
288 $where .= ' AND pe.grad_year > YEAR(CURDATE())';
290 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
291 return XDB
::iterator('SELECT pe.entry_year, pe.grad_year, pe.program,
292 pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
293 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
294 FIND_IN_SET(\'primary\', pe.flags) AS prim
295 FROM profile_education AS pe
296 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
297 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
298 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
299 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
301 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
305 public function getExtraEducations($limit = null
)
307 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
314 public function getNetworking($flags, $limit = null
)
316 $where = XDB
::format('pn.uid = {?}', $this->id());
317 if ($flags & self
::NETWORKING_WEB
) {
318 $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index
320 if ($this->visibility
) {
321 $where .= ' AND pn.pub IN ' . XDB
::formatArray($this->visibility
);
323 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
324 return XDB
::iterator('SELECT pne.name, pne.icon,
325 IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address),
326 pn.address) AS address
327 FROM profile_networking AS pn
328 INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type)
330 ORDER BY pn.network_type, pn.nwid
334 public function getWebSite()
336 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
337 if ($site->total() != 1) {
340 $site = $site->next();
341 return $site['address'];
348 public function getJobs($flags, $limit = null
)
350 $where = XDB
::format('pj.uid = {?}', $this->id());
352 if ($this->visibility
) {
353 $where .= ' AND pj.pub IN ' . XDB
::formatArray($this->visibility
);
354 $cond = 'pj.email_pub IN ' . XDB
::formatArray($this->visibility
);
356 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
357 return XDB
::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code,
358 pj.description, pj.url AS user_site,
359 IF (' . $cond . ', pj.email, NULL) AS user_email,
360 pjse.name AS sector, pjsse.name AS subsector,
361 pjssse.name AS subsubsector
362 FROM profile_job AS pj
363 INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid)
364 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
365 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
366 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
372 public function getMailJob()
374 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
375 if ($job->total() != 1) {
382 public function owner()
384 return User
::getSilent($this);
387 private static function fetchProfileData(array $pids)
389 if (count($pids) == 0) {
392 return XDB
::fetchAllAssoc('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
393 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
394 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_usual,
395 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_usual,
396 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
397 pp.display_tel AS mobile, pp.pub AS mobile_pub, ph.pub AS photo_pub
399 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
400 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
401 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
402 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
403 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
404 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
405 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
406 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
407 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
408 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
409 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
410 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
411 LEFT JOIN profile_phones AS pp ON (pp.uid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
412 LEFT JOIN photo AS ph ON (ph.uid = p.pid)
413 WHERE p.pid IN ' . XDB
::formatArray($pids) . '
417 public static function getPID($login)
419 if ($login instanceof PlUser
) {
420 return XDB
::fetchOneCell('SELECT pid
421 FROM account_profiles
422 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
424 } else if (ctype_digit($login)) {
425 return XDB
::fetchOneCell('SELECT pid
427 WHERE pid = {?}', $login);
429 return XDB
::fetchOneCell('SELECT pid
431 WHERE hrpid = {?}', $login);
436 /** Return the profile associated with the given login.
438 public static function get($login)
440 $pid = self
::getPID($login);
441 if (!is_null($pid)) {
442 $data = self
::fetchProfileData(array($pid));
443 return new Profile(array_pop($data));
445 /* Let say we can identify a profile using the identifiers of its owner.
447 if (!($login instanceof PlUser
)) {
448 $user = User
::getSilent($login);
449 if ($user && $user->hasProfile()) {
450 return $user->profile();
457 /** Return profiles for the list of pids.
459 public static function getBulkProfilesWithPIDs(array $pids)
461 if (count($pids) == 0) {
464 $data = self
::fetchProfileData($pids);
465 $inv = array_flip($pids);
467 foreach ($data AS $p) {
468 $p = new Profile($p);
469 $key = $inv[$p->id()];
470 $profiles[$key] = $p;
475 /** Return profiles for uids.
477 public static function getBulkProfilesWithUIDS(array $uids)
479 if (count($uids) == 0) {
482 $table = XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
483 FROM account_profiles AS ap
484 WHERE FIND_IN_SET(\'owner\', ap.perms)
485 AND ap.uid IN ' . XDB
::formatArray($uids));
486 return self
::getBulkProfilesWithPIDs($table);
489 public static function isDisplayName($name)
491 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
492 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
493 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
494 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
497 public static function getNameTypeId($type, $for_sql = false
)
499 if (!S
::has('name_types')) {
500 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
501 FROM profile_name_enum');
502 S
::set('name_types', $table);
504 $table = S
::v('name_types');
507 return XDB
::escape($table[$type]);
509 return $table[$type];
514 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: