Add name variants and binets in Profile
[platal.git] / classes / profile.php
CommitLineData
e7b93962
FB
1<?php
2/***************************************************************************
d4c08d89 3 * Copyright (C) 2003-2010 Polytechnique.org *
e7b93962
FB
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class Profile
23{
f5642983
FB
24 static private $v_values = array('public' => array('public'),
25 'ax' => array('ax', 'public'),
26 'private' => array('private', 'ax', 'public'));
913a4e90 27
f5642983
FB
28 const VISIBILITY_PUBLIC = 'public';
29 const VISIBILITY_AX = 'ax';
30 const VISIBILITY_PRIVATE = 'private';
31
913a4e90
RB
32 /* name tokens */
33 const LASTNAME = 'lastname';
34 const FIRSTNAME = 'firstname';
35 const NICKNAME = 'nickname';
36 const PSEUDONYM = 'pseudonym';
37 const NAME = 'name';
38 /* name variants */
39 const VN_MARITAL = 'marital';
40 const VN_ORDINARY = 'ordinary';
41 const VN_OTHER = 'other';
42 const VN_INI = 'ini';
43 /* display names */
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
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)
56 );
57
f5642983
FB
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;
63
64 const EDUCATION_MAIN = 0x000010;
4bc5b8f0
FB
65 const EDUCATION_EXTRA = 0x000020;
66 const EDUCATION_ALL = 0x000040;
67 const EDUCATION_FINISHED = 0x000080;
68 const EDUCATION_CURRENT = 0x000100;
f5642983 69
4bc5b8f0
FB
70 const JOBS_MAIN = 0x001000;
71 const JOBS_ALL = 0x002000;
72 const JOBS_FINISHED = 0x004000;
73 const JOBS_CURRENT = 0x008000;
f5642983 74
04a94b1d
FB
75 const NETWORKING_ALL = 0x000000;
76 const NETWORKING_WEB = 0x010000;
77 const NETWORKING_IM = 0x020000;
78 const NETWORKING_SOCIAL = 0x040000;
79
e7b93962
FB
80 private $pid;
81 private $hrpid;
3e53a496
FB
82 private $data = array();
83
f5642983
FB
84 private $visibility = null;
85
b774ddab 86 private function __construct(array $data)
e7b93962 87 {
b774ddab 88 $this->data = $data;
832e6fcb
FB
89 $this->pid = $this->data['pid'];
90 $this->hrpid = $this->data['hrpid'];
f74fb084
FB
91 if (!S::logged()) {
92 $this->setVisibilityLevel(self::VISIBILITY_PUBLIC);
93 }
e7b93962
FB
94 }
95
96 public function id()
97 {
98 return $this->pid;
99 }
100
101 public function hrid()
102 {
103 return $this->hrpid;
104 }
105
d5e60905
FB
106 public function promo()
107 {
108 return $this->promo;
109 }
110
94b72319
FB
111 /** Print a name with the given formatting:
112 * %s = • for women
113 * %f = firstname
114 * %l = lastname
115 * %F = fullname
116 * %S = shortname
117 * %p = promo
118 */
119 public function name($format)
120 {
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);
126 }
127
128 public function fullName($with_promo = false)
129 {
130 if ($with_promo) {
131 return $this->full_name . ' (' . $this->promo . ')';
132 }
133 return $this->full_name;
134 }
135
136 public function shortName($with_promo = false)
137 {
138 if ($with_promo) {
139 return $this->short_name . ' (' . $this->promo . ')';
140 }
141 return $this->short_name;
142 }
143
144 public function firstName()
145 {
08c91036 146 return $this->firstname;
94b72319
FB
147 }
148
5c005b37
RB
149 public function firstNames()
150 {
151 return $this->nameVariants(self::FIRSTNAME);
152 }
153
94b72319
FB
154 public function lastName()
155 {
08c91036 156 return $this->lastname;
94b72319
FB
157 }
158
5c005b37
RB
159 public function lastNames()
160 {
161 return $this->nameVariants(self::LASTNAME);
162 }
163
94b72319
FB
164 public function isFemale()
165 {
166 return $this->sex == PlUser::GENDER_FEMALE;
167 }
168
169 public function data()
170 {
171 $this->first_name;
172 return $this->data;
173 }
174
5c005b37
RB
175 private function nameVariants($type)
176 {
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 != "") {
182 $vals[] = $varname;
183 }
184 }
185 return array_unique($vals);
186 }
187
3e53a496
FB
188 public function __get($name)
189 {
190 if (property_exists($this, $name)) {
191 return $this->$name;
192 }
193
3e53a496
FB
194 if (isset($this->data[$name])) {
195 return $this->data[$name];
196 }
197
198 return null;
199 }
200
201 public function __isset($name)
202 {
203 return property_exists($this, $name) || isset($this->data[$name]);
204 }
205
f5642983
FB
206 public function setVisibilityLevel($visibility)
207 {
208 if ($visibility != self::VISIBILITY_PRIVATE
209 && $visibility != self::VISIBILITY_AX
210 && $visibility != self::VISIBILITY_PUBLIC) {
211 Platal::page()->kill("Visibility invalide: " . $visibility);
212 }
213 $this->visibility = self::$v_values[$visibility];
5c005b37 214 if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) {
46e9eb99
FB
215 unset($this->data['mobile']);
216 }
f5642983
FB
217 }
218
4bc5b8f0 219
833a6e86
FB
220 /* Photo
221 */
222 public function getPhoto($fallback = true)
223 {
224 /* TODO: migrate photo table to profile_photo, change uid to pid
225 */
226 $cond = '';
227 if ($this->visibility) {
228 $cond = ' AND pub IN ' . XDB::formatArray($this->visibility);
229 }
230 $res = XDB::query('SELECT *
231 FROM photo
232 WHERE attachmime IN (\'jpeg\', \'png\')
233 ' . $cond . ' AND uid = {?}',
234 $this->id());
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',
241 'image/png');
242 }
243 return null;
244 }
245
4bc5b8f0
FB
246 /* Addresses
247 */
248 public function getAddresses($flags, $limit = null)
f5642983
FB
249 {
250 $where = XDB::format('pa.pid = {?}', $this->id());
251 if ($flags & self::ADDRESS_MAIN) {
252 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
253 }
254 if ($flags & self::ADDRESS_POSTAL) {
255 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
256 }
257 if ($this->visibility) {
258 $where .= ' AND pa.pub IN ' . XDB::formatArray($this->visibility);
259 }
260 $type = array();
261 if ($flags & self::ADDRESS_PRO) {
262 $type[] = 'job';
263 }
264 if ($flags & self::ADDRESS_PERSO) {
265 $type[] = 'home';
266 }
267 if (count($type) > 0) {
268 $where .= ' AND pa.type IN ' . XDB::formatArray($type);
269 }
4bc5b8f0 270 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
f5642983
FB
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 FIND_IN_SET(\'current\', pa.flags) AS current,
275 FIND_IN_SET(\'temporary\', pa.flags) AS temporary,
276 FIND_IN_SET(\'secondary\', pa.flags) AS secondary,
277 FIND_IN_SET(\'mail\', pa.flags) AS mail, pa.type
278 FROM profile_addresses AS pa
279 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
280 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
281 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
282 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
4bc5b8f0
FB
283 WHERE ' . $where . '
284 ORDER BY pa.id
285 ' . $limit);
f5642983
FB
286 }
287
288 public function getMainAddress()
289 {
290 $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
291 if ($it->total() == 0) {
292 return null;
293 } else {
294 return $it->next();
295 }
296 }
3e53a496 297
4bc5b8f0
FB
298
299 /* Educations
300 */
301 public function getEducations($flags, $limit = null)
302 {
303 $where = XDB::format('pe.uid = {?}', $this->id());
304 if ($flags & self::EDUCATION_MAIN) {
305 $where .= ' AND FIND_IN_SET(\'primary\', pe.flags)';
306 } else if ($flags & self::EDUCATION_EXTRA) {
307 $where .= ' AND NOT FIND_IN_SET(\'primary\', pe.flags)';
308 } else if ($flags & self::EDUCATION_FINISHED) {
309 $where .= ' AND pe.grad_year <= YEAR(CURDATE())';
310 } else if ($flags & self::EDUCATION_CURRENT) {
311 $where .= ' AND pe.grad_year > YEAR(CURDATE())';
312 }
313 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
314 return XDB::iterator('SELECT pe.entry_year, pe.grad_year, pe.program,
315 pee.name AS school, pee.abbreviation AS school_short, pee.url AS school_url, gc.countryFR AS country,
316 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level, pefe.field,
317 FIND_IN_SET(\'primary\', pe.flags) AS prim
318 FROM profile_education AS pe
319 INNER JOIN profile_education_enum AS pee ON (pe.eduid = pee.id)
320 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
321 INNER JOIN profile_education_degree_enum AS pede ON (pe.degreeid = pede.id)
322 LEFT JOIN profile_education_field_enum AS pefe ON (pe.fieldid = pefe.id)
323 WHERE ' . $where . '
324 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
325 ' . $limit);
326 }
327
328 public function getExtraEducations($limit = null)
329 {
330 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
331 }
332
333
04a94b1d
FB
334 /** Networking
335 */
336
337 public function getNetworking($flags, $limit = null)
338 {
339 $where = XDB::format('pn.uid = {?}', $this->id());
340 if ($flags & self::NETWORKING_WEB) {
341 $where .= ' AND pn.network_type = 0'; // XXX hardcoded reference to web site index
342 }
343 if ($this->visibility) {
344 $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility);
345 }
346 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
347 return XDB::iterator('SELECT pne.name, pne.icon,
348 IF (LENGTH(pne.link) > 0, REPLACE(pne.link, \'%s\', pn.address),
349 pn.address) AS address
350 FROM profile_networking AS pn
351 INNER JOIN profile_networking_enum AS pne ON (pn.network_type = pne.network_type)
352 WHERE ' . $where . '
353 ORDER BY pn.network_type, pn.nwid
354 ' . $limit);
355 }
356
357 public function getWebSite()
358 {
359 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
360 if ($site->total() != 1) {
361 return null;
362 }
363 $site = $site->next();
364 return $site['address'];
365 }
366
367
e718bd18
FB
368 /** Jobs
369 */
370
371 public function getJobs($flags, $limit = null)
372 {
373 $where = XDB::format('pj.uid = {?}', $this->id());
374 $cond = 'TRUE';
375 if ($this->visibility) {
376 $where .= ' AND pj.pub IN ' . XDB::formatArray($this->visibility);
377 $cond = 'pj.email_pub IN ' . XDB::formatArray($this->visibility);
378 }
379 $limit = is_null($limit) ? '' : XDB::format('LIMIT {?}', (int)$limit);
380 return XDB::iterator('SELECT pje.name, pje.acronym, pje.url, pje.email, pje.NAF_code,
381 pj.description, pj.url AS user_site,
382 IF (' . $cond . ', pj.email, NULL) AS user_email,
83ff5281
SJ
383 pjse.name AS sector, pjsse.name AS subsector,
384 pjssse.name AS subsubsector
e718bd18
FB
385 FROM profile_job AS pj
386 INNER JOIN profile_job_enum AS pje ON (pje.id = pj.jobid)
e718bd18
FB
387 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
388 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
389 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
390 WHERE ' . $where . '
391 ORDER BY pj.id
392 ' . $limit);
393 }
394
395 public function getMailJob()
396 {
397 $job = $this->getJobs(self::JOBS_MAIN, 1);
398 if ($job->total() != 1) {
399 return null;
400 }
401 return $job->next();
402 }
403
5c005b37
RB
404 /* Binets
405 */
406 public function getBinets()
407 {
408 return XDB::fetchColumn('SELECT binet_id
409 FROM binets_ins
410 WHERE user_id = {?}', $this->id());
411 }
412
e718bd18 413
e7b93962
FB
414 public function owner()
415 {
416 return User::getSilent($this);
417 }
418
b774ddab
FB
419 private static function fetchProfileData(array $pids)
420 {
421 if (count($pids) == 0) {
422 return array();
423 }
424 return XDB::fetchAllAssoc('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year,
425 pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname,
5c005b37
RB
426 IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary,
427 IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary,
46e9eb99 428 pd.promo AS promo, pd.short_name, pd.directory_name AS full_name,
5c005b37
RB
429 pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub,
430 ph.pub AS photo_pub
b774ddab
FB
431 FROM profiles AS p
432 INNER JOIN profile_display AS pd ON (pd.pid = p.pid)
433 INNER JOIN profile_education AS pe ON (pe.uid = p.pid AND FIND_IN_SET(\'primary\', pe.flags))
434 INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid
767025be 435 AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ')
b774ddab 436 INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid
767025be 437 AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ')
b774ddab 438 LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid
767025be 439 AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ')
b774ddab 440 LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid
767025be 441 AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ')
46e9eb99 442 LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid
b774ddab 443 AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ')
46e9eb99 444 LEFT JOIN profile_phones AS pp ON (pp.uid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\')
833a6e86 445 LEFT JOIN photo AS ph ON (ph.uid = p.pid)
b774ddab
FB
446 WHERE p.pid IN ' . XDB::formatArray($pids) . '
447 GROUP BY p.pid');
448 }
449
450 public static function getPID($login)
451 {
452 if ($login instanceof PlUser) {
453 return XDB::fetchOneCell('SELECT pid
454 FROM account_profiles
455 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
456 $login->id());
457 } else if (ctype_digit($login)) {
458 return XDB::fetchOneCell('SELECT pid
459 FROM profiles
460 WHERE pid = {?}', $login);
461 } else {
462 return XDB::fetchOneCell('SELECT pid
463 FROM profiles
464 WHERE hrpid = {?}', $login);
465 }
466 }
467
468
e7b93962
FB
469 /** Return the profile associated with the given login.
470 */
a3118782
FB
471 public static function get($login)
472 {
b774ddab
FB
473 $pid = self::getPID($login);
474 if (!is_null($pid)) {
475 $data = self::fetchProfileData(array($pid));
476 return new Profile(array_pop($data));
477 } else {
efe597c5
FB
478 /* Let say we can identify a profile using the identifiers of its owner.
479 */
455ea0c9
FB
480 if (!($login instanceof PlUser)) {
481 $user = User::getSilent($login);
482 if ($user && $user->hasProfile()) {
483 return $user->profile();
484 }
efe597c5 485 }
3e53a496 486 return null;
e7b93962
FB
487 }
488 }
a3118782 489
b774ddab
FB
490 /** Return profiles for the list of pids.
491 */
492 public static function getBulkProfilesWithPIDs(array $pids)
493 {
494 if (count($pids) == 0) {
495 return array();
496 }
497 $data = self::fetchProfileData($pids);
498 $inv = array_flip($pids);
499 $profiles = array();
500 foreach ($data AS $p) {
501 $p = new Profile($p);
502 $key = $inv[$p->id()];
503 $profiles[$key] = $p;
504 }
505 return $profiles;
506 }
507
508 /** Return profiles for uids.
509 */
510 public static function getBulkProfilesWithUIDS(array $uids)
511 {
512 if (count($uids) == 0) {
513 return array();
514 }
515 $table = XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid
516 FROM account_profiles AS ap
517 WHERE FIND_IN_SET(\'owner\', ap.perms)
518 AND ap.uid IN ' . XDB::formatArray($uids));
519 return self::getBulkProfilesWithPIDs($table);
520 }
521
913a4e90
RB
522 public static function isDisplayName($name)
523 {
524 return $name == self::DN_FULL || $name == self::DN_DISPLAY
525 || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY
526 || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC
527 || $name == self::DN_SHORT || $name == self::DN_SORT;
528 }
529
a3118782
FB
530 public static function getNameTypeId($type, $for_sql = false)
531 {
532 if (!S::has('name_types')) {
eb6207f7 533 $table = XDB::fetchAllAssoc('type', 'SELECT id, type
32742f84 534 FROM profile_name_enum');
a3118782
FB
535 S::set('name_types', $table);
536 } else {
537 $table = S::v('name_types');
538 }
539 if ($for_sql) {
540 return XDB::escape($table[$type]);
541 } else {
542 return $table[$type];
543 }
544 }
e7b93962
FB
545}
546
547// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
548?>