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