Fix minor Smarty warnings in minifiche.tpl
[platal.git] / classes / profile.php
... / ...
CommitLineData
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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 ProfileVisibility
23{
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;
33
34 public function __construct($level = null)
35 {
36 $this->setLevel($level);
37 }
38
39 public function setLevel($level = self::VIS_PUBLIC)
40 {
41 if ($level != null && $level != self::VIS_PRIVATE && $level != self::VIS_AX && $level != self::VIS_PUBLIC) {
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{
82
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';
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';
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
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;
121 const EDUCATION_EXTRA = 0x000020;
122 const EDUCATION_ALL = 0x000040;
123 const EDUCATION_FINISHED = 0x000080;
124 const EDUCATION_CURRENT = 0x000100;
125
126 const JOBS_MAIN = 0x001000;
127 const JOBS_ALL = 0x002000;
128 const JOBS_FINISHED = 0x004000;
129 const JOBS_CURRENT = 0x008000;
130
131 const NETWORKING_ALL = 0x000000;
132 const NETWORKING_WEB = 0x010000;
133 const NETWORKING_IM = 0x020000;
134 const NETWORKING_SOCIAL = 0x040000;
135
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;
143
144 const FETCH_MINIFICHES = 0x00006D; // FETCH_ADDRESSES | FETCH_EDU | FETCH_JOBS | FETCH_NETWORKING | FETCH_PHONES
145
146 const FETCH_ALL = 0x0000FF; // OR of FETCH_*
147
148 private $fetched_fields = 0x000000;
149
150 private $pid;
151 private $hrpid;
152 private $data = array();
153
154 private $visibility = null;
155
156
157 private function __construct(array $data)
158 {
159 $this->data = $data;
160 $this->pid = $this->data['pid'];
161 $this->hrpid = $this->data['hrpid'];
162 $this->visibility = new ProfileVisibility();
163 }
164
165 public function id()
166 {
167 return $this->pid;
168 }
169
170 public function hrid()
171 {
172 return $this->hrpid;
173 }
174
175 public function promo()
176 {
177 return $this->promo;
178 }
179
180 public function yearpromo()
181 {
182 return intval(substr($this->promo, 1, 4));
183 }
184
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() ? '•' : '',
197 $this->firstName(), $this->lastName(),
198 $this->fullName(), $this->shortName(),
199 $this->promo()), $format);
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 {
220 return $this->firstname;
221 }
222
223 public function firstNames()
224 {
225 return $this->nameVariants(self::FIRSTNAME);
226 }
227
228 public function lastName()
229 {
230 return $this->lastname;
231 }
232
233 public function lastNames()
234 {
235 return $this->nameVariants(self::LASTNAME);
236 }
237
238 public function isFemale()
239 {
240 return $this->sex == PlUser::GENDER_FEMALE;
241 }
242
243 public function isDead()
244 {
245 return ($this->deathdate != null);
246 }
247
248 public function data()
249 {
250 $this->first_name;
251 return $this->data;
252 }
253
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
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
282 public function __get($name)
283 {
284 if (property_exists($this, $name)) {
285 return $this->$name;
286 }
287
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
300 /** Sets the level of visibility of the profile
301 * Sets $this->visibility to a list of valid visibilities.
302 * @param one of the self::VIS_* values
303 */
304 public function setVisibilityLevel($visibility)
305 {
306 $this->visibility->setLevel($visibility);
307 }
308
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 {
315 return $this->visibility->isVisible($visibility);
316 }
317
318 /** Stores the list of fields which have already been fetched for this Profile
319 */
320 public function setFetchedFields($fields)
321 {
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
352 return ProfileField::getForPID($cls, $this->id(), $this->visibility);
353 }
354
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
374 /* Photo
375 */
376 private $photo = null;
377 public function getPhoto($fallback = true, $data = false)
378 {
379 if ($this->has_photo) {
380 if ($data && ($this->photo == null || $this->photo->mimeType == null)) {
381 $res = XDB::fetchOneAssoc('SELECT attach, attachmime, x, y
382 FROM profile_photos
383 WHERE pid = {?}', $this->pid);
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;
389 } else if ($fallback) {
390 return PlImage::fromFile(dirname(__FILE__).'/../htdocs/images/none.png',
391 'image/png');
392 }
393 return null;
394 }
395
396 /* Addresses
397 */
398 private $addresses = null;
399 public function setAddresses(ProfileAddresses $addr)
400 {
401 $this->addresses = $addr;
402 $this->consolidateFields();
403 }
404
405 public function getAddresses($flags, $limit = null)
406 {
407 if ($this->addresses == null && !$this->fetched(self::FETCH_ADDRESSES)) {
408 $this->setAddresses($this->getProfileField(self::FETCH_ADDRESSES));
409 }
410
411 if ($this->addresses == null) {
412 return array();
413 }
414 return $this->addresses->get($flags, $limit);
415 }
416
417 public function iterAddresses($flags, $limit = null)
418 {
419 return PlIteratorUtils::fromArray($this->getAddresses($flags, $limit), 1, true);
420 }
421
422 public function getMainAddress()
423 {
424 $addr = $this->getAddresses(self::ADDRESS_PERSO | self::ADDRESS_MAIN);
425 if (count($addr) == 0) {
426 return null;
427 } else {
428 return array_pop($addr);
429 }
430 }
431
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 {
443 if ($this->phones == null && !$this->fetched(self::FETCH_PHONES)) {
444 $this->setPhones($this->getProfileField(self::FETCH_PHONES));
445 }
446
447 if ($this->phones == null) {
448 return array();
449 }
450 return $this->phones->get($flags, $limit);
451 }
452
453 /* Educations
454 */
455 private $educations = null;
456 public function setEducations(ProfileEducation $edu)
457 {
458 $this->educations = $edu;
459 }
460
461 public function getEducations($flags, $limit = null)
462 {
463 if ($this->educations == null && !$this->fetched(self::FETCH_EDU)) {
464 $this->setEducations($this->getProfileField(self::FETCH_EDU));
465 }
466
467 if ($this->educations == null) {
468 return array();
469 }
470 return $this->educations->get($flags, $limit);
471 }
472
473 public function getExtraEducations($limit = null)
474 {
475 return $this->getEducations(self::EDUCATION_EXTRA, $limit);
476 }
477
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 {
488 if ($this->corps == null && !$this->fetched(self::FETCH_CORPS)) {
489 $this->setCorps($this->getProfileField(self::FETCH_CORPS));
490 }
491 return $this->corps;
492 }
493
494 /** Networking
495 */
496 private $networks = null;
497 public function setNetworking(ProfileNetworking $nw)
498 {
499 $this->networks = $nw;
500 }
501
502 public function getNetworking($flags, $limit = null)
503 {
504 if ($this->networks == null && !$this->fetched(self::FETCH_NETWORKING)) {
505 $this->setNetworking($this->getProfileField(self::FETCH_NETWORKING));
506 }
507 if ($this->networks == null) {
508 return array();
509 }
510 return $this->networks->get($flags, $limit);
511 }
512
513 public function getWebSite()
514 {
515 $site = $this->getNetworking(self::NETWORKING_WEB, 1);
516 if (count($site) != 1) {
517 return null;
518 }
519 $site = array_pop($site);
520 return $site['address'];
521 }
522
523
524 /** Jobs
525 */
526 private $jobs = null;
527 public function setJobs(ProfileJobs $jobs)
528 {
529 $this->jobs = $jobs;
530 $this->consolidateFields();
531 }
532
533 public function getJobs($flags, $limit = null)
534 {
535 if ($this->jobs == null && !$this->fetched(self::FETCH_JOBS)) {
536 $this->setJobs($this->getProfileField(self::FETCH_JOBS));
537 }
538
539 if ($this->jobs == null) {
540 return array();
541 }
542 return $this->jobs->get($flags, $limit);
543 }
544
545 public function getMainJob()
546 {
547 $job = $this->getJobs(self::JOBS_MAIN, 1);
548 if (count($job) != 1) {
549 return null;
550 }
551 return array_pop($job);
552 }
553
554 /* Binets
555 */
556 public function getBinets()
557 {
558 return XDB::fetchColumn('SELECT binet_id
559 FROM profile_binets
560 WHERE pid = {?}', $this->id());
561 }
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 }
569
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 {
580 if ($this->medals == null && !$this->fetched(self::FETCH_MEDALS)) {
581 $this->setMedals($this->getProfileField(self::FETCH_MEDALS));
582 }
583 if ($this->medals == null) {
584 return array();
585 }
586 return $this->medals->medals;
587 }
588
589 public function owner()
590 {
591 return User::getSilent($this);
592 }
593
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
613 private static function fetchProfileData(array $pids, $respect_order = true, $fields = 0x0000, $visibility = null)
614 {
615 if (count($pids) == 0) {
616 return array();
617 }
618
619 if ($respect_order) {
620 $order = 'ORDER BY ' . XDB::formatCustomOrder('p.pid', $pids);
621 } else {
622 $order = '';
623 }
624
625 $visibility = new ProfileVisibility($visibility);
626
627 $it = XDB::Iterator('SELECT p.*, p.sex = \'female\' AS sex, pe.entry_year, pe.grad_year, pse.text AS section,
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,
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,
634 ph.x AS photo_width, ph.y AS photo_height,
635 p.last_change < DATE_SUB(NOW(), INTERVAL 365 DAY) AS is_old,
636 pm.expertise AS mentor_expertise,
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))
641 LEFT JOIN profile_section_enum AS pse ON (pse.id = p.section)
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)
654 LEFT JOIN profile_mentor AS pm ON (pm.pid = p.pid)
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
658 ' . $order, $visibility->levels(), $visibility->levels());
659 return new ProfileIterator($it, $pids, $fields, $visibility);
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());
669 } else if (ctype_digit($login)) {
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
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)
690 AND ap.uid IN ' . XDB::formatArray($uids) .'
691 ' . $order);
692 }
693
694 /** Return the profile associated with the given login.
695 */
696 public static function get($login, $fields = 0x0000, $visibility = null)
697 {
698 if (is_array($login)) {
699 $pf = new Profile($login);
700 $pf->setVisibilityLevel($visibility);
701 return $pf;
702 }
703 $pid = self::getPID($login);
704 if (!is_null($pid)) {
705 $it = self::iterOverPIDs(array($pid), false, $fields, $visibility);
706 return $it->next();
707 } else {
708 /* Let say we can identify a profile using the identifiers of its owner.
709 */
710 if (!($login instanceof PlUser)) {
711 $user = User::getSilent($login);
712 if ($user && $user->hasProfile()) {
713 return $user->profile();
714 }
715 }
716 return null;
717 }
718 }
719
720 public static function iterOverUIDs($uids, $respect_order = true, $fields = 0x0000, $visibility = null)
721 {
722 return self::iterOverPIDs(self::getPIDsFromUIDs($uids), $respect_order, $fields, $visibility);
723 }
724
725 public static function iterOverPIDs($pids, $respect_order = true, $fields = 0x0000, $visibility = null)
726 {
727 return self::fetchProfileData($pids, $respect_order, $fields, $visibility);
728 }
729
730 /** Return profiles for the list of pids.
731 */
732 public static function getBulkProfilesWithPIDs(array $pids, $fields = 0x0000, $visibility = null)
733 {
734 if (count($pids) == 0) {
735 return array();
736 }
737 $it = self::iterOverPIDs($pids, true, $fields, $visibility);
738 $profiles = array();
739 while ($p = $it->next()) {
740 $profiles[$p->id()] = $p;
741 }
742 return $profiles;
743 }
744
745 /** Return profiles for uids.
746 */
747 public static function getBulkProfilesWithUIDS(array $uids, $fields = 0x000, $visibility = null)
748 {
749 if (count($uids) == 0) {
750 return array();
751 }
752 return self::getBulkProfilesWithPIDs(self::getPIDsFromUIDs($uids), $fields, $visibility);
753 }
754
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
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
779 public static function rebuildSearchTokens($pid)
780 {
781 XDB::execute('DELETE FROM search_name
782 WHERE pid = {?}',
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);
801 XDB::execute('REPLACE INTO search_name (token, pid, soundex, score, flags)
802 VALUES ({?}, {?}, {?}, {?}, {?})',
803 $token, $pid, soundex_fr($token), $score, $key['public']);
804 $first = 0;
805 }
806 }
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 }
837
838 public static function getXorgId($schoolId)
839 {
840 if (!preg_match('/^[0-9]{6}$/', $schoolId)) {
841 return null;
842 }
843
844 $year = intval(substr($schoolId, 0, 3));
845 $rank = intval(substr($schoolId, 3, 3));
846
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 }
855 }
856}
857
858
859/** Iterator over a set of Profiles
860 */
861class ProfileIterator implements PlIterator
862{
863 private $iterator = null;
864 private $fields;
865 private $visibility;
866
867 public function __construct(PlIterator $it, array $pids, $fields = 0x0000, ProfileVisibility $visibility = null)
868 {
869 require_once 'profilefields.inc.php';
870
871 if ($visibility == null) {
872 $visibility = new ProfileVisibility();
873 }
874
875 $this->fields = $fields;
876 $this->visibility = $visibility;
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
920 $this->iterator = PlIteratorUtils::parallelIterator($subits, $callbacks, 0);
921 }
922
923 private function hasData($field, $vals)
924 {
925 return ($this->fields & $field) && ($vals[$field] != null);
926 }
927
928 private function fillProfile(array $vals)
929 {
930 $pf = Profile::get($vals[0]);
931 $pf->setVisibilityLevel($this->visibility->level());
932 $pf->setFetchedFields($this->fields);
933
934 if ($this->hasData(Profile::FETCH_PHONES, $vals)) {
935 $pf->setPhones($vals[Profile::FETCH_PHONES]);
936 }
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)) {
945 $pf->setCorps($vals[Profile::FETCH_CORPS]);
946 }
947 if ($this->hasData(Profile::FETCH_EDU, $vals)) {
948 $pf->setEducations($vals[Profile::FETCH_EDU]);
949 }
950 if ($this->hasData(Profile::FETCH_MEDALS, $vals)) {
951 $pf->setMedals($vals[Profile::FETCH_MEDALS]);
952 }
953 if ($this->hasData(Profile::FETCH_NETWORKING, $vals)) {
954 $pf->setNetworking($vals[Profile::FETCH_NETWORKING]);
955 }
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
985// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
986?>