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