0f6d3df4fafdc9948d7c4867a052fb8304eaa24e
[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 = 'pro';
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 public $id;
146
147 /** Fields are :
148 * $type, $search, $display, $link_type, $link_id, $comment, $pid, $id
149 */
150 public function __construct($data)
151 {
152 foreach ($data as $key => $val) {
153 $this->$key = $val;
154 }
155 }
156
157 public function hasFlags($flags) {
158 return $this->hasType($flags) && $this->hasLink($flags);
159 }
160
161 /** Returns true if this phone's type matches the flags
162 */
163 public function hasType($flags) {
164 $flags = $flags & Profile::PHONE_TYPE_ANY;
165 return (
166 ($flags == Profile::PHONE_TYPE_ANY)
167 ||
168 (($flags & Profile::PHONE_TYPE_FAX) && $this->type == self::TYPE_FAX)
169 ||
170 (($flags & Profile::PHONE_TYPE_FIXED) && $this->type == self::TYPE_FIXED)
171 ||
172 (($flags & Profile::PHONE_TYPE_MOBILE) && $this->type == self::TYPE_MOBILE)
173 );
174 }
175
176 /** Returns true if this phone's link matches the flags
177 */
178 public function hasLink($flags) {
179 $flags = $flags & Profile::PHONE_LINK_ANY;
180 return (
181 ($flags == Profile::PHONE_LINK_ANY)
182 ||
183 (($flags & Profile::PHONE_LINK_COMPANY) && $this->link_type == self::LINK_COMPANY)
184 ||
185 (($flags & Profile::PHONE_LINK_JOB) && $this->link_type == self::LINK_JOB)
186 ||
187 (($flags & Profile::PHONE_LINK_ADDRESS) && $this->link_type == self::LINK_ADDRESS)
188 ||
189 (($flags & Profile::PHONE_LINK_PROFILE) && $this->link_type == self::LINK_PROFILE)
190 );
191 }
192 }
193 // }}}
194 // {{{ class Company
195 class Company
196 {
197 public $id;
198 public $name;
199 public $acronym;
200 public $url;
201 public $phone = null;
202 public $address = null;
203
204 /** Fields are:
205 * $id, $name, $acronym, $url
206 */
207 public function __construct($data)
208 {
209 foreach ($data as $key => $val) {
210 $this->$key = $val;
211 }
212 }
213
214 public function setPhone(Phone &$phone)
215 {
216 if ($phone->link_type == Phone::LINK_COMPANY && $phone->link_id == $this->id) {
217 $this->phone = $phone;
218 }
219 }
220
221 public function setAddress(Address &$address)
222 {
223 if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
224 $this->address = $address;
225 }
226 }
227
228 }
229 // }}}
230 // {{{ class Job
231 class Job
232 {
233 public $pid;
234 public $id;
235
236 public $company = null;
237 private $phones = array();
238 private $address = null;
239
240 public $jobid;
241
242 public $description;
243 public $user_site;
244 public $user_email;
245
246 public $sector;
247 public $subsector;
248 public $subsubsector;
249
250 /** Fields are:
251 * pid, id, company_id, description, url, email
252 */
253 public function __construct($data)
254 {
255 foreach ($data as $key => $val) {
256 $this->$key = $val;
257 }
258 $this->company = CompanyList::get($this->jobid);
259 }
260
261 public function phones()
262 {
263 return $this->phones;
264 }
265
266 public function address()
267 {
268 return $this->address;
269 }
270
271 public function addPhone(Phone &$phone)
272 {
273 if ($phone->link_type == Phone::LINK_JOB && $phone->link_id == $this->id && $phone->pid == $this->pid) {
274 $this->phones[] = $phone;
275 }
276 }
277
278 public function setAddress(Address $address)
279 {
280 if ($address->link_id == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
281 $this->address = $address;
282 }
283 }
284 }
285 // }}}
286 // {{{ class Address
287 class Address
288 {
289 const LINK_JOB = 'job';
290 const LINK_COMPANY = 'hq';
291 const LINK_PROFILE = 'home';
292
293 public $flags;
294 public $link_id;
295 public $link_type;
296
297 public $text;
298 public $postalCode;
299 public $latitude;
300 public $longitude;
301
302 public $locality;
303 public $subAdministrativeArea;
304 public $administrativeArea;
305 public $country;
306
307 public $comment;
308
309 private $phones = array();
310
311 /** Fields are:
312 * pîd, id, link_id, link_type, flags, text, postcode, country
313 */
314 public function __construct($data)
315 {
316 foreach ($data as $key => $val) {
317 $this->$key = $val;
318 }
319 $this->flags = new PlFlagSet($this->flags);
320 }
321
322 public function addPhone(Phone &$phone)
323 {
324 if ($phone->link_type == Phone::LINK_ADDRESS && $phone->link_id == $this->id && $phone->pid == $this->pid) {
325 $this->phones[$phone->id] = $phone;
326 }
327 }
328
329 public function phones()
330 {
331 return $this->phones;
332 }
333
334 public function hasFlag($flag)
335 {
336 if (!$this->flags instanceof PlFlagSet) {
337 $this->flags = new PlFlagSet($this->flags);
338 }
339 return $this->flags->hasFlag($flag);
340 }
341 }
342 // }}}
343 // {{{ class Education
344 class Education
345 {
346 public $id;
347 public $pid;
348
349 public $entry_year;
350 public $grad_year;
351 public $program;
352 public $flags;
353
354 public $school;
355 public $school_short;
356 public $school_url;
357 public $country;
358
359 public $degree;
360 public $degree_short;
361 public $degree_level;
362
363 public $field;
364
365 public function __construct(array $data)
366 {
367 foreach ($data as $key => $val) {
368 $this->$key = $val;
369 }
370 $this->flags = new PlFlagSet($this->flags);
371 }
372 }
373 // }}}
374
375 // {{{ class ProfileEducation [ Field ]
376 class ProfileEducation extends ProfileField
377 {
378 private $educations = array();
379
380 public function __construct(PlInnerSubIterator $it)
381 {
382 $this->pid = $it->value();
383 while ($edu = $it->next()) {
384 $this->educations[$edu['id']] = new Education($edu);
385 }
386 }
387
388 public function get($flags, $limit)
389 {
390 $educations = array();
391 $year = getdate();
392 $year = $year['year'];
393 $nb = 0;
394 foreach ($this->educations as $id => $edu) {
395 if (
396 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
397 ||
398 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
399 ||
400 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
401 ||
402 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
403 ||
404 ($flags & Profile::EDUCATION_ALL)
405 ) {
406 $educations[$id] = $edu;
407 ++$nb;
408 }
409 if ($limit != null && $nb >= $limit) {
410 break;
411 }
412 }
413 return $educations;
414 }
415
416 public static function fetchData(array $pids, ProfileVisibility $visibility)
417 {
418 $data = XDB::iterator('SELECT pe.id, pe.pid,
419 pe.entry_year, pe.grad_year, pe.program, pe.flags,
420 pee.name AS school, pee.abbreviation AS school_short,
421 pee.url AS school_url, gc.countryFR AS country,
422 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
423 pefe.field
424 FROM profile_education AS pe
425 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
426 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
427 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
428 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
429 WHERE pe.pid IN {?}
430 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
431 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
432 $pids);
433
434 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
435 }
436 }
437 // }}}
438 // {{{ class ProfileMedals [ Field ]
439 class ProfileMedals extends ProfileField
440 {
441 public $medals = array();
442
443 public function __construct(PlInnerSubIterator $it)
444 {
445 $this->pid = $it->value();
446 while ($medal = $it->next()) {
447 $this->medals[$medal['mid']] = $medal;
448 }
449 }
450
451 public static function fetchData(array $pids, ProfileVisibility $visibility)
452 {
453 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
454 FROM profile_medals AS pm
455 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
456 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
457 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
458 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
459 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
460 $pids, $visibility->levels());
461
462 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
463 }
464 }
465 // }}}
466 // {{{ class ProfileNetworking [ Field ]
467 class ProfileNetworking extends ProfileField
468 {
469 private $networks = array();
470
471 public function __construct(PlInnerSubIterator $it)
472 {
473 $this->pid = $it->value();
474 while ($network = $it->next()) {
475 $network['network_type'] = new PlFlagSet($network['network_type']);
476 $this->networks[$network['id']] = $network;
477 }
478 }
479
480 public static function fetchData(array $pids, ProfileVisibility $visibility)
481 {
482 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
483 FROM profile_networking AS pn
484 LEFT JOIN profile_networking_enum AS pne USING(nwid)
485 WHERE pid IN {?} AND pub IN {?}
486 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
487 pn.nwid, id',
488 $pids, $visibility->levels());
489
490 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
491 }
492
493 public function get($flags, $limit = null)
494 {
495 if (!$flags) {
496 $flags = Profile::NETWORKING_ALL;
497 }
498 $nws = array();
499 $nb = 0;
500 foreach ($this->networks as $id => $nw) {
501 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
502 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
503 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
504 ($flags & Profile::NETWORKING_ALL)) {
505 $nws[$id] = $nw;
506 ++$nb;
507 if (isset($limit) && $nb >= $limit) {
508 break;
509 }
510 }
511 }
512 return $nws;
513 }
514 }
515 // }}}
516 // {{{ class ProfileCorps [ Field ]
517 class ProfileCorps extends ProfileField
518 {
519 public $original;
520 public $current;
521
522 public $original_name;
523 public $original_abbrev;
524 public $original_still_exists;
525
526 public $current_name;
527 public $current_abbrev;
528 public $current_still_exists;
529 public $current_rank;
530 public $current_rank_abbrev;
531
532 public function __construct(array $data)
533 {
534 foreach ($data as $key => $val) {
535 $this->$key = $val;
536 }
537 }
538
539 public static function fetchData(array $pids, ProfileVisibility $visibility)
540 {
541 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
542 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
543 pceo.still_exists AS original_still_exists,
544 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
545 pcec.still_exists AS current_still_exists,
546 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
547 rankid
548 FROM profile_corps AS pc
549 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
550 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
551 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
552 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
553 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
554 $pids, $visibility->levels());
555
556 return $data;
557 }
558 }
559 // }}}
560 // {{{ class ProfileMentoringSectors [ Field ]
561 class ProfileMentoringSectors extends ProfileField
562 {
563 public $sectors = array();
564
565 public function __construct(PlInnerSubIterator $it)
566 {
567 $this->pid = $it->value();
568 while ($sector = $it->next()) {
569 $this->sectors[] = $sector;
570 }
571 }
572
573 public static function fetchData(array $pids, ProfileVisibility $visibility)
574 {
575 $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
576 FROM profile_mentor_sector AS pms
577 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
578 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
579 WHERE pms.pid IN {?}
580 ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids),
581 $pids);
582
583 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
584 }
585 }
586 // }}}
587 // {{{ class ProfileMentoringCountries [ Field ]
588 class ProfileMentoringCountries extends ProfileField
589 {
590 public $countries = array();
591
592 public function __construct(PlInnerSubIterator $it)
593 {
594 $this->pid = $it->value();
595 while ($country = $it->next()) {
596 $this->countries[$country['id']] = $country['name'];
597 }
598 }
599
600 public static function fetchData(array $pids, ProfileVisibility $visibility)
601 {
602 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
603 FROM profile_mentor_country AS pmc
604 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
605 WHERE pmc.pid IN {?}
606 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
607 $pids);
608
609 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
610 }
611 }
612 // }}}
613
614 /** Loading of data for a Profile :
615 * 1) load jobs, addresses, phones
616 * 2) attach phones to addresses, jobs and profiles
617 * 3) attach addresses to jobs and profiles
618 */
619
620 // {{{ class ProfileAddresses [ Field ]
621 class ProfileAddresses extends ProfileField
622 {
623 private $addresses = array();
624
625 public function __construct(PlIterator $it)
626 {
627 if ($it instanceof PlInnerSubIterator) {
628 $this->pid = $it->value();
629 }
630
631 while ($addr = $it->next()) {
632 $this->addresses[$addr['id']] = new Address($addr);
633 }
634 }
635
636 public function get($flags, $limit = null)
637 {
638 $res = array();
639 $nb = 0;
640 foreach ($this->addresses as $addr) {
641 if (
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 == Address::LINK_PROFILE)
647 ||
648 (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_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(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
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, link_id, tel_id AS id
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(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
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]->addPhone($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 $newcompanies = array();
832 while ($row = $it->next()) {
833 $cp = new Company($row);
834 $addr = new Address($row);
835 $cp->setAddress($addr);
836 if (!array_key_exists($row['id'], self::$companies)) {
837 $newcompanies[] = $row['id'];
838 }
839 self::$companies[$row['id']] = $cp;
840 }
841
842 // TODO: determine whether there can be phones attached to a hq's address
843 // Add phones to hq
844 if (count($newcompanies)) {
845 $it = XDB::iterator('SELECT search_tel AS search, display_tel AS display, comment, link_id, tel_type AS type, link_type, tel_id AS id
846 FROM profile_phones
847 WHERE link_id IN {?} AND link_type = \'hq\'',
848 $newcompanies);
849 while ($row = $it->next()) {
850 $p = new Phone($row);
851 self::$companies[$row['link_id']]->setPhone($p);
852 }
853 }
854
855 if (count($pids) == 0) {
856 self::$fullload = true;
857 }
858 }
859
860 static public function get($id)
861 {
862 if (!array_key_exists($id, self::$companies)) {
863 self::preload();
864 }
865 return self::$companies[$id];
866 }
867 }
868
869 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
870 ?>