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 | ||
94b72319 FB |
286 | /** Print a name with the given formatting: |
287 | * %s = • for women | |
288 | * %f = firstname | |
289 | * %l = lastname | |
290 | * %F = fullname | |
291 | * %S = shortname | |
292 | * %p = promo | |
293 | */ | |
294 | public function name($format) | |
295 | { | |
296 | return str_replace(array('%s', '%f', '%l', '%F', '%S', '%p'), | |
297 | array($this->isFemale() ? '•' : '', | |
faf75faf SJ |
298 | $this->firstName(), $this->lastName(), |
299 | $this->fullName(), $this->shortName(), | |
300 | $this->promo()), $format); | |
94b72319 FB |
301 | } |
302 | ||
303 | public function fullName($with_promo = false) | |
304 | { | |
305 | if ($with_promo) { | |
306 | return $this->full_name . ' (' . $this->promo . ')'; | |
307 | } | |
308 | return $this->full_name; | |
309 | } | |
310 | ||
311 | public function shortName($with_promo = false) | |
312 | { | |
313 | if ($with_promo) { | |
314 | return $this->short_name . ' (' . $this->promo . ')'; | |
315 | } | |
316 | return $this->short_name; | |
317 | } | |
318 | ||
319 | public function firstName() | |
320 | { | |
08c91036 | 321 | return $this->firstname; |
94b72319 FB |
322 | } |
323 | ||
5c005b37 RB |
324 | public function firstNames() |
325 | { | |
326 | return $this->nameVariants(self::FIRSTNAME); | |
327 | } | |
328 | ||
94b72319 FB |
329 | public function lastName() |
330 | { | |
08c91036 | 331 | return $this->lastname; |
94b72319 FB |
332 | } |
333 | ||
5c005b37 RB |
334 | public function lastNames() |
335 | { | |
336 | return $this->nameVariants(self::LASTNAME); | |
337 | } | |
338 | ||
94b72319 FB |
339 | public function isFemale() |
340 | { | |
341 | return $this->sex == PlUser::GENDER_FEMALE; | |
342 | } | |
343 | ||
400ac338 RB |
344 | public function isDead() |
345 | { | |
346 | return ($this->deathdate != null); | |
347 | } | |
348 | ||
564e2b2a RB |
349 | public function displayEmail() |
350 | { | |
351 | $o = $this->owner(); | |
352 | if ($o != null) { | |
353 | return $o->bestEmail(); | |
354 | } else { | |
355 | return $this->email_directory; | |
356 | } | |
357 | } | |
358 | ||
94b72319 FB |
359 | public function data() |
360 | { | |
361 | $this->first_name; | |
362 | return $this->data; | |
363 | } | |
364 | ||
5c005b37 RB |
365 | private function nameVariants($type) |
366 | { | |
367 | $vals = array($this->$type); | |
368 | foreach (self::$name_variants[$type] as $var) { | |
369 | $vartype = $type . '_' . $var; | |
370 | $varname = $this->$vartype; | |
371 | if ($varname != null && $varname != "") { | |
372 | $vals[] = $varname; | |
373 | } | |
374 | } | |
375 | return array_unique($vals); | |
376 | } | |
377 | ||
b6569a95 FB |
378 | public function nationalities() |
379 | { | |
380 | $nats = array(); | |
6420023b | 381 | $countries = DirEnum::getOptions(DirEnum::COUNTRIES); |
b6569a95 | 382 | if ($this->nationality1) { |
6420023b | 383 | $nats[$this->nationality1] = $countries[$this->nationality1]; |
b6569a95 FB |
384 | } |
385 | if ($this->nationality2) { | |
6420023b | 386 | $nats[$this->nationality2] = $countries[$this->nationality2]; |
b6569a95 FB |
387 | } |
388 | if ($this->nationality3) { | |
6420023b | 389 | $nats[$this->nationality3] = $countries[$this->nationality3]; |
b6569a95 FB |
390 | } |
391 | return $nats; | |
392 | } | |
393 | ||
3e53a496 FB |
394 | public function __get($name) |
395 | { | |
396 | if (property_exists($this, $name)) { | |
397 | return $this->$name; | |
398 | } | |
399 | ||
3e53a496 FB |
400 | if (isset($this->data[$name])) { |
401 | return $this->data[$name]; | |
402 | } | |
403 | ||
404 | return null; | |
405 | } | |
406 | ||
407 | public function __isset($name) | |
408 | { | |
409 | return property_exists($this, $name) || isset($this->data[$name]); | |
410 | } | |
411 | ||
ddbebe4c FB |
412 | public function __unset($name) |
413 | { | |
414 | if (property_exists($this, $name)) { | |
415 | $this->$name = null; | |
416 | } else { | |
417 | unset($this->data[$name]); | |
418 | } | |
419 | } | |
420 | ||
421 | ||
405d70cc RB |
422 | /** |
423 | * Clears a profile. | |
424 | * *always deletes in: profile_addresses, profile_binets, profile_job, | |
425 | * profile_langskills, profile_mentor, profile_networking, | |
426 | * profile_phones, profile_skills, watch_profile | |
427 | * *always keeps in: profile_corps, profile_display, profile_education, | |
428 | * profile_medals, profile_name, profile_photos, search_name | |
429 | * *modifies: profiles | |
430 | */ | |
431 | public function clear() | |
432 | { | |
433 | $tables = array( | |
434 | 'profile_job', 'profile_langskills', 'profile_mentor', | |
435 | 'profile_networking', 'profile_skills', 'watch_profile', | |
436 | 'profile_phones', 'profile_addresses', 'profile_binets'); | |
437 | ||
438 | foreach ($tables as $t) { | |
439 | XDB::execute('DELETE FROM ' . $t . ' | |
440 | WHERE pid = {?}', | |
441 | $this->id()); | |
442 | } | |
443 | ||
444 | XDB::execute("UPDATE profiles | |
445 | SET cv = NULL, freetext = NULL, freetext_pub = 'private', | |
446 | medals_pub = 'private', alias_pub = 'private', | |
447 | email_directory = NULL | |
448 | WHERE pid = {?}", | |
449 | $this->id()); | |
450 | } | |
451 | ||
c159e50f RB |
452 | /** Sets the level of visibility of the profile |
453 | * Sets $this->visibility to a list of valid visibilities. | |
1a9affb7 | 454 | * @param one of the self::VIS_* values |
c159e50f | 455 | */ |
f5642983 FB |
456 | public function setVisibilityLevel($visibility) |
457 | { | |
1a9affb7 | 458 | $this->visibility->setLevel($visibility); |
f5642983 FB |
459 | } |
460 | ||
c159e50f RB |
461 | /** Determine whether an item with visibility $visibility can be displayed |
462 | * with the current level of visibility of the profile | |
463 | * @param $visibility The level of visibility to be checked | |
464 | */ | |
465 | public function isVisible($visibility) | |
466 | { | |
1a9affb7 | 467 | return $this->visibility->isVisible($visibility); |
9f21bd15 RB |
468 | } |
469 | ||
a0f5fa32 | 470 | /** Stores the list of fields which have already been fetched for this Profile |
1f8250e4 RB |
471 | */ |
472 | public function setFetchedFields($fields) | |
990cb17b | 473 | { |
1f8250e4 RB |
474 | if (($fields | self::FETCH_ALL) != self::FETCH_ALL) { |
475 | Platal::page()->kill("Invalid fetched fields: $fields"); | |
476 | } | |
477 | ||
478 | $this->fetched_fields = $fields; | |
479 | } | |
480 | ||
4ec03752 RB |
481 | /** Have we already fetched this field ? |
482 | */ | |
1f8250e4 RB |
483 | private function fetched($field) |
484 | { | |
4ec03752 RB |
485 | if (!array_key_exists($field, ProfileField::$fields)) { |
486 | Platal::page()->kill("Invalid field: $field"); | |
1f8250e4 RB |
487 | } |
488 | ||
489 | return ($this->fetched_fields & $field); | |
490 | } | |
491 | ||
492 | /** If not already done, fetches data for the given field | |
493 | * @param $field One of the Profile::FETCH_* | |
494 | * @return A ProfileField, or null | |
495 | */ | |
496 | private function getProfileField($field) | |
497 | { | |
3ac45f10 PC |
498 | if (!array_key_exists($field, ProfileField::$fields)) { |
499 | Platal::page()->kill("Invalid field: $field"); | |
500 | } | |
1f8250e4 RB |
501 | if ($this->fetched($field)) { |
502 | return null; | |
503 | } else { | |
504 | $this->fetched_fields = $this->fetched_fields | $field; | |
505 | } | |
506 | ||
507 | $cls = ProfileField::$fields[$field]; | |
508 | ||
990cb17b RB |
509 | return ProfileField::getForPID($cls, $this->id(), $this->visibility); |
510 | } | |
511 | ||
8cf886dc RB |
512 | /** Consolidates internal data (addresses, phones, jobs) |
513 | */ | |
514 | private function consolidateFields() | |
515 | { | |
4ec03752 | 516 | // Link phones to addresses |
8cf886dc RB |
517 | if ($this->phones != null) { |
518 | if ($this->addresses != null) { | |
519 | $this->addresses->addPhones($this->phones); | |
520 | } | |
521 | ||
522 | if ($this->jobs != null) { | |
523 | $this->jobs->addPhones($this->phones); | |
524 | } | |
525 | } | |
526 | ||
4ec03752 | 527 | // Link addresses to jobs |
8cf886dc RB |
528 | if ($this->addresses != null && $this->jobs != null) { |
529 | $this->jobs->addAddresses($this->addresses); | |
530 | } | |
4ec03752 RB |
531 | |
532 | // Link jobterms to jobs | |
3ac45f10 PC |
533 | if ($this->jobs != null && $this->jobterms != null) { |
534 | $this->jobs->addJobTerms($this->jobterms); | |
535 | } | |
8cf886dc RB |
536 | } |
537 | ||
833a6e86 FB |
538 | /* Photo |
539 | */ | |
1a9affb7 | 540 | private $photo = null; |
7988f7d6 | 541 | public function getPhoto($fallback = true, $data = false) |
9f21bd15 | 542 | { |
1a9affb7 RB |
543 | if ($this->has_photo) { |
544 | if ($data && ($this->photo == null || $this->photo->mimeType == null)) { | |
8d6bbca3 | 545 | $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y, last_update |
1f8250e4 RB |
546 | FROM profile_photos |
547 | WHERE pid = {?}', $this->pid); | |
8d6bbca3 | 548 | $this->photo = PlImage::fromData($res['attach'], 'image/' .$res['attachmime'], $res['x'], $res['y'], $res['last_update']); |
1a9affb7 RB |
549 | } else if ($this->photo == null) { |
550 | $this->photo = PlImage::fromData(null, null, $this->photo_width, $this->photo_height); | |
551 | } | |
552 | return $this->photo; | |
9f21bd15 | 553 | } else if ($fallback) { |
fa671a30 SJ |
554 | if ($this->mainEducation() == 'X') { |
555 | return PlImage::fromFile(dirname(__FILE__) . '/../htdocs/images/none_x.png', 'image/png'); | |
556 | } | |
557 | return PlImage::fromFile(dirname(__FILE__) . '/../htdocs/images/none_md.png', 'image/png'); | |
9f21bd15 RB |
558 | } |
559 | return null; | |
560 | } | |
7d0ebdf5 | 561 | |
4bc5b8f0 FB |
562 | /* Addresses |
563 | */ | |
7d0ebdf5 RB |
564 | private $addresses = null; |
565 | public function setAddresses(ProfileAddresses $addr) | |
566 | { | |
567 | $this->addresses = $addr; | |
8cf886dc | 568 | $this->consolidateFields(); |
7d0ebdf5 RB |
569 | } |
570 | ||
dbcc3b3d | 571 | private function fetchAddresses() |
f5642983 | 572 | { |
1f8250e4 | 573 | if ($this->addresses == null && !$this->fetched(self::FETCH_ADDRESSES)) { |
61ee68c7 FB |
574 | $addr = $this->getProfileField(self::FETCH_ADDRESSES); |
575 | if ($addr) { | |
576 | $this->setAddresses($addr); | |
dbcc3b3d | 577 | $this->fetchPhones(); |
61ee68c7 | 578 | } |
f5642983 | 579 | } |
dbcc3b3d RB |
580 | } |
581 | ||
582 | public function getAddresses($flags, $limit = null) | |
583 | { | |
584 | $this->fetchAddresses(); | |
0907501b RB |
585 | |
586 | if ($this->addresses == null) { | |
598c57f6 | 587 | return array(); |
0907501b | 588 | } |
990cb17b | 589 | return $this->addresses->get($flags, $limit); |
f5642983 FB |
590 | } |
591 | ||
598c57f6 RB |
592 | public function iterAddresses($flags, $limit = null) |
593 | { | |
594 | return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true); | |
595 | } | |
596 | ||
f5642983 FB |
597 | public function getMainAddress() |
598 | { | |
b0ffc881 RB |
599 | $main = $this->getAddresses(self::ADDRESS_MAIN); |
600 | $perso = $this->getAddresses(self::ADDRESS_PERSO); | |
601 | ||
602 | if (count($main)) { | |
603 | return array_pop($main); | |
604 | } else if (count($perso)) { | |
605 | return array_pop($perso); | |
f5642983 | 606 | } else { |
b0ffc881 | 607 | return null; |
f5642983 FB |
608 | } |
609 | } | |
3e53a496 | 610 | |
bdef0d33 RB |
611 | /* Phones |
612 | */ | |
613 | private $phones = null; | |
614 | public function setPhones(ProfilePhones $phones) | |
615 | { | |
616 | $this->phones = $phones; | |
617 | $this->consolidateFields(); | |
618 | } | |
619 | ||
dbcc3b3d | 620 | private function fetchPhones() |
bdef0d33 | 621 | { |
1f8250e4 | 622 | if ($this->phones == null && !$this->fetched(self::FETCH_PHONES)) { |
56721636 PC |
623 | $phones = $this->getProfileField(self::FETCH_PHONES); |
624 | if (isset($phones)) { | |
625 | $this->setPhones($phones); | |
626 | } | |
bdef0d33 | 627 | } |
dbcc3b3d | 628 | } |
bdef0d33 | 629 | |
dbcc3b3d RB |
630 | public function getPhones($flags, $limit = null) |
631 | { | |
632 | $this->fetchPhones(); | |
bdef0d33 | 633 | if ($this->phones == null) { |
598c57f6 | 634 | return array(); |
bdef0d33 RB |
635 | } |
636 | return $this->phones->get($flags, $limit); | |
637 | } | |
4bc5b8f0 FB |
638 | |
639 | /* Educations | |
640 | */ | |
d4d395bb | 641 | private $educations = null; |
a060e1c3 RB |
642 | public function setEducations(ProfileEducation $edu) |
643 | { | |
644 | $this->educations = $edu; | |
645 | } | |
646 | ||
4bc5b8f0 FB |
647 | public function getEducations($flags, $limit = null) |
648 | { | |
1f8250e4 RB |
649 | if ($this->educations == null && !$this->fetched(self::FETCH_EDU)) { |
650 | $this->setEducations($this->getProfileField(self::FETCH_EDU)); | |
d4d395bb | 651 | } |
0907501b RB |
652 | |
653 | if ($this->educations == null) { | |
598c57f6 | 654 | return array(); |
0907501b | 655 | } |
a060e1c3 | 656 | return $this->educations->get($flags, $limit); |
4bc5b8f0 FB |
657 | } |
658 | ||
659 | public function getExtraEducations($limit = null) | |
660 | { | |
661 | return $this->getEducations(self::EDUCATION_EXTRA, $limit); | |
662 | } | |
663 | ||
0396c259 RB |
664 | /* Corps |
665 | */ | |
666 | private $corps = null; | |
667 | public function setCorps(ProfileCorps $corps) | |
668 | { | |
669 | $this->corps = $corps; | |
670 | } | |
671 | ||
672 | public function getCorps() | |
673 | { | |
1f8250e4 RB |
674 | if ($this->corps == null && !$this->fetched(self::FETCH_CORPS)) { |
675 | $this->setCorps($this->getProfileField(self::FETCH_CORPS)); | |
0396c259 RB |
676 | } |
677 | return $this->corps; | |
678 | } | |
4bc5b8f0 | 679 | |
04a94b1d FB |
680 | /** Networking |
681 | */ | |
d4d395bb RB |
682 | private $networks = null; |
683 | public function setNetworking(ProfileNetworking $nw) | |
684 | { | |
685 | $this->networks = $nw; | |
686 | } | |
04a94b1d FB |
687 | |
688 | public function getNetworking($flags, $limit = null) | |
689 | { | |
1f8250e4 | 690 | if ($this->networks == null && !$this->fetched(self::FETCH_NETWORKING)) { |
dad85695 FB |
691 | $nw = $this->getProfileField(self::FETCH_NETWORKING); |
692 | if ($nw) { | |
693 | $this->setNetworking($nw); | |
694 | } | |
04a94b1d | 695 | } |
0907501b | 696 | if ($this->networks == null) { |
598c57f6 | 697 | return array(); |
0907501b | 698 | } |
d4d395bb | 699 | return $this->networks->get($flags, $limit); |
04a94b1d FB |
700 | } |
701 | ||
702 | public function getWebSite() | |
703 | { | |
704 | $site = $this->getNetworking(self::NETWORKING_WEB, 1); | |
598c57f6 | 705 | if (count($site) != 1) { |
04a94b1d FB |
706 | return null; |
707 | } | |
598c57f6 | 708 | $site = array_pop($site); |
dad85695 | 709 | return $site; |
04a94b1d FB |
710 | } |
711 | ||
712 | ||
e718bd18 FB |
713 | /** Jobs |
714 | */ | |
949fc736 RB |
715 | private $jobs = null; |
716 | public function setJobs(ProfileJobs $jobs) | |
717 | { | |
718 | $this->jobs = $jobs; | |
719 | $this->consolidateFields(); | |
720 | } | |
e718bd18 | 721 | |
dbcc3b3d | 722 | private function fetchJobs() |
e718bd18 | 723 | { |
1f8250e4 | 724 | if ($this->jobs == null && !$this->fetched(self::FETCH_JOBS)) { |
61ee68c7 FB |
725 | $jobs = $this->getProfileField(self::FETCH_JOBS); |
726 | if ($jobs) { | |
727 | $this->setJobs($jobs); | |
dbcc3b3d | 728 | $this->fetchAddresses(); |
61ee68c7 | 729 | } |
e718bd18 | 730 | } |
dbcc3b3d RB |
731 | } |
732 | ||
733 | public function getJobs($flags, $limit = null) | |
734 | { | |
735 | $this->fetchJobs(); | |
949fc736 | 736 | |
0907501b | 737 | if ($this->jobs == null) { |
598c57f6 | 738 | return array(); |
0907501b | 739 | } |
949fc736 | 740 | return $this->jobs->get($flags, $limit); |
e718bd18 FB |
741 | } |
742 | ||
44ec5eb5 | 743 | public function getMainJob() |
e718bd18 FB |
744 | { |
745 | $job = $this->getJobs(self::JOBS_MAIN, 1); | |
598c57f6 | 746 | if (count($job) != 1) { |
e718bd18 FB |
747 | return null; |
748 | } | |
598c57f6 | 749 | return array_pop($job); |
e718bd18 FB |
750 | } |
751 | ||
3ac45f10 PC |
752 | /** JobTerms |
753 | */ | |
754 | private $jobterms = null; | |
755 | public function setJobTerms(ProfileJobTerms $jobterms) | |
756 | { | |
757 | $this->jobterms = $jobterms; | |
758 | $this->consolidateFields(); | |
759 | } | |
760 | ||
4d1f0f6b RB |
761 | private $mentor_countries = null; |
762 | public function setMentoringCountries(ProfileMentoringCountries $countries) | |
763 | { | |
764 | $this->mentor_countries = $countries; | |
765 | } | |
766 | ||
767 | public function getMentoringCountries() | |
768 | { | |
769 | if ($this->mentor_countries == null && !$this->fetched(self::FETCH_MENTOR_COUNTRY)) { | |
770 | $this->setMentoringCountries($this->getProfileField(self::FETCH_MENTOR_COUNTRY)); | |
771 | } | |
772 | ||
773 | if ($this->mentor_countries == null) { | |
774 | return array(); | |
775 | } else { | |
776 | return $this->mentor_countries->countries; | |
777 | } | |
778 | } | |
779 | ||
3ac45f10 PC |
780 | /** List of job terms to specify mentoring */ |
781 | private $mentor_terms = null; | |
782 | /** | |
783 | * set job terms to specify mentoring | |
784 | * @param $terms a ProfileMentoringTerms object listing terms only for this profile | |
785 | */ | |
786 | public function setMentoringTerms(ProfileMentoringTerms $terms) | |
787 | { | |
788 | $this->mentor_terms = $terms; | |
789 | } | |
790 | /** | |
791 | * get all job terms that specify mentoring | |
792 | * @return an array of JobTerms objects | |
793 | */ | |
794 | public function getMentoringTerms() | |
795 | { | |
796 | if ($this->mentor_terms == null && !$this->fetched(self::FETCH_MENTOR_TERMS)) { | |
797 | $this->setMentoringTerms($this->getProfileField(self::FETCH_MENTOR_TERMS)); | |
798 | } | |
799 | ||
800 | if ($this->mentor_terms == null) { | |
801 | return array(); | |
802 | } else { | |
803 | return $this->mentor_terms->get(); | |
804 | } | |
805 | } | |
806 | ||
807 | ||
5c005b37 RB |
808 | /* Binets |
809 | */ | |
810 | public function getBinets() | |
811 | { | |
875a88d3 RB |
812 | if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) { |
813 | return XDB::fetchColumn('SELECT binet_id | |
814 | FROM profile_binets | |
815 | WHERE pid = {?}', $this->id()); | |
816 | } else { | |
817 | return array(); | |
818 | } | |
5c005b37 | 819 | } |
97b71de5 RB |
820 | public function getBinetsNames() |
821 | { | |
875a88d3 RB |
822 | if ($this->visibility->isVisible(ProfileVisibility::VIS_PRIVATE)) { |
823 | return XDB::fetchColumn('SELECT text | |
824 | FROM profile_binets AS pb | |
825 | LEFT JOIN profile_binet_enum AS pbe ON (pbe.id = pb.binet_id) | |
826 | WHERE pb.pid = {?}', $this->id()); | |
827 | } else { | |
828 | return array(); | |
829 | } | |
97b71de5 | 830 | } |
5c005b37 | 831 | |
26ca919b RB |
832 | /* Medals |
833 | */ | |
834 | private $medals = null; | |
835 | public function setMedals(ProfileMedals $medals) | |
836 | { | |
837 | $this->medals = $medals; | |
838 | } | |
839 | ||
840 | public function getMedals() | |
841 | { | |
1f8250e4 RB |
842 | if ($this->medals == null && !$this->fetched(self::FETCH_MEDALS)) { |
843 | $this->setMedals($this->getProfileField(self::FETCH_MEDALS)); | |
26ca919b RB |
844 | } |
845 | if ($this->medals == null) { | |
846 | return array(); | |
847 | } | |
848 | return $this->medals->medals; | |
849 | } | |
e718bd18 | 850 | |
94590511 SJ |
851 | public function compareNames($firstname, $lastname) |
852 | { | |
853 | $_lastname = mb_strtoupper($this->lastName()); | |
854 | $_firstname = mb_strtoupper($this->firstName()); | |
855 | $lastname = mb_strtoupper($lastname); | |
856 | $firstname = mb_strtoupper($firstname); | |
857 | ||
858 | $isOk = (mb_strtoupper($_firstname) == mb_strtoupper($firstname)); | |
859 | $tokens = preg_split("/[ \-']/", $lastname, -1, PREG_SPLIT_NO_EMPTY); | |
860 | $maxlen = 0; | |
861 | ||
862 | foreach ($tokens as $str) { | |
863 | $isOk &= (strpos($_lastname, $str) !== false); | |
864 | $maxlen = max($maxlen, strlen($str)); | |
865 | } | |
866 | ||
867 | return ($isOk && ($maxlen > 2 || $maxlen == strlen($_lastname))); | |
868 | } | |
869 | ||
aaffb7b2 FB |
870 | /* Export to JSON |
871 | */ | |
872 | public function export() | |
873 | { | |
874 | return array( | |
875 | 'hrpid' => $this->hrid(), | |
876 | 'display_name' => $this->shortName(), | |
877 | 'full_name' => $this->fullName(), | |
878 | 'directory_name' => $this->directory_name, | |
879 | 'promo' => $this->promo(), | |
880 | 'year_promo' => $this->yearpromo(), | |
881 | 'is_active' => $this->isActive(), | |
882 | 'first_name' => $this->firstName(), | |
883 | 'last_name' => $this->lastName(), | |
884 | 'is_female' => $this->isFemale(), | |
885 | ); | |
886 | } | |
887 | ||
9f21bd15 | 888 | private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
b774ddab FB |
889 | { |
890 | if (count($pids) == 0) { | |
7a8da8e8 | 891 | return null; |
b774ddab | 892 | } |
0d906109 RB |
893 | |
894 | if ($respect_order) { | |
895 | $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids); | |
896 | } else { | |
897 | $order = ''; | |
898 | } | |
899 | ||
1a9affb7 | 900 | $visibility = new ProfileVisibility($visibility); |
9f21bd15 | 901 | |
3e2442cd RB |
902 | $it = XDB::Iterator('SELECT p.pid, p.hrpid, p.xorg_id, p.ax_id, p.birthdate, p.birthdate_ref, |
903 | p.next_birthday, p.deathdate, p.deathdate_rec, p.sex = \'female\' AS sex, | |
77d2ae52 RB |
904 | IF ({?}, p.cv, NULL) AS cv, p.medals_pub, p.alias_pub, p.email_directory, |
905 | p.last_change, p.nationality1, p.nationality2, p.nationality3, | |
3e2442cd | 906 | IF (p.freetext_pub IN {?}, p.freetext, NULL) AS freetext, |
d718e91c | 907 | pe.entry_year, pe.grad_year, pe.promo_year, pe.program, pe.fieldid, |
348b3844 RB |
908 | IF ({?}, pse.text, NULL) AS section, |
909 | pn_f.name AS firstname, pn_l.name AS lastname, | |
910 | IF( {?}, pn_n.name, NULL) AS nickname, | |
9f21bd15 RB |
911 | IF(pn_uf.name IS NULL, pn_f.name, pn_uf.name) AS firstname_ordinary, |
912 | IF(pn_ul.name IS NULL, pn_l.name, pn_ul.name) AS lastname_ordinary, | |
09e54905 SJ |
913 | pd.yourself, pd.promo, pd.short_name, pd.public_name AS full_name, |
914 | pd.directory_name, pd.public_name, pd.private_name, | |
915 | IF(pp.pub IN {?}, pp.display_tel, NULL) AS mobile, | |
1a9affb7 | 916 | (ph.pub IN {?} AND ph.attach IS NOT NULL) AS has_photo, |
7988f7d6 | 917 | ph.x AS photo_width, ph.y AS photo_height, |
9f21bd15 | 918 | p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old, |
bdef0d33 | 919 | pm.expertise AS mentor_expertise, |
9f21bd15 RB |
920 | ap.uid AS owner_id |
921 | FROM profiles AS p | |
922 | INNER JOIN profile_display AS pd ON (pd.pid = p.pid) | |
923 | INNER JOIN profile_education AS pe ON (pe.pid = p.pid AND FIND_IN_SET(\'primary\', pe.flags)) | |
97b71de5 | 924 | LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section) |
9f21bd15 RB |
925 | INNER JOIN profile_name AS pn_f ON (pn_f.pid = p.pid |
926 | AND pn_f.typeid = ' . self::getNameTypeId('firstname', true) . ') | |
927 | INNER JOIN profile_name AS pn_l ON (pn_l.pid = p.pid | |
928 | AND pn_l.typeid = ' . self::getNameTypeId('lastname', true) . ') | |
929 | LEFT JOIN profile_name AS pn_uf ON (pn_uf.pid = p.pid | |
930 | AND pn_uf.typeid = ' . self::getNameTypeId('firstname_ordinary', true) . ') | |
931 | LEFT JOIN profile_name AS pn_ul ON (pn_ul.pid = p.pid | |
932 | AND pn_ul.typeid = ' . self::getNameTypeId('lastname_ordinary', true) . ') | |
933 | LEFT JOIN profile_name AS pn_n ON (pn_n.pid = p.pid | |
934 | AND pn_n.typeid = ' . self::getNameTypeId('nickname', true) . ') | |
935 | LEFT JOIN profile_phones AS pp ON (pp.pid = p.pid AND pp.link_type = \'user\' AND tel_type = \'mobile\') | |
936 | LEFT JOIN profile_photos AS ph ON (ph.pid = p.pid) | |
bdef0d33 | 937 | LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid) |
9f21bd15 | 938 | LEFT JOIN account_profiles AS ap ON (ap.pid = p.pid AND FIND_IN_SET(\'owner\', ap.perms)) |
3e2442cd | 939 | WHERE p.pid IN {?} |
9f21bd15 | 940 | GROUP BY p.pid |
3e2442cd | 941 | ' . $order, |
77d2ae52 RB |
942 | $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // CV |
943 | $visibility->levels(), // freetext | |
944 | $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // section | |
945 | $visibility->isVisible(ProfileVisibility::VIS_PRIVATE), // nickname | |
946 | $visibility->levels(), // mobile | |
947 | $visibility->levels(), // photo | |
3e2442cd RB |
948 | $pids |
949 | ); | |
31ef12ef | 950 | return new ProfileIterator($it, $pids, $fields, $visibility); |
b774ddab FB |
951 | } |
952 | ||
953 | public static function getPID($login) | |
954 | { | |
955 | if ($login instanceof PlUser) { | |
956 | return XDB::fetchOneCell('SELECT pid | |
957 | FROM account_profiles | |
958 | WHERE uid = {?} AND FIND_IN_SET(\'owner\', perms)', | |
959 | $login->id()); | |
9f21bd15 | 960 | } else if (ctype_digit($login)) { |
b774ddab FB |
961 | return XDB::fetchOneCell('SELECT pid |
962 | FROM profiles | |
963 | WHERE pid = {?}', $login); | |
964 | } else { | |
965 | return XDB::fetchOneCell('SELECT pid | |
966 | FROM profiles | |
967 | WHERE hrpid = {?}', $login); | |
968 | } | |
969 | } | |
970 | ||
0d906109 RB |
971 | public static function getPIDsFromUIDs($uids, $respect_order = true) |
972 | { | |
973 | if ($respect_order) { | |
974 | $order = 'ORDER BY ' . XDB::formatCustomOrder('uid', $uids); | |
975 | } else { | |
976 | $order = ''; | |
977 | } | |
978 | return XDB::fetchAllAssoc('uid', 'SELECT ap.uid, ap.pid | |
979 | FROM account_profiles AS ap | |
980 | WHERE FIND_IN_SET(\'owner\', ap.perms) | |
9f21bd15 RB |
981 | AND ap.uid IN ' . XDB::formatArray($uids) .' |
982 | ' . $order); | |
0d906109 | 983 | } |
b774ddab | 984 | |
e7b93962 FB |
985 | /** Return the profile associated with the given login. |
986 | */ | |
9f21bd15 | 987 | public static function get($login, $fields = 0x0000, $visibility = null) |
a3118782 | 988 | { |
641f2115 | 989 | if (is_array($login)) { |
1a9affb7 RB |
990 | $pf = new Profile($login); |
991 | $pf->setVisibilityLevel($visibility); | |
992 | return $pf; | |
641f2115 | 993 | } |
b774ddab FB |
994 | $pid = self::getPID($login); |
995 | if (!is_null($pid)) { | |
9f21bd15 | 996 | $it = self::iterOverPIDs(array($pid), false, $fields, $visibility); |
0d906109 | 997 | return $it->next(); |
b774ddab | 998 | } else { |
efe597c5 FB |
999 | /* Let say we can identify a profile using the identifiers of its owner. |
1000 | */ | |
455ea0c9 FB |
1001 | if (!($login instanceof PlUser)) { |
1002 | $user = User::getSilent($login); | |
1003 | if ($user && $user->hasProfile()) { | |
1004 | return $user->profile(); | |
1005 | } | |
efe597c5 | 1006 | } |
3e53a496 | 1007 | return null; |
e7b93962 FB |
1008 | } |
1009 | } | |
a3118782 | 1010 | |
9f21bd15 | 1011 | public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 1012 | { |
9f21bd15 | 1013 | return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility); |
0d906109 RB |
1014 | } |
1015 | ||
9f21bd15 | 1016 | public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null) |
0d906109 | 1017 | { |
9f21bd15 | 1018 | return self::fetchProfileData($pids, $respect_order, $fields, $visibility); |
0d906109 RB |
1019 | } |
1020 | ||
b774ddab FB |
1021 | /** Return profiles for the list of pids. |
1022 | */ | |
1a9affb7 | 1023 | public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null) |
b774ddab FB |
1024 | { |
1025 | if (count($pids) == 0) { | |
1026 | return array(); | |
1027 | } | |
9f21bd15 | 1028 | $it = self::iterOverPIDs($pids, true, $fields, $visibility); |
b774ddab | 1029 | $profiles = array(); |
0d906109 RB |
1030 | while ($p = $it->next()) { |
1031 | $profiles[$p->id()] = $p; | |
b774ddab FB |
1032 | } |
1033 | return $profiles; | |
1034 | } | |
1035 | ||
1036 | /** Return profiles for uids. | |
1037 | */ | |
9f21bd15 | 1038 | public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null) |
b774ddab FB |
1039 | { |
1040 | if (count($uids) == 0) { | |
1041 | return array(); | |
1042 | } | |
9f21bd15 | 1043 | return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility); |
b774ddab FB |
1044 | } |
1045 | ||
913a4e90 RB |
1046 | public static function isDisplayName($name) |
1047 | { | |
1048 | return $name == self::DN_FULL || $name == self::DN_DISPLAY | |
1049 | || $name == self::DN_YOURSELF || $name == self::DN_DIRECTORY | |
1050 | || $name == self::DN_PRIVATE || $name == self::DN_PUBLIC | |
1051 | || $name == self::DN_SHORT || $name == self::DN_SORT; | |
1052 | } | |
1053 | ||
a9ef52c9 RB |
1054 | /** Returns the closest "accounts only" name type for $name |
1055 | */ | |
1056 | public static function getAccountEquivalentName($name) | |
1057 | { | |
1058 | switch ($name) | |
1059 | { | |
1060 | case self::DN_DIRECTORY: | |
1061 | case self::DN_SORT: | |
1062 | return 'directory_name'; | |
1063 | case self::DN_FULL: | |
1064 | case self::DN_PUBLIC: | |
1065 | return 'full_name'; | |
1066 | case self::DN_PRIVATE: | |
1067 | case self::DN_SHORT: | |
1068 | case self::DN_YOURSELF: | |
1069 | default: | |
1070 | return 'display_name'; | |
1071 | } | |
1072 | } | |
1073 | ||
9f21bd15 RB |
1074 | public static function getNameTypeId($type, $for_sql = false) |
1075 | { | |
1076 | if (!S::has('name_types')) { | |
1077 | $table = XDB::fetchAllAssoc('type', 'SELECT id, type | |
1078 | FROM profile_name_enum'); | |
1079 | S::set('name_types', $table); | |
1080 | } else { | |
1081 | $table = S::v('name_types'); | |
1082 | } | |
1083 | if ($for_sql) { | |
1084 | return XDB::escape($table[$type]); | |
1085 | } else { | |
1086 | return $table[$type]; | |
1087 | } | |
1088 | } | |
1089 | ||
bedc4295 | 1090 | public static function rebuildSearchTokens($pids, $transaction = true) |
726eaf7a | 1091 | { |
5acd53a9 FB |
1092 | if (!is_array($pids)) { |
1093 | $pids = array($pids); | |
1094 | } | |
80bc45c2 FB |
1095 | $keys = XDB::iterator("(SELECT n.pid AS pid, n.name AS name, e.score AS score, |
1096 | IF(FIND_IN_SET('public', e.flags), 'public', '') AS public | |
1097 | FROM profile_name AS n | |
1098 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
1099 | WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags)) | |
1100 | UNION | |
1101 | (SELECT n.pid AS pid, n.particle AS name, 0 AS score, | |
1102 | IF(FIND_IN_SET('public', e.flags), 'public', '') AS public | |
1103 | FROM profile_name AS n | |
1104 | INNER JOIN profile_name_enum AS e ON (n.typeid = e.id) | |
1105 | WHERE n.pid IN {?} AND NOT FIND_IN_SET('not_displayed', e.flags)) | |
1106 | ", | |
1107 | $pids, $pids); | |
5acd53a9 | 1108 | $names = array(); |
2f62eba7 | 1109 | while ($key = $keys->next()) { |
726eaf7a SJ |
1110 | if ($key['name'] == '') { |
1111 | continue; | |
1112 | } | |
5acd53a9 | 1113 | $pid = $key['pid']; |
5ed1cce8 RB |
1114 | require_once 'name.func.inc.php'; |
1115 | $toks = split_name_for_search($key['name']); | |
80bc45c2 FB |
1116 | $toks = array_reverse($toks); |
1117 | ||
1118 | /* Split the score between the tokens to avoid the user to be over-rated. | |
1119 | * Let says my user name is "Machin-Truc Bidule" and I also have a user named | |
1120 | * 'Machin Truc'. Distributing the score force "Machin Truc" to be displayed | |
1121 | * before "Machin-Truc" for both "Machin Truc" and "Machin" searches. | |
1122 | */ | |
1123 | $eltScore = ceil(((float)$key['score'])/((float)count($toks))); | |
726eaf7a | 1124 | $token = ''; |
80bc45c2 FB |
1125 | foreach ($toks as $tok) { |
1126 | $token = $tok . $token; | |
5acd53a9 FB |
1127 | $names["$pid-$token"] = XDB::format('({?}, {?}, {?}, {?}, {?})', |
1128 | $token, $pid, soundex_fr($token), | |
80bc45c2 | 1129 | $eltScore, $key['public']); |
726eaf7a SJ |
1130 | } |
1131 | } | |
bedc4295 FB |
1132 | if ($transaction) { |
1133 | XDB::startTransaction(); | |
1134 | } | |
5acd53a9 FB |
1135 | XDB::execute('DELETE FROM search_name |
1136 | WHERE pid IN {?}', | |
1137 | $pids); | |
1138 | if (count($names) > 0) { | |
80bc45c2 FB |
1139 | XDB::rawExecute('INSERT INTO search_name (token, pid, soundex, score, flags) |
1140 | VALUES ' . implode(', ', $names)); | |
5acd53a9 | 1141 | } |
bedc4295 FB |
1142 | if ($transaction) { |
1143 | XDB::commit(); | |
1144 | } | |
69b46857 SJ |
1145 | } |
1146 | ||
1147 | /** The school identifier consists of 6 digits. The first 3 represent the | |
1148 | * promotion entry year. The last 3 indicate the student's rank. | |
aab2ffdd | 1149 | * |
69b46857 SJ |
1150 | * Our identifier consists of 8 digits and both half have the same role. |
1151 | * This enables us to deal with bigger promotions and with a wider range | |
1152 | * of promotions. | |
1153 | * | |
1154 | * getSchoolId returns a school identifier given one of ours. | |
1155 | * getXorgId returns a X.org identifier given a school identifier. | |
1156 | */ | |
1157 | public static function getSchoolId($xorgId) | |
1158 | { | |
1159 | if (!preg_match('/^[0-9]{8}$/', $xorgId)) { | |
1160 | return null; | |
1161 | } | |
1162 | ||
1163 | $year = intval(substr($xorgId, 0, 4)); | |
1164 | $rank = intval(substr($xorgId, 5, 3)); | |
1165 | if ($year < 1996) { | |
1166 | return null; | |
1167 | } elseif ($year < 2000) { | |
1168 | $year = intval(substr(1900 - $year, 1, 3)); | |
1169 | return sprintf('%02u0%03u', $year, $rank); | |
1170 | } else { | |
1171 | $year = intval(substr(1900 - $year, 1, 3)); | |
1172 | return sprintf('%03u%03u', $year, $rank); | |
1173 | } | |
1174 | } | |
726eaf7a | 1175 | |
69b46857 SJ |
1176 | public static function getXorgId($schoolId) |
1177 | { | |
94590511 | 1178 | if (!preg_match('/^[0-9]{6}$/', $schoolId)) { |
a292484d SJ |
1179 | return null; |
1180 | } | |
1181 | ||
69b46857 SJ |
1182 | $year = intval(substr($schoolId, 0, 3)); |
1183 | $rank = intval(substr($schoolId, 3, 3)); | |
726eaf7a | 1184 | |
69b46857 SJ |
1185 | if ($year > 200) { |
1186 | $year /= 10; | |
1187 | } | |
1188 | if ($year < 96) { | |
1189 | return null; | |
1190 | } else { | |
1191 | return sprintf('%04u%04u', 1900 + $year, $rank); | |
1192 | } | |
726eaf7a | 1193 | } |
e7b93962 FB |
1194 | } |
1195 | ||
31ef12ef RB |
1196 | |
1197 | /** Iterator over a set of Profiles | |
1198 | */ | |
1199 | class ProfileIterator implements PlIterator | |
9f21bd15 RB |
1200 | { |
1201 | private $iterator = null; | |
1202 | private $fields; | |
1a9affb7 | 1203 | private $visibility; |
9f21bd15 | 1204 | |
3ac45f10 PC |
1205 | const FETCH_ALL = 0x000033F; // FETCH_ADDRESSES | FETCH_CORPS | FETCH_EDU | FETCH_JOBS | FETCH_MEDALS | FETCH_NETWORKING | FETCH_PHONES | FETCH_JOB_TERMS |
1206 | ||
1a9affb7 | 1207 | public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null) |
9f21bd15 RB |
1208 | { |
1209 | require_once 'profilefields.inc.php'; | |
1a9affb7 RB |
1210 | |
1211 | if ($visibility == null) { | |
1212 | $visibility = new ProfileVisibility(); | |
1213 | } | |
1214 | ||
9f21bd15 | 1215 | $this->fields = $fields; |
1a9affb7 | 1216 | $this->visibility = $visibility; |
9f21bd15 RB |
1217 | |
1218 | $subits = array(); | |
1219 | $callbacks = array(); | |
1220 | ||
1221 | $subits[0] = $it; | |
1222 | $callbacks[0] = PlIteratorUtils::arrayValueCallback('pid'); | |
1223 | $cb = PlIteratorUtils::objectPropertyCallback('pid'); | |
1224 | ||
3ac45f10 PC |
1225 | $fields = $fields & self::FETCH_ALL; |
1226 | for ($field = 1; $field < $fields; $field *= 2) { | |
1227 | if (($fields & $field) ) { | |
1228 | $callbacks[$field] = $cb; | |
1229 | $subits[$field] = new ProfileFieldIterator($field, $pids, $visibility); | |
1230 | } | |
9f21bd15 RB |
1231 | } |
1232 | ||
9f21bd15 RB |
1233 | $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0); |
1234 | } | |
1235 | ||
1f8250e4 | 1236 | private function hasData($field, $vals) |
9f21bd15 | 1237 | { |
1f8250e4 | 1238 | return ($this->fields & $field) && ($vals[$field] != null); |
9f21bd15 RB |
1239 | } |
1240 | ||
1241 | private function fillProfile(array $vals) | |
1242 | { | |
9f21bd15 | 1243 | $pf = Profile::get($vals[0]); |
1a9affb7 | 1244 | $pf->setVisibilityLevel($this->visibility->level()); |
1f8250e4 | 1245 | $pf->setFetchedFields($this->fields); |
1a9affb7 | 1246 | |
8cf886dc RB |
1247 | if ($this->hasData(Profile::FETCH_PHONES, $vals)) { |
1248 | $pf->setPhones($vals[Profile::FETCH_PHONES]); | |
9f21bd15 | 1249 | } |
8cf886dc RB |
1250 | if ($this->hasData(Profile::FETCH_ADDRESSES, $vals)) { |
1251 | $pf->setAddresses($vals[Profile::FETCH_ADDRESSES]); | |
1252 | } | |
1253 | if ($this->hasData(Profile::FETCH_JOBS, $vals)) { | |
1254 | $pf->setJobs($vals[Profile::FETCH_JOBS]); | |
1255 | } | |
3ac45f10 PC |
1256 | if ($this->hasData(Profile::FETCH_JOB_TERMS, $vals)) { |
1257 | $pf->setJobTerms($vals[Profile::FETCH_JOB_TERMS]); | |
1258 | } | |
8cf886dc RB |
1259 | |
1260 | if ($this->hasData(Profile::FETCH_CORPS, $vals)) { | |
9f21bd15 RB |
1261 | $pf->setCorps($vals[Profile::FETCH_CORPS]); |
1262 | } | |
8cf886dc | 1263 | if ($this->hasData(Profile::FETCH_EDU, $vals)) { |
26ca919b | 1264 | $pf->setEducations($vals[Profile::FETCH_EDU]); |
9f21bd15 | 1265 | } |
8cf886dc | 1266 | if ($this->hasData(Profile::FETCH_MEDALS, $vals)) { |
9f21bd15 RB |
1267 | $pf->setMedals($vals[Profile::FETCH_MEDALS]); |
1268 | } | |
8cf886dc | 1269 | if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) { |
9f21bd15 RB |
1270 | $pf->setNetworking($vals[Profile::FETCH_NETWORKING]); |
1271 | } | |
9f21bd15 RB |
1272 | |
1273 | return $pf; | |
1274 | } | |
1275 | ||
1276 | public function next() | |
1277 | { | |
1278 | $vals = $this->iterator->next(); | |
1279 | if ($vals == null) { | |
1280 | return null; | |
1281 | } | |
1282 | return $this->fillProfile($vals); | |
1283 | } | |
1284 | ||
1285 | public function first() | |
1286 | { | |
1287 | return $this->iterator->first(); | |
1288 | } | |
1289 | ||
1290 | public function last() | |
1291 | { | |
1292 | return $this->iterator->last(); | |
1293 | } | |
1294 | ||
1295 | public function total() | |
1296 | { | |
1297 | return $this->iterator->total(); | |
1298 | } | |
1299 | } | |
1300 | ||
e7b93962 FB |
1301 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
1302 | ?> |