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