3dd0e146e59f9033449d52cbb54cd297b6380bf0
[platal.git] / classes / profile.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22 class Profile
23 {
24 static private $v_values = array('public' => array('public'),
25 'ax' => array('ax', 'public'),
26 'private' => array('private', 'ax', 'public'));
27
28 const VISIBILITY_PUBLIC = 'public';
29 const VISIBILITY_AX = 'ax';
30 const VISIBILITY_PRIVATE = 'private';
31
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
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;
65 const EDUCATION_EXTRA = 0x000020;
66 const EDUCATION_ALL = 0x000040;
67 const EDUCATION_FINISHED = 0x000080;
68 const EDUCATION_CURRENT = 0x000100;
69
70 const JOBS_MAIN = 0x001000;
71 const JOBS_ALL = 0x002000;
72 const JOBS_FINISHED = 0x004000;
73 const JOBS_CURRENT = 0x008000;
74
75 const NETWORKING_ALL = 0x000000;
76 const NETWORKING_WEB = 0x010000;
77 const NETWORKING_IM = 0x020000;
78 const NETWORKING_SOCIAL = 0x040000;
79
80 private $pid;
81 private $hrpid;
82 private $data = array();
83
84 private $visibility = null;
85
86 private function __construct(array $data)
87 {
88 $this->data = $data;
89 $this->pid = $this->data['pid'];
90 $this->hrpid = $this->data['hrpid'];
91 if (!S::logged()) {
92 $this->setVisibilityLevel(self::VISIBILITY_PUBLIC);
93 }
94 }
95
96 public function id()
97 {
98 return $this->pid;
99 }
100
101 public function hrid()
102 {
103 return $this->hrpid;
104 }
105
106 public function promo()
107 {
108 return $this->promo;
109 }
110
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 {
146 return $this->firstname;
147 }
148
149 public function lastName()
150 {
151 return $this->lastname;
152 }
153
154 public function isFemale()
155 {
156 return $this->sex == PlUser::GENDER_FEMALE;
157 }
158
159 public function data()
160 {
161 $this->first_name;
162 return $this->data;
163 }
164
165 public function __get($name)
166 {
167 if (property_exists($this, $name)) {
168 return $this->$name;
169 }
170
171 if (isset($this->data[$name])) {
172 return $this->data[$name];
173 }
174
175 return null;
176 }
177
178 public function __isset($name)
179 {
180 return property_exists($this, $name) || isset($this->data[$name]);
181 }
182
183 public function setVisibilityLevel($visibility)
184 {
185 if ($visibility != self::VISIBILITY_PRIVATE
186 && $visibility != self::VISIBILITY_AX
187 && $visibility != self::VISIBILITY_PUBLIC) {
188 Platal::page()->kill("Visibility invalide: " . $visibility);
189 }
190 $this->visibility = self::$v_values[$visibility];
191 if ($this->mobile && !in_array($this->modbile_pub, $this->visibility)) {
192 unset($this->data['mobile']);
193 }
194 }
195
196
197 /* Photo
198 */
199 public function getPhoto($fallback = true)
200 {
201 /* TODO: migrate photo table to profile_photo, change uid to pid
202 */
203 $cond = '';
204 if ($this->visibility) {
205 $cond = ' AND pub IN ' . XDB::formatArray($this->visibility);
206 }
207 $res = XDB::query('SELECT *
208 FROM photo
209 WHERE attachmime IN (\'jpeg\', \'png\')
210 ' . $cond . ' AND uid = {?}',
211 $this->id());
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',
218 'image/png');
219 }
220 return null;
221 }
222
223 /* Addresses
224 */
225 public function getAddresses($flags, $limit = null)
226 {
227 $where = XDB::format('pa.pid = {?}', $this->id());
228 if ($flags & self::ADDRESS_MAIN) {
229 $where .= ' AND FIND_IN_SET(\'current\', pa.flags)';
230 }
231 if ($flags & self::ADDRESS_POSTAL) {
232 $where .= ' AND FIND_IN_SET(\'mail\', pa.flags)';
233 }
234 if ($this->visibility) {
235 $where .= ' AND pa.pub IN ' . XDB::formatArray($this->visibility);
236 }
237 $type = array();
238 if ($flags & self::ADDRESS_PRO) {
239 $type[] = 'job';
240 }
241 if ($flags & self::ADDRESS_PERSO) {
242 $type[] = 'home';
243 }
244 if (count($type) > 0) {
245 $where .= ' AND pa.type IN ' . XDB::formatArray($type);
246 }
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)
260 WHERE ' . $where . '
261 ORDER BY pa.id
262 ' . $limit);
263 }
264
265 public function getMainAddress()
266 {
267 $it = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
268 if ($it->total() == 0) {
269 return null;
270 } else {
271 return $it->next();
272 }
273 }
274
275
276 /* Educations
277 */
278 public function getEducations($flags, $limit = null)
279 {
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())';
289 }
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)
300 WHERE ' . $where . '
301 ORDER BY NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id
302 ' . $limit);
303 }
304
305 public function getExtraEducations($limit = null)
306 {
307 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
308 }
309
310
311 /** Networking
312 */
313
314 public function getNetworking($flags, $limit = null)
315 {
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
319 }
320 if ($this->visibility) {
321 $where .= ' AND pn.pub IN ' . XDB::formatArray($this->visibility);
322 }
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)
329 WHERE ' . $where . '
330 ORDER BY pn.network_type, pn.nwid
331 ' . $limit);
332 }
333
334 public function getWebSite()
335 {
336 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
337 if ($site->total() != 1) {
338 return null;
339 }
340 $site = $site->next();
341 return $site['address'];
342 }
343
344
345 /** Jobs
346 */
347
348 public function getJobs($flags, $limit = null)
349 {
350 $where = XDB::format('pj.uid = {?}', $this->id());
351 $cond = 'TRUE';
352 if ($this->visibility) {
353 $where .= ' AND pj.pub IN ' . XDB::formatArray($this->visibility);
354 $cond = 'pj.email_pub IN ' . XDB::formatArray($this->visibility);
355 }
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)
367 WHERE ' . $where . '
368 ORDER BY pj.id
369 ' . $limit);
370 }
371
372 public function getMailJob()
373 {
374 $job = $this->getJobs(self::JOBS_MAIN, 1);
375 if ($job->total() != 1) {
376 return null;
377 }
378 return $job->next();
379 }
380
381
382 public function owner()
383 {
384 return User::getSilent($this);
385 }
386
387 private static function fetchProfileData(array $pids)
388 {
389 if (count($pids) == 0) {
390 return array();
391 }
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
398 FROM profiles AS p
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) . '
414 GROUP BY p.pid');
415 }
416
417 public static function getPID($login)
418 {
419 if ($login instanceof PlUser) {
420 return XDB::fetchOneCell('SELECT pid
421 FROM account_profiles
422 WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)',
423 $login->id());
424 } else if (ctype_digit($login)) {
425 return XDB::fetchOneCell('SELECT pid
426 FROM profiles
427 WHERE pid = {?}', $login);
428 } else {
429 return XDB::fetchOneCell('SELECT pid
430 FROM profiles
431 WHERE hrpid = {?}', $login);
432 }
433 }
434
435
436 /** Return the profile associated with the given login.
437 */
438 public static function get($login)
439 {
440 $pid = self::getPID($login);
441 if (!is_null($pid)) {
442 $data = self::fetchProfileData(array($pid));
443 return new Profile(array_pop($data));
444 } else {
445 /* Let say we can identify a profile using the identifiers of its owner.
446 */
447 if (!($login instanceof PlUser)) {
448 $user = User::getSilent($login);
449 if ($user && $user->hasProfile()) {
450 return $user->profile();
451 }
452 }
453 return null;
454 }
455 }
456
457 /** Return profiles for the list of pids.
458 */
459 public static function getBulkProfilesWithPIDs(array $pids)
460 {
461 if (count($pids) == 0) {
462 return array();
463 }
464 $data = self::fetchProfileData($pids);
465 $inv = array_flip($pids);
466 $profiles = array();
467 foreach ($data AS $p) {
468 $p = new Profile($p);
469 $key = $inv[$p->id()];
470 $profiles[$key] = $p;
471 }
472 return $profiles;
473 }
474
475 /** Return profiles for uids.
476 */
477 public static function getBulkProfilesWithUIDS(array $uids)
478 {
479 if (count($uids) == 0) {
480 return array();
481 }
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);
487 }
488
489 public static function isDisplayName($name)
490 {
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;
495 }
496
497 public static function getNameTypeId($type, $for_sql = false)
498 {
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);
503 } else {
504 $table = S::v('name_types');
505 }
506 if ($for_sql) {
507 return XDB::escape($table[$type]);
508 } else {
509 return $table[$type];
510 }
511 }
512 }
513
514 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
515 ?>