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