Fix networking with new network_type issues introduced by previous commit
[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 public function hasFlags($flags) {
156 return $this->hasType($flags) && $this->hasLink($flags);
157 }
158
159 /** Returns true if this phone's type matches the flags
160 */
161 public function hasType($flags) {
162 $flags = $flags & Profile::PHONE_TYPE_ANY;
163 return (
164 ($flags == Profile::PHONE_TYPE_ANY)
165 ||
166 (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX)
167 ||
168 (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED)
169 ||
170 (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE)
171 );
172 }
173
174 /** Returns true if this phone's link matches the flags
175 */
176 public function hasLink($flags) {
177 $flags = $flags & Profile::PHONE_LINK_ANY;
178 return (
179 ($flags == Profile::PHONE_LINK_ANY)
180 ||
181 (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY)
182 ||
183 (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB)
184 ||
185 (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS)
186 ||
187 (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE)
188 );
189 }
190 }
191 // }}}
192 // {{{ class Company
193 class Company
194 {
195 public $id;
196 public $name;
197 public $acronym;
198 public $url;
199 public $phone = null;
200 public $address = null;
201
202 /** Fields are:
203 * $id, $name, $acronym, $url
204 */
205 public function __construct($data)
206 {
207 foreach ($data as $key => $val) {
208 $this->$key = $val;
209 }
210 }
211
212 public function setPhone(Phone &$phone)
213 {
214 if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
215 $this->phone = $phone;
216 }
217 }
218
219 public function setAddress(Address &$address)
220 {
221 if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
222 $this->address = $address;
223 }
224 }
225
226 }
227 // }}}
228 // {{{ class Job
229 class Job
230 {
231 public $pid;
232 public $id;
233
234 public $company = null;
235 private $phones = array();
236 private $address = null;
237
238 public $jobid;
239
240 public $description;
241 public $user_site;
242 public $user_email;
243
244 public $sector;
245 public $subsector;
246 public $subsubsector;
247
248 /** Fields are:
249 * pid, id, company_id, description, url, email
250 */
251 public function __construct($data)
252 {
253 foreach ($data as $key => $val) {
254 $this->$key = $val;
255 }
256 $this->company = CompanyList::get($this->jobid);
257 }
258
259 public function phones()
260 {
261 return $this->phones;
262 }
263
264 public function address()
265 {
266 return $this->address;
267 }
268
269 public function addPhone(Phone &$phone)
270 {
271 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
272 $this->phones[] = $phone;
273 }
274 }
275
276 public function setAddress(Address $address)
277 {
278 if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
279 $this->address = $address;
280 }
281 }
282 }
283 // }}}
284 // {{{ class Address
285 class Address
286 {
287 const LINK_JOB = 'job';
288 const LINK_COMPANY = 'hq';
289 const LINK_PROFILE = 'home';
290
291 public $flags;
292 public $link_id;
293 public $link_type;
294
295 public $text;
296 public $postalCode;
297 public $latitude;
298 public $longitude;
299
300 public $locality;
301 public $subAdministrativeArea;
302 public $administrativeArea;
303 public $country;
304
305 public $comment;
306
307 private $phones = array();
308
309 /** Fields are:
310 * pîd, id, link_id, link_type, flags, text, postcode, country
311 */
312 public function __construct($data)
313 {
314 foreach ($data as $key => $val) {
315 $this->$key = $val;
316 }
317 $this->flags = new PlFlagSet($this->flags);
318 }
319
320 public function addPhone(Phone &$phone)
321 {
322 if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
323 $this->phones[] = $phone;
324 }
325 }
326
327 public function phones()
328 {
329 return $this->phones;
330 }
331
332 public function hasFlag($flag)
333 {
334 if (!$this->flags instanceof PlFlagSet) {
335 $this->flags = new PlFlagSet($this->flags);
336 }
337 return $this->flags->hasFlag($flag);
338 }
339 }
340 // }}}
341 // {{{ class Education
342 class Education
343 {
344 public $id;
345 public $pid;
346
347 public $entry_year;
348 public $grad_year;
349 public $program;
350 public $flags;
351
352 public $school;
353 public $school_short;
354 public $school_url;
355 public $country;
356
357 public $degree;
358 public $degree_short;
359 public $degree_level;
360
361 public $field;
362
363 public function __construct(array $data)
364 {
365 foreach ($data as $key => $val) {
366 $this->$key = $val;
367 }
368 $this->flags = new PlFlagSet($this->flags);
369 }
370 }
371 // }}}
372
373 // {{{ class ProfileEducation [ Field ]
374 class ProfileEducation extends ProfileField
375 {
376 private $educations = array();
377
378 public function __construct(PlInnerSubIterator $it)
379 {
380 $this->pid = $it->value();
381 while ($edu = $it->next()) {
382 $this->educations[$edu['id']] = new Education($edu);
383 }
384 }
385
386 public function get($flags, $limit)
387 {
388 $educations = array();
389 $year = getdate();
390 $year = $year['year'];
391 $nb = 0;
392 foreach ($this->educations as $id => $edu) {
393 if (
394 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
395 ||
396 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
397 ||
398 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
399 ||
400 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
401 ||
402 ($flags & Profile::EDUCATION_ALL)
403 ) {
404 $educations[$id] = $edu;
405 ++$nb;
406 }
407 if ($limit != null && $nb >= $limit) {
408 break;
409 }
410 }
411 return $educations;
412 }
413
414 public static function fetchData(array $pids, ProfileVisibility $visibility)
415 {
416 $data = XDB::iterator('SELECT pe.id, pe.pid,
417 pe.entry_year, pe.grad_year, pe.program, pe.flags,
418 pee.name AS school, pee.abbreviation AS school_short,
419 pee.url AS school_url, gc.countryFR AS country,
420 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
421 pefe.field
422 FROM profile_education AS pe
423 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
424 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
425 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
426 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
427 WHERE pe.pid IN {?}
428 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
429 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
430 $pids);
431
432 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
433 }
434 }
435 // }}}
436 // {{{ class ProfileMedals [ Field ]
437 class ProfileMedals extends ProfileField
438 {
439 public $medals = array();
440
441 public function __construct(PlInnerSubIterator $it)
442 {
443 $this->pid = $it->value();
444 while ($medal = $it->next()) {
445 $this->medals[$medal['mid']] = $medal;
446 }
447 }
448
449 public static function fetchData(array $pids, ProfileVisibility $visibility)
450 {
451 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
452 FROM profile_medals AS pm
453 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
454 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
455 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
456 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
457 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
458 $pids, $visibility->levels());
459
460 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
461 }
462 }
463 // }}}
464 // {{{ class ProfileNetworking [ Field ]
465 class ProfileNetworking extends ProfileField
466 {
467 private $networks = array();
468
469 public function __construct(PlInnerSubIterator $it)
470 {
471 $this->pid = $it->value();
472 while ($network = $it->next()) {
473 $network['network_type'] = new PlFlagSet($network['network_type']);
474 $this->networks[$network['id']] = $network;
475 }
476 }
477
478 public static function fetchData(array $pids, ProfileVisibility $visibility)
479 {
480 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
481 FROM profile_networking AS pn
482 LEFT JOIN profile_networking_enum AS pne USING(nwid)
483 WHERE pid IN {?} AND pub IN {?}
484 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
485 pn.nwid, id',
486 $pids, $visibility->levels());
487
488 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
489 }
490
491 public function get($flags, $limit = null)
492 {
493 if (!$flags) {
494 $flags = Profile::NETWORKING_ALL;
495 }
496 $nws = array();
497 $nb = 0;
498 foreach ($this->networks as $id => $nw) {
499 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
500 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
501 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
502 ($flags & Profile::NETWORKING_ALL)) {
503 $nws[$id] = $nw;
504 ++$nb;
505 if (isset($limit) && $nb >= $limit) {
506 break;
507 }
508 }
509 }
510 return $nws;
511 }
512 }
513 // }}}
514 // {{{ class ProfileCorps [ Field ]
515 class ProfileCorps extends ProfileField
516 {
517 public $original;
518 public $current;
519
520 public $original_name;
521 public $original_abbrev;
522 public $original_still_exists;
523
524 public $current_name;
525 public $current_abbrev;
526 public $current_still_exists;
527 public $current_rank;
528 public $current_rank_abbrev;
529
530 public function __construct(array $data)
531 {
532 foreach ($data as $key => $val) {
533 $this->$key = $val;
534 }
535 }
536
537 public static function fetchData(array $pids, ProfileVisibility $visibility)
538 {
539 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
540 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
541 pceo.still_exists AS original_still_exists,
542 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
543 pcec.still_exists AS current_still_exists,
544 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
545 rankid
546 FROM profile_corps AS pc
547 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
548 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
549 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
550 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
551 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
552 $pids, $visibility->levels());
553
554 return $data;
555 }
556 }
557 // }}}
558 // {{{ class ProfileMentoringSectors [ Field ]
559 class ProfileMentoringSectors extends ProfileField
560 {
561 public $sectors = array();
562
563 public function __construct(PlInnerSubIterator $it)
564 {
565 $this->pid = $it->value();
566 while ($sector = $it->next()) {
567 $this->sectors[] = $sector;
568 }
569 }
570
571 public static function fetchData(array $pids, ProfileVisibility $visibility)
572 {
573 $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
574 FROM profile_mentor_sector AS pms
575 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
576 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
577 WHERE pms.pid IN {?}
578 ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids),
579 $pids);
580
581 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
582 }
583 }
584 // }}}
585 // {{{ class ProfileMentoringCountries [ Field ]
586 class ProfileMentoringCountries extends ProfileField
587 {
588 public $countries = array();
589
590 public function __construct(PlInnerSubIterator $it)
591 {
592 $this->pid = $it->value();
593 while ($country = $it->next()) {
594 $this->countries[$country['id']] = $country['name'];
595 }
596 }
597
598 public static function fetchData(array $pids, ProfileVisibility $visibility)
599 {
600 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
601 FROM profile_mentor_country AS pmc
602 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
603 WHERE pmc.pid IN {?}
604 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
605 $pids);
606
607 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
608 }
609 }
610 // }}}
611
612 /** Loading of data for a Profile :
613 * 1) load jobs, addresses, phones
614 * 2) attach phones to addresses, jobs and profiles
615 * 3) attach addresses to jobs and profiles
616 */
617
618 // {{{ class ProfileAddresses [ Field ]
619 class ProfileAddresses extends ProfileField
620 {
621 private $addresses = array();
622
623 public function __construct(PlIterator $it)
624 {
625 if ($it instanceof PlInnerSubIterator) {
626 $this->pid = $it->value();
627 }
628
629 while ($addr = $it->next()) {
630 $this->addresses[] = new Address($addr);
631 }
632 }
633
634 public function get($flags, $limit = null)
635 {
636 $res = array();
637 $nb = 0;
638 foreach ($this->addresses as $addr) {
639 if (
640 ($flags & Profile::ADDRESS_ALL)
641 ||
642 (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
643 ||
644 (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
645 ||
646 (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == 'home')
647 ||
648 (($flags & Profile::ADDRESS_PRO) && $addr->link_type == 'job')
649 ) {
650 $res[] = $addr;
651 $nb++;
652 }
653 if ($limit != null && $nb == $limit) {
654 break;
655 }
656 }
657 return $res;
658 }
659
660 public static function fetchData(array $pids, ProfileVisibility $visibility)
661 {
662 $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
663 IF(pa.type = \'home\', pid, jobid) AS link_id,
664 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
665 gl.name AS locality, gas.name AS subAdministrativeArea,
666 ga.name AS administrativeArea, gc.countryFR AS country
667 FROM profile_addresses AS pa
668 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
669 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
670 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
671 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
672 WHERE pa.pid in {?} AND pa.pub IN {?}
673 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
674 $pids, $visibility->levels());
675
676 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
677 }
678
679 public function addPhones(ProfilePhones $phones)
680 {
681 $p = $phones->get(0);
682 foreach ($p as $phone) {
683 if ($phone->link_type == Phone::LINK_ADDRESS && array_key_exists($phone->link_id, $this->addresses)) {
684 $this->addresses[$phone->link_id]->addPhone($phone);
685 }
686 }
687 }
688 }
689 // }}}
690 // {{{ class ProfilePhones [ Field ]
691 class ProfilePhones extends ProfileField
692 {
693 private $phones = array();
694
695 public function __construct(PlInnerSubIterator $it)
696 {
697 $this->pid = $it->value();
698 while ($phone = $it->next()) {
699 $this->phones[] = new Phone($phone);
700 }
701 }
702
703 public function get($flags, $limit = null)
704 {
705 $phones = array();
706 $nb = 0;
707 foreach ($this->phones as $id => $phone) {
708 if ($phone->hasFlags($flags)) {
709 $phones[$id] = $phone;
710 ++$nb;
711 if ($limit != null && $nb == $limit) {
712 break;
713 }
714 }
715 }
716 return $phones;
717 }
718
719 public static function fetchData(array $pids, ProfileVisibility $visibility)
720 {
721 $data = XDB::iterator('SELECT tel_type AS type, search_tel AS search, display_tel AS display, link_type, comment, pid
722 FROM profile_phones
723 WHERE pid IN {?} AND pub IN {?}
724 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
725 $pids, $visibility->levels());
726 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
727 }
728 }
729 // }}}
730 // {{{ class ProfileJobs [ Field ]
731 class ProfileJobs extends ProfileField
732 {
733 private $jobs = array();
734
735 public function __construct(PlInnerSubIterator $jobs)
736 {
737 $this->pid = $jobs->value();
738 while ($job = $jobs->next()) {
739 $this->jobs[$job['id']] = new Job($job);
740 }
741 }
742
743 public static function fetchData(array $pids, ProfileVisibility $visibility)
744 {
745 CompanyList::preload($pids);
746 $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
747 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
748 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
749 pjssse.name AS subsubsector
750 FROM profile_job AS pj
751 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
752 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
753 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
754 WHERE pj.pid IN {?} AND pj.pub IN {?}
755 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
756 pj.id',
757 $visibility->levels(), $pids, $visibility->levels());
758 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
759 }
760
761 public function get($flags, $limit = null)
762 {
763 $jobs = array();
764 $nb = 0;
765 foreach ($this->jobs as $id => $job) {
766 $jobs[$id] = $job;
767 ++$nb;
768 if ($limit != null && $nb >= $limit) {
769 break;
770 }
771 }
772 return $jobs;
773 }
774
775 public function addPhones(ProfilePhones $phones)
776 {
777 $p = $phones->get(0);
778 foreach ($p as $phone) {
779 if ($phone->link_type == Phone::LINK_JOB && array_key_exists($phone->link_id, $this->jobs)) {
780 $this->jobs[$phone->link_id]->addPhones($phone);
781 }
782 }
783 }
784
785 public function addAddresses(ProfileAddresses $addresses)
786 {
787 $a = $addresses->get(Profile::ADDRESS_PRO);
788 foreach ($a as $address) {
789 if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
790 $this->jobs[$address->link_id]->setAddress($address);
791 }
792 }
793 }
794
795 public function addCompanies(array $companies)
796 {
797 foreach ($this->jobs as $job) {
798 $this->company = $companies[$job->jobid];
799 }
800 }
801 }
802 // }}}
803
804 // {{{ class CompanyList
805 class CompanyList
806 {
807 static private $fullload = false;
808 static private $companies = array();
809
810 static public function preload($pids = array())
811 {
812 if (self::$fullload) {
813 return;
814 }
815 // Load raw data
816 if (count($pids)) {
817 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
818 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
819 } else {
820 $join = '';
821 $where = '';
822 }
823
824 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
825 pa.flags, pa.text, pa.postalCode, pa.countryId,
826 pa.type, pa.pub
827 FROM profile_job_enum AS pje
828 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
829 ' . $join . '
830 ' . $where);
831 while ($row = $it->next()) {
832 $cp = new Company($row);
833 $addr = new Address($row);
834 $cp->setAddress($addr);
835 self::$companies[$row['id']] = $cp;
836 }
837
838 // TODO: add phones to addresses
839 if (count($pids) == 0) {
840 self::$fullload = true;
841 }
842 }
843
844 static public function get($id)
845 {
846 if (!array_key_exists($id, self::$companies)) {
847 self::preload();
848 }
849 return self::$companies[$id];
850 }
851 }
852
853 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
854 ?>