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 | ||
22 | class 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'; | |
e02d9fbb SJ |
52 | /* education related names */ |
53 | const EDU_X = 'École polytechnique'; | |
54 | const DEGREE_X = 'Ingénieur'; | |
55 | const DEGREE_M = 'Master'; | |
56 | const DEGREE_D = 'Doctorat'; | |
913a4e90 RB |
57 | |
58 | static public $name_variants = array( | |
59 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
60 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) | |
61 | ); | |
62 | ||
f5642983 FB |
63 | const ADDRESS_MAIN = 0x000001; |
64 | const ADDRESS_PERSO = 0x000002; | |
65 | const ADDRESS_PRO = 0x000004; | |
66 | const ADDRESS_ALL = 0x000006; | |
67 | const ADDRESS_POSTAL = 0x000008; | |
68 | ||
69 | const EDUCATION_MAIN = 0x000010; | |
4bc5b8f0 FB |
70 | const EDUCATION_EXTRA = 0x000020; |
71 | const EDUCATION_ALL = 0x000040; | |
72 | const EDUCATION_FINISHED = 0x000080; | |
73 | const EDUCATION_CURRENT = 0x000100; | |
f5642983 | 74 | |
4bc5b8f0 FB |
75 | const JOBS_MAIN = 0x001000; |
76 | const JOBS_ALL = 0x002000; | |
77 | const JOBS_FINISHED = 0x004000; | |
78 | const JOBS_CURRENT = 0x008000; | |
f5642983 | 79 | |
04a94b1d FB |
80 | const NETWORKING_ALL = 0x000000; |
81 | const NETWORKING_WEB = 0x010000; | |
82 | const NETWORKING_IM = 0x020000; | |
83 | const NETWORKING_SOCIAL = 0x040000; | |
84 | ||
e7ba5725 RB |
85 | const FETCH_ADDRESSES = 0x000001; |
86 | const FETCH_CORPS = 0x000002; | |
87 | const FETCH_EDU = 0x000004; | |
88 | const FETCH_JOBS = 0x000008; | |
89 | const FETCH_MEDALS = 0x000010; | |
90 | const FETCH_NETWORKING = 0x000020; | |
91 | const FETCH_PHONES = 0x000040; | |
92 | const FETCH_PHOTO = 0x000080; | |
9f21bd15 | 93 | |
330ffaa0 | 94 | const FETCH_MINIFICHES = 0x0000CD; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_PHONES | FETCH_PHOTO |
fa9994fa | 95 | |
330ffaa0 | 96 | const FETCH_ALL = 0x0000FF; // OR of FETCH_* |
6b5008ec | 97 | |
e7b93962 FB |
98 | private $pid; |
99 | private $hrpid; | |
3e53a496 FB |
100 | private $data = array(); |
101 | ||
f5642983 FB |
102 | private $visibility = null; |
103 | ||
b774ddab | 104 | private function __construct(array $data) |
e7b93962 | 105 | { |
b774ddab | 106 | $this->data = $data; |
832e6fcb FB |
107 | $this->pid = $this->data['pid']; |
108 | $this->hrpid = $this->data['hrpid']; | |
f74fb084 FB |
109 | if (!S::logged()) { |
110 | $this->setVisibilityLevel(self::VISIBILITY_PUBLIC); | |
d52b48a0 RB |
111 | } else { |
112 | $this->setVisibilityLevel(self::VISIBILITY_PRIVATE); | |
f74fb084 | 113 | } |
e7b93962 FB |
114 | } |
115 | ||
9f21bd15 RB |
116 | static private $contexts = array(); |
117 | ||
118 | /** Returns the best visibility context toward $visibility | |
119 | * @param $visibility A wished visibility level | |
abb8de12 | 120 | * @return An array of compatible visibilities |
9f21bd15 RB |
121 | * |
122 | * if $visibility is null, the best visibility is returned | |
123 | */ | |
124 | static public function getVisibilityContext($visibility = null) | |
125 | { | |
126 | if (array_key_exists($visibility, self::$contexts)) { | |
127 | return self::$contexts[$visibility]; | |
128 | } | |
129 | ||
130 | $asked_vis = $visibility; | |
131 | ||
132 | if (S::logged()) { | |
133 | $minvis = self::VISIBILITY_PRIVATE; | |
134 | } else { | |
135 | $minvis = self::VISIBILITY_PUBLIC; | |
136 | } | |
137 | if ($visibility == null) { | |
545ea3a5 | 138 | $visibility = $minvis; |
9f21bd15 RB |
139 | } |
140 | ||
141 | if ($minvis == self::VISIBILITY_PUBLIC) { | |
545ea3a5 | 142 | $visibility = self::VISIBILITY_PUBLIC; |
9f21bd15 RB |
143 | } |
144 | ||
545ea3a5 | 145 | $visibility = self::$v_values[$visibility]; |
9f21bd15 RB |
146 | self::$contexts[$asked_vis] = $visibility; |
147 | ||
148 | return $visibility; | |
149 | } | |
150 | ||
e7b93962 FB |
151 | public function id() |
152 | { | |
153 | return $this->pid; | |
154 | } | |
155 | ||
156 | public function hrid() | |
157 | { | |
158 | return $this->hrpid; | |
159 | } | |
160 | ||
d5e60905 FB |
161 | public function promo() |
162 | { | |
163 | return $this->promo; | |
164 | } | |
165 | ||
845f0a4d SJ |
166 | public function yearpromo() |
167 | { | |
168 | return intval(substr($this->promo, 1, 4)); | |
169 | } | |
170 | ||
94b72319 FB |
171 | /** Print a name with the given formatting: |
172 | * %s = • for women | |
173 | * %f = firstname | |
174 | * %l = lastname | |
175 | * %F = fullname | |
176 | * %S = shortname | |
177 | * %p = promo | |
178 | */ | |
179 | public function name($format) | |
180 | { | |
181 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
182 | array($this->isFemale() ? '•' : '', | |
faf75faf SJ |
183 | $this->firstName(), $this->lastName(), |
184 | $this->fullName(), $this->shortName(), | |
185 | $this->promo()), $format); | |
94b72319 FB |
186 | } |
187 | ||
188 | public function fullName($with_promo = false) | |
189 | { | |
190 | if ($with_promo) { | |
191 | return $this->full_name . ' (' . $this->promo . ')'; | |
192 | } | |
193 | return $this->full_name; | |
194 | } | |
195 | ||
196 | public function shortName($with_promo = false) | |
197 | { | |
198 | if ($with_promo) { | |
199 | return $this->short_name . ' (' . $this->promo . ')'; | |
200 | } | |
201 | return $this->short_name; | |
202 | } | |
203 | ||
204 | public function firstName() | |
205 | { | |
08c91036 | 206 | return $this->firstname; |
94b72319 FB |
207 | } |
208 | ||
5c005b37 RB |
209 | public function firstNames() |
210 | { | |
211 | return $this->nameVariants(self::FIRSTNAME); | |
212 | } | |
213 | ||
94b72319 FB |
214 | public function lastName() |
215 | { | |
08c91036 | 216 | return $this->lastname; |
94b72319 FB |
217 | } |
218 | ||
5c005b37 RB |
219 | public function lastNames() |
220 | { | |
221 | return $this->nameVariants(self::LASTNAME); | |
222 | } | |
223 | ||
94b72319 FB |
224 | public function isFemale() |
225 | { | |
226 | return $this->sex == PlUser::GENDER_FEMALE; | |
227 | } | |
228 | ||
400ac338 RB |
229 | public function isDead() |
230 | { | |
231 | return ($this->deathdate != null); | |
232 | } | |
233 | ||
94b72319 FB |
234 | public function data() |
235 | { | |
236 | $this->first_name; | |
237 | return $this->data; | |
238 | } | |
239 | ||
5c005b37 RB |
240 | private function nameVariants($type) |
241 | { | |
242 | $vals = array($this->$type); | |
243 | foreach (self::$name_variants[$type] as $var) { | |
244 | $vartype = $type . '_' . $var; | |
245 | $varname = $this->$vartype; | |
246 | if ($varname != null && $varname != "") { | |
247 | $vals[] = $varname; | |
248 | } | |
249 | } | |
250 | return array_unique($vals); | |
251 | } | |
252 | ||
b6569a95 FB |
253 | public function nationalities() |
254 | { | |
255 | $nats = array(); | |
256 | if ($this->nationality1) { | |
257 | $nats[] = $this->nationality1; | |
258 | } | |
259 | if ($this->nationality2) { | |
260 | $nats[] = $this->nationality2; | |
261 | } | |
262 | if ($this->nationality3) { | |
263 | $nats[] = $this->nationality3; | |
264 | } | |
265 | return $nats; | |
266 | } | |
267 | ||
3e53a496 FB |
268 | public function __get($name) |
269 | { | |
270 | if (property_exists($this, $name)) { | |
271 | return $this->$name; | |
272 | } | |
273 | ||
3e53a496 FB |
274 | if (isset($this->data[$name])) { |
275 | return $this->data[$name]; | |
276 | } | |
277 | ||
278 | return null; | |
279 | } | |
280 | ||
281 | public function __isset($name) | |
282 | { | |
283 | return property_exists($this, $name) || isset($this->data[$name]); | |
284 | } | |
285 | ||
c159e50f RB |
286 | /** Sets the level of visibility of the profile |
287 | * Sets $this->visibility to a list of valid visibilities. | |
288 | * @param one of the self::VISIBILITY_* values | |
289 | */ | |
f5642983 FB |
290 | public function setVisibilityLevel($visibility) |
291 | { | |
0d906109 RB |
292 | if ($visibility != self::VISIBILITY_PRIVATE |
293 | && $visibility != self::VISIBILITY_AX | |
f5642983 FB |
294 | && $visibility != self::VISIBILITY_PUBLIC) { |
295 | Platal::page()->kill("Visibility invalide: " . $visibility); | |
296 | } | |
297 | $this->visibility = self::$v_values[$visibility]; | |
5c005b37 | 298 | if ($this->mobile && !in_array($this->mobile_pub, $this->visibility)) { |
46e9eb99 FB |
299 | unset($this->data['mobile']); |
300 | } | |
f5642983 FB |
301 | } |
302 | ||
c159e50f RB |
303 | /** Determine whether an item with visibility $visibility can be displayed |
304 | * with the current level of visibility of the profile | |
305 | * @param $visibility The level of visibility to be checked | |
306 | */ | |
307 | public function isVisible($visibility) | |
308 | { | |
309 | return in_array($visibility, $this->visibility); | |
310 | } | |
4bc5b8f0 | 311 | |
9f21bd15 RB |
312 | public static function getCompatibleVisibilities($visibility) |
313 | { | |
314 | return self::$v_values[$visibility]; | |
315 | } | |
316 | ||
990cb17b RB |
317 | private function getProfileField($cls) |
318 | { | |
319 | return ProfileField::getForPID($cls, $this->id(), $this->visibility); | |
320 | } | |
321 | ||
8cf886dc RB |
322 | /** Consolidates internal data (addresses, phones, jobs) |
323 | */ | |
324 | private function consolidateFields() | |
325 | { | |
326 | if ($this->phones != null) { | |
327 | if ($this->addresses != null) { | |
328 | $this->addresses->addPhones($this->phones); | |
329 | } | |
330 | ||
331 | if ($this->jobs != null) { | |
332 | $this->jobs->addPhones($this->phones); | |
333 | } | |
334 | } | |
335 | ||
336 | if ($this->addresses != null && $this->jobs != null) { | |
337 | $this->jobs->addAddresses($this->addresses); | |
338 | } | |
339 | } | |
340 | ||
833a6e86 FB |
341 | /* Photo |
342 | */ | |
9f21bd15 RB |
343 | private $photo = null; |
344 | public function setPhoto(ProfilePhoto $photo) | |
345 | { | |
346 | $this->photo = $photo; | |
347 | } | |
348 | ||
833a6e86 FB |
349 | public function getPhoto($fallback = true) |
350 | { | |
990cb17b RB |
351 | if ($this->photo == null) { |
352 | $this->setPhoto($this->getProfileField('ProfilePhoto')); | |
353 | } | |
354 | ||
9f21bd15 RB |
355 | if ($this->photo != null) { |
356 | return $this->photo->pic; | |
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 | ||
9f21bd15 | 593 | |
97b71de5 | 594 | $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, pse.text AS section, |
9f21bd15 RB |
595 | pn_f.name AS firstname, pn_l.name AS lastname, pn_n.name AS nickname, |
596 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, | |
597 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, | |
598 | pd.promo AS promo, pd.short_name, pd.directory_name AS full_name, | |
599 | pd.directory_name, pp.display_tel AS mobile, pp.pub AS mobile_pub, | |
600 | ph.attach IS NOT NULL AS has_photo, ph.pub AS photo_pub, | |
601 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, | |
bdef0d33 | 602 | pm.expertise AS mentor_expertise, |
9f21bd15 RB |
603 | ap.uid AS owner_id |
604 | FROM profiles AS p | |
605 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
606 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
97b71de5 | 607 | LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section) |
9f21bd15 RB |
608 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid |
609 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
610 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
611 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
612 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
613 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
614 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
615 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
616 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid | |
617 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
618 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') | |
619 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) | |
bdef0d33 | 620 | LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid) |
9f21bd15 RB |
621 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
622 | WHERE p.pid IN ' . XDB::formatArray($pids) . ' | |
623 | GROUP BY p.pid | |
624 | ' . $order); | |
31ef12ef | 625 | return new ProfileIterator($it, $pids, $fields, $visibility); |
b774ddab FB |
626 | } |
627 | ||
628 | public static function getPID($login) | |
629 | { | |
630 | if ($login instanceof PlUser) { | |
631 | return XDB::fetchOneCell('SELECT pid | |
632 | FROM account_profiles | |
633 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
634 | $login->id()); | |
9f21bd15 | 635 | } else if (ctype_digit($login)) { |
b774ddab FB |
636 | return XDB::fetchOneCell('SELECT pid |
637 | FROM profiles | |
638 | WHERE pid = {?}', $login); | |
639 | } else { | |
640 | return XDB::fetchOneCell('SELECT pid | |
641 | FROM profiles | |
642 | WHERE hrpid = {?}', $login); | |
643 | } | |
644 | } | |
645 | ||
0d906109 RB |
646 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
647 | { | |
648 | if ($respect_order) { | |
649 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
650 | } else { | |
651 | $order = ''; | |
652 | } | |
653 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
654 | FROM account_profiles AS ap | |
655 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
9f21bd15 RB |
656 | AND ap.uid IN ' . XDB::formatArray($uids) .' |
657 | ' . $order); | |
0d906109 | 658 | } |
b774ddab | 659 | |
e7b93962 FB |
660 | /** Return the profile associated with the given login. |
661 | */ | |
9f21bd15 | 662 | public static function get($login, $fields = 0x0000, $visibility = null) |
a3118782 | 663 | { |
641f2115 FB |
664 | if (is_array($login)) { |
665 | return new Profile($login); | |
666 | } | |
b774ddab FB |
667 | $pid = self::getPID($login); |
668 | if (!is_null($pid)) { | |
9f21bd15 | 669 | $it = self::iterOverPIDs(array($pid), false, $fields, $visibility); |
0d906109 | 670 | return $it->next(); |
b774ddab | 671 | } else { |
efe597c5 FB |
672 | /* Let say we can identify a profile using the identifiers of its owner. |
673 | */ | |
455ea0c9 FB |
674 | if (!($login instanceof PlUser)) { |
675 | $user = User::getSilent($login); | |
676 | if ($user && $user->hasProfile()) { | |
677 | return $user->profile(); | |
678 | } | |
efe597c5 | 679 | } |
3e53a496 | 680 | return null; |
e7b93962 FB |
681 | } |
682 | } | |
a3118782 | 683 | |
9f21bd15 | 684 | public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 685 | { |
9f21bd15 | 686 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility); |
0d906109 RB |
687 | } |
688 | ||
9f21bd15 | 689 | public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 690 | { |
9f21bd15 | 691 | return self::fetchProfileData($pids, $respect_order, $fields, $visibility); |
0d906109 RB |
692 | } |
693 | ||
b774ddab FB |
694 | /** Return profiles for the list of pids. |
695 | */ | |
7d0ebdf5 | 696 | public static function getBulkProfilesWithPIDs(array $pids, $fields = self::FETCH_ADDRESSES, $visibility = null) |
b774ddab FB |
697 | { |
698 | if (count($pids) == 0) { | |
699 | return array(); | |
700 | } | |
9f21bd15 | 701 | $it = self::iterOverPIDs($pids, true, $fields, $visibility); |
b774ddab | 702 | $profiles = array(); |
0d906109 RB |
703 | while ($p = $it->next()) { |
704 | $profiles[$p->id()] = $p; | |
b774ddab FB |
705 | } |
706 | return $profiles; | |
707 | } | |
708 | ||
709 | /** Return profiles for uids. | |
710 | */ | |
9f21bd15 | 711 | public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null) |
b774ddab FB |
712 | { |
713 | if (count($uids) == 0) { | |
714 | return array(); | |
715 | } | |
9f21bd15 | 716 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility); |
b774ddab FB |
717 | } |
718 | ||
913a4e90 RB |
719 | public static function isDisplayName($name) |
720 | { | |
721 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
722 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
723 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
724 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
725 | } | |
726 | ||
9f21bd15 RB |
727 | public static function getNameTypeId($type, $for_sql = false) |
728 | { | |
729 | if (!S::has('name_types')) { | |
730 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type | |
731 | FROM profile_name_enum'); | |
732 | S::set('name_types', $table); | |
733 | } else { | |
734 | $table = S::v('name_types'); | |
735 | } | |
736 | if ($for_sql) { | |
737 | return XDB::escape($table[$type]); | |
738 | } else { | |
739 | return $table[$type]; | |
740 | } | |
741 | } | |
742 | ||
726eaf7a SJ |
743 | public static function rebuildSearchTokens($pid) |
744 | { | |
745 | XDB::execute('DELETE FROM search_name | |
6dbb167e | 746 | WHERE pid = {?}', |
726eaf7a SJ |
747 | $pid); |
748 | $keys = XDB::iterator("SELECT CONCAT(n.particle, n.name) AS name, e.score, | |
749 | FIND_IN_SET('public', e.flags) AS public | |
750 | FROM profile_name AS n | |
751 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
752 | WHERE n.pid = {?}", | |
753 | $pid); | |
754 | ||
755 | foreach ($keys as $i => $key) { | |
756 | if ($key['name'] == '') { | |
757 | continue; | |
758 | } | |
759 | $toks = preg_split('/[ \'\-]+/', $key['name']); | |
760 | $token = ''; | |
761 | $first = 5; | |
762 | while ($toks) { | |
763 | $token = strtolower(replace_accent(array_pop($toks) . $token)); | |
764 | $score = ($toks ? 0 : 10 + $first) * ($key['score'] / 10); | |
6dbb167e | 765 | XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags) |
726eaf7a | 766 | VALUES ({?}, {?}, {?}, {?}, {?})', |
6dbb167e | 767 | $token, $pid, soundex_fr($token), $score, $key['public']); |
726eaf7a SJ |
768 | $first = 0; |
769 | } | |
770 | } | |
69b46857 SJ |
771 | } |
772 | ||
773 | /** The school identifier consists of 6 digits. The first 3 represent the | |
774 | * promotion entry year. The last 3 indicate the student's rank. | |
775 | * | |
776 | * Our identifier consists of 8 digits and both half have the same role. | |
777 | * This enables us to deal with bigger promotions and with a wider range | |
778 | * of promotions. | |
779 | * | |
780 | * getSchoolId returns a school identifier given one of ours. | |
781 | * getXorgId returns a X.org identifier given a school identifier. | |
782 | */ | |
783 | public static function getSchoolId($xorgId) | |
784 | { | |
785 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
786 | return null; | |
787 | } | |
788 | ||
789 | $year = intval(substr($xorgId, 0, 4)); | |
790 | $rank = intval(substr($xorgId, 5, 3)); | |
791 | if ($year < 1996) { | |
792 | return null; | |
793 | } elseif ($year < 2000) { | |
794 | $year = intval(substr(1900 - $year, 1, 3)); | |
795 | return sprintf('%02u0%03u', $year, $rank); | |
796 | } else { | |
797 | $year = intval(substr(1900 - $year, 1, 3)); | |
798 | return sprintf('%03u%03u', $year, $rank); | |
799 | } | |
800 | } | |
726eaf7a | 801 | |
69b46857 SJ |
802 | public static function getXorgId($schoolId) |
803 | { | |
94590511 | 804 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
a292484d SJ |
805 | return null; |
806 | } | |
807 | ||
69b46857 SJ |
808 | $year = intval(substr($schoolId, 0, 3)); |
809 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 810 | |
69b46857 SJ |
811 | if ($year > 200) { |
812 | $year /= 10; | |
813 | } | |
814 | if ($year < 96) { | |
815 | return null; | |
816 | } else { | |
817 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
818 | } | |
726eaf7a | 819 | } |
e7b93962 FB |
820 | } |
821 | ||
31ef12ef RB |
822 | |
823 | /** Iterator over a set of Profiles | |
824 | */ | |
825 | class ProfileIterator implements PlIterator | |
9f21bd15 RB |
826 | { |
827 | private $iterator = null; | |
828 | private $fields; | |
829 | ||
830 | public function __construct(PlIterator $it, array $pids, $fields = 0x0000, $visibility = null) | |
831 | { | |
832 | require_once 'profilefields.inc.php'; | |
833 | $visibility = Profile::getVisibilityContext($visibility); | |
834 | $this->fields = $fields; | |
835 | ||
836 | $subits = array(); | |
837 | $callbacks = array(); | |
838 | ||
839 | $subits[0] = $it; | |
840 | $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid'); | |
841 | $cb = PlIteratorUtils::objectPropertyCallback('pid'); | |
842 | ||
843 | if ($fields & Profile::FETCH_ADDRESSES) { | |
844 | $callbacks[Profile::FETCH_ADDRESSES] = $cb; | |
845 | $subits[Profile::FETCH_ADDRESSES] = new ProfileFieldIterator('ProfileAddresses', $pids, $visibility); | |
846 | } | |
847 | ||
848 | if ($fields & Profile::FETCH_CORPS) { | |
849 | $callbacks[Profile::FETCH_CORPS] = $cb; | |
850 | $subits[Profile::FETCH_CORPS] = new ProfileFieldIterator('ProfileCorps', $pids, $visibility); | |
851 | } | |
852 | ||
853 | if ($fields & Profile::FETCH_EDU) { | |
854 | $callbacks[Profile::FETCH_EDU] = $cb; | |
855 | $subits[Profile::FETCH_EDU] = new ProfileFieldIterator('ProfileEducation', $pids, $visibility); | |
856 | } | |
857 | ||
858 | if ($fields & Profile::FETCH_JOBS) { | |
859 | $callbacks[Profile::FETCH_JOBS] = $cb; | |
860 | $subits[Profile::FETCH_JOBS] = new ProfileFieldIterator('ProfileJobs', $pids, $visibility); | |
861 | } | |
862 | ||
863 | if ($fields & Profile::FETCH_MEDALS) { | |
864 | $callbacks[Profile::FETCH_MEDALS] = $cb; | |
865 | $subits[Profile::FETCH_MEDALS] = new ProfileFieldIterator('ProfileMedals', $pids, $visibility); | |
866 | } | |
867 | ||
868 | if ($fields & Profile::FETCH_NETWORKING) { | |
869 | $callbacks[Profile::FETCH_NETWORKING] = $cb; | |
870 | $subits[Profile::FETCH_NETWORKING] = new ProfileFieldIterator('ProfileNetworking', $pids, $visibility); | |
871 | } | |
872 | ||
873 | if ($fields & Profile::FETCH_PHONES) { | |
874 | $callbacks[Profile::FETCH_PHONES] = $cb; | |
875 | $subits[Profile::FETCH_PHONES] = new ProfileFieldIterator('ProfilePhones', $pids, $visibility); | |
876 | } | |
877 | ||
878 | if ($fields & Profile::FETCH_PHOTO) { | |
879 | $callbacks[Profile::FETCH_PHOTO] = $cb; | |
880 | $subits[Profile::FETCH_PHOTO] = new ProfileFieldIterator('ProfilePhoto', $pids, $visibility); | |
881 | } | |
882 | ||
883 | $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0); | |
884 | } | |
885 | ||
8cf886dc | 886 | private function hasData($flag, $vals) |
9f21bd15 | 887 | { |
8cf886dc | 888 | return ($this->fields & $flag) && ($vals[$flag] != null); |
9f21bd15 RB |
889 | } |
890 | ||
891 | private function fillProfile(array $vals) | |
892 | { | |
9f21bd15 | 893 | $pf = Profile::get($vals[0]); |
8cf886dc RB |
894 | if ($this->hasData(Profile::FETCH_PHONES, $vals)) { |
895 | $pf->setPhones($vals[Profile::FETCH_PHONES]); | |
9f21bd15 | 896 | } |
8cf886dc RB |
897 | if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) { |
898 | $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]); | |
899 | } | |
900 | if ($this->hasData(Profile::FETCH_JOBS, $vals)) { | |
901 | $pf->setJobs($vals[Profile::FETCH_JOBS]); | |
902 | } | |
903 | ||
904 | if ($this->hasData(Profile::FETCH_CORPS, $vals)) { | |
9f21bd15 RB |
905 | $pf->setCorps($vals[Profile::FETCH_CORPS]); |
906 | } | |
8cf886dc | 907 | if ($this->hasData(Profile::FETCH_EDU, $vals)) { |
26ca919b | 908 | $pf->setEducations($vals[Profile::FETCH_EDU]); |
9f21bd15 | 909 | } |
8cf886dc | 910 | if ($this->hasData(Profile::FETCH_MEDALS, $vals)) { |
9f21bd15 RB |
911 | $pf->setMedals($vals[Profile::FETCH_MEDALS]); |
912 | } | |
8cf886dc | 913 | if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) { |
9f21bd15 RB |
914 | $pf->setNetworking($vals[Profile::FETCH_NETWORKING]); |
915 | } | |
8cf886dc RB |
916 | if ($this->hasData(Profile::FETCH_PHOTO, $vals)) { |
917 | $pf->setPhoto($vals[Profile::FETCH_PHOTO]); | |
9f21bd15 RB |
918 | } |
919 | ||
920 | return $pf; | |
921 | } | |
922 | ||
923 | public function next() | |
924 | { | |
925 | $vals = $this->iterator->next(); | |
926 | if ($vals == null) { | |
927 | return null; | |
928 | } | |
929 | return $this->fillProfile($vals); | |
930 | } | |
931 | ||
932 | public function first() | |
933 | { | |
934 | return $this->iterator->first(); | |
935 | } | |
936 | ||
937 | public function last() | |
938 | { | |
939 | return $this->iterator->last(); | |
940 | } | |
941 | ||
942 | public function total() | |
943 | { | |
944 | return $this->iterator->total(); | |
945 | } | |
946 | } | |
947 | ||
e7b93962 FB |
948 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
949 | ?> |