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