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 require_once 'validations.inc.php';
211 $entreprise = ProfileValidate
::get_typed_requests($this->pid
, 'entreprise');
212 $this->company
= new Company(array('name' => $entreprise[$this->id
]->name
));
216 public function phones()
218 return $this->phones
;
221 public function address()
223 return $this->address
;
226 public function addPhone(Phone
&$phone)
228 if ($phone->linkType() == Phone
::LINK_JOB
&& $phone->linkId() == $this->id
&& $phone->pid() == $this->pid
) {
229 $this->phones
[$phone->uniqueId()] = $phone;
233 public function setAddress(Address
$address)
235 if ($address->type
== Address
::LINK_JOB
&& $address->jobid
== $this->id
&& $address->pid
== $this->pid
) {
236 $this->address
= $address;
240 public function addTerm(JobTerm
&$term)
242 $this->terms
[$term->jtid
] = $term;
255 * pid, jid, jtid, full_name
257 public function __construct($data)
259 foreach ($data as $key => $val) {
265 // {{{ class Education
277 public $school_short;
282 public $degree_short;
283 public $degree_level;
287 public function __construct(array $data)
289 foreach ($data as $key => $val) {
292 $this->flags
= new PlFlagSet($this->flags
);
297 // {{{ class ProfileEducation [ Field ]
298 class ProfileEducation
extends ProfileField
300 private $educations = array();
302 public function __construct(PlInnerSubIterator
$it)
304 $this->pid
= $it->value();
305 while ($edu = $it->next()) {
306 $this->educations
[$edu['id']] = new Education($edu);
310 public function get($flags, $limit)
312 $educations = array();
314 $year = $year['year'];
316 foreach ($this->educations
as $id => $edu) {
318 (($flags & Profile
::EDUCATION_MAIN
) && $edu->flags
->hasFlag('primary'))
320 (($flags & Profile
::EDUCATION_EXTRA
) && !$edu->flags
->hasFlag('primary'))
322 (($flags & Profile
::EDUCATION_FINISHED
) && $edu->grad_year
<= $year)
324 (($flags & Profile
::EDUCATION_CURRENT
) && $edu->grad_year
> $year)
326 ($flags & Profile
::EDUCATION_ALL
)
328 $educations[$id] = $edu;
331 if ($limit != null
&& $nb >= $limit) {
338 public static function fetchData(array $pids, ProfileVisibility
$visibility)
340 $data = XDB
::iterator('SELECT pe.id, pe.pid,
341 pe.entry_year, pe.grad_year, pe.program, pe.flags,
342 pee.name AS school, pee.abbreviation AS school_short,
343 pee.url AS school_url, gc.countryFR AS country,
344 pede.degree, pede.abbreviation AS degree_short, pede.level AS degree_level,
346 FROM profile_education AS pe
347 LEFT JOIN profile_education_enum AS pee ON (pee.id = pe.eduid)
348 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pee.country)
349 LEFT JOIN profile_education_degree_enum AS pede ON (pede.id = pe.degreeid)
350 LEFT JOIN profile_education_field_enum AS pefe ON (pefe.id = pe.fieldid)
352 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
353 NOT FIND_IN_SET(\'primary\', pe.flags), pe.entry_year, pe.id',
356 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
360 // {{{ class ProfileMedals [ Field ]
361 class ProfileMedals
extends ProfileField
363 public $medals = array();
365 public function __construct(PlInnerSubIterator
$it)
367 $this->pid
= $it->value();
368 while ($medal = $it->next()) {
369 $this->medals
[$medal['mid']] = $medal;
373 public static function fetchData(array $pids, ProfileVisibility
$visibility)
375 $data = XDB
::iterator('SELECT pm.pid, pm.mid, pm.gid, pme.text, pme.img, pmge.text AS grade
376 FROM profile_medals AS pm
377 LEFT JOIN profiles AS p ON (pm.pid = p.pid)
378 LEFT JOIN profile_medal_enum AS pme ON (pme.id = pm.mid)
379 LEFT JOIN profile_medal_grade_enum AS pmge ON (pmge.mid = pm.mid AND pmge.gid = pm.gid)
380 WHERE pm.pid IN {?} AND p.medals_pub IN {?}
381 ORDER BY ' . XDB
::formatCustomOrder('pm.pid', $pids),
382 $pids, $visibility->levels());
384 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
388 // {{{ class ProfileNetworking [ Field ]
389 class ProfileNetworking
extends ProfileField
391 private $networks = array();
393 public function __construct(PlInnerSubIterator
$it)
395 $this->pid
= $it->value();
396 while ($network = $it->next()) {
397 $network['network_type'] = new PlFlagSet($network['network_type']);
398 $this->networks
[$network['id']] = $network;
402 public static function fetchData(array $pids, ProfileVisibility
$visibility)
404 $data = XDB
::iterator('SELECT pid, id, address, pne.nwid, pne.network_type, pne.link, pne.name
405 FROM profile_networking AS pn
406 LEFT JOIN profile_networking_enum AS pne USING(nwid)
407 WHERE pid IN {?} AND pub IN {?}
408 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
410 $pids, $visibility->levels());
412 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
415 public function get($flags, $limit = null
)
419 foreach ($this->networks
as $id => $nw) {
420 if (($flags & Profile
::NETWORKING_WEB
) && $nw['network_type']->hasFlag('web') ||
421 ($flags & Profile
::NETWORKING_IM
) && $nw['network_type']->hasFlag('im') ||
422 ($flags & Profile
::NETWORKING_SOCIAL
) && $nw['network_type']->hasFlag('social') ||
423 ($flags == Profile
::NETWORKING_ALL
)) {
426 if (isset($limit) && $nb >= $limit) {
435 // {{{ class ProfileCorps [ Field ]
436 class ProfileCorps
extends ProfileField
441 public $original_name;
442 public $original_abbrev;
443 public $original_still_exists;
445 public $current_name;
446 public $current_abbrev;
447 public $current_still_exists;
448 public $current_rank;
449 public $current_rank_abbrev;
451 public function __construct(array $data)
453 foreach ($data as $key => $val) {
458 public static function fetchData(array $pids, ProfileVisibility
$visibility)
460 $data = XDB
::iterator('SELECT pc.pid, pc.original_corpsid AS original, pc.current_corpsid AS current,
461 pceo.name AS original_name, pceo.abbreviation AS original_abbrev,
462 pceo.still_exists AS original_still_exists,
463 pcec.name AS current_name, pcec.abbreviation AS current_abbrev,
464 pcec.still_exists AS current_still_exists,
465 pcrec.name AS current_rank, pcrec.abbreviation AS current_rank_abbrev,
467 FROM profile_corps AS pc
468 LEFT JOIN profile_corps_enum AS pceo ON (pceo.id = pc.original_corpsid)
469 LEFT JOIN profile_corps_enum AS pcec ON (pcec.id = pc.current_corpsid)
470 LEFT JOIN profile_corps_rank_enum AS pcrec ON (pcrec.id = pc.rankid)
471 WHERE pc.pid IN {?} AND pc.corps_pub IN {?} AND pceo.id != 1
472 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids),
473 $pids, $visibility->levels());
479 // {{{ class ProfileMentoringSectors [ Field ]
480 class ProfileMentoringSectors
extends ProfileField
482 public $sectors = array();
484 public function __construct(PlInnerSubIterator
$it)
486 $this->pid
= $it->value();
487 while ($sector = $it->next()) {
488 $this->sectors
[] = $sector;
492 public static function fetchData(array $pids, ProfileVisibility
$visibility)
494 $data = XDB
::iterator('SELECT pms.pid, pjse.name AS sector, pjsse.name AS subsector
495 FROM profile_mentor_sector AS pms
496 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pms.sectorid)
497 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pms.subsectorid)
499 ORDER BY ' . XDB
::formatCustomOrder('pms.pid', $pids),
502 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
506 // {{{ class ProfileMentoringCountries [ Field ]
507 class ProfileMentoringCountries
extends ProfileField
509 public $countries = array();
511 public function __construct(PlInnerSubIterator
$it)
513 $this->pid
= $it->value();
514 while ($country = $it->next()) {
515 $this->countries
[$country['id']] = $country['name'];
519 public static function fetchData(array $pids, ProfileVisibility
$visibility)
521 $data = XDB
::iterator('SELECT pmc.pid, pmc.country AS id, gc.countryFR AS name
522 FROM profile_mentor_country AS pmc
523 LEFT JOIN geoloc_countries AS gc ON (gc.iso_3166_1_a2 = pmc.country)
525 ORDER BY ' . XDB
::formatCustomOrder('pmc.pid', $pids),
528 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
532 // {{{ class ProfileAddresses [ Field ]
533 class ProfileAddresses
extends ProfileField
535 private $addresses = array();
537 public function __construct(PlInnerSubIterator
$it)
539 $this->pid
= $it->value();
540 while ($address = $it->next()) {
541 $this->addresses
[] = new Address($address);
545 public function get($flags, $limit = null
)
547 $addresses = array();
549 foreach ($this->addresses
as $address) {
550 if ((($flags & Profile
::ADDRESS_MAIN
) && $address->hasFlag('current'))
551 ||
(($flags & Profile
::ADDRESS_POSTAL
) && $address->hasFlag('mail'))
552 ||
(($flags & Profile
::ADDRESS_PERSO
) && $address->type
== Address
::LINK_PROFILE
)
553 ||
(($flags & Profile
::ADDRESS_PRO
) && $address->type
== Address
::LINK_JOB
)
555 $addresses[] = $address;
558 if ($limit != null
&& $nb == $limit) {
565 public static function fetchData(array $pids, ProfileVisibility
$visibility)
567 $it = Address
::iterate($pids, array(), array(), $visibility->levels());
568 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
571 public function addPhones(ProfilePhones
$phones)
573 $p = $phones->get(Profile
::PHONE_LINK_ADDRESS | Profile
::PHONE_TYPE_ANY
);
574 foreach ($p as $phone) {
575 if ($phone->linkType() == Phone
::LINK_ADDRESS
&& array_key_exists($phone->linkId(), $this->addresses
)) {
576 $this->addresses
[$phone->linkId()]->addPhone($phone);
582 // {{{ class ProfilePhones [ Field ]
583 class ProfilePhones
extends ProfileField
585 private $phones = array();
587 public function __construct(PlInnerSubIterator
$it)
589 $this->pid
= $it->value();
590 while ($phone = $it->next()) {
591 $this->phones
[] = new Phone($phone);
595 public function get($flags, $limit = null
)
599 foreach ($this->phones
as $id => $phone) {
600 if ($phone->hasFlags($flags)) {
601 $phones[$id] = $phone;
603 if ($limit != null
&& $nb == $limit) {
611 public static function fetchData(array $pids, ProfileVisibility
$visibility)
613 $it = Phone
::iterate($pids, array(), array(), $visibility->levels());
614 return PlIteratorUtils
::subIterator($it->value(), PlIteratorUtils
::arrayValueCallback('pid'));
618 // {{{ class ProfileJobs [ Field ]
619 class ProfileJobs
extends ProfileField
621 private $jobs = array();
623 public function __construct(PlInnerSubIterator
$jobs)
625 $this->pid
= $jobs->value();
626 while ($job = $jobs->next()) {
627 $this->jobs
[$job['id']] = new Job($job);
631 public static function fetchData(array $pids, ProfileVisibility
$visibility)
633 CompanyList
::preload($pids);
634 $data = XDB
::iterator('SELECT pj.id, pj.pid, pj.description, pj.url as user_site,
635 IF(pj.email_pub IN {?}, pj.email, NULL) AS user_email,
636 pj.jobid, pjse.name AS sector, pjsse.name AS subsector,
637 pjssse.name AS subsubsector
638 FROM profile_job AS pj
639 LEFT JOIN profile_job_sector_enum AS pjse ON (pjse.id = pj.sectorid)
640 LEFT JOIN profile_job_subsector_enum AS pjsse ON (pjsse.id = pj.subsectorid)
641 LEFT JOIN profile_job_subsubsector_enum AS pjssse ON (pjssse.id = pj.subsubsectorid)
642 WHERE pj.pid IN {?} AND pj.pub IN {?}
643 ORDER BY ' . XDB
::formatCustomOrder('pid', $pids) . ',
645 $visibility->levels(), $pids, $visibility->levels());
646 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
649 public function get($flags, $limit = null
)
653 foreach ($this->jobs
as $id => $job) {
656 if ($limit != null
&& $nb >= $limit) {
663 public function addPhones(ProfilePhones
$phones)
665 $p = $phones->get(Profile
::PHONE_LINK_JOB | Profile
::PHONE_TYPE_ANY
);
666 foreach ($p as $phone) {
667 if ($phone->linkType() == Phone
::LINK_JOB
&& array_key_exists($phone->linkId(), $this->jobs
)) {
668 $this->jobs
[$phone->linkId()]->addPhone($phone);
673 public function addAddresses(ProfileAddresses
$addresses)
675 $a = $addresses->get(Profile
::ADDRESS_PRO
);
676 foreach ($a as $address) {
677 if ($address->type
== Address
::LINK_JOB
&& array_key_exists($address->jobid
, $this->jobs
)) {
678 $this->jobs
[$address->jobid
]->setAddress($address);
683 public function addCompanies(array $companies)
685 foreach ($this->jobs
as $job) {
686 $this->company
= $companies[$job->jobid
];
690 public function addJobTerms(ProfileJobTerms
$jobterms)
692 $terms = $jobterms->get();
693 foreach ($terms as $term) {
694 if ($this->pid
== $term->pid
&& array_key_exists($term->jid
, $this->jobs
)) {
695 $this->jobs
[$term->jid
]->addTerm(&$term);
701 // {{{ class ProfileJobTerms [ Field ]
702 class ProfileJobTerms
extends ProfileField
704 private $jobterms = array();
706 public function __construct(PlInnerSubIterator
$it)
708 $this->pid
= $it->value();
709 while ($term = $it->next()) {
710 $this->jobterms
[] = new JobTerm($term);
714 public function get()
716 return $this->jobterms
;
719 public static function fetchData(array $pids, ProfileVisibility
$visibility)
721 $data = XDB
::iterator('SELECT jt.jtid, jte.full_name, jt.pid, jt.jid
722 FROM profile_job_term AS jt
723 INNER JOIN profile_job AS j ON (jt.pid = j.pid AND jt.jid = j.id)
724 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
725 WHERE jt.pid IN {?} AND j.pub IN {?}
726 ORDER BY ' . XDB
::formatCustomOrder('jt.pid', $pids),
727 $pids, $visibility->levels());
728 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
732 // {{{ class ProfileMentoringTerms [ Field ]
733 class ProfileMentoringTerms
extends ProfileJobTerms
735 public static function fetchData(array $pids, ProfileVisibility
$visibility)
737 $data = XDB
::iterator('SELECT mt.jtid, jte.full_name, mt.pid
738 FROM profile_mentor_term AS mt
739 LEFT JOIN profile_job_term_enum AS jte USING(jtid)
741 ORDER BY ' . XDB
::formatCustomOrder('mt.pid', $pids),
743 return PlIteratorUtils
::subIterator($data, PlIteratorUtils
::arrayValueCallback('pid'));
747 // {{{ class CompanyList
750 static private $fullload = false
;
751 static private $companies = array();
753 static public function preload($pids = array())
755 if (self
::$fullload) {
760 $join = 'LEFT JOIN profile_job ON (profile_job.jobid = pje.id)';
761 $where = 'WHERE profile_job.pid IN ' . XDB
::formatArray($pids);
767 $it = XDB
::iterator('SELECT pje.id, pje.name, pje.acronym, pje.url,
768 pa.flags, pa.text, pa.postalCode, pa.countryId,
770 FROM profile_job_enum AS pje
771 LEFT JOIN profile_addresses AS pa ON (pje.id = pa.jobid AND pa.type = \'hq\')
774 $newcompanies = array();
775 while ($row = $it->next()) {
776 $cp = new Company($row);
777 $addr = new Address($row);
778 $cp->setAddress($addr);
779 if (!array_key_exists($row['id'], self
::$companies)) {
780 $newcompanies[] = $row['id'];
782 self
::$companies[$row['id']] = $cp;
786 if (count($newcompanies)) {
787 $it = Phone
::iterate(array(), array(Phone
::LINK_COMPANY
), $newcompanies);
788 while ($phone = $it->next()) {
789 self
::$companies[$phone->linkId()]->setPhone($phone);
793 if (count($pids) == 0) {
794 self
::$fullload = true
;
798 static public function get($id)
800 if (!array_key_exists($id, self
::$companies)) {
803 return self
::$companies[$id];
807 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: