Commit | Line | Data |
---|---|---|
e7b93962 FB |
1 | <?php |
2 | /*************************************************************************** | |
5e1513f6 | 3 | * Copyright (C) 2003-2011 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 | ||
aaffb7b2 | 22 | class Profile implements PlExportable |
1a9affb7 | 23 | { |
f5642983 | 24 | |
913a4e90 RB |
25 | /* name tokens */ |
26 | const LASTNAME = 'lastname'; | |
27 | const FIRSTNAME = 'firstname'; | |
28 | const NICKNAME = 'nickname'; | |
29 | const PSEUDONYM = 'pseudonym'; | |
30 | const NAME = 'name'; | |
31 | /* name variants */ | |
32 | const VN_MARITAL = 'marital'; | |
33 | const VN_ORDINARY = 'ordinary'; | |
34 | const VN_OTHER = 'other'; | |
35 | const VN_INI = 'ini'; | |
36 | /* display names */ | |
37 | const DN_FULL = 'directory_name'; | |
38 | const DN_DISPLAY = 'yourself'; | |
39 | const DN_YOURSELF = 'yourself'; | |
40 | const DN_DIRECTORY = 'directory_name'; | |
41 | const DN_PRIVATE = 'private_name'; | |
42 | const DN_PUBLIC = 'public_name'; | |
43 | const DN_SHORT = 'short_name'; | |
44 | const DN_SORT = 'sort_name'; | |
e02d9fbb SJ |
45 | /* education related names */ |
46 | const EDU_X = 'École polytechnique'; | |
47 | const DEGREE_X = 'Ingénieur'; | |
48 | const DEGREE_M = 'Master'; | |
49 | const DEGREE_D = 'Doctorat'; | |
913a4e90 | 50 | |
9fd2ca04 SJ |
51 | static public $cycles = array( |
52 | self::DEGREE_X => 'polytechnicien', | |
53 | self::DEGREE_M => 'master', | |
54 | self::DEGREE_D => 'docteur' | |
55 | ); | |
e4860774 SJ |
56 | static public $cycle_prefixes = array( |
57 | self::DEGREE_X => 'X', | |
58 | self::DEGREE_M => 'M', | |
59 | self::DEGREE_D => 'D' | |
60 | ); | |
9fd2ca04 | 61 | |
913a4e90 RB |
62 | static public $name_variants = array( |
63 | self::LASTNAME => array(self::VN_MARITAL, self::VN_ORDINARY), | |
64 | self::FIRSTNAME => array(self::VN_ORDINARY, self::VN_INI, self::VN_OTHER) | |
65 | ); | |
66 | ||
3c985c08 RB |
67 | const ADDRESS_MAIN = 0x00000001; |
68 | const ADDRESS_PERSO = 0x00000002; | |
69 | const ADDRESS_PRO = 0x00000004; | |
70 | const ADDRESS_ALL = 0x00000006; | |
71 | const ADDRESS_POSTAL = 0x00000008; | |
72 | ||
73 | const EDUCATION_MAIN = 0x00000010; | |
74 | const EDUCATION_EXTRA = 0x00000020; | |
75 | const EDUCATION_ALL = 0x00000040; | |
76 | const EDUCATION_FINISHED = 0x00000080; | |
77 | const EDUCATION_CURRENT = 0x00000100; | |
78 | ||
79 | const JOBS_MAIN = 0x00001000; | |
80 | const JOBS_ALL = 0x00002000; | |
81 | const JOBS_FINISHED = 0x00004000; | |
82 | const JOBS_CURRENT = 0x00008000; | |
83 | ||
34de4b20 | 84 | const NETWORKING_ALL = 0x00070000; |
3c985c08 RB |
85 | const NETWORKING_WEB = 0x00010000; |
86 | const NETWORKING_IM = 0x00020000; | |
87 | const NETWORKING_SOCIAL = 0x00040000; | |
88 | ||
89 | const PHONE_LINK_JOB = 0x00100000; | |
90 | const PHONE_LINK_ADDRESS = 0x00200000; | |
91 | const PHONE_LINK_PROFILE = 0x00400000; | |
92 | const PHONE_LINK_COMPANY = 0x00800000; | |
93 | const PHONE_LINK_ANY = 0x00F00000; | |
94 | ||
95 | const PHONE_TYPE_FAX = 0x01000000; | |
96 | const PHONE_TYPE_FIXED = 0x02000000; | |
97 | const PHONE_TYPE_MOBILE = 0x04000000; | |
98 | const PHONE_TYPE_ANY = 0x07000000; | |
99 | ||
100 | const PHONE_ANY = 0x07F00000; | |
04a94b1d | 101 | |
4d1f0f6b RB |
102 | const FETCH_ADDRESSES = 0x000001; |
103 | const FETCH_CORPS = 0x000002; | |
104 | const FETCH_EDU = 0x000004; | |
105 | const FETCH_JOBS = 0x000008; | |
106 | const FETCH_MEDALS = 0x000010; | |
107 | const FETCH_NETWORKING = 0x000020; | |
4d1f0f6b RB |
108 | const FETCH_MENTOR_COUNTRY = 0x000080; |
109 | const FETCH_PHONES = 0x000100; | |
3ac45f10 PC |
110 | const FETCH_JOB_TERMS = 0x000200; |
111 | const FETCH_MENTOR_TERMS = 0x000400; | |
9f21bd15 | 112 | |
4d1f0f6b | 113 | const FETCH_MINIFICHES = 0x00012D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES |
fa9994fa | 114 | |
3ac45f10 | 115 | const FETCH_ALL = 0x0007FF; // OR of FETCH_* |
6b5008ec | 116 | |
7a9680aa SJ |
117 | static public $descriptions = array( |
118 | 'search_names' => 'Noms', | |
119 | 'nationality1' => 'Nationalité', | |
120 | 'nationality2' => '2e nationalité', | |
121 | 'nationality3' => '3e nationalité', | |
122 | 'promo_display' => 'Promotion affichée', | |
123 | 'email_directory' => 'Email annuaire papier', | |
124 | 'networking' => 'Messageries…', | |
125 | 'tels' => 'Téléphones', | |
126 | 'edus' => 'Formations', | |
d7de04af | 127 | 'main_edus' => 'Formations à l\'X', |
7a9680aa SJ |
128 | 'promo' => 'Promotion de sortie', |
129 | 'birthdate' => 'Date de naissance', | |
130 | 'yourself' => 'Nom affiché', | |
131 | 'freetext' => 'Commentaire', | |
132 | 'freetext_pub' => 'Affichage du commentaire', | |
133 | 'photo' => 'Photographie', | |
134 | 'photo_pub' => 'Affichage de la photographie', | |
135 | 'addresses' => 'Adresses', | |
136 | 'corps' => 'Corps', | |
137 | 'cv' => 'CV', | |
138 | 'jobs' => 'Emplois', | |
139 | 'section' => 'Section', | |
140 | 'binets' => 'Binets', | |
141 | 'medals' => 'Décorations', | |
142 | 'medals_pub' => 'Affichage des décorations', | |
143 | 'competences' => 'Compétences', | |
144 | 'langues' => 'Langues', | |
145 | 'expertise' => 'Expertises (mentoring)', | |
146 | 'terms' => 'Compétences (mentoring)', | |
147 | 'countries' => 'Pays (mentoring)' | |
148 | ); | |
149 | ||
1f8250e4 RB |
150 | private $fetched_fields = 0x000000; |
151 | ||
e7b93962 FB |
152 | private $pid; |
153 | private $hrpid; | |
564e2b2a RB |
154 | private $owner; |
155 | private $owner_fetched = false; | |
3e53a496 FB |
156 | private $data = array(); |
157 | ||
f5642983 FB |
158 | private $visibility = null; |
159 | ||
1f8250e4 | 160 | |
b774ddab | 161 | private function __construct(array $data) |
e7b93962 | 162 | { |
b774ddab | 163 | $this->data = $data; |
832e6fcb FB |
164 | $this->pid = $this->data['pid']; |
165 | $this->hrpid = $this->data['hrpid']; | |
1a9affb7 | 166 | $this->visibility = new ProfileVisibility(); |
9f21bd15 RB |
167 | } |
168 | ||
e7b93962 FB |
169 | public function id() |
170 | { | |
171 | return $this->pid; | |
172 | } | |
173 | ||
174 | public function hrid() | |
175 | { | |
176 | return $this->hrpid; | |
177 | } | |
178 | ||
564e2b2a RB |
179 | public function owner() |
180 | { | |
181 | if ($this->owner == null && !$this->owner_fetched) { | |
182 | $this->owner_fetched = true; | |
183 | $this->owner = User::getSilent($this); | |
184 | } | |
185 | return $this->owner; | |
186 | } | |
187 | ||
280806d9 SJ |
188 | public function isActive() |
189 | { | |
190 | if ($this->owner()) { | |
191 | return $this->owner->isActive(); | |
192 | } | |
193 | return false; | |
194 | } | |
195 | ||
d718e91c | 196 | public function promo($details = false) |
d5e60905 | 197 | { |
d718e91c SJ |
198 | if ($details && ($this->program || $this->fieldid)) { |
199 | $text = array(); | |
200 | if ($this->program) { | |
201 | $text[] = $this->program; | |
202 | } | |
203 | if ($this->fieldid) { | |
204 | $fieldsList = DirEnum::getOptions(DirEnum::EDUFIELDS); | |
205 | $text[] = $fieldsList[$this->fieldid]; | |
206 | } | |
207 | return $this->promo . ' (' . implode(', ', $text) . ')'; | |
208 | } | |
209 | ||
d5e60905 FB |
210 | return $this->promo; |
211 | } | |
212 | ||
845f0a4d SJ |
213 | public function yearpromo() |
214 | { | |
7733ade1 | 215 | return $this->promo_year; |
845f0a4d SJ |
216 | } |
217 | ||
1168306a PC |
218 | /** Check if user is an orange (associated with several promos) |
219 | */ | |
220 | public function isMultiPromo() | |
221 | { | |
222 | return $this->grad_year != $this->entry_year + $this->mainEducationDuration(); | |
223 | } | |
224 | ||
225 | /** Returns an array with all associated promo years. | |
226 | */ | |
227 | public function yearspromo() | |
228 | { | |
229 | $promos = array(); | |
230 | $d = -$this->deltaPromoToGradYear(); | |
231 | for ($g = $this->entry_year + $this->mainEducationDuration(); $g <= $this->grad_year; ++$g) { | |
232 | $promos[] = $g + $d; | |
233 | } | |
234 | return $promos; | |
235 | } | |
236 | ||
963bc7fc SJ |
237 | public function mainEducation() |
238 | { | |
39e2c58c FB |
239 | if (empty($this->promo)) { |
240 | return null; | |
241 | } else { | |
242 | return $this->promo{0}; | |
243 | } | |
963bc7fc SJ |
244 | } |
245 | ||
02d9dff0 FB |
246 | public function mainGrade() |
247 | { | |
248 | switch ($this->mainEducation()) { | |
249 | case 'X': | |
250 | return UserFilter::GRADE_ING; | |
251 | case 'M': | |
252 | return UserFilter::GRADE_MST; | |
253 | case 'D': | |
254 | return UserFilter::GRADE_PHD; | |
255 | default: | |
256 | return null; | |
257 | } | |
258 | } | |
259 | ||
963bc7fc SJ |
260 | public function mainEducationDuration() |
261 | { | |
262 | switch ($this->mainEducation()) { | |
263 | case 'X': | |
264 | return 3; | |
265 | case 'M': | |
266 | return 2; | |
267 | case 'D': | |
268 | return 3; | |
269 | default: | |
270 | return 0; | |
271 | } | |
272 | } | |
273 | ||
1168306a PC |
274 | /** Number of years between the promotion year until the |
275 | * graduation year. In standard schools it's 0, but for | |
276 | * Polytechnique the promo year is the entry year. | |
277 | */ | |
278 | public function deltaPromoToGradYear() | |
279 | { | |
280 | if ($this->mainEducation() == 'X') { | |
281 | return $this->mainEducationDuration(); | |
282 | } | |
283 | return 0; | |
284 | } | |
285 | ||
3ac6d61d SJ |
286 | // Returns the profile's color. |
287 | public function promoColor() | |
288 | { | |
289 | switch ($this->mainEducation()) { | |
290 | case 'X': | |
291 | if (($this->yearpromo() % 2) === 0) { | |
292 | return 'red'; | |
293 | } else { | |
294 | return 'yellow'; | |
295 | } | |
296 | case 'M': | |
297 | return 'green'; | |
298 | case 'D': | |
299 | return 'blue'; | |
300 | default: | |
301 | return 'gray'; | |
302 | } | |
303 | } | |
304 | ||
94b72319 FB |
305 | /** Print a name with the given formatting: |
306 | * %s = • for women | |
307 | * %f = firstname | |
308 | * %l = lastname | |
309 | * %F = fullname | |
310 | * %S = shortname | |
311 | * %p = promo | |
312 | */ | |
313 | public function name($format) | |
314 | { | |
315 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
316 | array($this->isFemale() ? '•' : '', | |
faf75faf SJ |
317 | $this->firstName(), $this->lastName(), |
318 | $this->fullName(), $this->shortName(), | |
319 | $this->promo()), $format); | |
94b72319 FB |
320 | } |
321 | ||
322 | public function fullName($with_promo = false) | |
323 | { | |
324 | if ($with_promo) { | |
325 | return $this->full_name . ' (' . $this->promo . ')'; | |
326 | } | |
327 | return $this->full_name; | |
328 | } | |
329 | ||
330 | public function shortName($with_promo = false) | |
331 | { | |
332 | if ($with_promo) { | |
333 | return $this->short_name . ' (' . $this->promo . ')'; | |
334 | } | |
335 | return $this->short_name; | |
336 | } | |
337 | ||
338 | public function firstName() | |
339 | { | |
08c91036 | 340 | return $this->firstname; |
94b72319 FB |
341 | } |
342 | ||
5c005b37 RB |
343 | public function firstNames() |
344 | { | |
345 | return $this->nameVariants(self::FIRSTNAME); | |
346 | } | |
347 | ||
94b72319 FB |
348 | public function lastName() |
349 | { | |
08c91036 | 350 | return $this->lastname; |
94b72319 FB |
351 | } |
352 | ||
5c005b37 RB |
353 | public function lastNames() |
354 | { | |
355 | return $this->nameVariants(self::LASTNAME); | |
356 | } | |
357 | ||
94b72319 FB |
358 | public function isFemale() |
359 | { | |
360 | return $this->sex == PlUser::GENDER_FEMALE; | |
361 | } | |
362 | ||
400ac338 RB |
363 | public function isDead() |
364 | { | |
365 | return ($this->deathdate != null); | |
366 | } | |
367 | ||
564e2b2a RB |
368 | public function displayEmail() |
369 | { | |
370 | $o = $this->owner(); | |
371 | if ($o != null) { | |
372 | return $o->bestEmail(); | |
373 | } else { | |
374 | return $this->email_directory; | |
375 | } | |
376 | } | |
377 | ||
94b72319 FB |
378 | public function data() |
379 | { | |
380 | $this->first_name; | |
381 | return $this->data; | |
382 | } | |
383 | ||
5c005b37 RB |
384 | private function nameVariants($type) |
385 | { | |
386 | $vals = array($this->$type); | |
387 | foreach (self::$name_variants[$type] as $var) { | |
388 | $vartype = $type . '_' . $var; | |
389 | $varname = $this->$vartype; | |
390 | if ($varname != null && $varname != "") { | |
391 | $vals[] = $varname; | |
392 | } | |
393 | } | |
394 | return array_unique($vals); | |
395 | } | |
396 | ||
b6569a95 FB |
397 | public function nationalities() |
398 | { | |
399 | $nats = array(); | |
6420023b | 400 | $countries = DirEnum::getOptions(DirEnum::COUNTRIES); |
b6569a95 | 401 | if ($this->nationality1) { |
6420023b | 402 | $nats[$this->nationality1] = $countries[$this->nationality1]; |
b6569a95 FB |
403 | } |
404 | if ($this->nationality2) { | |
6420023b | 405 | $nats[$this->nationality2] = $countries[$this->nationality2]; |
b6569a95 FB |
406 | } |
407 | if ($this->nationality3) { | |
6420023b | 408 | $nats[$this->nationality3] = $countries[$this->nationality3]; |
b6569a95 FB |
409 | } |
410 | return $nats; | |
411 | } | |
412 | ||
3e53a496 FB |
413 | public function __get($name) |
414 | { | |
415 | if (property_exists($this, $name)) { | |
416 | return $this->$name; | |
417 | } | |
418 | ||
3e53a496 FB |
419 | if (isset($this->data[$name])) { |
420 | return $this->data[$name]; | |
421 | } | |
422 | ||
423 | return null; | |
424 | } | |
425 | ||
426 | public function __isset($name) | |
427 | { | |
428 | return property_exists($this, $name) || isset($this->data[$name]); | |
429 | } | |
430 | ||
ddbebe4c FB |
431 | public function __unset($name) |
432 | { | |
433 | if (property_exists($this, $name)) { | |
434 | $this->$name = null; | |
435 | } else { | |
436 | unset($this->data[$name]); | |
437 | } | |
438 | } | |
439 | ||
440 | ||
405d70cc RB |
441 | /** |
442 | * Clears a profile. | |
443 | * *always deletes in: profile_addresses, profile_binets, profile_job, | |
444 | * profile_langskills, profile_mentor, profile_networking, | |
445 | * profile_phones, profile_skills, watch_profile | |
446 | * *always keeps in: profile_corps, profile_display, profile_education, | |
447 | * profile_medals, profile_name, profile_photos, search_name | |
448 | * *modifies: profiles | |
449 | */ | |
450 | public function clear() | |
451 | { | |
452 | $tables = array( | |
453 | 'profile_job', 'profile_langskills', 'profile_mentor', | |
454 | 'profile_networking', 'profile_skills', 'watch_profile', | |
455 | 'profile_phones', 'profile_addresses', 'profile_binets'); | |
456 | ||
457 | foreach ($tables as $t) { | |
458 | XDB::execute('DELETE FROM ' . $t . ' | |
459 | WHERE pid = {?}', | |
460 | $this->id()); | |
461 | } | |
462 | ||
463 | XDB::execute("UPDATE profiles | |
464 | SET cv = NULL, freetext = NULL, freetext_pub = 'private', | |
465 | medals_pub = 'private', alias_pub = 'private', | |
466 | email_directory = NULL | |
467 | WHERE pid = {?}", | |
468 | $this->id()); | |
469 | } | |
470 | ||
c159e50f RB |
471 | /** Sets the level of visibility of the profile |
472 | * Sets $this->visibility to a list of valid visibilities. | |
1a9affb7 | 473 | * @param one of the self::VIS_* values |
c159e50f | 474 | */ |
f5642983 FB |
475 | public function setVisibilityLevel($visibility) |
476 | { | |
1a9affb7 | 477 | $this->visibility->setLevel($visibility); |
f5642983 FB |
478 | } |
479 | ||
c159e50f RB |
480 | /** Determine whether an item with visibility $visibility can be displayed |
481 | * with the current level of visibility of the profile | |
482 | * @param $visibility The level of visibility to be checked | |
483 | */ | |
484 | public function isVisible($visibility) | |
485 | { | |
1a9affb7 | 486 | return $this->visibility->isVisible($visibility); |
9f21bd15 RB |
487 | } |
488 | ||
a0f5fa32 | 489 | /** Stores the list of fields which have already been fetched for this Profile |
1f8250e4 RB |
490 | */ |
491 | public function setFetchedFields($fields) | |
990cb17b | 492 | { |
1f8250e4 RB |
493 | if (($fields | self::FETCH_ALL) != self::FETCH_ALL) { |
494 | Platal::page()->kill("Invalid fetched fields: $fields"); | |
495 | } | |
496 | ||
497 | $this->fetched_fields = $fields; | |
498 | } | |
499 | ||
4ec03752 RB |
500 | /** Have we already fetched this field ? |
501 | */ | |
1f8250e4 RB |
502 | private function fetched($field) |
503 | { | |
4ec03752 RB |
504 | if (!array_key_exists($field, ProfileField::$fields)) { |
505 | Platal::page()->kill("Invalid field: $field"); | |
1f8250e4 RB |
506 | } |
507 | ||
508 | return ($this->fetched_fields & $field); | |
509 | } | |
510 | ||
511 | /** If not already done, fetches data for the given field | |
512 | * @param $field One of the Profile::FETCH_* | |
513 | * @return A ProfileField, or null | |
514 | */ | |
515 | private function getProfileField($field) | |
516 | { | |
3ac45f10 PC |
517 | if (!array_key_exists($field, ProfileField::$fields)) { |
518 | Platal::page()->kill("Invalid field: $field"); | |
519 | } | |
1f8250e4 RB |
520 | if ($this->fetched($field)) { |
521 | return null; | |
522 | } else { | |
523 | $this->fetched_fields = $this->fetched_fields | $field; | |
524 | } | |
525 | ||
526 | $cls = ProfileField::$fields[$field]; | |
527 | ||
990cb17b RB |
528 | return ProfileField::getForPID($cls, $this->id(), $this->visibility); |
529 | } | |
530 | ||
8cf886dc RB |
531 | /** Consolidates internal data (addresses, phones, jobs) |
532 | */ | |
533 | private function consolidateFields() | |
534 | { | |
4ec03752 | 535 | // Link phones to addresses |
8cf886dc RB |
536 | if ($this->phones != null) { |
537 | if ($this->addresses != null) { | |
538 | $this->addresses->addPhones($this->phones); | |
539 | } | |
540 | ||
541 | if ($this->jobs != null) { | |
542 | $this->jobs->addPhones($this->phones); | |
543 | } | |
544 | } | |
545 | ||
4ec03752 | 546 | // Link addresses to jobs |
8cf886dc RB |
547 | if ($this->addresses != null && $this->jobs != null) { |
548 | $this->jobs->addAddresses($this->addresses); | |
549 | } | |
4ec03752 RB |
550 | |
551 | // Link jobterms to jobs | |
3ac45f10 PC |
552 | if ($this->jobs != null && $this->jobterms != null) { |
553 | $this->jobs->addJobTerms($this->jobterms); | |
554 | } | |
8cf886dc RB |
555 | } |
556 | ||
833a6e86 FB |
557 | /* Photo |
558 | */ | |
1a9affb7 | 559 | private $photo = null; |
7988f7d6 | 560 | public function getPhoto($fallback = true, $data = false) |
9f21bd15 | 561 | { |
1a9affb7 RB |
562 | if ($this->has_photo) { |
563 | if ($data && ($this->photo == null || $this->photo->mimeType == null)) { | |
8d6bbca3 | 564 | $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update |
1f8250e4 RB |
565 | FROM profile_photos |
566 | WHERE pid = {?}', $this->pid); | |
8d6bbca3 | 567 | $this->photo = PlImage::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']); |
1a9affb7 RB |
568 | } else if ($this->photo == null) { |
569 | $this->photo = PlImage::fromData(null, null, $this->photo_width, $this->photo_height); | |
570 | } | |
571 | return $this->photo; | |
9f21bd15 | 572 | } else if ($fallback) { |
fa671a30 SJ |
573 | if ($this->mainEducation() == 'X') { |
574 | return PlImage::fromFile(dirname(__FILE__) . '/../htdocs/images/none_x.png', 'image/png'); | |
575 | } | |
576 | return PlImage::fromFile(dirname(__FILE__) . '/../htdocs/images/none_md.png', 'image/png'); | |
9f21bd15 RB |
577 | } |
578 | return null; | |
579 | } | |
7d0ebdf5 | 580 | |
4bc5b8f0 FB |
581 | /* Addresses |
582 | */ | |
7d0ebdf5 RB |
583 | private $addresses = null; |
584 | public function setAddresses(ProfileAddresses $addr) | |
585 | { | |
586 | $this->addresses = $addr; | |
8cf886dc | 587 | $this->consolidateFields(); |
7d0ebdf5 RB |
588 | } |
589 | ||
dbcc3b3d | 590 | private function fetchAddresses() |
f5642983 | 591 | { |
1f8250e4 | 592 | if ($this->addresses == null && !$this->fetched(self::FETCH_ADDRESSES)) { |
61ee68c7 FB |
593 | $addr = $this->getProfileField(self::FETCH_ADDRESSES); |
594 | if ($addr) { | |
595 | $this->setAddresses($addr); | |
dbcc3b3d | 596 | $this->fetchPhones(); |
61ee68c7 | 597 | } |
f5642983 | 598 | } |
dbcc3b3d RB |
599 | } |
600 | ||
601 | public function getAddresses($flags, $limit = null) | |
602 | { | |
603 | $this->fetchAddresses(); | |
0907501b RB |
604 | |
605 | if ($this->addresses == null) { | |
598c57f6 | 606 | return array(); |
0907501b | 607 | } |
990cb17b | 608 | return $this->addresses->get($flags, $limit); |
f5642983 FB |
609 | } |
610 | ||
598c57f6 RB |
611 | public function iterAddresses($flags, $limit = null) |
612 | { | |
613 | return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true); | |
614 | } | |
615 | ||
f5642983 FB |
616 | public function getMainAddress() |
617 | { | |
b0ffc881 RB |
618 | $main = $this->getAddresses(self::ADDRESS_MAIN); |
619 | $perso = $this->getAddresses(self::ADDRESS_PERSO); | |
620 | ||
621 | if (count($main)) { | |
622 | return array_pop($main); | |
623 | } else if (count($perso)) { | |
624 | return array_pop($perso); | |
f5642983 | 625 | } else { |
b0ffc881 | 626 | return null; |
f5642983 FB |
627 | } |
628 | } | |
3e53a496 | 629 | |
bdef0d33 RB |
630 | /* Phones |
631 | */ | |
632 | private $phones = null; | |
633 | public function setPhones(ProfilePhones $phones) | |
634 | { | |
635 | $this->phones = $phones; | |
636 | $this->consolidateFields(); | |
637 | } | |
638 | ||
dbcc3b3d | 639 | private function fetchPhones() |
bdef0d33 | 640 | { |
1f8250e4 | 641 | if ($this->phones == null && !$this->fetched(self::FETCH_PHONES)) { |
56721636 PC |
642 | $phones = $this->getProfileField(self::FETCH_PHONES); |
643 | if (isset($phones)) { | |
644 | $this->setPhones($phones); | |
645 | } | |
bdef0d33 | 646 | } |
dbcc3b3d | 647 | } |
bdef0d33 | 648 | |
dbcc3b3d RB |
649 | public function getPhones($flags, $limit = null) |
650 | { | |
651 | $this->fetchPhones(); | |
bdef0d33 | 652 | if ($this->phones == null) { |
598c57f6 | 653 | return array(); |
bdef0d33 RB |
654 | } |
655 | return $this->phones->get($flags, $limit); | |
656 | } | |
4bc5b8f0 FB |
657 | |
658 | /* Educations | |
659 | */ | |
d4d395bb | 660 | private $educations = null; |
a060e1c3 RB |
661 | public function setEducations(ProfileEducation $edu) |
662 | { | |
663 | $this->educations = $edu; | |
664 | } | |
665 | ||
4bc5b8f0 FB |
666 | public function getEducations($flags, $limit = null) |
667 | { | |
1f8250e4 RB |
668 | if ($this->educations == null && !$this->fetched(self::FETCH_EDU)) { |
669 | $this->setEducations($this->getProfileField(self::FETCH_EDU)); | |
d4d395bb | 670 | } |
0907501b RB |
671 | |
672 | if ($this->educations == null) { | |
598c57f6 | 673 | return array(); |
0907501b | 674 | } |
a060e1c3 | 675 | return $this->educations->get($flags, $limit); |
4bc5b8f0 FB |
676 | } |
677 | ||
678 | public function getExtraEducations($limit = null) | |
679 | { | |
680 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); | |
681 | } | |
682 | ||
0396c259 RB |
683 | /* Corps |
684 | */ | |
685 | private $corps = null; | |
686 | public function setCorps(ProfileCorps $corps) | |
687 | { | |
688 | $this->corps = $corps; | |
689 | } | |
690 | ||
691 | public function getCorps() | |
692 | { | |
1f8250e4 RB |
693 | if ($this->corps == null && !$this->fetched(self::FETCH_CORPS)) { |
694 | $this->setCorps($this->getProfileField(self::FETCH_CORPS)); | |
0396c259 RB |
695 | } |
696 | return $this->corps; | |
697 | } | |
4bc5b8f0 | 698 | |
04a94b1d FB |
699 | /** Networking |
700 | */ | |
d4d395bb RB |
701 | private $networks = null; |
702 | public function setNetworking(ProfileNetworking $nw) | |
703 | { | |
704 | $this->networks = $nw; | |
705 | } | |
04a94b1d FB |
706 | |
707 | public function getNetworking($flags, $limit = null) | |
708 | { | |
1f8250e4 | 709 | if ($this->networks == null && !$this->fetched(self::FETCH_NETWORKING)) { |
dad85695 FB |
710 | $nw = $this->getProfileField(self::FETCH_NETWORKING); |
711 | if ($nw) { | |
712 | $this->setNetworking($nw); | |
713 | } | |
04a94b1d | 714 | } |
0907501b | 715 | if ($this->networks == null) { |
598c57f6 | 716 | return array(); |
0907501b | 717 | } |
d4d395bb | 718 | return $this->networks->get($flags, $limit); |
04a94b1d FB |
719 | } |
720 | ||
721 | public function getWebSite() | |
722 | { | |
723 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); | |
598c57f6 | 724 | if (count($site) != 1) { |
04a94b1d FB |
725 | return null; |
726 | } | |
598c57f6 | 727 | $site = array_pop($site); |
dad85695 | 728 | return $site; |
04a94b1d FB |
729 | } |
730 | ||
731 | ||
e718bd18 FB |
732 | /** Jobs |
733 | */ | |
949fc736 RB |
734 | private $jobs = null; |
735 | public function setJobs(ProfileJobs $jobs) | |
736 | { | |
737 | $this->jobs = $jobs; | |
738 | $this->consolidateFields(); | |
739 | } | |
e718bd18 | 740 | |
dbcc3b3d | 741 | private function fetchJobs() |
e718bd18 | 742 | { |
1f8250e4 | 743 | if ($this->jobs == null && !$this->fetched(self::FETCH_JOBS)) { |
61ee68c7 FB |
744 | $jobs = $this->getProfileField(self::FETCH_JOBS); |
745 | if ($jobs) { | |
746 | $this->setJobs($jobs); | |
dbcc3b3d | 747 | $this->fetchAddresses(); |
61ee68c7 | 748 | } |
e718bd18 | 749 | } |
dbcc3b3d RB |
750 | } |
751 | ||
752 | public function getJobs($flags, $limit = null) | |
753 | { | |
754 | $this->fetchJobs(); | |
949fc736 | 755 | |
0907501b | 756 | if ($this->jobs == null) { |
598c57f6 | 757 | return array(); |
0907501b | 758 | } |
949fc736 | 759 | return $this->jobs->get($flags, $limit); |
e718bd18 FB |
760 | } |
761 | ||
44ec5eb5 | 762 | public function getMainJob() |
e718bd18 FB |
763 | { |
764 | $job = $this->getJobs(self::JOBS_MAIN, 1); | |
598c57f6 | 765 | if (count($job) != 1) { |
e718bd18 FB |
766 | return null; |
767 | } | |
598c57f6 | 768 | return array_pop($job); |
e718bd18 FB |
769 | } |
770 | ||
3ac45f10 PC |
771 | /** JobTerms |
772 | */ | |
773 | private $jobterms = null; | |
774 | public function setJobTerms(ProfileJobTerms $jobterms) | |
775 | { | |
776 | $this->jobterms = $jobterms; | |
777 | $this->consolidateFields(); | |
778 | } | |
779 | ||
4d1f0f6b RB |
780 | private $mentor_countries = null; |
781 | public function setMentoringCountries(ProfileMentoringCountries $countries) | |
782 | { | |
783 | $this->mentor_countries = $countries; | |
784 | } | |
785 | ||
786 | public function getMentoringCountries() | |
787 | { | |
788 | if ($this->mentor_countries == null && !$this->fetched(self::FETCH_MENTOR_COUNTRY)) { | |
789 | $this->setMentoringCountries($this->getProfileField(self::FETCH_MENTOR_COUNTRY)); | |
790 | } | |
791 | ||
792 | if ($this->mentor_countries == null) { | |
793 | return array(); | |
794 | } else { | |
795 | return $this->mentor_countries->countries; | |
796 | } | |
797 | } | |
798 | ||
3ac45f10 PC |
799 | /** List of job terms to specify mentoring */ |
800 | private $mentor_terms = null; | |
801 | /** | |
802 | * set job terms to specify mentoring | |
803 | * @param $terms a ProfileMentoringTerms object listing terms only for this profile | |
804 | */ | |
805 | public function setMentoringTerms(ProfileMentoringTerms $terms) | |
806 | { | |
807 | $this->mentor_terms = $terms; | |
808 | } | |
809 | /** | |
810 | * get all job terms that specify mentoring | |
811 | * @return an array of JobTerms objects | |
812 | */ | |
813 | public function getMentoringTerms() | |
814 | { | |
815 | if ($this->mentor_terms == null && !$this->fetched(self::FETCH_MENTOR_TERMS)) { | |
816 | $this->setMentoringTerms($this->getProfileField(self::FETCH_MENTOR_TERMS)); | |
817 | } | |
818 | ||
819 | if ($this->mentor_terms == null) { | |
820 | return array(); | |
821 | } else { | |
822 | return $this->mentor_terms->get(); | |
823 | } | |
824 | } | |
825 | ||
826 | ||
5c005b37 RB |
827 | /* Binets |
828 | */ | |
829 | public function getBinets() | |
830 | { | |
875a88d3 RB |
831 | if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) { |
832 | return XDB::fetchColumn('SELECT binet_id | |
833 | FROM profile_binets | |
834 | WHERE pid = {?}', $this->id()); | |
835 | } else { | |
836 | return array(); | |
837 | } | |
5c005b37 | 838 | } |
97b71de5 RB |
839 | public function getBinetsNames() |
840 | { | |
875a88d3 RB |
841 | if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) { |
842 | return XDB::fetchColumn('SELECT text | |
843 | FROM profile_binets AS pb | |
844 | LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id) | |
845 | WHERE pb.pid = {?}', $this->id()); | |
846 | } else { | |
847 | return array(); | |
848 | } | |
97b71de5 | 849 | } |
5c005b37 | 850 | |
26ca919b RB |
851 | /* Medals |
852 | */ | |
853 | private $medals = null; | |
854 | public function setMedals(ProfileMedals $medals) | |
855 | { | |
856 | $this->medals = $medals; | |
857 | } | |
858 | ||
859 | public function getMedals() | |
860 | { | |
1f8250e4 RB |
861 | if ($this->medals == null && !$this->fetched(self::FETCH_MEDALS)) { |
862 | $this->setMedals($this->getProfileField(self::FETCH_MEDALS)); | |
26ca919b RB |
863 | } |
864 | if ($this->medals == null) { | |
865 | return array(); | |
866 | } | |
867 | return $this->medals->medals; | |
868 | } | |
e718bd18 | 869 | |
94590511 SJ |
870 | public function compareNames($firstname, $lastname) |
871 | { | |
872 | $_lastname = mb_strtoupper($this->lastName()); | |
873 | $_firstname = mb_strtoupper($this->firstName()); | |
874 | $lastname = mb_strtoupper($lastname); | |
875 | $firstname = mb_strtoupper($firstname); | |
876 | ||
877 | $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname)); | |
878 | $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY); | |
879 | $maxlen = 0; | |
880 | ||
881 | foreach ($tokens as $str) { | |
882 | $isOk &= (strpos($_lastname, $str) !== false); | |
883 | $maxlen = max($maxlen, strlen($str)); | |
884 | } | |
885 | ||
886 | return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname))); | |
887 | } | |
888 | ||
aaffb7b2 FB |
889 | /* Export to JSON |
890 | */ | |
891 | public function export() | |
892 | { | |
893 | return array( | |
894 | 'hrpid' => $this->hrid(), | |
895 | 'display_name' => $this->shortName(), | |
896 | 'full_name' => $this->fullName(), | |
897 | 'directory_name' => $this->directory_name, | |
898 | 'promo' => $this->promo(), | |
899 | 'year_promo' => $this->yearpromo(), | |
900 | 'is_active' => $this->isActive(), | |
901 | 'first_name' => $this->firstName(), | |
902 | 'last_name' => $this->lastName(), | |
903 | 'is_female' => $this->isFemale(), | |
904 | ); | |
905 | } | |
906 | ||
9f21bd15 | 907 | private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
b774ddab FB |
908 | { |
909 | if (count($pids) == 0) { | |
7a8da8e8 | 910 | return null; |
b774ddab | 911 | } |
0d906109 RB |
912 | |
913 | if ($respect_order) { | |
914 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); | |
915 | } else { | |
916 | $order = ''; | |
917 | } | |
918 | ||
1a9affb7 | 919 | $visibility = new ProfileVisibility($visibility); |
9f21bd15 | 920 | |
3e2442cd RB |
921 | $it = XDB::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref, |
922 | p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex, | |
77d2ae52 RB |
923 | IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory, |
924 | p.last_change, p.nationality1, p.nationality2, p.nationality3, | |
3e2442cd | 925 | IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext, |
d718e91c | 926 | pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid, |
348b3844 RB |
927 | IF ({?}, pse.text, NULL) AS section, |
928 | pn_f.name AS firstname, pn_l.name AS lastname, | |
929 | IF( {?}, pn_n.name, NULL) AS nickname, | |
9f21bd15 RB |
930 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, |
931 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, | |
09e54905 SJ |
932 | pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name, |
933 | pd.directory_name, pd.public_name, pd.private_name, | |
934 | IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile, | |
1a9affb7 | 935 | (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo, |
7988f7d6 | 936 | ph.x AS photo_width, ph.y AS photo_height, |
9f21bd15 | 937 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, |
bdef0d33 | 938 | pm.expertise AS mentor_expertise, |
9f21bd15 RB |
939 | ap.uid AS owner_id |
940 | FROM profiles AS p | |
941 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
942 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
97b71de5 | 943 | LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section) |
9f21bd15 RB |
944 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid |
945 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
946 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
947 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
948 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
949 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
950 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
951 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
952 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid | |
953 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
954 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') | |
955 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) | |
bdef0d33 | 956 | LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid) |
9f21bd15 | 957 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
3e2442cd | 958 | WHERE p.pid IN {?} |
9f21bd15 | 959 | GROUP BY p.pid |
3e2442cd | 960 | ' . $order, |
77d2ae52 RB |
961 | $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // CV |
962 | $visibility->levels(), // freetext | |
963 | $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // section | |
964 | $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // nickname | |
965 | $visibility->levels(), // mobile | |
966 | $visibility->levels(), // photo | |
3e2442cd RB |
967 | $pids |
968 | ); | |
31ef12ef | 969 | return new ProfileIterator($it, $pids, $fields, $visibility); |
b774ddab FB |
970 | } |
971 | ||
972 | public static function getPID($login) | |
973 | { | |
974 | if ($login instanceof PlUser) { | |
975 | return XDB::fetchOneCell('SELECT pid | |
976 | FROM account_profiles | |
977 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
978 | $login->id()); | |
9f21bd15 | 979 | } else if (ctype_digit($login)) { |
b774ddab FB |
980 | return XDB::fetchOneCell('SELECT pid |
981 | FROM profiles | |
982 | WHERE pid = {?}', $login); | |
983 | } else { | |
984 | return XDB::fetchOneCell('SELECT pid | |
985 | FROM profiles | |
986 | WHERE hrpid = {?}', $login); | |
987 | } | |
988 | } | |
989 | ||
0d906109 RB |
990 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
991 | { | |
992 | if ($respect_order) { | |
993 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
994 | } else { | |
995 | $order = ''; | |
996 | } | |
997 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
998 | FROM account_profiles AS ap | |
999 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
9f21bd15 RB |
1000 | AND ap.uid IN ' . XDB::formatArray($uids) .' |
1001 | ' . $order); | |
0d906109 | 1002 | } |
b774ddab | 1003 | |
e7b93962 FB |
1004 | /** Return the profile associated with the given login. |
1005 | */ | |
9f21bd15 | 1006 | public static function get($login, $fields = 0x0000, $visibility = null) |
a3118782 | 1007 | { |
641f2115 | 1008 | if (is_array($login)) { |
1a9affb7 RB |
1009 | $pf = new Profile($login); |
1010 | $pf->setVisibilityLevel($visibility); | |
1011 | return $pf; | |
641f2115 | 1012 | } |
b774ddab FB |
1013 | $pid = self::getPID($login); |
1014 | if (!is_null($pid)) { | |
9f21bd15 | 1015 | $it = self::iterOverPIDs(array($pid), false, $fields, $visibility); |
0d906109 | 1016 | return $it->next(); |
b774ddab | 1017 | } else { |
efe597c5 FB |
1018 | /* Let say we can identify a profile using the identifiers of its owner. |
1019 | */ | |
455ea0c9 FB |
1020 | if (!($login instanceof PlUser)) { |
1021 | $user = User::getSilent($login); | |
1022 | if ($user && $user->hasProfile()) { | |
1023 | return $user->profile(); | |
1024 | } | |
efe597c5 | 1025 | } |
3e53a496 | 1026 | return null; |
e7b93962 FB |
1027 | } |
1028 | } | |
a3118782 | 1029 | |
9f21bd15 | 1030 | public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 1031 | { |
9f21bd15 | 1032 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility); |
0d906109 RB |
1033 | } |
1034 | ||
9f21bd15 | 1035 | public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 1036 | { |
9f21bd15 | 1037 | return self::fetchProfileData($pids, $respect_order, $fields, $visibility); |
0d906109 RB |
1038 | } |
1039 | ||
b774ddab FB |
1040 | /** Return profiles for the list of pids. |
1041 | */ | |
1a9affb7 | 1042 | public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null) |
b774ddab FB |
1043 | { |
1044 | if (count($pids) == 0) { | |
1045 | return array(); | |
1046 | } | |
9f21bd15 | 1047 | $it = self::iterOverPIDs($pids, true, $fields, $visibility); |
b774ddab | 1048 | $profiles = array(); |
0d906109 RB |
1049 | while ($p = $it->next()) { |
1050 | $profiles[$p->id()] = $p; | |
b774ddab FB |
1051 | } |
1052 | return $profiles; | |
1053 | } | |
1054 | ||
1055 | /** Return profiles for uids. | |
1056 | */ | |
9f21bd15 | 1057 | public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null) |
b774ddab FB |
1058 | { |
1059 | if (count($uids) == 0) { | |
1060 | return array(); | |
1061 | } | |
9f21bd15 | 1062 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility); |
b774ddab FB |
1063 | } |
1064 | ||
913a4e90 RB |
1065 | public static function isDisplayName($name) |
1066 | { | |
1067 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
1068 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
1069 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
1070 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
1071 | } | |
1072 | ||
a9ef52c9 RB |
1073 | /** Returns the closest "accounts only" name type for $name |
1074 | */ | |
1075 | public static function getAccountEquivalentName($name) | |
1076 | { | |
1077 | switch ($name) | |
1078 | { | |
1079 | case self::DN_DIRECTORY: | |
1080 | case self::DN_SORT: | |
1081 | return 'directory_name'; | |
1082 | case self::DN_FULL: | |
1083 | case self::DN_PUBLIC: | |
1084 | return 'full_name'; | |
1085 | case self::DN_PRIVATE: | |
1086 | case self::DN_SHORT: | |
1087 | case self::DN_YOURSELF: | |
1088 | default: | |
1089 | return 'display_name'; | |
1090 | } | |
1091 | } | |
1092 | ||
9f21bd15 RB |
1093 | public static function getNameTypeId($type, $for_sql = false) |
1094 | { | |
1095 | if (!S::has('name_types')) { | |
1096 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type | |
1097 | FROM profile_name_enum'); | |
1098 | S::set('name_types', $table); | |
1099 | } else { | |
1100 | $table = S::v('name_types'); | |
1101 | } | |
1102 | if ($for_sql) { | |
1103 | return XDB::escape($table[$type]); | |
1104 | } else { | |
1105 | return $table[$type]; | |
1106 | } | |
1107 | } | |
1108 | ||
bedc4295 | 1109 | public static function rebuildSearchTokens($pids, $transaction = true) |
726eaf7a | 1110 | { |
5acd53a9 FB |
1111 | if (!is_array($pids)) { |
1112 | $pids = array($pids); | |
1113 | } | |
5600b2fe | 1114 | $keys = XDB::iterator("(SELECT n.pid AS pid, n.name AS name, e.score AS score, e.general_type, |
80bc45c2 FB |
1115 | IF(FIND_IN_SET('public', e.flags), 'public', '') AS public |
1116 | FROM profile_name AS n | |
1117 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
1118 | WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags)) | |
1119 | UNION | |
5600b2fe | 1120 | (SELECT n.pid AS pid, n.particle AS name, 0 AS score, e.general_type, |
80bc45c2 FB |
1121 | IF(FIND_IN_SET('public', e.flags), 'public', '') AS public |
1122 | FROM profile_name AS n | |
1123 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
1124 | WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags)) | |
1125 | ", | |
1126 | $pids, $pids); | |
5acd53a9 | 1127 | $names = array(); |
2f62eba7 | 1128 | while ($key = $keys->next()) { |
726eaf7a SJ |
1129 | if ($key['name'] == '') { |
1130 | continue; | |
1131 | } | |
5acd53a9 | 1132 | $pid = $key['pid']; |
5ed1cce8 RB |
1133 | require_once 'name.func.inc.php'; |
1134 | $toks = split_name_for_search($key['name']); | |
80bc45c2 FB |
1135 | $toks = array_reverse($toks); |
1136 | ||
1137 | /* Split the score between the tokens to avoid the user to be over-rated. | |
1138 | * Let says my user name is "Machin-Truc Bidule" and I also have a user named | |
1139 | * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed | |
1140 | * before "Machin-Truc" for both "Machin Truc" and "Machin" searches. | |
1141 | */ | |
1142 | $eltScore = ceil(((float)$key['score'])/((float)count($toks))); | |
726eaf7a | 1143 | $token = ''; |
80bc45c2 FB |
1144 | foreach ($toks as $tok) { |
1145 | $token = $tok . $token; | |
5600b2fe | 1146 | $names["$pid-$token"] = XDB::format('({?}, {?}, {?}, {?}, {?}, {?})', |
5acd53a9 | 1147 | $token, $pid, soundex_fr($token), |
5600b2fe | 1148 | $eltScore, $key['public'], $key['general_type']); |
726eaf7a SJ |
1149 | } |
1150 | } | |
bedc4295 FB |
1151 | if ($transaction) { |
1152 | XDB::startTransaction(); | |
1153 | } | |
5acd53a9 FB |
1154 | XDB::execute('DELETE FROM search_name |
1155 | WHERE pid IN {?}', | |
1156 | $pids); | |
1157 | if (count($names) > 0) { | |
5600b2fe | 1158 | XDB::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags, general_type) |
80bc45c2 | 1159 | VALUES ' . implode(', ', $names)); |
5acd53a9 | 1160 | } |
bedc4295 FB |
1161 | if ($transaction) { |
1162 | XDB::commit(); | |
1163 | } | |
69b46857 SJ |
1164 | } |
1165 | ||
1166 | /** The school identifier consists of 6 digits. The first 3 represent the | |
1167 | * promotion entry year. The last 3 indicate the student's rank. | |
aab2ffdd | 1168 | * |
69b46857 SJ |
1169 | * Our identifier consists of 8 digits and both half have the same role. |
1170 | * This enables us to deal with bigger promotions and with a wider range | |
1171 | * of promotions. | |
1172 | * | |
1173 | * getSchoolId returns a school identifier given one of ours. | |
1174 | * getXorgId returns a X.org identifier given a school identifier. | |
1175 | */ | |
1176 | public static function getSchoolId($xorgId) | |
1177 | { | |
1178 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
1179 | return null; | |
1180 | } | |
1181 | ||
1182 | $year = intval(substr($xorgId, 0, 4)); | |
1183 | $rank = intval(substr($xorgId, 5, 3)); | |
1184 | if ($year < 1996) { | |
1185 | return null; | |
1186 | } elseif ($year < 2000) { | |
1187 | $year = intval(substr(1900 - $year, 1, 3)); | |
1188 | return sprintf('%02u0%03u', $year, $rank); | |
1189 | } else { | |
1190 | $year = intval(substr(1900 - $year, 1, 3)); | |
1191 | return sprintf('%03u%03u', $year, $rank); | |
1192 | } | |
1193 | } | |
726eaf7a | 1194 | |
69b46857 SJ |
1195 | public static function getXorgId($schoolId) |
1196 | { | |
94590511 | 1197 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
a292484d SJ |
1198 | return null; |
1199 | } | |
1200 | ||
69b46857 SJ |
1201 | $year = intval(substr($schoolId, 0, 3)); |
1202 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 1203 | |
69b46857 SJ |
1204 | if ($year > 200) { |
1205 | $year /= 10; | |
1206 | } | |
1207 | if ($year < 96) { | |
1208 | return null; | |
1209 | } else { | |
1210 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
1211 | } | |
726eaf7a | 1212 | } |
e7b93962 FB |
1213 | } |
1214 | ||
31ef12ef RB |
1215 | |
1216 | /** Iterator over a set of Profiles | |
1217 | */ | |
1218 | class ProfileIterator implements PlIterator | |
9f21bd15 RB |
1219 | { |
1220 | private $iterator = null; | |
1221 | private $fields; | |
1a9affb7 | 1222 | private $visibility; |
9f21bd15 | 1223 | |
3ac45f10 PC |
1224 | const FETCH_ALL = 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS |
1225 | ||
1a9affb7 | 1226 | public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null) |
9f21bd15 RB |
1227 | { |
1228 | require_once 'profilefields.inc.php'; | |
1a9affb7 RB |
1229 | |
1230 | if ($visibility == null) { | |
1231 | $visibility = new ProfileVisibility(); | |
1232 | } | |
1233 | ||
9f21bd15 | 1234 | $this->fields = $fields; |
1a9affb7 | 1235 | $this->visibility = $visibility; |
9f21bd15 RB |
1236 | |
1237 | $subits = array(); | |
1238 | $callbacks = array(); | |
1239 | ||
1240 | $subits[0] = $it; | |
1241 | $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid'); | |
1242 | $cb = PlIteratorUtils::objectPropertyCallback('pid'); | |
1243 | ||
3ac45f10 PC |
1244 | $fields = $fields & self::FETCH_ALL; |
1245 | for ($field = 1; $field < $fields; $field *= 2) { | |
1246 | if (($fields & $field) ) { | |
1247 | $callbacks[$field] = $cb; | |
1248 | $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility); | |
1249 | } | |
9f21bd15 RB |
1250 | } |
1251 | ||
9f21bd15 RB |
1252 | $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0); |
1253 | } | |
1254 | ||
1f8250e4 | 1255 | private function hasData($field, $vals) |
9f21bd15 | 1256 | { |
1f8250e4 | 1257 | return ($this->fields & $field) && ($vals[$field] != null); |
9f21bd15 RB |
1258 | } |
1259 | ||
1260 | private function fillProfile(array $vals) | |
1261 | { | |
9f21bd15 | 1262 | $pf = Profile::get($vals[0]); |
1a9affb7 | 1263 | $pf->setVisibilityLevel($this->visibility->level()); |
1f8250e4 | 1264 | $pf->setFetchedFields($this->fields); |
1a9affb7 | 1265 | |
8cf886dc RB |
1266 | if ($this->hasData(Profile::FETCH_PHONES, $vals)) { |
1267 | $pf->setPhones($vals[Profile::FETCH_PHONES]); | |
9f21bd15 | 1268 | } |
8cf886dc RB |
1269 | if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) { |
1270 | $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]); | |
1271 | } | |
1272 | if ($this->hasData(Profile::FETCH_JOBS, $vals)) { | |
1273 | $pf->setJobs($vals[Profile::FETCH_JOBS]); | |
1274 | } | |
3ac45f10 PC |
1275 | if ($this->hasData(Profile::FETCH_JOB_TERMS, $vals)) { |
1276 | $pf->setJobTerms($vals[Profile::FETCH_JOB_TERMS]); | |
1277 | } | |
8cf886dc RB |
1278 | |
1279 | if ($this->hasData(Profile::FETCH_CORPS, $vals)) { | |
9f21bd15 RB |
1280 | $pf->setCorps($vals[Profile::FETCH_CORPS]); |
1281 | } | |
8cf886dc | 1282 | if ($this->hasData(Profile::FETCH_EDU, $vals)) { |
26ca919b | 1283 | $pf->setEducations($vals[Profile::FETCH_EDU]); |
9f21bd15 | 1284 | } |
8cf886dc | 1285 | if ($this->hasData(Profile::FETCH_MEDALS, $vals)) { |
9f21bd15 RB |
1286 | $pf->setMedals($vals[Profile::FETCH_MEDALS]); |
1287 | } | |
8cf886dc | 1288 | if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) { |
9f21bd15 RB |
1289 | $pf->setNetworking($vals[Profile::FETCH_NETWORKING]); |
1290 | } | |
9f21bd15 RB |
1291 | |
1292 | return $pf; | |
1293 | } | |
1294 | ||
1295 | public function next() | |
1296 | { | |
1297 | $vals = $this->iterator->next(); | |
1298 | if ($vals == null) { | |
1299 | return null; | |
1300 | } | |
1301 | return $this->fillProfile($vals); | |
1302 | } | |
1303 | ||
1304 | public function first() | |
1305 | { | |
1306 | return $this->iterator->first(); | |
1307 | } | |
1308 | ||
1309 | public function last() | |
1310 | { | |
1311 | return $this->iterator->last(); | |
1312 | } | |
1313 | ||
1314 | public function total() | |
1315 | { | |
1316 | return $this->iterator->total(); | |
1317 | } | |
1318 | } | |
1319 | ||
e7b93962 FB |
1320 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
1321 | ?> |