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