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',
40 /** The profile to which this field belongs
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)"
50 * MUST be reimplemented for each kind of ProfileField.
52 public static function fetchData(array $pids, ProfileVisibility
$visibility)
54 return PlIteratorUtils
::emptyIterator();
57 public static function buildForPID($cls, $pid, ProfileVisibility
$visibility)
59 $res = self
::buildFromPIDs($cls, array($pid), $visibility);
60 return array_pop($res);
63 /** Build a list of ProfileFields from a set of pids
64 * @param $cls The name of the field to create ('ProfileMedals', ...)
65 * @param $pids An array of pids
66 * @param $visibility An array of allowed visibility contexts
67 * @return An array of $pid => ProfileField
69 public static function buildFromPIDs($cls, array $pids, ProfileVisibility
$visibility)
71 $it = new ProfileFieldIterator($cls, $pids, $visibility);
73 while ($pf = $it->next()) {
79 public static function getForPID($cls, $pid, ProfileVisibility
$visibility)
81 $it = new ProfileFieldIterator($cls, array($pid), $visibility);
87 // {{{ class ProfileFieldIterator
88 class ProfileFieldIterator
implements PlIterator
93 public function __construct($cls, array $pids, ProfileVisibility
$visibility)
95 $this->data
= call_user_func(array($cls, 'fetchData'), $pids, $visibility);
99 public function next()
101 $d = $this->data
->next();
110 public function total()
112 return $this->data
->total();
115 public function first()
117 return $this->data
->first();
120 public function last()
122 return $this->data
->last();
134 public $phone = null
;
135 public $address = null
;
138 * $id, $name, $acronym, $url
140 public function __construct($data)
142 foreach ($data as $key => $val) {
147 public function setPhone(Phone
&$phone)
149 if ($phone->linkType() == Phone
::LINK_COMPANY
&& $phone->linkId() == $this->id
) {
150 $this->phone
= $phone;
154 public function setAddress(Address
&$address)
156 if ($address->link_type
== Address
::LINK_COMPANY
&& $address->link_id
== $this->id
) {
157 $this->address
= $address;
169 public $company = null
;
170 public $phones = array();
171 public $address = null
;
181 public $subsubsector;
184 * pid, id, company_id, description, url, email
186 public function __construct($data)
188 foreach ($data as $key => $val) {
191 $this->company
= CompanyList
::get($this->jobid
);
192 if (is_null($this->company
)) {
193 require_once 'validations.inc.php';
194 $entreprise = ProfileValidate
::get_typed_requests($this->pid
, 'entreprise');
195 $this->company
= new Company(array('name' => $entreprise[$this->id
]->name
));
199 public function phones()
201 return $this->phones
;
204 public function address()
206 return $this->address
;
209 public function addPhone(Phone
&$phone)
211 if ($phone->linkType() == Phone
::LINK_JOB
&& $phone->linkId() == $this->id
&& $phone->pid() == $this->pid
) {
212 $this->phones
[$phone->uniqueId()] = $phone;
216 public function setAddress(Address
$address)
218 if ($address->link_type
== Address
::LINK_JOB
&& $address->link_id
== $this->id
&& $address->pid
== $this->pid
) {
219 $this->address
= $address;
227 const LINK_JOB
= 'job';
228 const LINK_COMPANY
= 'hq';
229 const LINK_PROFILE
= 'home';
232 public $id; // The ID of the address among those associated with its link
233 public $link_id; // The ID of the object to which the address is linked (profile, job, company)
242 public $subAdministrativeArea;
243 public $administrativeArea;
248 private $phones = array();
251 * pîd, id, link_id, link_type, flags, text, postcode, country
253 public function __construct($data)
255 foreach ($data as $key => $val) {
258 $this->flags
= new PlFlagSet($this->flags
);
261 public function uid() {
262 $uid = $this->link_type
. '_';
263 if ($this->link_type
!= self
::LINK_COMPANY
) {
264 $uid .= $this->pid
. '_';
266 $uid .= $this->link_id
. '_' . $this->id
;
269 public function addPhone(Phone
&$phone)
272 $phone->linkType() == Phone
::LINK_ADDRESS
&& $phone->linkId() == $this->id
&&
273 ($this->link_type
== self
::LINK_COMPANY ||
$phone->pid() == $this->pid
) ) {
274 $this->phones
[$phone->uniqueId()] = $phone;
278 public function phones()
280 return $this->phones
;
283 public function hasFlag($flag)
285 if (!$this->flags
instanceof PlFlagSet
) {
286 $this->flags
= new PlFlagSet($this->flags
);
288 return $this->flags
->hasFlag($flag);
292 // {{{ class Education
304 public $school_short;
309 public $degree_short;
310 public $degree_level;
314 public function __construct(array $data)
316 foreach ($data as $key => $val) {
319 $this->flags
= new PlFlagSet($this->flags
);
324 // {{{ class ProfileEducation [ Field ]
325 class ProfileEducation
extends ProfileField
327 private $educations = array();
329 public function __construct(PlInnerSubIterator
$it)
331 $this->pid
= $it->value();
332 while ($edu = $it->next()) {
333 $this->educations
[$edu['id']] = new Education($edu);
337 public function get($flags, $limit)
339 $educations = array();
341 $year = $year['year'];
343 foreach ($this->educations
as $id => $edu) {
345 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
347 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
349 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
351 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
353 ($flags & Profile
::EDUCATION_ALL
)
355 $educations[$id] = $edu;
358 if ($limit != null
&& $nb >= $limit) {
365 public static function fetchData(array $pids, ProfileVisibility
$visibility)
367 $data = XDB
::iterator('SELECT pe.id, pe.pid,
368 pe.entry_year, pe.grad_year, pe.program, pe.flags,
369 pee.name AS school, pee.abbreviation AS school_short,
370 pee.url AS school_url, gc.countryFR AS country,
371 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
373 FROM profile_education AS pe
374 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
375 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
376 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
377 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
379 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
380 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
383 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
387 // {{{ class ProfileMedals [ Field ]
388 class ProfileMedals
extends ProfileField
390 public $medals = array();
392 public function __construct(PlInnerSubIterator
$it)
394 $this->pid
= $it->value();
395 while ($medal = $it->next()) {
396 $this->medals
[$medal['mid']] = $medal;
400 public static function fetchData(array $pids, ProfileVisibility
$visibility)
402 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
403 FROM profile_medals AS pm
404 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
405 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
406 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
407 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
408 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
409 $pids, $visibility->levels());
411 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
415 // {{{ class ProfileNetworking [ Field ]
416 class ProfileNetworking
extends ProfileField
418 private $networks = array();
420 public function __construct(PlInnerSubIterator
$it)
422 $this->pid
= $it->value();
423 while ($network = $it->next()) {
424 $network['network_type'] = new PlFlagSet($network['network_type']);
425 $this->networks
[$network['id']] = $network;
429 public static function fetchData(array $pids, ProfileVisibility
$visibility)
431 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
432 FROM profile_networking AS pn
433 LEFT JOIN profile_networking_enum AS pne USING(nwid)
434 WHERE pid IN {?} AND pub IN {?}
435 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
437 $pids, $visibility->levels());
439 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
442 public function get($flags, $limit = null
)
446 foreach ($this->networks
as $id => $nw) {
447 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
448 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
449 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
450 ($flags == Profile
::NETWORKING_ALL
)) {
453 if (isset($limit) && $nb >= $limit) {
462 // {{{ class ProfileCorps [ Field ]
463 class ProfileCorps
extends ProfileField
468 public $original_name;
469 public $original_abbrev;
470 public $original_still_exists;
472 public $current_name;
473 public $current_abbrev;
474 public $current_still_exists;
475 public $current_rank;
476 public $current_rank_abbrev;
478 public function __construct(array $data)
480 foreach ($data as $key => $val) {
485 public static function fetchData(array $pids, ProfileVisibility
$visibility)
487 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
488 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
489 pceo.still_exists AS original_still_exists,
490 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
491 pcec.still_exists AS current_still_exists,
492 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
494 FROM profile_corps AS pc
495 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
496 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
497 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
498 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
499 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
500 $pids, $visibility->levels());
506 // {{{ class ProfileMentoringSectors [ Field ]
507 class ProfileMentoringSectors
extends ProfileField
509 public $sectors = array();
511 public function __construct(PlInnerSubIterator
$it)
513 $this->pid
= $it->value();
514 while ($sector = $it->next()) {
515 $this->sectors
[] = $sector;
519 public static function fetchData(array $pids, ProfileVisibility
$visibility)
521 $data = XDB
::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
522 FROM profile_mentor_sector AS pms
523 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
524 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
526 ORDER BY ' . XDB
::formatCustomOrder('pms.pid', $pids),
529 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
533 // {{{ class ProfileMentoringCountries [ Field ]
534 class ProfileMentoringCountries
extends ProfileField
536 public $countries = array();
538 public function __construct(PlInnerSubIterator
$it)
540 $this->pid
= $it->value();
541 while ($country = $it->next()) {
542 $this->countries
[$country['id']] = $country['name'];
546 public static function fetchData(array $pids, ProfileVisibility
$visibility)
548 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
549 FROM profile_mentor_country AS pmc
550 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
552 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
555 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
560 /** Loading of data for a Profile :
561 * 1) load jobs, addresses, phones
562 * 2) attach phones to addresses, jobs and profiles
563 * 3) attach addresses to jobs and profiles
566 // {{{ Database schema (profile_address, profile_jobs)
567 /** The database for this is very unclear, so here is a little schema :
568 * profile_job describes a Job, links to:
569 * - a Profile, through `pid`
570 * - a Company, through `jobid`
571 * The `id` field is the id of this job in the list of the jobs of its profile
573 * profile_addresses describes an Address, which
574 * related to either a Profile, a Job or a Company:
576 * - `type` is set to 'home'
577 * - `pid` is set to the related profile pid
578 * - `id` is the id of the address in the list of those related to that profile
582 * - `type` is set to 'hq'
583 * - `pid` is set to 0
584 * - `jobid` is set to the id of the company
585 * - `id` is set to 0 (only one address per Company)
588 * - `type` is set to 'job'
589 * - `pid` is set to the pid of the Profile of the related Job
590 * - `jobid` is set to the Company of the job (this information is redundant
591 * with that of the row of profile_job for the related job)
592 * - `id` is the id of the job to which we refer (i.e `profile_job.id`)
594 * For the documentation of the phone table, please see classes/phone.php.
596 * The possible relations are as follow:
597 * An Address can be linked to a Company, a Profile, a Job
598 * A Job is linked to a Company and a Profile
602 // {{{ class ProfileAddresses [ Field ]
603 class ProfileAddresses
extends ProfileField
605 private $addresses = array();
607 public function __construct(PlIterator
$it)
609 if ($it instanceof PlInnerSubIterator
) {
610 $this->pid
= $it->value();
613 while ($addr = $it->next()) {
614 $this->addresses
[] = new Address($addr);
618 public function get($flags, $limit = null
)
622 foreach ($this->addresses
as $addr) {
624 (($flags & Profile
::ADDRESS_MAIN
) && $addr->hasFlag('current'))
626 (($flags & Profile
::ADDRESS_POSTAL
) && $addr->hasFlag('mail'))
628 (($flags & Profile
::ADDRESS_PERSO
) && $addr->link_type
== Address
::LINK_PROFILE
)
630 (($flags & Profile
::ADDRESS_PRO
) && $addr->link_type
== Address
::LINK_JOB
)
635 if ($limit != null
&& $nb == $limit) {
642 public static function fetchData(array $pids, ProfileVisibility
$visibility)
644 $data = XDB
::iterator('SELECT pa.id, pa.pid, pa.flags, pa.type AS link_type,
645 IF(pa.type = \'home\', pid, IF(pa.type = \'job\', pa.id, jobid)) AS link_id,
646 pa.text, pa.postalCode, pa.latitude, pa.longitude, pa.comment,
647 gl.name AS locality, gas.name AS subAdministrativeArea,
648 ga.name AS administrativeArea, gc.countryFR AS country
649 FROM profile_addresses AS pa
650 LEFT JOIN geoloc_localities AS gl ON (gl.id = pa.localityId)
651 LEFT JOIN geoloc_administrativeareas AS ga ON (ga.id = pa.administrativeAreaId)
652 LEFT JOIN geoloc_administrativeareas AS gas ON (gas.id = pa.subAdministrativeAreaId)
653 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pa.countryId)
654 WHERE pa.pid in {?} AND pa.pub IN {?}
655 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
656 $pids, $visibility->levels());
658 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
661 public function addPhones(ProfilePhones
$phones)
663 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
664 foreach ($p as $phone) {
665 if ($phone->linkType() == Phone
::LINK_ADDRESS
&& array_key_exists($phone->linkId(), $this->addresses
)) {
666 $this->addresses
[$phone->linkId()]->addPhone($phone);
672 // {{{ class ProfilePhones [ Field ]
673 class ProfilePhones
extends ProfileField
675 private $phones = array();
677 public function __construct(PlInnerSubIterator
$it)
679 $this->pid
= $it->value();
680 while ($phone = $it->next()) {
681 $this->phones
[] = new Phone($phone);
685 public function get($flags, $limit = null
)
689 foreach ($this->phones
as $id => $phone) {
690 if ($phone->hasFlags($flags)) {
691 $phones[$id] = $phone;
693 if ($limit != null
&& $nb == $limit) {
701 public static function fetchData(array $pids, ProfileVisibility
$visibility)
703 $it = Phone
::iterate($pids, array(), array(), $visibility->levels());
704 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
708 // {{{ class ProfileJobs [ Field ]
709 class ProfileJobs
extends ProfileField
711 private $jobs = array();
713 public function __construct(PlInnerSubIterator
$jobs)
715 $this->pid
= $jobs->value();
716 while ($job = $jobs->next()) {
717 $this->jobs
[$job['id']] = new Job($job);
721 public static function fetchData(array $pids, ProfileVisibility
$visibility)
723 CompanyList
::preload($pids);
724 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
725 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
726 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
727 pjssse.name AS subsubsector
728 FROM profile_job AS pj
729 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
730 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
731 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
732 WHERE pj.pid IN {?} AND pj.pub IN {?}
733 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
735 $visibility->levels(), $pids, $visibility->levels());
736 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
739 public function get($flags, $limit = null
)
743 foreach ($this->jobs
as $id => $job) {
746 if ($limit != null
&& $nb >= $limit) {
753 public function addPhones(ProfilePhones
$phones)
755 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
756 foreach ($p as $phone) {
757 if ($phone->linkType() == Phone
::LINK_JOB
&& array_key_exists($phone->linkId(), $this->jobs
)) {
758 $this->jobs
[$phone->linkId()]->addPhone($phone);
763 public function addAddresses(ProfileAddresses
$addresses)
765 $a = $addresses->get(Profile
::ADDRESS_PRO
);
766 foreach ($a as $address) {
767 if ($address->link_type
== Address
::LINK_JOB
&& array_key_exists($address->link_id
, $this->jobs
)) {
768 $this->jobs
[$address->link_id
]->setAddress($address);
773 public function addCompanies(array $companies)
775 foreach ($this->jobs
as $job) {
776 $this->company
= $companies[$job->jobid
];
782 // {{{ class CompanyList
785 static private $fullload = false
;
786 static private $companies = array();
788 static public function preload($pids = array())
790 if (self
::$fullload) {
795 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
796 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
802 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
803 pa.flags, pa.text, pa.postalCode, pa.countryId,
805 FROM profile_job_enum AS pje
806 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
809 $newcompanies = array();
810 while ($row = $it->next()) {
811 $cp = new Company($row);
812 $addr = new Address($row);
813 $cp->setAddress($addr);
814 if (!array_key_exists($row['id'], self
::$companies)) {
815 $newcompanies[] = $row['id'];
817 self
::$companies[$row['id']] = $cp;
821 if (count($newcompanies)) {
822 $it = Phone
::iterate(array(), array(Phone
::LINK_COMPANY
), $newcompanies);
823 while ($phone = $it->next()) {
824 self
::$companies[$phone->linkId()]->setPhone($phone);
828 if (count($pids) == 0) {
829 self
::$fullload = true
;
833 static public function get($id)
835 if (!array_key_exists($id, self
::$companies)) {
838 return self
::$companies[$id];
842 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: