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