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 firstNames()
151 return $this->nameVariants(self
::FIRSTNAME
);
154 public function lastName()
156 return $this->lastname
;
159 public function lastNames()
161 return $this->nameVariants(self
::LASTNAME
);
164 public function isFemale()
166 return $this->sex
== PlUser
::GENDER_FEMALE
;
169 public function data()
175 private function nameVariants($type)
177 $vals = array($this->$type);
178 foreach (self
::$name_variants[$type] as $var) {
179 $vartype = $type . '_' . $var;
180 $varname = $this->$vartype;
181 if ($varname != null
&& $varname != "") {
185 return array_unique($vals);
188 public function __get($name)
190 if (property_exists($this, $name)) {
194 if (isset($this->data
[$name])) {
195 return $this->data
[$name];
201 public function __isset($name)
203 return property_exists($this, $name) ||
isset($this->data
[$name]);
206 public function setVisibilityLevel($visibility)
208 if ($visibility != self
::VISIBILITY_PRIVATE
209 && $visibility != self
::VISIBILITY_AX
210 && $visibility != self
::VISIBILITY_PUBLIC
) {
211 Platal
::page()->kill("Visibility invalide: " . $visibility);
213 $this->visibility
= self
::$v_values[$visibility];
214 if ($this->mobile
&& !in_array($this->mobile_pub
, $this->visibility
)) {
215 unset($this->data
['mobile']);
222 public function getPhoto($fallback = true
)
224 /* TODO: migrate photo table to profile_photo, change uid to pid
227 if ($this->visibility
) {
228 $cond = ' AND pub IN ' . XDB
::formatArray($this->visibility
);
230 $res = XDB
::query('SELECT *
232 WHERE attachmime IN (\'jpeg\', \'png\')
233 ' . $cond . ' AND pid = {?}',
235 if ($res->numRows() > 0) {
236 $photo = $res->fetchOneAssoc();
237 return PlImage
::fromData($photo['attach'], 'image/' . $photo['attachmime'],
238 $photo['x'], $photo['y']);
239 } else if ($fallback) {
240 return PlImage
::fromFile(dirname(__FILE__
).'/../htdocs/images/none.png',
248 public function getAddresses($flags, $limit = null
)
250 $where = XDB
::format('pa.pid = {?}', $this->id());
251 if ($flags & self
::ADDRESS_MAIN
) {
252 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
254 if ($flags & self
::ADDRESS_POSTAL
) {
255 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
257 if ($this->visibility
) {
258 $where .= ' AND pa.pub IN ' . XDB
::formatArray($this->visibility
);
261 if ($flags & self
::ADDRESS_PRO
) {
264 if ($flags & self
::ADDRESS_PERSO
) {
267 if (count($type) > 0) {
268 $where .= ' AND pa.type IN ' . XDB
::formatArray($type);
270 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
271 return XDB
::iterator('SELECT pa.text, pa.postalCode, pa.type, pa.latitude, pa.longitude,
272 gl.name AS locality, gas.name AS subAdministrativeArea,
273 ga.name AS administrativeArea, gc.countryFR AS country,
274 ppfix.display_tel AS fixed_tel, ppfax.display_tel AS fax_tel,
275 FIND_IN_SET(\'current\', pa.flags) AS current,
276 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
277 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
278 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
279 FROM profile_addresses AS pa
280 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
281 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
282 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
283 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
284 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\')
285 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\')
291 public function getMainAddress()
293 $it = $this->getAddresses(self
::ADDRESS_PERSO | self
::ADDRESS_MAIN
);
294 if ($it->total() == 0) {
304 public function getEducations($flags, $limit = null
)
306 $where = XDB
::format('pe.uid = {?}', $this->id());
307 if ($flags & self
::EDUCATION_MAIN
) {
308 $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
309 } else if ($flags & self
::EDUCATION_EXTRA
) {
310 $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
311 } else if ($flags & self
::EDUCATION_FINISHED
) {
312 $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
313 } else if ($flags & self
::EDUCATION_CURRENT
) {
314 $where .= ' AND pe.grad_year > YEAR(CURDATE())';
316 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
317 return XDB
::iterator('SELECT pe.entry_year, pe.grad_year, pe.program,
318 pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
319 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
320 FIND_IN_SET(\'primary\', pe.flags) AS prim
321 FROM profile_education AS pe
322 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
323 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
324 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
325 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
327 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
331 public function getExtraEducations($limit = null
)
333 return $this->getEducations(self
::EDUCATION_EXTRA
, $limit);
340 public function getNetworking($flags, $limit = null
)
342 $where = XDB
::format('pn.uid = {?}', $this->id());
343 if ($flags & self
::NETWORKING_WEB
) {
344 $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index
346 if ($this->visibility
) {
347 $where .= ' AND pn.pub IN ' . XDB
::formatArray($this->visibility
);
349 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
350 return XDB
::iterator('SELECT pne.name, pne.icon,
351 IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address),
352 pn.address) AS address
353 FROM profile_networking AS pn
354 INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type)
356 ORDER BY pn.network_type, pn.nwid
360 public function getWebSite()
362 $site = $this->getNetworking(self
::NETWORKING_WEB
, 1);
363 if ($site->total() != 1) {
366 $site = $site->next();
367 return $site['address'];
374 public function getJobs($flags, $limit = null
)
376 $where = XDB
::format('pj.uid = {?}', $this->id());
378 if ($this->visibility
) {
379 $where .= ' AND pj.pub IN ' . XDB
::formatArray($this->visibility
);
380 $cond = 'pj.email_pub IN ' . XDB
::formatArray($this->visibility
);
382 $limit = is_null($limit) ?
'' : XDB
::format('LIMIT {?}', (int)$limit);
383 return XDB
::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code,
384 pj.description, pj.url AS user_site,
385 IF (' . $cond . ', pj.email, NULL) AS user_email,
386 pjse.name AS sector, pjsse.name AS subsector,
387 pjssse.name AS subsubsector
388 FROM profile_job AS pj
389 INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid)
390 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
391 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
392 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
398 public function getMailJob()
400 $job = $this->getJobs(self
::JOBS_MAIN
, 1);
401 if ($job->total() != 1) {
409 public function getBinets()
411 return XDB
::fetchColumn('SELECT binet_id
413 WHERE user_id = {?}', $this->id());
417 public function owner()
419 return User
::getSilent($this);
422 private static function fetchProfileData(array $pids, $respect_order = true
)
424 if (count($pids) == 0) {
428 if ($respect_order) {
429 $order = 'ORDER BY ' . XDB
::formatCustomOrder('p.pid', $pids);
434 return XDB
::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
435 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
436 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
437 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
438 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
439 pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub,
440 ph.pub AS photo_pub, ap.uid AS owner_id
442 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
443 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
444 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
445 AND pn_f.typeid = ' . self
::getNameTypeId('firstname', true
) . ')
446 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
447 AND pn_l.typeid = ' . self
::getNameTypeId('lastname', true
) . ')
448 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
449 AND pn_uf.typeid = ' . self
::getNameTypeId('firstname_ordinary', true
) . ')
450 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
451 AND pn_ul.typeid = ' . self
::getNameTypeId('lastname_ordinary', true
) . ')
452 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
453 AND pn_n.typeid = ' . self
::getNameTypeId('nickname', true
) . ')
454 LEFT JOIN profile_phones AS pp ON (pp.uid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
455 LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid)
456 LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms))
457 WHERE p.pid IN ' . XDB
::formatArray($pids) . '
462 public static function getPID($login)
464 if ($login instanceof PlUser
) {
465 return XDB
::fetchOneCell('SELECT pid
466 FROM account_profiles
467 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
469 } else if (ctype_digit($login)) {
470 return XDB
::fetchOneCell('SELECT pid
472 WHERE pid = {?}', $login);
474 return XDB
::fetchOneCell('SELECT pid
476 WHERE hrpid = {?}', $login);
480 public static function getPIDsFromUIDs($uids, $respect_order = true
)
482 if ($respect_order) {
483 $order = 'ORDER BY ' . XDB
::formatCustomOrder('uid', $uids);
487 return XDB
::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
488 FROM account_profiles AS ap
489 WHERE FIND_IN_SET(\'owner\', ap.perms)
490 AND ap.uid IN ' . XDB
::formatArray($uids) .'
494 /** Return the profile associated with the given login.
496 public static function get($login)
498 if (is_array($login)) {
499 return new Profile($login);
501 $pid = self
::getPID($login);
502 if (!is_null($pid)) {
503 $it = self
::iterOverPIDs(array($pid), false
);
506 /* Let say we can identify a profile using the identifiers of its owner.
508 if (!($login instanceof PlUser
)) {
509 $user = User
::getSilent($login);
510 if ($user && $user->hasProfile()) {
511 return $user->profile();
518 public static function iterOverUIDs($uids, $respect_order = true
)
520 return self
::iterOverPIDs(self
::getPIDsFromUIDs($uids), $respect_order);
523 public static function iterOverPIDs($pids, $respect_order = true
)
525 return new ProfileIterator(self
::fetchProfileData($pids, $respect_order));
528 /** Return profiles for the list of pids.
530 public static function getBulkProfilesWithPIDs(array $pids)
532 if (count($pids) == 0) {
535 $it = self
::iterOverPIDs($pids);
537 while ($p = $it->next()) {
538 $profiles[$p->id()] = $p;
543 /** Return profiles for uids.
545 public static function getBulkProfilesWithUIDS(array $uids)
547 if (count($uids) == 0) {
550 return self
::getBulkProfilesWithPIDs(self
::getPIDsFromUIDs($uids));
553 public static function isDisplayName($name)
555 return $name == self
::DN_FULL ||
$name == self
::DN_DISPLAY
556 ||
$name == self
::DN_YOURSELF ||
$name == self
::DN_DIRECTORY
557 ||
$name == self
::DN_PRIVATE ||
$name == self
::DN_PUBLIC
558 ||
$name == self
::DN_SHORT ||
$name == self
::DN_SORT
;
561 public static function getNameTypeId($type, $for_sql = false
)
563 if (!S
::has('name_types')) {
564 $table = XDB
::fetchAllAssoc('type', 'SELECT id, type
565 FROM profile_name_enum');
566 S
::set('name_types', $table);
568 $table = S
::v('name_types');
571 return XDB
::escape($table[$type]);
573 return $table[$type];
578 /** Iterator over a set of Profiles
579 * @param an XDB::Iterator obtained from a Profile::fetchProfileData
581 class ProfileIterator
implements PlIterator
585 public function __construct($dbiter)
587 $this->dbiter
= $dbiter;
590 public function next()
592 $data = $this->dbiter
->next();
596 return Profile
::get($data);
600 public function total()
602 return $this->dbiter
->total();
605 public function first()
607 return $this->dbiter
->first();
610 public function last()
612 return $this->dbiter
->last();
616 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: