Fixes request for profile display.
[platal.git] / include / profilefields.inc.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 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_SECTOR => 'ProfileMentoringSectors',
37 Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries',
38 Profile::FETCH_JOB_TERMS => 'ProfileJobTerms',
39 Profile::FETCH_MENTOR_TERMS => 'ProfileMentoringTerms',
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, ProfileVisibility $visibility)
55 {
56 return PlIteratorUtils::emptyIterator();
57 }
58
59 public static function buildForPID($cls, $pid, ProfileVisibility $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, ProfileVisibility $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, ProfileVisibility $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, ProfileVisibility $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->linkType() == Phone::LINK_COMPANY && $phone->linkId() == $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 $sector;
197 public $subsector;
198 public $subsubsector;
199
200 /** Fields are:
201 * pid, id, company_id, description, url, email
202 */
203 public function __construct($data)
204 {
205 foreach ($data as $key => $val) {
206 $this->$key = $val;
207 }
208 $this->company = CompanyList::get($this->jobid);
209 if (is_null($this->company)) {
210 $entreprise = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
211 $this->company = new Company(array('name' => $entreprise[$this->id]->name));
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->linkType() == Phone::LINK_JOB && $phone->linkId() == $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->jobid == $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, ProfileVisibility $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.countryFR AS 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, ProfileVisibility $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 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
380 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
381 $pids, $visibility->levels());
382
383 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
384 }
385 }
386 // }}}
387 // {{{ class ProfileNetworking [ Field ]
388 class ProfileNetworking extends ProfileField
389 {
390 private $networks = array();
391
392 public function __construct(PlInnerSubIterator $it)
393 {
394 $this->pid = $it->value();
395 while ($network = $it->next()) {
396 $network['network_type'] = new PlFlagSet($network['network_type']);
397 $this->networks[$network['id']] = $network;
398 }
399 }
400
401 public static function fetchData(array $pids, ProfileVisibility $visibility)
402 {
403 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
404 FROM profile_networking AS pn
405 LEFT JOIN profile_networking_enum AS pne USING(nwid)
406 WHERE pid IN {?} AND pub IN {?}
407 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
408 pn.nwid, id',
409 $pids, $visibility->levels());
410
411 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
412 }
413
414 public function get($flags, $limit = null)
415 {
416 $nws = array();
417 $nb = 0;
418 foreach ($this->networks as $id => $nw) {
419 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
420 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
421 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
422 ($flags == Profile::NETWORKING_ALL)) {
423 $nws[$id] = $nw;
424 ++$nb;
425 if (isset($limit) && $nb >= $limit) {
426 break;
427 }
428 }
429 }
430 return $nws;
431 }
432 }
433 // }}}
434 // {{{ class ProfileCorps [ Field ]
435 class ProfileCorps extends ProfileField
436 {
437 public $original;
438 public $current;
439
440 public $original_name;
441 public $original_abbrev;
442 public $original_still_exists;
443
444 public $current_name;
445 public $current_abbrev;
446 public $current_still_exists;
447 public $current_rank;
448 public $current_rank_abbrev;
449
450 public function __construct(array $data)
451 {
452 foreach ($data as $key => $val) {
453 $this->$key = $val;
454 }
455 }
456
457 public static function fetchData(array $pids, ProfileVisibility $visibility)
458 {
459 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
460 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
461 pceo.still_exists AS original_still_exists,
462 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
463 pcec.still_exists AS current_still_exists,
464 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
465 rankid
466 FROM profile_corps AS pc
467 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
468 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
469 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
470 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
471 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
472 $pids, $visibility->levels());
473
474 return $data;
475 }
476 }
477 // }}}
478 // {{{ class ProfileMentoringSectors [ Field ]
479 class ProfileMentoringSectors extends ProfileField
480 {
481 public $sectors = array();
482
483 public function __construct(PlInnerSubIterator $it)
484 {
485 $this->pid = $it->value();
486 while ($sector = $it->next()) {
487 $this->sectors[] = $sector;
488 }
489 }
490
491 public static function fetchData(array $pids, ProfileVisibility $visibility)
492 {
493 $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
494 FROM profile_mentor_sector AS pms
495 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
496 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
497 WHERE pms.pid IN {?}
498 ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids),
499 $pids);
500
501 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
502 }
503 }
504 // }}}
505 // {{{ class ProfileMentoringCountries [ Field ]
506 class ProfileMentoringCountries extends ProfileField
507 {
508 public $countries = array();
509
510 public function __construct(PlInnerSubIterator $it)
511 {
512 $this->pid = $it->value();
513 while ($country = $it->next()) {
514 $this->countries[$country['id']] = $country['name'];
515 }
516 }
517
518 public static function fetchData(array $pids, ProfileVisibility $visibility)
519 {
520 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
521 FROM profile_mentor_country AS pmc
522 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
523 WHERE pmc.pid IN {?}
524 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
525 $pids);
526
527 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
528 }
529 }
530 // }}}
531 // {{{ class ProfileAddresses [ Field ]
532 class ProfileAddresses extends ProfileField
533 {
534 private $addresses = array();
535
536 public function __construct(PlInnerSubIterator $it)
537 {
538 $this->pid = $it->value();
539 while ($address = $it->next()) {
540 $this->addresses[] = new Address($address);
541 }
542 }
543
544 public function get($flags, $limit = null)
545 {
546 $addresses = array();
547 $nb = 0;
548 foreach ($this->addresses as $address) {
549 if ((($flags & Profile::ADDRESS_MAIN) && $address->hasFlag('current'))
550 || (($flags & Profile::ADDRESS_POSTAL) && $address->hasFlag('mail'))
551 || (($flags & Profile::ADDRESS_PERSO) && $address->type == Address::LINK_PROFILE)
552 || (($flags & Profile::ADDRESS_PRO) && $address->type == Address::LINK_JOB)
553 ) {
554 $addresses[] = $address;
555 ++$nb;
556 }
557 if ($limit != null && $nb == $limit) {
558 break;
559 }
560 }
561 return $addresses;
562 }
563
564 public static function fetchData(array $pids, ProfileVisibility $visibility)
565 {
566 $it = Address::iterate($pids, array(), array(), $visibility->levels());
567 return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
568 }
569
570 public function addPhones(ProfilePhones $phones)
571 {
572 $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
573 foreach ($p as $phone) {
574 if ($phone->linkType() == Phone::LINK_ADDRESS && array_key_exists($phone->linkId(), $this->addresses)) {
575 $this->addresses[$phone->linkId()]->addPhone($phone);
576 }
577 }
578 }
579 }
580 // }}}
581 // {{{ class ProfilePhones [ Field ]
582 class ProfilePhones extends ProfileField
583 {
584 private $phones = array();
585
586 public function __construct(PlInnerSubIterator $it)
587 {
588 $this->pid = $it->value();
589 while ($phone = $it->next()) {
590 $this->phones[] = new Phone($phone);
591 }
592 }
593
594 public function get($flags, $limit = null)
595 {
596 $phones = array();
597 $nb = 0;
598 foreach ($this->phones as $id => $phone) {
599 if ($phone->hasFlags($flags)) {
600 $phones[$id] = $phone;
601 ++$nb;
602 if ($limit != null && $nb == $limit) {
603 break;
604 }
605 }
606 }
607 return $phones;
608 }
609
610 public static function fetchData(array $pids, ProfileVisibility $visibility)
611 {
612 $it = Phone::iterate($pids, array(), array(), $visibility->levels());
613 return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
614 }
615 }
616 // }}}
617 // {{{ class ProfileJobs [ Field ]
618 class ProfileJobs extends ProfileField
619 {
620 private $jobs = array();
621
622 public function __construct(PlInnerSubIterator $jobs)
623 {
624 $this->pid = $jobs->value();
625 while ($job = $jobs->next()) {
626 $this->jobs[$job['id']] = new Job($job);
627 }
628 }
629
630 public static function fetchData(array $pids, ProfileVisibility $visibility)
631 {
632 CompanyList::preload($pids);
633 $data = XDB::iterator('SELECT id, pid, description, url as user_site, jobid,
634 IF(email_pub IN {?}, email, NULL) AS user_email
635 FROM profile_job
636 WHERE pid IN {?} AND pub IN {?}
637 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ', id',
638 $visibility->levels(), $pids, $visibility->levels());
639 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
640 }
641
642 public function get($flags, $limit = null)
643 {
644 $jobs = array();
645 $nb = 0;
646 foreach ($this->jobs as $id => $job) {
647 $jobs[$id] = $job;
648 ++$nb;
649 if ($limit != null && $nb >= $limit) {
650 break;
651 }
652 }
653 return $jobs;
654 }
655
656 public function addPhones(ProfilePhones $phones)
657 {
658 $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
659 foreach ($p as $phone) {
660 if ($phone->linkType() == Phone::LINK_JOB && array_key_exists($phone->linkId(), $this->jobs)) {
661 $this->jobs[$phone->linkId()]->addPhone($phone);
662 }
663 }
664 }
665
666 public function addAddresses(ProfileAddresses $addresses)
667 {
668 $a = $addresses->get(Profile::ADDRESS_PRO);
669 foreach ($a as $address) {
670 if ($address->type == Address::LINK_JOB && array_key_exists($address->jobid, $this->jobs)) {
671 $this->jobs[$address->jobid]->setAddress($address);
672 }
673 }
674 }
675
676 public function addCompanies(array $companies)
677 {
678 foreach ($this->jobs as $job) {
679 $this->company = $companies[$job->jobid];
680 }
681 }
682
683 public function addJobTerms(ProfileJobTerms $jobterms)
684 {
685 $terms = $jobterms->get();
686 foreach ($terms as $term) {
687 if ($this->pid == $term->pid && array_key_exists($term->jid, $this->jobs)) {
688 $this->jobs[$term->jid]->addTerm(&$term);
689 }
690 }
691 }
692 }
693 // }}}
694 // {{{ class ProfileJobTerms [ Field ]
695 class ProfileJobTerms extends ProfileField
696 {
697 private $jobterms = array();
698
699 public function __construct(PlInnerSubIterator $it)
700 {
701 $this->pid = $it->value();
702 while ($term = $it->next()) {
703 $this->jobterms[] = new JobTerm($term);
704 }
705 }
706
707 public function get()
708 {
709 return $this->jobterms;
710 }
711
712 public static function fetchData(array $pids, ProfileVisibility $visibility)
713 {
714 $data = XDB::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
715 FROM profile_job_term AS jt
716 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
717 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
718 WHERE jt.pid IN {?} AND j.pub IN {?}
719 ORDER BY ' . XDB::formatCustomOrder('jt.pid', $pids),
720 $pids, $visibility->levels());
721 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
722 }
723 }
724 // }}}
725 // {{{ class ProfileMentoringTerms [ Field ]
726 class ProfileMentoringTerms extends ProfileJobTerms
727 {
728 public static function fetchData(array $pids, ProfileVisibility $visibility)
729 {
730 $data = XDB::iterator('SELECT mt.jtid, jte.full_name, mt.pid
731 FROM profile_mentor_term AS mt
732 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
733 WHERE mt.pid IN {?}
734 ORDER BY ' . XDB::formatCustomOrder('mt.pid', $pids),
735 $pids);
736 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
737 }
738 }
739 // }}}
740 // {{{ class CompanyList
741 class CompanyList
742 {
743 static private $fullload = false;
744 static private $companies = array();
745
746 static public function preload($pids = array())
747 {
748 if (self::$fullload) {
749 return;
750 }
751 // Load raw data
752 if (count($pids)) {
753 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
754 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
755 } else {
756 $join = '';
757 $where = '';
758 }
759
760 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
761 pa.flags, pa.text, pa.postalCode, pa.countryId,
762 pa.type, pa.pub
763 FROM profile_job_enum AS pje
764 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
765 ' . $join . '
766 ' . $where);
767 $newcompanies = array();
768 while ($row = $it->next()) {
769 $cp = new Company($row);
770 $addr = new Address($row);
771 $cp->setAddress($addr);
772 if (!array_key_exists($row['id'], self::$companies)) {
773 $newcompanies[] = $row['id'];
774 }
775 self::$companies[$row['id']] = $cp;
776 }
777
778 // Add phones to hq
779 if (count($newcompanies)) {
780 $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies);
781 while ($phone = $it->next()) {
782 self::$companies[$phone->linkId()]->setPhone($phone);
783 }
784 }
785
786 if (count($pids) == 0) {
787 self::$fullload = true;
788 }
789 }
790
791 static public function get($id)
792 {
793 if (!array_key_exists($id, self::$companies)) {
794 self::preload();
795 }
796 return self::$companies[$id];
797 }
798 }
799
800 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
801 ?>