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