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