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