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