Merge branch 'xorg/maint' into xorg/master
[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 public $pub;
197 public $email_pub;
198
199 /** Fields are:
200 * pid, id, company_id, description, url, email
201 */
202 public function __construct($data)
203 {
204 foreach ($data as $key => $val) {
205 $this->$key = $val;
206 }
207 $this->company = CompanyList::get($this->jobid);
208 if (is_null($this->company)) {
209 $entreprises = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
210 foreach ($entreprises as $entreprise) {
211 if ($entreprise->id == $this->id) {
212 $this->company = new Company(array('name' => $entreprise->name));
213 }
214 }
215 }
216 }
217
218 public function phones()
219 {
220 return $this->phones;
221 }
222
223 public function address()
224 {
225 return $this->address;
226 }
227
228 public function addPhone(Phone $phone)
229 {
230 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
231 $this->phones[$phone->uniqueId()] = $phone;
232 }
233 }
234
235 public function setAddress(Address $address)
236 {
237 if ($address->type == Address::LINK_JOB && $address->id == $this->id && $address->pid == $this->pid) {
238 $this->address = $address;
239 }
240 }
241
242 public function addTerm(JobTerm $term)
243 {
244 $this->terms[$term->jtid] = $term;
245 }
246 }
247 // }}}
248 // {{{ class JobTerm
249 class JobTerm
250 {
251 public $jtid;
252 public $full_name;
253 public $pid;
254 public $jid;
255
256 /** Fields are:
257 * pid, jid, jtid, full_name
258 */
259 public function __construct($data)
260 {
261 foreach ($data as $key => $val) {
262 $this->$key = $val;
263 }
264 }
265 }
266 // }}}
267 // {{{ class Education
268 class Education
269 {
270 public $id;
271 public $pid;
272
273 public $entry_year;
274 public $grad_year;
275 public $program;
276 public $flags;
277
278 public $school;
279 public $school_short;
280 public $school_url;
281 public $country;
282
283 public $degree;
284 public $degree_short;
285 public $degree_level;
286
287 public $field;
288
289 public function __construct(array $data)
290 {
291 foreach ($data as $key => $val) {
292 $this->$key = $val;
293 }
294 $this->flags = new PlFlagSet($this->flags);
295 }
296 }
297 // }}}
298
299 // {{{ class ProfileEducation [ Field ]
300 class ProfileEducation extends ProfileField
301 {
302 private $educations = array();
303
304 public function __construct(PlInnerSubIterator $it)
305 {
306 $this->pid = $it->value();
307 while ($edu = $it->next()) {
308 $this->educations[$edu['id']] = new Education($edu);
309 }
310 }
311
312 public function get($flags, $limit)
313 {
314 $educations = array();
315 $year = getdate();
316 $year = $year['year'];
317 $nb = 0;
318 foreach ($this->educations as $id => $edu) {
319 if (
320 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
321 ||
322 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
323 ||
324 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
325 ||
326 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
327 ||
328 ($flags & Profile::EDUCATION_ALL)
329 ) {
330 $educations[$id] = $edu;
331 ++$nb;
332 }
333 if ($limit != null && $nb >= $limit) {
334 break;
335 }
336 }
337 return $educations;
338 }
339
340 public static function fetchData(array $pids, Visibility $visibility)
341 {
342 $data = XDB::iterator('SELECT pe.id, pe.pid,
343 pe.entry_year, pe.grad_year, pe.program, pe.flags,
344 pee.name AS school, pee.abbreviation AS school_short,
345 pee.url AS school_url, gc.country,
346 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
347 pefe.field
348 FROM profile_education AS pe
349 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
350 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
351 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
352 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
353 WHERE pe.pid IN {?}
354 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
355 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
356 $pids);
357
358 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
359 }
360 }
361 // }}}
362 // {{{ class ProfileMedals [ Field ]
363 class ProfileMedals extends ProfileField
364 {
365 public $medals = array();
366
367 public function __construct(PlInnerSubIterator $it)
368 {
369 $this->pid = $it->value();
370 while ($medal = $it->next()) {
371 $this->medals[$medal['mid']] = $medal;
372 }
373 }
374
375 public static function fetchData(array $pids, Visibility $visibility)
376 {
377 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
378 FROM profile_medals AS pm
379 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
380 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
381 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
382 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
383 WHERE pm.pid IN {?} AND pve.best_display_level + 0 <= p.medals_pub + 0
384 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
385 $visibility->level(), $pids);
386
387 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
388 }
389 }
390 // }}}
391 // {{{ class ProfileNetworking [ Field ]
392 class ProfileNetworking extends ProfileField
393 {
394 private $networks = array();
395
396 public function __construct(PlInnerSubIterator $it)
397 {
398 $this->pid = $it->value();
399 while ($network = $it->next()) {
400 $network['network_type'] = new PlFlagSet($network['network_type']);
401 $this->networks[$network['id']] = $network;
402 }
403 }
404
405 public static function fetchData(array $pids, Visibility $visibility)
406 {
407 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
408 FROM profile_networking AS pn
409 LEFT JOIN profile_networking_enum AS pne USING(nwid)
410 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
411 WHERE pn.pid IN {?} AND pve.best_display_level + 0 <= pn.pub + 0
412 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
413 pn.nwid, id',
414 $visibility->level(), $pids);
415
416 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
417 }
418
419 public function get($flags, $limit = null)
420 {
421 $nws = array();
422 $nb = 0;
423 foreach ($this->networks as $id => $nw) {
424 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
425 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
426 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
427 ($flags == Profile::NETWORKING_ALL)) {
428 $nws[$id] = $nw;
429 ++$nb;
430 if (isset($limit) && $nb >= $limit) {
431 break;
432 }
433 }
434 }
435 return $nws;
436 }
437 }
438 // }}}
439 // {{{ class ProfileCorps [ Field ]
440 class ProfileCorps extends ProfileField
441 {
442 public $original;
443 public $current;
444
445 public $original_name;
446 public $original_abbrev;
447 public $original_still_exists;
448
449 public $current_name;
450 public $current_abbrev;
451 public $current_still_exists;
452 public $current_rank;
453 public $current_rank_abbrev;
454
455 public function __construct(array $data)
456 {
457 foreach ($data as $key => $val) {
458 $this->$key = $val;
459 }
460 }
461
462 public static function fetchData(array $pids, Visibility $visibility)
463 {
464 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
465 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
466 pceo.still_exists AS original_still_exists,
467 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
468 pcec.still_exists AS current_still_exists,
469 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
470 rankid
471 FROM profile_corps AS pc
472 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
473 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
474 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
475 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
476 WHERE pc.pid IN {?} AND pve.best_display_level + 0 <= pc.corps_pub + 0 AND pceo.id != 1
477 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
478 $visibility->level(), $pids);
479
480 return $data;
481 }
482 }
483 // }}}
484 // {{{ class ProfileMentoringCountries [ Field ]
485 class ProfileMentoringCountries extends ProfileField
486 {
487 public $countries = array();
488
489 public function __construct(PlInnerSubIterator $it)
490 {
491 $this->pid = $it->value();
492 while ($country = $it->next()) {
493 $this->countries[$country['id']] = $country['name'];
494 }
495 }
496
497 public static function fetchData(array $pids, Visibility $visibility)
498 {
499 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.country AS name
500 FROM profile_mentor_country AS pmc
501 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
502 WHERE pmc.pid IN {?}
503 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
504 $pids);
505
506 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
507 }
508 }
509 // }}}
510 // {{{ class ProfileAddresses [ Field ]
511 class ProfileAddresses extends ProfileField
512 {
513 private $addresses = array();
514
515 public function __construct(PlInnerSubIterator $it)
516 {
517 $this->pid = $it->value();
518 while ($address = $it->next()) {
519 $this->addresses[] = new Address($address);
520 }
521 }
522
523 public function get($flags, $limit = null)
524 {
525 $addresses = array();
526 $nb = 0;
527 foreach ($this->addresses as $address) {
528 if ((($flags & Profile::ADDRESS_MAIN) && $address->hasFlag('current'))
529 || (($flags & Profile::ADDRESS_POSTAL) && $address->hasFlag('mail'))
530 || (($flags & Profile::ADDRESS_PERSO) && $address->type == Address::LINK_PROFILE)
531 || (($flags & Profile::ADDRESS_PRO) && $address->type == Address::LINK_JOB)
532 ) {
533 $addresses[] = $address;
534 ++$nb;
535 }
536 if ($limit != null && $nb == $limit) {
537 break;
538 }
539 }
540 return $addresses;
541 }
542
543 public static function fetchData(array $pids, Visibility $visibility)
544 {
545 $it = Address::iterate($pids, array(), array(), $visibility);
546 return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
547 }
548
549 public function addPhones(ProfilePhones $phones)
550 {
551 $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
552 foreach ($p as $phone) {
553 /* We must iterate on the addresses because id is not uniq thus,
554 * $this->addresse[$phone->link_id] is invalid.
555 */
556 foreach ($this->addresses as $address) {
557 if ($address->type == Address::LINK_PROFILE && $address->id == $phone->link_id) {
558 $address->addPhone($phone);
559 }
560 }
561 }
562 }
563 }
564 // }}}
565 // {{{ class ProfilePhones [ Field ]
566 class ProfilePhones extends ProfileField
567 {
568 private $phones = array();
569
570 public function __construct(PlInnerSubIterator $it)
571 {
572 $this->pid = $it->value();
573 while ($phone = $it->next()) {
574 $this->phones[] = new Phone($phone);
575 }
576 }
577
578 public function get($flags, $limit = null)
579 {
580 $phones = array();
581 $nb = 0;
582 foreach ($this->phones as $id => $phone) {
583 if ($phone->hasFlags($flags)) {
584 $phones[$id] = $phone;
585 ++$nb;
586 if ($limit != null && $nb == $limit) {
587 break;
588 }
589 }
590 }
591 return $phones;
592 }
593
594 public static function fetchData(array $pids, Visibility $visibility)
595 {
596 $it = Phone::iterate($pids, array(), array(), $visibility);
597 return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
598 }
599 }
600 // }}}
601 // {{{ class ProfileJobs [ Field ]
602 class ProfileJobs extends ProfileField
603 {
604 private $jobs = array();
605
606 public function __construct(PlInnerSubIterator $jobs)
607 {
608 $this->pid = $jobs->value();
609 while ($job = $jobs->next()) {
610 $this->jobs[$job['id']] = new Job($job);
611 }
612 }
613
614 public static function fetchData(array $pids, Visibility $visibility)
615 {
616 CompanyList::preload($pids);
617 $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
618 FROM profile_job
619 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
620 WHERE pid IN {?} AND pve.best_display_level + 0 <= pub + 0
621 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', id',
622 $visibility->level(), $pids);
623 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
624 }
625
626 public function get($flags, $limit = null)
627 {
628 $jobs = array();
629 $nb = 0;
630 foreach ($this->jobs as $id => $job) {
631 $jobs[$id] = $job;
632 ++$nb;
633 if ($limit != null && $nb >= $limit) {
634 break;
635 }
636 }
637 return $jobs;
638 }
639
640 public function addPhones(ProfilePhones $phones)
641 {
642 $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
643 foreach ($p as $phone) {
644 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
645 $this->jobs[$phone->link_id]->addPhone($phone);
646 }
647 }
648 }
649
650 public function addAddresses(ProfileAddresses $addresses)
651 {
652 $a = $addresses->get(Profile::ADDRESS_PRO);
653 foreach ($a as $address) {
654 if ($address->type == Address::LINK_JOB && array_key_exists($address->id, $this->jobs)) {
655 $this->jobs[$address->id]->setAddress($address);
656 }
657 }
658 }
659
660 public function addCompanies(array $companies)
661 {
662 foreach ($this->jobs as $job) {
663 $this->company = $companies[$job->jobid];
664 }
665 }
666
667 public function addJobTerms(ProfileJobTerms $jobterms)
668 {
669 $terms = $jobterms->get();
670 foreach ($terms as $term) {
671 if ($this->pid == $term->pid && array_key_exists($term->jid, $this->jobs)) {
672 $this->jobs[$term->jid]->addTerm($term);
673 }
674 }
675 }
676 }
677 // }}}
678 // {{{ class ProfileJobTerms [ Field ]
679 class ProfileJobTerms extends ProfileField
680 {
681 private $jobterms = array();
682
683 public function __construct(PlInnerSubIterator $it)
684 {
685 $this->pid = $it->value();
686 while ($term = $it->next()) {
687 $this->jobterms[] = new JobTerm($term);
688 }
689 }
690
691 public function get()
692 {
693 return $this->jobterms;
694 }
695
696 public static function fetchData(array $pids, Visibility $visibility)
697 {
698 $data = XDB::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
699 FROM profile_job_term AS jt
700 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
701 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
702 LEFT JOIN profile_visibility_enum AS pve ON (pve.access_level = {?})
703 WHERE jt.pid IN {?} AND pve.best_display_level + 0 <= j.pub + 0
704 ORDER BY ' . XDB::formatCustomOrder('jt.pid', $pids),
705 $visibility->level(), $pids);
706 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
707 }
708 }
709 // }}}
710 // {{{ class ProfileMentoringTerms [ Field ]
711 class ProfileMentoringTerms extends ProfileJobTerms
712 {
713 public static function fetchData(array $pids, Visibility $visibility)
714 {
715 $data = XDB::iterator('SELECT mt.jtid, jte.full_name, mt.pid
716 FROM profile_mentor_term AS mt
717 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
718 WHERE mt.pid IN {?}
719 ORDER BY ' . XDB::formatCustomOrder('mt.pid', $pids),
720 $pids);
721 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
722 }
723 }
724 // }}}
725 // {{{ class ProfilePartnerSharing [ Field ]
726 class ProfilePartnerSharing extends ProfileField
727 {
728 public function __construct(PlInnerSubIterator $it)
729 {
730 require_once 'partnersharing.inc.php';
731
732 $this->pid = $it->value();
733 while ($partner_settings = $it->next()) {
734 $settings = new PartnerSettings($partner_settings);
735 $this->partners_settings[$settings->partner->id] = $settings;
736 }
737 }
738
739 public static function fetchData(array $pids, Visibility $visibility)
740 {
741 $data = XDB::iterator('SELECT ppss.pid, ppss.exposed_uid, ppss.sharing_level,
742 ppss.allow_email, ppss.partner_id,
743 ppse.shortname AS partner_shortname,
744 ppse.name AS partner_name,
745 ppse.url AS partner_url
746 FROM profile_partnersharing_settings AS ppss
747 LEFT JOIN profile_partnersharing_enum AS ppse ON (ppss.partner_id = ppse.id)
748 WHERE ppss.pid IN {?}
749 ORDER BY ' . XDB::formatCustomOrder('ppss.pid', $pids),
750 $pids);
751 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
752 }
753
754 public function get($partner_id)
755 {
756 if (isset($this->partners_settings[$partner_id])) {
757 return $this->partners_settings[$partner_id];
758 } else {
759 return PartnerSettings::getEmpty($partner_id);
760 }
761 }
762 }
763 // }}}
764 // {{{ class CompanyList
765 class CompanyList
766 {
767 static private $fullload = false;
768 static private $companies = array();
769
770 static public function preload($pids = array())
771 {
772 if (self::$fullload) {
773 return;
774 }
775 // Load raw data
776 if (count($pids)) {
777 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
778 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
779 } else {
780 $join = '';
781 $where = '';
782 }
783
784 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
785 pa.flags, pa.text, pa.type, pa.pub
786 FROM profile_job_enum AS pje
787 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
788 ' . $join . '
789 ' . $where);
790 $newcompanies = array();
791 while ($row = $it->next()) {
792 $cp = new Company($row);
793 $addr = new Address($row);
794 $cp->setAddress($addr);
795 if (!array_key_exists($row['id'], self::$companies)) {
796 $newcompanies[] = $row['id'];
797 }
798 self::$companies[$row['id']] = $cp;
799 }
800
801 // Add phones to hq
802 if (count($newcompanies)) {
803 $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies, Visibility::defaultForRead());
804 while ($phone = $it->next()) {
805 self::$companies[$phone->link_id]->setPhone($phone);
806 }
807 }
808
809 if (count($pids) == 0) {
810 self::$fullload = true;
811 }
812 }
813
814 static public function get($id)
815 {
816 if (!array_key_exists($id, self::$companies)) {
817 self::preload();
818 }
819 if (isset(self::$companies[$id])) {
820 return self::$companies[$id];
821 }
822 return null;
823 }
824 }
825 // }}}
826
827 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
828 ?>