Replace sectors by job terms in profile and search (job and mentoring).
[platal.git] / include / profilefields.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 // {{{ class ProfileField
23 /** To store a "field" from the profile
24 * Provides functions for loading a batch of such data
25 */
26 abstract class ProfileField
27 {
28 public static $fields = array(
29 Profile::FETCH_ADDRESSES => 'ProfileAddresses',
30 Profile::FETCH_CORPS => 'ProfileCorps',
31 Profile::FETCH_EDU => 'ProfileEducation',
32 Profile::FETCH_JOBS => 'ProfileJobs',
33 Profile::FETCH_MEDALS => 'ProfileMedals',
34 Profile::FETCH_NETWORKING => 'ProfileNetworking',
35 Profile::FETCH_PHONES => 'ProfilePhones',
36 Profile::FETCH_MENTOR_SECTOR => 'ProfileMentoringSectors',
37 Profile::FETCH_MENTOR_COUNTRY => 'ProfileMentoringCountries',
38 Profile::FETCH_JOB_TERMS => 'ProfileJobTerms',
39 Profile::FETCH_MENTOR_TERMS => 'ProfileMentoringTerms',
40 );
41
42 /** The profile to which this field belongs
43 */
44 public $pid;
45
46 /** Fetches data from the database for the given pids, compatible with
47 * the visibility context.
48 * @param $pids An array of pids
49 * @param $visibility The level of visibility fetched fields must have
50 * @return a PlIterator yielding data suitable for a "new ProfileBlah($data)"
51 *
52 * MUST be reimplemented for each kind of ProfileField.
53 */
54 public static function fetchData(array $pids, ProfileVisibility $visibility)
55 {
56 return PlIteratorUtils::emptyIterator();
57 }
58
59 public static function buildForPID($cls, $pid, ProfileVisibility $visibility)
60 {
61 $res = self::buildFromPIDs($cls, array($pid), $visibility);
62 return array_pop($res);
63 }
64
65 /** Build a list of ProfileFields from a set of pids
66 * @param $cls The name of the field to create ('ProfileMedals', ...)
67 * @param $pids An array of pids
68 * @param $visibility An array of allowed visibility contexts
69 * @return An array of $pid => ProfileField
70 */
71 public static function buildFromPIDs($cls, array $pids, ProfileVisibility $visibility)
72 {
73 $it = new ProfileFieldIterator($cls, $pids, $visibility);
74 $res = array();
75 while ($pf = $it->next()) {
76 $res[$pf->pid] = $pf;
77 }
78 return $res;
79 }
80
81 public static function getForPID($cls, $pid, ProfileVisibility $visibility)
82 {
83 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
84 return $it->next();
85 }
86 }
87 // }}}
88
89 // {{{ class ProfileFieldIterator
90 class ProfileFieldIterator implements PlIterator
91 {
92 private $data;
93 private $cls;
94
95 public function __construct($cls, array $pids, ProfileVisibility $visibility)
96 {
97 if (is_numeric($cls) && isset(ProfileField::$fields[$cls])) {
98 $cls = ProfileField::$fields[$cls];
99 }
100 $this->data = call_user_func(array($cls, 'fetchData'), $pids, $visibility);
101 $this->cls = $cls;
102 }
103
104 public function next()
105 {
106 $d = $this->data->next();
107 if ($d == null) {
108 return null;
109 } else {
110 $cls = $this->cls;
111 return new $cls($d);
112 }
113 }
114
115 public function total()
116 {
117 return $this->data->total();
118 }
119
120 public function first()
121 {
122 return $this->data->first();
123 }
124
125 public function last()
126 {
127 return $this->data->last();
128 }
129 }
130 // }}}
131
132 // {{{ class Company
133 class Company
134 {
135 public $id;
136 public $name;
137 public $acronym;
138 public $url;
139 public $phone = null;
140 public $address = null;
141
142 /** Fields are:
143 * $id, $name, $acronym, $url
144 */
145 public function __construct($data)
146 {
147 foreach ($data as $key => $val) {
148 $this->$key = $val;
149 }
150 }
151
152 public function setPhone(Phone &$phone)
153 {
154 if ($phone->linkType() == Phone::LINK_COMPANY && $phone->linkId() == $this->id) {
155 $this->phone = $phone;
156 }
157 }
158
159 public function setAddress(Address &$address)
160 {
161 if ($address->link_type == Address::LINK_COMPANY && $address->link_id == $this->id) {
162 $this->address = $address;
163 }
164 }
165
166 }
167 // }}}
168 // {{{ class Job
169 class Job
170 {
171 public $pid;
172 public $id;
173
174 public $company = null;
175 public $phones = array();
176 public $address = null;
177 public $terms = array();
178
179 public $jobid;
180
181 public $description;
182 public $user_site;
183 public $user_email;
184
185 public $sector;
186 public $subsector;
187 public $subsubsector;
188
189 /** Fields are:
190 * pid, id, company_id, description, url, email
191 */
192 public function __construct($data)
193 {
194 foreach ($data as $key => $val) {
195 $this->$key = $val;
196 }
197 $this->company = CompanyList::get($this->jobid);
198 if (is_null($this->company)) {
199 require_once 'validations.inc.php';
200 $entreprise = ProfileValidate::get_typed_requests($this->pid, 'entreprise');
201 $this->company = new Company(array('name' => $entreprise[$this->id]->name));
202 }
203 }
204
205 public function phones()
206 {
207 return $this->phones;
208 }
209
210 public function address()
211 {
212 return $this->address;
213 }
214
215 public function addPhone(Phone &$phone)
216 {
217 if ($phone->linkType() == Phone::LINK_JOB && $phone->linkId() == $this->id && $phone->pid() == $this->pid) {
218 $this->phones[$phone->uniqueId()] = $phone;
219 }
220 }
221
222 public function setAddress(Address $address)
223 {
224 if ($address->link_type == Address::LINK_JOB && $address->link_id == $this->id && $address->pid == $this->pid) {
225 $this->address = $address;
226 }
227 }
228
229 public function addTerm(JobTerm &$term)
230 {
231 $this->terms[$term->jtid] = $term;
232 }
233 }
234 // }}}
235 // {{{ class JobTerm
236 class JobTerm
237 {
238 public $jtid;
239 public $full_name;
240 public $pid;
241 public $jid;
242
243 /** Fields are:
244 * pid, jid, jtid, full_name
245 */
246 public function __construct($data)
247 {
248 foreach ($data as $key => $val) {
249 $this->$key = $val;
250 }
251 }
252 }
253 // }}}
254 // {{{ class Address
255 class Address
256 {
257 const LINK_JOB = 'job';
258 const LINK_COMPANY = 'hq';
259 const LINK_PROFILE = 'home';
260
261 public $flags;
262 public $id; // The ID of the address among those associated with its link
263 public $link_id; // The ID of the object to which the address is linked (profile, job, company)
264 public $link_type;
265
266 public $text;
267 public $postalCode;
268 public $latitude;
269 public $longitude;
270
271 public $locality;
272 public $subAdministrativeArea;
273 public $administrativeArea;
274 public $country;
275
276 public $comment;
277
278 private $phones = array();
279
280 /** Fields are:
281 * pîd, id, link_id, link_type, flags, text, postcode, country
282 */
283 public function __construct($data)
284 {
285 foreach ($data as $key => $val) {
286 $this->$key = $val;
287 }
288 $this->flags = new PlFlagSet($this->flags);
289 }
290
291 public function uid() {
292 $uid = $this->link_type . '_';
293 if ($this->link_type != self::LINK_COMPANY) {
294 $uid .= $this->pid . '_';
295 }
296 $uid .= $this->link_id . '_' . $this->id;
297 }
298
299 public function addPhone(Phone &$phone)
300 {
301 if (
302 $phone->linkType() == Phone::LINK_ADDRESS && $phone->linkId() == $this->id &&
303 ($this->link_type == self::LINK_COMPANY || $phone->pid() == $this->pid) ) {
304 $this->phones[$phone->uniqueId()] = $phone;
305 }
306 }
307
308 public function phones()
309 {
310 return $this->phones;
311 }
312
313 public function hasFlag($flag)
314 {
315 if (!$this->flags instanceof PlFlagSet) {
316 $this->flags = new PlFlagSet($this->flags);
317 }
318 return $this->flags->hasFlag($flag);
319 }
320 }
321 // }}}
322 // {{{ class Education
323 class Education
324 {
325 public $id;
326 public $pid;
327
328 public $entry_year;
329 public $grad_year;
330 public $program;
331 public $flags;
332
333 public $school;
334 public $school_short;
335 public $school_url;
336 public $country;
337
338 public $degree;
339 public $degree_short;
340 public $degree_level;
341
342 public $field;
343
344 public function __construct(array $data)
345 {
346 foreach ($data as $key => $val) {
347 $this->$key = $val;
348 }
349 $this->flags = new PlFlagSet($this->flags);
350 }
351 }
352 // }}}
353
354 // {{{ class ProfileEducation [ Field ]
355 class ProfileEducation extends ProfileField
356 {
357 private $educations = array();
358
359 public function __construct(PlInnerSubIterator $it)
360 {
361 $this->pid = $it->value();
362 while ($edu = $it->next()) {
363 $this->educations[$edu['id']] = new Education($edu);
364 }
365 }
366
367 public function get($flags, $limit)
368 {
369 $educations = array();
370 $year = getdate();
371 $year = $year['year'];
372 $nb = 0;
373 foreach ($this->educations as $id => $edu) {
374 if (
375 (($flags & Profile::EDUCATION_MAIN) && $edu->flags->hasFlag('primary'))
376 ||
377 (($flags & Profile::EDUCATION_EXTRA) && !$edu->flags->hasFlag('primary'))
378 ||
379 (($flags & Profile::EDUCATION_FINISHED) && $edu->grad_year <= $year)
380 ||
381 (($flags & Profile::EDUCATION_CURRENT) && $edu->grad_year > $year)
382 ||
383 ($flags & Profile::EDUCATION_ALL)
384 ) {
385 $educations[$id] = $edu;
386 ++$nb;
387 }
388 if ($limit != null && $nb >= $limit) {
389 break;
390 }
391 }
392 return $educations;
393 }
394
395 public static function fetchData(array $pids, ProfileVisibility $visibility)
396 {
397 $data = XDB::iterator('SELECT pe.id, pe.pid,
398 pe.entry_year, pe.grad_year, pe.program, pe.flags,
399 pee.name AS school, pee.abbreviation AS school_short,
400 pee.url AS school_url, gc.countryFR AS country,
401 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
402 pefe.field
403 FROM profile_education AS pe
404 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
405 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
406 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
407 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
408 WHERE pe.pid IN {?}
409 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
410 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
411 $pids);
412
413 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
414 }
415 }
416 // }}}
417 // {{{ class ProfileMedals [ Field ]
418 class ProfileMedals extends ProfileField
419 {
420 public $medals = array();
421
422 public function __construct(PlInnerSubIterator $it)
423 {
424 $this->pid = $it->value();
425 while ($medal = $it->next()) {
426 $this->medals[$medal['mid']] = $medal;
427 }
428 }
429
430 public static function fetchData(array $pids, ProfileVisibility $visibility)
431 {
432 $data = XDB::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
433 FROM profile_medals AS pm
434 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
435 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
436 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
437 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
438 ORDER BY ' . XDB::formatCustomOrder('pm.pid', $pids),
439 $pids, $visibility->levels());
440
441 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
442 }
443 }
444 // }}}
445 // {{{ class ProfileNetworking [ Field ]
446 class ProfileNetworking extends ProfileField
447 {
448 private $networks = array();
449
450 public function __construct(PlInnerSubIterator $it)
451 {
452 $this->pid = $it->value();
453 while ($network = $it->next()) {
454 $network['network_type'] = new PlFlagSet($network['network_type']);
455 $this->networks[$network['id']] = $network;
456 }
457 }
458
459 public static function fetchData(array $pids, ProfileVisibility $visibility)
460 {
461 $data = XDB::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
462 FROM profile_networking AS pn
463 LEFT JOIN profile_networking_enum AS pne USING(nwid)
464 WHERE pid IN {?} AND pub IN {?}
465 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
466 pn.nwid, id',
467 $pids, $visibility->levels());
468
469 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
470 }
471
472 public function get($flags, $limit = null)
473 {
474 $nws = array();
475 $nb = 0;
476 foreach ($this->networks as $id => $nw) {
477 if (($flags & Profile::NETWORKING_WEB) && $nw['network_type']->hasFlag('web') ||
478 ($flags & Profile::NETWORKING_IM) && $nw['network_type']->hasFlag('im') ||
479 ($flags & Profile::NETWORKING_SOCIAL) && $nw['network_type']->hasFlag('social') ||
480 ($flags == Profile::NETWORKING_ALL)) {
481 $nws[$id] = $nw;
482 ++$nb;
483 if (isset($limit) && $nb >= $limit) {
484 break;
485 }
486 }
487 }
488 return $nws;
489 }
490 }
491 // }}}
492 // {{{ class ProfileCorps [ Field ]
493 class ProfileCorps extends ProfileField
494 {
495 public $original;
496 public $current;
497
498 public $original_name;
499 public $original_abbrev;
500 public $original_still_exists;
501
502 public $current_name;
503 public $current_abbrev;
504 public $current_still_exists;
505 public $current_rank;
506 public $current_rank_abbrev;
507
508 public function __construct(array $data)
509 {
510 foreach ($data as $key => $val) {
511 $this->$key = $val;
512 }
513 }
514
515 public static function fetchData(array $pids, ProfileVisibility $visibility)
516 {
517 $data = XDB::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
518 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
519 pceo.still_exists AS original_still_exists,
520 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
521 pcec.still_exists AS current_still_exists,
522 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
523 rankid
524 FROM profile_corps AS pc
525 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
526 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
527 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
528 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
529 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
530 $pids, $visibility->levels());
531
532 return $data;
533 }
534 }
535 // }}}
536 // {{{ class ProfileMentoringSectors [ Field ]
537 class ProfileMentoringSectors extends ProfileField
538 {
539 public $sectors = array();
540
541 public function __construct(PlInnerSubIterator $it)
542 {
543 $this->pid = $it->value();
544 while ($sector = $it->next()) {
545 $this->sectors[] = $sector;
546 }
547 }
548
549 public static function fetchData(array $pids, ProfileVisibility $visibility)
550 {
551 $data = XDB::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
552 FROM profile_mentor_sector AS pms
553 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
554 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
555 WHERE pms.pid IN {?}
556 ORDER BY ' . XDB::formatCustomOrder('pms.pid', $pids),
557 $pids);
558
559 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
560 }
561 }
562 // }}}
563 // {{{ class ProfileMentoringCountries [ Field ]
564 class ProfileMentoringCountries extends ProfileField
565 {
566 public $countries = array();
567
568 public function __construct(PlInnerSubIterator $it)
569 {
570 $this->pid = $it->value();
571 while ($country = $it->next()) {
572 $this->countries[$country['id']] = $country['name'];
573 }
574 }
575
576 public static function fetchData(array $pids, ProfileVisibility $visibility)
577 {
578 $data = XDB::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
579 FROM profile_mentor_country AS pmc
580 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
581 WHERE pmc.pid IN {?}
582 ORDER BY ' . XDB::formatCustomOrder('pmc.pid', $pids),
583 $pids);
584
585 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
586 }
587 }
588 // }}}
589
590 /** Loading of data for a Profile :
591 * 1) load jobs, addresses, phones
592 * 2) attach phones to addresses, jobs and profiles
593 * 3) attach addresses to jobs and profiles
594 */
595
596 // {{{ Database schema (profile_address, profile_jobs)
597 /** The database for this is very unclear, so here is a little schema :
598 * profile_job describes a Job, links to:
599 * - a Profile, through `pid`
600 * - a Company, through `jobid`
601 * The `id` field is the id of this job in the list of the jobs of its profile
602 *
603 * profile_addresses describes an Address, which
604 * related to either a Profile, a Job or a Company:
605 * - for a Profile:
606 * - `type` is set to 'home'
607 * - `pid` is set to the related profile pid
608 * - `id` is the id of the address in the list of those related to that profile
609 * - `jobid` is empty
610 *
611 * - for a Company:
612 * - `type` is set to 'hq'
613 * - `pid` is set to 0
614 * - `jobid` is set to the id of the company
615 * - `id` is set to 0 (only one address per Company)
616 *
617 * - for a Job:
618 * - `type` is set to 'job'
619 * - `pid` is set to the pid of the Profile of the related Job
620 * - `jobid` is set to the Company of the job (this information is redundant
621 * with that of the row of profile_job for the related job)
622 * - `id` is the id of the job to which we refer (i.e `profile_job.id`)
623 *
624 * For the documentation of the phone table, please see classes/phone.php.
625 *
626 * The possible relations are as follow:
627 * An Address can be linked to a Company, a Profile, a Job
628 * A Job is linked to a Company and a Profile
629 */
630 // }}}
631
632 // {{{ class ProfileAddresses [ Field ]
633 class ProfileAddresses extends ProfileField
634 {
635 private $addresses = array();
636
637 public function __construct(PlIterator $it)
638 {
639 if ($it instanceof PlInnerSubIterator) {
640 $this->pid = $it->value();
641 }
642
643 while ($addr = $it->next()) {
644 $this->addresses[] = new Address($addr);
645 }
646 }
647
648 public function get($flags, $limit = null)
649 {
650 $res = array();
651 $nb = 0;
652 foreach ($this->addresses as $addr) {
653 if (
654 (($flags & Profile::ADDRESS_MAIN) && $addr->hasFlag('current'))
655 ||
656 (($flags & Profile::ADDRESS_POSTAL) && $addr->hasFlag('mail'))
657 ||
658 (($flags & Profile::ADDRESS_PERSO) && $addr->link_type == Address::LINK_PROFILE)
659 ||
660 (($flags & Profile::ADDRESS_PRO) && $addr->link_type == Address::LINK_JOB)
661 ) {
662 $res[] = $addr;
663 $nb++;
664 }
665 if ($limit != null && $nb == $limit) {
666 break;
667 }
668 }
669 return $res;
670 }
671
672 public static function fetchData(array $pids, ProfileVisibility $visibility)
673 {
674 $data = XDB::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
675 IF(pa.type = \'home\', pid, IF(pa.type = \'job\', pa.id, jobid)) AS link_id,
676 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
677 gl.name AS locality, gas.name AS subAdministrativeArea,
678 ga.name AS administrativeArea, gc.countryFR AS country
679 FROM profile_addresses AS pa
680 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
681 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
682 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
683 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
684 WHERE pa.pid in {?} AND pa.pub IN {?}
685 ORDER BY ' . XDB::formatCustomOrder('pid', $pids),
686 $pids, $visibility->levels());
687
688 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
689 }
690
691 public function addPhones(ProfilePhones $phones)
692 {
693 $p = $phones->get(Profile::PHONE_LINK_ADDRESS | Profile::PHONE_TYPE_ANY);
694 foreach ($p as $phone) {
695 if ($phone->linkType() == Phone::LINK_ADDRESS && array_key_exists($phone->linkId(), $this->addresses)) {
696 $this->addresses[$phone->linkId()]->addPhone($phone);
697 }
698 }
699 }
700 }
701 // }}}
702 // {{{ class ProfilePhones [ Field ]
703 class ProfilePhones extends ProfileField
704 {
705 private $phones = array();
706
707 public function __construct(PlInnerSubIterator $it)
708 {
709 $this->pid = $it->value();
710 while ($phone = $it->next()) {
711 $this->phones[] = new Phone($phone);
712 }
713 }
714
715 public function get($flags, $limit = null)
716 {
717 $phones = array();
718 $nb = 0;
719 foreach ($this->phones as $id => $phone) {
720 if ($phone->hasFlags($flags)) {
721 $phones[$id] = $phone;
722 ++$nb;
723 if ($limit != null && $nb == $limit) {
724 break;
725 }
726 }
727 }
728 return $phones;
729 }
730
731 public static function fetchData(array $pids, ProfileVisibility $visibility)
732 {
733 $it = Phone::iterate($pids, array(), array(), $visibility->levels());
734 return PlIteratorUtils::subIterator($it->value(), PlIteratorUtils::arrayValueCallback('pid'));
735 }
736 }
737 // }}}
738 // {{{ class ProfileJobs [ Field ]
739 class ProfileJobs extends ProfileField
740 {
741 private $jobs = array();
742
743 public function __construct(PlInnerSubIterator $jobs)
744 {
745 $this->pid = $jobs->value();
746 while ($job = $jobs->next()) {
747 $this->jobs[$job['id']] = new Job($job);
748 }
749 }
750
751 public static function fetchData(array $pids, ProfileVisibility $visibility)
752 {
753 CompanyList::preload($pids);
754 $data = XDB::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
755 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
756 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
757 pjssse.name AS subsubsector
758 FROM profile_job AS pj
759 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
760 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
761 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
762 WHERE pj.pid IN {?} AND pj.pub IN {?}
763 ORDER BY ' . XDB::formatCustomOrder('pid', $pids) . ',
764 pj.id',
765 $visibility->levels(), $pids, $visibility->levels());
766 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
767 }
768
769 public function get($flags, $limit = null)
770 {
771 $jobs = array();
772 $nb = 0;
773 foreach ($this->jobs as $id => $job) {
774 $jobs[$id] = $job;
775 ++$nb;
776 if ($limit != null && $nb >= $limit) {
777 break;
778 }
779 }
780 return $jobs;
781 }
782
783 public function addPhones(ProfilePhones $phones)
784 {
785 $p = $phones->get(Profile::PHONE_LINK_JOB | Profile::PHONE_TYPE_ANY);
786 foreach ($p as $phone) {
787 if ($phone->linkType() == Phone::LINK_JOB && array_key_exists($phone->linkId(), $this->jobs)) {
788 $this->jobs[$phone->linkId()]->addPhone($phone);
789 }
790 }
791 }
792
793 public function addAddresses(ProfileAddresses $addresses)
794 {
795 $a = $addresses->get(Profile::ADDRESS_PRO);
796 foreach ($a as $address) {
797 if ($address->link_type == Address::LINK_JOB && array_key_exists($address->link_id, $this->jobs)) {
798 $this->jobs[$address->link_id]->setAddress($address);
799 }
800 }
801 }
802
803 public function addCompanies(array $companies)
804 {
805 foreach ($this->jobs as $job) {
806 $this->company = $companies[$job->jobid];
807 }
808 }
809
810 public function addJobTerms(ProfileJobTerms $jobterms)
811 {
812 $terms = $jobterms->get();
813 foreach ($terms as $term) {
814 if ($this->pid == $term->pid && array_key_exists($term->jid, $this->jobs)) {
815 $this->jobs[$term->jid]->addTerm(&$term);
816 }
817 }
818 }
819 }
820 // }}}
821 // {{{ class ProfileJobTerms [ Field ]
822 class ProfileJobTerms extends ProfileField
823 {
824 private $jobterms = array();
825
826 public function __construct(PlInnerSubIterator $it)
827 {
828 $this->pid = $it->value();
829 while ($term = $it->next()) {
830 $this->jobterms[] = new JobTerm($term);
831 }
832 }
833
834 public function get()
835 {
836 return $this->jobterms;
837 }
838
839 public static function fetchData(array $pids, ProfileVisibility $visibility)
840 {
841 $data = XDB::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
842 FROM profile_job_term AS jt
843 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
844 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
845 WHERE jt.pid IN {?} AND j.pub IN {?}
846 ORDER BY ' . XDB::formatCustomOrder('jt.pid', $pids),
847 $pids, $visibility->levels());
848 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
849 }
850 }
851 // }}}
852 // {{{ class ProfileMentoringTerms [ Field ]
853 class ProfileMentoringTerms extends ProfileJobTerms
854 {
855 public static function fetchData(array $pids, ProfileVisibility $visibility)
856 {
857 $data = XDB::iterator('SELECT mt.jtid, jte.full_name, mt.pid
858 FROM profile_mentor_term AS mt
859 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
860 WHERE mt.pid IN {?}
861 ORDER BY ' . XDB::formatCustomOrder('mt.pid', $pids),
862 $pids);
863 return PlIteratorUtils::subIterator($data, PlIteratorUtils::arrayValueCallback('pid'));
864 }
865 }
866 // }}}
867 // {{{ class CompanyList
868 class CompanyList
869 {
870 static private $fullload = false;
871 static private $companies = array();
872
873 static public function preload($pids = array())
874 {
875 if (self::$fullload) {
876 return;
877 }
878 // Load raw data
879 if (count($pids)) {
880 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
881 $where = 'WHERE profile_job.pid IN ' . XDB::formatArray($pids);
882 } else {
883 $join = '';
884 $where = '';
885 }
886
887 $it = XDB::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
888 pa.flags, pa.text, pa.postalCode, pa.countryId,
889 pa.type, pa.pub
890 FROM profile_job_enum AS pje
891 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
892 ' . $join . '
893 ' . $where);
894 $newcompanies = array();
895 while ($row = $it->next()) {
896 $cp = new Company($row);
897 $addr = new Address($row);
898 $cp->setAddress($addr);
899 if (!array_key_exists($row['id'], self::$companies)) {
900 $newcompanies[] = $row['id'];
901 }
902 self::$companies[$row['id']] = $cp;
903 }
904
905 // Add phones to hq
906 if (count($newcompanies)) {
907 $it = Phone::iterate(array(), array(Phone::LINK_COMPANY), $newcompanies);
908 while ($phone = $it->next()) {
909 self::$companies[$phone->linkId()]->setPhone($phone);
910 }
911 }
912
913 if (count($pids) == 0) {
914 self::$fullload = true;
915 }
916 }
917
918 static public function get($id)
919 {
920 if (!array_key_exists($id, self::$companies)) {
921 self::preload();
922 }
923 return self::$companies[$id];
924 }
925 }
926
927 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
928 ?>