Moving to GitHub.
[platal.git] / include / profilefields.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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 ProfileField
23 /** To store a "field" from the profile
24 * Provides functions for loading a batch of such data
25 */
26 abstract class ProfileField
27 {
28 public static $fields = array(
29 Profile::FETCH_ADDRESSES => 'ProfileAddresses',
30 Profile::FETCH_CORPS => 'ProfileCorps',
31 Profile::FETCH_EDU => 'ProfileEducation',
32 Profile::FETCH_JOBS => 'ProfileJobs',
33 Profile::FETCH_MEDALS => 'ProfileMedals',
34 Profile::FETCH_NETWORKING => 'ProfileNetworking',
35 Profile::FETCH_PHONES => 'ProfilePhones',
36 Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries',
37 Profile::FETCH_JOB_TERMS => 'ProfileJobTerms',
38 Profile::FETCH_MENTOR_TERMS => 'ProfileMentoringTerms',
39 Profile::FETCH_SKILL => 'ProfileSkills',
40 Profile::FETCH_LANGUAGE => 'ProfileLanguages',
41 Profile::FETCH_PARTNER => 'ProfilePartnerSharing',
42 );
43
44 /** The profile to which this field belongs
45 */
46 public $pid;
47
48 /** Fetches data from the database for the given pids, compatible with
49 * the visibility context.
50 * @param $pids An array of pids
51 * @param $visibility The level of visibility fetched fields must have
52 * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
53 *
54 * MUST be reimplemented for each kind of ProfileField.
55 */
56 public static function fetchData(array $pids, Visibility $visibility)
57 {
58 return PlIteratorUtils::emptyIterator();
59 }
60
61 public static function buildForPID($cls, $pid, Visibility $visibility)
62 {
63 $res = self::buildFromPIDs($cls, array($pid), $visibility);
64 return array_pop($res);
65 }
66
67 /** Build a list of ProfileFields from a set of pids
68 * @param $cls The name of the field to create ('ProfileMedals', ...)
69 * @param $pids An array of pids
70 * @param $visibility An array of allowed visibility contexts
71 * @return An array of $pid => ProfileField
72 */
73 public static function buildFromPIDs($cls, array $pids, Visibility $visibility)
74 {
75 $it = new ProfileFieldIterator($cls, $pids, $visibility);
76 $res = array();
77 while ($pf = $it->next()) {
78 $res[$pf->pid] = $pf;
79 }
80 return $res;
81 }
82
83 public static function getForPID($cls, $pid, Visibility $visibility)
84 {
85 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
86 return $it->next();
87 }
88 }
89 // }}}
90
91 // {{{ class ProfileFieldIterator
92 class ProfileFieldIterator implements PlIterator
93 {
94 private $data;
95 private $cls;
96
97 public function __construct($cls, array $pids, Visibility $visibility)
98 {
99 if (is_numeric($cls) && isset(ProfileField::$fields[$cls])) {
100 $cls = ProfileField::$fields[$cls];
101 }
102 $this->data = call_user_func(array($cls, 'fetchData'), $pids, $visibility);
103 $this->cls = $cls;
104 }
105
106 public function next()
107 {
108 $d = $this->data->next();
109 if ($d == null) {
110 return null;
111 } else {
112 $cls = $this->cls;
113 return new $cls($d);
114 }
115 }
116
117 public function total()
118 {
119 return $this->data->total();
120 }
121
122 public function first()
123 {
124 return $this->data->first();
125 }
126
127 public function last()
128 {
129 return $this->data->last();
130 }
131 }
132 // }}}
133
134 // {{{ class Company
135 class Company
136 {
137 public $id;
138 public $name;
139 public $acronym;
140 public $url;
141 public $phone = null;
142 public $address = null;
143
144 /** Fields are:
145 * $id, $name, $acronym, $url
146 */
147 public function __construct($data)
148 {
149 foreach ($data as $key => $val) {
150 $this->$key = $val;
151 }
152 }
153
154 public function setPhone(Phone $phone)
155 {
156 if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
157 $this->phone = $phone;
158 }
159 }
160
161 public function setAddress(Address $address)
162 {
163 if ($address->type == Address::LINK_COMPANY && $address->jobid == $this->id) {
164 $this->address = $address;
165 }
166 }
167
168 }
169 // }}}
170 // {{{ class Job
171 /** profile_job describes a Job, links to:
172 * - a Profile, through `pid`
173 * - a Company, through `jobid`
174 * The `id` field is the id of this job in the list of the jobs of its profile
175 *
176 * For the documentation of the phone table, please see classes/phone.php.
177 * For the documentation of the address table, please see classes/address.php.
178 *
179 * The possible relations are as follow:
180 * A Job is linked to a Company and a Profile
181 */
182 class Job
183 {
184 public $pid;
185 public $id;
186
187 public $company = null;
188 public $phones = array();
189 public $address = null;
190 public $terms = array();
191
192 public $jobid;
193
194 public $description;
195 public $user_site;
196 public $user_email;
197
198 public $pub;
199 public $email_pub;
200
201 /** Fields are:
202 * pid, id, company_id, description, url, email
203 */
204 public function __construct($data)
205 {
206 foreach ($data as $key => $val) {
207 $this->$key = $val;
208 }
209 $this->company = CompanyList::get($this->jobid);
210 if (is_null($this->company)) {
211 $entreprises = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
212 foreach ($entreprises as $entreprise) {
213 if ($entreprise->id == $this->id) {
214 $this->company = new Company(array('name' => $entreprise->name));
215 }
216 }
217 }
218 }
219
220 public function phones()
221 {
222 return $this->phones;
223 }
224
225 public function address()
226 {
227 return $this->address;
228 }
229
230 public function addPhone(Phone $phone)
231 {
232 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
233 $this->phones[$phone->uniqueId()] = $phone;
234 }
235 }
236
237 public function setAddress(Address $address)
238 {
239 if ($address->type == Address::LINK_JOB && $address->id == $this->id && $address->pid == $this->pid) {
240 $this->address = $address;
241 }
242 }
243
244 public function addTerm(JobTerm $term)
245 {
246 $this->terms[$term->jtid] = $term;
247 }
248 }
249 // }}}
250 // {{{ class JobTerm
251 class JobTerm
252 {
253 public $jtid;
254 public $full_name;
255 public $pid;
256 public $jid;
257
258 /** Fields are:
259 * pid, jid, jtid, full_name
260 */
261 public function __construct($data)
262 {
263 foreach ($data as $key => $val) {
264 $this->$key = $val;
265 }
266 }
267 }
268 // }}}
269 // {{{ class Education
270 class Education
271 {
272 public $id;
273 public $pid;
274
275 public $entry_year;
276 public $grad_year;
277 public $program;
278 public $flags;
279
280 public $school;
281 public $school_short;
282 public $school_url;
283 public $country;
284
285 public $degree;
286 public $degree_short;
287 public $degree_level;
288
289 public $field;
290
291 public function __construct(array $data)
292 {
293 foreach ($data as $key => $val) {
294 $this->$key = $val;
295 }
296 $this->flags = new PlFlagSet($this->flags);
297 }
298 }
299 // }}}
300
301 // {{{ class ProfileEducation [ Field ]
302 class ProfileEducation extends ProfileField
303 {
304 private $educations = array();
305
306 public function __construct(PlInnerSubIterator $it)
307 {
308 $this->pid = $it->value();
309 while ($edu = $it->next()) {
310 $this->educations[$edu['id']] = new Education($edu);
311 }
312 }
313
314 public function get($flags, $limit)
315 {
316 $educations = array();
317 $year = getdate();
318 $year = $year['year'];
319 $nb = 0;
320 foreach ($this->educations as $id => $edu) {
321 if (
322 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
323 ||
324 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
325 ||
326 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
327 ||
328 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
329 ||
330 ($flags & Profile::EDUCATION_ALL)
331 ) {
332 $educations[$id] = $edu;
333 ++$nb;
334 }
335 if ($limit != null && $nb >= $limit) {
336 break;
337 }
338 }
339 return $educations;
340 }
341
342 public static function fetchData(array $pids, Visibility $visibility)
343 {
344 $data = XDB::iterator('SELECT pe.id, pe.pid,
345 pe.entry_year, pe.grad_year, pe.program, pe.flags,
346 pee.name AS school, pee.abbreviation AS school_short,
347 pee.url AS school_url, gc.country,
348 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
349 pefe.field
350 FROM profile_education AS pe
351 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
352 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
353 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
354 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
355 WHERE pe.pid IN {?}
356 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
357 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
358 $pids);
359
360 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
361 }
362 }
363 // }}}
364 // {{{ class ProfileMedals [ Field ]
365 class ProfileMedals extends ProfileField
366 {
367 public $medals = array();
368
369 public function __construct(PlInnerSubIterator $it)
370 {
371 $this->pid = $it->value();
372 while ($medal = $it->next()) {
373 $this->medals[$medal['mid']] = $medal;
374 }
375 }
376
377 public static function fetchData(array $pids, Visibility $visibility)
378 {
379 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade, pm.level
380 FROM profile_medals AS pm
381 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
382 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
383 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
384 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
385 WHERE pm.pid IN {?} AND pve.best_display_level + 0 <= p.medals_pub + 0
386 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
387 $visibility->level(), $pids);
388
389 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
390 }
391 }
392 // }}}
393 // {{{ class ProfileNetworking [ Field ]
394 class ProfileNetworking extends ProfileField
395 {
396 private $networks = array();
397
398 public function __construct(PlInnerSubIterator $it)
399 {
400 $this->pid = $it->value();
401 while ($network = $it->next()) {
402 $network['network_type'] = new PlFlagSet($network['network_type']);
403 $this->networks[$network['id']] = $network;
404 }
405 }
406
407 public static function fetchData(array $pids, Visibility $visibility)
408 {
409 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
410 FROM profile_networking AS pn
411 LEFT JOIN profile_networking_enum AS pne USING(nwid)
412 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
413 WHERE pn.pid IN {?} AND pve.best_display_level + 0 <= pn.pub + 0
414 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
415 pn.nwid, id',
416 $visibility->level(), $pids);
417
418 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
419 }
420
421 public function get($flags, $limit = null)
422 {
423 $nws = array();
424 $nb = 0;
425 foreach ($this->networks as $id => $nw) {
426 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
427 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
428 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
429 ($flags == Profile::NETWORKING_ALL)) {
430 $nws[$id] = $nw;
431 ++$nb;
432 if (isset($limit) && $nb >= $limit) {
433 break;
434 }
435 }
436 }
437 return $nws;
438 }
439 }
440 // }}}
441 // {{{ class ProfileCorps [ Field ]
442 class ProfileCorps extends ProfileField
443 {
444 public $original;
445 public $current;
446
447 public $original_name;
448 public $original_abbrev;
449 public $original_still_exists;
450
451 public $current_name;
452 public $current_abbrev;
453 public $current_still_exists;
454 public $current_rank;
455 public $current_rank_abbrev;
456
457 public function __construct(array $data)
458 {
459 foreach ($data as $key => $val) {
460 $this->$key = $val;
461 }
462 }
463
464 public static function fetchData(array $pids, Visibility $visibility)
465 {
466 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
467 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
468 pceo.still_exists AS original_still_exists,
469 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
470 pcec.still_exists AS current_still_exists,
471 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
472 rankid
473 FROM profile_corps AS pc
474 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
475 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
476 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
477 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
478 WHERE pc.pid IN {?} AND pve.best_display_level + 0 <= pc.corps_pub + 0 AND pceo.id != 1
479 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
480 $visibility->level(), $pids);
481
482 return $data;
483 }
484 }
485 // }}}
486 // {{{ class ProfileMentoringCountries [ Field ]
487 class ProfileMentoringCountries extends ProfileField
488 {
489 public $countries = array();
490
491 public function __construct(PlInnerSubIterator $it)
492 {
493 $this->pid = $it->value();
494 while ($country = $it->next()) {
495 $this->countries[$country['id']] = $country['name'];
496 }
497 }
498
499 public static function fetchData(array $pids, Visibility $visibility)
500 {
501 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.country AS name
502 FROM profile_mentor_country AS pmc
503 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
504 WHERE pmc.pid IN {?}
505 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
506 $pids);
507
508 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
509 }
510 }
511 // }}}
512 // {{{ class ProfileAddresses [ Field ]
513 class ProfileAddresses extends ProfileField
514 {
515 private $addresses = array();
516
517 public function __construct(PlInnerSubIterator $it)
518 {
519 $this->pid = $it->value();
520 while ($address = $it->next()) {
521 $this->addresses[] = new Address($address);
522 }
523 }
524
525 public function get($flags, $limit = null)
526 {
527 $addresses = array();
528 $nb = 0;
529 foreach ($this->addresses as $address) {
530 if ((($flags & Profile::ADDRESS_MAIN) && $address->hasFlag('current'))
531 || (($flags & Profile::ADDRESS_POSTAL) && $address->hasFlag('mail'))
532 || (($flags & Profile::ADDRESS_PERSO) && $address->type == Address::LINK_PROFILE)
533 || (($flags & Profile::ADDRESS_PRO) && $address->type == Address::LINK_JOB)
534 ) {
535 $addresses[] = $address;
536 ++$nb;
537 }
538 if ($limit != null && $nb == $limit) {
539 break;
540 }
541 }
542 return $addresses;
543 }
544
545 public static function fetchData(array $pids, Visibility $visibility)
546 {
547 $it = Address::iterate($pids, array(), array(), $visibility);
548 return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
549 }
550
551 public function addPhones(ProfilePhones $phones)
552 {
553 $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
554 foreach ($p as $phone) {
555 /* We must iterate on the addresses because id is not uniq thus,
556 * $this->addresse[$phone->link_id] is invalid.
557 */
558 foreach ($this->addresses as $address) {
559 if ($address->type == Address::LINK_PROFILE && $address->id == $phone->link_id) {
560 $address->addPhone($phone);
561 }
562 }
563 }
564 }
565 }
566 // }}}
567 // {{{ class ProfilePhones [ Field ]
568 class ProfilePhones extends ProfileField
569 {
570 private $phones = array();
571
572 public function __construct(PlInnerSubIterator $it)
573 {
574 $this->pid = $it->value();
575 while ($phone = $it->next()) {
576 $this->phones[] = new Phone($phone);
577 }
578 }
579
580 public function get($flags, $limit = null)
581 {
582 $phones = array();
583 $nb = 0;
584 foreach ($this->phones as $id => $phone) {
585 if ($phone->hasFlags($flags)) {
586 $phones[$id] = $phone;
587 ++$nb;
588 if ($limit != null && $nb == $limit) {
589 break;
590 }
591 }
592 }
593 return $phones;
594 }
595
596 public static function fetchData(array $pids, Visibility $visibility)
597 {
598 $it = Phone::iterate($pids, array(), array(), $visibility);
599 return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
600 }
601 }
602 // }}}
603 // {{{ class ProfileJobs [ Field ]
604 class ProfileJobs extends ProfileField
605 {
606 private $jobs = array();
607
608 public function __construct(PlInnerSubIterator $jobs)
609 {
610 $this->pid = $jobs->value();
611 while ($job = $jobs->next()) {
612 $this->jobs[$job['id']] = new Job($job);
613 }
614 }
615
616 public static function fetchData(array $pids, Visibility $visibility)
617 {
618 CompanyList::preload($pids);
619 $data = XDB::iterator('SELECT id, pid, description, url as user_site, jobid, pub, IF(pve.best_display_level + 0 <= email_pub + 0, email, NULL) AS user_email, email_pub
620 FROM profile_job
621 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
622 WHERE pid IN {?} AND pve.best_display_level + 0 <= pub + 0
623 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', id',
624 $visibility->level(), $pids);
625 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
626 }
627
628 public function get($flags, $limit = null)
629 {
630 $jobs = array();
631 $nb = 0;
632 foreach ($this->jobs as $id => $job) {
633 $jobs[$id] = $job;
634 ++$nb;
635 if ($limit != null && $nb >= $limit) {
636 break;
637 }
638 }
639 return $jobs;
640 }
641
642 public function addPhones(ProfilePhones $phones)
643 {
644 $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
645 foreach ($p as $phone) {
646 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
647 $this->jobs[$phone->link_id]->addPhone($phone);
648 }
649 }
650 }
651
652 public function addAddresses(ProfileAddresses $addresses)
653 {
654 $a = $addresses->get(Profile::ADDRESS_PRO);
655 foreach ($a as $address) {
656 if ($address->type == Address::LINK_JOB && array_key_exists($address->id, $this->jobs)) {
657 $this->jobs[$address->id]->setAddress($address);
658 }
659 }
660 }
661
662 public function addCompanies(array $companies)
663 {
664 foreach ($this->jobs as $job) {
665 $this->company = $companies[$job->jobid];
666 }
667 }
668
669 public function addJobTerms(ProfileJobTerms $jobterms)
670 {
671 $terms = $jobterms->get();
672 foreach ($terms as $term) {
673 if ($this->pid == $term->pid && array_key_exists($term->jid, $this->jobs)) {
674 $this->jobs[$term->jid]->addTerm($term);
675 }
676 }
677 }
678 }
679 // }}}
680 // {{{ class ProfileJobTerms [ Field ]
681 class ProfileJobTerms extends ProfileField
682 {
683 private $jobterms = array();
684
685 public function __construct(PlInnerSubIterator $it)
686 {
687 $this->pid = $it->value();
688 while ($term = $it->next()) {
689 $this->jobterms[] = new JobTerm($term);
690 }
691 }
692
693 public function get()
694 {
695 return $this->jobterms;
696 }
697
698 public static function fetchData(array $pids, Visibility $visibility)
699 {
700 $data = XDB::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
701 FROM profile_job_term AS jt
702 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
703 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
704 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
705 WHERE jt.pid IN {?} AND pve.best_display_level + 0 <= j.pub + 0
706 ORDER BY ' . XDB::formatCustomOrder('jt.pid', $pids),
707 $visibility->level(), $pids);
708 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
709 }
710 }
711 // }}}
712 // {{{ class ProfileMentoringTerms [ Field ]
713 class ProfileMentoringTerms extends ProfileJobTerms
714 {
715 public static function fetchData(array $pids, Visibility $visibility)
716 {
717 $data = XDB::iterator('SELECT mt.jtid, jte.full_name, mt.pid
718 FROM profile_mentor_term AS mt
719 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
720 WHERE mt.pid IN {?}
721 ORDER BY ' . XDB::formatCustomOrder('mt.pid', $pids),
722 $pids);
723 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
724 }
725 }
726 // }}}
727 // {{{ class ProfileSkills [ Field ]
728 class ProfileSkills extends ProfileField
729 {
730 public $skills = array();
731
732 public function __construct(PlInnerSubIterator $it)
733 {
734 $this->pid = $it->value();
735 while ($skill = $it->next()) {
736 $this->skills[$skill['cid']] = $skill;
737 }
738 }
739
740 public static function fetchData(array $pids, Visibility $visibility)
741 {
742 $data = XDB::iterator('SELECT ps.cid, pse.text_fr, ps.level, ps.pid
743 FROM profile_skills AS ps
744 INNER JOIN profile_skill_enum AS pse ON (pse.id = ps.cid)
745 WHERE ps.pid IN {?}
746 ORDER BY ' . XDB::formatCustomOrder('ps.pid', $pids),
747 $pids);
748 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
749 }
750 }
751 // }}}
752 // {{{ class ProfileLanguages [ Field ]
753 class ProfileLanguages extends ProfileField
754 {
755 public $languages = array();
756
757 public function __construct(PlInnerSubIterator $it)
758 {
759 static $levels = array(
760 1 => 'connaissance basique',
761 2 => 'maîtrise des bases',
762 3 => 'maîtrise limitée',
763 4 => 'maîtrise générale',
764 5 => 'bonne maîtrise',
765 6 => 'maîtrise complète'
766 );
767
768 $this->pid = $it->value();
769 while ($language = $it->next()) {
770 $this->languages[$language['lid']] = $language;
771 $this->languages[$language['lid']]['level'] = $levels[$language['level']];
772 }
773 }
774
775 public static function fetchData(array $pids, Visibility $visibility)
776 {
777 $data = XDB::iterator('SELECT ps.lid, pse.language, ps.level, ps.pid
778 FROM profile_langskills AS ps
779 INNER JOIN profile_langskill_enum AS pse ON (pse.iso_639_2b = ps.lid)
780 WHERE ps.pid IN {?}
781 ORDER BY ' . XDB::formatCustomOrder('ps.pid', $pids),
782 $pids);
783 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
784 }
785 }
786 // }}}
787 // {{{ class ProfilePartnerSharing [ Field ]
788 class ProfilePartnerSharing extends ProfileField
789 {
790 public function __construct(PlInnerSubIterator $it)
791 {
792 require_once 'partnersharing.inc.php';
793
794 $this->pid = $it->value();
795 while ($partner_settings = $it->next()) {
796 $settings = new PartnerSettings($partner_settings);
797 $this->partners_settings[$settings->partner->id] = $settings;
798 }
799 }
800
801 public static function fetchData(array $pids, Visibility $visibility)
802 {
803 $data = XDB::iterator('SELECT ppss.pid, ppss.exposed_uid, ppss.sharing_level,
804 ppss.allow_email, ppss.partner_id,
805 ppse.shortname AS partner_shortname,
806 ppse.name AS partner_name,
807 ppse.url AS partner_url
808 FROM profile_partnersharing_settings AS ppss
809 LEFT JOIN profile_partnersharing_enum AS ppse ON (ppss.partner_id = ppse.id)
810 WHERE ppss.pid IN {?}
811 ORDER BY ' . XDB::formatCustomOrder('ppss.pid', $pids),
812 $pids);
813 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
814 }
815
816 public function get($partner_id)
817 {
818 if (isset($this->partners_settings[$partner_id])) {
819 return $this->partners_settings[$partner_id];
820 } else {
821 return PartnerSettings::getEmpty($partner_id);
822 }
823 }
824 }
825 // }}}
826 // {{{ class CompanyList
827 class CompanyList
828 {
829 static private $fullload = false;
830 static private $companies = array();
831
832 static public function preload($pids = array())
833 {
834 if (self::$fullload) {
835 return;
836 }
837 // Load raw data
838 if (count($pids)) {
839 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
840 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
841 } else {
842 $join = '';
843 $where = '';
844 }
845
846 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
847 pa.flags, pa.text, pa.type, pa.pub
848 FROM profile_job_enum AS pje
849 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
850 ' . $join . '
851 ' . $where);
852 $newcompanies = array();
853 while ($row = $it->next()) {
854 $cp = new Company($row);
855 $addr = new Address($row);
856 $cp->setAddress($addr);
857 if (!array_key_exists($row['id'], self::$companies)) {
858 $newcompanies[] = $row['id'];
859 }
860 self::$companies[$row['id']] = $cp;
861 }
862
863 // Add phones to hq
864 if (count($newcompanies)) {
865 $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies, Visibility::defaultForRead());
866 while ($phone = $it->next()) {
867 self::$companies[$phone->link_id]->setPhone($phone);
868 }
869 }
870
871 if (count($pids) == 0) {
872 self::$fullload = true;
873 }
874 }
875
876 static public function get($id)
877 {
878 if (!is_null($id) && !array_key_exists($id, self::$companies)) {
879 self::preload();
880 }
881 if (isset(self::$companies[$id])) {
882 return self::$companies[$id];
883 }
884 return null;
885 }
886 }
887 // }}}
888
889 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
890 ?>