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