2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 // {{{ class ProfileField
23 /** To store a "field" from the profile
24 * Provides functions for loading a batch of such data
26 abstract class ProfileField
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',
42 /** The profile to which this field belongs
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)"
52 * MUST be reimplemented for each kind of ProfileField.
54 public static function fetchData(array $pids, ProfileVisibility
$visibility)
56 return PlIteratorUtils
::emptyIterator();
59 public static function buildForPID($cls, $pid, ProfileVisibility
$visibility)
61 $res = self
::buildFromPIDs($cls, array($pid), $visibility);
62 return array_pop($res);
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
71 public static function buildFromPIDs($cls, array $pids, ProfileVisibility
$visibility)
73 $it = new ProfileFieldIterator($cls, $pids, $visibility);
75 while ($pf = $it->next()) {
81 public static function getForPID($cls, $pid, ProfileVisibility
$visibility)
83 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
89 // {{{ class ProfileFieldIterator
90 class ProfileFieldIterator
implements PlIterator
95 public function __construct($cls, array $pids, ProfileVisibility
$visibility)
97 if (is_numeric($cls) && isset(ProfileField
::$fields[$cls])) {
98 $cls = ProfileField
::$fields[$cls];
100 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
104 public function next()
106 $d = $this->data
->next();
115 public function total()
117 return $this->data
->total();
120 public function first()
122 return $this->data
->first();
125 public function last()
127 return $this->data
->last();
139 public $phone = null
;
140 public $address = null
;
143 * $id, $name, $acronym, $url
145 public function __construct($data)
147 foreach ($data as $key => $val) {
152 public function setPhone(Phone
&$phone)
154 if ($phone->linkType() == Phone
::LINK_COMPANY
&& $phone->linkId() == $this->id
) {
155 $this->phone
= $phone;
159 public function setAddress(Address
&$address)
161 if ($address->type
== Address
::LINK_COMPANY
&& $address->jobid
== $this->id
) {
162 $this->address
= $address;
169 /** profile_job describes a Job, links to:
170 * - a Profile, through `pid`
171 * - a Company, through `jobid`
172 * The `id` field is the id of this job in the list of the jobs of its profile
174 * For the documentation of the phone table, please see classes/phone.php.
175 * For the documentation of the address table, please see classes/address.php.
177 * The possible relations are as follow:
178 * A Job is linked to a Company and a Profile
185 public $company = null
;
186 public $phones = array();
187 public $address = null
;
188 public $terms = array();
198 public $subsubsector;
201 * pid, id, company_id, description, url, email
203 public function __construct($data)
205 foreach ($data as $key => $val) {
208 $this->company
= CompanyList
::get($this->jobid
);
209 if (is_null($this->company
)) {
210 $entreprise = ProfileValidate
::get_typed_requests($this->pid
, 'entreprise');
211 $this->company
= new Company(array('name' => $entreprise[$this->id
]->name
));
215 public function phones()
217 return $this->phones
;
220 public function address()
222 return $this->address
;
225 public function addPhone(Phone
&$phone)
227 if ($phone->linkType() == Phone
::LINK_JOB
&& $phone->linkId() == $this->id
&& $phone->pid() == $this->pid
) {
228 $this->phones
[$phone->uniqueId()] = $phone;
232 public function setAddress(Address
$address)
234 if ($address->type
== Address
::LINK_JOB
&& $address->jobid
== $this->id
&& $address->pid
== $this->pid
) {
235 $this->address
= $address;
239 public function addTerm(JobTerm
&$term)
241 $this->terms
[$term->jtid
] = $term;
254 * pid, jid, jtid, full_name
256 public function __construct($data)
258 foreach ($data as $key => $val) {
264 // {{{ class Education
276 public $school_short;
281 public $degree_short;
282 public $degree_level;
286 public function __construct(array $data)
288 foreach ($data as $key => $val) {
291 $this->flags
= new PlFlagSet($this->flags
);
296 // {{{ class ProfileEducation [ Field ]
297 class ProfileEducation
extends ProfileField
299 private $educations = array();
301 public function __construct(PlInnerSubIterator
$it)
303 $this->pid
= $it->value();
304 while ($edu = $it->next()) {
305 $this->educations
[$edu['id']] = new Education($edu);
309 public function get($flags, $limit)
311 $educations = array();
313 $year = $year['year'];
315 foreach ($this->educations
as $id => $edu) {
317 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
319 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
321 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
323 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
325 ($flags & Profile
::EDUCATION_ALL
)
327 $educations[$id] = $edu;
330 if ($limit != null
&& $nb >= $limit) {
337 public static function fetchData(array $pids, ProfileVisibility
$visibility)
339 $data = XDB
::iterator('SELECT pe.id, pe.pid,
340 pe.entry_year, pe.grad_year, pe.program, pe.flags,
341 pee.name AS school, pee.abbreviation AS school_short,
342 pee.url AS school_url, gc.countryFR AS country,
343 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
345 FROM profile_education AS pe
346 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
347 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
348 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
349 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
351 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
352 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
355 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
359 // {{{ class ProfileMedals [ Field ]
360 class ProfileMedals
extends ProfileField
362 public $medals = array();
364 public function __construct(PlInnerSubIterator
$it)
366 $this->pid
= $it->value();
367 while ($medal = $it->next()) {
368 $this->medals
[$medal['mid']] = $medal;
372 public static function fetchData(array $pids, ProfileVisibility
$visibility)
374 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
375 FROM profile_medals AS pm
376 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
377 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
378 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
379 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
380 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
381 $pids, $visibility->levels());
383 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
387 // {{{ class ProfileNetworking [ Field ]
388 class ProfileNetworking
extends ProfileField
390 private $networks = array();
392 public function __construct(PlInnerSubIterator
$it)
394 $this->pid
= $it->value();
395 while ($network = $it->next()) {
396 $network['network_type'] = new PlFlagSet($network['network_type']);
397 $this->networks
[$network['id']] = $network;
401 public static function fetchData(array $pids, ProfileVisibility
$visibility)
403 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
404 FROM profile_networking AS pn
405 LEFT JOIN profile_networking_enum AS pne USING(nwid)
406 WHERE pid IN {?} AND pub IN {?}
407 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
409 $pids, $visibility->levels());
411 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
414 public function get($flags, $limit = null
)
418 foreach ($this->networks
as $id => $nw) {
419 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
420 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
421 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
422 ($flags == Profile
::NETWORKING_ALL
)) {
425 if (isset($limit) && $nb >= $limit) {
434 // {{{ class ProfileCorps [ Field ]
435 class ProfileCorps
extends ProfileField
440 public $original_name;
441 public $original_abbrev;
442 public $original_still_exists;
444 public $current_name;
445 public $current_abbrev;
446 public $current_still_exists;
447 public $current_rank;
448 public $current_rank_abbrev;
450 public function __construct(array $data)
452 foreach ($data as $key => $val) {
457 public static function fetchData(array $pids, ProfileVisibility
$visibility)
459 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
460 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
461 pceo.still_exists AS original_still_exists,
462 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
463 pcec.still_exists AS current_still_exists,
464 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
466 FROM profile_corps AS pc
467 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
468 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
469 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
470 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
471 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
472 $pids, $visibility->levels());
478 // {{{ class ProfileMentoringSectors [ Field ]
479 class ProfileMentoringSectors
extends ProfileField
481 public $sectors = array();
483 public function __construct(PlInnerSubIterator
$it)
485 $this->pid
= $it->value();
486 while ($sector = $it->next()) {
487 $this->sectors
[] = $sector;
491 public static function fetchData(array $pids, ProfileVisibility
$visibility)
493 $data = XDB
::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
494 FROM profile_mentor_sector AS pms
495 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
496 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
498 ORDER BY ' . XDB
::formatCustomOrder('pms.pid', $pids),
501 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
505 // {{{ class ProfileMentoringCountries [ Field ]
506 class ProfileMentoringCountries
extends ProfileField
508 public $countries = array();
510 public function __construct(PlInnerSubIterator
$it)
512 $this->pid
= $it->value();
513 while ($country = $it->next()) {
514 $this->countries
[$country['id']] = $country['name'];
518 public static function fetchData(array $pids, ProfileVisibility
$visibility)
520 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
521 FROM profile_mentor_country AS pmc
522 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
524 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
527 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
531 // {{{ class ProfileAddresses [ Field ]
532 class ProfileAddresses
extends ProfileField
534 private $addresses = array();
536 public function __construct(PlInnerSubIterator
$it)
538 $this->pid
= $it->value();
539 while ($address = $it->next()) {
540 $this->addresses
[] = new Address($address);
544 public function get($flags, $limit = null
)
546 $addresses = array();
548 foreach ($this->addresses
as $address) {
549 if ((($flags & Profile
::ADDRESS_MAIN
) && $address->hasFlag('current'))
550 ||
(($flags & Profile
::ADDRESS_POSTAL
) && $address->hasFlag('mail'))
551 ||
(($flags & Profile
::ADDRESS_PERSO
) && $address->type
== Address
::LINK_PROFILE
)
552 ||
(($flags & Profile
::ADDRESS_PRO
) && $address->type
== Address
::LINK_JOB
)
554 $addresses[] = $address;
557 if ($limit != null
&& $nb == $limit) {
564 public static function fetchData(array $pids, ProfileVisibility
$visibility)
566 $it = Address
::iterate($pids, array(), array(), $visibility->levels());
567 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
570 public function addPhones(ProfilePhones
$phones)
572 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
573 foreach ($p as $phone) {
574 if ($phone->linkType() == Phone
::LINK_ADDRESS
&& array_key_exists($phone->linkId(), $this->addresses
)) {
575 $this->addresses
[$phone->linkId()]->addPhone($phone);
581 // {{{ class ProfilePhones [ Field ]
582 class ProfilePhones
extends ProfileField
584 private $phones = array();
586 public function __construct(PlInnerSubIterator
$it)
588 $this->pid
= $it->value();
589 while ($phone = $it->next()) {
590 $this->phones
[] = new Phone($phone);
594 public function get($flags, $limit = null
)
598 foreach ($this->phones
as $id => $phone) {
599 if ($phone->hasFlags($flags)) {
600 $phones[$id] = $phone;
602 if ($limit != null
&& $nb == $limit) {
610 public static function fetchData(array $pids, ProfileVisibility
$visibility)
612 $it = Phone
::iterate($pids, array(), array(), $visibility->levels());
613 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
617 // {{{ class ProfileJobs [ Field ]
618 class ProfileJobs
extends ProfileField
620 private $jobs = array();
622 public function __construct(PlInnerSubIterator
$jobs)
624 $this->pid
= $jobs->value();
625 while ($job = $jobs->next()) {
626 $this->jobs
[$job['id']] = new Job($job);
630 public static function fetchData(array $pids, ProfileVisibility
$visibility)
632 CompanyList
::preload($pids);
633 $data = XDB
::iterator('SELECT id, pid, description, url as user_site, jobid,
634 IF(email_pub IN {?}, email, NULL) AS user_email
636 WHERE pid IN {?} AND pub IN {?}
637 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ', id',
638 $visibility->levels(), $pids, $visibility->levels());
639 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
642 public function get($flags, $limit = null
)
646 foreach ($this->jobs
as $id => $job) {
649 if ($limit != null
&& $nb >= $limit) {
656 public function addPhones(ProfilePhones
$phones)
658 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
659 foreach ($p as $phone) {
660 if ($phone->linkType() == Phone
::LINK_JOB
&& array_key_exists($phone->linkId(), $this->jobs
)) {
661 $this->jobs
[$phone->linkId()]->addPhone($phone);
666 public function addAddresses(ProfileAddresses
$addresses)
668 $a = $addresses->get(Profile
::ADDRESS_PRO
);
669 foreach ($a as $address) {
670 if ($address->type
== Address
::LINK_JOB
&& array_key_exists($address->jobid
, $this->jobs
)) {
671 $this->jobs
[$address->jobid
]->setAddress($address);
676 public function addCompanies(array $companies)
678 foreach ($this->jobs
as $job) {
679 $this->company
= $companies[$job->jobid
];
683 public function addJobTerms(ProfileJobTerms
$jobterms)
685 $terms = $jobterms->get();
686 foreach ($terms as $term) {
687 if ($this->pid
== $term->pid
&& array_key_exists($term->jid
, $this->jobs
)) {
688 $this->jobs
[$term->jid
]->addTerm(&$term);
694 // {{{ class ProfileJobTerms [ Field ]
695 class ProfileJobTerms
extends ProfileField
697 private $jobterms = array();
699 public function __construct(PlInnerSubIterator
$it)
701 $this->pid
= $it->value();
702 while ($term = $it->next()) {
703 $this->jobterms
[] = new JobTerm($term);
707 public function get()
709 return $this->jobterms
;
712 public static function fetchData(array $pids, ProfileVisibility
$visibility)
714 $data = XDB
::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
715 FROM profile_job_term AS jt
716 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
717 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
718 WHERE jt.pid IN {?} AND j.pub IN {?}
719 ORDER BY ' . XDB
::formatCustomOrder('jt.pid', $pids),
720 $pids, $visibility->levels());
721 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
725 // {{{ class ProfileMentoringTerms [ Field ]
726 class ProfileMentoringTerms
extends ProfileJobTerms
728 public static function fetchData(array $pids, ProfileVisibility
$visibility)
730 $data = XDB
::iterator('SELECT mt.jtid, jte.full_name, mt.pid
731 FROM profile_mentor_term AS mt
732 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
734 ORDER BY ' . XDB
::formatCustomOrder('mt.pid', $pids),
736 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
740 // {{{ class CompanyList
743 static private $fullload = false
;
744 static private $companies = array();
746 static public function preload($pids = array())
748 if (self
::$fullload) {
753 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
754 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
760 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
761 pa.flags, pa.text, pa.postalCode, pa.countryId,
763 FROM profile_job_enum AS pje
764 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
767 $newcompanies = array();
768 while ($row = $it->next()) {
769 $cp = new Company($row);
770 $addr = new Address($row);
771 $cp->setAddress($addr);
772 if (!array_key_exists($row['id'], self
::$companies)) {
773 $newcompanies[] = $row['id'];
775 self
::$companies[$row['id']] = $cp;
779 if (count($newcompanies)) {
780 $it = Phone
::iterate(array(), array(Phone
::LINK_COMPANY
), $newcompanies);
781 while ($phone = $it->next()) {
782 self
::$companies[$phone->linkId()]->setPhone($phone);
786 if (count($pids) == 0) {
787 self
::$fullload = true
;
791 static public function get($id)
793 if (!array_key_exists($id, self
::$companies)) {
796 return self
::$companies[$id];
800 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: