Commit | Line | Data |
---|---|---|
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 | ||
1a9affb7 | 22 | class ProfileVisibility |
e7b93962 | 23 | { |
1a9affb7 RB |
24 | static private $v_values = array(self::VIS_PUBLIC => array(self::VIS_PUBLIC), |
25 | self::VIS_AX => array(self::VIS_AX, self::VIS_PUBLIC), | |
26 | self::VIS_PRIVATE => array(self::VIS_PRIVATE, self::VIS_AX, self::VIS_PUBLIC)); | |
27 | ||
28 | const VIS_PUBLIC = 'public'; | |
29 | const VIS_AX = 'ax'; | |
30 | const VIS_PRIVATE = 'private'; | |
31 | ||
32 | private $level; | |
913a4e90 | 33 | |
1a9affb7 RB |
34 | public function __construct($level = null) |
35 | { | |
36 | $this->setLevel($level); | |
37 | } | |
38 | ||
39 | public function setLevel($level = self::VIS_PUBLIC) | |
40 | { | |
41 | if ($level != null | |
42 | && $level != self::VIS_PRIVATE | |
43 | && $level != self::VIS_AX | |
44 | && $level != self::VIS_PUBLIC) { | |
45 | Platal::page()->kill("Invalid visibility: " . $level); | |
46 | } | |
47 | ||
48 | if (!S::logged()) { | |
49 | $level = self::VIS_PUBLIC; | |
50 | } else if ($level == null) { | |
51 | $level = self::VIS_PRIVATE; | |
52 | } | |
53 | ||
54 | if ($this->level == null || $this->level == self::VIS_PRIVATE) { | |
55 | $this->level = $level; | |
56 | } else if ($this->level == self::VIS_AX && $level == self::VIS_PRIVATE) { | |
57 | return; | |
58 | } else { | |
59 | $this->level = self::VIS_PUBLIC; | |
60 | } | |
61 | } | |
62 | ||
63 | public function level() | |
64 | { | |
65 | if ($this->level == null) { | |
66 | return self::VIS_PUBLIC; | |
67 | } else { | |
68 | return $this->level; | |
69 | } | |
70 | } | |
71 | ||
72 | public function levels() | |
73 | { | |
74 | return self::$v_values[$this->level()]; | |
75 | } | |
76 | ||
77 | public function isVisible($visibility) | |
78 | { | |
79 | return in_array($visibility, $this->levels()); | |
80 | } | |
81 | } | |
82 | ||
83 | class Profile | |
84 | { | |
f5642983 | 85 | |
913a4e90 RB |
86 | /* name tokens */ |
87 | const LASTNAME = 'lastname'; | |
88 | const FIRSTNAME = 'firstname'; | |
89 | const NICKNAME = 'nickname'; | |
90 | const PSEUDONYM = 'pseudonym'; | |
91 | const NAME = 'name'; | |
92 | /* name variants */ | |
93 | const VN_MARITAL = 'marital'; | |
94 | const VN_ORDINARY = 'ordinary'; | |
95 | const VN_OTHER = 'other'; | |
96 | const VN_INI = 'ini'; | |
97 | /* display names */ | |
98 | const DN_FULL = 'directory_name'; | |
99 | const DN_DISPLAY = 'yourself'; | |
100 | const DN_YOURSELF = 'yourself'; | |
101 | const DN_DIRECTORY = 'directory_name'; | |
102 | const DN_PRIVATE = 'private_name'; | |
103 | const DN_PUBLIC = 'public_name'; | |
104 | const DN_SHORT = 'short_name'; | |
105 | const DN_SORT = 'sort_name'; | |
e02d9fbb SJ |
106 | /* education related names */ |
107 | const EDU_X = 'École polytechnique'; | |
108 | const DEGREE_X = 'Ingénieur'; | |
109 | const DEGREE_M = 'Master'; | |
110 | const DEGREE_D = 'Doctorat'; | |
913a4e90 RB |
111 | |
112 | static public $name_variants = array( | |
113 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
114 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) | |
115 | ); | |
116 | ||
f5642983 FB |
117 | const ADDRESS_MAIN = 0x000001; |
118 | const ADDRESS_PERSO = 0x000002; | |
119 | const ADDRESS_PRO = 0x000004; | |
120 | const ADDRESS_ALL = 0x000006; | |
121 | const ADDRESS_POSTAL = 0x000008; | |
122 | ||
123 | const EDUCATION_MAIN = 0x000010; | |
4bc5b8f0 FB |
124 | const EDUCATION_EXTRA = 0x000020; |
125 | const EDUCATION_ALL = 0x000040; | |
126 | const EDUCATION_FINISHED = 0x000080; | |
127 | const EDUCATION_CURRENT = 0x000100; | |
f5642983 | 128 | |
4bc5b8f0 FB |
129 | const JOBS_MAIN = 0x001000; |
130 | const JOBS_ALL = 0x002000; | |
131 | const JOBS_FINISHED = 0x004000; | |
132 | const JOBS_CURRENT = 0x008000; | |
f5642983 | 133 | |
04a94b1d FB |
134 | const NETWORKING_ALL = 0x000000; |
135 | const NETWORKING_WEB = 0x010000; | |
136 | const NETWORKING_IM = 0x020000; | |
137 | const NETWORKING_SOCIAL = 0x040000; | |
138 | ||
e7ba5725 RB |
139 | const FETCH_ADDRESSES = 0x000001; |
140 | const FETCH_CORPS = 0x000002; | |
141 | const FETCH_EDU = 0x000004; | |
142 | const FETCH_JOBS = 0x000008; | |
143 | const FETCH_MEDALS = 0x000010; | |
144 | const FETCH_NETWORKING = 0x000020; | |
145 | const FETCH_PHONES = 0x000040; | |
9f21bd15 | 146 | |
7988f7d6 | 147 | const FETCH_MINIFICHES = 0x00004D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_PHONES |
fa9994fa | 148 | |
330ffaa0 | 149 | const FETCH_ALL = 0x0000FF; // OR of FETCH_* |
6b5008ec | 150 | |
e7b93962 FB |
151 | private $pid; |
152 | private $hrpid; | |
3e53a496 FB |
153 | private $data = array(); |
154 | ||
f5642983 FB |
155 | private $visibility = null; |
156 | ||
b774ddab | 157 | private function __construct(array $data) |
e7b93962 | 158 | { |
b774ddab | 159 | $this->data = $data; |
832e6fcb FB |
160 | $this->pid = $this->data['pid']; |
161 | $this->hrpid = $this->data['hrpid']; | |
1a9affb7 | 162 | $this->visibility = new ProfileVisibility(); |
9f21bd15 RB |
163 | } |
164 | ||
e7b93962 FB |
165 | public function id() |
166 | { | |
167 | return $this->pid; | |
168 | } | |
169 | ||
170 | public function hrid() | |
171 | { | |
172 | return $this->hrpid; | |
173 | } | |
174 | ||
d5e60905 FB |
175 | public function promo() |
176 | { | |
177 | return $this->promo; | |
178 | } | |
179 | ||
845f0a4d SJ |
180 | public function yearpromo() |
181 | { | |
182 | return intval(substr($this->promo, 1, 4)); | |
183 | } | |
184 | ||
94b72319 FB |
185 | /** Print a name with the given formatting: |
186 | * %s = • for women | |
187 | * %f = firstname | |
188 | * %l = lastname | |
189 | * %F = fullname | |
190 | * %S = shortname | |
191 | * %p = promo | |
192 | */ | |
193 | public function name($format) | |
194 | { | |
195 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
196 | array($this->isFemale() ? '•' : '', | |
faf75faf SJ |
197 | $this->firstName(), $this->lastName(), |
198 | $this->fullName(), $this->shortName(), | |
199 | $this->promo()), $format); | |
94b72319 FB |
200 | } |
201 | ||
202 | public function fullName($with_promo = false) | |
203 | { | |
204 | if ($with_promo) { | |
205 | return $this->full_name . ' (' . $this->promo . ')'; | |
206 | } | |
207 | return $this->full_name; | |
208 | } | |
209 | ||
210 | public function shortName($with_promo = false) | |
211 | { | |
212 | if ($with_promo) { | |
213 | return $this->short_name . ' (' . $this->promo . ')'; | |
214 | } | |
215 | return $this->short_name; | |
216 | } | |
217 | ||
218 | public function firstName() | |
219 | { | |
08c91036 | 220 | return $this->firstname; |
94b72319 FB |
221 | } |
222 | ||
5c005b37 RB |
223 | public function firstNames() |
224 | { | |
225 | return $this->nameVariants(self::FIRSTNAME); | |
226 | } | |
227 | ||
94b72319 FB |
228 | public function lastName() |
229 | { | |
08c91036 | 230 | return $this->lastname; |
94b72319 FB |
231 | } |
232 | ||
5c005b37 RB |
233 | public function lastNames() |
234 | { | |
235 | return $this->nameVariants(self::LASTNAME); | |
236 | } | |
237 | ||
94b72319 FB |
238 | public function isFemale() |
239 | { | |
240 | return $this->sex == PlUser::GENDER_FEMALE; | |
241 | } | |
242 | ||
400ac338 RB |
243 | public function isDead() |
244 | { | |
245 | return ($this->deathdate != null); | |
246 | } | |
247 | ||
94b72319 FB |
248 | public function data() |
249 | { | |
250 | $this->first_name; | |
251 | return $this->data; | |
252 | } | |
253 | ||
5c005b37 RB |
254 | private function nameVariants($type) |
255 | { | |
256 | $vals = array($this->$type); | |
257 | foreach (self::$name_variants[$type] as $var) { | |
258 | $vartype = $type . '_' . $var; | |
259 | $varname = $this->$vartype; | |
260 | if ($varname != null && $varname != "") { | |
261 | $vals[] = $varname; | |
262 | } | |
263 | } | |
264 | return array_unique($vals); | |
265 | } | |
266 | ||
b6569a95 FB |
267 | public function nationalities() |
268 | { | |
269 | $nats = array(); | |
270 | if ($this->nationality1) { | |
271 | $nats[] = $this->nationality1; | |
272 | } | |
273 | if ($this->nationality2) { | |
274 | $nats[] = $this->nationality2; | |
275 | } | |
276 | if ($this->nationality3) { | |
277 | $nats[] = $this->nationality3; | |
278 | } | |
279 | return $nats; | |
280 | } | |
281 | ||
3e53a496 FB |
282 | public function __get($name) |
283 | { | |
284 | if (property_exists($this, $name)) { | |
285 | return $this->$name; | |
286 | } | |
287 | ||
3e53a496 FB |
288 | if (isset($this->data[$name])) { |
289 | return $this->data[$name]; | |
290 | } | |
291 | ||
292 | return null; | |
293 | } | |
294 | ||
295 | public function __isset($name) | |
296 | { | |
297 | return property_exists($this, $name) || isset($this->data[$name]); | |
298 | } | |
299 | ||
c159e50f RB |
300 | /** Sets the level of visibility of the profile |
301 | * Sets $this->visibility to a list of valid visibilities. | |
1a9affb7 | 302 | * @param one of the self::VIS_* values |
c159e50f | 303 | */ |
f5642983 FB |
304 | public function setVisibilityLevel($visibility) |
305 | { | |
1a9affb7 | 306 | $this->visibility->setLevel($visibility); |
f5642983 FB |
307 | } |
308 | ||
c159e50f RB |
309 | /** Determine whether an item with visibility $visibility can be displayed |
310 | * with the current level of visibility of the profile | |
311 | * @param $visibility The level of visibility to be checked | |
312 | */ | |
313 | public function isVisible($visibility) | |
314 | { | |
1a9affb7 | 315 | return $this->visibility->isVisible($visibility); |
9f21bd15 RB |
316 | } |
317 | ||
990cb17b RB |
318 | private function getProfileField($cls) |
319 | { | |
320 | return ProfileField::getForPID($cls, $this->id(), $this->visibility); | |
321 | } | |
322 | ||
8cf886dc RB |
323 | /** Consolidates internal data (addresses, phones, jobs) |
324 | */ | |
325 | private function consolidateFields() | |
326 | { | |
327 | if ($this->phones != null) { | |
328 | if ($this->addresses != null) { | |
329 | $this->addresses->addPhones($this->phones); | |
330 | } | |
331 | ||
332 | if ($this->jobs != null) { | |
333 | $this->jobs->addPhones($this->phones); | |
334 | } | |
335 | } | |
336 | ||
337 | if ($this->addresses != null && $this->jobs != null) { | |
338 | $this->jobs->addAddresses($this->addresses); | |
339 | } | |
340 | } | |
341 | ||
833a6e86 FB |
342 | /* Photo |
343 | */ | |
1a9affb7 | 344 | private $photo = null; |
7988f7d6 | 345 | public function getPhoto($fallback = true, $data = false) |
9f21bd15 | 346 | { |
1a9affb7 RB |
347 | if ($this->has_photo) { |
348 | if ($data && ($this->photo == null || $this->photo->mimeType == null)) { | |
349 | $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y | |
7988f7d6 RB |
350 | FROM profile_photos |
351 | WHERE pid = {?}', $this->pid); | |
1a9affb7 RB |
352 | $this->photo = PlImage::fromData($res['attach'], $res['attachmime'], $res['x'], $res['y']); |
353 | } else if ($this->photo == null) { | |
354 | $this->photo = PlImage::fromData(null, null, $this->photo_width, $this->photo_height); | |
355 | } | |
356 | return $this->photo; | |
9f21bd15 RB |
357 | } else if ($fallback) { |
358 | return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png', | |
359 | 'image/png'); | |
360 | } | |
361 | return null; | |
362 | } | |
7d0ebdf5 | 363 | |
4bc5b8f0 FB |
364 | /* Addresses |
365 | */ | |
7d0ebdf5 RB |
366 | private $addresses = null; |
367 | public function setAddresses(ProfileAddresses $addr) | |
368 | { | |
369 | $this->addresses = $addr; | |
8cf886dc | 370 | $this->consolidateFields(); |
7d0ebdf5 RB |
371 | } |
372 | ||
4bc5b8f0 | 373 | public function getAddresses($flags, $limit = null) |
f5642983 | 374 | { |
7d0ebdf5 | 375 | if ($this->addresses == null) { |
990cb17b | 376 | $this->setAddresses($this->getProfileField('ProfileAddresses')); |
f5642983 | 377 | } |
0907501b RB |
378 | |
379 | if ($this->addresses == null) { | |
598c57f6 | 380 | return array(); |
0907501b | 381 | } |
990cb17b | 382 | return $this->addresses->get($flags, $limit); |
f5642983 FB |
383 | } |
384 | ||
598c57f6 RB |
385 | public function iterAddresses($flags, $limit = null) |
386 | { | |
387 | return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true); | |
388 | } | |
389 | ||
f5642983 FB |
390 | public function getMainAddress() |
391 | { | |
598c57f6 RB |
392 | $addr = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN); |
393 | if (count($addr) == 0) { | |
f5642983 FB |
394 | return null; |
395 | } else { | |
598c57f6 | 396 | return array_pop($addr); |
f5642983 FB |
397 | } |
398 | } | |
3e53a496 | 399 | |
bdef0d33 RB |
400 | /* Phones |
401 | */ | |
402 | private $phones = null; | |
403 | public function setPhones(ProfilePhones $phones) | |
404 | { | |
405 | $this->phones = $phones; | |
406 | $this->consolidateFields(); | |
407 | } | |
408 | ||
409 | public function getPhones($flags, $limit = null) | |
410 | { | |
411 | if ($this->phones == null) { | |
412 | $this->setPhones($this->getProfileField('ProfilePhones')); | |
413 | } | |
414 | ||
415 | if ($this->phones == null) { | |
598c57f6 | 416 | return array(); |
bdef0d33 RB |
417 | } |
418 | return $this->phones->get($flags, $limit); | |
419 | } | |
4bc5b8f0 FB |
420 | |
421 | /* Educations | |
422 | */ | |
d4d395bb | 423 | private $educations = null; |
a060e1c3 RB |
424 | public function setEducations(ProfileEducation $edu) |
425 | { | |
426 | $this->educations = $edu; | |
427 | } | |
428 | ||
4bc5b8f0 FB |
429 | public function getEducations($flags, $limit = null) |
430 | { | |
d4d395bb | 431 | if ($this->educations == null) { |
990cb17b | 432 | $this->setEducations($this->getProfileField('ProfileEducation')); |
d4d395bb | 433 | } |
0907501b RB |
434 | |
435 | if ($this->educations == null) { | |
598c57f6 | 436 | return array(); |
0907501b | 437 | } |
a060e1c3 | 438 | return $this->educations->get($flags, $limit); |
4bc5b8f0 FB |
439 | } |
440 | ||
441 | public function getExtraEducations($limit = null) | |
442 | { | |
443 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); | |
444 | } | |
445 | ||
0396c259 RB |
446 | /* Corps |
447 | */ | |
448 | private $corps = null; | |
449 | public function setCorps(ProfileCorps $corps) | |
450 | { | |
451 | $this->corps = $corps; | |
452 | } | |
453 | ||
454 | public function getCorps() | |
455 | { | |
456 | if ($this->corps == null) { | |
457 | $this->setCorps($this->getProfileField('ProfileCorps')); | |
458 | } | |
459 | return $this->corps; | |
460 | } | |
4bc5b8f0 | 461 | |
04a94b1d FB |
462 | /** Networking |
463 | */ | |
d4d395bb RB |
464 | private $networks = null; |
465 | public function setNetworking(ProfileNetworking $nw) | |
466 | { | |
467 | $this->networks = $nw; | |
468 | } | |
04a94b1d FB |
469 | |
470 | public function getNetworking($flags, $limit = null) | |
471 | { | |
d4d395bb | 472 | if ($this->networks == null) { |
990cb17b | 473 | $this->setNetworking($this->getProfileField('ProfileNetworking')); |
04a94b1d | 474 | } |
0907501b | 475 | if ($this->networks == null) { |
598c57f6 | 476 | return array(); |
0907501b | 477 | } |
d4d395bb | 478 | return $this->networks->get($flags, $limit); |
04a94b1d FB |
479 | } |
480 | ||
481 | public function getWebSite() | |
482 | { | |
483 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); | |
598c57f6 | 484 | if (count($site) != 1) { |
04a94b1d FB |
485 | return null; |
486 | } | |
598c57f6 | 487 | $site = array_pop($site); |
04a94b1d FB |
488 | return $site['address']; |
489 | } | |
490 | ||
491 | ||
e718bd18 FB |
492 | /** Jobs |
493 | */ | |
949fc736 RB |
494 | private $jobs = null; |
495 | public function setJobs(ProfileJobs $jobs) | |
496 | { | |
497 | $this->jobs = $jobs; | |
498 | $this->consolidateFields(); | |
499 | } | |
e718bd18 FB |
500 | |
501 | public function getJobs($flags, $limit = null) | |
502 | { | |
949fc736 RB |
503 | if ($this->jobs == null) { |
504 | $this->setJobs($this->getProfileField('ProfileJobs')); | |
e718bd18 | 505 | } |
949fc736 | 506 | |
0907501b | 507 | if ($this->jobs == null) { |
598c57f6 | 508 | return array(); |
0907501b | 509 | } |
949fc736 | 510 | return $this->jobs->get($flags, $limit); |
e718bd18 FB |
511 | } |
512 | ||
44ec5eb5 | 513 | public function getMainJob() |
e718bd18 FB |
514 | { |
515 | $job = $this->getJobs(self::JOBS_MAIN, 1); | |
598c57f6 | 516 | if (count($job) != 1) { |
e718bd18 FB |
517 | return null; |
518 | } | |
598c57f6 | 519 | return array_pop($job); |
e718bd18 FB |
520 | } |
521 | ||
5c005b37 RB |
522 | /* Binets |
523 | */ | |
524 | public function getBinets() | |
525 | { | |
526 | return XDB::fetchColumn('SELECT binet_id | |
5c8a71f2 | 527 | FROM profile_binets |
bdd977d7 | 528 | WHERE pid = {?}', $this->id()); |
5c005b37 | 529 | } |
97b71de5 RB |
530 | public function getBinetsNames() |
531 | { | |
532 | return XDB::fetchColumn('SELECT text | |
533 | FROM profile_binets AS pb | |
534 | LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id) | |
535 | WHERE pb.pid = {?}', $this->id()); | |
536 | } | |
5c005b37 | 537 | |
26ca919b RB |
538 | /* Medals |
539 | */ | |
540 | private $medals = null; | |
541 | public function setMedals(ProfileMedals $medals) | |
542 | { | |
543 | $this->medals = $medals; | |
544 | } | |
545 | ||
546 | public function getMedals() | |
547 | { | |
548 | if ($this->medals == null) { | |
549 | $this->setMedals($this->getProfileField('ProfileMedals')); | |
550 | } | |
551 | if ($this->medals == null) { | |
552 | return array(); | |
553 | } | |
554 | return $this->medals->medals; | |
555 | } | |
e718bd18 | 556 | |
e7b93962 FB |
557 | public function owner() |
558 | { | |
559 | return User::getSilent($this); | |
560 | } | |
561 | ||
94590511 SJ |
562 | public function compareNames($firstname, $lastname) |
563 | { | |
564 | $_lastname = mb_strtoupper($this->lastName()); | |
565 | $_firstname = mb_strtoupper($this->firstName()); | |
566 | $lastname = mb_strtoupper($lastname); | |
567 | $firstname = mb_strtoupper($firstname); | |
568 | ||
569 | $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname)); | |
570 | $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY); | |
571 | $maxlen = 0; | |
572 | ||
573 | foreach ($tokens as $str) { | |
574 | $isOk &= (strpos($_lastname, $str) !== false); | |
575 | $maxlen = max($maxlen, strlen($str)); | |
576 | } | |
577 | ||
578 | return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname))); | |
579 | } | |
580 | ||
9f21bd15 | 581 | private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
b774ddab FB |
582 | { |
583 | if (count($pids) == 0) { | |
584 | return array(); | |
585 | } | |
0d906109 RB |
586 | |
587 | if ($respect_order) { | |
588 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); | |
589 | } else { | |
590 | $order = ''; | |
591 | } | |
592 | ||
1a9affb7 | 593 | $visibility = new ProfileVisibility($visibility); |
9f21bd15 | 594 | |
97b71de5 | 595 | $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, pse.text AS section, |
9f21bd15 RB |
596 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, |
597 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, | |
598 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, | |
599 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name, | |
1a9affb7 RB |
600 | pd.directory_name, IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile, |
601 | (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo, | |
7988f7d6 | 602 | ph.x AS photo_width, ph.y AS photo_height, |
9f21bd15 | 603 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, |
bdef0d33 | 604 | pm.expertise AS mentor_expertise, |
9f21bd15 RB |
605 | ap.uid AS owner_id |
606 | FROM profiles AS p | |
607 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
608 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
97b71de5 | 609 | LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section) |
9f21bd15 RB |
610 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid |
611 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
612 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
613 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
614 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
615 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
616 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
617 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
618 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid | |
619 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
620 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') | |
621 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) | |
bdef0d33 | 622 | LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid) |
9f21bd15 RB |
623 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
624 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' | |
625 | GROUP BY p.pid | |
1a9affb7 | 626 | ' . $order, $visibility->levels(), $visibility->levels()); |
31ef12ef | 627 | return new ProfileIterator($it, $pids, $fields, $visibility); |
b774ddab FB |
628 | } |
629 | ||
630 | public static function getPID($login) | |
631 | { | |
632 | if ($login instanceof PlUser) { | |
633 | return XDB::fetchOneCell('SELECT pid | |
634 | FROM account_profiles | |
635 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
636 | $login->id()); | |
9f21bd15 | 637 | } else if (ctype_digit($login)) { |
b774ddab FB |
638 | return XDB::fetchOneCell('SELECT pid |
639 | FROM profiles | |
640 | WHERE pid = {?}', $login); | |
641 | } else { | |
642 | return XDB::fetchOneCell('SELECT pid | |
643 | FROM profiles | |
644 | WHERE hrpid = {?}', $login); | |
645 | } | |
646 | } | |
647 | ||
0d906109 RB |
648 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
649 | { | |
650 | if ($respect_order) { | |
651 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
652 | } else { | |
653 | $order = ''; | |
654 | } | |
655 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
656 | FROM account_profiles AS ap | |
657 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
9f21bd15 RB |
658 | AND ap.uid IN ' . XDB::formatArray($uids) .' |
659 | ' . $order); | |
0d906109 | 660 | } |
b774ddab | 661 | |
e7b93962 FB |
662 | /** Return the profile associated with the given login. |
663 | */ | |
9f21bd15 | 664 | public static function get($login, $fields = 0x0000, $visibility = null) |
a3118782 | 665 | { |
641f2115 | 666 | if (is_array($login)) { |
1a9affb7 RB |
667 | $pf = new Profile($login); |
668 | $pf->setVisibilityLevel($visibility); | |
669 | return $pf; | |
641f2115 | 670 | } |
b774ddab FB |
671 | $pid = self::getPID($login); |
672 | if (!is_null($pid)) { | |
9f21bd15 | 673 | $it = self::iterOverPIDs(array($pid), false, $fields, $visibility); |
0d906109 | 674 | return $it->next(); |
b774ddab | 675 | } else { |
efe597c5 FB |
676 | /* Let say we can identify a profile using the identifiers of its owner. |
677 | */ | |
455ea0c9 FB |
678 | if (!($login instanceof PlUser)) { |
679 | $user = User::getSilent($login); | |
680 | if ($user && $user->hasProfile()) { | |
681 | return $user->profile(); | |
682 | } | |
efe597c5 | 683 | } |
3e53a496 | 684 | return null; |
e7b93962 FB |
685 | } |
686 | } | |
a3118782 | 687 | |
9f21bd15 | 688 | public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 689 | { |
9f21bd15 | 690 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility); |
0d906109 RB |
691 | } |
692 | ||
9f21bd15 | 693 | public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 694 | { |
9f21bd15 | 695 | return self::fetchProfileData($pids, $respect_order, $fields, $visibility); |
0d906109 RB |
696 | } |
697 | ||
b774ddab FB |
698 | /** Return profiles for the list of pids. |
699 | */ | |
1a9affb7 | 700 | public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null) |
b774ddab FB |
701 | { |
702 | if (count($pids) == 0) { | |
703 | return array(); | |
704 | } | |
9f21bd15 | 705 | $it = self::iterOverPIDs($pids, true, $fields, $visibility); |
b774ddab | 706 | $profiles = array(); |
0d906109 RB |
707 | while ($p = $it->next()) { |
708 | $profiles[$p->id()] = $p; | |
b774ddab FB |
709 | } |
710 | return $profiles; | |
711 | } | |
712 | ||
713 | /** Return profiles for uids. | |
714 | */ | |
9f21bd15 | 715 | public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null) |
b774ddab FB |
716 | { |
717 | if (count($uids) == 0) { | |
718 | return array(); | |
719 | } | |
9f21bd15 | 720 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility); |
b774ddab FB |
721 | } |
722 | ||
913a4e90 RB |
723 | public static function isDisplayName($name) |
724 | { | |
725 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
726 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
727 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
728 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
729 | } | |
730 | ||
9f21bd15 RB |
731 | public static function getNameTypeId($type, $for_sql = false) |
732 | { | |
733 | if (!S::has('name_types')) { | |
734 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type | |
735 | FROM profile_name_enum'); | |
736 | S::set('name_types', $table); | |
737 | } else { | |
738 | $table = S::v('name_types'); | |
739 | } | |
740 | if ($for_sql) { | |
741 | return XDB::escape($table[$type]); | |
742 | } else { | |
743 | return $table[$type]; | |
744 | } | |
745 | } | |
746 | ||
726eaf7a SJ |
747 | public static function rebuildSearchTokens($pid) |
748 | { | |
749 | XDB::execute('DELETE FROM search_name | |
6dbb167e | 750 | WHERE pid = {?}', |
726eaf7a SJ |
751 | $pid); |
752 | $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score, | |
753 | FIND_IN_SET('public', e.flags) AS public | |
754 | FROM profile_name AS n | |
755 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
756 | WHERE n.pid = {?}", | |
757 | $pid); | |
758 | ||
759 | foreach ($keys as $i => $key) { | |
760 | if ($key['name'] == '') { | |
761 | continue; | |
762 | } | |
763 | $toks = preg_split('/[ \'\-]+/', $key['name']); | |
764 | $token = ''; | |
765 | $first = 5; | |
766 | while ($toks) { | |
767 | $token = strtolower(replace_accent(array_pop($toks) . $token)); | |
768 | $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10); | |
6dbb167e | 769 | XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags) |
726eaf7a | 770 | VALUES ({?}, {?}, {?}, {?}, {?})', |
6dbb167e | 771 | $token, $pid, soundex_fr($token), $score, $key['public']); |
726eaf7a SJ |
772 | $first = 0; |
773 | } | |
774 | } | |
69b46857 SJ |
775 | } |
776 | ||
777 | /** The school identifier consists of 6 digits. The first 3 represent the | |
778 | * promotion entry year. The last 3 indicate the student's rank. | |
779 | * | |
780 | * Our identifier consists of 8 digits and both half have the same role. | |
781 | * This enables us to deal with bigger promotions and with a wider range | |
782 | * of promotions. | |
783 | * | |
784 | * getSchoolId returns a school identifier given one of ours. | |
785 | * getXorgId returns a X.org identifier given a school identifier. | |
786 | */ | |
787 | public static function getSchoolId($xorgId) | |
788 | { | |
789 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
790 | return null; | |
791 | } | |
792 | ||
793 | $year = intval(substr($xorgId, 0, 4)); | |
794 | $rank = intval(substr($xorgId, 5, 3)); | |
795 | if ($year < 1996) { | |
796 | return null; | |
797 | } elseif ($year < 2000) { | |
798 | $year = intval(substr(1900 - $year, 1, 3)); | |
799 | return sprintf('%02u0%03u', $year, $rank); | |
800 | } else { | |
801 | $year = intval(substr(1900 - $year, 1, 3)); | |
802 | return sprintf('%03u%03u', $year, $rank); | |
803 | } | |
804 | } | |
726eaf7a | 805 | |
69b46857 SJ |
806 | public static function getXorgId($schoolId) |
807 | { | |
94590511 | 808 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
a292484d SJ |
809 | return null; |
810 | } | |
811 | ||
69b46857 SJ |
812 | $year = intval(substr($schoolId, 0, 3)); |
813 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 814 | |
69b46857 SJ |
815 | if ($year > 200) { |
816 | $year /= 10; | |
817 | } | |
818 | if ($year < 96) { | |
819 | return null; | |
820 | } else { | |
821 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
822 | } | |
726eaf7a | 823 | } |
e7b93962 FB |
824 | } |
825 | ||
31ef12ef RB |
826 | |
827 | /** Iterator over a set of Profiles | |
828 | */ | |
829 | class ProfileIterator implements PlIterator | |
9f21bd15 RB |
830 | { |
831 | private $iterator = null; | |
832 | private $fields; | |
1a9affb7 | 833 | private $visibility; |
9f21bd15 | 834 | |
1a9affb7 | 835 | public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null) |
9f21bd15 RB |
836 | { |
837 | require_once 'profilefields.inc.php'; | |
1a9affb7 RB |
838 | |
839 | if ($visibility == null) { | |
840 | $visibility = new ProfileVisibility(); | |
841 | } | |
842 | ||
9f21bd15 | 843 | $this->fields = $fields; |
1a9affb7 | 844 | $this->visibility = $visibility; |
9f21bd15 RB |
845 | |
846 | $subits = array(); | |
847 | $callbacks = array(); | |
848 | ||
849 | $subits[0] = $it; | |
850 | $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid'); | |
851 | $cb = PlIteratorUtils::objectPropertyCallback('pid'); | |
852 | ||
853 | if ($fields & Profile::FETCH_ADDRESSES) { | |
854 | $callbacks[Profile::FETCH_ADDRESSES] = $cb; | |
855 | $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility); | |
856 | } | |
857 | ||
858 | if ($fields & Profile::FETCH_CORPS) { | |
859 | $callbacks[Profile::FETCH_CORPS] = $cb; | |
860 | $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility); | |
861 | } | |
862 | ||
863 | if ($fields & Profile::FETCH_EDU) { | |
864 | $callbacks[Profile::FETCH_EDU] = $cb; | |
865 | $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility); | |
866 | } | |
867 | ||
868 | if ($fields & Profile::FETCH_JOBS) { | |
869 | $callbacks[Profile::FETCH_JOBS] = $cb; | |
870 | $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility); | |
871 | } | |
872 | ||
873 | if ($fields & Profile::FETCH_MEDALS) { | |
874 | $callbacks[Profile::FETCH_MEDALS] = $cb; | |
875 | $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility); | |
876 | } | |
877 | ||
878 | if ($fields & Profile::FETCH_NETWORKING) { | |
879 | $callbacks[Profile::FETCH_NETWORKING] = $cb; | |
880 | $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility); | |
881 | } | |
882 | ||
883 | if ($fields & Profile::FETCH_PHONES) { | |
884 | $callbacks[Profile::FETCH_PHONES] = $cb; | |
885 | $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility); | |
886 | } | |
887 | ||
9f21bd15 RB |
888 | $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0); |
889 | } | |
890 | ||
8cf886dc | 891 | private function hasData($flag, $vals) |
9f21bd15 | 892 | { |
8cf886dc | 893 | return ($this->fields & $flag) && ($vals[$flag] != null); |
9f21bd15 RB |
894 | } |
895 | ||
896 | private function fillProfile(array $vals) | |
897 | { | |
9f21bd15 | 898 | $pf = Profile::get($vals[0]); |
1a9affb7 RB |
899 | $pf->setVisibilityLevel($this->visibility->level()); |
900 | ||
8cf886dc RB |
901 | if ($this->hasData(Profile::FETCH_PHONES, $vals)) { |
902 | $pf->setPhones($vals[Profile::FETCH_PHONES]); | |
9f21bd15 | 903 | } |
8cf886dc RB |
904 | if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) { |
905 | $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]); | |
906 | } | |
907 | if ($this->hasData(Profile::FETCH_JOBS, $vals)) { | |
908 | $pf->setJobs($vals[Profile::FETCH_JOBS]); | |
909 | } | |
910 | ||
911 | if ($this->hasData(Profile::FETCH_CORPS, $vals)) { | |
9f21bd15 RB |
912 | $pf->setCorps($vals[Profile::FETCH_CORPS]); |
913 | } | |
8cf886dc | 914 | if ($this->hasData(Profile::FETCH_EDU, $vals)) { |
26ca919b | 915 | $pf->setEducations($vals[Profile::FETCH_EDU]); |
9f21bd15 | 916 | } |
8cf886dc | 917 | if ($this->hasData(Profile::FETCH_MEDALS, $vals)) { |
9f21bd15 RB |
918 | $pf->setMedals($vals[Profile::FETCH_MEDALS]); |
919 | } | |
8cf886dc | 920 | if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) { |
9f21bd15 RB |
921 | $pf->setNetworking($vals[Profile::FETCH_NETWORKING]); |
922 | } | |
9f21bd15 RB |
923 | |
924 | return $pf; | |
925 | } | |
926 | ||
927 | public function next() | |
928 | { | |
929 | $vals = $this->iterator->next(); | |
930 | if ($vals == null) { | |
931 | return null; | |
932 | } | |
933 | return $this->fillProfile($vals); | |
934 | } | |
935 | ||
936 | public function first() | |
937 | { | |
938 | return $this->iterator->first(); | |
939 | } | |
940 | ||
941 | public function last() | |
942 | { | |
943 | return $this->iterator->last(); | |
944 | } | |
945 | ||
946 | public function total() | |
947 | { | |
948 | return $this->iterator->total(); | |
949 | } | |
950 | } | |
951 | ||
e7b93962 FB |
952 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
953 | ?> |